xref: /aoo41x/main/rsc/source/rscpp/cpp3.c (revision 8e2a856b)
1*8e2a856bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*8e2a856bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*8e2a856bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*8e2a856bSAndrew Rist  * distributed with this work for additional information
6*8e2a856bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*8e2a856bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*8e2a856bSAndrew Rist  * "License"); you may not use this file except in compliance
9*8e2a856bSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*8e2a856bSAndrew Rist  *
11*8e2a856bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*8e2a856bSAndrew Rist  *
13*8e2a856bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*8e2a856bSAndrew Rist  * software distributed under the License is distributed on an
15*8e2a856bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*8e2a856bSAndrew Rist  * KIND, either express or implied.  See the License for the
17*8e2a856bSAndrew Rist  * specific language governing permissions and limitations
18*8e2a856bSAndrew Rist  * under the License.
19*8e2a856bSAndrew Rist  *
20*8e2a856bSAndrew Rist  *************************************************************/
21*8e2a856bSAndrew Rist 
22*8e2a856bSAndrew Rist 
23cdf0e10cSrcweir #if defined(_MSC_VER) && (_MSC_VER > 1310)
24cdf0e10cSrcweir #define _USE_32BIT_TIME_T
25cdf0e10cSrcweir #endif
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include        <stdio.h>
28cdf0e10cSrcweir #ifdef UNX
29cdf0e10cSrcweir #include        <stdlib.h>
30cdf0e10cSrcweir #endif
31cdf0e10cSrcweir #include        <ctype.h>
32cdf0e10cSrcweir #include        "cppdef.h"
33cdf0e10cSrcweir #include        "cpp.h"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include        "time.h" /* BP */
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #ifndef _STRING_H
38cdf0e10cSrcweir #include <string.h>
39cdf0e10cSrcweir #endif
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #ifndef _NO_PROTO
42cdf0e10cSrcweir int AddInclude( char *pIncStr );  /* BP, 11.09.91, Forward-Deklaration */
43cdf0e10cSrcweir #endif
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #if (OSL_DEBUG_LEVEL > 1) && (HOST == SYS_VMS || HOST == SYS_UNIX)
46cdf0e10cSrcweir #include        <signal.h>
47cdf0e10cSrcweir #endif
48cdf0e10cSrcweir 
InitCpp3()49cdf0e10cSrcweir void InitCpp3()
50cdf0e10cSrcweir {
51cdf0e10cSrcweir }
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 
54cdf0e10cSrcweir int
openfile(char * filename)55cdf0e10cSrcweir openfile(char* filename)
56cdf0e10cSrcweir /*
57cdf0e10cSrcweir  * Open a file, add it to the linked list of open files.
58cdf0e10cSrcweir  * This is called only from openfile() above.
59cdf0e10cSrcweir  */
60cdf0e10cSrcweir {
61cdf0e10cSrcweir         register FILE           *fp;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir         if ((fp = fopen(filename, "r")) == NULL) {
64cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
65cdf0e10cSrcweir 			if ( debug || !bDumpDefs )
66cdf0e10cSrcweir 	            perror(filename);
67cdf0e10cSrcweir #endif
68cdf0e10cSrcweir             return (FALSE);
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
71cdf0e10cSrcweir         if (debug)
72cdf0e10cSrcweir             fprintf(stderr, "Reading from \"%s\"\n", filename);
73cdf0e10cSrcweir #endif
74cdf0e10cSrcweir         addfile(fp, filename);
75cdf0e10cSrcweir         return (TRUE);
76cdf0e10cSrcweir }
77cdf0e10cSrcweir 
addfile(FILE * fp,char * filename)78cdf0e10cSrcweir void addfile(FILE* fp, char* filename)
79cdf0e10cSrcweir /*
80cdf0e10cSrcweir  * Initialize tables for this open file.  This is called from openfile()
81cdf0e10cSrcweir  * above (for #include files), and from the entry to cpp to open the main
82cdf0e10cSrcweir  * input file.  It calls a common routine, getfile() to build the FILEINFO
83cdf0e10cSrcweir  * structure which is used to read characters.  (getfile() is also called
84cdf0e10cSrcweir  * to setup a macro replacement.)
85cdf0e10cSrcweir  */
86cdf0e10cSrcweir {
87cdf0e10cSrcweir         register FILEINFO       *file;
88cdf0e10cSrcweir /* #ifndef _NO_PROTO */
89cdf0e10cSrcweir                 extern FILEINFO         *getfile( int bufsize, char *filename ); /* BP */
90cdf0e10cSrcweir /* #endif */
91cdf0e10cSrcweir         file = getfile(NBUFF, filename);
92cdf0e10cSrcweir         file->fp = fp;                  /* Better remember FILE *       */
93cdf0e10cSrcweir         file->buffer[0] = EOS;          /* Initialize for first read    */
94cdf0e10cSrcweir         line = 1;                       /* Working on line 1 now        */
95cdf0e10cSrcweir         wrongline = TRUE;               /* Force out initial #line      */
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
setincdirs()98cdf0e10cSrcweir void setincdirs()
99cdf0e10cSrcweir /*
100cdf0e10cSrcweir  * Append system-specific directories to the include directory list.
101cdf0e10cSrcweir  * Called only when cpp is started.
102cdf0e10cSrcweir  */
103cdf0e10cSrcweir {
104cdf0e10cSrcweir 
105cdf0e10cSrcweir #ifdef  CPP_INCLUDE
106cdf0e10cSrcweir         *incend++ = CPP_INCLUDE;
107cdf0e10cSrcweir #define IS_INCLUDE      1
108cdf0e10cSrcweir #else
109cdf0e10cSrcweir #define IS_INCLUDE      0
110cdf0e10cSrcweir #endif
111cdf0e10cSrcweir 
112cdf0e10cSrcweir #if HOST == SYS_UNIX
113cdf0e10cSrcweir         *incend++ = "/usr/include";
114cdf0e10cSrcweir #define MAXINCLUDE      (NINCLUDE - 1 - IS_INCLUDE)
115cdf0e10cSrcweir #endif
116cdf0e10cSrcweir 
117cdf0e10cSrcweir #if HOST == SYS_VMS
118cdf0e10cSrcweir         extern char     *getenv();
119cdf0e10cSrcweir 
120cdf0e10cSrcweir         if (getenv("C$LIBRARY") != NULL)
121cdf0e10cSrcweir             *incend++ = "C$LIBRARY:";
122cdf0e10cSrcweir         *incend++ = "SYS$LIBRARY:";
123cdf0e10cSrcweir #define MAXINCLUDE      (NINCLUDE - 2 - IS_INCLUDE)
124cdf0e10cSrcweir #endif
125cdf0e10cSrcweir 
126cdf0e10cSrcweir #if HOST == SYS_RSX
127cdf0e10cSrcweir         extern int      $$rsts;                 /* TRUE on RSTS/E       */
128cdf0e10cSrcweir         extern int      $$pos;                  /* TRUE on PRO-350 P/OS */
129cdf0e10cSrcweir         extern int      $$vms;                  /* TRUE on VMS compat.  */
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         if ($$pos) {                            /* P/OS?                */
132cdf0e10cSrcweir             *incend++ = "SY:[ZZDECUSC]";        /* C #includes          */
133cdf0e10cSrcweir             *incend++ = "LB:[1,5]";             /* RSX library          */
134cdf0e10cSrcweir         }
135cdf0e10cSrcweir         else if ($$rsts) {                      /* RSTS/E?              */
136cdf0e10cSrcweir             *incend++ = "SY:@";                 /* User-defined account */
137cdf0e10cSrcweir             *incend++ = "C:";                   /* Decus-C library      */
138cdf0e10cSrcweir             *incend++ = "LB:[1,1]";             /* RSX library          */
139cdf0e10cSrcweir         }
140cdf0e10cSrcweir         else if ($$vms) {                       /* VMS compatibility?   */
141cdf0e10cSrcweir             *incend++ = "C:";
142cdf0e10cSrcweir         }
143cdf0e10cSrcweir         else {                                  /* Plain old RSX/IAS    */
144cdf0e10cSrcweir             *incend++ = "LB:[1,1]";
145cdf0e10cSrcweir         }
146cdf0e10cSrcweir #define MAXINCLUDE      (NINCLUDE - 3 - IS_INCLUDE)
147cdf0e10cSrcweir #endif
148cdf0e10cSrcweir 
149cdf0e10cSrcweir #if HOST == SYS_RT11
150cdf0e10cSrcweir         extern int      $$rsts;                 /* RSTS/E emulation?    */
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         if ($$rsts)
153cdf0e10cSrcweir             *incend++ = "SY:@";                 /* User-defined account */
154cdf0e10cSrcweir         *incend++ = "C:";                       /* Decus-C library disk */
155cdf0e10cSrcweir         *incend++ = "SY:";                      /* System (boot) disk   */
156cdf0e10cSrcweir #define MAXINCLUDE      (NINCLUDE - 3 - IS_INCLUDE)
157cdf0e10cSrcweir #endif
158cdf0e10cSrcweir 
159cdf0e10cSrcweir #if HOST == SYS_UNKNOWN
160cdf0e10cSrcweir /*
161cdf0e10cSrcweir  * BP: 25.07.91, Kontext: GenMake
162cdf0e10cSrcweir  * Unter DOS wird nun auch die Environment-Variable INCLUDE ausgewetet.
163cdf0e10cSrcweir  * Es kommt erschwerend hinzu, dass alle Eintraege, die mit ';' getrennt
164cdf0e10cSrcweir  * sind, mit in die Liste aufenommen werden muessen.
165cdf0e10cSrcweir  * Dies wird mit der Funktion strtok() realisiert.
166cdf0e10cSrcweir  * Vorsicht bei der Benutzung von malloc !!!
167cdf0e10cSrcweir  * In savestring wird naemlich getmem() verwendet. Vermutlich kommen sich
168cdf0e10cSrcweir  * die beiden Funktion in die Quere. Als ich malloc statt savestring
169cdf0e10cSrcweir  * verwendete knallte es in strcpy() !
170cdf0e10cSrcweir  */
171cdf0e10cSrcweir 
172cdf0e10cSrcweir #if !defined( ZTC ) && !defined( WNT ) && !defined(BLC) && ! defined UNX && ! defined OS2
173cdf0e10cSrcweir         extern   char     *getenv( char *pStr ); /* BP */
174cdf0e10cSrcweir #endif
175cdf0e10cSrcweir                  char     *pIncGetEnv = NULL;    /* Pointer auf INCLUDE   */
176cdf0e10cSrcweir 
177cdf0e10cSrcweir         if ( ( pIncGetEnv = getenv("INCLUDE") ) != NULL )
178cdf0e10cSrcweir             AddInclude( pIncGetEnv );
179cdf0e10cSrcweir 
180cdf0e10cSrcweir #define MAXINCLUDE      (NINCLUDE - 3 - IS_INCLUDE)
181cdf0e10cSrcweir #endif
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 
184cdf0e10cSrcweir }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir /* BP: 11.09.91, Kontext: Erweiterung des INCLUDE-Services
187cdf0e10cSrcweir  * Bislang konnte der cpp keine Include-Angaben in der Kommandozeile
188cdf0e10cSrcweir  * vertragen, bei denen die directries mit ';' getrennt wurden.
189cdf0e10cSrcweir  * Dies ist auch verstaendlich, da dieses cpp fuer UNIX-Systeme
190cdf0e10cSrcweir  * massgeschneidert wurde und in UNI die ';' als Zeichen zum Abschluss
191cdf0e10cSrcweir  * von Kommandos gilt.
192cdf0e10cSrcweir  */
193cdf0e10cSrcweir 
AddInclude(char * pIncStr)194cdf0e10cSrcweir int AddInclude( char* pIncStr )
195cdf0e10cSrcweir {
196cdf0e10cSrcweir     char     *pIncEnv    = NULL;    /* Kopie des INCLUDE     */
197cdf0e10cSrcweir     char     *pIncPos;              /* wandert zum naechsten */
198cdf0e10cSrcweir 
199cdf0e10cSrcweir     pIncEnv = savestring( pIncStr );
200cdf0e10cSrcweir     pIncPos = strtok( pIncEnv, ";" );
201cdf0e10cSrcweir 
202cdf0e10cSrcweir     while( pIncPos != NULL )
203cdf0e10cSrcweir     {
204cdf0e10cSrcweir         if (incend >= &incdir[MAXINCLUDE])
205cdf0e10cSrcweir             cfatal("Too many include directories", NULLST);
206cdf0e10cSrcweir         *incend++ = pIncPos;
207cdf0e10cSrcweir         pIncPos   = strtok( NULL, ";" );
208cdf0e10cSrcweir     }
209cdf0e10cSrcweir     return( 1 );
210cdf0e10cSrcweir }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 
215cdf0e10cSrcweir int
dooptions(int argc,char ** argv)216cdf0e10cSrcweir dooptions(int argc, char** argv)
217cdf0e10cSrcweir /*
218cdf0e10cSrcweir  * dooptions is called to process command line arguments (-Detc).
219cdf0e10cSrcweir  * It is called only at cpp startup.
220cdf0e10cSrcweir  */
221cdf0e10cSrcweir {
222cdf0e10cSrcweir         register char           *ap;
223cdf0e10cSrcweir         register DEFBUF         *dp;
224cdf0e10cSrcweir         register int            c;
225cdf0e10cSrcweir         int                     i, j;
226cdf0e10cSrcweir         char                    *arg;
227cdf0e10cSrcweir         SIZES                   *sizp;          /* For -S               */
228cdf0e10cSrcweir         int                     size;           /* For -S               */
229cdf0e10cSrcweir         int                     isdatum;        /* FALSE for -S*        */
230cdf0e10cSrcweir         int                     endtest;        /* For -S               */
231cdf0e10cSrcweir 
232cdf0e10cSrcweir         for (i = j = 1; i < argc; i++) {
233cdf0e10cSrcweir             arg = ap = argv[i];
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 			if (*ap++ != '-' || *ap == EOS)
236cdf0e10cSrcweir 			{
237cdf0e10cSrcweir 					argv[j++] = argv[i];
238cdf0e10cSrcweir 			}
239cdf0e10cSrcweir 			else {
240cdf0e10cSrcweir                 c = *ap++;                      /* Option byte          */
241cdf0e10cSrcweir                 if (islower(c))                 /* Normalize case       */
242cdf0e10cSrcweir                     c = toupper(c);
243cdf0e10cSrcweir                 switch (c) {                    /* Command character    */
244cdf0e10cSrcweir                 case 'C':                       /* Keep comments        */
245cdf0e10cSrcweir                     cflag = TRUE;
246cdf0e10cSrcweir                     keepcomments = TRUE;
247cdf0e10cSrcweir                     break;
248cdf0e10cSrcweir 
249cdf0e10cSrcweir                 case 'D':                       /* Define symbol        */
250cdf0e10cSrcweir #if HOST != SYS_UNIX
251cdf0e10cSrcweir /*                   zap_uc(ap); */             /* Force define to U.C. */
252cdf0e10cSrcweir #endif
253cdf0e10cSrcweir                     /*
254cdf0e10cSrcweir                      * If the option is just "-Dfoo", make it -Dfoo=1
255cdf0e10cSrcweir                      */
256cdf0e10cSrcweir                     while (*ap != EOS && *ap != '=')
257cdf0e10cSrcweir                         ap++;
258cdf0e10cSrcweir                     if (*ap == EOS)
259cdf0e10cSrcweir                         ap = "1";
260cdf0e10cSrcweir                     else
261cdf0e10cSrcweir                         *ap++ = EOS;
262cdf0e10cSrcweir                     /*
263cdf0e10cSrcweir                      * Now, save the word and its definition.
264cdf0e10cSrcweir                      */
265cdf0e10cSrcweir                     dp = defendel(argv[i] + 2, FALSE);
266cdf0e10cSrcweir                     dp->repl = savestring(ap);
267cdf0e10cSrcweir                     dp->nargs = DEF_NOARGS;
268cdf0e10cSrcweir                     break;
269cdf0e10cSrcweir 
270cdf0e10cSrcweir                 case 'E':                       /* Ignore non-fatal     */
271cdf0e10cSrcweir                     eflag = TRUE;               /* errors.              */
272cdf0e10cSrcweir                     break;
273cdf0e10cSrcweir 
274cdf0e10cSrcweir                 case 'I':                       /* Include directory    */
275cdf0e10cSrcweir                     AddInclude( ap );           /* BP, 11.09.91 */
276cdf0e10cSrcweir                     break;
277cdf0e10cSrcweir 
278cdf0e10cSrcweir                 case 'N':                       /* No predefineds       */
279cdf0e10cSrcweir                     nflag++;                    /* Repeat to undefine   */
280cdf0e10cSrcweir                     break;                      /* __LINE__, etc.       */
281cdf0e10cSrcweir 
282cdf0e10cSrcweir                 case 'S':
283cdf0e10cSrcweir                     sizp = size_table;
284cdf0e10cSrcweir                     if (0 != (isdatum = (*ap != '*'))) /* If it's just -S,     */
285cdf0e10cSrcweir                         endtest = T_FPTR;       /* Stop here            */
286cdf0e10cSrcweir                     else {                      /* But if it's -S*      */
287cdf0e10cSrcweir                         ap++;                   /* Step over '*'        */
288cdf0e10cSrcweir                         endtest = 0;            /* Stop at end marker   */
289cdf0e10cSrcweir                     }
290cdf0e10cSrcweir                     while (sizp->bits != endtest && *ap != EOS) {
291cdf0e10cSrcweir                         if (!isdigit(*ap)) {    /* Skip to next digit   */
292cdf0e10cSrcweir                             ap++;
293cdf0e10cSrcweir                             continue;
294cdf0e10cSrcweir                         }
295cdf0e10cSrcweir                         size = 0;               /* Compile the value    */
296cdf0e10cSrcweir                         while (isdigit(*ap)) {
297cdf0e10cSrcweir                             size *= 10;
298cdf0e10cSrcweir                             size += (*ap++ - '0');
299cdf0e10cSrcweir                         }
300cdf0e10cSrcweir                         if (isdatum)
301cdf0e10cSrcweir                             sizp->size = size;  /* Datum size           */
302cdf0e10cSrcweir                         else
303cdf0e10cSrcweir                             sizp->psize = size; /* Pointer size         */
304cdf0e10cSrcweir                         sizp++;
305cdf0e10cSrcweir                     }
306cdf0e10cSrcweir                     if (sizp->bits != endtest)
307cdf0e10cSrcweir                         cwarn("-S, too few values specified in %s", argv[i]);
308cdf0e10cSrcweir                     else if (*ap != EOS)
309cdf0e10cSrcweir                         cwarn("-S, too many values, \"%s\" unused", ap);
310cdf0e10cSrcweir                     break;
311cdf0e10cSrcweir 
312cdf0e10cSrcweir                 case 'U':                       /* Undefine symbol      */
313cdf0e10cSrcweir #if HOST != SYS_UNIX
314cdf0e10cSrcweir /*                    zap_uc(ap);*/
315cdf0e10cSrcweir #endif
316cdf0e10cSrcweir                     if (defendel(ap, TRUE) == NULL)
317cdf0e10cSrcweir                         cwarn("\"%s\" wasn't defined", ap);
318cdf0e10cSrcweir                     break;
319cdf0e10cSrcweir 
320cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
321cdf0e10cSrcweir                 case 'X':                       /* Debug                */
322cdf0e10cSrcweir                     debug = (isdigit(*ap)) ? atoi(ap) : 1;
323cdf0e10cSrcweir #if (HOST == SYS_VMS || HOST == SYS_UNIX)
324cdf0e10cSrcweir                     signal(SIGINT, (void (*)(int)) abort); /* Trap "interrupt" */
325cdf0e10cSrcweir #endif
326cdf0e10cSrcweir                     fprintf(stderr, "Debug set to %d\n", debug);
327cdf0e10cSrcweir                     break;
328cdf0e10cSrcweir #endif
329cdf0e10cSrcweir 
330cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
331cdf0e10cSrcweir                 case 'P':                       /* #define's dump       */
332cdf0e10cSrcweir                     bDumpDefs = 1;
333cdf0e10cSrcweir                     fprintf(stderr, "Dump #define's is on\n");
334cdf0e10cSrcweir                     break;
335cdf0e10cSrcweir #endif
336cdf0e10cSrcweir 
337cdf0e10cSrcweir                 default:                        /* What is this one?    */
338cdf0e10cSrcweir                     cwarn("Unknown option \"%s\"", arg);
339cdf0e10cSrcweir                     fprintf(stderr, "The following options are valid:\n\
340cdf0e10cSrcweir   -C\t\t\tWrite source file comments to output\n\
341cdf0e10cSrcweir   -Dsymbol=value\tDefine a symbol with the given (optional) value\n\
342cdf0e10cSrcweir   -Idirectory\t\tAdd a directory to the #include search list\n\
343cdf0e10cSrcweir   -N\t\t\tDon't predefine target-specific names\n\
344cdf0e10cSrcweir   -Stext\t\tSpecify sizes for #if sizeof\n\
345cdf0e10cSrcweir   -Usymbol\t\tUndefine symbol\n");
346cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
347cdf0e10cSrcweir                     fprintf(stderr, "  -Xvalue\t\tSet internal debug flag\n");
348cdf0e10cSrcweir                     fprintf(stderr, "  -P\t\t\tdump #define's\n");
349cdf0e10cSrcweir #endif
350cdf0e10cSrcweir                     break;
351cdf0e10cSrcweir                 }                       /* Switch on all options        */
352cdf0e10cSrcweir             }                           /* If it's a -option            */
353cdf0e10cSrcweir         }                               /* For all arguments            */
354cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
355cdf0e10cSrcweir 		if ( (bDumpDefs ? j > 4 : j > 3) ) {
356cdf0e10cSrcweir #else
357cdf0e10cSrcweir         if (j > 3) {
358cdf0e10cSrcweir #endif
359cdf0e10cSrcweir             cerror(
360cdf0e10cSrcweir                 "Too many file arguments.  Usage: cpp [input [output]]",
361cdf0e10cSrcweir                 NULLST);
362cdf0e10cSrcweir         }
363cdf0e10cSrcweir 		return (j);                     /* Return new argc              */
364cdf0e10cSrcweir }
365cdf0e10cSrcweir 
366cdf0e10cSrcweir int
367cdf0e10cSrcweir readoptions(char* filename, char*** pfargv)
368cdf0e10cSrcweir {
369cdf0e10cSrcweir         FILE           *fp;
370cdf0e10cSrcweir 		int c;
371cdf0e10cSrcweir         int bInQuotes = 0;
372cdf0e10cSrcweir 		char optbuff[1024], *poptbuff;
373cdf0e10cSrcweir 		int fargc=0, back;
374cdf0e10cSrcweir 		char *fargv[PARALIMIT], **pfa;
375cdf0e10cSrcweir 
376cdf0e10cSrcweir 		pfa=*pfargv=malloc(sizeof(fargv));
377cdf0e10cSrcweir 
378cdf0e10cSrcweir 		poptbuff=&optbuff[0];
379cdf0e10cSrcweir 		filename++;
380cdf0e10cSrcweir 		if ((fp = fopen(filename, "r")) == NULL) {
381cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
382cdf0e10cSrcweir 			if ( debug || !bDumpDefs )
383cdf0e10cSrcweir 	            perror(filename);
384cdf0e10cSrcweir #endif
385cdf0e10cSrcweir             return (FALSE);
386cdf0e10cSrcweir         }
387cdf0e10cSrcweir 		do
388cdf0e10cSrcweir 		{
389cdf0e10cSrcweir             /*
390cdf0e10cSrcweir              *  #i27914# double ticks '"' now have a duplicate function:
391cdf0e10cSrcweir              *  1. they define a string ( e.g. -DFOO="baz" )
392cdf0e10cSrcweir              *  2. a string can contain spaces, so -DFOO="baz zum" defines one
393cdf0e10cSrcweir              *  argument no two !
394cdf0e10cSrcweir              */
395cdf0e10cSrcweir 			c=fgetc(fp);
396cdf0e10cSrcweir 			if ( c != ' ' && c != CR && c != NL && c != HT && c != EOF)
397cdf0e10cSrcweir 			{
398cdf0e10cSrcweir 				*poptbuff++=(char)c;
399cdf0e10cSrcweir                 if( c == '"' )
400cdf0e10cSrcweir                     bInQuotes = ~bInQuotes;
401cdf0e10cSrcweir 			}
402cdf0e10cSrcweir 			else
403cdf0e10cSrcweir 			{
404cdf0e10cSrcweir                 if( c != EOF && bInQuotes )
405cdf0e10cSrcweir                     *poptbuff++=(char)c;
406cdf0e10cSrcweir                 else
407cdf0e10cSrcweir                 {
408cdf0e10cSrcweir                     *poptbuff=EOS;
409cdf0e10cSrcweir                     if (strlen(optbuff)>0)
410cdf0e10cSrcweir                     {
411cdf0e10cSrcweir                         pfa[fargc+1]=malloc(strlen(optbuff)+1);
412cdf0e10cSrcweir                         strcpy(pfa[fargc+1],optbuff);
413cdf0e10cSrcweir                         fargc++;
414cdf0e10cSrcweir                         pfa[fargc+1]=0;
415cdf0e10cSrcweir                         poptbuff=&optbuff[0];
416cdf0e10cSrcweir                     }
417cdf0e10cSrcweir                 }
418cdf0e10cSrcweir 			}
419cdf0e10cSrcweir 		}
420cdf0e10cSrcweir 		while ( c != EOF );
421cdf0e10cSrcweir 
422cdf0e10cSrcweir 		fclose(fp);
423cdf0e10cSrcweir 		back=dooptions(fargc+1,pfa);
424cdf0e10cSrcweir 
425cdf0e10cSrcweir 		return (back);
426cdf0e10cSrcweir }
427cdf0e10cSrcweir 
428cdf0e10cSrcweir 
429cdf0e10cSrcweir 
430cdf0e10cSrcweir #if HOST != SYS_UNIX
431cdf0e10cSrcweir FILE_LOCAL void
432cdf0e10cSrcweir zap_uc(char* ap)
433cdf0e10cSrcweir /*
434cdf0e10cSrcweir  * Dec operating systems mangle upper-lower case in command lines.
435cdf0e10cSrcweir  * This routine forces the -D and -U arguments to uppercase.
436cdf0e10cSrcweir  * It is called only on cpp startup by dooptions().
437cdf0e10cSrcweir  */
438cdf0e10cSrcweir {
439cdf0e10cSrcweir         while (*ap != EOS) {
440cdf0e10cSrcweir             /*
441cdf0e10cSrcweir              * Don't use islower() here so it works with Multinational
442cdf0e10cSrcweir              */
443cdf0e10cSrcweir             if (*ap >= 'a' && *ap <= 'z')
444cdf0e10cSrcweir                 *ap = (char)toupper(*ap);
445cdf0e10cSrcweir             ap++;
446cdf0e10cSrcweir         }
447cdf0e10cSrcweir }
448cdf0e10cSrcweir #endif
449cdf0e10cSrcweir 
450cdf0e10cSrcweir void initdefines()
451cdf0e10cSrcweir /*
452cdf0e10cSrcweir  * Initialize the built-in #define's.  There are two flavors:
453cdf0e10cSrcweir  *      #define decus   1               (static definitions)
454cdf0e10cSrcweir  *      #define __FILE__ ??             (dynamic, evaluated by magic)
455cdf0e10cSrcweir  * Called only on cpp startup.
456cdf0e10cSrcweir  *
457cdf0e10cSrcweir  * Note: the built-in static definitions are supressed by the -N option.
458cdf0e10cSrcweir  * __LINE__, __FILE__, and __DATE__ are always present.
459cdf0e10cSrcweir  */
460cdf0e10cSrcweir {
461cdf0e10cSrcweir         register char           **pp;
462cdf0e10cSrcweir         register char           *tp;
463cdf0e10cSrcweir         register DEFBUF         *dp;
464cdf0e10cSrcweir         int                     i;
465cdf0e10cSrcweir         long                    tvec;
466cdf0e10cSrcweir 
467cdf0e10cSrcweir #if !defined( ZTC ) && !defined( WNT ) && !defined(BLC) && !defined(G3)
468cdf0e10cSrcweir         extern char             *ctime();
469cdf0e10cSrcweir #endif
470cdf0e10cSrcweir 
471cdf0e10cSrcweir         /*
472cdf0e10cSrcweir          * Predefine the built-in symbols.  Allow the
473cdf0e10cSrcweir          * implementor to pre-define a symbol as "" to
474cdf0e10cSrcweir          * eliminate it.
475cdf0e10cSrcweir          */
476cdf0e10cSrcweir         if (nflag == 0) {
477cdf0e10cSrcweir             for (pp = preset; *pp != NULL; pp++) {
478cdf0e10cSrcweir                 if (*pp[0] != EOS) {
479cdf0e10cSrcweir                     dp = defendel(*pp, FALSE);
480cdf0e10cSrcweir                     dp->repl = savestring("1");
481cdf0e10cSrcweir                     dp->nargs = DEF_NOARGS;
482cdf0e10cSrcweir                 }
483cdf0e10cSrcweir             }
484cdf0e10cSrcweir         }
485cdf0e10cSrcweir         /*
486cdf0e10cSrcweir          * The magic pre-defines (__FILE__ and __LINE__ are
487cdf0e10cSrcweir          * initialized with negative argument counts.  expand()
488cdf0e10cSrcweir          * notices this and calls the appropriate routine.
489cdf0e10cSrcweir          * DEF_NOARGS is one greater than the first "magic" definition.
490cdf0e10cSrcweir          */
491cdf0e10cSrcweir         if (nflag < 2) {
492cdf0e10cSrcweir             for (pp = magic, i = DEF_NOARGS; *pp != NULL; pp++) {
493cdf0e10cSrcweir                 dp = defendel(*pp, FALSE);
494cdf0e10cSrcweir                 dp->nargs = --i;
495cdf0e10cSrcweir             }
496cdf0e10cSrcweir #if OK_DATE
497cdf0e10cSrcweir             /*
498cdf0e10cSrcweir              * Define __DATE__ as today's date.
499cdf0e10cSrcweir              */
500cdf0e10cSrcweir             dp = defendel("__DATE__", FALSE);
501cdf0e10cSrcweir             dp->repl = tp = getmem(27);
502cdf0e10cSrcweir             dp->nargs = DEF_NOARGS;
503cdf0e10cSrcweir             time( (time_t*)&tvec);
504cdf0e10cSrcweir             *tp++ = '"';
505cdf0e10cSrcweir             strcpy(tp, ctime((const time_t*)&tvec));
506cdf0e10cSrcweir             tp[24] = '"';                       /* Overwrite newline    */
507cdf0e10cSrcweir #endif
508cdf0e10cSrcweir         }
509cdf0e10cSrcweir }
510cdf0e10cSrcweir 
511cdf0e10cSrcweir #if HOST == SYS_VMS
512cdf0e10cSrcweir /*
513cdf0e10cSrcweir  * getredirection() is intended to aid in porting C programs
514cdf0e10cSrcweir  * to VMS (Vax-11 C) which does not support '>' and '<'
515cdf0e10cSrcweir  * I/O redirection.  With suitable modification, it may
516cdf0e10cSrcweir  * useful for other portability problems as well.
517cdf0e10cSrcweir  */
518cdf0e10cSrcweir 
519cdf0e10cSrcweir int
520cdf0e10cSrcweir getredirection(argc, argv)
521cdf0e10cSrcweir int             argc;
522cdf0e10cSrcweir char            **argv;
523cdf0e10cSrcweir /*
524cdf0e10cSrcweir  * Process vms redirection arg's.  Exit if any error is seen.
525cdf0e10cSrcweir  * If getredirection() processes an argument, it is erased
526cdf0e10cSrcweir  * from the vector.  getredirection() returns a new argc value.
527cdf0e10cSrcweir  *
528cdf0e10cSrcweir  * Warning: do not try to simplify the code for vms.  The code
529cdf0e10cSrcweir  * presupposes that getredirection() is called before any data is
530cdf0e10cSrcweir  * read from stdin or written to stdout.
531cdf0e10cSrcweir  *
532cdf0e10cSrcweir  * Normal usage is as follows:
533cdf0e10cSrcweir  *
534cdf0e10cSrcweir  *      main(argc, argv)
535cdf0e10cSrcweir  *      int             argc;
536cdf0e10cSrcweir  *      char            *argv[];
537cdf0e10cSrcweir  *      {
538cdf0e10cSrcweir  *              argc = getredirection(argc, argv);
539cdf0e10cSrcweir  *      }
540cdf0e10cSrcweir  */
541cdf0e10cSrcweir {
542cdf0e10cSrcweir         register char           *ap;    /* Argument pointer     */
543cdf0e10cSrcweir         int                     i;      /* argv[] index         */
544cdf0e10cSrcweir         int                     j;      /* Output index         */
545cdf0e10cSrcweir         int                     file;   /* File_descriptor      */
546cdf0e10cSrcweir         extern int              errno;  /* Last vms i/o error   */
547cdf0e10cSrcweir 
548cdf0e10cSrcweir         for (j = i = 1; i < argc; i++) {   /* Do all arguments  */
549cdf0e10cSrcweir             switch (*(ap = argv[i])) {
550cdf0e10cSrcweir             case '<':                   /* <file                */
551cdf0e10cSrcweir                 if (freopen(++ap, "r", stdin) == NULL) {
552cdf0e10cSrcweir                     perror(ap);         /* Can't find file      */
553cdf0e10cSrcweir                     exit(errno);        /* Is a fatal error     */
554cdf0e10cSrcweir                 }
555cdf0e10cSrcweir                 break;
556cdf0e10cSrcweir 
557cdf0e10cSrcweir             case '>':                   /* >file or >>file      */
558cdf0e10cSrcweir                 if (*++ap == '>') {     /* >>file               */
559cdf0e10cSrcweir                     /*
560cdf0e10cSrcweir                      * If the file exists, and is writable by us,
561cdf0e10cSrcweir                      * call freopen to append to the file (using the
562cdf0e10cSrcweir                      * file's current attributes).  Otherwise, create
563cdf0e10cSrcweir                      * a new file with "vanilla" attributes as if the
564cdf0e10cSrcweir                      * argument was given as ">filename".
565cdf0e10cSrcweir                      * access(name, 2) returns zero if we can write on
566cdf0e10cSrcweir                      * the specified file.
567cdf0e10cSrcweir                      */
568cdf0e10cSrcweir                     if (access(++ap, 2) == 0) {
569cdf0e10cSrcweir                         if (freopen(ap, "a", stdout) != NULL)
570cdf0e10cSrcweir                             break;      /* Exit case statement  */
571cdf0e10cSrcweir                         perror(ap);     /* Error, can't append  */
572cdf0e10cSrcweir                         exit(errno);    /* After access test    */
573cdf0e10cSrcweir                     }                   /* If file accessable   */
574cdf0e10cSrcweir                 }
575cdf0e10cSrcweir                 /*
576cdf0e10cSrcweir                  * On vms, we want to create the file using "standard"
577cdf0e10cSrcweir                  * record attributes.  creat(...) creates the file
578cdf0e10cSrcweir                  * using the caller's default protection mask and
579cdf0e10cSrcweir                  * "variable length, implied carriage return"
580cdf0e10cSrcweir                  * attributes. dup2() associates the file with stdout.
581cdf0e10cSrcweir                  */
582cdf0e10cSrcweir                 if ((file = creat(ap, 0, "rat=cr", "rfm=var")) == -1
583cdf0e10cSrcweir                  || dup2(file, fileno(stdout)) == -1) {
584cdf0e10cSrcweir                     perror(ap);         /* Can't create file    */
585cdf0e10cSrcweir                     exit(errno);        /* is a fatal error     */
586cdf0e10cSrcweir                 }                       /* If '>' creation      */
587cdf0e10cSrcweir                 break;                  /* Exit case test       */
588cdf0e10cSrcweir 
589cdf0e10cSrcweir             default:
590cdf0e10cSrcweir                 argv[j++] = ap;         /* Not a redirector     */
591cdf0e10cSrcweir                 break;                  /* Exit case test       */
592cdf0e10cSrcweir             }
593cdf0e10cSrcweir         }                               /* For all arguments    */
594cdf0e10cSrcweir         argv[j] = NULL;                 /* Terminate argv[]     */
595cdf0e10cSrcweir         return (j);                     /* Return new argc      */
596cdf0e10cSrcweir }
597cdf0e10cSrcweir #endif
598