1 /*
2  * Copyright 1993, 1995 Christopher Seiwald.
3  *
4  * This file is part of Jam - see jam.c for Copyright information.
5  */
6 
7 /*  This file is ALSO:
8  *  Copyright 2001-2004 David Abrahams.
9  *  Distributed under the Boost Software License, Version 1.0.
10  *  (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
11  */
12 
13 /*
14  * mkjambase.c - turn Jambase into a big C structure
15  *
16  * Usage: mkjambase jambase.c Jambase ...
17  *
18  * Results look like this:
19  *
20  *   char *jambase[] = {
21  *   "...\n",
22  *   ...
23  *   0 };
24  *
25  * Handles \'s and "'s specially; knows to delete blank and comment lines.
26  *
27  */
28 
29 #include <stdio.h>
30 #include <string.h>
31 
32 
main(int argc,char ** argv,char ** envp)33 int main( int argc, char * * argv, char * * envp )
34 {
35     char buf[ 1024 ];
36     FILE * fin;
37     FILE * fout;
38     char * p;
39     int doDotC = 0;
40 
41     if ( argc < 3 )
42     {
43         fprintf( stderr, "usage: %s jambase.c Jambase ...\n", argv[ 0 ] );
44         return -1;
45     }
46 
47     if ( !( fout = fopen( argv[1], "w" ) ) )
48     {
49         perror( argv[ 1 ] );
50         return -1;
51     }
52 
53     /* If the file ends in .c generate a C source file. */
54     if ( ( p = strrchr( argv[1], '.' ) ) && ( !strcmp( p, ".c" ) || !strcmp( p, ".cpp" ) ) )
55         doDotC++;
56 
57     /* Now process the files. */
58 
59     argc -= 2;
60     argv += 2;
61 
62     if ( doDotC )
63     {
64         fprintf( fout, "/* Generated by mkjambase from Jambase */\n" );
65         fprintf( fout, "const char *jambase[] = {\n" );
66     }
67 
68     for ( ; argc--; ++argv )
69     {
70         if ( !( fin = fopen( *argv, "r" ) ) )
71         {
72             perror( *argv );
73             return -1;
74         }
75 
76         if ( doDotC )
77             fprintf( fout, "/* %s */\n", *argv );
78         else
79             fprintf( fout, "### %s ###\n", *argv );
80 
81         while ( fgets( buf, sizeof( buf ), fin ) )
82         {
83             if ( doDotC )
84             {
85                 char * p = buf;
86 
87                 /* Strip leading whitespace. */
88                 while ( ( *p == ' ' ) || ( *p == '\t' ) || ( *p == '\n' ) )
89                     ++p;
90 
91                 /* Drop comments and empty lines. */
92                 if ( ( *p == '#' ) || !*p )
93                     continue;
94 
95                 /* Copy. */
96                 putc( '"', fout );
97                 for ( ; *p && ( *p != '\n' ); ++p )
98                     switch ( *p )
99                     {
100                         case '\\': putc( '\\', fout ); putc( '\\', fout ); break;
101                         case '"' : putc( '\\', fout ); putc( '"' , fout ); break;
102                         case '\r': break;
103                         default: putc( *p, fout ); break;
104                     }
105 
106                 fprintf( fout, "\\n\",\n" );
107             }
108             else
109             {
110                 fprintf( fout, "%s", buf );
111             }
112         }
113 
114         fclose( fin );
115     }
116 
117     if ( doDotC )
118         fprintf( fout, "0 };\n" );
119 
120     fclose( fout );
121 
122     return 0;
123 }
124