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