LCOV - code coverage report
Current view: top level - rsc/source/rscpp - cpp3.c (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 88 129 68.2 %
Date: 2014-11-03 Functions: 8 8 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         363 : void InitCpp3()
      36             : {
      37         363 : }
      38             : 
      39             : 
      40             : /*
      41             :  * Open a file, add it to the linked list of open files.
      42             :  * This is called only from openfile() above.
      43             :  */
      44       13790 : int openfile(char* filename)
      45             : {
      46             :     FILE* fp;
      47             : 
      48       13790 :     if ((fp = fopen(filename, "r")) == NULL)
      49             :     {
      50             : #if OSL_DEBUG_LEVEL > 1
      51             :         if ( debug || !bDumpDefs )
      52             :             perror(filename);
      53             : #endif
      54        9587 :         return (FALSE);
      55             :     }
      56             : #if OSL_DEBUG_LEVEL > 1
      57             :     if (debug)
      58             :         fprintf(stderr, "Reading from \"%s\"\n", filename);
      59             : #endif
      60        4203 :     addfile(fp, filename);
      61        4203 :     return (TRUE);
      62             : }
      63             : 
      64             : /*
      65             :  * Initialize tables for this open file.  This is called from openfile()
      66             :  * above (for #include files), and from the entry to cpp to open the main
      67             :  * input file.  It calls a common routine, getfile() to build the FILEINFO
      68             :  * structure which is used to read characters.  (getfile() is also called
      69             :  * to setup a macro replacement.)
      70             :  */
      71        4566 : void addfile(FILE* fp, char* filename)
      72             : {
      73             :     FILEINFO* file;
      74             : 
      75        4566 :     file = getfile(NBUFF, filename);
      76        4566 :     file->fp = fp;                  /* Better remember FILE *       */
      77        4566 :     file->buffer[0] = EOS;          /* Initialize for first read    */
      78        4566 :     line = 1;                       /* Working on line 1 now        */
      79        4566 :     wrongline = TRUE;               /* Force out initial #line      */
      80        4566 : }
      81             : 
      82             : /*
      83             :  * Append system-specific directories to the include directory list.
      84             :  * Called only when cpp is started.
      85             :  */
      86         363 : void setincdirs()
      87             : {
      88             : 
      89             : #ifdef  CPP_INCLUDE
      90             :     *incend++ = CPP_INCLUDE;
      91             : #define IS_INCLUDE      1
      92             : #else
      93             : #define IS_INCLUDE      0
      94             : #endif
      95             : 
      96             : #if HOST == SYS_UNIX
      97         363 :     *incend++ = "/usr/include";
      98             : #define MAXINCLUDE      (NINCLUDE - 1 - IS_INCLUDE)
      99             : #endif
     100             : 
     101             : 
     102             : #if HOST == SYS_UNKNOWN
     103             : /*
     104             :  * Kontext: GenMake
     105             :  * Unter DOS wird nun auch die Environment-Variable INCLUDE ausgewetet.
     106             :  * Es kommt erschwerend hinzu, dass alle Eintraege, die mit ';' getrennt
     107             :  * sind, mit in die Liste aufenommen werden muessen.
     108             :  * Dies wird mit der Funktion strtok() realisiert.
     109             :  * Vorsicht bei der Benutzung von malloc !!!
     110             :  * In savestring wird naemlich getmem() verwendet. Vermutlich kommen sich
     111             :  * die beiden Funktion in die Quere. Als ich malloc statt savestring
     112             :  * verwendete knallte es in strcpy() !
     113             :  */
     114             : 
     115             : #if !defined( WNT ) && ! defined UNX
     116             :     extern char* getenv( char *pStr ); /* BP */
     117             : #endif
     118             :     char* pIncGetEnv = NULL;    /* Pointer auf INCLUDE   */
     119             : 
     120             :     if ( ( pIncGetEnv = getenv("INCLUDE") ) != NULL )
     121             :         AddInclude( pIncGetEnv );
     122             : 
     123             : #define MAXINCLUDE      (NINCLUDE - 3 - IS_INCLUDE)
     124             : #endif
     125             : 
     126         363 : }
     127             : 
     128             : /* Kontext: Erweiterung des INCLUDE-Services
     129             :  * Bislang konnte der cpp keine Include-Angaben in der Kommandozeile
     130             :  * vertragen, bei denen die directries mit ';' getrennt wurden.
     131             :  * Dies ist auch verstaendlich, da dieses cpp fuer UNIX-Systeme
     132             :  * massgeschneidert wurde und in UNI die ';' als Zeichen zum Abschluss
     133             :  * von Kommandos gilt.
     134             :  */
     135             : 
     136        4324 : int AddInclude( char* pIncStr )
     137             : {
     138        4324 :     char* pIncEnv    = NULL;    /* Kopie des INCLUDE     */
     139             :     char* pIncPos;              /* wandert zum naechsten */
     140             : 
     141        4324 :     pIncEnv = savestring( pIncStr );
     142        4324 :     pIncPos = strtok( pIncEnv, ";" );
     143             : 
     144       12972 :     while( pIncPos != NULL )
     145             :     {
     146        4324 :         if (incend >= &incdir[MAXINCLUDE])
     147           0 :             cfatal("Too many include directories", NULLST);
     148        4324 :         *incend++ = pIncPos;
     149        4324 :         pIncPos   = strtok( NULL, ";" );
     150             :     }
     151        4324 :     return( 1 );
     152             : }
     153             : 
     154             : /*
     155             :  * dooptions is called to process command line arguments (-Detc).
     156             :  * It is called only at cpp startup.
     157             :  */
     158         363 : int dooptions(int argc, char** argv)
     159             : {
     160             :     char* ap;
     161             :     DEFBUF* dp;
     162             :     int c;
     163             :     int i, j;
     164             :     char* arg;
     165             :     SIZES* sizp;        /* For -S               */
     166             :     int size;           /* For -S               */
     167             :     int isdatum;        /* FALSE for -S*        */
     168             :     int endtest;        /* For -S               */
     169             : 
     170        9406 :     for (i = j = 1; i < argc; i++)
     171             :     {
     172        9043 :         arg = ap = argv[i];
     173             : 
     174        9043 :         if (*ap++ != '-' || *ap == EOS)
     175             :         {
     176         726 :             argv[j++] = argv[i];
     177             :         }
     178             :         else
     179             :         {
     180        8317 :             c = *ap++;                      /* Option byte          */
     181        8317 :             if (islower(c))                 /* Normalize case       */
     182           0 :                 c = toupper(c);
     183        8317 :             switch (c)                      /* Command character    */
     184             :             {
     185             :             case 'C':                       /* Keep comments        */
     186           0 :                 cflag = TRUE;
     187           0 :                 keepcomments = TRUE;
     188           0 :                 break;
     189             : 
     190             :             case 'D':                       /* Define symbol        */
     191             :                 /*
     192             :                  * If the option is just "-Dfoo", make it -Dfoo=1
     193             :                  */
     194       41382 :                 while (*ap != EOS && *ap != '=')
     195       33396 :                     ap++;
     196        3993 :                 if (*ap == EOS)
     197        3267 :                     ap = "1";
     198             :                 else
     199         726 :                     *ap++ = EOS;
     200             :                 /*
     201             :                  * Now, save the word and its definition.
     202             :                  */
     203        3993 :                 dp = defendel(argv[i] + 2, FALSE);
     204        3993 :                 dp->repl = savestring(ap);
     205        3993 :                 dp->nargs = DEF_NOARGS;
     206        3993 :                 break;
     207             : 
     208             :             case 'E':                       /* Ignore non-fatal     */
     209           0 :                 eflag = TRUE;               /* errors.              */
     210           0 :                 break;
     211             : 
     212             :             case 'I':                       /* Include directory    */
     213        4324 :                 AddInclude( ap );           /* BP, 11.09.91 */
     214        4324 :                 break;
     215             : 
     216             :             case 'N':                       /* No predefineds       */
     217           0 :                 nflag++;                    /* Repeat to undefine   */
     218           0 :                 break;                      /* __LINE__, etc.       */
     219             : 
     220             :             case 'S':
     221           0 :                 sizp = size_table;
     222           0 :                 if (0 != (isdatum = (*ap != '*'))) /* If it's just -S,     */
     223           0 :                     endtest = T_FPTR;       /* Stop here            */
     224             :                 else                        /* But if it's -S*      */
     225             :                 {
     226           0 :                     ap++;                   /* Step over '*'        */
     227           0 :                     endtest = 0;            /* Stop at end marker   */
     228             :                 }
     229           0 :                 while (sizp->bits != endtest && *ap != EOS)
     230             :                 {
     231           0 :                     if (!isdigit(*ap))      /* Skip to next digit   */
     232             :                     {
     233           0 :                         ap++;
     234           0 :                         continue;
     235             :                     }
     236           0 :                     size = 0;               /* Compile the value    */
     237           0 :                     while (isdigit(*ap))
     238             :                     {
     239           0 :                         size *= 10;
     240           0 :                         size += (*ap++ - '0');
     241             :                     }
     242           0 :                     if (isdatum)
     243           0 :                         sizp->size = size;  /* Datum size           */
     244             :                     else
     245           0 :                         sizp->psize = size; /* Pointer size         */
     246           0 :                     sizp++;
     247             :                 }
     248           0 :                 if (sizp->bits != endtest)
     249           0 :                     cwarn("-S, too few values specified in %s", argv[i]);
     250           0 :                 else if (*ap != EOS)
     251           0 :                     cwarn("-S, too many values, \"%s\" unused", ap);
     252           0 :                 break;
     253             : 
     254             :             case 'U':                       /* Undefine symbol      */
     255           0 :                 if (defendel(ap, TRUE) == NULL)
     256           0 :                     cwarn("\"%s\" wasn't defined", ap);
     257           0 :                 break;
     258             : 
     259             : #if OSL_DEBUG_LEVEL > 1
     260             :             case 'X':                       /* Debug                */
     261             :                 debug = (isdigit(*ap)) ? atoi(ap) : 1;
     262             : #if (HOST == SYS_UNIX)
     263             :                 signal(SIGINT, (void (*)(int)) abort); /* Trap "interrupt" */
     264             : #endif
     265             :                 fprintf(stderr, "Debug set to %d\n", debug);
     266             :                 break;
     267             : #endif
     268             : 
     269             : #if OSL_DEBUG_LEVEL > 1
     270             :             case 'P':                       /* #define's dump       */
     271             :                 bDumpDefs = 1;
     272             :                 fprintf(stderr, "Dump #define's is on\n");
     273             :                 break;
     274             : #endif
     275             : 
     276             :             default:                        /* What is this one?    */
     277           0 :                 cwarn("Unknown option \"%s\"", arg);
     278           0 :                 fprintf(stderr, "The following options are valid:\n\
     279             :   -C\t\t\tWrite source file comments to output\n\
     280             :   -Dsymbol=value\tDefine a symbol with the given (optional) value\n\
     281             :   -Idirectory\t\tAdd a directory to the #include search list\n\
     282             :   -N\t\t\tDon't predefine target-specific names\n\
     283             :   -Stext\t\tSpecify sizes for #if sizeof\n\
     284             :   -Usymbol\t\tUndefine symbol\n");
     285             : #if OSL_DEBUG_LEVEL > 1
     286             :                 fprintf(stderr, "  -Xvalue\t\tSet internal debug flag\n");
     287             :                 fprintf(stderr, "  -P\t\t\tdump #define's\n");
     288             : #endif
     289           0 :                 break;
     290             :             }                       /* Switch on all options        */
     291             :         }                           /* If it's a -option            */
     292             :     }                               /* For all arguments            */
     293             : #if OSL_DEBUG_LEVEL > 1
     294             :     if ( (bDumpDefs ? j > 4 : j > 3) )
     295             : #else
     296         363 :     if (j > 3)
     297             : #endif
     298             :     {
     299           0 :         cerror( "Too many file arguments.  Usage: cpp [input [output]]",
     300             :                 NULLST);
     301             :     }
     302         363 :     return (j);                     /* Return new argc              */
     303             : }
     304             : 
     305         363 : int readoptions(char* filename, char*** pfargv)
     306             : {
     307             :     FILE* fp;
     308             :     int c;
     309         363 :     int bInQuotes = 0;
     310             :     char optbuff[1024];
     311             :     char* poptbuff;
     312         363 :     int fargc=0;
     313             :     int back;
     314             :     char* fargv[PARALIMIT];
     315             :     char** pfa;
     316             : 
     317         363 :     pfa = *pfargv = malloc(sizeof(fargv));
     318             : 
     319         363 :     poptbuff = &optbuff[0];
     320         363 :     filename++;
     321         363 :     if ((fp = fopen(filename, "r")) == NULL)
     322             :     {
     323             : #if OSL_DEBUG_LEVEL > 1
     324             :         if ( debug || !bDumpDefs )
     325             :             perror(filename);
     326             : #endif
     327           0 :         return (FALSE);
     328             :     }
     329             :     do
     330             :     {
     331             :         /*
     332             :          *  #i27914# double ticks '"' now have a duplicate function:
     333             :          *  1. they define a string ( e.g. -DFOO="baz" )
     334             :          *  2. a string can contain spaces, so -DFOO="baz zum" defines one
     335             :          *  argument no two !
     336             :          */
     337      285643 :         c = fgetc(fp);
     338      285643 :         if ( c != ' ' && c != CR && c != NL && c != HT && c != EOF)
     339             :         {
     340      276237 :             *poptbuff++ = (char)c;
     341      552474 :             if( c == '"' )
     342           0 :                 bInQuotes = ~bInQuotes;
     343             :         }
     344             :         else
     345             :         {
     346        9406 :             if( c != EOF && bInQuotes )
     347           0 :                 *poptbuff++ = (char)c;
     348             :             else
     349             :             {
     350        9406 :                 *poptbuff = EOS;
     351        9406 :                 if (strlen(optbuff)>0)
     352             :                 {
     353        9043 :                     pfa[fargc + 1] = strdup(optbuff);
     354        9043 :                     fargc++;
     355        9043 :                     pfa[fargc + 1] = 0;
     356        9043 :                     poptbuff = &optbuff[0];
     357             :                 }
     358             :             }
     359             :         }
     360             :     }
     361      285643 :     while ( c != EOF );
     362             : 
     363         363 :     fclose(fp);
     364         363 :     back=dooptions(fargc+1,pfa);
     365             : 
     366         363 :     return (back);
     367             : }
     368             : 
     369             : #if HOST != SYS_UNIX
     370             : 
     371             : /*
     372             :  * Dec operating systems mangle upper-lower case in command lines.
     373             :  * This routine forces the -D and -U arguments to uppercase.
     374             :  * It is called only on cpp startup by dooptions().
     375             :  */
     376             : FILE_LOCAL void zap_uc(char* ap)
     377             : {
     378             :     while (*ap != EOS)
     379             :     {
     380             :         /*
     381             :          * Don't use islower() here so it works with Multinational
     382             :          */
     383             :         if (*ap >= 'a' && *ap <= 'z')
     384             :             *ap = (char)toupper(*ap);
     385             :         ap++;
     386             :     }
     387             : }
     388             : #endif
     389             : 
     390             : /*
     391             :  * Initialize the built-in #define's.  There are two flavors:
     392             :  *      #define decus   1               (static definitions)
     393             :  *      #define __FILE__ ??             (dynamic, evaluated by magic)
     394             :  * Called only on cpp startup.
     395             :  *
     396             :  * Note: the built-in static definitions are suppressed by the -N option.
     397             :  * __LINE__, __FILE__, and __DATE__ are always present.
     398             :  */
     399         363 : void initdefines()
     400             : {
     401             :     char** pp;
     402             :     char* tp;
     403             :     DEFBUF* dp;
     404             :     int i;
     405             :     time_t tvec;
     406             : 
     407             :     /*
     408             :      * Predefine the built-in symbols.  Allow the
     409             :      * implementor to pre-define a symbol as "" to
     410             :      * eliminate it.
     411             :      */
     412         363 :     if (nflag == 0)
     413             :     {
     414         726 :         for (pp = preset; *pp != NULL; pp++)
     415             :         {
     416         363 :             if (*pp[0] != EOS)
     417             :             {
     418         363 :                 dp = defendel(*pp, FALSE);
     419         363 :                 dp->repl = savestring("1");
     420         363 :                 dp->nargs = DEF_NOARGS;
     421             :             }
     422             :         }
     423             :     }
     424             :     /*
     425             :      * The magic pre-defines (__FILE__ and __LINE__ are
     426             :      * initialized with negative argument counts.  expand()
     427             :      * notices this and calls the appropriate routine.
     428             :      * DEF_NOARGS is one greater than the first "magic" definition.
     429             :      */
     430         363 :     if (nflag < 2)
     431             :     {
     432        1089 :         for (pp = magic, i = DEF_NOARGS; *pp != NULL; pp++)
     433             :         {
     434         726 :             dp = defendel(*pp, FALSE);
     435         726 :             dp->nargs = --i;
     436             :         }
     437             : #if OK_DATE
     438             :         /*
     439             :          * Define __DATE__ as today's date.
     440             :          */
     441         363 :         dp = defendel("__DATE__", FALSE);
     442         363 :         dp->repl = tp = getmem(27);
     443         363 :         dp->nargs = DEF_NOARGS;
     444         363 :         time( &tvec);
     445         363 :         *tp++ = '"';
     446         363 :         strcpy(tp, ctime(&tvec));
     447         363 :         tp[24] = '"';                       /* Overwrite newline    */
     448             : #endif
     449             :     }
     450         363 : }
     451             : 
     452             : 
     453             : 
     454             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10