diff options
author | IronClawTrem <louie.nutman@gmail.com> | 2020-02-16 03:40:06 +0000 |
---|---|---|
committer | IronClawTrem <louie.nutman@gmail.com> | 2020-02-16 03:40:06 +0000 |
commit | 425decdf7e9284d15aa726e3ae96b9942fb0e3ea (patch) | |
tree | 6c0dd7edfefff1be7b9e75fe0b3a0a85fe1595f3 /src/tools/lcc/cpp | |
parent | ccb0b2e4d6674a7a00c9bf491f08fc73b6898c54 (diff) |
create tremded branch
Diffstat (limited to 'src/tools/lcc/cpp')
-rw-r--r-- | src/tools/lcc/cpp/cpp.c | 621 | ||||
-rw-r--r-- | src/tools/lcc/cpp/include.c | 268 | ||||
-rw-r--r-- | src/tools/lcc/cpp/unix.c | 215 |
3 files changed, 572 insertions, 532 deletions
diff --git a/src/tools/lcc/cpp/cpp.c b/src/tools/lcc/cpp/cpp.c index 5c0cfd7..c379a72 100644 --- a/src/tools/lcc/cpp/cpp.c +++ b/src/tools/lcc/cpp/cpp.c @@ -1,326 +1,339 @@ +#include "cpp.h" +#include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> -#include <stdarg.h> -#include "cpp.h" char rcsid[] = "cpp.c - faked rcsid"; -#define OUTS 16384 -char outbuf[OUTS]; -char *outbufp = outbuf; -Source *cursource; -int nerrs; -struct token nltoken = { NL, 0, 0, 0, 1, (uchar*)"\n" }; -char *curtime; -int incdepth; -int ifdepth; -int ifsatisfied[NIF]; -int skipping; - - -int -main(int argc, char **argv) +#define OUTS 16384 +char outbuf[OUTS]; +char *outbufp = outbuf; +Source *cursource; +int nerrs; +struct token nltoken = {NL, 0, 0, 0, 1, (uchar *)"\n"}; +char *curtime; +int incdepth; +int ifdepth; +int ifsatisfied[NIF]; +int skipping; + +int main(int argc, char **argv) { - Tokenrow tr; - time_t t; - char ebuf[BUFSIZ]; - - setbuf(stderr, ebuf); - t = time(NULL); - curtime = ctime(&t); - maketokenrow(3, &tr); - expandlex(); - setup(argc, argv); - fixlex(); - iniths(); - genline(); - process(&tr); - flushout(); - fflush(stderr); - exit(nerrs > 0); - return 0; + Tokenrow tr; + time_t t; + char ebuf[BUFSIZ]; + + setbuf(stderr, ebuf); + t = time(NULL); + curtime = ctime(&t); + maketokenrow(3, &tr); + expandlex(); + setup(argc, argv); + fixlex(); + iniths(); + genline(); + process(&tr); + flushout(); + fflush(stderr); + exit(nerrs > 0); + return 0; } -void -process(Tokenrow *trp) +void process(Tokenrow *trp) { - int anymacros = 0; - - for (;;) { - if (trp->tp >= trp->lp) { - trp->tp = trp->lp = trp->bp; - outbufp = outbuf; - anymacros |= gettokens(trp, 1); - trp->tp = trp->bp; - } - if (trp->tp->type == END) { - if (--incdepth>=0) { - if (cursource->ifdepth) - error(ERROR, - "Unterminated conditional in #include"); - unsetsource(); - cursource->line += cursource->lineinc; - trp->tp = trp->lp; - genline(); - continue; - } - if (ifdepth) - error(ERROR, "Unterminated #if/#ifdef/#ifndef"); - break; - } - if (trp->tp->type==SHARP) { - trp->tp += 1; - control(trp); - } else if (!skipping && anymacros) - expandrow(trp, NULL); - if (skipping) - setempty(trp); - puttokens(trp); - anymacros = 0; - cursource->line += cursource->lineinc; - if (cursource->lineinc>1) { - genline(); - } - } + int anymacros = 0; + + for (;;) + { + if (trp->tp >= trp->lp) + { + trp->tp = trp->lp = trp->bp; + outbufp = outbuf; + anymacros |= gettokens(trp, 1); + trp->tp = trp->bp; + } + if (trp->tp->type == END) + { + if (--incdepth >= 0) + { + if (cursource->ifdepth) + error(ERROR, "Unterminated conditional in #include"); + unsetsource(); + cursource->line += cursource->lineinc; + trp->tp = trp->lp; + genline(); + continue; + } + if (ifdepth) + error(ERROR, "Unterminated #if/#ifdef/#ifndef"); + break; + } + if (trp->tp->type == SHARP) + { + trp->tp += 1; + control(trp); + } + else if (!skipping && anymacros) + expandrow(trp, NULL); + if (skipping) + setempty(trp); + puttokens(trp); + anymacros = 0; + cursource->line += cursource->lineinc; + if (cursource->lineinc > 1) + { + genline(); + } + } } - -void -control(Tokenrow *trp) -{ - Nlist *np; - Token *tp; - - tp = trp->tp; - if (tp->type!=NAME) { - if (tp->type==NUMBER) - goto kline; - if (tp->type != NL) - error(ERROR, "Unidentifiable control line"); - return; /* else empty line */ - } - if ((np = lookup(tp, 0))==NULL || ((np->flag&ISKW)==0 && !skipping)) { - error(WARNING, "Unknown preprocessor control %t", tp); - return; - } - if (skipping) { - switch (np->val) { - case KENDIF: - if (--ifdepth<skipping) - skipping = 0; - --cursource->ifdepth; - setempty(trp); - return; - - case KIFDEF: - case KIFNDEF: - case KIF: - if (++ifdepth >= NIF) - error(FATAL, "#if too deeply nested"); - ++cursource->ifdepth; - return; - - case KELIF: - case KELSE: - if (ifdepth<=skipping) - break; - return; - - default: - return; - } - } - switch (np->val) { - case KDEFINE: - dodefine(trp); - break; - - case KUNDEF: - tp += 1; - if (tp->type!=NAME || trp->lp - trp->bp != 4) { - error(ERROR, "Syntax error in #undef"); - break; - } - if ((np = lookup(tp, 0)) != NULL) - np->flag &= ~ISDEFINED; - break; - - case KPRAGMA: - return; - - case KIFDEF: - case KIFNDEF: - case KIF: - if (++ifdepth >= NIF) - error(FATAL, "#if too deeply nested"); - ++cursource->ifdepth; - ifsatisfied[ifdepth] = 0; - if (eval(trp, np->val)) - ifsatisfied[ifdepth] = 1; - else - skipping = ifdepth; - break; - case KELIF: - if (ifdepth==0) { - error(ERROR, "#elif with no #if"); - return; - } - if (ifsatisfied[ifdepth]==2) - error(ERROR, "#elif after #else"); - if (eval(trp, np->val)) { - if (ifsatisfied[ifdepth]) - skipping = ifdepth; - else { - skipping = 0; - ifsatisfied[ifdepth] = 1; - } - } else - skipping = ifdepth; - break; - - case KELSE: - if (ifdepth==0 || cursource->ifdepth==0) { - error(ERROR, "#else with no #if"); - return; - } - if (ifsatisfied[ifdepth]==2) - error(ERROR, "#else after #else"); - if (trp->lp - trp->bp != 3) - error(ERROR, "Syntax error in #else"); - skipping = ifsatisfied[ifdepth]? ifdepth: 0; - ifsatisfied[ifdepth] = 2; - break; - - case KENDIF: - if (ifdepth==0 || cursource->ifdepth==0) { - error(ERROR, "#endif with no #if"); - return; - } - --ifdepth; - --cursource->ifdepth; - if (trp->lp - trp->bp != 3) - error(WARNING, "Syntax error in #endif"); - break; - - case KWARNING: - trp->tp = tp+1; - error(WARNING, "#warning directive: %r", trp); - break; - - case KERROR: - trp->tp = tp+1; - error(ERROR, "#error directive: %r", trp); - break; - - case KLINE: - trp->tp = tp+1; - expandrow(trp, "<line>"); - tp = trp->bp+2; - kline: - if (tp+1>=trp->lp || tp->type!=NUMBER || tp+3<trp->lp - || ((tp+3==trp->lp && ((tp+1)->type!=STRING))||*(tp+1)->t=='L')){ - error(ERROR, "Syntax error in #line"); - return; - } - cursource->line = atol((char*)tp->t)-1; - if (cursource->line<0 || cursource->line>=32768) - error(WARNING, "#line specifies number out of range"); - tp = tp+1; - if (tp+1<trp->lp) - cursource->filename=(char*)newstring(tp->t+1,tp->len-2,0); - return; - - case KDEFINED: - error(ERROR, "Bad syntax for control line"); - break; - - case KINCLUDE: - doinclude(trp); - trp->lp = trp->bp; - return; - - case KEVAL: - eval(trp, np->val); - break; - - default: - error(ERROR, "Preprocessor control `%t' not yet implemented", tp); - break; - } - setempty(trp); +void control(Tokenrow *trp) +{ + Nlist *np; + Token *tp; + + tp = trp->tp; + if (tp->type != NAME) + { + if (tp->type == NUMBER) + goto kline; + if (tp->type != NL) + error(ERROR, "Unidentifiable control line"); + return; /* else empty line */ + } + if ((np = lookup(tp, 0)) == NULL || ((np->flag & ISKW) == 0 && !skipping)) + { + error(WARNING, "Unknown preprocessor control %t", tp); + return; + } + if (skipping) + { + switch (np->val) + { + case KENDIF: + if (--ifdepth < skipping) + skipping = 0; + --cursource->ifdepth; + setempty(trp); + return; + + case KIFDEF: + case KIFNDEF: + case KIF: + if (++ifdepth >= NIF) + error(FATAL, "#if too deeply nested"); + ++cursource->ifdepth; + return; + + case KELIF: + case KELSE: + if (ifdepth <= skipping) + break; + return; + + default: + return; + } + } + switch (np->val) + { + case KDEFINE: + dodefine(trp); + break; + + case KUNDEF: + tp += 1; + if (tp->type != NAME || trp->lp - trp->bp != 4) + { + error(ERROR, "Syntax error in #undef"); + break; + } + if ((np = lookup(tp, 0)) != NULL) + np->flag &= ~ISDEFINED; + break; + + case KPRAGMA: + return; + + case KIFDEF: + case KIFNDEF: + case KIF: + if (++ifdepth >= NIF) + error(FATAL, "#if too deeply nested"); + ++cursource->ifdepth; + ifsatisfied[ifdepth] = 0; + if (eval(trp, np->val)) + ifsatisfied[ifdepth] = 1; + else + skipping = ifdepth; + break; + + case KELIF: + if (ifdepth == 0) + { + error(ERROR, "#elif with no #if"); + return; + } + if (ifsatisfied[ifdepth] == 2) + error(ERROR, "#elif after #else"); + if (eval(trp, np->val)) + { + if (ifsatisfied[ifdepth]) + skipping = ifdepth; + else + { + skipping = 0; + ifsatisfied[ifdepth] = 1; + } + } + else + skipping = ifdepth; + break; + + case KELSE: + if (ifdepth == 0 || cursource->ifdepth == 0) + { + error(ERROR, "#else with no #if"); + return; + } + if (ifsatisfied[ifdepth] == 2) + error(ERROR, "#else after #else"); + if (trp->lp - trp->bp != 3) + error(ERROR, "Syntax error in #else"); + skipping = ifsatisfied[ifdepth] ? ifdepth : 0; + ifsatisfied[ifdepth] = 2; + break; + + case KENDIF: + if (ifdepth == 0 || cursource->ifdepth == 0) + { + error(ERROR, "#endif with no #if"); + return; + } + --ifdepth; + --cursource->ifdepth; + if (trp->lp - trp->bp != 3) + error(WARNING, "Syntax error in #endif"); + break; + + case KWARNING: + trp->tp = tp + 1; + error(WARNING, "#warning directive: %r", trp); + break; + + case KERROR: + trp->tp = tp + 1; + error(ERROR, "#error directive: %r", trp); + break; + + case KLINE: + trp->tp = tp + 1; + expandrow(trp, "<line>"); + tp = trp->bp + 2; + kline: + if (tp + 1 >= trp->lp || tp->type != NUMBER || tp + 3 < trp->lp || + ((tp + 3 == trp->lp && ((tp + 1)->type != STRING)) || *(tp + 1)->t == 'L')) + { + error(ERROR, "Syntax error in #line"); + return; + } + cursource->line = atol((char *)tp->t) - 1; + if (cursource->line < 0 || cursource->line >= 32768) + error(WARNING, "#line specifies number out of range"); + tp = tp + 1; + if (tp + 1 < trp->lp) + cursource->filename = (char *)newstring(tp->t + 1, tp->len - 2, 0); + return; + + case KDEFINED: + error(ERROR, "Bad syntax for control line"); + break; + + case KINCLUDE: + doinclude(trp); + trp->lp = trp->bp; + return; + + case KEVAL: + eval(trp, np->val); + break; + + default: + error(ERROR, "Preprocessor control `%t' not yet implemented", tp); + break; + } + setempty(trp); } -void * -domalloc(int size) +void *domalloc(int size) { - void *p = malloc(size); + void *p = malloc(size); - if (p==NULL) - error(FATAL, "Out of memory from malloc"); - return p; + if (p == NULL) + error(FATAL, "Out of memory from malloc"); + return p; } -void -dofree(void *p) -{ - free(p); -} +void dofree(void *p) { free(p); } -void -error(enum errtype type, char *string, ...) +void error(enum errtype type, char *string, ...) { - va_list ap; - char *cp, *ep; - Token *tp; - Tokenrow *trp; - Source *s; - int i; - - fprintf(stderr, "cpp: "); - for (s=cursource; s; s=s->next) - if (*s->filename) - fprintf(stderr, "%s:%d ", s->filename, s->line); - va_start(ap, string); - for (ep=string; *ep; ep++) { - if (*ep=='%') { - switch (*++ep) { - - case 's': - cp = va_arg(ap, char *); - fprintf(stderr, "%s", cp); - break; - case 'd': - i = va_arg(ap, int); - fprintf(stderr, "%d", i); - break; - case 't': - tp = va_arg(ap, Token *); - fprintf(stderr, "%.*s", tp->len, tp->t); - break; - - case 'r': - trp = va_arg(ap, Tokenrow *); - for (tp=trp->tp; tp<trp->lp&&tp->type!=NL; tp++) { - if (tp>trp->tp && tp->wslen) - fputc(' ', stderr); - fprintf(stderr, "%.*s", tp->len, tp->t); - } - break; - - default: - fputc(*ep, stderr); - break; - } - } else - fputc(*ep, stderr); - } - va_end(ap); - fputc('\n', stderr); - if (type==FATAL) - exit(1); - if (type!=WARNING) - nerrs = 1; - fflush(stderr); + va_list ap; + char *cp, *ep; + Token *tp; + Tokenrow *trp; + Source *s; + int i; + + fprintf(stderr, "cpp: "); + for (s = cursource; s; s = s->next) + if (*s->filename) + fprintf(stderr, "%s:%d ", s->filename, s->line); + va_start(ap, string); + for (ep = string; *ep; ep++) + { + if (*ep == '%') + { + switch (*++ep) + { + case 's': + cp = va_arg(ap, char *); + fprintf(stderr, "%s", cp); + break; + case 'd': + i = va_arg(ap, int); + fprintf(stderr, "%d", i); + break; + case 't': + tp = va_arg(ap, Token *); + fprintf(stderr, "%.*s", tp->len, tp->t); + break; + + case 'r': + trp = va_arg(ap, Tokenrow *); + for (tp = trp->tp; tp < trp->lp && tp->type != NL; tp++) + { + if (tp > trp->tp && tp->wslen) + fputc(' ', stderr); + fprintf(stderr, "%.*s", tp->len, tp->t); + } + break; + + default: + fputc(*ep, stderr); + break; + } + } + else + fputc(*ep, stderr); + } + va_end(ap); + fputc('\n', stderr); + if (type == FATAL) + exit(1); + if (type != WARNING) + nerrs = 1; + fflush(stderr); } diff --git a/src/tools/lcc/cpp/include.c b/src/tools/lcc/cpp/include.c index 5ecd8b3..778cea2 100644 --- a/src/tools/lcc/cpp/include.c +++ b/src/tools/lcc/cpp/include.c @@ -3,151 +3,171 @@ #include <string.h> #include "cpp.h" -Includelist includelist[NINCLUDE]; +Includelist includelist[NINCLUDE]; -extern char *objname; +extern char *objname; -void appendDirToIncludeList( char *dir ) +void appendDirToIncludeList(char *dir) { - int i; - char *fqdir; + int i; + char *fqdir; - fqdir = (char *)newstring( (uchar *)includelist[NINCLUDE-1].file, 256, 0 ); - strcat( fqdir, "/" ); - strcat( fqdir, dir ); + fqdir = (char *)newstring((uchar *)includelist[NINCLUDE - 1].file, 256, 0); + strcat(fqdir, "/"); + strcat(fqdir, dir); - //avoid adding it more than once - for (i=NINCLUDE-2; i>=0; i--) { - if (includelist[i].file && - !strcmp (includelist[i].file, fqdir)) { - return; - } - } + // avoid adding it more than once + for (i = NINCLUDE - 2; i >= 0; i--) + { + if (includelist[i].file && !strcmp(includelist[i].file, fqdir)) + { + return; + } + } - for (i=NINCLUDE-2; i>=0; i--) { - if (includelist[i].file==NULL) { - includelist[i].always = 1; - includelist[i].file = fqdir; - break; - } - } - if (i<0) - error(FATAL, "Too many -I directives"); + for (i = NINCLUDE - 2; i >= 0; i--) + { + if (includelist[i].file == NULL) + { + includelist[i].always = 1; + includelist[i].file = fqdir; + break; + } + } + if (i < 0) + error(FATAL, "Too many -I directives"); } -void -doinclude(Tokenrow *trp) +void doinclude(Tokenrow *trp) { - char fname[256], iname[256]; - Includelist *ip; - int angled, len, fd, i; + char fname[256], iname[256]; + Includelist *ip; + int angled, len, fd, i; - trp->tp += 1; - if (trp->tp>=trp->lp) - goto syntax; - if (trp->tp->type!=STRING && trp->tp->type!=LT) { - len = trp->tp - trp->bp; - expandrow(trp, "<include>"); - trp->tp = trp->bp+len; - } - if (trp->tp->type==STRING) { - len = trp->tp->len-2; - if (len > sizeof(fname) - 1) - len = sizeof(fname) - 1; - strncpy(fname, (char*)trp->tp->t+1, len); - angled = 0; - } else if (trp->tp->type==LT) { - len = 0; - trp->tp++; - while (trp->tp->type!=GT) { - if (trp->tp>trp->lp || len+trp->tp->len+2 >= sizeof(fname)) - goto syntax; - strncpy(fname+len, (char*)trp->tp->t, trp->tp->len); - len += trp->tp->len; - trp->tp++; - } - angled = 1; - } else - goto syntax; - trp->tp += 2; - if (trp->tp < trp->lp || len==0) - goto syntax; - fname[len] = '\0'; + trp->tp += 1; + if (trp->tp >= trp->lp) + goto syntax; + if (trp->tp->type != STRING && trp->tp->type != LT) + { + len = trp->tp - trp->bp; + expandrow(trp, "<include>"); + trp->tp = trp->bp + len; + } + if (trp->tp->type == STRING) + { + len = trp->tp->len - 2; + if (len > sizeof(fname) - 1) + len = sizeof(fname) - 1; + strncpy(fname, (char *)trp->tp->t + 1, len); + angled = 0; + } + else if (trp->tp->type == LT) + { + len = 0; + trp->tp++; + while (trp->tp->type != GT) + { + if (trp->tp > trp->lp || len + trp->tp->len + 2 >= sizeof(fname)) + goto syntax; + strncpy(fname + len, (char *)trp->tp->t, trp->tp->len); + len += trp->tp->len; + trp->tp++; + } + angled = 1; + } + else + goto syntax; + trp->tp += 2; + if (trp->tp < trp->lp || len == 0) + goto syntax; + fname[len] = '\0'; - appendDirToIncludeList( basepath( fname ) ); + appendDirToIncludeList(basepath(fname)); - if (fname[0]=='/') { - fd = open(fname, 0); - strcpy(iname, fname); - } else for (fd = -1,i=NINCLUDE-1; i>=0; i--) { - ip = &includelist[i]; - if (ip->file==NULL || ip->deleted || (angled && ip->always==0)) - continue; - if (strlen(fname)+strlen(ip->file)+2 > sizeof(iname)) - continue; - strcpy(iname, ip->file); - strcat(iname, "/"); - strcat(iname, fname); - if ((fd = open(iname, 0)) >= 0) - break; - } - if ( Mflag>1 || (!angled&&Mflag==1) ) { - write(1,objname,strlen(objname)); - write(1,iname,strlen(iname)); - write(1,"\n",1); - } - if (fd >= 0) { - if (++incdepth > 10) - error(FATAL, "#include too deeply nested"); - setsource((char*)newstring((uchar*)iname, strlen(iname), 0), fd, NULL); - genline(); - } else { - trp->tp = trp->bp+2; - error(ERROR, "Could not find include file %r", trp); - } - return; + if (fname[0] == '/') + { + fd = open(fname, 0); + strcpy(iname, fname); + } + else + for (fd = -1, i = NINCLUDE - 1; i >= 0; i--) + { + ip = &includelist[i]; + if (ip->file == NULL || ip->deleted || (angled && ip->always == 0)) + continue; + if (strlen(fname) + strlen(ip->file) + 2 > sizeof(iname)) + continue; + strcpy(iname, ip->file); + strcat(iname, "/"); + strcat(iname, fname); + if ((fd = open(iname, 0)) >= 0) + break; + } + if (Mflag > 1 || (!angled && Mflag == 1)) + { + write(1, objname, strlen(objname)); + write(1, iname, strlen(iname)); + write(1, "\n", 1); + } + if (fd >= 0) + { + if (++incdepth > 10) + error(FATAL, "#include too deeply nested"); + setsource((char *)newstring((uchar *)iname, strlen(iname), 0), fd, NULL); + genline(); + } + else + { + trp->tp = trp->bp + 2; + error(ERROR, "Could not find include file %r", trp); + } + return; syntax: - error(ERROR, "Syntax error in #include"); + error(ERROR, "Syntax error in #include"); } /* * Generate a line directive for cursource */ -void -genline(void) +void genline(void) { - static Token ta = { UNCLASS }; - static Tokenrow tr = { &ta, &ta, &ta+1, 1 }; - uchar *p; + static Token ta = {UNCLASS}; + static Tokenrow tr = {&ta, &ta, &ta + 1, 1}; + uchar *p; - ta.t = p = (uchar*)outbufp; - strcpy((char*)p, "#line "); - p += sizeof("#line ")-1; - p = (uchar*)outnum((char*)p, cursource->line); - *p++ = ' '; *p++ = '"'; - if (cursource->filename[0]!='/' && wd[0]) { - strcpy((char*)p, wd); - p += strlen(wd); - *p++ = '/'; - } - strcpy((char*)p, cursource->filename); - p += strlen((char*)p); - *p++ = '"'; *p++ = '\n'; - ta.len = (char*)p-outbufp; - outbufp = (char*)p; - tr.tp = tr.bp; - puttokens(&tr); + ta.t = p = (uchar *)outbufp; + strcpy((char *)p, "#line "); + p += sizeof("#line ") - 1; + p = (uchar *)outnum((char *)p, cursource->line); + *p++ = ' '; + *p++ = '"'; + if (cursource->filename[0] != '/' && wd[0]) + { + strcpy((char *)p, wd); + p += strlen(wd); + *p++ = '/'; + } + strcpy((char *)p, cursource->filename); + p += strlen((char *)p); + *p++ = '"'; + *p++ = '\n'; + ta.len = (char *)p - outbufp; + outbufp = (char *)p; + tr.tp = tr.bp; + puttokens(&tr); } -void -setobjname(char *f) +void setobjname(char *f) { - int n = strlen(f); - objname = (char*)domalloc(n+5); - strcpy(objname,f); - if(objname[n-2]=='.'){ - strcpy(objname+n-1,"$O: "); - }else{ - strcpy(objname+n,"$O: "); - } + int n = strlen(f); + objname = (char *)domalloc(n + 5); + strcpy(objname, f); + if (objname[n - 2] == '.') + { + strcpy(objname + n - 1, "$O: "); + } + else + { + strcpy(objname + n, "$O: "); + } } diff --git a/src/tools/lcc/cpp/unix.c b/src/tools/lcc/cpp/unix.c index ff1496a..ee975aa 100644 --- a/src/tools/lcc/cpp/unix.c +++ b/src/tools/lcc/cpp/unix.c @@ -1,134 +1,141 @@ -#include <stdio.h> #include <stddef.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include "cpp.h" -extern int lcc_getopt(int, char *const *, const char *); -extern char *optarg, rcsid[]; -extern int optind; -int verbose; -int Mflag; /* only print active include files */ -char *objname; /* "src.$O: " */ -int Cplusplus = 1; +extern int lcc_getopt(int, char *const *, const char *); +extern char *optarg, rcsid[]; +extern int optind; +int verbose; +int Mflag; /* only print active include files */ +char *objname; /* "src.$O: " */ +int Cplusplus = 1; -void -setup(int argc, char **argv) +void setup(int argc, char **argv) { - int c, fd, i; - char *fp, *dp; - Tokenrow tr; - extern void setup_kwtab(void); - uchar *includeDirs[ NINCLUDE ] = { 0 }; - int numIncludeDirs = 0; + int c, fd, i; + char *fp, *dp; + Tokenrow tr; + extern void setup_kwtab(void); + uchar *includeDirs[NINCLUDE] = {0}; + int numIncludeDirs = 0; - setup_kwtab(); - while ((c = lcc_getopt(argc, argv, "MNOVv+I:D:U:F:lg")) != -1) - switch (c) { - case 'N': - for (i=0; i<NINCLUDE; i++) - if (includelist[i].always==1) - includelist[i].deleted = 1; - break; - case 'I': - includeDirs[ numIncludeDirs++ ] = newstring( (uchar *)optarg, strlen( optarg ), 0 ); - break; - case 'D': - case 'U': - setsource("<cmdarg>", -1, optarg); - maketokenrow(3, &tr); - gettokens(&tr, 1); - doadefine(&tr, c); - unsetsource(); - break; - case 'M': - Mflag++; - break; - case 'v': - fprintf(stderr, "%s %s\n", argv[0], rcsid); - break; - case 'V': - verbose++; - break; - case '+': - Cplusplus++; - break; - default: - break; - } - dp = "."; - fp = "<stdin>"; - fd = 0; - if (optind<argc) { - dp = basepath( argv[optind] ); - fp = (char*)newstring((uchar*)argv[optind], strlen(argv[optind]), 0); - if ((fd = open(fp, 0)) <= 0) - error(FATAL, "Can't open input file %s", fp); - } - if (optind+1<argc) { - int fdo; + setup_kwtab(); + while ((c = lcc_getopt(argc, argv, "MNOVv+I:D:U:F:lg")) != -1) + switch (c) + { + case 'N': + for (i = 0; i < NINCLUDE; i++) + if (includelist[i].always == 1) + includelist[i].deleted = 1; + break; + case 'I': + includeDirs[numIncludeDirs++] = newstring((uchar *)optarg, strlen(optarg), 0); + break; + case 'D': + case 'U': + setsource("<cmdarg>", -1, optarg); + maketokenrow(3, &tr); + gettokens(&tr, 1); + doadefine(&tr, c); + unsetsource(); + break; + case 'M': + Mflag++; + break; + case 'v': + fprintf(stderr, "%s %s\n", argv[0], rcsid); + break; + case 'V': + verbose++; + break; + case '+': + Cplusplus++; + break; + default: + break; + } + dp = "."; + fp = "<stdin>"; + fd = 0; + if (optind < argc) + { + dp = basepath(argv[optind]); + fp = (char *)newstring((uchar *)argv[optind], strlen(argv[optind]), 0); + if ((fd = open(fp, 0)) <= 0) + error(FATAL, "Can't open input file %s", fp); + } + if (optind + 1 < argc) + { + int fdo; #ifdef WIN32 - fdo = creat(argv[optind+1], _S_IREAD | _S_IWRITE); + fdo = creat(argv[optind + 1], _S_IREAD | _S_IWRITE); #else - fdo = creat(argv[optind+1], 0666); + fdo = creat(argv[optind + 1], 0666); #endif - if (fdo<0) - error(FATAL, "Can't open output file %s", argv[optind+1]); - dup2(fdo, 1); - } - if(Mflag) - setobjname(fp); - includelist[NINCLUDE-1].always = 0; - includelist[NINCLUDE-1].file = dp; + if (fdo < 0) + error(FATAL, "Can't open output file %s", argv[optind + 1]); + dup2(fdo, 1); + } + if (Mflag) + setobjname(fp); + includelist[NINCLUDE - 1].always = 0; + includelist[NINCLUDE - 1].file = dp; - for( i = 0; i < numIncludeDirs; i++ ) - appendDirToIncludeList( (char *)includeDirs[ i ] ); + for (i = 0; i < numIncludeDirs; i++) + appendDirToIncludeList((char *)includeDirs[i]); - setsource(fp, fd, NULL); + setsource(fp, fd, NULL); } - -char *basepath( char *fname ) +char *basepath(char *fname) { - char *dp = "."; - char *p; - if ((p = strrchr(fname, '/')) != NULL) { - int dlen = p - fname; - dp = (char*)newstring((uchar*)fname, dlen+1, 0); - dp[dlen] = '\0'; - } + char *dp = "."; + char *p; + if ((p = strrchr(fname, '/')) != NULL) + { + int dlen = p - fname; + dp = (char *)newstring((uchar *)fname, dlen + 1, 0); + dp[dlen] = '\0'; + } - return dp; + return dp; } /* memmove is defined here because some vendors don't provide it at all and others do a terrible job (like calling malloc) */ // -- ouch, that hurts -- ln -#ifndef __APPLE__ /* always use the system memmove() on Mac OS X. --ryan. */ +/* always use the system memmove() on Mac OS X. --ryan. */ +#if !defined(__APPLE__) && !defined(_MSC_VER) #ifdef memmove #undef memmove #endif -void * -memmove(void *dp, const void *sp, size_t n) +void *memmove(void *dp, const void *sp, size_t n) { - unsigned char *cdp, *csp; + unsigned char *cdp, *csp; - if (n<=0) - return dp; - cdp = dp; - csp = (unsigned char *)sp; - if (cdp < csp) { - do { - *cdp++ = *csp++; - } while (--n); - } else { - cdp += n; - csp += n; - do { - *--cdp = *--csp; - } while (--n); - } - return dp; + if (n <= 0) + return dp; + cdp = dp; + csp = (unsigned char *)sp; + if (cdp < csp) + { + do + { + *cdp++ = *csp++; + } while (--n); + } + else + { + cdp += n; + csp += n; + do + { + *--cdp = *--csp; + } while (--n); + } + return dp; } #endif |