| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- /******************************************************************************
- Copyright (c) 1999 Daniel Stenberg
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- ******************************************************************************/
- /******************************************************************************
- * FREXXWARE
- * ----------------------------------------------------------------------------
- *
- * Project: Frexx C Preprocessor
- * $Source: /home/user/start/cpp/RCS/usecpp.c,v $
- * $Revision: 1.6 $
- * $Date: 1994/06/02 09:11:01 $
- * $Author: start $
- * $State: Exp $
- * $Locker: start $
- *
- * ----------------------------------------------------------------------------
- * $Log: usecpp.c,v $
- * Revision 1.6 1994/06/02 09:11:01 start
- * Added the '-n' option!
- *
- * Revision 1.5 1994/06/02 08:51:49 start
- * Added three more command line parameters
- * Made -h invokes exit nice
- *
- * Revision 1.4 1994/01/24 09:37:17 start
- * Major difference.
- *
- * Revision 1.3 1993/12/06 13:51:20 start
- * A lot of new stuff (too much to mention)
- *
- * Revision 1.2 1993/11/11 07:16:39 start
- * New stuff
- *
- * Revision 1.1 1993/11/03 09:13:08 start
- * Initial revision
- *
- *
- *****************************************************************************/
- /**********************************************************************
- *
- * usecpp.c
- *
- * This is a routine that is should be used to call functions in the
- * fpp.library. We supply the own_input() and own_output() functions to
- * the preprocessor to remain flexible.
- */
- #include <stdlib.h>
- #ifdef AMIGA
- #include <proto/exec.h>
- #include <exec/types.h>
- #if defined(SHARED)
- #include <exec/libraries.h>
- #include <libraries/dos.h>
- #include "fpp_pragmas.h"
- #include "fpp_protos.h"
- #include "FPPBase.h"
- struct Library *FPPBase=NULL;
- #define PREFIX __saveds
- #define REG(x) register __ ## x
- #else
- #define PREFIX
- #define REG(x)
- #endif
- #elif defined(UNIX)
- #if defined(OS9)
- #include <types.h>
- #else
- #include <sys/types.h>
- #ifdef BSD
- #include <sys/unistd.h> /* for BSD systems (SUN OS at least) */
- #endif
- #endif
- #define PREFIX
- #define REG(x)
- #endif
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #ifndef OS9
- #include <stdarg.h>
- #else
- #define va_list void *
- #endif
- #include "fpp.h"
- #define MAX_TAGS 40 /* maximum number of tags allowed! */
- #define FILE_LOCAL static
- #define CPP_PREFS_FILE "cpp.prefs"
- #ifdef AMIGA
- #define DEFAULT_CPP_PREFS_FILE "s:cpp.prefs"
- #else
- #define DEFAULT_CPP_PREFS_FILE "$HOME/cpp.prefs"
- #endif
- FILE_LOCAL char PREFIX *own_input(char *, int, void *);
- FILE_LOCAL void PREFIX own_output(int, void *);
- FILE_LOCAL void PREFIX own_error(void *, char *, va_list);
- FILE_LOCAL int SetOptions(int, char **, struct fppTag **);
- FILE_LOCAL char GetPrefs(struct fppTag **, char **);
- FILE_LOCAL char DoString(struct fppTag **, char *);
- #ifdef AMIGA
- extern long __stack=8000;
- #endif
- FILE_LOCAL char ignore=FPP_FALSE; /* if we should ignore strange flags! */
- FILE_LOCAL char display=FPP_FALSE; /* display all options in use! */
- FILE_LOCAL char dontreadprefs; /* set if only the command line is valid */
- int main(int argc, char **argv)
- {
- struct fppTag tags[MAX_TAGS];
- int i;
- struct fppTag *tagptr = tags;
- char *dealloc;
- /*
- * Append system-specific directories to the include directory list.
- * The include directories will be searched through in the same order
- * as you add them in the taglist!
- * The directory _must_ end with a proper directory speparator!
- */
- tagptr->tag=FPPTAG_INCLUDE_DIR;
- #if defined (AMIGA)
- tagptr->data = "INCLUDE:";
- #elif defined (OS9)
- tagptr->data = "/dd/defs/";
- #else
- tagptr->data = "/usr/include/";
- #endif
- tagptr++;
- if(GetPrefs(&tagptr, &dealloc))
- return(0);
- if( !(i = SetOptions(argc, argv, &tagptr)))
- return(0);
-
- if (argc - i >2) {
- printf("Too many file arguments. Usage: cpp [options] [input [output]]\n");
- return(-1);
- }
- tagptr->tag=FPPTAG_INPUT;
- tagptr->data=(void *)own_input;
- tagptr++;
- if(i<argc) {
- /*
- * Open input file, "-" means use stdin.
- */
- if (strcmp(argv[i], "-")) {
- if (freopen(argv[i], "r", stdin) == NULL) {
- perror(argv[i]);
- fprintf(stderr, "Can't open input file \"%s\"", argv[i]);
- return(-2);
- }
- tagptr->tag=FPPTAG_INPUT_NAME;
- tagptr->data=argv[i];
- tagptr++;
- if(display)
- fprintf(stderr, "cpp: input: %s\n", argv[i]);
- } else /* Else, just get stdin */
- if(display)
- fprintf(stderr, "cpp: input: [stdin]\n");
- i++;
- } else
- if(display)
- fprintf(stderr, "cpp: input: [stdin]\n");
- if(i<argc) {
- /*
- * Get output file, "-" means use stdout.
- */
- if (strcmp(argv[i], "-")) {
- if (freopen(argv[i], "w", stdout) == NULL) {
- perror(argv[i]);
- fprintf(stderr, "Can't open output file \"%s\"", argv[i]);
- return(-1);
- }
- if(display)
- fprintf(stderr, "cpp: output: %s\n", argv[i]);
- } else
- if(display)
- fprintf(stderr, "cpp: output: [stdout]\n");
- } else
- if(display)
- fprintf(stderr, "cpp: output: [stdout]\n");
- tagptr->tag=FPPTAG_OUTPUT;
- tagptr->data=(void *)own_output;
- tagptr++;
- tagptr->tag=FPPTAG_ERROR;
- tagptr->data=(void *)own_error;
- tagptr++;
- /* The LAST tag: */
- tagptr->tag=FPPTAG_END;
- tagptr->data=0;
- tagptr++;
- #if defined(SHARED) && defined(AMIGA)
- if(!(FPPBase=OpenLibrary(FPPNAME, 1))) {
- printf("Error opening %s!\n", FPPNAME);
- return(-1);
- }
- #endif
- fppPreProcess(tags);
- #if defined(SHARED) && defined(AMIGA)
- CloseLibrary((struct Library *)FPPBase);
- #endif
- /*
- * Preprocess ready!
- */
- if( dealloc )
- free( dealloc );
- return(0);
- }
- FILE_LOCAL
- char PREFIX *own_input(char *buffer, int size, void *userdata)
- {
- return(fgets(buffer, size, stdin));
- }
- FILE_LOCAL
- void PREFIX own_output(int c, void *userdata)
- {
- putchar(c);
- }
- FILE_LOCAL
- void PREFIX own_error(void *userdata, char *format, va_list arg)
- {
- vfprintf(stderr, format, arg);
- }
- FILE_LOCAL
- char GetPrefs(struct fppTag **tagptr, char **string)
- {
-
- FILE *PrefsFile_PF;
- unsigned Length_U;
- char *PrefsBuffer_PC;
- char ret= 0;
- char *env;
- *string = NULL;
- /* Open prefs file for read */
- if ( (PrefsFile_PF = fopen(CPP_PREFS_FILE, "r")) ||
- (PrefsFile_PF = fopen(DEFAULT_CPP_PREFS_FILE, "r"))) {
-
- fseek(PrefsFile_PF, 0 , SEEK_END);
- Length_U = ftell(PrefsFile_PF);
- fseek(PrefsFile_PF, 0, SEEK_SET);
- if (*string = (char *)malloc(Length_U+1)) {
- fread(*string, 1, Length_U, PrefsFile_PF);
- (*string)[Length_U] = '\0';
-
- ret = !DoString(tagptr, *string);
- }
- fclose(PrefsFile_PF);
- if(ret) {
- free( *string );
- return ret;
- }
- }
- if((env = getenv("CPP_PREFS"))) {
- ret= !DoString(tagptr, env);
- if(ret && *string)
- free( *string );
- }
- return ret;
- }
- FILE_LOCAL
- char DoString(struct fppTag **tagptr, char *string)
- {
- char *argv[MAX_TAGS];
- int argc=1;
- do {
- while(*string && *string != '-')
- string++;
- if(!*string)
- break;
- argv[argc]=string;
- do {
- string++;
- if(*string=='\"') {
- do
- string++;
- while(*string != '\"');
- string++;
- }
- } while(*string && *string!=' ' && *string != '\n' && *string != '\t');
- argc++;
- if(*string) {
- *string='\0';
- string++;
- } else
- break;
- } while(1);
- return (SetOptions(argc, argv, tagptr));
- }
- FILE_LOCAL
- int SetOptions(int argc, char **argv, struct fppTag **tagptr)
- {
- int i;
- char *ap;
- for (i = 1; i < argc; i++) {
- ap = argv[i];
- if (*ap++ != '-' || *ap == '\0')
- break;
- else {
- char c = *ap++;
- if(display)
- fprintf(stderr, "cpp: option: %s\n", ap-2);
- switch (c) { /* Command character */
- case 'Q': /* ignore unknown flags but */
- ignore=1; /* output them on stderr */
- break;
- case 'q': /* ignore unknown flags */
- ignore=2;
- break;
- case 'H': /* display all whitespaces */
- (*tagptr)->tag = FPPTAG_OUTPUTSPACE;
- (*tagptr)->data= (void *)FPP_TRUE;
- (*tagptr)++;
- break;
- case 'b': /* display unbalance */
- (*tagptr)->tag = FPPTAG_OUTPUTBALANCE;
- (*tagptr)->data= (void *)FPP_TRUE;
- (*tagptr)++;
- break;
- case 'f': /* output all defined functions! */
- (*tagptr)->tag = FPPTAG_DISPLAYFUNCTIONS;
- (*tagptr)->data= (void *)FPP_TRUE;
- (*tagptr)++;
- break;
- case 'F': /* output all included files! */
- (*tagptr)->tag = FPPTAG_OUTPUTINCLUDES;
- (*tagptr)->data= (void *)FPP_TRUE;
- (*tagptr)++;
- break;
- case 'V': /* do not output version */
- (*tagptr)->tag = FPPTAG_SHOWVERSION;
- (*tagptr)->data= (void *)FPP_FALSE;
- (*tagptr)++;
- break;
- case 'C': /* Keep comments */
- (*tagptr)->tag = FPPTAG_KEEPCOMMENTS;
- (*tagptr)->data= (void *)FPP_TRUE;
- (*tagptr)++;
- break;
-
- case 'D': /* Define symbol */
- (*tagptr)->tag=FPPTAG_DEFINE;
- (*tagptr)->data=argv[i]+2;
- (*tagptr)++;
- break;
- case 'd': /* Display all options */
- fprintf(stderr, "FOUND -d flag!\n");
- display = FPP_TRUE;
- break;
-
- case 'E': /* Ignore non-fatal errors */
- (*tagptr)->tag=FPPTAG_IGNORE_NONFATAL;
- (*tagptr)->data=(void *)FPP_TRUE;
- (*tagptr)++;
- break;
-
- case 'I': /* Include directory */
- (*tagptr)->tag=FPPTAG_INCLUDE_DIR;
- (*tagptr)->data=ap;
- (*tagptr)++;
- break;
-
- case 'J': /* Allow nested comments */
- (*tagptr)->tag=FPPTAG_NESTED_COMMENTS;
- (*tagptr)->data=ap;
- (*tagptr)++;
- break;
-
- case 'j': /* Warn at nested comments */
- (*tagptr)->tag=FPPTAG_WARN_NESTED_COMMENTS;
- (*tagptr)->data=ap;
- (*tagptr)++;
- break;
-
- case 'L':
- if(*ap == 'L') { /* Do not output #line */
- (*tagptr)->tag=FPPTAG_LINE;
- } else {
- /* Do not output the 'line' keyword */
- (*tagptr)->tag=FPPTAG_OUTPUTLINE;
- }
- (*tagptr)->data=(void *)FPP_FALSE;
- (*tagptr)++;
- break;
- case 'M': /* Do not warn at missing includes */
- (*tagptr)->tag=FPPTAG_WARNMISSINCLUDE;
- (*tagptr)->data=(void *)FPP_FALSE;
- (*tagptr)++;
- break;
- case 'n':
- dontreadprefs^=1; /* toggle prefsreading, default is read prefs */
- /*
- * This flag should reset all previously added tags!
- */
- break;
- case 'N': /* No machine specific built-ins */
- (*tagptr)->tag=FPPTAG_BUILTINS;
- (*tagptr)->data=(void *)FPP_FALSE;
- (*tagptr)++;
- break;
- case 'B': /* No predefines like __LINE__, etc. */
- (*tagptr)->tag=FPPTAG_PREDEFINES;
- (*tagptr)->data=(void *)FPP_FALSE;
- (*tagptr)++;
- break;
-
- case 'P': /* No C++ comments */
- (*tagptr)->tag=FPPTAG_IGNORE_CPLUSPLUS;
- (*tagptr)->data=(void *)FPP_TRUE;
- (*tagptr)++;
- break;
-
- case 'p': /* warn about illegal # - instructions */
- (*tagptr)->tag = FPPTAG_WARNILLEGALCPP;
- (*tagptr)->data= (void *)FPP_TRUE;
- (*tagptr)++;
- break;
- case 'R':
- (*tagptr)->tag = FPPTAG_RIGHTCONCAT;
- (*tagptr)->data= (void *)FPP_TRUE;
- (*tagptr)++;
- break;
- case 's': /* sizeof table */
- (*tagptr)->tag=FPPTAG_INITFUNC;
- (*tagptr)->data=ap;
- (*tagptr)++;
- break;
-
- case 't': /* excluded functions */
- (*tagptr)->tag=FPPTAG_EXCLFUNC;
- (*tagptr)->data=ap;
- (*tagptr)++;
- break;
-
- case 'S': /* sizeof table */
- (*tagptr)->tag=FPPTAG_SIZEOF_TABLE;
- (*tagptr)->data=ap;
- (*tagptr)++;
- break;
-
- case 'U': /* Undefine symbol */
- (*tagptr)->tag=FPPTAG_UNDEFINE;
- (*tagptr)->data=ap;
- (*tagptr)++;
- break;
- case 'w': /* Output all #defines but not the
- main file */
- (*tagptr)->tag=FPPTAG_OUTPUTMAIN;
- (*tagptr)->data=(void *)FPP_FALSE;
- (*tagptr)++;
-
- case 'W': /* Output all #defines */
- if(!strncmp(ap, "WW", 2)) {
- (*tagptr)->tag=FPPTAG_WEBMODE;
- (*tagptr)->data=(void *)FPP_TRUE;
- (*tagptr)++;
- }
- else {
- (*tagptr)->tag=FPPTAG_OUTPUT_DEFINES;
- (*tagptr)->data=(void *)FPP_TRUE;
- (*tagptr)++;
- }
- break;
- case 'X':
- (*tagptr)->tag=FPPTAG_INCLUDE_FILE;
- (*tagptr)->data=ap;
- (*tagptr)++;
- break;
- /*
- case 'x':
- tags[tag]->tag=FPPTAG_INCLUDE_MACRO_FILE;
- tags[tag++]->data=ap;
- break;
- */
- case 'h':
- case '?': /* if a question mark is possible to specify! */
- default: /* What is this one? */
- if( ignore < 2 && c != 'h') {
- fprintf(stderr, "cpp: unknown option: -%s\n", ap-1);
- }
- if(!ignore || c == 'h') {
- fprintf(stderr,
- "Usage: cpp [options] [infile [outfile] ]\n\n"
- "The following options are valid:\n"
- " -B\tNo machine specific built-in symbols\n"
- " -b\tOutput any parentheses, brace or bracket unbalance\n"
- " -C\tWrite source file comments to output\n"
- " -D\tDefine a symbol with the given (optional) value \"symbol[=value]\"\n"
- " -d\tDisplay all specified options\n"
- " -E\tIgnore non-fatal errors\n"
- " -F\tOutput all included file names on stderr\n"
- " -f\tOutput all defined functions' names on stderr\n"
- " -H\tOutput all whitespaces from the source file\n"
- " -h\tOutput this help text\n"
- " -I\tAdd directory to the #include search list\n"
- " -J\tAllow nested comments\n"
- " -j\tEnable warnings for nested comments\n"
- " -LL\tDon't output #line instructions\n"
- " -L\tDon't output the 'line' keyword in \"#line\" instructions\n"
- " -M\tDon't warn for missing include files\n"
- " -N\tDon't predefine target-specific names\n"
- " -n\tToggle prefs usage\n"
- " -P\tDon't recognize C++ comment style\n"
- " -p\tEnable warnings on non ANSI preprocessor instructions\n"
- " -Q\tIgnore but visualize undefined flags\n"
- " -q\tIgnore all undefined flags\n"
- " -R\tEvaluate the right part first in symbol concatenations\n"
- " -s\tInclude the following string at the top of each function\n"
- " -S\tSpecify sizes for #if sizeof\n"
- " -t\tThis function should not get an initial function\n"
- " -U\tUndefine symbol\n"
- " -V\tDon't output version information\n"
- " -W\tOutput all #defines\n"
- " -WWW\tWeb mode preprocessing\n"
- " -w\tOnly output #defines\n"
- " -X\tInclude file\n");
- return(0);
- } /* if (!ignore) */
- } /* Switch on all options */
- } /* If it's a -option */
- } /* For all arguments */
- return i;
- } /* end of function */
|