#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)input.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: input.c,v 1.21 2023/08/26 15:18:27 rillig Exp $");
#endif
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"
int cur_wordc;
char **cur_wordv;
static Errorclass catchall(void);
static Errorclass cpp(void);
static Errorclass f77(void);
static Errorclass lint0(void);
static Errorclass lint1(void);
static Errorclass lint2(void);
static Errorclass lint3(void);
static Errorclass make(void);
static Errorclass mod2(void);
static Errorclass onelong(void);
static Errorclass pccccom(void);
static Errorclass ri(void);
static Errorclass richieccom(void);
static Errorclass gcc45ccom(void);
static Errorclass troff(void);
void
eaterrors(int *r_errorc, Eptr **r_errorv)
{
Errorclass errorclass = C_SYNC;
char *line;
size_t inbuflen;
for (;;) {
line = NULL;
inbuflen = 0;
if (getline(&line, &inbuflen, errorfile) == -1)
break;
wordvbuild(line, &cur_wordc, &cur_wordv);
cur_wordv -= 1;
if (cur_wordc > 0 &&
((( errorclass = onelong() ) != C_UNKNOWN)
|| (( errorclass = cpp() ) != C_UNKNOWN)
|| (( errorclass = gcc45ccom() ) != C_UNKNOWN)
|| (( errorclass = pccccom() ) != C_UNKNOWN)
|| (( errorclass = richieccom() ) != C_UNKNOWN)
|| (( errorclass = lint0() ) != C_UNKNOWN)
|| (( errorclass = lint1() ) != C_UNKNOWN)
|| (( errorclass = lint2() ) != C_UNKNOWN)
|| (( errorclass = lint3() ) != C_UNKNOWN)
|| (( errorclass = make() ) != C_UNKNOWN)
|| (( errorclass = f77() ) != C_UNKNOWN)
|| ((errorclass = pi() ) != C_UNKNOWN)
|| (( errorclass = ri() )!= C_UNKNOWN)
|| (( errorclass = mod2() )!= C_UNKNOWN)
|| (( errorclass = troff() )!= C_UNKNOWN))
) ;
else
errorclass = catchall();
if (cur_wordc > 0)
erroradd(cur_wordc, cur_wordv+1, errorclass, C_UNKNOWN);
}
#ifdef FULLDEBUG
printf("%d errorentrys\n", nerrors);
#endif
arrayify(r_errorc, r_errorv, er_head);
}
void
erroradd(int errorlength, char **errorv, Errorclass errorclass,
Errorclass errorsubclass)
{
Eptr newerror;
const char *cp;
if (errorclass == C_TRUE) {
for (cp = errorv[1];
*cp != '\0' && isdigit((unsigned char)*cp); cp++)
continue;
errorclass = (*cp == '\0') ? C_TRUE : C_NONSPEC;
#ifdef FULLDEBUG
if (errorclass != C_TRUE)
printf("The 2nd word, \"%s\" is not a number.\n",
errorv[1]);
#endif
}
if (errorlength > 0) {
newerror = Calloc(1, sizeof(Edesc));
newerror->error_language = language;
newerror->error_text = errorv;
newerror->error_lgtext = errorlength;
if (errorclass == C_TRUE)
newerror->error_line = atoi(errorv[1]);
newerror->error_e_class = errorclass;
newerror->error_s_class = errorsubclass;
switch (newerror->error_e_class = discardit(newerror)) {
case C_SYNC: nsyncerrors++; break;
case C_DISCARD: ndiscard++; break;
case C_NULLED: nnulled++; break;
case C_NONSPEC: nnonspec++; break;
case C_THISFILE: nthisfile++; break;
case C_TRUE: ntrue++; break;
case C_UNKNOWN: nunknown++; break;
case C_IGNORE: nignore++; break;
}
newerror->error_next = er_head;
er_head = newerror;
newerror->error_no = nerrors++;
}
}
static Errorclass
onelong(void)
{
char **nwordv;
if (cur_wordc == 1 && language != INLD) {
cur_wordc = 0;
if (strcmp(cur_wordv[1], "Stop.") == 0) {
language = INMAKE;
return C_SYNC;
}
if (strcmp(cur_wordv[1], "Assembler:") == 0) {
language = INAS;
return C_SYNC;
} else
if (strcmp(cur_wordv[1], "Undefined:") == 0) {
language = INLD;
return C_SYNC;
}
if (lastchar(cur_wordv[1]) == ':') {
currentfilename = cur_wordv[1];
(void)substitute(currentfilename, ':', '\0');
language = INCC;
return C_SYNC;
}
} else
if (cur_wordc == 1 && language == INLD) {
nwordv = Calloc(4, sizeof(char *));
nwordv[0] = Strdup("ld:");
nwordv[1] = cur_wordv[1];
nwordv[2] = Strdup("is");
nwordv[3] = Strdup("undefined.");
cur_wordc = 4;
cur_wordv = nwordv - 1;
return C_NONSPEC;
} else
if (cur_wordc == 1) {
return C_SYNC;
}
return C_UNKNOWN;
}
static Errorclass
cpp(void)
{
if (cur_wordc < 3)
return C_UNKNOWN;
if (language != INLD
&& lastchar(cur_wordv[1]) == ':'
&& isdigit((unsigned char)firstchar(cur_wordv[2]))
&& lastchar(cur_wordv[2]) == ':') {
language = INCPP;
clob_last(cur_wordv[1], '\0');
clob_last(cur_wordv[2], '\0');
return C_TRUE;
}
return C_UNKNOWN;
}
static Errorclass
pccccom(void)
{
if (cur_wordc < 4)
return C_UNKNOWN;
if (firstchar(cur_wordv[1]) == '"'
&& lastchar(cur_wordv[1]) == ','
&& next_lastchar(cur_wordv[1]) == '"'
&& strcmp(cur_wordv[2], "line") == 0
&& isdigit((unsigned char)firstchar(cur_wordv[3]))
&& lastchar(cur_wordv[3]) == ':') {
clob_last(cur_wordv[1], '\0');
clob_last(cur_wordv[1], '\0');
cur_wordv[1]++;
clob_last(cur_wordv[3], '\0');
cur_wordv[2] = cur_wordv[1];
cur_wordv++;
cur_wordc--;
currentfilename = cur_wordv[1];
language = INCC;
return C_TRUE;
}
return C_UNKNOWN;
}
static Errorclass
gcc45ccom(void)
{
char *cp, *ccp;
char **nwordv;
char *file;
if (cur_wordc < 2)
return C_UNKNOWN;
if (lastchar(cur_wordv[1]) != ':')
return C_UNKNOWN;
cp = cur_wordv[1] + strlen(cur_wordv[1]) - 1;
while (isdigit((unsigned char)*--cp))
continue;
if (*cp != ':')
return C_UNKNOWN;
ccp = cp;
while (isdigit((unsigned char)*--cp))
continue;
if (*cp != ':')
return C_UNKNOWN;
clob_last(cur_wordv[1], '\0');
*ccp = '\0';
*cp = '\0';
file = cur_wordv[1];
#ifdef notyet
#define EHEAD 2
#else
#define EHEAD 1
#endif
nwordv = wordvsplice(EHEAD, cur_wordc, cur_wordv + 1);
nwordv[0] = file;
nwordv[1] = cp + 1;
#ifdef notyet
nwordv[2] = ccp + 1;
#endif
cur_wordc += 1;
cur_wordv = nwordv - 1;
language = INCC;
currentfilename = cur_wordv[1];
return C_TRUE;
}
static Errorclass
richieccom(void)
{
char *cp;
char **nwordv;
char *file;
if (cur_wordc < 2)
return C_UNKNOWN;
if (lastchar(cur_wordv[1]) == ':') {
cp = cur_wordv[1] + strlen(cur_wordv[1]) - 1;
while (isdigit((unsigned char)*--cp))
continue;
if (*cp == ':') {
clob_last(cur_wordv[1], '\0');
*cp = '\0';
file = cur_wordv[1];
nwordv = wordvsplice(1, cur_wordc, cur_wordv+1);
nwordv[0] = file;
nwordv[1] = cp + 1;
cur_wordc += 1;
cur_wordv = nwordv - 1;
language = INCC;
currentfilename = cur_wordv[1];
return C_TRUE;
}
}
return C_UNKNOWN;
}
static Errorclass
lint0(void)
{
char **nwordv;
char *line, *file;
if (cur_wordc < 2)
return C_UNKNOWN;
if (lastchar(cur_wordv[1]) == ':'
&& next_lastchar(cur_wordv[1]) == ')') {
clob_last(cur_wordv[1], '\0');
if (persperdexplode(cur_wordv[1], &line, &file)) {
nwordv = wordvsplice(1, cur_wordc, cur_wordv+1);
nwordv[0] = file;
nwordv[1] = line;
cur_wordc += 1;
cur_wordv = nwordv - 1;
language = INLINT;
return C_TRUE;
}
cur_wordv[1][strlen(cur_wordv[1])] = ':';
}
return C_UNKNOWN;
}
static Errorclass
lint1(void)
{
char *line1 = NULL, *line2 = NULL;
char *file1 = NULL, *file2 = NULL;
char **nwordv1, **nwordv2;
if (cur_wordc > 1 && strcmp(cur_wordv[cur_wordc-1], "::") == 0) {
language = INLINT;
if (cur_wordc > 2
&& persperdexplode(cur_wordv[cur_wordc], &line2, &file2)
&& persperdexplode(cur_wordv[cur_wordc-2], &line1, &file1)) {
nwordv1 = wordvsplice(2, cur_wordc, cur_wordv+1);
nwordv2 = wordvsplice(2, cur_wordc, cur_wordv+1);
nwordv1[0] = file1;
nwordv1[1] = line1;
erroradd(cur_wordc+2, nwordv1, C_TRUE, C_DUPL);
nwordv2[0] = file2;
nwordv2[1] = line2;
cur_wordc = cur_wordc + 2;
cur_wordv = nwordv2 - 1;
return C_TRUE;
}
}
free(file2);
free(file1);
free(line2);
free(line1);
return C_UNKNOWN;
}
static Errorclass
lint2(void)
{
char *file;
char *line;
char **nwordv;
if (cur_wordc < 5)
return C_UNKNOWN;
if (lastchar(cur_wordv[2]) == '('
&& strcmp(cur_wordv[4], "),") == 0) {
language = INLINT;
if (persperdexplode(cur_wordv[3], &line, &file)) {
nwordv = wordvsplice(2, cur_wordc, cur_wordv+1);
nwordv[0] = file;
nwordv[1] = line;
cur_wordc = cur_wordc + 2;
cur_wordv = nwordv - 1;
return C_TRUE;
}
}
return C_UNKNOWN;
}
#if 0
static char *Lint31[4] = {"returns", "value", "which", "is"};
static char *Lint32[6] = {"value", "is", "used,", "but", "none", "returned"};
#else
DECL_STRINGS_4(static, Lint31,
"returns", "value", "which", "is");
DECL_STRINGS_6(static, Lint32,
"value", "is", "used,", "but", "none", "returned");
#endif
static Errorclass
lint3(void)
{
if (cur_wordc < 3)
return C_UNKNOWN;
if (wordv_eq(cur_wordv+2, 4, Lint31)
|| wordv_eq(cur_wordv+2, 6, Lint32)) {
language = INLINT;
return C_NONSPEC;
}
return C_UNKNOWN;
}
#if 0
static char *F77_fatal[3] = {"Compiler", "error", "line"};
static char *F77_error[3] = {"Error", "on", "line"};
static char *F77_warning[3] = {"Warning", "on", "line"};
static char *F77_no_ass[3] = {"Error.","No","assembly."};
#else
DECL_STRINGS_3(static, F77_fatal, "Compiler", "error", "line");
DECL_STRINGS_3(static, F77_error, "Error", "on", "line");
DECL_STRINGS_3(static, F77_warning, "Warning", "on", "line");
DECL_STRINGS_3(static, F77_no_ass, "Error.", "No", "assembly.");
#endif
static Errorclass
f77(void)
{
char **nwordv;
if (cur_wordc == 3 && wordv_eq(cur_wordv+1, 3, F77_no_ass)) {
cur_wordc = 0;
return C_SYNC;
}
if (cur_wordc < 6)
return C_UNKNOWN;
if (lastchar(cur_wordv[6]) == ':'
&& (
wordv_eq(cur_wordv+1, 3, F77_fatal)
|| wordv_eq(cur_wordv+1, 3, F77_error)
|| wordv_eq(cur_wordv+1, 3, F77_warning)
)
) {
language = INF77;
nwordv = wordvsplice(2, cur_wordc, cur_wordv+1);
nwordv[0] = cur_wordv[6];
clob_last(nwordv[0],'\0');
nwordv[1] = cur_wordv[4];
cur_wordc += 2;
cur_wordv = nwordv - 1;
return C_TRUE;
}
return C_UNKNOWN;
}
#if 0
static char *Make_Croak[3] = {"***", "Error", "code"};
static char *Make_NotRemade[5] = {"not", "remade", "because", "of", "errors"};
#else
DECL_STRINGS_3(static, Make_Croak, "***", "Error", "code");
DECL_STRINGS_5(static, Make_NotRemade,
"not", "remade", "because", "of", "errors");
#endif
static Errorclass
make(void)
{
if (wordv_eq(cur_wordv+1, 3, Make_Croak)) {
language = INMAKE;
return C_SYNC;
}
if (wordv_eq(cur_wordv+2, 5, Make_NotRemade)) {
language = INMAKE;
return C_SYNC;
}
return C_UNKNOWN;
}
static Errorclass
ri(void)
{
if (cur_wordc < 3)
return C_UNKNOWN;
if (firstchar(cur_wordv[1]) == '"'
&& lastchar(cur_wordv[1]) == '"'
&& lastchar(cur_wordv[2]) == ':'
&& isdigit((unsigned char)firstchar(cur_wordv[2]))) {
clob_last(cur_wordv[1], '\0');
cur_wordv[1]++;
clob_last(cur_wordv[2], '\0');
language = INRI;
return C_TRUE;
}
return C_UNKNOWN;
}
static Errorclass
catchall(void)
{
language = INUNKNOWN;
return C_NONSPEC;
}
static Errorclass
troff(void)
{
if (cur_wordc < 4)
return C_UNKNOWN;
if (firstchar(cur_wordv[1]) == '`'
&& lastchar(cur_wordv[1]) == ','
&& next_lastchar(cur_wordv[1]) == '\''
&& strcmp(cur_wordv[2], "line") == 0
&& isdigit((unsigned char)firstchar(cur_wordv[3]))
&& lastchar(cur_wordv[3]) == ':') {
clob_last(cur_wordv[1], '\0');
clob_last(cur_wordv[1], '\0');
cur_wordv[1]++;
clob_last(cur_wordv[3], '\0');
cur_wordv[2] = cur_wordv[1];
cur_wordv++;
currentfilename = cur_wordv[1];
language = INTROFF;
return C_TRUE;
}
return C_UNKNOWN;
}
static Errorclass
mod2(void)
{
if (cur_wordc < 5)
return C_UNKNOWN;
if ((strcmp(cur_wordv[1], "!!!") == 0
|| strcmp(cur_wordv[1], "File") == 0)
&& lastchar(cur_wordv[2]) == ','
&& strcmp(cur_wordv[3], "line") == 0
&& isdigit((unsigned char)firstchar(cur_wordv[4]))
&& lastchar(cur_wordv[4]) == ':'
) {
clob_last(cur_wordv[2], '\0');
clob_last(cur_wordv[4], '\0');
cur_wordv[3] = cur_wordv[2];
cur_wordv += 2;
cur_wordc -= 2;
currentfilename = cur_wordv[1];
language = INMOD2;
return C_TRUE;
}
return C_UNKNOWN;
}