File: ins_once.c

package info (click to toggle)
mcpp 2.5-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,544 kB
  • ctags: 28,000
  • sloc: ansic: 34,825; sh: 3,812; makefile: 120; cpp: 84; exp: 18
file content (354 lines) | stat: -rw-r--r-- 9,146 bytes parent folder | download
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * ins_once.c
 *  Quick and dirty program to insert '#pragma once' into the header files.
 *  1998/08     kmatsui
 *  2002/08     kmatsui
 *      Added -p, -o, -g option, removed -s option.
 *      Compile with -DPATH_DELIM='x' option, if the path-delimiter is any
 *          other than '/'.
 *  2004/11     kmatsui
 *      Changed '#pragma __once' to '#pragma once'.
 */

#include    "stdio.h"
#include    "stdlib.h"
#include    "string.h"
#include    "ctype.h"
#include    "errno.h"

#ifndef PATH_DELIM
#define PATH_DELIM  '/'
#endif

#define TRUE    1
#define FALSE   0

#define IFNDEF      0x100
#define IF          0x101
#define PRAGMA      0x102
#define DEFINED     0x103
#define _MCPP       0x110
#define _ONCE       0x111

void    usage( void);
void    test_a_file( char *);
void    conv_a_file( char *);
void    insert_once( FILE *, FILE *, char *);
void    prepend_once( FILE *, FILE *);
int     look_directive( FILE *);
int     get_token( char **);
void    ins_once( FILE *);

int     test;
    /* Only test files whether beginning with #ifndef or #if !defined.  */
int     pre_ansi;
/*
 * If TRUE, insert 9 lines for the old pre-ansi preprocessors.
 * If FALSE insert only '#pragma once' line for the preprocessors
 * which can accept #pragma.
 */
int     prepend;
/*
 * If TRUE, prepend '#pragma once' line to the file.
 * If FALSE, insert the line after the first #ifndef line.
 */
int     gcc;
/*
 * If TRUE, do not insert '#pragma once' line to "stddef.h".
 * This is the option for GCC family.
 */

main( argc, argv)
    int     argc;
    char    **argv;
{
    extern int      getopt( int, char **, char *);
    extern int      optind;
    extern char     *optarg;
    int     opt;
    char    *except[] = { "assert.h", "cassert", "cassert.h", NULL};
    char    *g_except[] = { "stddef.h", NULL};
    char    *arg;
    char    **ep;

    if (argc < 2)
        usage();
    while (optind < argc
            && (opt = getopt( argc, argv, "gopt")) != EOF) {
        switch (opt) {
        case 't':
            test = TRUE;
            break;
        case 'o':
            pre_ansi = TRUE;
            break;
        case 'p':
            prepend = TRUE;
            break;
        case 'g':
            gcc = TRUE;
            break;
        default:
            usage();
            break;
        }
    }
    argv += (optind - 1);
    while (*++argv) {
        if ((arg = strrchr( *argv, PATH_DELIM)) != NULL)
            arg++;
        else
            arg = *argv;
        for (ep = except; *ep; ep++) {
            if (strcmp( arg, *ep) == 0)
                goto skip;
        }
        if (gcc) {
            for (ep = g_except; *ep; ep++) {
                if (strcmp( arg, *ep) == 0)
                    goto skip;
            }
        }
        if (test)
            test_a_file( *argv);
        else
            conv_a_file( *argv);
        continue;
skip:   fprintf( stderr, "Skipped %s\n", *ep);
    }
    return 0;
}

void    usage( void)
{
    static char     *mes[] = {
   "ins_once: Insert '#pragma once' to header files except \"assert.h\"\n",
   "            and \"stddef.h\" (for GNU C).\n",
   "Usage: ins_once [-DPATH_DELIM=\\] [-t|-p|-o|-g] [header1.h [header2.h [...]]]\n",
   "    -t : Only test files whether beginning with #ifndef or #if !defined.\n",
   "    -p : Prepend the line to the file\n",
   "        (default: insert after the first #ifndef line -- for GNU C).\n",
   "    -o : For the OLD C preprocessors which cannot accept #pragma.\n",
   "    -g : Do not convert \"stddef.h\".\n",
        NULL,
    };
    char    **mesp = mes;

    while( *mesp)
        fputs( *mesp++, stderr);
    if (errno) {
        fputs( strerror( errno), stderr);
        fputc( '\n', stderr);
    }
    exit( errno);
}

void    test_a_file( fname)
    char    *fname;
/*
 * Only test the file whether it begins with #ifndef or #if !defined.
 */
{
    char    buf[ BUFSIZ];
    FILE    *fp_in;
    int     token;
    char    *cp;

    fp_in = fopen( fname, "r");
    if (fp_in == NULL)
        usage();

    while (fgets( buf, BUFSIZ, fp_in) != NULL) {
        cp = buf;
        if (get_token( &cp) == '#') {       /* The first directive  */
            if (((token = get_token( &cp)) != IFNDEF)   /* #ifndef  */
                    && (token != IF || get_token( &cp) != '!'
                        || get_token( &cp) != DEFINED)) {   /* #if ! defined*/
                fputs( fname, stderr);
                fputs( ": doesn't begin with #ifndef nor #if !defined\n",
                        stderr);
            }
            break;
        }
    }
    fclose( fp_in);
}

void    conv_a_file( fname)
    char    *fname;
/*
 * Insert '#pragma once' line to seemingly apropriate place according
 * the command-line options.
 */
{
    char    *tmp = "tmp_once";
    FILE    *fp_in, *fp_out;

    if ((fp_in = fopen( fname, "r")) == NULL)
        usage();
    if ((fp_out = fopen( tmp, "w")) == NULL)
        usage();
    fprintf( stderr, "Converting %s\n", fname);

    if (prepend)
        prepend_once( fp_in, fp_out);
    else
        insert_once( fp_in, fp_out, fname);

    fclose( fp_in);
    fclose( fp_out);
    if (remove( fname) != 0 || rename( tmp, fname) != 0)
        usage();
}

void    insert_once( fp_in, fp_out, fname)
    FILE    *fp_in, *fp_out;
    char    *fname;
/*
 * Insert '#pragma once' line after the first directive line, if the
 * directive is #ifndef or #if !defined, else append the line at the end
 * of the file.
 */
{
    char    buf[ BUFSIZ];
    int     token;
    int     no_ifndef = TRUE;
    char    *cp;

    while (fgets( buf, BUFSIZ, fp_in) != NULL) {
        fputs( buf, fp_out);
        cp = buf;
        if (get_token( &cp) == '#') {       /* The first directive  */
            if (((token = get_token( &cp)) == IFNDEF)   /* #ifndef  */
                    || (token == IF && get_token( &cp) == '!'
                        && get_token( &cp) == DEFINED)) {   /* #if ! defined*/
                no_ifndef = FALSE;
                if (! look_directive( fp_in))
                    ins_once( fp_out);
                /* Else already written in   */
            } else {                            /* Other directive  */
                fputs( fname, stderr);
                fputs( ": doesn't begin with #ifndef nor #if !defined\n",
                        stderr);
            }
            break;
        }
    }
    while (fgets( buf, BUFSIZ, fp_in) != NULL)
        fputs( buf, fp_out);
    if (no_ifndef)
        ins_once( fp_out);          /* Append the line to the file  */
}

void    prepend_once( fp_in, fp_out)
    FILE    *fp_in, *fp_out;
/*
 * Prepend the '#pragma once' line at the top of the file.
 */
{
    char    buf[ BUFSIZ];

    if (! look_directive( fp_in))
        ins_once( fp_out);          /* Prepend the line to the file */

    while (fgets( buf, BUFSIZ, fp_in) != NULL)
        fputs( buf, fp_out);
}

int     look_directive( fp)
    FILE    *fp;
/*
 * Look whether the next line is '#pragma once'.
 */
{
    char    buf[ BUFSIZ];
    long    pos;
    int     res = 0;
    int     token;
    char    *cp;

    pos = ftell( fp);
    cp = buf;
    if (fgets( buf, BUFSIZ, fp) && buf[ 0] == '\n'
            && fgets( buf, BUFSIZ, fp) && get_token( &cp) == '#') {
        if (!pre_ansi && get_token( &cp) == PRAGMA && get_token( &cp) == _ONCE)
            res = 1;
        else if (pre_ansi && get_token( &cp) == IF
                && get_token( &cp) == _MCPP)
            res = 1;
    }
    fseek( fp, pos, SEEK_SET);
    return res;
}

int     get_token( cpp)
    char    **cpp;
/* Get the next 'token', without parsing comments, literals, etc.   */
{
    int     token;
    char    *cp = *cpp;

    while (*cp != '\n' && isspace( *cp))
        cp++;
    if (memcmp( cp, "ifndef", 6) == 0) {
        token = IFNDEF;
        cp += 6;
    } else if (memcmp( cp, "if", 2) == 0) {
        token = IF;
        cp += 2;
    } else if (memcmp( cp, "pragma", 6) == 0) {
        token = PRAGMA;
        cp += 6;
    } else if (memcmp( cp, "defined", 7) == 0) {
        token = DEFINED;
        cp += 7;
    } else if (memcmp( cp, "__MCPP", 6) == 0) {
        token = _MCPP;
        cp += 11;
    } else if (memcmp( cp, "once", 6) == 0) {
        token = _ONCE;
        cp += 6;
    } else {
        token = *cp++;
    }
    *cpp = cp;
    return token;
}

void    ins_once( fp)
    FILE    *fp;
{
    static char     *once[] = {
        /* Redundant directives not to reject old preprocessors.    */
        "\n",
        "#if     __MCPP >= 2\n",
        "#ifdef  __STDC__\n",
        "#pragma once\n",
        "#else\n",
        "#ifdef  __cplusplus\n",
        "#pragma once\n",
        "#endif\n",
        "#endif\n",
        "#endif\n",
        "\n",
         NULL,
    };
    static char     *once_only[] = {
        "\n#pragma once\n\n",
        NULL,
    };
    static char    **cpp;
    char           **tpp;

    if (cpp == NULL) {                  /* Have to initialize           */
        if (pre_ansi)
            cpp = once;
        else
            cpp = once_only;
    }

    tpp = cpp;
    while (*tpp)
        fputs( *tpp++, fp);
}