ack/util/make/reader.c

122 lines
2.2 KiB
C
Raw Normal View History

1988-07-22 19:01:13 +00:00
/*
* Read in makefile
1988-07-22 19:01:13 +00:00
*
* $Header$
1988-07-22 19:01:13 +00:00
*/
#include <stdlib.h>
#include <string.h>
1988-07-22 19:01:13 +00:00
#include <stdio.h>
#include <ctype.h>
1988-07-22 19:01:13 +00:00
#include "h.h"
int lineno;
1988-07-22 19:01:13 +00:00
/*
* Syntax error handler. Print message, with line number, and exits.
1988-07-22 19:01:13 +00:00
*/
/*VARARGS1*/
void error(char *msg, char* a1)
1988-07-22 19:01:13 +00:00
{
fprintf(stderr, "%s: ", myname);
if (a1 != NULL)
{
fprintf(stderr, msg, a1);
} else
{
fprintf(stderr, "%s",msg);
}
if (lineno)
fprintf(stderr, " near line %d", lineno);
fputc('\n', stderr);
exit(1);
1988-07-22 19:01:13 +00:00
}
/*
* Read a line into the supplied string of length LZ. Remove
* comments, ignore blank lines. Deal with quoted (\) #, and
* quoted newlines. If EOF return TRUE.
1988-07-22 19:01:13 +00:00
*/
bool mgetline(char* str, FILE* fd)
1988-07-22 19:01:13 +00:00
{
register char * p;
char * q;
int pos = 0;
for (;;)
{
if (fgets(str+pos, LZ-pos, fd) == (char *)0)
return TRUE; /* EOF */
lineno++;
if ((p = strchr(str+pos, '\n')) == (char *)0)
error("Line too long", NULL);
if (p[-1] == '\\')
{
p[-1] = '\n';
pos = p - str;
continue;
}
p = str;
while (((q = strchr(p, '#')) != (char *)0) &&
(p != q) && (q[-1] == '\\'))
{
char *a;
a = q - 1; /* Del \ chr; move rest back */
p = q;
while ((*a++ = *q++))
;
}
if (q != (char *)0)
{
q[0] = '\n';
q[1] = '\0';
}
p = str;
while (isspace(*p)) /* Checking for blank */
p++;
if (*p != '\0')
return FALSE;
pos = 0;
}
1988-07-22 19:01:13 +00:00
}
/*
* Get a word from the current line, surounded by white space.
* return a pointer to it. String returned has no white spaces
* in it.
1988-07-22 19:01:13 +00:00
*/
char *gettok(char **ptr)
1988-07-22 19:01:13 +00:00
{
register char * p;
1988-07-22 19:01:13 +00:00
while (isspace(**ptr)) /* Skip spaces */
(*ptr)++;
1988-07-22 19:01:13 +00:00
if (**ptr == '\0') /* Nothing after spaces */
return NULL;
1988-07-22 19:01:13 +00:00
p = *ptr; /* word starts here */
1988-07-22 19:01:13 +00:00
while ((**ptr != '\0') && (!isspace(**ptr)))
(*ptr)++; /* Find end of word */
1988-07-22 19:01:13 +00:00
*(*ptr)++ = '\0'; /* Terminate it */
1988-07-22 19:01:13 +00:00
return(p);
1988-07-22 19:01:13 +00:00
}