1989-10-23 10:35:56 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
|
|
|
/* $Header$ */
|
|
|
|
/* PREPROCESSOR DRIVER */
|
|
|
|
|
|
|
|
#include <system.h>
|
1990-01-26 14:49:13 +00:00
|
|
|
#include <alloc.h>
|
1989-10-23 10:35:56 +00:00
|
|
|
#include "input.h"
|
|
|
|
#include "obufsize.h"
|
|
|
|
#include "arith.h"
|
|
|
|
#include "LLlex.h"
|
|
|
|
#include "class.h"
|
|
|
|
#include "macro.h"
|
|
|
|
#include "idf.h"
|
|
|
|
#include "idfsize.h"
|
|
|
|
#include "bits.h"
|
1990-03-12 13:28:12 +00:00
|
|
|
#include "ln_prefix.h"
|
1990-01-26 14:49:13 +00:00
|
|
|
#include "textsize.h"
|
1989-10-23 10:35:56 +00:00
|
|
|
|
|
|
|
char _obuf[OBUFSIZE];
|
|
|
|
#ifdef DOBITS
|
|
|
|
char bits[128];
|
|
|
|
#endif
|
|
|
|
extern int InputLevel;
|
|
|
|
|
|
|
|
Xflush()
|
|
|
|
{
|
|
|
|
sys_write(STDOUT, _obuf, OBUFSIZE);
|
|
|
|
}
|
|
|
|
|
1989-11-22 12:59:15 +00:00
|
|
|
static char *SkipComment();
|
|
|
|
extern char options[];
|
|
|
|
|
1990-01-26 14:49:13 +00:00
|
|
|
/* #pragma directives are saved here and passed to the compiler later on.
|
|
|
|
*/
|
|
|
|
struct prag_info {
|
|
|
|
int pr_linnr;
|
|
|
|
char *pr_fil;
|
|
|
|
char *pr_text;
|
|
|
|
};
|
|
|
|
static struct prag_info *pragma_tab;
|
|
|
|
static int pragma_nr;
|
|
|
|
|
|
|
|
do_pragma()
|
|
|
|
{
|
|
|
|
register int size = ITEXTSIZE;
|
|
|
|
char *cur_line = Malloc(size);
|
|
|
|
register char *c_ptr = cur_line;
|
|
|
|
register int c = GetChar();
|
1991-01-03 13:50:57 +00:00
|
|
|
register int delim = 0;
|
1990-01-26 14:49:13 +00:00
|
|
|
|
|
|
|
while(c != '\n') {
|
|
|
|
if (c_ptr + 1 - cur_line == size) {
|
1991-01-03 13:50:57 +00:00
|
|
|
cur_line = Realloc(cur_line, (unsigned)(size + ITEXTSIZE));
|
1990-01-26 14:49:13 +00:00
|
|
|
c_ptr = cur_line + size - 1;
|
1991-01-03 13:50:57 +00:00
|
|
|
size += ITEXTSIZE;
|
|
|
|
}
|
|
|
|
if (delim) {
|
|
|
|
if (c == delim) {
|
|
|
|
delim = 0;
|
|
|
|
}
|
|
|
|
else if (c == '\\') {
|
|
|
|
*c_ptr++ = c;
|
|
|
|
c = GetChar();
|
|
|
|
if (c == '\n') break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (c == '\'' || c == '"') {
|
|
|
|
delim = c;
|
|
|
|
}
|
|
|
|
else if (c == '/') {
|
|
|
|
if ((c = GetChar()) != '*' || InputLevel) {
|
|
|
|
*c_ptr++ = '/';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
skipcomment();
|
|
|
|
continue;
|
|
|
|
}
|
1990-01-26 14:49:13 +00:00
|
|
|
}
|
|
|
|
*c_ptr++ = c;
|
|
|
|
c = GetChar();
|
|
|
|
}
|
|
|
|
*c_ptr = '\0';
|
|
|
|
if (!pragma_nr) {
|
|
|
|
pragma_tab = (struct prag_info *)Malloc(sizeof(struct prag_info));
|
|
|
|
} else {
|
1990-10-02 17:19:17 +00:00
|
|
|
pragma_tab = (struct prag_info *)Realloc((char *)pragma_tab
|
|
|
|
, (unsigned)(sizeof(struct prag_info) * (pragma_nr+1)));
|
1990-01-26 14:49:13 +00:00
|
|
|
}
|
1991-01-03 13:50:57 +00:00
|
|
|
if (delim) {
|
|
|
|
error("unclosed opening %c", delim);
|
|
|
|
}
|
1990-01-26 14:49:13 +00:00
|
|
|
pragma_tab[pragma_nr].pr_linnr = LineNumber;
|
|
|
|
pragma_tab[pragma_nr].pr_fil = FileName;
|
|
|
|
pragma_tab[pragma_nr].pr_text = cur_line;
|
|
|
|
pragma_nr++;
|
|
|
|
LineNumber++;
|
|
|
|
}
|
|
|
|
|
1989-10-23 10:35:56 +00:00
|
|
|
preprocess(fn)
|
|
|
|
char *fn;
|
|
|
|
{
|
|
|
|
register int c;
|
|
|
|
register char *op = _obuf;
|
|
|
|
register char *ob = &_obuf[OBUFSIZE];
|
|
|
|
char Xbuf[256];
|
|
|
|
int lineno = 0;
|
1989-11-22 12:59:15 +00:00
|
|
|
int startline;
|
1989-10-23 10:35:56 +00:00
|
|
|
|
|
|
|
#define flush(X) (sys_write(STDOUT,_obuf,X))
|
|
|
|
#define echo(ch) if (op == ob) { Xflush(); op = _obuf; } *op++ = (ch);
|
|
|
|
#define newline() echo('\n')
|
|
|
|
|
|
|
|
if (!options['P']) {
|
|
|
|
/* Generate a line directive communicating the
|
|
|
|
source filename
|
|
|
|
*/
|
|
|
|
register char *p = Xbuf;
|
|
|
|
|
|
|
|
sprint(p, "%s 1 \"%s\"\n",
|
|
|
|
LINE_PREFIX,
|
|
|
|
FileName);
|
|
|
|
while (*p) {
|
|
|
|
echo(*p++);
|
|
|
|
}
|
|
|
|
}
|
1990-01-26 14:49:13 +00:00
|
|
|
|
|
|
|
#define do_line_dir(lineno, fn) \
|
1989-10-23 10:35:56 +00:00
|
|
|
if (lineno != LineNumber || fn != FileName) { \
|
|
|
|
fn = FileName; \
|
|
|
|
lineno = LineNumber; \
|
|
|
|
if (! options['P']) { \
|
|
|
|
register char *p = Xbuf; \
|
|
|
|
\
|
|
|
|
sprint(p, "%s %d \"%s\"\n", \
|
|
|
|
LINE_PREFIX, \
|
|
|
|
LineNumber, \
|
|
|
|
FileName); \
|
|
|
|
while (*p) { \
|
|
|
|
echo(*p++); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
LineNumber++;
|
|
|
|
lineno++;
|
1989-11-22 12:59:15 +00:00
|
|
|
startline = 1;
|
1989-10-23 10:35:56 +00:00
|
|
|
c = GetChar();
|
1989-11-22 12:59:15 +00:00
|
|
|
while (startline) {
|
1990-01-26 14:49:13 +00:00
|
|
|
/* first flush the saved pragma's */
|
|
|
|
if (pragma_nr) {
|
|
|
|
register int i = 0;
|
|
|
|
int LiNo = LineNumber;
|
|
|
|
char *FiNam = FileName;
|
|
|
|
|
|
|
|
while (i < pragma_nr) {
|
|
|
|
register char *c_ptr = "#pragma";
|
|
|
|
|
|
|
|
LineNumber = pragma_tab[i].pr_linnr;
|
|
|
|
FileName = pragma_tab[i].pr_fil;
|
|
|
|
do_line_dir(lineno, fn);
|
|
|
|
while (*c_ptr) { echo(*c_ptr++); }
|
|
|
|
c_ptr = pragma_tab[i].pr_text;
|
|
|
|
while (*c_ptr) { echo(*c_ptr++); }
|
|
|
|
newline(); lineno++;
|
|
|
|
free(pragma_tab[i].pr_text);
|
|
|
|
i++;
|
|
|
|
}
|
1990-10-02 17:19:17 +00:00
|
|
|
free((char *) pragma_tab);
|
1990-01-26 14:49:13 +00:00
|
|
|
pragma_tab = (struct prag_info *)0;
|
|
|
|
pragma_nr = 0;
|
|
|
|
LineNumber = LiNo;
|
|
|
|
FileName = FiNam;
|
|
|
|
do_line_dir(lineno, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1989-11-22 12:59:15 +00:00
|
|
|
while (class(c) == STSKIP || c == '/') {
|
|
|
|
if (c == '/') {
|
|
|
|
if (!InputLevel) {
|
|
|
|
c = GetChar();
|
|
|
|
if (c == '*') {
|
|
|
|
op = SkipComment(op, &lineno);
|
|
|
|
if (!op) return;
|
1990-03-13 14:33:43 +00:00
|
|
|
if (!options['C']) { echo(' '); }
|
1989-11-22 12:59:15 +00:00
|
|
|
c = GetChar();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
UnGetChar();
|
|
|
|
c = '/';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
1989-10-23 10:35:56 +00:00
|
|
|
echo(c);
|
|
|
|
c = GetChar();
|
1989-11-22 12:59:15 +00:00
|
|
|
}
|
1989-10-23 10:35:56 +00:00
|
|
|
|
1989-11-22 12:59:15 +00:00
|
|
|
if (c == '#') {
|
1990-01-26 14:49:13 +00:00
|
|
|
domacro();
|
1989-10-23 10:35:56 +00:00
|
|
|
lineno++;
|
|
|
|
newline();
|
|
|
|
c = GetChar();
|
1989-11-22 12:59:15 +00:00
|
|
|
} else startline = 0;
|
1989-10-23 10:35:56 +00:00
|
|
|
}
|
1990-01-26 14:49:13 +00:00
|
|
|
do_line_dir(lineno, fn);
|
1989-10-23 10:35:56 +00:00
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
/* illegal character */
|
|
|
|
if (c & 0200) {
|
|
|
|
if (c == EOI) {
|
|
|
|
newline();
|
1990-04-25 15:10:13 +00:00
|
|
|
flush((int)(op-_obuf));
|
1989-10-23 10:35:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
fatal("non-ascii character read");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* comments */
|
|
|
|
if (c == '/' && !InputLevel) {
|
|
|
|
c = GetChar();
|
|
|
|
if (c == '*') {
|
1989-11-22 12:59:15 +00:00
|
|
|
op = SkipComment(op, &lineno);
|
|
|
|
if (!op) return;
|
1990-03-13 14:33:43 +00:00
|
|
|
if (!options['C']) { echo(' '); }
|
1989-10-23 10:35:56 +00:00
|
|
|
c = GetChar();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
echo('/');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* switch on character */
|
|
|
|
switch(class(c)) {
|
|
|
|
case STNL:
|
|
|
|
echo(c);
|
|
|
|
break;
|
|
|
|
case STSTR:
|
|
|
|
case STCHAR:
|
|
|
|
{
|
|
|
|
register int stopc = c;
|
1990-08-31 10:27:12 +00:00
|
|
|
int escaped;
|
1989-10-23 10:35:56 +00:00
|
|
|
|
|
|
|
do {
|
1990-08-31 10:27:12 +00:00
|
|
|
escaped = 0;
|
1989-10-23 10:35:56 +00:00
|
|
|
echo(c);
|
|
|
|
c = GetChar();
|
|
|
|
if (c == '\n') {
|
1990-04-25 15:10:13 +00:00
|
|
|
/* the compiler will complain */
|
1989-10-23 10:35:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (c == EOI) {
|
|
|
|
newline();
|
1990-04-25 15:10:13 +00:00
|
|
|
flush((int)(op-_obuf));
|
1989-10-23 10:35:56 +00:00
|
|
|
return;
|
|
|
|
}
|
1990-08-31 10:27:12 +00:00
|
|
|
if (c == '\\') {
|
1989-10-23 10:35:56 +00:00
|
|
|
echo(c);
|
|
|
|
c = GetChar();
|
|
|
|
if (c == '\n') {
|
|
|
|
++LineNumber;
|
|
|
|
lineno++;
|
1990-04-25 15:10:13 +00:00
|
|
|
} else escaped = 1;
|
1990-08-31 10:27:12 +00:00
|
|
|
}
|
1989-10-23 10:35:56 +00:00
|
|
|
} while (escaped || c != stopc);
|
|
|
|
echo(c);
|
|
|
|
if (c == '\n')
|
|
|
|
break; /* Don't eat # */
|
|
|
|
c = GetChar();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case STNUM:
|
1989-10-31 10:02:48 +00:00
|
|
|
/* The following code is quit ugly. This because
|
|
|
|
* ..3 == . .3 , whereas ...3 == ... 3
|
|
|
|
*/
|
1989-10-23 10:35:56 +00:00
|
|
|
echo(c);
|
|
|
|
if (c == '.') {
|
|
|
|
c = GetChar();
|
|
|
|
if (c == '.') {
|
|
|
|
if ((c = GetChar()) == '.') {
|
|
|
|
echo('.'); echo('.');
|
1989-10-31 10:02:48 +00:00
|
|
|
c = GetChar();
|
1989-10-23 10:35:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
UnGetChar();
|
|
|
|
c = '.';
|
|
|
|
continue;
|
|
|
|
} else if (!is_dig(c)) {
|
|
|
|
continue;
|
1990-03-13 14:33:43 +00:00
|
|
|
} else { echo(c); }
|
1989-10-23 10:35:56 +00:00
|
|
|
}
|
|
|
|
c = GetChar();
|
|
|
|
while (in_idf(c) || c == '.') {
|
|
|
|
echo(c);
|
|
|
|
if (c == 'e' || c == 'E') {
|
|
|
|
c = GetChar();
|
|
|
|
if (c == '+' || c == '-') {
|
|
|
|
echo(c);
|
|
|
|
c = GetChar();
|
|
|
|
}
|
|
|
|
} else c = GetChar();
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case STELL:
|
1989-10-24 15:06:23 +00:00
|
|
|
c = GetChar();
|
|
|
|
UnGetChar();
|
1989-10-23 10:35:56 +00:00
|
|
|
if (c == '"' || c == '\'') {
|
1989-10-24 15:06:23 +00:00
|
|
|
echo('L');
|
1989-10-23 10:35:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
c = 'L';
|
|
|
|
case STIDF: {
|
|
|
|
extern int idfsize; /* ??? */
|
|
|
|
char buf[IDFSIZE + 1];
|
|
|
|
register char *tg = &buf[0];
|
|
|
|
register char *maxpos = &buf[idfsize];
|
|
|
|
register struct idf *idef;
|
|
|
|
int NoExpandNext = 0;
|
|
|
|
|
|
|
|
#define tstmac(bx) if (!(bits[c] & bx)) goto nomac
|
|
|
|
#define cpy *tg++ = c
|
|
|
|
#define load c = GetChar(); if (!in_idf(c)) goto endidf
|
|
|
|
|
|
|
|
/* unstack macro's when allowed. */
|
|
|
|
if (Unstacked)
|
|
|
|
EnableMacros();
|
|
|
|
if (c == NOEXPM) {
|
|
|
|
NoExpandNext = 1;
|
|
|
|
c = GetChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DOBITS
|
|
|
|
cpy; tstmac(bit0); load;
|
|
|
|
cpy; tstmac(bit1); load;
|
|
|
|
cpy; tstmac(bit2); load;
|
|
|
|
cpy; tstmac(bit3); load;
|
|
|
|
cpy; tstmac(bit4); load;
|
|
|
|
cpy; tstmac(bit5); load;
|
|
|
|
cpy; tstmac(bit6); load;
|
|
|
|
cpy; tstmac(bit7); load;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if (tg < maxpos) {
|
|
|
|
cpy;
|
|
|
|
}
|
|
|
|
load;
|
|
|
|
}
|
|
|
|
endidf:
|
|
|
|
if (c != EOF) UnGetChar();
|
|
|
|
*tg = '\0'; /* mark the end of the identifier */
|
|
|
|
if ((idef = findidf(buf))
|
|
|
|
&& idef->id_macro
|
|
|
|
&& ReplaceMacros && !NoExpandNext) {
|
|
|
|
if (replace(idef)) {
|
1990-09-10 11:57:16 +00:00
|
|
|
echo(' ');
|
1989-10-23 10:35:56 +00:00
|
|
|
c = GetChar();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tg = buf;
|
|
|
|
while (*tg) {
|
|
|
|
echo(*tg++);
|
|
|
|
}
|
|
|
|
c = GetChar();
|
1990-03-13 14:33:43 +00:00
|
|
|
if (in_idf(c)) { echo(' '); }
|
1989-10-23 10:35:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nomac:
|
|
|
|
*tg = '\0';
|
|
|
|
tg = buf;
|
|
|
|
while (*tg) {
|
|
|
|
echo(*tg++);
|
|
|
|
}
|
|
|
|
c = GetChar();
|
|
|
|
while (in_idf(c)) {
|
|
|
|
echo(c);
|
|
|
|
c = GetChar();
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case STMSPEC:
|
|
|
|
if (InputLevel) {
|
|
|
|
echo(' '); /* seperate tokens */
|
|
|
|
c = GetChar();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* else fallthrough */
|
|
|
|
default:
|
|
|
|
echo(c);
|
|
|
|
c = GetChar();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1989-11-22 12:59:15 +00:00
|
|
|
|
|
|
|
static char *
|
|
|
|
SkipComment(op, lineno)
|
|
|
|
char *op;
|
|
|
|
int *lineno;
|
|
|
|
{
|
|
|
|
char *ob = &_obuf[OBUFSIZE];
|
1990-04-10 10:52:52 +00:00
|
|
|
register int c, oldc = '\0';
|
1989-11-22 12:59:15 +00:00
|
|
|
|
|
|
|
NoUnstack++;
|
|
|
|
if (options['C']) {
|
|
|
|
echo('/');
|
|
|
|
echo('*');
|
|
|
|
}
|
|
|
|
c = GetChar();
|
|
|
|
for(;;) {
|
|
|
|
if (c == EOI) {
|
|
|
|
newline();
|
1990-04-25 15:10:13 +00:00
|
|
|
flush((int)(op - _obuf));
|
1989-11-22 12:59:15 +00:00
|
|
|
op = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (options['C']) {
|
|
|
|
echo(c);
|
|
|
|
}
|
|
|
|
if (c == '\n') {
|
|
|
|
++LineNumber;
|
|
|
|
++*lineno;
|
|
|
|
if (!options['C']) {
|
|
|
|
echo(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c == '*') {
|
|
|
|
c = GetChar();
|
|
|
|
if (c == '/') {
|
|
|
|
if (options['C']) {
|
|
|
|
echo(c);
|
|
|
|
}
|
|
|
|
break; /* for(;;) */
|
1990-04-10 10:52:52 +00:00
|
|
|
} else if (oldc == '/') {
|
|
|
|
warning("comment inside comment ?");
|
1989-11-22 12:59:15 +00:00
|
|
|
}
|
1990-04-10 10:52:52 +00:00
|
|
|
oldc = '*';
|
|
|
|
} else {
|
|
|
|
oldc = c;
|
|
|
|
c = GetChar();
|
|
|
|
}
|
1989-11-22 12:59:15 +00:00
|
|
|
}
|
|
|
|
NoUnstack--;
|
|
|
|
return op;
|
|
|
|
}
|