1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
27 #include "time.h" /* BP */
31 #if (OSL_DEBUG_LEVEL > 1) && (HOST == SYS_UNIX)
36 * Open a file, add it to the linked list of open files.
37 * This is called only from openfile() above.
39 int openfile(char* filename
)
43 if ((fp
= fopen(filename
, "r")) == NULL
)
45 #if OSL_DEBUG_LEVEL > 1
46 if ( debug
|| !bDumpDefs
)
51 #if OSL_DEBUG_LEVEL > 1
53 fprintf(stderr
, "Reading from \"%s\"\n", filename
);
55 addfile(fp
, filename
);
60 * Initialize tables for this open file. This is called from openfile()
61 * above (for #include files), and from the entry to cpp to open the main
62 * input file. It calls a common routine, getfile() to build the FILEINFO
63 * structure which is used to read characters. (getfile() is also called
64 * to setup a macro replacement.)
66 void addfile(FILE* fp
, char* filename
)
70 file
= getfile(NBUFF
, filename
);
71 file
->fp
= fp
; /* Better remember FILE * */
72 file
->buffer
[0] = EOS
; /* Initialize for first read */
73 line
= 1; /* Working on line 1 now */
74 wrongline
= TRUE
; /* Force out initial #line */
78 * Append system-specific directories to the include directory list.
79 * Called only when cpp is started.
85 *incend
++ = CPP_INCLUDE
;
92 *incend
++ = "/usr/include";
93 #define MAXINCLUDE (NINCLUDE - 1 - IS_INCLUDE)
97 #if HOST == SYS_UNKNOWN
100 * Unter DOS wird nun auch die Environment-Variable INCLUDE ausgewetet.
101 * Es kommt erschwerend hinzu, dass alle Eintraege, die mit ';' getrennt
102 * sind, mit in die Liste aufenommen werden muessen.
103 * Dies wird mit der Funktion strtok() realisiert.
104 * Vorsicht bei der Benutzung von malloc !!!
105 * In savestring wird naemlich getmem() verwendet. Vermutlich kommen sich
106 * die beiden Funktion in die Quere. Als ich malloc statt savestring
107 * verwendete knallte es in strcpy() !
110 #if !defined( WNT ) && ! defined UNX
111 extern char* getenv( char *pStr
); /* BP */
113 char* pIncGetEnv
= NULL
; /* Pointer auf INCLUDE */
115 if ( ( pIncGetEnv
= getenv("INCLUDE") ) != NULL
)
116 AddInclude( pIncGetEnv
);
118 #define MAXINCLUDE (NINCLUDE - 3 - IS_INCLUDE)
123 /* Kontext: Erweiterung des INCLUDE-Services
124 * Bislang konnte der cpp keine Include-Angaben in der Kommandozeile
125 * vertragen, bei denen die directries mit ';' getrennt wurden.
126 * Dies ist auch verstaendlich, da dieses cpp fuer UNIX-Systeme
127 * massgeschneidert wurde und in UNI die ';' als Zeichen zum Abschluss
128 * von Kommandos gilt.
131 int AddInclude( char* pIncStr
)
133 char* pIncEnv
= NULL
; /* Kopie des INCLUDE */
134 char* pIncPos
; /* wandert zum naechsten */
136 pIncEnv
= savestring( pIncStr
);
137 pIncPos
= strtok( pIncEnv
, ";" );
139 while( pIncPos
!= NULL
)
141 if (incend
>= &incdir
[MAXINCLUDE
])
142 cfatal("Too many include directories", NULLST
);
144 pIncPos
= strtok( NULL
, ";" );
146 /* coverity[leaked_storage] - we know this leaks, but it doesn't matter in this short lived utility */
151 * dooptions is called to process command line arguments (-Detc).
152 * It is called only at cpp startup.
154 int dooptions(int argc
, char** argv
)
161 SIZES
* sizp
; /* For -S */
162 int size
; /* For -S */
163 int isdatum
; /* FALSE for -S* */
164 int endtest
; /* For -S */
166 for (i
= j
= 1; i
< argc
; i
++)
170 if (*ap
++ != '-' || *ap
== EOS
)
176 c
= *ap
++; /* Option byte */
177 if (islower(c
)) /* Normalize case */
179 switch (c
) /* Command character */
181 case 'C': /* Keep comments */
186 case 'D': /* Define symbol */
188 * If the option is just "-Dfoo", make it -Dfoo=1
190 while (*ap
!= EOS
&& *ap
!= '=')
197 * Now, save the word and its definition.
199 dp
= defendel(argv
[i
] + 2, FALSE
);
200 dp
->repl
= savestring(ap
);
201 dp
->nargs
= DEF_NOARGS
;
204 case 'E': /* Ignore non-fatal */
205 eflag
= TRUE
; /* errors. */
208 case 'I': /* Include directory */
209 AddInclude( ap
); /* BP, 11.09.91 */
212 case 'N': /* No predefineds */
213 nflag
++; /* Repeat to undefine */
214 break; /* __LINE__, etc. */
218 if (0 != (isdatum
= (*ap
!= '*'))) /* If it's just -S, */
219 endtest
= T_FPTR
; /* Stop here */
220 else /* But if it's -S* */
222 ap
++; /* Step over '*' */
223 endtest
= 0; /* Stop at end marker */
225 while (sizp
->bits
!= endtest
&& *ap
!= EOS
)
227 if (!isdigit(*ap
)) /* Skip to next digit */
232 size
= 0; /* Compile the value */
236 size
+= (*ap
++ - '0');
239 sizp
->size
= size
; /* Datum size */
241 sizp
->psize
= size
; /* Pointer size */
244 if (sizp
->bits
!= endtest
)
245 cwarn("-S, too few values specified in %s", argv
[i
]);
247 cwarn("-S, too many values, \"%s\" unused", ap
);
250 case 'U': /* Undefine symbol */
251 if (defendel(ap
, TRUE
) == NULL
)
252 cwarn("\"%s\" wasn't defined", ap
);
255 #if OSL_DEBUG_LEVEL > 1
256 case 'X': /* Debug */
257 debug
= (isdigit(*ap
)) ? atoi(ap
) : 1;
258 #if (HOST == SYS_UNIX)
259 signal(SIGINT
, (void (*)(int)) abort
); /* Trap "interrupt" */
261 fprintf(stderr
, "Debug set to %d\n", debug
);
265 #if OSL_DEBUG_LEVEL > 1
266 case 'P': /* #define's dump */
268 fprintf(stderr
, "Dump #define's is on\n");
272 default: /* What is this one? */
273 cwarn("Unknown option \"%s\"", arg
);
274 fprintf(stderr
, "The following options are valid:\n\
275 -C\t\t\tWrite source file comments to output\n\
276 -Dsymbol=value\tDefine a symbol with the given (optional) value\n\
277 -Idirectory\t\tAdd a directory to the #include search list\n\
278 -N\t\t\tDon't predefine target-specific names\n\
279 -Stext\t\tSpecify sizes for #if sizeof\n\
280 -Usymbol\t\tUndefine symbol\n");
281 #if OSL_DEBUG_LEVEL > 1
282 fprintf(stderr
, " -Xvalue\t\tSet internal debug flag\n");
283 fprintf(stderr
, " -P\t\t\tdump #define's\n");
286 } /* Switch on all options */
287 } /* If it's a -option */
288 } /* For all arguments */
289 #if OSL_DEBUG_LEVEL > 1
290 if ( (bDumpDefs
? j
> 4 : j
> 3) )
295 cerror( "Too many file arguments. Usage: cpp [input [output]]",
298 return (j
); /* Return new argc */
301 int readoptions(char* filename
, char*** pfargv
)
310 char* fargv
[PARALIMIT
];
313 pfa
= *pfargv
= malloc(sizeof(fargv
));
315 poptbuff
= &optbuff
[0];
317 if ((fp
= fopen(filename
, "r")) == NULL
)
319 #if OSL_DEBUG_LEVEL > 1
320 if ( debug
|| !bDumpDefs
)
328 * #i27914# double ticks '"' now have a duplicate function:
329 * 1. they define a string ( e.g. -DFOO="baz" )
330 * 2. a string can contain spaces, so -DFOO="baz zum" defines one
334 if ( c
!= ' ' && c
!= CR
&& c
!= NL
&& c
!= HT
&& c
!= EOF
)
336 *poptbuff
++ = (char)c
;
338 bInQuotes
= ~bInQuotes
;
342 if( c
!= EOF
&& bInQuotes
)
343 *poptbuff
++ = (char)c
;
347 if (optbuff
[0] != '\0')
349 pfa
[fargc
+ 1] = strdup(optbuff
);
352 poptbuff
= &optbuff
[0];
360 back
=dooptions(fargc
+1,pfa
);
368 * Dec operating systems mangle upper-lower case in command lines.
369 * This routine forces the -D and -U arguments to uppercase.
370 * It is called only on cpp startup by dooptions().
372 FILE_LOCAL
void zap_uc(char* ap
)
377 * Don't use islower() here so it works with Multinational
379 if (*ap
>= 'a' && *ap
<= 'z')
380 *ap
= (char)toupper(*ap
);
387 * Initialize the built-in #define's. There are two flavors:
388 * #define decus 1 (static definitions)
389 * #define __FILE__ ?? (dynamic, evaluated by magic)
390 * Called only on cpp startup.
392 * Note: the built-in static definitions are suppressed by the -N option.
393 * __LINE__, __FILE__, and __DATE__ are always present.
404 * Predefine the built-in symbols. Allow the
405 * implementor to pre-define a symbol as "" to
410 for (pp
= preset
; *pp
!= NULL
; pp
++)
414 dp
= defendel(*pp
, FALSE
);
415 dp
->repl
= savestring("1");
416 dp
->nargs
= DEF_NOARGS
;
421 * The magic pre-defines (__FILE__ and __LINE__ are
422 * initialized with negative argument counts. expand()
423 * notices this and calls the appropriate routine.
424 * DEF_NOARGS is one greater than the first "magic" definition.
428 for (pp
= magic
, i
= DEF_NOARGS
; *pp
!= NULL
; pp
++)
430 dp
= defendel(*pp
, FALSE
);
435 * Define __DATE__ as today's date.
437 dp
= defendel("__DATE__", FALSE
);
438 dp
->repl
= tp
= getmem(27);
439 dp
->nargs
= DEF_NOARGS
;
442 strcpy(tp
, ctime(&tvec
));
443 tp
[24] = '"'; /* Overwrite newline */
450 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */