Sorry that this is going so slowly.
[samba.git] / source / 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  * $Revision: 1.6 $
32  *
33  * ========================================================================== **
34  */
35
36 #include "includes.h"
37
38 /* -------------------------------------------------------------------------- **
39  * The size of the read buffer.
40  */
41
42 #define DBG_BSIZE 1024
43
44 /* -------------------------------------------------------------------------- **
45  * Functions...
46  */
47
48 static dbg_Token modechange( dbg_Token new, dbg_Token mode )
49   /* ------------------------------------------------------------------------ **
50    * Handle a switch between header and message printing.
51    *
52    *  Input:  new   - The token value of the current token.  This indicates
53    *                  the lexical item currently being recognized.
54    *          mode  - The current mode.  This is either dbg_null or
55    *                  dbg_message.  It could really be any toggle
56    *                  (true/false, etc.)
57    *
58    *  Output: The new mode.  This will be the same as the input mode unless
59    *          there was a transition in or out of message processing.
60    *
61    *  Notes:  The purpose of the mode value is to mark the beginning and end
62    *          of the message text block.  In order to show the text in its
63    *          correct format, it must be included within a <PRE></PRE> block.
64    *
65    * ------------------------------------------------------------------------ **
66    */
67   {
68   switch( new )
69     {
70     case dbg_null:
71     case dbg_ignore:
72       return( mode );
73     case dbg_message:
74       if( dbg_message != mode )
75         {
76         /* Switching to message mode. */
77         (void)printf( "<PRE>\n" );
78         return( dbg_message );
79         }
80       break;
81     default:
82       if( dbg_message == mode )
83         {
84         /* Switching out of message mode. */
85         (void)printf( "</PRE>\n\n" );
86         return( dbg_null );
87         }
88     }
89
90   return( mode );
91   } /* modechange */
92
93 static void newblock( dbg_Token old, dbg_Token new )
94   /* ------------------------------------------------------------------------ **
95    * Handle the transition between tokens.
96    *
97    *  Input:  old - The previous token.
98    *          new - The current token.
99    *
100    *  Output: none.
101    *
102    *  Notes:  This is called whenever there is a transition from one token
103    *          type to another.  It first prints the markup tags that close
104    *          the previous token, and then the markup tags for the new
105    *          token.
106    *
107    * ------------------------------------------------------------------------ **
108    */
109   {
110   switch( old )
111     {
112     case dbg_timestamp:
113       (void)printf( ",</B>" );
114       break;
115     case dbg_level:
116       (void)printf( "</FONT>]</B>\n   " );
117       break;
118     case dbg_sourcefile:
119       (void)printf( ":" );
120       break;
121     case dbg_lineno:
122       (void)printf( ")" );
123       break;
124     }
125
126   switch( new )
127     {
128     case dbg_timestamp:
129       (void)printf( "<B>[" );
130       break;
131     case dbg_level:
132       (void)printf( " <B><FONT COLOR=MAROON>" );
133       break;
134     case dbg_lineno:
135       (void)printf( "(" );
136       break;
137     }
138   } /* newblock */
139
140 static void charprint( dbg_Token tok, int c )
141   /* ------------------------------------------------------------------------ **
142    * Filter the input characters to determine what goes to output.
143    *
144    *  Input:  tok - The token value of the current character.
145    *          c   - The current character.
146    *
147    *  Output: none.
148    *
149    * ------------------------------------------------------------------------ **
150    */
151   {
152   switch( tok )
153     {
154     case dbg_ignore:
155     case dbg_header:
156       break;
157     case dbg_null:
158     case dbg_eof:
159       (void)putchar( '\n' );
160       break;
161     default:
162       switch( c )
163         {
164         case '<':
165           (void)printf( "&lt;" );
166           break;
167         case '>':
168           (void)printf( "&gt;" );
169           break;
170         case '&':
171           (void)printf( "&amp;" );
172           break;
173         case '\"':
174           (void)printf( "&#34;" );
175           break;
176         default:
177           (void)putchar( c );
178           break;
179         }
180     }
181   } /* charprint */
182
183 int main( int argc, char *argv[] )
184   /* ------------------------------------------------------------------------ **
185    * This simple program scans and parses Samba debug logs, and produces HTML
186    * output.
187    *
188    *  Input:  argc  - Currently ignored.
189    *          argv  - Currently ignored.
190    *
191    *  Output: Always zero.
192    *
193    *  Notes:  The HTML output is sent to stdout.
194    *
195    * ------------------------------------------------------------------------ **
196    */
197   {
198   int       i;
199   int       len;
200   char      bufr[DBG_BSIZE];
201   dbg_Token old   = dbg_null,
202             new   = dbg_null,
203             state = dbg_null,
204             mode  = dbg_null;
205
206   (void)printf( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n" );
207   (void)printf( "<HTML>\n<HEAD>\n" );
208   (void)printf( "  <TITLE>Samba Debug Output</TITLE>\n</HEAD>\n\n<BODY>\n" );
209
210   while( (!feof( stdin ))
211       && ((len = fread( bufr, 1, DBG_BSIZE, stdin )) > 0) )
212     {
213     for( i = 0; i < len; i++ )
214       {
215       old = new;
216       new = dbg_char2token( &state, bufr[i] );
217       if( new != old )
218         {
219         mode = modechange( new, mode );
220         newblock( old, new );
221         }
222       charprint( new, bufr[i] );
223       }
224     }
225   (void)modechange( dbg_eof, mode );
226
227   (void)printf( "</BODY>\n</HTML>\n" );
228   return( 0 );
229   } /* main */