handle unterminated strings a bit better, and negative numbers

git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@212 db0b04b0-f4d1-0310-9a6d-de3e77497b0e
This commit is contained in:
Joshua Kwan 2004-02-02 01:03:57 +00:00
parent d5c7d15202
commit 4fc6f54bec
2 changed files with 15 additions and 24 deletions

View File

@ -15,7 +15,8 @@ unsigned int line = 1, col = 0;
%} %}
NUMBER -?[0-9]+ NEGNUMBER -[0-9]+
NUMBER [^-][0-9]+
VALUE \".*\" VALUE \".*\"
MALSTRING \"[^\"\n]*\n MALSTRING \"[^\"\n]*\n
WHITE [\t ]* WHITE [\t ]*
@ -23,27 +24,14 @@ COMMENT ^#.*
%% %%
{NUMBER} { {NEGNUMBER} {
unsigned int x; fprintf(stderr,"%s:%d: negative value not accepted! Fix it now!\n",
errno = 0;
if (atoi(yytext) < 0)
{
fprintf(stderr,"%s:%d: negative value not accepted! Fix it now!\n",
config, line); config, line);
graceful_exit(1); graceful_exit(1);
} }
x = strtoul(yytext, NULL, 10); {NUMBER} {
yylval.i = strtoul(yytext, NULL, 10);
if (errno == ERANGE)
{
fprintf(stderr, "%s:%d: %s is too big! Fix it now!\n",
config, line, yytext);
graceful_exit(1);
}
yylval.i = x;
return TYPE_NUMBER; return TYPE_NUMBER;
} }
@ -54,8 +42,10 @@ COMMENT ^#.*
} }
{MALSTRING} { {MALSTRING} {
yytext[yyleng - 1] = '\0'; /* remove trailing newline */
/* yytext already contains a newline, no need for one here */ /* yytext already contains a newline, no need for one here */
fprintf(stderr, "%s:%d:%d: unterminated string constant: %s\n", config, line, col - yyleng + 1, yytext); fprintf(stderr, "%s:%d:%d: unterminated string constant: %s\n", config, line, col - yyleng + 1, yytext);
return TYPE_MALSTRING;
} }
{WHITE} { } {WHITE} { }
@ -80,7 +70,7 @@ COMMENT ^#.*
\n { line++; col = 0; } \n { line++; col = 0; }
. { . {
fprintf(stderr, "%s: unrecognized token \"%s\" at line %d, column %d\n", config, yytext, line, col); fprintf(stderr, "%s:%d:%d unrecognized token \"%s\"\n", config, line, col, yytext);
} }
%% %%

View File

@ -26,7 +26,7 @@ static const char* lookup_token (int t);
%token TYPE_SUSER TYPE_SGROUP TYPE_SGID TYPE_SUID TYPE_MAX %token TYPE_SUSER TYPE_SGROUP TYPE_SGID TYPE_SUID TYPE_MAX
%token TYPE_PATH_NETHACK TYPE_PATH_DGLDIR TYPE_PATH_SPOOL %token TYPE_PATH_NETHACK TYPE_PATH_DGLDIR TYPE_PATH_SPOOL
%token TYPE_PATH_BANNER TYPE_PATH_CANNED TYPE_PATH_CHROOT %token TYPE_PATH_BANNER TYPE_PATH_CANNED TYPE_PATH_CHROOT
%token TYPE_PATH_PASSWD TYPE_PATH_LOCKFILE %token TYPE_PATH_PASSWD TYPE_PATH_LOCKFILE TYPE_MALSTRING
%token <s> TYPE_VALUE %token <s> TYPE_VALUE
%token <i> TYPE_NUMBER %token <i> TYPE_NUMBER
%type <kt> KeyType %type <kt> KeyType
@ -154,6 +154,7 @@ KeyPair: KeyType '=' TYPE_VALUE {
free($3); free($3);
} }
| KeyType '=' TYPE_MALSTRING {}
| KeyType '=' TYPE_NUMBER { | KeyType '=' TYPE_NUMBER {
if (!myconfig) if (!myconfig)
{ {
@ -238,5 +239,5 @@ const char* lookup_token (int t)
void yyerror(char const* s) void yyerror(char const* s)
{ {
if (!silent) if (!silent)
fprintf(stderr, "%s: couldn't parse \"%s\" at line %d, column %d: %s\n", config, yytext, line, col, s); fprintf(stderr, "%s:%d:%d: couldn't parse \"%s\": %s\n", config, line, col, yytext, s);
} }