[29] | 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 | int main( int argc, char **argv, char **envp ) |
---|
| 33 | { |
---|
| 34 | char buf[ 1024 ]; |
---|
| 35 | FILE *fin; |
---|
| 36 | FILE *fout; |
---|
| 37 | char *p; |
---|
| 38 | int doDotC = 0; |
---|
| 39 | |
---|
| 40 | if( argc < 3 ) |
---|
| 41 | { |
---|
| 42 | fprintf( stderr, "usage: %s jambase.c Jambase ...\n", argv[0] ); |
---|
| 43 | return -1; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | if( !( fout = fopen( argv[1], "w" ) ) ) |
---|
| 47 | { |
---|
| 48 | perror( argv[1] ); |
---|
| 49 | return -1; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /* If the file ends in .c generate a C source file */ |
---|
| 53 | |
---|
| 54 | if( ( p = strrchr( argv[1], '.' ) ) && !strcmp( p, ".c" ) ) |
---|
| 55 | doDotC++; |
---|
| 56 | |
---|
| 57 | /* Now process the files */ |
---|
| 58 | |
---|
| 59 | argc -= 2, argv += 2; |
---|
| 60 | |
---|
| 61 | if( doDotC ) |
---|
| 62 | { |
---|
| 63 | fprintf( fout, "/* Generated by mkjambase from Jambase */\n" ); |
---|
| 64 | fprintf( fout, "char *jambase[] = {\n" ); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | for( ; argc--; argv++ ) |
---|
| 68 | { |
---|
| 69 | if( !( fin = fopen( *argv, "r" ) ) ) |
---|
| 70 | { |
---|
| 71 | perror( *argv ); |
---|
| 72 | return -1; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | if( doDotC ) |
---|
| 76 | { |
---|
| 77 | fprintf( fout, "/* %s */\n", *argv ); |
---|
| 78 | } |
---|
| 79 | else |
---|
| 80 | { |
---|
| 81 | fprintf( fout, "### %s ###\n", *argv ); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | while( fgets( buf, sizeof( buf ), fin ) ) |
---|
| 85 | { |
---|
| 86 | if( doDotC ) |
---|
| 87 | { |
---|
| 88 | char *p = buf; |
---|
| 89 | |
---|
| 90 | /* Strip leading whitespace. */ |
---|
| 91 | |
---|
| 92 | while( *p == ' ' || *p == '\t' || *p == '\n' ) |
---|
| 93 | p++; |
---|
| 94 | |
---|
| 95 | /* Drop comments and empty lines. */ |
---|
| 96 | |
---|
| 97 | if( *p == '#' || !*p ) |
---|
| 98 | continue; |
---|
| 99 | |
---|
| 100 | /* Copy */ |
---|
| 101 | |
---|
| 102 | putc( '"', fout ); |
---|
| 103 | |
---|
| 104 | for( ; *p && *p != '\n'; p++ ) |
---|
| 105 | switch( *p ) |
---|
| 106 | { |
---|
| 107 | case '\\': putc( '\\', fout ); putc( '\\', fout ); break; |
---|
| 108 | case '"': putc( '\\', fout ); putc( '"', fout ); break; |
---|
| 109 | case '\r': break; |
---|
| 110 | default: putc( *p, fout ); break; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | fprintf( fout, "\\n\",\n" ); |
---|
| 114 | } |
---|
| 115 | else |
---|
| 116 | { |
---|
| 117 | fprintf( fout, "%s", buf ); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | fclose( fin ); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | if( doDotC ) |
---|
| 126 | fprintf( fout, "0 };\n" ); |
---|
| 127 | |
---|
| 128 | fclose( fout ); |
---|
| 129 | |
---|
| 130 | return 0; |
---|
| 131 | } |
---|