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