Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix segfault when involing "lex" without specify the output "-o" and … #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions ldefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ extern lchar yycgidtbl[];
extern int yycgid(wchar_t);
extern Boolean handleeuc; /* TRUE iff -w or -e option is specified. */
extern Boolean widecio; /* TRUE iff -w option is specified. */
extern Boolean caseless; /* TRUE iff -i option is specified. */

#define DEFSECTION 1
#define RULESECTION 2
Expand Down
4 changes: 4 additions & 0 deletions lex.1
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ Generates output that can handle multibyte characters,
with \fIyytext[]\fR being of type \fIunsigned char[]\fR.
This option is an extension.
.TP
.B \-i
Generates a case insensitive scanner (%option case-insensitive)\fR.
This option is an extension.
.TP
.B \-n
Opposite of
.BR \-v ;
Expand Down
19 changes: 15 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,17 @@ main(int argc, char **argv)
int c;
Boolean eoption = 0, woption = 0;
const char *driver = 0;
const char *fnout = "yy.lex.c";

sargv = argv;
sargc = argc;
errorf = stderr;
setlocale(LC_CTYPE, "");
while ((c = getopt(argc, argv,
#ifdef DEBUG
while ((c = getopt(argc, argv, "dyctvnewVQ:o:")) != EOF) {
#else
while ((c = getopt(argc, argv, "ctvnewVQ:o:")) != EOF) {
"dy"
#endif
"ctvneiwVQ:o:")) != EOF) {
switch (c) {
#ifdef DEBUG
case 'd':
Expand Down Expand Up @@ -149,9 +150,12 @@ main(int argc, char **argv)
handleeuc = 1;
widecio = 0;
break;
case 'i':
caseless = TRUE;
break;
default:
fprintf(stderr,
"Usage: lex [-ewctvnV] [-o outfile] [-Q(y/n)] [file]\n");
"Usage: lex [-eiwctvnV] [-o outfile] [-Q(y/n)] [file]\n");
exit(1);
}
}
Expand All @@ -172,6 +176,13 @@ main(int argc, char **argv)
}
} else
fin = stdin;
if(!fout) {
fout = fopen(fnout, "w");
if (!fout)
error(
"lex: could not open %s for writing",
fnout);
}

/* may be gotten: def, subs, sname, schar, ccl, dchar */
gch();
Expand Down
16 changes: 12 additions & 4 deletions ncform
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ yylook()
register struct yywork *yyt;
struct yysvf *yyz;
int yych, yyfirst;
#ifdef LEXCASELESS
int yychnc;
#else
#define yychnc yych
#endif
struct yywork *yyr;
# ifdef LEXDEBUG
int debug;
Expand Down Expand Up @@ -120,8 +125,11 @@ yylook()
}
# endif
yyr = yyt;
#ifdef LEXCASELESS
yychnc = tolower(yych);
#endif
if ( yyt > yycrank){
yyt = yyr + yych;
yyt = yyr + yychnc;
if (yyt <= yytop && yyt->verify+yysvec == yystate){
if(yyt->advance+yysvec == YYLERR) /* error transitions */
{unput(*--yylastch);break;}
Expand All @@ -139,7 +147,7 @@ yylook()
# ifdef LEXDEBUG
if(debug)fprintf(yyout,"compressed state\n");
# endif
yyt = yyt + yych;
yyt = yyt + yychnc;
if(yyt <= yytop && yyt->verify+yysvec == yystate){
if(yyt->advance+yysvec == YYLERR) /* error transitions */
{unput(*--yylastch);break;}
Expand All @@ -150,11 +158,11 @@ yylook()
}
goto contin;
}
yyt = yyr + YYU(yymatch[yych]);
yyt = yyr + YYU(yymatch[yychnc]);
# ifdef LEXDEBUG
if(debug){
fprintf(yyout,"try fall back character ");
allprint(YYU(yymatch[yych]));
allprint(YYU(yymatch[yychnc]));
putchar('\n');
}
# endif
Expand Down
1 change: 1 addition & 0 deletions once.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ CHR *psave;

Boolean handleeuc = FALSE;
Boolean widecio = FALSE;
Boolean caseless = FALSE;

int isArray = 1; /* XCU4: for %array %pointer */

Expand Down
26 changes: 17 additions & 9 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ defns: defns STR STR
error("Too many definitions");
dp += slength($3.cp) + 1;
if(dp >= dchar+DEFCHAR)
error("Definitions too long");
error("Definitions too long (%d)", DEFCHAR);
subs[dptr]=def[dptr]=0; /* for lookup - require ending null */
}
|
Expand Down Expand Up @@ -316,13 +316,19 @@ static int wcwordtos(CHR *ws, char *buf, size_t buflen) {
static void flex_noyywrap(void) {
fprintf(fout, "# define yywrap() 1\n");
}
static void flex_caseless(void) {
fprintf(fout, "# define LEXCASELESS 1\n");
fprintf(fout, "#include <ctype.h>\n");
caseless = TRUE;
}
static void parse_flex_opts(CHR *p) {
static const struct {
const char name[9];
const char *name;
void(*action)(void);
} flex_opts[] = {
{ "noyywrap", flex_noyywrap},
{ {0}, 0 }
{ "case-insensitive", flex_caseless},
{ 0, 0 }
};
while(1) {
char buf[60+MB_LEN_MAX];
Expand Down Expand Up @@ -795,6 +801,7 @@ start:
if(prev != '\n') /* not at line begin, not start */
goto character;
t = slptr;
x = 0;
do {
i = 0;
if(!isascii(c = gch()))
Expand Down Expand Up @@ -857,7 +864,7 @@ start:
while((c=gch()) && c != '"' && c != '\n'){
if(c == '\\') c = usescape(c=gch());
remch(c);
token[i++] = c;
token[i++] = caseless ? tolower(c) : c;
if(i >= TOKENSIZE){
warning("String too long");
i = TOKENSIZE-1;
Expand Down Expand Up @@ -923,8 +930,8 @@ Character range specified between different codesets.");
('0'<=j && k<='9')))
warning("Non-portable Character Class");
token[i++] = RANGE;
token[i++] = j;
token[i++] = k;
token[i++] = caseless ? tolower(j) : j;
token[i++] = caseless ? tolower(k) : k;
light = FALSE;
} else {
error("unmatched hyphen");
Expand All @@ -935,7 +942,7 @@ Character range specified between different codesets.");
} else {
j = c;
remch(c);
token[i++] = c; /* Remember whatever.*/
token[i++] = caseless ? tolower(c) : c; /* Remember whatever.*/
light = TRUE;
ESCAPE = FALSE;
}
Expand Down Expand Up @@ -969,9 +976,10 @@ Character range specified between different codesets.");
if(alpha(peek)){
i = 0;
yylval.cp = (CHR *)token;
token[i++] = c;
token[i++] = caseless ? tolower(c) : c;
while(alpha(peek)) {
remch(token[i++] = gch());
c = gch();
remch(token[i++] = caseless ? tolower(c) : c);
if(i >= TOKENSIZE) {
warning("string too long");
i = TOKENSIZE - 1;
Expand Down
11 changes: 4 additions & 7 deletions sub1.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,14 +674,11 @@ gch(void)
int
mn2(int a, intptr_t d, intptr_t c)
{
if (tptr >= treesize) {
if (tptr >= treesize || d >= treesize || c >= treesize) {
tptr++;
error("Parse tree too big %s",
error("Parse tree too big (%d) %s", treesize,
(treesize == TREESIZE ? "\nTry using %e num" : ""));
}
if (d >= treesize) {
error("Parse error");
}
name[tptr] = a;
left[tptr] = d;
right[tptr] = c;
Expand Down Expand Up @@ -723,7 +720,7 @@ mn1(int a, intptr_t d)
{
if (tptr >= treesize) {
tptr++;
error("Parse tree too big %s",
error("Parse tree too big (%d) %s", treesize,
(treesize == TREESIZE ? "\nTry using %e num" : ""));
}
name[tptr] = a;
Expand Down Expand Up @@ -766,7 +763,7 @@ mn0(int a)
{
if (tptr >= treesize) {
tptr++;
error("Parse tree too big %s",
error("Parse tree too big (%d) %s", treesize,
(treesize == TREESIZE ? "\nTry using %e num" : ""));
}

Expand Down
27 changes: 16 additions & 11 deletions sub2.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ cfoll(int v)
*pcptr++ = 0;
if (pcptr > pchar + pchlen)
error(
"Too many packed character classes");
"Too many packed character classes (%d)",
pchlen);
left[v] = (intptr_t)p;
name[v] = RCCL; /* RNCCL eliminated */
#ifdef DEBUG
Expand Down Expand Up @@ -167,7 +168,7 @@ add(int **array, int n)
nxtpos = temp;
if (nxtpos >= positions+maxpos)
error(
"Too many positions %s",
"Too many positions (%d) %s", maxpos,
(maxpos == MAXPOS ? "\nTry using %p num" : ""));
}

Expand Down Expand Up @@ -426,8 +427,8 @@ cgoto(void)
else if (xstate == -1) {
if (stnum+1 >= nstates) {
stnum++;
error("Too many states %s",
(nstates == NSTATES ?
error("Too many states (%d) %s",
nstates, (nstates == NSTATES ?
"\nTry using %n num":""));
}
add(state, ++stnum);
Expand Down Expand Up @@ -698,6 +699,8 @@ packtrans(int st, CHR *tch, int *tst, int cnt, int tryit)
#endif
nopack:
/* stick it in */
if((nptr+cnt) > ntrans)
goto errntrans;
gotof[st] = nptr;
nexts[nptr] = cnt;
for (i = 0; i < cnt; i++) {
Expand All @@ -712,10 +715,12 @@ packtrans(int st, CHR *tch, int *tst, int cnt, int tryit)
gotof[st] = -1;
nptr--;
} else
if (nptr > ntrans)
if (nptr > ntrans) {
errntrans:
error(
"Too many transitions %s",
"Too many transitions (%d) %s", ntrans,
(ntrans == NTRANS ? "\nTry using %a num" : ""));
}
}

#ifdef DEBUG
Expand Down Expand Up @@ -968,7 +973,7 @@ layout(void)
do {
startup += 1;
if (startup > outsize - ZCH)
error("output table overflow");
error("output table overflow (%d)", outsize);
for (j = bot; j <= top; j++) {
k = startup+ctable[nchar[j]];
if (verify[k])
Expand All @@ -995,7 +1000,7 @@ layout(void)
do {
startup += 1;
if (startup > outsize - ZCH)
error("output table overflow");
error("output table overflow (%d)", outsize);
for (j = bot; j <= top; j++) {
k = startup + nchar[j];
if (verify[k])
Expand Down Expand Up @@ -1070,12 +1075,12 @@ layout(void)
else
fprintf(fout, "0");
#ifdef DEBUG
fprintf(fout, " },\t\t/* state %d */", i);
fprintf(fout, " },\n\t/* state %d */", i);
#endif
fprintf(fout, " },\t\t/* state %d */", i);
fprintf(fout, " },\n\t/* state %d */", i);
}
fprintf(fout,
"{ 0,\t0,\t0}};\n"
"{ 0,\t0,\t0}\n};\n"

/* put out yymatch */

Expand Down