ack/lang/cem/cemcom/input.c

465 lines
10 KiB
C
Raw Normal View History

1986-03-10 13:07:55 +00:00
/* $Header$ */
/* INPUT AND BUFFER HANDLING MODULE */
/*
[input.c input.h]
Input buffering module: this module contains the routines that
offers an input buffering mechanism to the user.
This module exports the following objects:
InsertFile() : suspend input from current buffer and obtain the
next input characters from the specified file
InsertText() : suspend input from current buffer and take the
specified text as stream of input characters
LoadChar() : (defined in input.h) read next character from
the input ; LoadChar() invokes loadbuf() on
encounting a ASCII NUL character
NoUnstack : if set to non-zero:
loadbuf() reports "unexpected EOF" on encounting
the end-of-file or end-of-stacked-text.
Imported objects are:
IDEPTH, DEBUG, READ_IN_ONE, PATHLENGTH: compile-time parameters
Malloc(), Salloc(): memory allocation routines
fatal(), lexerror(): exception handling
FileName, LineNumber, WorkingDir: input trace for lexical analyser
READ_IN_ONE DEFINED: every input file is read into memory completely
and made an input buffer
READ_IN_ONE NOT DEFINED: the input from files is buffered in
a fixed length input buffer
*/
1986-03-25 10:39:23 +00:00
#include <system.h>
1986-03-10 13:07:55 +00:00
#include "nopp.h"
#include "inputtype.h"
1986-03-10 13:07:55 +00:00
#include "interface.h"
#include "arith.h"
#include "LLlex.h"
#include "input.h"
#include "alloc.h"
#ifndef NOPP
#include "idepth.h"
#include "debug.h"
#include "pathlength.h"
1986-03-10 13:07:55 +00:00
#include "assert.h"
#include "static.h"
1986-03-10 13:07:55 +00:00
#endif NOPP
EXPORT char *ipp = 0; /* input pointer */
EXPORT int NoUnstack = 0; /* if 1: report EOF */
#ifndef READ_IN_ONE
1986-03-25 10:39:23 +00:00
PRIVATE File *FilDes = 0; /* current input medium */
1986-03-10 13:07:55 +00:00
#endif READ_IN_ONE
#ifndef NOPP
struct buffer_header {
char *bh_name; /* file name where the text comes from */
unsigned int bh_lineno;
/* current lineno in file */
long bh_size; /* = strlen (text), should be unsigned */
char *bh_text; /* pointer to buffer containing text */
char *bh_ipp; /* current read pointer (= stacked ipp) */
char *bh_wdir; /* directory of current file */
1986-03-25 10:39:23 +00:00
File *bh_fp; /* needed for files if !READ_IN_ONE */
1986-03-10 13:07:55 +00:00
};
PRIVATE struct buffer_header instack[IDEPTH]; /* stack of input media */
PRIVATE struct buffer_header *head = 0; /* current input buffer */
IMPORT char **WorkingDir; /* name of current working directory */
#else NOPP
long isize;
1986-09-12 12:58:22 +00:00
char ibuf[BUFSIZ + 1];
1986-03-10 13:07:55 +00:00
#endif NOPP
#ifdef READ_IN_ONE
/* readfile() creates a buffer in which the text of the file
is situated. A pointer to the start of this text is
returned. *size is initialized with the buffer length.
Note that the file input buffer is prepared for the
preprocessor by inserting a '\n' in the beginning of the
text and appending a '\n' at the end of the text. The
file text start at position 1 of the input buffer. This is
done to allow pushback.
*/
PRIVATE char *
readfile(filename, size)
char *filename;
long *size;
{
1986-03-25 10:39:23 +00:00
File *fp; /* filedescriptor for `filename' */
1986-03-10 13:07:55 +00:00
char *cbuf; /* pointer to buffer to be returned */
1986-03-25 10:39:23 +00:00
int tmp;
long sys_filesize();
1986-03-10 13:07:55 +00:00
1986-03-25 10:39:23 +00:00
if (sys_open(filename, OP_READ, &fp) == 0) /* can't open this file */
1986-03-10 13:07:55 +00:00
return (char *) 0;
1986-03-25 10:39:23 +00:00
if ((*size = sys_filesize(filename)) == -1L)
1986-03-10 13:07:55 +00:00
fatal("(readfile) cannot get size of file");
/* allocate enough space to store contents of the file */
cbuf = Malloc(*size + 2);
1986-03-25 10:39:23 +00:00
if (sys_read(fp, cbuf + 1, (int) *size, &tmp) == 0 || tmp != *size)
fatal("(readfile) bad read");
1986-03-10 13:07:55 +00:00
(*size)++; /* keep book of the size! */
1986-03-25 10:39:23 +00:00
sys_close(fp); /* filedes no longer needed */
1986-03-10 13:07:55 +00:00
cbuf[0] = '\0'; /* allow pushback of first char */
cbuf[*size] = '\0'; /* invoke loadbuf() at end */
return cbuf;
}
#endif READ_IN_ONE
#ifndef NOPP
#ifndef READ_IN_ONE
/* Input buffer supplying routines: pushbuf() and popbuf()
*/
PRIVATE char *bufstack[IDEPTH] = 0;
PRIVATE bufstptr = 0;
PRIVATE char *
pushbuf()
{
if (bufstptr >= IDEPTH)
fatal("ran out of input buffers");
if (bufstack[bufstptr] == 0) {
bufstack[bufstptr] = Malloc(BUFSIZ + 4);
}
return bufstack[bufstptr++];
}
PRIVATE
popbuf()
{
bufstptr--;
ASSERT(bufstptr >= 0);
}
#endif READ_IN_ONE
#endif NOPP
#ifndef NOPP
/* Input buffer administration: push_bh() and pop_bh()
*/
PRIVATE struct buffer_header *
push_bh()
{
if (head) {
if (head >= &instack[IDEPTH - 1])
fatal("too many nested input texts");
head->bh_ipp = ipp;
head->bh_lineno = LineNumber;
head++;
}
else
head = &instack[0];
return head;
}
#endif NOPP
#ifndef NOPP
/* pop_bh() uncovers the previous inputbuffer on the stack
of headers. 0 is returned if there are no more
inputbuffers on the stack, 1 is returned in the other case.
*/
PRIVATE int
pop_bh()
{
1986-03-25 10:39:23 +00:00
File *pfp = head->bh_fp;
1986-03-10 13:07:55 +00:00
if (NoUnstack) {
lexerror("unexpected EOF");
}
if (head <= &instack[0]) { /* no more entries */
head = (struct buffer_header *) 0;
return 0;
}
ipp = (--head)->bh_ipp; /* restore the previous input pointer */
1986-03-25 10:39:23 +00:00
if (pfp != 0) { /* unstack a file */
1986-03-10 13:07:55 +00:00
#ifndef READ_IN_ONE
1986-03-25 10:39:23 +00:00
closefile(pfp);
1986-03-10 13:07:55 +00:00
popbuf(); /* free last buffer */
#endif READ_IN_ONE
LineNumber = head->bh_lineno;
FileName = head->bh_name;
*WorkingDir = head->bh_wdir;
}
#ifndef READ_IN_ONE
1986-03-25 10:39:23 +00:00
FilDes = head->bh_fp;
1986-03-10 13:07:55 +00:00
#endif READ_IN_ONE
return 1;
}
#endif NOPP
#ifndef READ_IN_ONE
/* low level IO routines: openfile(), readblock() and closefile()
*/
1986-03-25 10:39:23 +00:00
PRIVATE File *
1986-03-10 13:07:55 +00:00
openfile(filename)
char *filename;
{
1986-03-25 10:39:23 +00:00
File *fp;
1986-03-10 13:07:55 +00:00
1986-03-25 10:39:23 +00:00
if (filename == 0)
return STDIN;
if (sys_open(filename, OP_READ, &fp) == 0)
return (File *)0;
return fp;
1986-03-10 13:07:55 +00:00
}
PRIVATE
1986-03-25 10:39:23 +00:00
closefile(fp)
File *fp;
1986-03-10 13:07:55 +00:00
{
1986-03-25 10:39:23 +00:00
if (fp != STDIN)
sys_close(fp);
1986-03-10 13:07:55 +00:00
}
PRIVATE int
1986-03-25 10:39:23 +00:00
readblock(fp, buf)
File *fp;
1986-03-10 13:07:55 +00:00
char buf[];
{
1986-03-25 10:39:23 +00:00
int n;
1986-03-10 13:07:55 +00:00
1986-03-25 10:39:23 +00:00
if (sys_read(fp, &buf[1], BUFSIZ, &n) == 0)
fatal("(readblock) bad read");
1986-03-10 13:07:55 +00:00
buf[0] = buf[n + 1] = '\0';
return n;
}
#endif READ_IN_ONE
/* Interface routines : InsertFile(), InsertText() and loadbuf()
*/
EXPORT int
InsertFile(filnam, table)
char *filnam;
char *table[];
{
char *mk_filename(), *newfn;
char *strcpy();
1986-03-25 10:39:23 +00:00
File *openfile();
1986-03-10 13:07:55 +00:00
#ifdef READ_IN_ONE
char *readfile(), *text;
long size;
#else READ_IN_ONE
1986-03-25 10:39:23 +00:00
File *fp = 0;
1986-03-10 13:07:55 +00:00
#endif READ_IN_ONE
1986-03-25 10:39:23 +00:00
#ifdef READ_IN_ONE
1986-03-10 13:07:55 +00:00
if (!filnam)
return 0;
1986-03-25 10:39:23 +00:00
#endif READ_IN_ONE
1986-03-10 13:07:55 +00:00
#ifndef NOPP
if (table == 0 || filnam[0] == '/') { /* don't look in the table! */
#endif NOPP
#ifdef READ_IN_ONE
text = readfile(filnam, &size);
#else READ_IN_ONE
1986-03-25 10:39:23 +00:00
fp = openfile(filnam);
if (filnam == 0)
filnam = "standard input";
1986-03-10 13:07:55 +00:00
#endif READ_IN_ONE
#ifndef NOPP
}
else {
1986-03-25 10:39:23 +00:00
ASSERT(filnam != 0);
1986-03-10 13:07:55 +00:00
while (*table) { /* look in the directory table */
newfn = mk_filename(*table++, filnam);
#ifdef READ_IN_ONE
if (text = readfile(newfn, &size))
#else READ_IN_ONE
1986-03-25 10:39:23 +00:00
if ((fp = openfile(newfn)) != 0)
1986-03-10 13:07:55 +00:00
#endif READ_IN_ONE
{
/* free filnam ??? */
filnam = Salloc(newfn, strlen(newfn) + 1);
break;
}
}
}
#endif NOPP
#ifdef READ_IN_ONE
if (text)
#else READ_IN_ONE
1986-03-25 10:39:23 +00:00
if (fp != 0)
1986-03-10 13:07:55 +00:00
#endif READ_IN_ONE
#ifndef NOPP
{
struct buffer_header *push_bh();
register struct buffer_header *bh = push_bh();
setwdir(WorkingDir, filnam);
bh->bh_lineno = LineNumber = 0;
bh->bh_name = FileName = filnam;
bh->bh_wdir = *WorkingDir;
#ifdef READ_IN_ONE
bh->bh_size = size;
1986-03-25 10:39:23 +00:00
bh->bh_fp = STDIN; /* this is a file */
1986-03-10 13:07:55 +00:00
ipp = bh->bh_text = text;
#else READ_IN_ONE
1986-03-25 10:39:23 +00:00
bh->bh_size = readblock(fp, ipp = bh->bh_text = pushbuf()) + 1;
FilDes = bh->bh_fp = fp;
1986-03-10 13:07:55 +00:00
#endif READ_IN_ONE
bh->bh_text[0] = '\n'; /* wake up pp if '#' comes first */
return 1;
}
#else NOPP
{
LineNumber = 0;
FileName = filnam;
1986-03-10 13:07:55 +00:00
#ifdef READ_IN_ONE
isize = size;
ipp = text;
#else READ_IN_ONE
1986-03-25 10:39:23 +00:00
isize = readblock(FilDes = fp, ipp = &ibuf[0]) + 1;
1986-03-10 13:07:55 +00:00
#endif READ_IN_ONE
ibuf[0] = '\n';
return 1;
}
#endif NOPP
return 0;
}
#ifndef NOPP
EXPORT
InsertText(text, length)
char *text;
{
struct buffer_header *push_bh();
register struct buffer_header *bh = push_bh();
bh->bh_name = FileName;
bh->bh_lineno = LineNumber;
bh->bh_size = (long) length;
bh->bh_text = text;
bh->bh_wdir = *WorkingDir;
1986-03-25 10:39:23 +00:00
bh->bh_fp = 0; /* this is not a file ! */
1986-03-10 13:07:55 +00:00
ipp = text + 1;
#ifndef READ_IN_ONE
1986-03-25 10:39:23 +00:00
FilDes = 0;
1986-03-10 13:07:55 +00:00
#endif READ_IN_ONE
}
#endif NOPP
/* loadbuf() is called if LoadChar meets a '\0' character
which may be the end-of-buffer mark of the current input
buffer. The '\0' could be genuine although not likely.
Note: this routine is exported due to its occurence in the definition
of LoadChar [input.h], that is defined as a macro.
*/
EXPORT int
loadbuf()
{
#ifndef NOPP
if (!head) /* stack exhausted, EOF on sourcefile */
1986-03-10 13:07:55 +00:00
return EOI;
#endif NOPP
#ifndef NOPP
if (ipp < &(head->bh_text[head->bh_size]))
#else NOPP
if (ipp < &ibuf[isize])
#endif NOPP
return '\0'; /* a genuine '\0' character has been seen */
1986-03-10 13:07:55 +00:00
#ifndef READ_IN_ONE
#ifndef NOPP
1986-03-25 10:39:23 +00:00
if ( FilDes != 0
1986-03-17 17:47:04 +00:00
&& (head->bh_size = readblock(FilDes, head->bh_text)) > 0
)
1986-03-10 13:07:55 +00:00
return ipp = &(head->bh_text[1]), *ipp++;
#else NOPP
1986-03-25 10:39:23 +00:00
if (FilDes != 0 && (isize = readblock(FilDes, &ibuf[0])) > 0)
1986-03-10 13:07:55 +00:00
return ipp = &ibuf[1], *ipp++;
#endif NOPP
#endif READ_IN_ONE
#ifdef NOPP
if (NoUnstack)
lexerror("unexpected EOF");
#ifndef READ_IN_ONE
closefile(FilDes);
#endif READ_IN_ONE
#endif NOPP
#ifndef NOPP
if (pop_bh())
return *ipp ? *ipp++ : loadbuf();
1986-03-10 13:07:55 +00:00
#endif NOPP
ipp = &"\0\0"[1];
return EOI;
1986-03-10 13:07:55 +00:00
}
/* Some miscellaneous routines : setwdir() and mk_filename()
*/
#ifndef NOPP
/* setwdir() updates *wdir according to the old working
directory (*wdir) and the filename fn, which may contain
some path name. The algorithm used here is:
setwdir(DIR, FILE):
if (FILE == "/***")
*DIR = "/"
else
if (contains(FILE, '/'))
*DIR = directory(FILE)
else
*DIR remains unchanged
*/
PRIVATE
setwdir(wdir, fn)
char *fn, **wdir;
{
register char *p;
1986-03-26 16:58:43 +00:00
char *strrindex();
1986-03-10 13:07:55 +00:00
1986-03-26 16:58:43 +00:00
p = strrindex(fn, '/');
1986-03-10 13:07:55 +00:00
while (p && *(p + 1) == '\0') { /* remove trailing /'s */
*p = '\0';
1986-03-26 16:58:43 +00:00
p = strrindex(fn, '/');
1986-03-10 13:07:55 +00:00
}
if (fn[0] == '\0' || (fn[0] == '/' && p == &fn[0])) /* absolute path */
*wdir = "/";
else
if (p) {
*p = '\0';
*wdir = Salloc(fn, p - &fn[0] + 1);
*p = '/';
}
}
#endif NOPP
#ifndef NOPP
/* mk_filename() concatenates a dir and filename.
*/
1986-09-17 12:31:45 +00:00
GSTATIC char path[PATHLENGTH];
1986-03-10 13:07:55 +00:00
PRIVATE char *
mk_filename(dir, file)
register char *dir, *file;
{
register char *dst = &path[0];
1986-03-10 13:07:55 +00:00
if (!(dir[0] == '.' && dir[1] == '\0')) {
while (*dst++ = *dir++);
*(dst - 1) = '/';
}
while (*dst++ = *file++);
return &path[0];
1986-03-10 13:07:55 +00:00
}
#endif NOPP