fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / rsc / source / rscpp / cpp3.c
blob60762d8a188d116eba7a0d672b6365354f1f8a11
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
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"
27 #include "time.h" /* BP */
29 #include <string.h>
31 #if (OSL_DEBUG_LEVEL > 1) && (HOST == SYS_UNIX)
32 #include <signal.h>
33 #endif
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)
41 FILE* fp;
43 if ((fp = fopen(filename, "r")) == NULL)
45 #if OSL_DEBUG_LEVEL > 1
46 if ( debug || !bDumpDefs )
47 perror(filename);
48 #endif
49 return (FALSE);
51 #if OSL_DEBUG_LEVEL > 1
52 if (debug)
53 fprintf(stderr, "Reading from \"%s\"\n", filename);
54 #endif
55 addfile(fp, filename);
56 return (TRUE);
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)
68 FILEINFO* file;
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.
81 void setincdirs()
84 #ifdef CPP_INCLUDE
85 *incend++ = CPP_INCLUDE;
86 #define IS_INCLUDE 1
87 #else
88 #define IS_INCLUDE 0
89 #endif
91 #if HOST == SYS_UNIX
92 *incend++ = "/usr/include";
93 #define MAXINCLUDE (NINCLUDE - 1 - IS_INCLUDE)
94 #endif
97 #if HOST == SYS_UNKNOWN
99 * Kontext: GenMake
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 */
112 #endif
113 char* pIncGetEnv = NULL; /* Pointer auf INCLUDE */
115 if ( ( pIncGetEnv = getenv("INCLUDE") ) != NULL )
116 AddInclude( pIncGetEnv );
118 #define MAXINCLUDE (NINCLUDE - 3 - IS_INCLUDE)
119 #endif
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);
143 *incend++ = pIncPos;
144 pIncPos = strtok( NULL, ";" );
146 /* coverity[leaked_storage] - we know this leaks, but it doesn't matter in this short lived utility */
147 return 1;
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)
156 char* ap;
157 DEFBUF* dp;
158 int c;
159 int i, j;
160 char* arg;
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++)
168 arg = ap = argv[i];
170 if (*ap++ != '-' || *ap == EOS)
172 argv[j++] = argv[i];
174 else
176 c = *ap++; /* Option byte */
177 if (islower(c)) /* Normalize case */
178 c = toupper(c);
179 switch (c) /* Command character */
181 case 'C': /* Keep comments */
182 cflag = TRUE;
183 keepcomments = TRUE;
184 break;
186 case 'D': /* Define symbol */
188 * If the option is just "-Dfoo", make it -Dfoo=1
190 while (*ap != EOS && *ap != '=')
191 ap++;
192 if (*ap == EOS)
193 ap = "1";
194 else
195 *ap++ = EOS;
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;
202 break;
204 case 'E': /* Ignore non-fatal */
205 eflag = TRUE; /* errors. */
206 break;
208 case 'I': /* Include directory */
209 AddInclude( ap ); /* BP, 11.09.91 */
210 break;
212 case 'N': /* No predefineds */
213 nflag++; /* Repeat to undefine */
214 break; /* __LINE__, etc. */
216 case 'S':
217 sizp = size_table;
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 */
229 ap++;
230 continue;
232 size = 0; /* Compile the value */
233 while (isdigit(*ap))
235 size *= 10;
236 size += (*ap++ - '0');
238 if (isdatum)
239 sizp->size = size; /* Datum size */
240 else
241 sizp->psize = size; /* Pointer size */
242 sizp++;
244 if (sizp->bits != endtest)
245 cwarn("-S, too few values specified in %s", argv[i]);
246 else if (*ap != EOS)
247 cwarn("-S, too many values, \"%s\" unused", ap);
248 break;
250 case 'U': /* Undefine symbol */
251 if (defendel(ap, TRUE) == NULL)
252 cwarn("\"%s\" wasn't defined", ap);
253 break;
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" */
260 #endif
261 fprintf(stderr, "Debug set to %d\n", debug);
262 break;
263 #endif
265 #if OSL_DEBUG_LEVEL > 1
266 case 'P': /* #define's dump */
267 bDumpDefs = 1;
268 fprintf(stderr, "Dump #define's is on\n");
269 break;
270 #endif
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");
284 #endif
285 break;
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) )
291 #else
292 if (j > 3)
293 #endif
295 cerror( "Too many file arguments. Usage: cpp [input [output]]",
296 NULLST);
298 return (j); /* Return new argc */
301 int readoptions(char* filename, char*** pfargv)
303 FILE* fp;
304 int c;
305 int bInQuotes = 0;
306 char optbuff[1024];
307 char* poptbuff;
308 int fargc=0;
309 int back;
310 char* fargv[PARALIMIT];
311 char** pfa;
313 pfa = *pfargv = malloc(sizeof(fargv));
315 poptbuff = &optbuff[0];
316 filename++;
317 if ((fp = fopen(filename, "r")) == NULL)
319 #if OSL_DEBUG_LEVEL > 1
320 if ( debug || !bDumpDefs )
321 perror(filename);
322 #endif
323 return (FALSE);
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
331 * argument no two !
333 c = fgetc(fp);
334 if ( c != ' ' && c != CR && c != NL && c != HT && c != EOF)
336 *poptbuff++ = (char)c;
337 if( c == '"' )
338 bInQuotes = ~bInQuotes;
340 else
342 if( c != EOF && bInQuotes )
343 *poptbuff++ = (char)c;
344 else
346 *poptbuff = EOS;
347 if (optbuff[0] != '\0')
349 pfa[fargc + 1] = strdup(optbuff);
350 fargc++;
351 pfa[fargc + 1] = 0;
352 poptbuff = &optbuff[0];
357 while ( c != EOF );
359 fclose(fp);
360 back=dooptions(fargc+1,pfa);
362 return (back);
365 #if HOST != SYS_UNIX
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)
374 while (*ap != EOS)
377 * Don't use islower() here so it works with Multinational
379 if (*ap >= 'a' && *ap <= 'z')
380 *ap = (char)toupper(*ap);
381 ap++;
384 #endif
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.
395 void initdefines()
397 char** pp;
398 char* tp;
399 DEFBUF* dp;
400 int i;
401 time_t tvec;
404 * Predefine the built-in symbols. Allow the
405 * implementor to pre-define a symbol as "" to
406 * eliminate it.
408 if (nflag == 0)
410 for (pp = preset; *pp != NULL; pp++)
412 if (*pp[0] != EOS)
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.
426 if (nflag < 2)
428 for (pp = magic, i = DEF_NOARGS; *pp != NULL; pp++)
430 dp = defendel(*pp, FALSE);
431 dp->nargs = --i;
433 #if OK_DATE
435 * Define __DATE__ as today's date.
437 dp = defendel("__DATE__", FALSE);
438 dp->repl = tp = getmem(27);
439 dp->nargs = DEF_NOARGS;
440 time( &tvec);
441 *tp++ = '"';
442 strcpy(tp, ctime(&tvec));
443 tp[24] = '"'; /* Overwrite newline */
444 #endif
450 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */