Fixed a small bug in debug2html. It wasn't properly checking EOF. The
[samba.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 2 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  * $Log: debug2html.c,v $
32  * Revision 1.2  1998/10/27 23:28:29  crh
33  * Fixed a small bug in debug2html.  It wasn't properly checking EOF.  The
34  * current status is "it works".  I need to add some syntax error recovery
35  * and a usage message.  Basic stuff.
36  *
37  * I've also modified Makefile.in.  If you want to compile it you'll have to
38  * do a 'make debug2html', as I used smbtorture as a model.  We can decide
39  * later if this tool is useful enough to be compiled always.
40  *
41  * BTW, a 'make realclean' fails because the bin directory isn't empty.
42  * That's because it doesn't delete optionally compiled files such as
43  * smbtorture and debug2html (and because of the CVS subdirectory, but I
44  * think that's only a problem for developers).
45  *
46  * Chris -)-----
47  *
48  * Revision 1.1  1998/10/26 23:21:37  crh
49  * Here is the simple debug parser and the debug2html converter.  Still to do:
50  *
51  *   * Debug message filtering.
52  *   * I need to add all this to Makefile.in
53  *     (If it looks at all strange I'll ask for help.)
54  *
55  * If you want to compile debug2html, you'll need to do it by hand until I
56  * make the changes to Makefile.in.  Sorry.
57  *
58  * Chris -)-----
59  *
60  * ========================================================================== **
61  */
62
63 #include "debugparse.h"
64
65 /* -------------------------------------------------------------------------- **
66  * The size of the read buffer.
67  */
68
69 #define BSIZE 1024
70
71 /* -------------------------------------------------------------------------- **
72  * Functions...
73  */
74
75 static dbg_Token modechange( dbg_Token new, dbg_Token mode )
76   /* ------------------------------------------------------------------------ **
77    * Handle a switch between header and message printing.
78    *
79    *  Input:  new   - The token value of the current token.  This indicates
80    *                  the lexical item currently being recognized.
81    *          mode  - The current mode.  This is either dbg_null or
82    *                  dbg_message.  It could really be any toggle
83    *                  (true/false, etc.)
84    *
85    *  Output: The new mode.  This will be the same as the input mode unless
86    *          there was a transition in or out of message processing.
87    *
88    *  Notes:  The purpose of the mode value is to mark the beginning and end
89    *          of the message text block.  In order to show the text in its
90    *          correct format, it must be included within a <PRE></PRE> block.
91    *
92    * ------------------------------------------------------------------------ **
93    */
94   {
95   switch( new )
96     {
97     case dbg_null:
98     case dbg_ignore:
99       return( mode );
100     case dbg_message:
101       if( dbg_message != mode )
102         {
103         /* Switching to message mode. */
104         (void)printf( "<PRE>\n" );
105         return( dbg_message );
106         }
107       break;
108     default:
109       if( dbg_message == mode )
110         {
111         /* Switching out of message mode. */
112         (void)printf( "</PRE>\n\n" );
113         return( dbg_null );
114         }
115     }
116
117   return( mode );
118   } /* modechange */
119
120 static void newblock( dbg_Token old, dbg_Token new )
121   /* ------------------------------------------------------------------------ **
122    * Handle the transition between tokens.
123    *
124    *  Input:  old - The previous token.
125    *          new - The current token.
126    *
127    *  Output: none.
128    *
129    *  Notes:  This is called whenever there is a transition from one token
130    *          type to another.  It first prints the markup tags that close
131    *          the previous token, and then the markup tags for the new
132    *          token.
133    *
134    * ------------------------------------------------------------------------ **
135    */
136   {
137   switch( old )
138     {
139     case dbg_timestamp:
140       (void)printf( ", " );
141       break;
142     case dbg_level:
143       (void)printf( "</FONT>]</B>\n   " );
144       break;
145     case dbg_sourcefile:
146       (void)printf( ":" );
147       break;
148     case dbg_lineno:
149       (void)printf( ")" );
150       break;
151     }
152
153   switch( new )
154     {
155     case dbg_timestamp:
156       (void)printf( "<B>[" );
157       break;
158     case dbg_level:
159       (void)printf( "<FONT COLOR=MAROON>" );
160       break;
161     case dbg_lineno:
162       (void)printf( "(" );
163       break;
164     }
165   } /* newblock */
166
167 static void charprint( dbg_Token tok, int c )
168   /* ------------------------------------------------------------------------ **
169    * Filter the input characters to determine what goes to output.
170    *
171    *  Input:  tok - The token value of the current character.
172    *          c   - The current character.
173    *
174    *  Output: none.
175    *
176    * ------------------------------------------------------------------------ **
177    */
178   {
179   switch( tok )
180     {
181     case dbg_ignore:
182     case dbg_header:
183       break;
184     case dbg_null:
185     case dbg_eof:
186       (void)putchar( '\n' );
187       break;
188     default:
189       switch( c )
190         {
191         case '<':
192           (void)printf( "&lt;" );
193           break;
194         case '>':
195           (void)printf( "&gt;" );
196           break;
197         case '&':
198           (void)printf( "&amp;" );
199           break;
200         case '\"':
201           (void)printf( "&#34;" );
202           break;
203         default:
204           (void)putchar( c );
205           break;
206         }
207     }
208   } /* charprint */
209
210 int main( int argc, char *argv[] )
211   /* ------------------------------------------------------------------------ **
212    * This simple program scans and parses Samba debug logs, and produces HTML
213    * output.
214    *
215    *  Input:  argc  - Currently ignored.
216    *          argv  - Currently ignored.
217    *
218    *  Output: Always zero.
219    *
220    *  Notes:  The HTML output is sent to stdout.
221    *
222    * ------------------------------------------------------------------------ **
223    */
224   {
225   int       i;
226   int       len;
227   char      bufr[BSIZE];
228   dbg_Token old   = dbg_null,
229             new   = dbg_null,
230             state = dbg_null,
231             mode  = dbg_null;
232
233   (void)printf( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n" );
234   (void)printf( "<HTML>\n<HEAD>\n" );
235   (void)printf( "  <TITLE>Samba Debug Output</TITLE>\n</HEAD>\n\n<BODY>\n" );
236
237   while( (!feof( stdin ))
238       && ((len = fread( bufr, 1, BSIZE, stdin )) > 0) )
239     {
240     for( i = 0; i < len; i++ )
241       {
242       old = new;
243       new = dbg_char2token( &state, bufr[i] );
244       if( new != old )
245         {
246         mode = modechange( new, mode );
247         newblock( old, new );
248         }
249       charprint( new, bufr[i] );
250       }
251     }
252   (void)modechange( dbg_eof, mode );
253
254   (void)printf( "</BODY>\n</HTML>\n" );
255   return( 0 );
256   } /* main */