⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ncpp.c

📁 window下的c编译器。
💻 C
📖 第 1 页 / 共 5 页
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <fcntl.h>
#ifndef UNIX_COMPILE
#include <windows.h>
#include <io.h>
#endif
#pragma pack(1)
#define PATH_SEPARATOR '\\'
#define BUG_LCC_CORRECTED
extern FILE *xrefFile;
#define INS (32*1024)           /* 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
extern int outputLine;
extern char *OutputFileName;
static FILE *TraceIncludesFile;
typedef unsigned char uchar;
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,
	FLOATK,
	DOUBLEK,
	CHARK,
	SHORTK,
	INTK,
	UNSIGNEDK,
	VOIDK,
	STRUCTK,
	UNION,
	ENUM,
	LONGK,
	CONSTK,
	VOLATILEK,
	SIZEOFK,
	AUTOK,
	BREAK,
	CASE,
	CONTINUE,
	DEFAULT,
	DO,
	ELSE,
	EXTERNK,
	FOR,
	GOTO,
	IF,
	REGISTER,
	RETURN,
	SIGNED,
	STATICK,
	SWITCH,
	TYPEDEF,
	WHILE,
	PASCALKEYW
};
extern char *StrTab[];
enum kwtype {
	KIF, KIFDEF, KIFNDEF, KELIF, KELSE, KENDIF, KINCLUDE, KDEFINE,
	KUNDEF, KLINE, KERROR, KWARNING, KPRAGMA, KDEFINED,
	KLINENO, KFILE, KDATE, KTIME, KSTDC, KEVAL, KIMPORT
};
#define ISDEFINED   0x1         /* has #defined value */
#define ISKW        0x2         /* is PP keyword */
#define ISUNCHANGE  0x4         /* can't be #defined in PP */
#define ISMAC       0x8         /* builtin macro, e.g. __LINE__ */
#define ISUSED      0x10        /* is used somwhere */
#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;
	int len;
	int h;
	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 */
	unsigned short srcline;
	uchar *filename;
	uchar name[1];
}    Nlist;
typedef struct includelist {
	unsigned char deleted;
	unsigned char always;
	unsigned 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))
static unsigned long namebit[077 + 1];
enum errtype {
	CPPWARNING, CPPERROR, CPPFATAL
};
static void expandlex(void);
static void fixlex(void);
static void setup(unsigned char *);
static int gettokens(Tokenrow *, int);
static int comparetokens(Tokenrow *, Tokenrow *);
static Source *setsource(unsigned char *, int, char *);
static void unsetsource(void);
static int puttokens(Tokenrow *);
static int process(Tokenrow *);
static void *_stdcall domalloc(int);
static void _stdcall dofree(void *);
static void error(enum errtype, char *,...);
static void flushout(void);
static int fillbuf(Source *);
static int trigraph(Source *);
static int foldline(Source *);
#ifndef ASM_LIB
static Nlist *cpplookup(Token *);
#else
Nlist *cpplookup(Token *);
#endif
static Nlist *InstallToken(Token *);
static void control(Tokenrow *);
static Nlist *dodefine(Tokenrow *);
static Nlist *doadefine(Tokenrow *, int);
static void doinclude(Tokenrow *,int);
static void doif(Tokenrow *, enum kwtype);
static void expand(Tokenrow *, Nlist *);
static void builtin(Tokenrow *, int);
static int gatherargs(Tokenrow *, Tokenrow **, int *);
static void substargs(Nlist *, Tokenrow *, Tokenrow **);
static void expandrow(Tokenrow *, unsigned char *);
static void maketokenrow(int, Tokenrow *);
static Tokenrow *copytokenrow(Tokenrow *, Tokenrow *);
static Token *growtokenrow(Tokenrow *);
static Tokenrow *normtokenrow(Tokenrow *);
static void adjustrow(Tokenrow *, int);
static void insertrow(Tokenrow *, int, Tokenrow *);
static void peektokens(Tokenrow *, char *);
static void doconcat(Tokenrow *);
static Tokenrow *stringify(Tokenrow *);
static int lookuparg(Nlist *, Token *);
static long eval(Tokenrow *, int);
static int genline(void);
static void setempty(Tokenrow *);
static void makespace(Tokenrow *);
static char *outnum(char *, int);
static int digit(int);
static unsigned char *newstring(unsigned char *, int, int);
static int checkhideset(int, Nlist *);
static void prhideset(int);
static int newhideset(int, Nlist *);
static int unionhideset(int, int);
static void iniths(void);
static void setobjname(unsigned char *);
#ifndef LRC
static void dodeclspec(Tokenrow * trp);
#endif
#define rowlen(tokrow)  ((tokrow)->lp - (tokrow)->bp)
#define OUTS    4096
static char outbuf[OUTS + OUTS];
static char *OutputPtr = outbuf;
static Source *cursource;
static int nerrs;
static struct token nltoken = {NL, 0, 0, 0, 1, (uchar *) "\n"};
static char *curtime;
static int incdepth;
static int ifdepth;
static int ifsatisfied[NIF];
static int skipping;
static int Cplusplus;
static Nlist *kwdefined;
static char wd[128];
int Mflag;                      /* only print active include files */
static int Cplusplus = 1;
long CharsRead = 0;
#ifndef LRC
int dotokfile = 0;
int TraceDependencies = 0;
#endif
#define NLSIZE  1024
static unsigned char wbuf[4 * OBS];
static unsigned char *wbp = wbuf;
static int PartialSize = 0;
#ifdef ASM_LIB
Nlist *nlist[NLSIZE];
#else
static Nlist *nlist[NLSIZE];
#endif
extern void *allocate(unsigned long n, unsigned a);
static 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,
	"import", KIMPORT, ISKW,
	"define", KDEFINE, ISKW,
	"undef", KUNDEF, ISKW,
	"line", KLINE, ISKW,
	"error", KERROR, ISKW,
	"warning",KWARNING, 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
};
#define movetokenrow(dtr,str) memmove(dtr->tp, str->bp, (char *)str->lp - (char *)str->bp)
#ifndef LRC
typedef struct tagFileList {
	struct tagFileList *Next;
	char *fName;
}          FILELIST;
static FILELIST *fList;
void AddToFileTable(char *fname)
{
	FILELIST *rvp;
	if (fList == NULL) {
		fList = malloc(sizeof(FILELIST));
		rvp = fList;
	}
	else {
		rvp = fList;
		while (rvp->Next) {
			rvp = rvp->Next;
		}
		rvp->Next = malloc(sizeof(FILELIST));
		rvp = rvp->Next;
	}
	rvp->Next = NULL;
	rvp->fName = malloc(strlen(fname) + 1);
	strcpy(rvp->fName, fname);
}
int GetFileNumber(char *fname)
{
	FILELIST *rvp;
	int i;
	rvp = fList;
	i = 1;
	while (rvp) {
		if (!strcmp(fname, rvp->fName))
			return (i);
		i++;
		rvp = rvp->Next;
	}
	return (0);
}
void PrintIncludeTree(char *iname)
{
	Source *rvp;
	if (cursource) {
		fprintf(TraceIncludesFile,"%s: line %d --> %s\n", cursource->filename, cursource->line, iname);
		rvp = cursource->next;
		while (rvp) {
			fprintf(TraceIncludesFile,"%s: line %d\n", rvp->filename, rvp->line);
			rvp = rvp->next;
		}
		fprintf(TraceIncludesFile,"---\n");
	}
}
#endif                          // LRC
#ifdef UNIX_COMPILE
int DoOpen(char *name)
{
	return (open(name, 0));
}
int DoRead(int h, char *buf, int siz)
{
	return (read(h, buf, siz));
}
static int DoClose(int h)
{
	return (close(h));
}
#else
static int DoOpen(unsigned char *fname)
{
	void *fh;
	fh = CreateFileA(fname,
					GENERIC_READ,
					FILE_SHARE_READ,
					NULL,      /* no security */
					OPEN_EXISTING,
					FILE_ATTRIBUTE_NORMAL,
					0);
	if (fh == (void *) INVALID_HANDLE_VALUE)
		return -1;
	return ((int) fh);
}
int ReadTime;
#ifdef ASM_LIB
extern int DoRead(int, char *, int);
#else
static int DoRead(int hf, char *buf, int siz)
{
	unsigned long br;
	register char *p1, *p2;
	br = 0;
	ReadFile((void *) hf, buf, siz, &br, 0);
	CharsRead += br;
	{
		unsigned int i;
		p1 = p2 = buf;
		i = 0;
		while (i++ < br) {
			if (*p1 != '\r') {
				*p2++ = *p1;
			}
			p1++;
		}
	}
	return (br - (p1 - p2));
}
#endif
static int DoClose(int fh)
{
	return (CloseHandle((void *) fh));
}
#endif                          /* UNIX_COMPILE */
static int outfile;
static char *IncPath;
static Tokenrow TokenRow;
#ifndef LRC
static FILE *tokfile;
static unsigned char tbuf[512];
static unsigned char *TokFileName;
#endif
int CppInit(void)
{
	time_t t;
	outfile = 1;
/*  setbuf(stderr, ebuf);*/
	t = time(NULL);
	curtime = ctime(&t);
	maketokenrow(3, &TokenRow);
	expandlex();
#ifndef LRC
	if (Mflag) {
		TraceDependencies = 1;
	}
#endif
	return (1);
}
int StartCpp(unsigned char *filename)
{
	setup(filename);
	fixlex();
	iniths();
#ifndef LRC
	if (dotokfile) {
		tokfile = fopen(TokFileName, "wb");
		tbuf[0] = 0;
	}
#endif
	genline();
	return (1);
}
#ifndef LRC
int FinishCpp(void)
{
	fflush(stderr);
	return nerrs;
}
#endif
static int anymacros = 0;
static int process(Tokenrow * trp)
{
	int readMore = 1;
	int isEOF = 0;
	while (readMore) {
		if (trp->tp >= trp->lp) {
			trp->tp = trp->lp = trp->bp;
			OutputPtr = outbuf;
			anymacros |= gettokens(trp, 1);
			trp->tp = trp->bp;
		}
		if (trp->tp->type == END) {
			if (--incdepth >= 0) {
				if (cursource->ifdepth)
					error(CPPERROR,
						StrTab[101]); // <Unterminated conditional in #include>
				unsetsource();
				cursource->line += cursource->lineinc;
				trp->tp = trp->lp;
				genline();
				continue;
			}
			isEOF = 1;
			if (ifdepth)
				error(CPPERROR, StrTab[102]);   // <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);
		readMore = puttokens(trp);
		anymacros = 0;
		cursource->line += cursource->lineinc;
		if (cursource->lineinc > 1) {
			readMore = genline();
		}
	}
	return (isEOF);
}
static int isEOF = 0;
int ReadFromCpp(unsigned char *buf, int siz)
{
	int r = 0;
	unsigned char *stop;
	extern clock_t CppTime;
	stop = &wbuf[OBS];
#ifndef LRC
	if (TraceDependencies) {
		while (!isEOF) {
			isEOF = process(&TokenRow);
			wbp = wbuf;
		}
		if (TraceIncludesFile && TraceIncludesFile != stdout)
			fclose(TraceIncludesFile);	
		return (0);
	}
#endif
	if (isEOF) {
		r = (wbp - wbuf);
		if (r > siz) {
			memcpy(buf, wbuf, siz);
			memcpy(wbuf, wbuf + siz, r - siz);
			wbp = wbuf + r - siz;
			return (siz);
		}
		else if (r)
			memcpy(buf, wbuf, r);
		wbp -= r;
		return (r);
	}
	if (wbp < stop) {
		do {
			isEOF = process(&TokenRow);
		} while (isEOF == 0 && wbp < stop);
	}
	if (wbp >= stop) {
		memcpy(buf, wbuf, OBS);
		r = OBS;
		if (wbp > stop)
			memcpy(wbuf, wbuf + OBS, wbp - stop);
		wbp -= OBS;
	}
	else {
		r = (wbp - wbuf);
		memcpy(buf, wbuf, r);
		wbp -= r;
	}
	return (r);
}
static 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(CPPERROR, StrTab[103]);   // <Unidentifiable control line>
		return;                 /* else empty line */
	}
	if ((np = cpplookup(tp)) == NULL || (np->flag & ISKW) == 0 && !skipping) {
		error(CPPWARNING, StrTab[104], tp); // <Unknown preprocessor control %t>
		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(CPPFATAL, StrTab[105]);   // <#if too deeply nested>
			++cursource->ifdepth;
			return;
		case KELIF:
		case KELSE:
			if (ifdepth <= skipping)
				break;
			return;
		default:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -