1 /* ========================================================================== **
4 * Copyright (C) 1998 by Christopher R. Hertel
6 * Email: crh@ubiqx.mn.org
8 * -------------------------------------------------------------------------- **
9 * Parse Samba debug logs (2.0 & greater) and output the results as HTML.
10 * -------------------------------------------------------------------------- **
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.
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.
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.
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 * -------------------------------------------------------------------------- **
33 * ========================================================================== **
38 /* -------------------------------------------------------------------------- **
45 /* -------------------------------------------------------------------------- **
46 * The size of the read buffer.
49 #define DBG_BSIZE 1024
51 /* -------------------------------------------------------------------------- **
55 static dbg_Token modechange( dbg_Token new, dbg_Token mode )
56 /* ------------------------------------------------------------------------ **
57 * Handle a switch between header and message printing.
59 * Input: new - The token value of the current token. This indicates
60 * the lexical item currently being recognized.
61 * mode - The current mode. This is either dbg_null or
62 * dbg_message. It could really be any toggle
65 * Output: The new mode. This will be the same as the input mode unless
66 * there was a transition in or out of message processing.
68 * Notes: The purpose of the mode value is to mark the beginning and end
69 * of the message text block. In order to show the text in its
70 * correct format, it must be included within a <PRE></PRE> block.
72 * ------------------------------------------------------------------------ **
81 if( dbg_message != mode )
83 /* Switching to message mode. */
84 (void)fprintf( outfile, "<PRE>\n" );
85 return( dbg_message );
89 if( dbg_message == mode )
91 /* Switching out of message mode. */
92 (void)fprintf( outfile, "</PRE>\n\n" );
100 static void newblock( dbg_Token old, dbg_Token new )
101 /* ------------------------------------------------------------------------ **
102 * Handle the transition between tokens.
104 * Input: old - The previous token.
105 * new - The current token.
109 * Notes: This is called whenever there is a transition from one token
110 * type to another. It first prints the markup tags that close
111 * the previous token, and then the markup tags for the new
114 * ------------------------------------------------------------------------ **
120 (void)fprintf( outfile, ",</B>" );
123 (void)fprintf( outfile, "</FONT>]</B>\n " );
126 (void)fprintf( outfile, ":" );
129 (void)fprintf( outfile, ")" );
138 (void)fprintf( outfile, "<B>[" );
141 (void)fprintf( outfile, " <B><FONT COLOR=MAROON>" );
144 (void)fprintf( outfile, "(" );
151 static void charprint( dbg_Token tok, int c )
152 /* ------------------------------------------------------------------------ **
153 * Filter the input characters to determine what goes to output.
155 * Input: tok - The token value of the current character.
156 * c - The current character.
160 * ------------------------------------------------------------------------ **
170 (void)putc( '\n', outfile );
176 (void)fprintf( outfile, "<" );
179 (void)fprintf( outfile, ">" );
182 (void)fprintf( outfile, "&" );
185 (void)fprintf( outfile, """ );
188 (void)putc( c, outfile );
194 static void convert( void )
195 /* ------------------------------------------------------------------------ **
196 * Read the input logfile, converting the entries to HTML.
200 * Notes: Reads from the global infile, writes to the global outfile.
201 * These default to stdin and stdout, respectively.
203 * ------------------------------------------------------------------------ **
208 char bufr[DBG_BSIZE];
209 dbg_Token old = dbg_null,
214 while( (!feof( infile ))
215 && ((len = fread( bufr, 1, DBG_BSIZE, infile )) > 0) )
217 for( i = 0; i < len; i++ )
220 new = dbg_char2token( &state, bufr[i] );
223 mode = modechange( new, mode );
224 newblock( old, new );
226 charprint( new, bufr[i] );
229 (void)modechange( dbg_eof, mode );
233 static void usage( void )
234 /* ------------------------------------------------------------------------ **
235 * Prints a usage message on stderr, then gently exits.
238 * Output: none. Exits with return code of 0.
240 * ------------------------------------------------------------------------ **
243 fprintf( stderr, "This utility converts Samba log files " );
244 fprintf( stderr, "into HTML documents.\n" );
245 fprintf( stderr, "Usage:\n" );
246 fprintf( stderr, " debug2html <infile> <outfile>\n" );
250 static FILE *carefull_fopen( const char *path, const char *type )
251 /* ------------------------------------------------------------------------ **
252 * Checks for leading '-' characters, which are generically regarded as
253 * flags. Also exits the program gracefully should the file fail to open.
255 * Input: path - pathname of the file to open.
256 * type - open mode. See fopen(3S).
258 * Output: Pointer to open file.
260 * ------------------------------------------------------------------------ **
265 if( '-' == path[0] || '\0' == path[0] )
268 tmp = sys_fopen( path, type );
271 fprintf( stderr, "Error opening file %s: %s\n", path, strerror(errno) );
275 } /* carefull_fopen */
277 int main( int argc, char *argv[] )
278 /* ------------------------------------------------------------------------ **
279 * This simple program scans and parses Samba debug logs, and produces HTML
282 * Input: argc - Argument count.
283 * argv[1] - Input file name.
284 * argv[2] - Output file name.
285 * A '-' character by itself means use defaults (i.e.,
286 * <stdin> or <stdout> depending upon the argument.
287 * A string beginning with '-' and containing more than
288 * that one character will generate a usage message.
290 * Output: An exit value of 1 is returned if an error was encountered
291 * while opening a file, else 0.
293 * Notes: The HTML output is sent to stdout.
295 * ------------------------------------------------------------------------ **
304 if( argc > 1 && 0 != strcmp( argv[1], "-" ) )
305 infile = carefull_fopen( argv[1], "r" );
307 if( argc > 2 && 0 != strcmp( argv[2], "-" ) )
308 infile = carefull_fopen( argv[2], "w" );
310 (void)fprintf( outfile,
311 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n" );
312 (void)fprintf( outfile, "<HTML>\n<HEAD>\n" );
313 (void)fprintf( outfile,
314 " <TITLE>Samba Log</TITLE>\n</HEAD>\n\n<BODY>\n" );
318 (void)fprintf( outfile, "</BODY>\n</HTML>\n" );