Itsa me, quake3io!

This commit is contained in:
Zachary Slater 2005-08-26 04:48:05 +00:00
parent dbe4ddb103
commit 5b755058f5
1409 changed files with 798983 additions and 798983 deletions

644
lcc/cpp/cpp.c Normal file → Executable file
View file

@ -1,322 +1,322 @@
#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 *outp = 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;
}
void
process(Tokenrow *trp)
{
int anymacros = 0;
for (;;) {
if (trp->tp >= trp->lp) {
trp->tp = trp->lp = trp->bp;
outp = 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 KERROR:
trp->tp = tp+1;
error(WARNING, "#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);
return;
}
void *
domalloc(int size)
{
void *p = malloc(size);
if (p==NULL)
error(FATAL, "Out of memory from malloc");
return p;
}
void
dofree(void *p)
{
free(p);
}
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);
}
#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 *outp = 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;
}
void
process(Tokenrow *trp)
{
int anymacros = 0;
for (;;) {
if (trp->tp >= trp->lp) {
trp->tp = trp->lp = trp->bp;
outp = 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 KERROR:
trp->tp = tp+1;
error(WARNING, "#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);
return;
}
void *
domalloc(int size)
{
void *p = malloc(size);
if (p==NULL)
error(FATAL, "Out of memory from malloc");
return p;
}
void
dofree(void *p)
{
free(p);
}
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);
}

326
lcc/cpp/cpp.h Normal file → Executable file
View file

@ -1,163 +1,163 @@
#define INS 32768 /* input buffer */
#define OBS 4096 /* outbut buffer */
#define NARG 32 /* Max number arguments to a macro */
#define NINCLUDE 32 /* Max number of include directories (-I) */
#define NIF 32 /* depth of nesting of #if */
#ifndef EOF
#define EOF (-1)
#endif
#ifndef NULL
#define NULL 0
#endif
#ifndef __alpha
typedef unsigned char uchar;
#endif
enum toktype { END, UNCLASS, NAME, NUMBER, STRING, CCON, NL, WS, DSHARP,
EQ, NEQ, LEQ, GEQ, LSH, RSH, LAND, LOR, PPLUS, MMINUS,
ARROW, SBRA, SKET, LP, RP, DOT, AND, STAR, PLUS, MINUS,
TILDE, NOT, SLASH, PCT, LT, GT, CIRC, OR, QUEST,
COLON, ASGN, COMMA, SHARP, SEMIC, CBRA, CKET,
ASPLUS, ASMINUS, ASSTAR, ASSLASH, ASPCT, ASCIRC, ASLSH,
ASRSH, ASOR, ASAND, ELLIPS,
DSHARP1, NAME1, DEFINED, UMINUS };
enum kwtype { KIF, KIFDEF, KIFNDEF, KELIF, KELSE, KENDIF, KINCLUDE, KDEFINE,
KUNDEF, KLINE, KERROR, KPRAGMA, KDEFINED,
KLINENO, KFILE, KDATE, KTIME, KSTDC, KEVAL };
#define ISDEFINED 01 /* has #defined value */
#define ISKW 02 /* is PP keyword */
#define ISUNCHANGE 04 /* can't be #defined in PP */
#define ISMAC 010 /* builtin macro, e.g. __LINE__ */
#define EOB 0xFE /* sentinel for end of input buffer */
#define EOFC 0xFD /* sentinel for end of input file */
#define XPWS 1 /* token flag: white space to assure token sep. */
typedef struct token {
unsigned char type;
unsigned char flag;
unsigned short hideset;
unsigned int wslen;
unsigned int len;
uchar *t;
} Token;
typedef struct tokenrow {
Token *tp; /* current one to scan */
Token *bp; /* base (allocated value) */
Token *lp; /* last+1 token used */
int max; /* number allocated */
} Tokenrow;
typedef struct source {
char *filename; /* name of file of the source */
int line; /* current line number */
int lineinc; /* adjustment for \\n lines */
uchar *inb; /* input buffer */
uchar *inp; /* input pointer */
uchar *inl; /* end of input */
int fd; /* input source */
int ifdepth; /* conditional nesting in include */
struct source *next; /* stack for #include */
} Source;
typedef struct nlist {
struct nlist *next;
uchar *name;
int len;
Tokenrow *vp; /* value as macro */
Tokenrow *ap; /* list of argument names, if any */
char val; /* value as preprocessor name */
char flag; /* is defined, is pp name */
} Nlist;
typedef struct includelist {
char deleted;
char always;
char *file;
} Includelist;
#define new(t) (t *)domalloc(sizeof(t))
#define quicklook(a,b) (namebit[(a)&077] & (1<<((b)&037)))
#define quickset(a,b) namebit[(a)&077] |= (1<<((b)&037))
extern unsigned long namebit[077+1];
enum errtype { WARNING, ERROR, FATAL };
void expandlex(void);
void fixlex(void);
void setup(int, char **);
int gettokens(Tokenrow *, int);
int comparetokens(Tokenrow *, Tokenrow *);
Source *setsource(char *, int, char *);
void unsetsource(void);
void puttokens(Tokenrow *);
void process(Tokenrow *);
void *domalloc(int);
void dofree(void *);
void error(enum errtype, char *, ...);
void flushout(void);
int fillbuf(Source *);
int trigraph(Source *);
int foldline(Source *);
Nlist *lookup(Token *, int);
void control(Tokenrow *);
void dodefine(Tokenrow *);
void doadefine(Tokenrow *, int);
void doinclude(Tokenrow *);
void doif(Tokenrow *, enum kwtype);
void expand(Tokenrow *, Nlist *);
void builtin(Tokenrow *, int);
int gatherargs(Tokenrow *, Tokenrow **, int *);
void substargs(Nlist *, Tokenrow *, Tokenrow **);
void expandrow(Tokenrow *, char *);
void maketokenrow(int, Tokenrow *);
Tokenrow *copytokenrow(Tokenrow *, Tokenrow *);
Token *growtokenrow(Tokenrow *);
Tokenrow *normtokenrow(Tokenrow *);
void adjustrow(Tokenrow *, int);
void movetokenrow(Tokenrow *, Tokenrow *);
void insertrow(Tokenrow *, int, Tokenrow *);
void peektokens(Tokenrow *, char *);
void doconcat(Tokenrow *);
Tokenrow *stringify(Tokenrow *);
int lookuparg(Nlist *, Token *);
long eval(Tokenrow *, int);
void genline(void);
void setempty(Tokenrow *);
void makespace(Tokenrow *);
char *outnum(char *, int);
int digit(int);
uchar *newstring(uchar *, int, int);
int checkhideset(int, Nlist *);
void prhideset(int);
int newhideset(int, Nlist *);
int unionhideset(int, int);
void iniths(void);
void setobjname(char *);
#define rowlen(tokrow) ((tokrow)->lp - (tokrow)->bp)
extern char *outp;
extern Token nltoken;
extern Source *cursource;
extern char *curtime;
extern int incdepth;
extern int ifdepth;
extern int ifsatisfied[NIF];
extern int Mflag;
extern int skipping;
extern int verbose;
extern int Cplusplus;
extern Nlist *kwdefined;
extern Includelist includelist[NINCLUDE];
extern char wd[];
extern int creat(char *, int);
extern int open(char *, int);
extern int close(int);
extern int dup2(int, int);
extern int write(int, char *, size_t);
extern int read(int, char *, size_t);
#define INS 32768 /* input buffer */
#define OBS 4096 /* outbut buffer */
#define NARG 32 /* Max number arguments to a macro */
#define NINCLUDE 32 /* Max number of include directories (-I) */
#define NIF 32 /* depth of nesting of #if */
#ifndef EOF
#define EOF (-1)
#endif
#ifndef NULL
#define NULL 0
#endif
#ifndef __alpha
typedef unsigned char uchar;
#endif
enum toktype { END, UNCLASS, NAME, NUMBER, STRING, CCON, NL, WS, DSHARP,
EQ, NEQ, LEQ, GEQ, LSH, RSH, LAND, LOR, PPLUS, MMINUS,
ARROW, SBRA, SKET, LP, RP, DOT, AND, STAR, PLUS, MINUS,
TILDE, NOT, SLASH, PCT, LT, GT, CIRC, OR, QUEST,
COLON, ASGN, COMMA, SHARP, SEMIC, CBRA, CKET,
ASPLUS, ASMINUS, ASSTAR, ASSLASH, ASPCT, ASCIRC, ASLSH,
ASRSH, ASOR, ASAND, ELLIPS,
DSHARP1, NAME1, DEFINED, UMINUS };
enum kwtype { KIF, KIFDEF, KIFNDEF, KELIF, KELSE, KENDIF, KINCLUDE, KDEFINE,
KUNDEF, KLINE, KERROR, KPRAGMA, KDEFINED,
KLINENO, KFILE, KDATE, KTIME, KSTDC, KEVAL };
#define ISDEFINED 01 /* has #defined value */
#define ISKW 02 /* is PP keyword */
#define ISUNCHANGE 04 /* can't be #defined in PP */
#define ISMAC 010 /* builtin macro, e.g. __LINE__ */
#define EOB 0xFE /* sentinel for end of input buffer */
#define EOFC 0xFD /* sentinel for end of input file */
#define XPWS 1 /* token flag: white space to assure token sep. */
typedef struct token {
unsigned char type;
unsigned char flag;
unsigned short hideset;
unsigned int wslen;
unsigned int len;
uchar *t;
} Token;
typedef struct tokenrow {
Token *tp; /* current one to scan */
Token *bp; /* base (allocated value) */
Token *lp; /* last+1 token used */
int max; /* number allocated */
} Tokenrow;
typedef struct source {
char *filename; /* name of file of the source */
int line; /* current line number */
int lineinc; /* adjustment for \\n lines */
uchar *inb; /* input buffer */
uchar *inp; /* input pointer */
uchar *inl; /* end of input */
int fd; /* input source */
int ifdepth; /* conditional nesting in include */
struct source *next; /* stack for #include */
} Source;
typedef struct nlist {
struct nlist *next;
uchar *name;
int len;
Tokenrow *vp; /* value as macro */
Tokenrow *ap; /* list of argument names, if any */
char val; /* value as preprocessor name */
char flag; /* is defined, is pp name */
} Nlist;
typedef struct includelist {
char deleted;
char always;
char *file;
} Includelist;
#define new(t) (t *)domalloc(sizeof(t))
#define quicklook(a,b) (namebit[(a)&077] & (1<<((b)&037)))
#define quickset(a,b) namebit[(a)&077] |= (1<<((b)&037))
extern unsigned long namebit[077+1];
enum errtype { WARNING, ERROR, FATAL };
void expandlex(void);
void fixlex(void);
void setup(int, char **);
int gettokens(Tokenrow *, int);
int comparetokens(Tokenrow *, Tokenrow *);
Source *setsource(char *, int, char *);
void unsetsource(void);
void puttokens(Tokenrow *);
void process(Tokenrow *);
void *domalloc(int);
void dofree(void *);
void error(enum errtype, char *, ...);
void flushout(void);
int fillbuf(Source *);
int trigraph(Source *);
int foldline(Source *);
Nlist *lookup(Token *, int);
void control(Tokenrow *);
void dodefine(Tokenrow *);
void doadefine(Tokenrow *, int);
void doinclude(Tokenrow *);
void doif(Tokenrow *, enum kwtype);
void expand(Tokenrow *, Nlist *);
void builtin(Tokenrow *, int);
int gatherargs(Tokenrow *, Tokenrow **, int *);
void substargs(Nlist *, Tokenrow *, Tokenrow **);
void expandrow(Tokenrow *, char *);
void maketokenrow(int, Tokenrow *);
Tokenrow *copytokenrow(Tokenrow *, Tokenrow *);
Token *growtokenrow(Tokenrow *);
Tokenrow *normtokenrow(Tokenrow *);
void adjustrow(Tokenrow *, int);
void movetokenrow(Tokenrow *, Tokenrow *);
void insertrow(Tokenrow *, int, Tokenrow *);
void peektokens(Tokenrow *, char *);
void doconcat(Tokenrow *);
Tokenrow *stringify(Tokenrow *);
int lookuparg(Nlist *, Token *);
long eval(Tokenrow *, int);
void genline(void);
void setempty(Tokenrow *);
void makespace(Tokenrow *);
char *outnum(char *, int);
int digit(int);
uchar *newstring(uchar *, int, int);
int checkhideset(int, Nlist *);
void prhideset(int);
int newhideset(int, Nlist *);
int unionhideset(int, int);
void iniths(void);
void setobjname(char *);
#define rowlen(tokrow) ((tokrow)->lp - (tokrow)->bp)
extern char *outp;
extern Token nltoken;
extern Source *cursource;
extern char *curtime;
extern int incdepth;
extern int ifdepth;
extern int ifsatisfied[NIF];
extern int Mflag;
extern int skipping;
extern int verbose;
extern int Cplusplus;
extern Nlist *kwdefined;
extern Includelist includelist[NINCLUDE];
extern char wd[];
extern int creat(char *, int);
extern int open(char *, int);
extern int close(int);
extern int dup2(int, int);
extern int write(int, char *, size_t);
extern int read(int, char *, size_t);

1040
lcc/cpp/eval.c Normal file → Executable file

File diff suppressed because it is too large Load diff

104
lcc/cpp/getopt.c Normal file → Executable file
View file

@ -1,52 +1,52 @@
#include <stdio.h>
#define EPR fprintf(stderr,
#define ERR(str, chr) if(opterr){EPR "%s%c\n", str, chr);}
int opterr = 1;
int optind = 1;
int optopt;
char *optarg;
char *strchr();
int
getopt (int argc, char *const argv[], const char *opts)
{
static int sp = 1;
int c;
char *cp;
if (sp == 1)
if (optind >= argc ||
argv[optind][0] != '-' || argv[optind][1] == '\0')
return -1;
else if (strcmp(argv[optind], "--") == 0) {
optind++;
return -1;
}
optopt = c = argv[optind][sp];
if (c == ':' || (cp=strchr(opts, c)) == 0) {
ERR (": illegal option -- ", c);
if (argv[optind][++sp] == '\0') {
optind++;
sp = 1;
}
return '?';
}
if (*++cp == ':') {
if (argv[optind][sp+1] != '\0')
optarg = &argv[optind++][sp+1];
else if (++optind >= argc) {
ERR (": option requires an argument -- ", c);
sp = 1;
return '?';
} else
optarg = argv[optind++];
sp = 1;
} else {
if (argv[optind][++sp] == '\0') {
sp = 1;
optind++;
}
optarg = 0;
}
return c;
}
#include <stdio.h>
#define EPR fprintf(stderr,
#define ERR(str, chr) if(opterr){EPR "%s%c\n", str, chr);}
int opterr = 1;
int optind = 1;
int optopt;
char *optarg;
char *strchr();
int
getopt (int argc, char *const argv[], const char *opts)
{
static int sp = 1;
int c;
char *cp;
if (sp == 1)
if (optind >= argc ||
argv[optind][0] != '-' || argv[optind][1] == '\0')
return -1;
else if (strcmp(argv[optind], "--") == 0) {
optind++;
return -1;
}
optopt = c = argv[optind][sp];
if (c == ':' || (cp=strchr(opts, c)) == 0) {
ERR (": illegal option -- ", c);
if (argv[optind][++sp] == '\0') {
optind++;
sp = 1;
}
return '?';
}
if (*++cp == ':') {
if (argv[optind][sp+1] != '\0')
optarg = &argv[optind++][sp+1];
else if (++optind >= argc) {
ERR (": option requires an argument -- ", c);
sp = 1;
return '?';
} else
optarg = argv[optind++];
sp = 1;
} else {
if (argv[optind][++sp] == '\0') {
sp = 1;
optind++;
}
optarg = 0;
}
return c;
}

224
lcc/cpp/hideset.c Normal file → Executable file
View file

@ -1,112 +1,112 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cpp.h"
/*
* A hideset is a null-terminated array of Nlist pointers.
* They are referred to by indices in the hidesets array.
* Hideset 0 is empty.
*/
#define HSSIZ 32
typedef Nlist **Hideset;
Hideset *hidesets;
int nhidesets = 0;
int maxhidesets = 3;
int inserths(Hideset, Hideset, Nlist *);
/*
* Test for membership in a hideset
*/
int
checkhideset(int hs, Nlist *np)
{
Hideset hsp;
if (hs>=nhidesets)
abort();
for (hsp = hidesets[hs]; *hsp; hsp++) {
if (*hsp == np)
return 1;
}
return 0;
}
/*
* Return the (possibly new) hideset obtained by adding np to hs.
*/
int
newhideset(int hs, Nlist *np)
{
int i, len;
Nlist *nhs[HSSIZ+3];
Hideset hs1, hs2;
len = inserths(nhs, hidesets[hs], np);
for (i=0; i<nhidesets; i++) {
for (hs1=nhs, hs2=hidesets[i]; *hs1==*hs2; hs1++, hs2++)
if (*hs1 == NULL)
return i;
}
if (len>=HSSIZ)
return hs;
if (nhidesets >= maxhidesets) {
maxhidesets = 3*maxhidesets/2+1;
hidesets = (Hideset *)realloc(hidesets, (sizeof (Hideset *))*maxhidesets);
if (hidesets == NULL)
error(FATAL, "Out of memory from realloc");
}
hs1 = (Hideset)domalloc(len*sizeof(Hideset));
memmove(hs1, nhs, len*sizeof(Hideset));
hidesets[nhidesets] = hs1;
return nhidesets++;
}
int
inserths(Hideset dhs, Hideset shs, Nlist *np)
{
Hideset odhs = dhs;
while (*shs && *shs < np)
*dhs++ = *shs++;
if (*shs != np)
*dhs++ = np;
do {
*dhs++ = *shs;
} while (*shs++);
return dhs - odhs;
}
/*
* Hideset union
*/
int
unionhideset(int hs1, int hs2)
{
Hideset hp;
for (hp = hidesets[hs2]; *hp; hp++)
hs1 = newhideset(hs1, *hp);
return hs1;
}
void
iniths(void)
{
hidesets = (Hideset *)domalloc(maxhidesets*sizeof(Hideset *));
hidesets[0] = (Hideset)domalloc(sizeof(Hideset));
*hidesets[0] = NULL;
nhidesets++;
}
void
prhideset(int hs)
{
Hideset np;
for (np = hidesets[hs]; *np; np++) {
fprintf(stderr, (char*)(*np)->name, (*np)->len);
fprintf(stderr, " ");
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cpp.h"
/*
* A hideset is a null-terminated array of Nlist pointers.
* They are referred to by indices in the hidesets array.
* Hideset 0 is empty.
*/
#define HSSIZ 32
typedef Nlist **Hideset;
Hideset *hidesets;
int nhidesets = 0;
int maxhidesets = 3;
int inserths(Hideset, Hideset, Nlist *);
/*
* Test for membership in a hideset
*/
int
checkhideset(int hs, Nlist *np)
{
Hideset hsp;
if (hs>=nhidesets)
abort();
for (hsp = hidesets[hs]; *hsp; hsp++) {
if (*hsp == np)
return 1;
}
return 0;
}
/*
* Return the (possibly new) hideset obtained by adding np to hs.
*/
int
newhideset(int hs, Nlist *np)
{
int i, len;
Nlist *nhs[HSSIZ+3];
Hideset hs1, hs2;
len = inserths(nhs, hidesets[hs], np);
for (i=0; i<nhidesets; i++) {
for (hs1=nhs, hs2=hidesets[i]; *hs1==*hs2; hs1++, hs2++)
if (*hs1 == NULL)
return i;
}
if (len>=HSSIZ)
return hs;
if (nhidesets >= maxhidesets) {
maxhidesets = 3*maxhidesets/2+1;
hidesets = (Hideset *)realloc(hidesets, (sizeof (Hideset *))*maxhidesets);
if (hidesets == NULL)
error(FATAL, "Out of memory from realloc");
}
hs1 = (Hideset)domalloc(len*sizeof(Hideset));
memmove(hs1, nhs, len*sizeof(Hideset));
hidesets[nhidesets] = hs1;
return nhidesets++;
}
int
inserths(Hideset dhs, Hideset shs, Nlist *np)
{
Hideset odhs = dhs;
while (*shs && *shs < np)
*dhs++ = *shs++;
if (*shs != np)
*dhs++ = np;
do {
*dhs++ = *shs;
} while (*shs++);
return dhs - odhs;
}
/*
* Hideset union
*/
int
unionhideset(int hs1, int hs2)
{
Hideset hp;
for (hp = hidesets[hs2]; *hp; hp++)
hs1 = newhideset(hs1, *hp);
return hs1;
}
void
iniths(void)
{
hidesets = (Hideset *)domalloc(maxhidesets*sizeof(Hideset *));
hidesets[0] = (Hideset)domalloc(sizeof(Hideset));
*hidesets[0] = NULL;
nhidesets++;
}
void
prhideset(int hs)
{
Hideset np;
for (np = hidesets[hs]; *np; np++) {
fprintf(stderr, (char*)(*np)->name, (*np)->len);
fprintf(stderr, " ");
}
}

244
lcc/cpp/include.c Normal file → Executable file
View file

@ -1,122 +1,122 @@
#include <stdlib.h>
#include <string.h>
#include "cpp.h"
Includelist includelist[NINCLUDE];
extern char *objname;
void
doinclude(Tokenrow *trp)
{
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';
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");
return;
}
/*
* Generate a line directive for cursource
*/
void
genline(void)
{
static Token ta = { UNCLASS };
static Tokenrow tr = { &ta, &ta, &ta+1, 1 };
uchar *p;
ta.t = p = (uchar*)outp;
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-outp;
outp = (char*)p;
tr.tp = tr.bp;
puttokens(&tr);
}
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: ");
}
}
#include <stdlib.h>
#include <string.h>
#include "cpp.h"
Includelist includelist[NINCLUDE];
extern char *objname;
void
doinclude(Tokenrow *trp)
{
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';
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");
return;
}
/*
* Generate a line directive for cursource
*/
void
genline(void)
{
static Token ta = { UNCLASS };
static Tokenrow tr = { &ta, &ta, &ta+1, 1 };
uchar *p;
ta.t = p = (uchar*)outp;
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-outp;
outp = (char*)p;
tr.tp = tr.bp;
puttokens(&tr);
}
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: ");
}
}

1162
lcc/cpp/lex.c Normal file → Executable file

File diff suppressed because it is too large Load diff

1030
lcc/cpp/macro.c Normal file → Executable file

File diff suppressed because it is too large Load diff

208
lcc/cpp/nlist.c Normal file → Executable file
View file

@ -1,104 +1,104 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cpp.h"
extern int getopt(int, char *const *, const char *);
extern char *optarg;
extern int optind;
extern int verbose;
extern int Cplusplus;
Nlist *kwdefined;
char wd[128];
#define NLSIZE 128
static Nlist *nlist[NLSIZE];
struct kwtab {
char *kw;
int val;
int flag;
} kwtab[] = {
"if", KIF, ISKW,
"ifdef", KIFDEF, ISKW,
"ifndef", KIFNDEF, ISKW,
"elif", KELIF, ISKW,
"else", KELSE, ISKW,
"endif", KENDIF, ISKW,
"include", KINCLUDE, ISKW,
"define", KDEFINE, ISKW,
"undef", KUNDEF, ISKW,
"line", KLINE, ISKW,
"error", KERROR, ISKW,
"pragma", KPRAGMA, ISKW,
"eval", KEVAL, ISKW,
"defined", KDEFINED, ISDEFINED+ISUNCHANGE,
"__LINE__", KLINENO, ISMAC+ISUNCHANGE,
"__FILE__", KFILE, ISMAC+ISUNCHANGE,
"__DATE__", KDATE, ISMAC+ISUNCHANGE,
"__TIME__", KTIME, ISMAC+ISUNCHANGE,
"__STDC__", KSTDC, ISUNCHANGE,
NULL
};
unsigned long namebit[077+1];
Nlist *np;
void
setup_kwtab(void)
{
struct kwtab *kp;
Nlist *np;
Token t;
static Token deftoken[1] = {{ NAME, 0, 0, 0, 7, (uchar*)"defined" }};
static Tokenrow deftr = { deftoken, deftoken, deftoken+1, 1 };
for (kp=kwtab; kp->kw; kp++) {
t.t = (uchar*)kp->kw;
t.len = strlen(kp->kw);
np = lookup(&t, 1);
np->flag = kp->flag;
np->val = kp->val;
if (np->val == KDEFINED) {
kwdefined = np;
np->val = NAME;
np->vp = &deftr;
np->ap = 0;
}
}
}
Nlist *
lookup(Token *tp, int install)
{
unsigned int h;
Nlist *np;
uchar *cp, *cpe;
h = 0;
for (cp=tp->t, cpe=cp+tp->len; cp<cpe; )
h += *cp++;
h %= NLSIZE;
np = nlist[h];
while (np) {
if (*tp->t==*np->name && tp->len==np->len
&& strncmp((char*)tp->t, (char*)np->name, tp->len)==0)
return np;
np = np->next;
}
if (install) {
np = new(Nlist);
np->vp = NULL;
np->ap = NULL;
np->flag = 0;
np->val = 0;
np->len = tp->len;
np->name = newstring(tp->t, tp->len, 0);
np->next = nlist[h];
nlist[h] = np;
quickset(tp->t[0], tp->len>1? tp->t[1]:0);
return np;
}
return NULL;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cpp.h"
extern int getopt(int, char *const *, const char *);
extern char *optarg;
extern int optind;
extern int verbose;
extern int Cplusplus;
Nlist *kwdefined;
char wd[128];
#define NLSIZE 128
static Nlist *nlist[NLSIZE];
struct kwtab {
char *kw;
int val;
int flag;
} kwtab[] = {
"if", KIF, ISKW,
"ifdef", KIFDEF, ISKW,
"ifndef", KIFNDEF, ISKW,
"elif", KELIF, ISKW,
"else", KELSE, ISKW,
"endif", KENDIF, ISKW,
"include", KINCLUDE, ISKW,
"define", KDEFINE, ISKW,
"undef", KUNDEF, ISKW,
"line", KLINE, ISKW,
"error", KERROR, ISKW,
"pragma", KPRAGMA, ISKW,
"eval", KEVAL, ISKW,
"defined", KDEFINED, ISDEFINED+ISUNCHANGE,
"__LINE__", KLINENO, ISMAC+ISUNCHANGE,
"__FILE__", KFILE, ISMAC+ISUNCHANGE,
"__DATE__", KDATE, ISMAC+ISUNCHANGE,
"__TIME__", KTIME, ISMAC+ISUNCHANGE,
"__STDC__", KSTDC, ISUNCHANGE,
NULL
};
unsigned long namebit[077+1];
Nlist *np;
void
setup_kwtab(void)
{
struct kwtab *kp;
Nlist *np;
Token t;
static Token deftoken[1] = {{ NAME, 0, 0, 0, 7, (uchar*)"defined" }};
static Tokenrow deftr = { deftoken, deftoken, deftoken+1, 1 };
for (kp=kwtab; kp->kw; kp++) {
t.t = (uchar*)kp->kw;
t.len = strlen(kp->kw);
np = lookup(&t, 1);
np->flag = kp->flag;
np->val = kp->val;
if (np->val == KDEFINED) {
kwdefined = np;
np->val = NAME;
np->vp = &deftr;
np->ap = 0;
}
}
}
Nlist *
lookup(Token *tp, int install)
{
unsigned int h;
Nlist *np;
uchar *cp, *cpe;
h = 0;
for (cp=tp->t, cpe=cp+tp->len; cp<cpe; )
h += *cp++;
h %= NLSIZE;
np = nlist[h];
while (np) {
if (*tp->t==*np->name && tp->len==np->len
&& strncmp((char*)tp->t, (char*)np->name, tp->len)==0)
return np;
np = np->next;
}
if (install) {
np = new(Nlist);
np->vp = NULL;
np->ap = NULL;
np->flag = 0;
np->val = 0;
np->len = tp->len;
np->name = newstring(tp->t, tp->len, 0);
np->next = nlist[h];
nlist[h] = np;
quickset(tp->t[0], tp->len>1? tp->t[1]:0);
return np;
}
return NULL;
}

740
lcc/cpp/tokens.c Normal file → Executable file
View file

@ -1,370 +1,370 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cpp.h"
static char wbuf[2*OBS];
static char *wbp = wbuf;
/*
* 1 for tokens that don't need whitespace when they get inserted
* by macro expansion
*/
static const char wstab[] = {
0, /* END */
0, /* UNCLASS */
0, /* NAME */
0, /* NUMBER */
0, /* STRING */
0, /* CCON */
1, /* NL */
0, /* WS */
0, /* DSHARP */
0, /* EQ */
0, /* NEQ */
0, /* LEQ */
0, /* GEQ */
0, /* LSH */
0, /* RSH */
0, /* LAND */
0, /* LOR */
0, /* PPLUS */
0, /* MMINUS */
0, /* ARROW */
1, /* SBRA */
1, /* SKET */
1, /* LP */
1, /* RP */
0, /* DOT */
0, /* AND */
0, /* STAR */
0, /* PLUS */
0, /* MINUS */
0, /* TILDE */
0, /* NOT */
0, /* SLASH */
0, /* PCT */
0, /* LT */
0, /* GT */
0, /* CIRC */
0, /* OR */
0, /* QUEST */
0, /* COLON */
0, /* ASGN */
1, /* COMMA */
0, /* SHARP */
1, /* SEMIC */
1, /* CBRA */
1, /* CKET */
0, /* ASPLUS */
0, /* ASMINUS */
0, /* ASSTAR */
0, /* ASSLASH */
0, /* ASPCT */
0, /* ASCIRC */
0, /* ASLSH */
0, /* ASRSH */
0, /* ASOR */
0, /* ASAND */
0, /* ELLIPS */
0, /* DSHARP1 */
0, /* NAME1 */
0, /* DEFINED */
0, /* UMINUS */
};
void
maketokenrow(int size, Tokenrow *trp)
{
trp->max = size;
if (size>0)
trp->bp = (Token *)domalloc(size*sizeof(Token));
else
trp->bp = NULL;
trp->tp = trp->bp;
trp->lp = trp->bp;
}
Token *
growtokenrow(Tokenrow *trp)
{
int ncur = trp->tp - trp->bp;
int nlast = trp->lp - trp->bp;
trp->max = 3*trp->max/2 + 1;
trp->bp = (Token *)realloc(trp->bp, trp->max*sizeof(Token));
if (trp->bp == NULL)
error(FATAL, "Out of memory from realloc");
trp->lp = &trp->bp[nlast];
trp->tp = &trp->bp[ncur];
return trp->lp;
}
/*
* Compare a row of tokens, ignoring the content of WS; return !=0 if different
*/
int
comparetokens(Tokenrow *tr1, Tokenrow *tr2)
{
Token *tp1, *tp2;
tp1 = tr1->tp;
tp2 = tr2->tp;
if (tr1->lp-tp1 != tr2->lp-tp2)
return 1;
for (; tp1<tr1->lp ; tp1++, tp2++) {
if (tp1->type != tp2->type
|| (tp1->wslen==0) != (tp2->wslen==0)
|| tp1->len != tp2->len
|| strncmp((char*)tp1->t, (char*)tp2->t, tp1->len)!=0)
return 1;
}
return 0;
}
/*
* replace ntok tokens starting at dtr->tp with the contents of str.
* tp ends up pointing just beyond the replacement.
* Canonical whitespace is assured on each side.
*/
void
insertrow(Tokenrow *dtr, int ntok, Tokenrow *str)
{
int nrtok = rowlen(str);
dtr->tp += ntok;
adjustrow(dtr, nrtok-ntok);
dtr->tp -= ntok;
movetokenrow(dtr, str);
makespace(dtr);
dtr->tp += nrtok;
makespace(dtr);
}
/*
* make sure there is WS before trp->tp, if tokens might merge in the output
*/
void
makespace(Tokenrow *trp)
{
uchar *tt;
Token *tp = trp->tp;
if (tp >= trp->lp)
return;
if (tp->wslen) {
if (tp->flag&XPWS
&& (wstab[tp->type] || trp->tp>trp->bp && wstab[(tp-1)->type])) {
tp->wslen = 0;
return;
}
tp->t[-1] = ' ';
return;
}
if (wstab[tp->type] || trp->tp>trp->bp && wstab[(tp-1)->type])
return;
tt = newstring(tp->t, tp->len, 1);
*tt++ = ' ';
tp->t = tt;
tp->wslen = 1;
tp->flag |= XPWS;
}
/*
* Copy an entire tokenrow into another, at tp.
* It is assumed that there is enough space.
* Not strictly conforming.
*/
void
movetokenrow(Tokenrow *dtr, Tokenrow *str)
{
int nby;
/* nby = sizeof(Token) * (str->lp - str->bp); */
nby = (char *)str->lp - (char *)str->bp;
memmove(dtr->tp, str->bp, nby);
}
/*
* Move the tokens in a row, starting at tr->tp, rightward by nt tokens;
* nt may be negative (left move).
* The row may need to be grown.
* Non-strictly conforming because of the (char *), but easily fixed
*/
void
adjustrow(Tokenrow *trp, int nt)
{
int nby, size;
if (nt==0)
return;
size = (trp->lp - trp->bp) + nt;
while (size > trp->max)
growtokenrow(trp);
/* nby = sizeof(Token) * (trp->lp - trp->tp); */
nby = (char *)trp->lp - (char *)trp->tp;
if (nby)
memmove(trp->tp+nt, trp->tp, nby);
trp->lp += nt;
}
/*
* Copy a row of tokens into the destination holder, allocating
* the space for the contents. Return the destination.
*/
Tokenrow *
copytokenrow(Tokenrow *dtr, Tokenrow *str)
{
int len = rowlen(str);
maketokenrow(len, dtr);
movetokenrow(dtr, str);
dtr->lp += len;
return dtr;
}
/*
* Produce a copy of a row of tokens. Start at trp->tp.
* The value strings are copied as well. The first token
* has WS available.
*/
Tokenrow *
normtokenrow(Tokenrow *trp)
{
Token *tp;
Tokenrow *ntrp = new(Tokenrow);
int len;
len = trp->lp - trp->tp;
if (len<=0)
len = 1;
maketokenrow(len, ntrp);
for (tp=trp->tp; tp < trp->lp; tp++) {
*ntrp->lp = *tp;
if (tp->len) {
ntrp->lp->t = newstring(tp->t, tp->len, 1);
*ntrp->lp->t++ = ' ';
if (tp->wslen)
ntrp->lp->wslen = 1;
}
ntrp->lp++;
}
if (ntrp->lp > ntrp->bp)
ntrp->bp->wslen = 0;
return ntrp;
}
/*
* Debugging
*/
void
peektokens(Tokenrow *trp, char *str)
{
Token *tp;
tp = trp->tp;
flushout();
if (str)
fprintf(stderr, "%s ", str);
if (tp<trp->bp || tp>trp->lp)
fprintf(stderr, "(tp offset %d) ", tp-trp->bp);
for (tp=trp->bp; tp<trp->lp && tp<trp->bp+32; tp++) {
if (tp->type!=NL) {
int c = tp->t[tp->len];
tp->t[tp->len] = 0;
fprintf(stderr, "%s", tp->t);
tp->t[tp->len] = c;
}
if (tp->type==NAME) {
fprintf(stderr, tp==trp->tp?"{*":"{");
prhideset(tp->hideset);
fprintf(stderr, "} ");
} else
fprintf(stderr, tp==trp->tp?"{%x*} ":"{%x} ", tp->type);
}
fprintf(stderr, "\n");
fflush(stderr);
}
void
puttokens(Tokenrow *trp)
{
Token *tp;
int len;
uchar *p;
if (verbose)
peektokens(trp, "");
tp = trp->bp;
for (; tp<trp->lp; tp++) {
len = tp->len+tp->wslen;
p = tp->t-tp->wslen;
while (tp<trp->lp-1 && p+len == (tp+1)->t - (tp+1)->wslen) {
tp++;
len += tp->wslen+tp->len;
}
if (len>OBS/2) { /* handle giant token */
if (wbp > wbuf)
write(1, wbuf, wbp-wbuf);
write(1, (char *)p, len);
wbp = wbuf;
} else {
memcpy(wbp, p, len);
wbp += len;
}
if (wbp >= &wbuf[OBS]) {
write(1, wbuf, OBS);
if (wbp > &wbuf[OBS])
memcpy(wbuf, wbuf+OBS, wbp - &wbuf[OBS]);
wbp -= OBS;
}
}
trp->tp = tp;
if (cursource->fd==0)
flushout();
}
void
flushout(void)
{
if (wbp>wbuf) {
write(1, wbuf, wbp-wbuf);
wbp = wbuf;
}
}
/*
* turn a row into just a newline
*/
void
setempty(Tokenrow *trp)
{
trp->tp = trp->bp;
trp->lp = trp->bp+1;
*trp->bp = nltoken;
}
/*
* generate a number
*/
char *
outnum(char *p, int n)
{
if (n>=10)
p = outnum(p, n/10);
*p++ = n%10 + '0';
return p;
}
/*
* allocate and initialize a new string from s, of length l, at offset o
* Null terminated.
*/
uchar *
newstring(uchar *s, int l, int o)
{
uchar *ns = (uchar *)domalloc(l+o+1);
ns[l+o] = '\0';
return (uchar*)strncpy((char*)ns+o, (char*)s, l) - o;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cpp.h"
static char wbuf[2*OBS];
static char *wbp = wbuf;
/*
* 1 for tokens that don't need whitespace when they get inserted
* by macro expansion
*/
static const char wstab[] = {
0, /* END */
0, /* UNCLASS */
0, /* NAME */
0, /* NUMBER */
0, /* STRING */
0, /* CCON */
1, /* NL */
0, /* WS */
0, /* DSHARP */
0, /* EQ */
0, /* NEQ */
0, /* LEQ */
0, /* GEQ */
0, /* LSH */
0, /* RSH */
0, /* LAND */
0, /* LOR */
0, /* PPLUS */
0, /* MMINUS */
0, /* ARROW */
1, /* SBRA */
1, /* SKET */
1, /* LP */
1, /* RP */
0, /* DOT */
0, /* AND */
0, /* STAR */
0, /* PLUS */
0, /* MINUS */
0, /* TILDE */
0, /* NOT */
0, /* SLASH */
0, /* PCT */
0, /* LT */
0, /* GT */
0, /* CIRC */
0, /* OR */
0, /* QUEST */
0, /* COLON */
0, /* ASGN */
1, /* COMMA */
0, /* SHARP */
1, /* SEMIC */
1, /* CBRA */
1, /* CKET */
0, /* ASPLUS */
0, /* ASMINUS */
0, /* ASSTAR */
0, /* ASSLASH */
0, /* ASPCT */
0, /* ASCIRC */
0, /* ASLSH */
0, /* ASRSH */
0, /* ASOR */
0, /* ASAND */
0, /* ELLIPS */
0, /* DSHARP1 */
0, /* NAME1 */
0, /* DEFINED */
0, /* UMINUS */
};
void
maketokenrow(int size, Tokenrow *trp)
{
trp->max = size;
if (size>0)
trp->bp = (Token *)domalloc(size*sizeof(Token));
else
trp->bp = NULL;
trp->tp = trp->bp;
trp->lp = trp->bp;
}
Token *
growtokenrow(Tokenrow *trp)
{
int ncur = trp->tp - trp->bp;
int nlast = trp->lp - trp->bp;
trp->max = 3*trp->max/2 + 1;
trp->bp = (Token *)realloc(trp->bp, trp->max*sizeof(Token));
if (trp->bp == NULL)
error(FATAL, "Out of memory from realloc");
trp->lp = &trp->bp[nlast];
trp->tp = &trp->bp[ncur];
return trp->lp;
}
/*
* Compare a row of tokens, ignoring the content of WS; return !=0 if different
*/
int
comparetokens(Tokenrow *tr1, Tokenrow *tr2)
{
Token *tp1, *tp2;
tp1 = tr1->tp;
tp2 = tr2->tp;
if (tr1->lp-tp1 != tr2->lp-tp2)
return 1;
for (; tp1<tr1->lp ; tp1++, tp2++) {
if (tp1->type != tp2->type
|| (tp1->wslen==0) != (tp2->wslen==0)
|| tp1->len != tp2->len
|| strncmp((char*)tp1->t, (char*)tp2->t, tp1->len)!=0)
return 1;
}
return 0;
}
/*
* replace ntok tokens starting at dtr->tp with the contents of str.
* tp ends up pointing just beyond the replacement.
* Canonical whitespace is assured on each side.
*/
void
insertrow(Tokenrow *dtr, int ntok, Tokenrow *str)
{
int nrtok = rowlen(str);
dtr->tp += ntok;
adjustrow(dtr, nrtok-ntok);
dtr->tp -= ntok;
movetokenrow(dtr, str);
makespace(dtr);
dtr->tp += nrtok;
makespace(dtr);
}
/*
* make sure there is WS before trp->tp, if tokens might merge in the output
*/
void
makespace(Tokenrow *trp)
{
uchar *tt;
Token *tp = trp->tp;
if (tp >= trp->lp)
return;
if (tp->wslen) {
if (tp->flag&XPWS
&& (wstab[tp->type] || trp->tp>trp->bp && wstab[(tp-1)->type])) {
tp->wslen = 0;
return;
}
tp->t[-1] = ' ';
return;
}
if (wstab[tp->type] || trp->tp>trp->bp && wstab[(tp-1)->type])
return;
tt = newstring(tp->t, tp->len, 1);
*tt++ = ' ';
tp->t = tt;
tp->wslen = 1;
tp->flag |= XPWS;
}
/*
* Copy an entire tokenrow into another, at tp.
* It is assumed that there is enough space.
* Not strictly conforming.
*/
void
movetokenrow(Tokenrow *dtr, Tokenrow *str)
{
int nby;
/* nby = sizeof(Token) * (str->lp - str->bp); */
nby = (char *)str->lp - (char *)str->bp;
memmove(dtr->tp, str->bp, nby);
}
/*
* Move the tokens in a row, starting at tr->tp, rightward by nt tokens;
* nt may be negative (left move).
* The row may need to be grown.
* Non-strictly conforming because of the (char *), but easily fixed
*/
void
adjustrow(Tokenrow *trp, int nt)
{
int nby, size;
if (nt==0)
return;
size = (trp->lp - trp->bp) + nt;
while (size > trp->max)
growtokenrow(trp);
/* nby = sizeof(Token) * (trp->lp - trp->tp); */
nby = (char *)trp->lp - (char *)trp->tp;
if (nby)
memmove(trp->tp+nt, trp->tp, nby);
trp->lp += nt;
}
/*
* Copy a row of tokens into the destination holder, allocating
* the space for the contents. Return the destination.
*/
Tokenrow *
copytokenrow(Tokenrow *dtr, Tokenrow *str)
{
int len = rowlen(str);
maketokenrow(len, dtr);
movetokenrow(dtr, str);
dtr->lp += len;
return dtr;
}
/*
* Produce a copy of a row of tokens. Start at trp->tp.
* The value strings are copied as well. The first token
* has WS available.
*/
Tokenrow *
normtokenrow(Tokenrow *trp)
{
Token *tp;
Tokenrow *ntrp = new(Tokenrow);
int len;
len = trp->lp - trp->tp;
if (len<=0)
len = 1;
maketokenrow(len, ntrp);
for (tp=trp->tp; tp < trp->lp; tp++) {
*ntrp->lp = *tp;
if (tp->len) {
ntrp->lp->t = newstring(tp->t, tp->len, 1);
*ntrp->lp->t++ = ' ';
if (tp->wslen)
ntrp->lp->wslen = 1;
}
ntrp->lp++;
}
if (ntrp->lp > ntrp->bp)
ntrp->bp->wslen = 0;
return ntrp;
}
/*
* Debugging
*/
void
peektokens(Tokenrow *trp, char *str)
{
Token *tp;
tp = trp->tp;
flushout();
if (str)
fprintf(stderr, "%s ", str);
if (tp<trp->bp || tp>trp->lp)
fprintf(stderr, "(tp offset %d) ", tp-trp->bp);
for (tp=trp->bp; tp<trp->lp && tp<trp->bp+32; tp++) {
if (tp->type!=NL) {
int c = tp->t[tp->len];
tp->t[tp->len] = 0;
fprintf(stderr, "%s", tp->t);
tp->t[tp->len] = c;
}
if (tp->type==NAME) {
fprintf(stderr, tp==trp->tp?"{*":"{");
prhideset(tp->hideset);
fprintf(stderr, "} ");
} else
fprintf(stderr, tp==trp->tp?"{%x*} ":"{%x} ", tp->type);
}
fprintf(stderr, "\n");
fflush(stderr);
}
void
puttokens(Tokenrow *trp)
{
Token *tp;
int len;
uchar *p;
if (verbose)
peektokens(trp, "");
tp = trp->bp;
for (; tp<trp->lp; tp++) {
len = tp->len+tp->wslen;
p = tp->t-tp->wslen;
while (tp<trp->lp-1 && p+len == (tp+1)->t - (tp+1)->wslen) {
tp++;
len += tp->wslen+tp->len;
}
if (len>OBS/2) { /* handle giant token */
if (wbp > wbuf)
write(1, wbuf, wbp-wbuf);
write(1, (char *)p, len);
wbp = wbuf;
} else {
memcpy(wbp, p, len);
wbp += len;
}
if (wbp >= &wbuf[OBS]) {
write(1, wbuf, OBS);
if (wbp > &wbuf[OBS])
memcpy(wbuf, wbuf+OBS, wbp - &wbuf[OBS]);
wbp -= OBS;
}
}
trp->tp = tp;
if (cursource->fd==0)
flushout();
}
void
flushout(void)
{
if (wbp>wbuf) {
write(1, wbuf, wbp-wbuf);
wbp = wbuf;
}
}
/*
* turn a row into just a newline
*/
void
setempty(Tokenrow *trp)
{
trp->tp = trp->bp;
trp->lp = trp->bp+1;
*trp->bp = nltoken;
}
/*
* generate a number
*/
char *
outnum(char *p, int n)
{
if (n>=10)
p = outnum(p, n/10);
*p++ = n%10 + '0';
return p;
}
/*
* allocate and initialize a new string from s, of length l, at offset o
* Null terminated.
*/
uchar *
newstring(uchar *s, int l, int o)
{
uchar *ns = (uchar *)domalloc(l+o+1);
ns[l+o] = '\0';
return (uchar*)strncpy((char*)ns+o, (char*)s, l) - o;
}

232
lcc/cpp/unix.c Normal file → Executable file
View file

@ -1,116 +1,116 @@
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "cpp.h"
extern int 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)
{
int c, fd, i;
char *fp, *dp;
Tokenrow tr;
extern void setup_kwtab(void);
setup_kwtab();
while ((c = 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':
for (i=NINCLUDE-2; i>=0; i--) {
if (includelist[i].file==NULL) {
includelist[i].always = 1;
includelist[i].file = optarg;
break;
}
}
if (i<0)
error(FATAL, "Too many -I directives");
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) {
if ((fp = strrchr(argv[optind], '/')) != NULL) {
int len = fp - argv[optind];
dp = (char*)newstring((uchar*)argv[optind], len+1, 0);
dp[len] = '\0';
}
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 = creat(argv[optind+1], 0666);
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;
setsource(fp, fd, NULL);
}
/* memmove is defined here because some vendors don't provide it at
all and others do a terrible job (like calling malloc) */
void *
memmove(void *dp, const void *sp, size_t n)
{
unsigned char *cdp, *csp;
if (n<=0)
return 0;
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 0;
}
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "cpp.h"
extern int 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)
{
int c, fd, i;
char *fp, *dp;
Tokenrow tr;
extern void setup_kwtab(void);
setup_kwtab();
while ((c = 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':
for (i=NINCLUDE-2; i>=0; i--) {
if (includelist[i].file==NULL) {
includelist[i].always = 1;
includelist[i].file = optarg;
break;
}
}
if (i<0)
error(FATAL, "Too many -I directives");
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) {
if ((fp = strrchr(argv[optind], '/')) != NULL) {
int len = fp - argv[optind];
dp = (char*)newstring((uchar*)argv[optind], len+1, 0);
dp[len] = '\0';
}
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 = creat(argv[optind+1], 0666);
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;
setsource(fp, fd, NULL);
}
/* memmove is defined here because some vendors don't provide it at
all and others do a terrible job (like calling malloc) */
void *
memmove(void *dp, const void *sp, size_t n)
{
unsigned char *cdp, *csp;
if (n<=0)
return 0;
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 0;
}