summaryrefslogtreecommitdiff
path: root/jam-files/engine/mkjambase.c
blob: cdf5998200ad783b6e59d06eb8a11c5626073721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright 1993, 1995 Christopher Seiwald.
 *
 * This file is part of Jam - see jam.c for Copyright information.
 */

/*  This file is ALSO:
 *  Copyright 2001-2004 David Abrahams.
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
 */

/*
 * mkjambase.c - turn Jambase into a big C structure
 *
 * Usage: mkjambase jambase.c Jambase ...
 *
 * Results look like this:
 *
 *   char *jambase[] = {
 *   "...\n",
 *   ...
 *   0 };
 *
 * Handles \'s and "'s specially; knows to delete blank and comment lines.
 *
 */

#include <stdio.h>
#include <string.h>


int main( int argc, char * * argv, char * * envp )
{
    char buf[ 1024 ];
    FILE * fin;
    FILE * fout;
    char * p;
    int doDotC = 0;

    if ( argc < 3 )
    {
        fprintf( stderr, "usage: %s jambase.c Jambase ...\n", argv[ 0 ] );
        return -1;
    }

    if ( !( fout = fopen( argv[1], "w" ) ) )
    {
        perror( argv[ 1 ] );
        return -1;
    }

    /* If the file ends in .c generate a C source file. */
    if ( ( p = strrchr( argv[1], '.' ) ) && !strcmp( p, ".c" ) )
        doDotC++;

    /* Now process the files. */

    argc -= 2;
    argv += 2;

    if ( doDotC )
    {
        fprintf( fout, "/* Generated by mkjambase from Jambase */\n" );
        fprintf( fout, "char *jambase[] = {\n" );
    }

    for ( ; argc--; ++argv )
    {
        if ( !( fin = fopen( *argv, "r" ) ) )
        {
            perror( *argv );
            return -1;
        }

        if ( doDotC )
            fprintf( fout, "/* %s */\n", *argv );
        else
            fprintf( fout, "### %s ###\n", *argv );

        while ( fgets( buf, sizeof( buf ), fin ) )
        {
            if ( doDotC )
            {
                char * p = buf;

                /* Strip leading whitespace. */
                while ( ( *p == ' ' ) || ( *p == '\t' ) || ( *p == '\n' ) )
                    ++p;

                /* Drop comments and empty lines. */
                if ( ( *p == '#' ) || !*p )
                    continue;

                /* Copy. */
                putc( '"', fout );
                for ( ; *p && ( *p != '\n' ); ++p )
                    switch ( *p )
                    {
                        case '\\': putc( '\\', fout ); putc( '\\', fout ); break;
                        case '"' : putc( '\\', fout ); putc( '"' , fout ); break;
                        case '\r': break;
                        default: putc( *p, fout ); break;
                    }

                fprintf( fout, "\\n\",\n" );
            }
            else
            {
                fprintf( fout, "%s", buf );
            }
        }

        fclose( fin );
    }

    if ( doDotC )
        fprintf( fout, "0 };\n" );

    fclose( fout );

    return 0;
}