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