summaryrefslogtreecommitdiff
path: root/rsc/source/rscpp/cpp3.c
blob: df5a2c7acf705559a3d865d9aefc465a52fa946a (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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
#include <stdio.h>
#ifdef UNX
#include <stdlib.h>
#endif
#include <ctype.h>
#include "cppdef.h"
#include "cpp.h"

#include "time.h" /* BP */

#include <string.h>

#ifndef _NO_PROTO
int AddInclude( char *pIncStr );  /* BP, 11.09.91, Forward-Deklaration */
#endif

#if (OSL_DEBUG_LEVEL > 1) && (HOST == SYS_VMS || HOST == SYS_UNIX)
#include <signal.h>
#endif

void InitCpp3()
{
}


int
openfile(char* filename)
/*
 * Open a file, add it to the linked list of open files.
 * This is called only from openfile() above.
 */
{
        register FILE           *fp;

        if ((fp = fopen(filename, "r")) == NULL) {
#if OSL_DEBUG_LEVEL > 1
            if ( debug || !bDumpDefs )
                perror(filename);
#endif
            return (FALSE);
        }
#if OSL_DEBUG_LEVEL > 1
        if (debug)
            fprintf(stderr, "Reading from \"%s\"\n", filename);
#endif
        addfile(fp, filename);
        return (TRUE);
}

void addfile(FILE* fp, char* filename)
/*
 * Initialize tables for this open file.  This is called from openfile()
 * above (for #include files), and from the entry to cpp to open the main
 * input file.  It calls a common routine, getfile() to build the FILEINFO
 * structure which is used to read characters.  (getfile() is also called
 * to setup a macro replacement.)
 */
{
        register FILEINFO       *file;
        extern FILEINFO         *getfile( int, char * );
        file = getfile(NBUFF, filename);
        file->fp = fp;                  /* Better remember FILE *       */
        file->buffer[0] = EOS;          /* Initialize for first read    */
        line = 1;                       /* Working on line 1 now        */
        wrongline = TRUE;               /* Force out initial #line      */
}

void setincdirs()
/*
 * Append system-specific directories to the include directory list.
 * Called only when cpp is started.
 */
{

#ifdef  CPP_INCLUDE
        *incend++ = CPP_INCLUDE;
#define IS_INCLUDE      1
#else
#define IS_INCLUDE      0
#endif

#if HOST == SYS_UNIX
        *incend++ = "/usr/include";
#define MAXINCLUDE      (NINCLUDE - 1 - IS_INCLUDE)
#endif

#if HOST == SYS_VMS
        extern char     *getenv();

        if (getenv("C$LIBRARY") != NULL)
            *incend++ = "C$LIBRARY:";
        *incend++ = "SYS$LIBRARY:";
#define MAXINCLUDE      (NINCLUDE - 2 - IS_INCLUDE)
#endif

#if HOST == SYS_RSX
        extern int      $$rsts;                 /* TRUE on RSTS/E       */
        extern int      $$pos;                  /* TRUE on PRO-350 P/OS */
        extern int      $$vms;                  /* TRUE on VMS compat.  */

        if ($$pos) {                            /* P/OS?                */
            *incend++ = "SY:[ZZDECUSC]";        /* C #includes          */
            *incend++ = "LB:[1,5]";             /* RSX library          */
        }
        else if ($$rsts) {                      /* RSTS/E?              */
            *incend++ = "SY:@";                 /* User-defined account */
            *incend++ = "C:";                   /* Decus-C library      */
            *incend++ = "LB:[1,1]";             /* RSX library          */
        }
        else if ($$vms) {                       /* VMS compatibility?   */
            *incend++ = "C:";
        }
        else {                                  /* Plain old RSX/IAS    */
            *incend++ = "LB:[1,1]";
        }
#define MAXINCLUDE      (NINCLUDE - 3 - IS_INCLUDE)
#endif

#if HOST == SYS_RT11
        extern int      $$rsts;                 /* RSTS/E emulation?    */

        if ($$rsts)
            *incend++ = "SY:@";                 /* User-defined account */
        *incend++ = "C:";                       /* Decus-C library disk */
        *incend++ = "SY:";                      /* System (boot) disk   */
#define MAXINCLUDE      (NINCLUDE - 3 - IS_INCLUDE)
#endif

#if HOST == SYS_UNKNOWN
/*
 * Kontext: GenMake
 * Unter DOS wird nun auch die Environment-Variable INCLUDE ausgewetet.
 * Es kommt erschwerend hinzu, dass alle Eintraege, die mit ';' getrennt
 * sind, mit in die Liste aufenommen werden muessen.
 * Dies wird mit der Funktion strtok() realisiert.
 * Vorsicht bei der Benutzung von malloc !!!
 * In savestring wird naemlich getmem() verwendet. Vermutlich kommen sich
 * die beiden Funktion in die Quere. Als ich malloc statt savestring
 * verwendete knallte es in strcpy() !
 */

#if !defined( WNT ) && ! defined UNX
        extern   char     *getenv( char *pStr ); /* BP */
#endif
                 char     *pIncGetEnv = NULL;    /* Pointer auf INCLUDE   */

        if ( ( pIncGetEnv = getenv("INCLUDE") ) != NULL )
            AddInclude( pIncGetEnv );

#define MAXINCLUDE      (NINCLUDE - 3 - IS_INCLUDE)
#endif


}

/* Kontext: Erweiterung des INCLUDE-Services
 * Bislang konnte der cpp keine Include-Angaben in der Kommandozeile
 * vertragen, bei denen die directries mit ';' getrennt wurden.
 * Dies ist auch verstaendlich, da dieses cpp fuer UNIX-Systeme
 * massgeschneidert wurde und in UNI die ';' als Zeichen zum Abschluss
 * von Kommandos gilt.
 */

int AddInclude( char* pIncStr )
{
    char     *pIncEnv    = NULL;    /* Kopie des INCLUDE     */
    char     *pIncPos;              /* wandert zum naechsten */

    pIncEnv = savestring( pIncStr );
    pIncPos = strtok( pIncEnv, ";" );

    while( pIncPos != NULL )
    {
        if (incend >= &incdir[MAXINCLUDE])
            cfatal("Too many include directories", NULLST);
        *incend++ = pIncPos;
        pIncPos   = strtok( NULL, ";" );
    }
    return( 1 );
}

int
dooptions(int argc, char** argv)
/*
 * dooptions is called to process command line arguments (-Detc).
 * It is called only at cpp startup.
 */
{
        register char           *ap;
        register DEFBUF         *dp;
        register int            c;
        int                     i, j;
        char                    *arg;
        SIZES                   *sizp;          /* For -S               */
        int                     size;           /* For -S               */
        int                     isdatum;        /* FALSE for -S*        */
        int                     endtest;        /* For -S               */

        for (i = j = 1; i < argc; i++) {
            arg = ap = argv[i];

            if (*ap++ != '-' || *ap == EOS)
            {
                    argv[j++] = argv[i];
            }
            else {
                c = *ap++;                      /* Option byte          */
                if (islower(c))                 /* Normalize case       */
                    c = toupper(c);
                switch (c) {                    /* Command character    */
                case 'C':                       /* Keep comments        */
                    cflag = TRUE;
                    keepcomments = TRUE;
                    break;

                case 'D':                       /* Define symbol        */
                    /*
                     * If the option is just "-Dfoo", make it -Dfoo=1
                     */
                    while (*ap != EOS && *ap != '=')
                        ap++;
                    if (*ap == EOS)
                        ap = "1";
                    else
                        *ap++ = EOS;
                    /*
                     * Now, save the word and its definition.
                     */
                    dp = defendel(argv[i] + 2, FALSE);
                    dp->repl = savestring(ap);
                    dp->nargs = DEF_NOARGS;
                    break;

                case 'E':                       /* Ignore non-fatal     */
                    eflag = TRUE;               /* errors.              */
                    break;

                case 'I':                       /* Include directory    */
                    AddInclude( ap );           /* BP, 11.09.91 */
                    break;

                case 'N':                       /* No predefineds       */
                    nflag++;                    /* Repeat to undefine   */
                    break;                      /* __LINE__, etc.       */

                case 'S':
                    sizp = size_table;
                    if (0 != (isdatum = (*ap != '*'))) /* If it's just -S,     */
                        endtest = T_FPTR;       /* Stop here            */
                    else {                      /* But if it's -S*      */
                        ap++;                   /* Step over '*'        */
                        endtest = 0;            /* Stop at end marker   */
                    }
                    while (sizp->bits != endtest && *ap != EOS) {
                        if (!isdigit(*ap)) {    /* Skip to next digit   */
                            ap++;
                            continue;
                        }
                        size = 0;               /* Compile the value    */
                        while (isdigit(*ap)) {
                            size *= 10;
                            size += (*ap++ - '0');
                        }
                        if (isdatum)
                            sizp->size = size;  /* Datum size           */
                        else
                            sizp->psize = size; /* Pointer size         */
                        sizp++;
                    }
                    if (sizp->bits != endtest)
                        cwarn("-S, too few values specified in %s", argv[i]);
                    else if (*ap != EOS)
                        cwarn("-S, too many values, \"%s\" unused", ap);
                    break;

                case 'U':                       /* Undefine symbol      */
                    if (defendel(ap, TRUE) == NULL)
                        cwarn("\"%s\" wasn't defined", ap);
                    break;

#if OSL_DEBUG_LEVEL > 1
                case 'X':                       /* Debug                */
                    debug = (isdigit(*ap)) ? atoi(ap) : 1;
#if (HOST == SYS_VMS || HOST == SYS_UNIX)
                    signal(SIGINT, (void (*)(int)) abort); /* Trap "interrupt" */
#endif
                    fprintf(stderr, "Debug set to %d\n", debug);
                    break;
#endif

#if OSL_DEBUG_LEVEL > 1
                case 'P':                       /* #define's dump       */
                    bDumpDefs = 1;
                    fprintf(stderr, "Dump #define's is on\n");
                    break;
#endif

                default:                        /* What is this one?    */
                    cwarn("Unknown option \"%s\"", arg);
                    fprintf(stderr, "The following options are valid:\n\
  -C\t\t\tWrite source file comments to output\n\
  -Dsymbol=value\tDefine a symbol with the given (optional) value\n\
  -Idirectory\t\tAdd a directory to the #include search list\n\
  -N\t\t\tDon't predefine target-specific names\n\
  -Stext\t\tSpecify sizes for #if sizeof\n\
  -Usymbol\t\tUndefine symbol\n");
#if OSL_DEBUG_LEVEL > 1
                    fprintf(stderr, "  -Xvalue\t\tSet internal debug flag\n");
                    fprintf(stderr, "  -P\t\t\tdump #define's\n");
#endif
                    break;
                }                       /* Switch on all options        */
            }                           /* If it's a -option            */
        }                               /* For all arguments            */
#if OSL_DEBUG_LEVEL > 1
        if ( (bDumpDefs ? j > 4 : j > 3) ) {
#else
        if (j > 3) {
#endif
            cerror(
                "Too many file arguments.  Usage: cpp [input [output]]",
                NULLST);
        }
        return (j);                     /* Return new argc              */
}

int
readoptions(char* filename, char*** pfargv)
{
        FILE           *fp;
        int c;
        int bInQuotes = 0;
        char optbuff[1024], *poptbuff;
        int fargc=0, back;
        char *fargv[PARALIMIT], **pfa;

        pfa=*pfargv=malloc(sizeof(fargv));

        poptbuff=&optbuff[0];
        filename++;
        if ((fp = fopen(filename, "r")) == NULL) {
#if OSL_DEBUG_LEVEL > 1
            if ( debug || !bDumpDefs )
                perror(filename);
#endif
            return (FALSE);
        }
        do
        {
            /*
             *  #i27914# double ticks '"' now have a duplicate function:
             *  1. they define a string ( e.g. -DFOO="baz" )
             *  2. a string can contain spaces, so -DFOO="baz zum" defines one
             *  argument no two !
             */
            c=fgetc(fp);
            if ( c != ' ' && c != CR && c != NL && c != HT && c != EOF)
            {
                *poptbuff++=(char)c;
                if( c == '"' )
                    bInQuotes = ~bInQuotes;
            }
            else
            {
                if( c != EOF && bInQuotes )
                    *poptbuff++=(char)c;
                else
                {
                    *poptbuff=EOS;
                    if (strlen(optbuff)>0)
                    {
                        pfa[fargc+1]=malloc(strlen(optbuff)+1);
                        strcpy(pfa[fargc+1],optbuff);
                        fargc++;
                        pfa[fargc+1]=0;
                        poptbuff=&optbuff[0];
                    }
                }
            }
        }
        while ( c != EOF );

        fclose(fp);
        back=dooptions(fargc+1,pfa);

        return (back);
}

#if HOST != SYS_UNIX
FILE_LOCAL void
zap_uc(char* ap)
/*
 * Dec operating systems mangle upper-lower case in command lines.
 * This routine forces the -D and -U arguments to uppercase.
 * It is called only on cpp startup by dooptions().
 */
{
        while (*ap != EOS) {
            /*
             * Don't use islower() here so it works with Multinational
             */
            if (*ap >= 'a' && *ap <= 'z')
                *ap = (char)toupper(*ap);
            ap++;
        }
}
#endif

void initdefines()
/*
 * Initialize the built-in #define's.  There are two flavors:
 *      #define decus   1               (static definitions)
 *      #define __FILE__ ??             (dynamic, evaluated by magic)
 * Called only on cpp startup.
 *
 * Note: the built-in static definitions are supressed by the -N option.
 * __LINE__, __FILE__, and __DATE__ are always present.
 */
{
        register char           **pp;
        register char           *tp;
        register DEFBUF         *dp;
        int                     i;
        time_t                  tvec;

#if !defined( WNT ) && !defined(G3)
        extern char             *ctime();
#endif

        /*
         * Predefine the built-in symbols.  Allow the
         * implementor to pre-define a symbol as "" to
         * eliminate it.
         */
        if (nflag == 0) {
            for (pp = preset; *pp != NULL; pp++) {
                if (*pp[0] != EOS) {
                    dp = defendel(*pp, FALSE);
                    dp->repl = savestring("1");
                    dp->nargs = DEF_NOARGS;
                }
            }
        }
        /*
         * The magic pre-defines (__FILE__ and __LINE__ are
         * initialized with negative argument counts.  expand()
         * notices this and calls the appropriate routine.
         * DEF_NOARGS is one greater than the first "magic" definition.
         */
        if (nflag < 2) {
            for (pp = magic, i = DEF_NOARGS; *pp != NULL; pp++) {
                dp = defendel(*pp, FALSE);
                dp->nargs = --i;
            }
#if OK_DATE
            /*
             * Define __DATE__ as today's date.
             */
            dp = defendel("__DATE__", FALSE);
            dp->repl = tp = getmem(27);
            dp->nargs = DEF_NOARGS;
            time( &tvec);
            *tp++ = '"';
            strcpy(tp, ctime(&tvec));
            tp[24] = '"';                       /* Overwrite newline    */
#endif
        }
}

#if HOST == SYS_VMS
/*
 * getredirection() is intended to aid in porting C programs
 * to VMS (Vax-11 C) which does not support '>' and '<'
 * I/O redirection.  With suitable modification, it may
 * useful for other portability problems as well.
 */

int
getredirection(argc, argv)
int             argc;
char            **argv;
/*
 * Process vms redirection arg's.  Exit if any error is seen.
 * If getredirection() processes an argument, it is erased
 * from the vector.  getredirection() returns a new argc value.
 *
 * Warning: do not try to simplify the code for vms.  The code
 * presupposes that getredirection() is called before any data is
 * read from stdin or written to stdout.
 *
 * Normal usage is as follows:
 *
 *      main(argc, argv)
 *      int             argc;
 *      char            *argv[];
 *      {
 *              argc = getredirection(argc, argv);
 *      }
 */
{
        register char           *ap;    /* Argument pointer     */
        int                     i;      /* argv[] index         */
        int                     j;      /* Output index         */
        int                     file;   /* File_descriptor      */
        extern int              errno;  /* Last vms i/o error   */

        for (j = i = 1; i < argc; i++) {   /* Do all arguments  */
            switch (*(ap = argv[i])) {
            case '<':                   /* <file                */
                if (freopen(++ap, "r", stdin) == NULL) {
                    perror(ap);         /* Can't find file      */
                    exit(errno);        /* Is a fatal error     */
                }
                break;

            case '>':                   /* >file or >>file      */
                if (*++ap == '>') {     /* >>file               */
                    /*
                     * If the file exists, and is writable by us,
                     * call freopen to append to the file (using the
                     * file's current attributes).  Otherwise, create
                     * a new file with "vanilla" attributes as if the
                     * argument was given as ">filename".
                     * access(name, 2) returns zero if we can write on
                     * the specified file.
                     */
                    if (access(++ap, 2) == 0) {
                        if (freopen(ap, "a", stdout) != NULL)
                            break;      /* Exit case statement  */
                        perror(ap);     /* Error, can't append  */
                        exit(errno);    /* After access test    */
                    }                   /* If file accessible   */
                }
                /*
                 * On vms, we want to create the file using "standard"
                 * record attributes.  creat(...) creates the file
                 * using the caller's default protection mask and
                 * "variable length, implied carriage return"
                 * attributes. dup2() associates the file with stdout.
                 */
                if ((file = creat(ap, 0, "rat=cr", "rfm=var")) == -1
                 || dup2(file, fileno(stdout)) == -1) {
                    perror(ap);         /* Can't create file    */
                    exit(errno);        /* is a fatal error     */
                }                       /* If '>' creation      */
                break;                  /* Exit case test       */

            default:
                argv[j++] = ap;         /* Not a redirector     */
                break;                  /* Exit case test       */
            }
        }                               /* For all arguments    */
        argv[j] = NULL;                 /* Terminate argv[]     */
        return (j);                     /* Return new argc      */
}
#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */