good line number reporting for errors and treat ^Z and ^D as whitespace

This commit is contained in:
Bill Currie 2003-09-03 18:27:27 +00:00
parent 0b0f8bb613
commit 996de18cc6

View file

@ -52,6 +52,26 @@ char miptex[MAX_MAP_TEXINFO][16];
int numdetailbrushes; int numdetailbrushes;
#define MAXTOKEN 128
char token[MAXTOKEN];
qboolean unget;
char *script_p;
const char *script_file;
int script_line;
static void __attribute__ ((format (printf, 1, 2), noreturn))
map_error (const char *fmt, ...)
{
va_list args;
va_start (args, fmt);
fprintf (stderr, "%s:%d: ", script_file, script_line);
vfprintf (stderr, fmt, args);
fprintf (stderr, "\n");
va_end (args);
exit (1);
}
int int
FindMiptex (const char *name) FindMiptex (const char *name)
@ -67,7 +87,7 @@ FindMiptex (const char *name)
return i; return i;
} }
if (nummiptex == MAX_MAP_TEXINFO) if (nummiptex == MAX_MAP_TEXINFO)
Sys_Error ("nummiptex == MAX_MAP_TEXINFO"); map_error ("nummiptex == MAX_MAP_TEXINFO");
strcpy (miptex[i], name); strcpy (miptex[i], name);
nummiptex++; nummiptex++;
return i; return i;
@ -114,17 +134,10 @@ FindTexinfo (texinfo_t *t)
return i; return i;
} }
#define MAXTOKEN 128
char token[MAXTOKEN];
qboolean unget;
char *script_p;
int scriptline;
static void static void
StartTokenParsing (char *data) StartTokenParsing (char *data)
{ {
scriptline = 1; script_line = 1;
script_p = data; script_p = data;
unget = false; unget = false;
} }
@ -136,14 +149,19 @@ TokenAvailable (qboolean crossline)
return true; return true;
skipspace: skipspace:
while (isspace ((unsigned char) *script_p)) { while (isspace ((unsigned char) *script_p)) {
if (*script_p++ == '\n') { if (*script_p == '\n') {
if (!crossline) if (!crossline)
return false; return false;
scriptline++; script_line++;
} }
script_p++;
} }
if (!*script_p) if (!*script_p)
return false; return false;
if (*script_p == 26 || *script_p == 4) { // end of file characters
script_p++;
goto skipspace;
}
if (script_p[0] == '/' && script_p[1] == '/') { // comment field if (script_p[0] == '/' && script_p[1] == '/') { // comment field
while (*script_p && *script_p != '\n') while (*script_p && *script_p != '\n')
@ -152,7 +170,6 @@ TokenAvailable (qboolean crossline)
return false; return false;
if (!crossline) if (!crossline)
return false; return false;
scriptline++;
goto skipspace; goto skipspace;
} }
return true; return true;
@ -170,7 +187,7 @@ GetToken (qboolean crossline)
if (!TokenAvailable (crossline)) { if (!TokenAvailable (crossline)) {
if (!crossline) if (!crossline)
Sys_Error ("Line %i is incomplete", scriptline); map_error ("line is incomplete");
return false; return false;
} }
@ -181,17 +198,19 @@ GetToken (qboolean crossline)
script_p++; script_p++;
while (*script_p != '"') { while (*script_p != '"') {
if (!*script_p) if (!*script_p)
Sys_Error ("EOF inside quoted token"); map_error ("EOF inside quoted token");
if (*script_p == '\n')
script_line++;
*token_p++ = *script_p++; *token_p++ = *script_p++;
if (token_p > &token[MAXTOKEN - 1]) if (token_p > &token[MAXTOKEN - 1])
Sys_Error ("Token too large on line %i", scriptline); map_error ("token too large");
} }
script_p++; script_p++;
} else } else
while (*script_p && !isspace ((unsigned char) *script_p)) { while (*script_p && !isspace ((unsigned char) *script_p)) {
*token_p++ = *script_p++; *token_p++ = *script_p++;
if (token_p > &token[MAXTOKEN - 1]) if (token_p > &token[MAXTOKEN - 1])
Sys_Error ("Token too large on line %i", scriptline); map_error ("token too large");
} }
*token_p = 0; *token_p = 0;
@ -219,11 +238,11 @@ ParseEpair (void)
mapent->epairs = e; mapent->epairs = e;
if (strlen (token) >= MAX_KEY - 1) if (strlen (token) >= MAX_KEY - 1)
Sys_Error ("ParseEpar: token too long"); map_error ("ParseEpar: token too long");
e->key = strdup (token); e->key = strdup (token);
GetToken (false); GetToken (false);
if (strlen (token) >= MAX_VALUE - 1) if (strlen (token) >= MAX_VALUE - 1)
Sys_Error ("ParseEpar: token too long"); map_error ("ParseEpar: token too long");
e->value = strdup (token); e->value = strdup (token);
} }
@ -264,7 +283,7 @@ ParseVerts (int *n_verts)
int i; int i;
if (token[0] != ':') if (token[0] != ':')
Sys_Error ("parsing brush"); map_error ("parsing brush");
*n_verts = atoi (token + 1); *n_verts = atoi (token + 1);
verts = malloc (sizeof (vec3_t) * *n_verts); verts = malloc (sizeof (vec3_t) * *n_verts);
@ -327,7 +346,7 @@ ParseBrush (void)
if (i != 0) if (i != 0)
GetToken (true); GetToken (true);
if (strcmp (token, "(")) if (strcmp (token, "("))
Sys_Error ("parsing brush"); map_error ("parsing brush");
for (j = 0; j < 3; j++) { for (j = 0; j < 3; j++) {
GetToken (false); GetToken (false);
@ -336,7 +355,7 @@ ParseBrush (void)
GetToken (false); GetToken (false);
if (strcmp (token, ")")) if (strcmp (token, ")"))
Sys_Error ("parsing brush"); map_error ("parsing brush");
} }
} }
@ -360,7 +379,7 @@ ParseBrush (void)
if (!strcmp (token, "detail")) if (!strcmp (token, "detail"))
b->detail = 1; b->detail = 1;
else else
Sys_Error ("Parse error on line %i", scriptline); map_error ("parse error");
} }
// if the three points are all on a previous plane, it is a duplicate // if the three points are all on a previous plane, it is a duplicate
@ -476,17 +495,17 @@ ParseEntity (void)
return false; return false;
if (strcmp (token, "{")) if (strcmp (token, "{"))
Sys_Error ("ParseEntity: { not found"); map_error ("ParseEntity: { not found");
if (num_entities == MAX_MAP_ENTITIES) if (num_entities == MAX_MAP_ENTITIES)
Sys_Error ("num_entities == MAX_MAP_ENTITIES"); map_error ("num_entities == MAX_MAP_ENTITIES");
mapent = &entities[num_entities]; mapent = &entities[num_entities];
num_entities++; num_entities++;
do { do {
if (!GetToken (true)) if (!GetToken (true))
Sys_Error ("ParseEntity: EOF without closing brace"); map_error ("ParseEntity: EOF without closing brace");
if (!strcmp (token, "}")) if (!strcmp (token, "}"))
break; break;
if (!strcmp (token, "{")) if (!strcmp (token, "{"))
@ -533,6 +552,8 @@ LoadMapFile (const char *filename)
Qread (file, buf, Qfilesize (file)); Qread (file, buf, Qfilesize (file));
Qclose (file); Qclose (file);
script_file = filename;
StartTokenParsing (buf); StartTokenParsing (buf);
num_entities = 0; num_entities = 0;