r23779: Change from v2 or later to v3 or later.
[bbaumbach/samba-autobuild/.git] / source3 / utils / debug2html.c
1 /* ========================================================================== **
2  *                                debug2html.c
3  *
4  * Copyright (C) 1998 by Christopher R. Hertel
5  *
6  * Email: crh@ubiqx.mn.org
7  *
8  * -------------------------------------------------------------------------- **
9  * Parse Samba debug logs (2.0 & greater) and output the results as HTML.
10  * -------------------------------------------------------------------------- **
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 3 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  * -------------------------------------------------------------------------- **
27  * This program provides an example of the use of debugparse.c, and also
28  * does a decent job of converting Samba logs into HTML.
29  * -------------------------------------------------------------------------- **
30  *
31  * Revision 1.4  1998/11/13 03:37:01  tridge
32  * fixes for OSF1 compilation
33  *
34  * Revision 1.3  1998/10/28 20:33:35  crh
35  * I've moved the debugparse module files into the ubiqx directory because I
36  * know that 'make proto' will ignore them there.  The debugparse.h header
37  * file is included in includes.h, and includes.h is included in debugparse.c,
38  * so all of the pieces "see" each other.  I've compiled and tested this,
39  * and it does seem to work.  It's the same compromise model I used when
40  * adding the ubiqx modules into the system, which is why I put it all into
41  * the same directory.
42  *
43  * Chris -)-----
44  *
45  * Revision 1.1  1998/10/26 23:21:37  crh
46  * Here is the simple debug parser and the debug2html converter.  Still to do:
47  *
48  *   * Debug message filtering.
49  *   * I need to add all this to Makefile.in
50  *     (If it looks at all strange I'll ask for help.)
51  *
52  * If you want to compile debug2html, you'll need to do it by hand until I
53  * make the changes to Makefile.in.  Sorry.
54  *
55  * Chris -)-----
56  *
57  * ========================================================================== **
58  */
59
60 #include "debugparse.h"
61
62 /* -------------------------------------------------------------------------- **
63  * The size of the read buffer.
64  */
65
66 #define DBG_BSIZE 1024
67
68 /* -------------------------------------------------------------------------- **
69  * Functions...
70  */
71
72 static dbg_Token modechange( dbg_Token newmode, dbg_Token mode )
73   /* ------------------------------------------------------------------------ **
74    * Handle a switch between header and message printing.
75    *
76    *  Input:  new   - The token value of the current token.  This indicates
77    *                  the lexical item currently being recognized.
78    *          mode  - The current mode.  This is either dbg_null or
79    *                  dbg_message.  It could really be any toggle
80    *                  (true/false, etc.)
81    *
82    *  Output: The new mode.  This will be the same as the input mode unless
83    *          there was a transition in or out of message processing.
84    *
85    *  Notes:  The purpose of the mode value is to mark the beginning and end
86    *          of the message text block.  In order to show the text in its
87    *          correct format, it must be included within a <PRE></PRE> block.
88    *
89    * ------------------------------------------------------------------------ **
90    */
91   {
92   switch( newmode )
93     {
94     case dbg_null:
95     case dbg_ignore:
96       return( mode );
97     case dbg_message:
98       if( dbg_message != mode )
99         {
100         /* Switching to message mode. */
101         (void)printf( "<PRE>\n" );
102         return( dbg_message );
103         }
104       break;
105     default:
106       if( dbg_message == mode )
107         {
108         /* Switching out of message mode. */
109         (void)printf( "</PRE>\n\n" );
110         return( dbg_null );
111         }
112     }
113
114   return( mode );
115   } /* modechange */
116
117 static void newblock( dbg_Token old, dbg_Token newtok )
118   /* ------------------------------------------------------------------------ **
119    * Handle the transition between tokens.
120    *
121    *  Input:  old - The previous token.
122    *          new - The current token.
123    *
124    *  Output: none.
125    *
126    *  Notes:  This is called whenever there is a transition from one token
127    *          type to another.  It first prints the markup tags that close
128    *          the previous token, and then the markup tags for the new
129    *          token.
130    *
131    * ------------------------------------------------------------------------ **
132    */
133   {
134   switch( old )
135     {
136     case dbg_timestamp:
137       (void)printf( ",</B>" );
138       break;
139     case dbg_level:
140       (void)printf( "</FONT>]</B>\n   " );
141       break;
142     case dbg_sourcefile:
143       (void)printf( ":" );
144       break;
145     case dbg_lineno:
146       (void)printf( ")" );
147       break;
148     default:
149       break;
150     }
151
152   switch( newtok )
153     {
154     case dbg_timestamp:
155       (void)printf( "<B>[" );
156       break;
157     case dbg_level:
158       (void)printf( " <B><FONT COLOR=MAROON>" );
159       break;
160     case dbg_lineno:
161       (void)printf( "(" );
162       break;
163     default:
164       break;
165     }
166   } /* newblock */
167
168 static void charprint( dbg_Token tok, int c )
169   /* ------------------------------------------------------------------------ **
170    * Filter the input characters to determine what goes to output.
171    *
172    *  Input:  tok - The token value of the current character.
173    *          c   - The current character.
174    *
175    *  Output: none.
176    *
177    * ------------------------------------------------------------------------ **
178    */
179   {
180   switch( tok )
181     {
182     case dbg_ignore:
183     case dbg_header:
184       break;
185     case dbg_null:
186     case dbg_eof:
187       (void)putchar( '\n' );
188       break;
189     default:
190       switch( c )
191         {
192         case '<':
193           (void)printf( "&lt;" );
194           break;
195         case '>':
196           (void)printf( "&gt;" );
197           break;
198         case '&':
199           (void)printf( "&amp;" );
200           break;
201         case '\"':
202           (void)printf( "&#34;" );
203           break;
204         default:
205           (void)putchar( c );
206           break;
207         }
208     }
209   } /* charprint */
210
211 int main( int argc, char *argv[] )
212   /* ------------------------------------------------------------------------ **
213    * This simple program scans and parses Samba debug logs, and produces HTML
214    * output.
215    *
216    *  Input:  argc  - Currently ignored.
217    *          argv  - Currently ignored.
218    *
219    *  Output: Always zero.
220    *
221    *  Notes:  The HTML output is sent to stdout.
222    *
223    * ------------------------------------------------------------------------ **
224    */
225   {
226   int       i;
227   int       len;
228   char      bufr[DBG_BSIZE];
229   dbg_Token old   = dbg_null,
230             newtok = dbg_null,
231             state = dbg_null,
232             mode  = dbg_null;
233
234   (void)printf( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n" );
235   (void)printf( "<HTML>\n<HEAD>\n" );
236   (void)printf( "  <TITLE>Samba Debug Output</TITLE>\n</HEAD>\n\n<BODY>\n" );
237
238   while( (!feof( stdin ))
239       && ((len = fread( bufr, 1, DBG_BSIZE, stdin )) > 0) )
240     {
241     for( i = 0; i < len; i++ )
242       {
243       old = newtok;
244       newtok = dbg_char2token( &state, bufr[i] );
245       if( newtok != old )
246         {
247         mode = modechange( newtok, mode );
248         newblock( old, newtok );
249         }
250       charprint( newtok, bufr[i] );
251       }
252     }
253   (void)modechange( dbg_eof, mode );
254
255   (void)printf( "</BODY>\n</HTML>\n" );
256   return( 0 );
257   } /* main */