LCOV - code coverage report
Current view: top level - rsc/source/rscpp - cpp3.c (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 86 127 67.7 %
Date: 2015-06-13 12:38:46 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          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             : #if (OSL_DEBUG_LEVEL > 1) && (HOST == SYS_UNIX)
      32             : #include <signal.h>
      33             : #endif
      34             : 
      35             : /*
      36             :  * Open a file, add it to the linked list of open files.
      37             :  * This is called only from openfile() above.
      38             :  */
      39       13626 : int openfile(char* filename)
      40             : {
      41             :     FILE* fp;
      42             : 
      43       13626 :     if ((fp = fopen(filename, "r")) == NULL)
      44             :     {
      45             : #if OSL_DEBUG_LEVEL > 1
      46             :         if ( debug || !bDumpDefs )
      47             :             perror(filename);
      48             : #endif
      49        9478 :         return (FALSE);
      50             :     }
      51             : #if OSL_DEBUG_LEVEL > 1
      52             :     if (debug)
      53             :         fprintf(stderr, "Reading from \"%s\"\n", filename);
      54             : #endif
      55        4148 :     addfile(fp, filename);
      56        4148 :     return (TRUE);
      57             : }
      58             : 
      59             : /*
      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.)
      65             :  */
      66        4509 : void addfile(FILE* fp, char* filename)
      67             : {
      68             :     FILEINFO* file;
      69             : 
      70        4509 :     file = getfile(NBUFF, filename);
      71        4509 :     file->fp = fp;                  /* Better remember FILE *       */
      72        4509 :     file->buffer[0] = EOS;          /* Initialize for first read    */
      73        4509 :     line = 1;                       /* Working on line 1 now        */
      74        4509 :     wrongline = TRUE;               /* Force out initial #line      */
      75        4509 : }
      76             : 
      77             : /*
      78             :  * Append system-specific directories to the include directory list.
      79             :  * Called only when cpp is started.
      80             :  */
      81         361 : void setincdirs()
      82             : {
      83             : 
      84             : #ifdef  CPP_INCLUDE
      85             :     *incend++ = CPP_INCLUDE;
      86             : #define IS_INCLUDE      1
      87             : #else
      88             : #define IS_INCLUDE      0
      89             : #endif
      90             : 
      91             : #if HOST == SYS_UNIX
      92         361 :     *incend++ = "/usr/include";
      93             : #define MAXINCLUDE      (NINCLUDE - 1 - IS_INCLUDE)
      94             : #endif
      95             : 
      96             : 
      97             : #if HOST == SYS_UNKNOWN
      98             : /*
      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() !
     108             :  */
     109             : 
     110             : #if !defined( WNT ) && ! defined UNX
     111             :     extern char* getenv( char *pStr ); /* BP */
     112             : #endif
     113             :     char* pIncGetEnv = NULL;    /* Pointer auf INCLUDE   */
     114             : 
     115             :     if ( ( pIncGetEnv = getenv("INCLUDE") ) != NULL )
     116             :         AddInclude( pIncGetEnv );
     117             : 
     118             : #define MAXINCLUDE      (NINCLUDE - 3 - IS_INCLUDE)
     119             : #endif
     120             : 
     121         361 : }
     122             : 
     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.
     129             :  */
     130             : 
     131        4340 : int AddInclude( char* pIncStr )
     132             : {
     133        4340 :     char* pIncEnv    = NULL;    /* Kopie des INCLUDE     */
     134             :     char* pIncPos;              /* wandert zum naechsten */
     135             : 
     136        4340 :     pIncEnv = savestring( pIncStr );
     137        4340 :     pIncPos = strtok( pIncEnv, ";" );
     138             : 
     139       13020 :     while( pIncPos != NULL )
     140             :     {
     141        4340 :         if (incend >= &incdir[MAXINCLUDE])
     142           0 :             cfatal("Too many include directories", NULLST);
     143        4340 :         *incend++ = pIncPos;
     144        4340 :         pIncPos   = strtok( NULL, ";" );
     145             :     }
     146             :     /* coverity[leaked_storage] - we know this leaks, but it doesn't matter in this short lived utility */
     147        4340 :     return 1;
     148             : }
     149             : 
     150             : /*
     151             :  * dooptions is called to process command line arguments (-Detc).
     152             :  * It is called only at cpp startup.
     153             :  */
     154         361 : int dooptions(int argc, char** argv)
     155             : {
     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               */
     165             : 
     166        9033 :     for (i = j = 1; i < argc; i++)
     167             :     {
     168        8672 :         arg = ap = argv[i];
     169             : 
     170        8672 :         if (*ap++ != '-' || *ap == EOS)
     171             :         {
     172         722 :             argv[j++] = argv[i];
     173             :         }
     174             :         else
     175             :         {
     176        7950 :             c = *ap++;                      /* Option byte          */
     177        7950 :             if (islower(c))                 /* Normalize case       */
     178           0 :                 c = toupper(c);
     179        7950 :             switch (c)                      /* Command character    */
     180             :             {
     181             :             case 'C':                       /* Keep comments        */
     182           0 :                 cflag = TRUE;
     183           0 :                 keepcomments = TRUE;
     184           0 :                 break;
     185             : 
     186             :             case 'D':                       /* Define symbol        */
     187             :                 /*
     188             :                  * If the option is just "-Dfoo", make it -Dfoo=1
     189             :                  */
     190       33934 :                 while (*ap != EOS && *ap != '=')
     191       26714 :                     ap++;
     192        3610 :                 if (*ap == EOS)
     193        2888 :                     ap = "1";
     194             :                 else
     195         722 :                     *ap++ = EOS;
     196             :                 /*
     197             :                  * Now, save the word and its definition.
     198             :                  */
     199        3610 :                 dp = defendel(argv[i] + 2, FALSE);
     200        3610 :                 dp->repl = savestring(ap);
     201        3610 :                 dp->nargs = DEF_NOARGS;
     202        3610 :                 break;
     203             : 
     204             :             case 'E':                       /* Ignore non-fatal     */
     205           0 :                 eflag = TRUE;               /* errors.              */
     206           0 :                 break;
     207             : 
     208             :             case 'I':                       /* Include directory    */
     209        4340 :                 AddInclude( ap );           /* BP, 11.09.91 */
     210        4340 :                 break;
     211             : 
     212             :             case 'N':                       /* No predefineds       */
     213           0 :                 nflag++;                    /* Repeat to undefine   */
     214           0 :                 break;                      /* __LINE__, etc.       */
     215             : 
     216             :             case 'S':
     217           0 :                 sizp = size_table;
     218           0 :                 if (0 != (isdatum = (*ap != '*'))) /* If it's just -S,     */
     219           0 :                     endtest = T_FPTR;       /* Stop here            */
     220             :                 else                        /* But if it's -S*      */
     221             :                 {
     222           0 :                     ap++;                   /* Step over '*'        */
     223           0 :                     endtest = 0;            /* Stop at end marker   */
     224             :                 }
     225           0 :                 while (sizp->bits != endtest && *ap != EOS)
     226             :                 {
     227           0 :                     if (!isdigit(*ap))      /* Skip to next digit   */
     228             :                     {
     229           0 :                         ap++;
     230           0 :                         continue;
     231             :                     }
     232           0 :                     size = 0;               /* Compile the value    */
     233           0 :                     while (isdigit(*ap))
     234             :                     {
     235           0 :                         size *= 10;
     236           0 :                         size += (*ap++ - '0');
     237             :                     }
     238           0 :                     if (isdatum)
     239           0 :                         sizp->size = size;  /* Datum size           */
     240             :                     else
     241           0 :                         sizp->psize = size; /* Pointer size         */
     242           0 :                     sizp++;
     243             :                 }
     244           0 :                 if (sizp->bits != endtest)
     245           0 :                     cwarn("-S, too few values specified in %s", argv[i]);
     246           0 :                 else if (*ap != EOS)
     247           0 :                     cwarn("-S, too many values, \"%s\" unused", ap);
     248           0 :                 break;
     249             : 
     250             :             case 'U':                       /* Undefine symbol      */
     251           0 :                 if (defendel(ap, TRUE) == NULL)
     252           0 :                     cwarn("\"%s\" wasn't defined", ap);
     253           0 :                 break;
     254             : 
     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
     264             : 
     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
     271             : 
     272             :             default:                        /* What is this one?    */
     273           0 :                 cwarn("Unknown option \"%s\"", arg);
     274           0 :                 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           0 :                 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         361 :     if (j > 3)
     293             : #endif
     294             :     {
     295           0 :         cerror( "Too many file arguments.  Usage: cpp [input [output]]",
     296             :                 NULLST);
     297             :     }
     298         361 :     return (j);                     /* Return new argc              */
     299             : }
     300             : 
     301         361 : int readoptions(char* filename, char*** pfargv)
     302             : {
     303             :     FILE* fp;
     304             :     int c;
     305         361 :     int bInQuotes = 0;
     306             :     char optbuff[1024];
     307             :     char* poptbuff;
     308         361 :     int fargc=0;
     309             :     int back;
     310             :     char* fargv[PARALIMIT];
     311             :     char** pfa;
     312             : 
     313         361 :     pfa = *pfargv = malloc(sizeof(fargv));
     314             : 
     315         361 :     poptbuff = &optbuff[0];
     316         361 :     filename++;
     317         361 :     if ((fp = fopen(filename, "r")) == NULL)
     318             :     {
     319             : #if OSL_DEBUG_LEVEL > 1
     320             :         if ( debug || !bDumpDefs )
     321             :             perror(filename);
     322             : #endif
     323           0 :         return (FALSE);
     324             :     }
     325             :     do
     326             :     {
     327             :         /*
     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 !
     332             :          */
     333      289392 :         c = fgetc(fp);
     334      289392 :         if ( c != ' ' && c != CR && c != NL && c != HT && c != EOF)
     335             :         {
     336      280359 :             *poptbuff++ = (char)c;
     337      560718 :             if( c == '"' )
     338           0 :                 bInQuotes = ~bInQuotes;
     339             :         }
     340             :         else
     341             :         {
     342        9033 :             if( c != EOF && bInQuotes )
     343           0 :                 *poptbuff++ = (char)c;
     344             :             else
     345             :             {
     346        9033 :                 *poptbuff = EOS;
     347        9033 :                 if (optbuff[0] != '\0')
     348             :                 {
     349        8672 :                     pfa[fargc + 1] = strdup(optbuff);
     350        8672 :                     fargc++;
     351        8672 :                     pfa[fargc + 1] = 0;
     352        8672 :                     poptbuff = &optbuff[0];
     353             :                 }
     354             :             }
     355             :         }
     356             :     }
     357      289392 :     while ( c != EOF );
     358             : 
     359         361 :     fclose(fp);
     360         361 :     back=dooptions(fargc+1,pfa);
     361             : 
     362         361 :     return (back);
     363             : }
     364             : 
     365             : #if HOST != SYS_UNIX
     366             : 
     367             : /*
     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().
     371             :  */
     372             : FILE_LOCAL void zap_uc(char* ap)
     373             : {
     374             :     while (*ap != EOS)
     375             :     {
     376             :         /*
     377             :          * Don't use islower() here so it works with Multinational
     378             :          */
     379             :         if (*ap >= 'a' && *ap <= 'z')
     380             :             *ap = (char)toupper(*ap);
     381             :         ap++;
     382             :     }
     383             : }
     384             : #endif
     385             : 
     386             : /*
     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.
     391             :  *
     392             :  * Note: the built-in static definitions are suppressed by the -N option.
     393             :  * __LINE__, __FILE__, and __DATE__ are always present.
     394             :  */
     395         361 : void initdefines()
     396             : {
     397             :     char** pp;
     398             :     char* tp;
     399             :     DEFBUF* dp;
     400             :     int i;
     401             :     time_t tvec;
     402             : 
     403             :     /*
     404             :      * Predefine the built-in symbols.  Allow the
     405             :      * implementor to pre-define a symbol as "" to
     406             :      * eliminate it.
     407             :      */
     408         361 :     if (nflag == 0)
     409             :     {
     410         722 :         for (pp = preset; *pp != NULL; pp++)
     411             :         {
     412         361 :             if (*pp[0] != EOS)
     413             :             {
     414         361 :                 dp = defendel(*pp, FALSE);
     415         361 :                 dp->repl = savestring("1");
     416         361 :                 dp->nargs = DEF_NOARGS;
     417             :             }
     418             :         }
     419             :     }
     420             :     /*
     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.
     425             :      */
     426         361 :     if (nflag < 2)
     427             :     {
     428        1083 :         for (pp = magic, i = DEF_NOARGS; *pp != NULL; pp++)
     429             :         {
     430         722 :             dp = defendel(*pp, FALSE);
     431         722 :             dp->nargs = --i;
     432             :         }
     433             : #if OK_DATE
     434             :         /*
     435             :          * Define __DATE__ as today's date.
     436             :          */
     437         361 :         dp = defendel("__DATE__", FALSE);
     438         361 :         dp->repl = tp = getmem(27);
     439         361 :         dp->nargs = DEF_NOARGS;
     440         361 :         time( &tvec);
     441         361 :         *tp++ = '"';
     442         361 :         strcpy(tp, ctime(&tvec));
     443         361 :         tp[24] = '"';                       /* Overwrite newline    */
     444             : #endif
     445             :     }
     446         361 : }
     447             : 
     448             : 
     449             : 
     450             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11