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