aboutsummaryrefslogtreecommitdiff
path: root/lex.l
diff options
context:
space:
mode:
Diffstat (limited to 'lex.l')
-rw-r--r--lex.l213
1 files changed, 98 insertions, 115 deletions
diff --git a/lex.l b/lex.l
index 48f943a..ba48a9c 100644
--- a/lex.l
+++ b/lex.l
@@ -1,6 +1,6 @@
%{
/* ckaliases - verify syntax of sendmail-style alias files
- Copyright (C) 2005 Sergey Poznyakoff
+ Copyright (C) 2005, 2007 Sergey Poznyakoff
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
@@ -15,23 +15,13 @@
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>. */
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/file.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <string.h>
-#define obstack_chunk_alloc malloc
-#define obstack_chunk_free free
-#include <obstack.h>
#include "ckaliases.h"
#include "gram.h"
-static void line_begin(void);
-static void line_add(char *text, size_t len);
-static void line_add_unescape(char *text, size_t len);
-static void line_finish(void);
+static void line_begin (void);
+static void line_add (char *text, size_t len);
+static void line_add_unescape (char *text, size_t len);
+static void line_finish (void);
struct obstack string_stk;
@@ -54,32 +44,32 @@ SPEC [:@\\]
/* Names and emails */
:include: return INCLUDE;
^{IDENT} {
- line_begin();
- line_add(yytext, yyleng);
- line_finish();
+ line_begin ();
+ line_add (yytext, yyleng);
+ line_finish ();
return LHS; }
{IDENT}@{IDENT} {
- line_begin();
- line_add(yytext, yyleng);
- line_finish();
+ line_begin ();
+ line_add (yytext, yyleng);
+ line_finish ();
return EMAIL; }
-{IDENT} { line_begin();
- line_add(yytext, yyleng);
- line_finish();
+{IDENT} { line_begin ();
+ line_add (yytext, yyleng);
+ line_finish ();
return IDENT; }
/* Quoted strings */
-\"[^\\"\n]*\" { line_begin();
- line_add(yytext, yyleng);
- line_finish();
+\"[^\\"\n]*\" { line_begin ();
+ line_add (yytext, yyleng);
+ line_finish ();
return STRING; }
-\"[^\\"\n]*\\. { BEGIN(STR);
- line_begin();
- line_add_unescape(yytext + 1, yyleng - 1); }
-<STR>[^\\"\n]*\\. { line_add_unescape(yytext, yyleng); }
-<STR>[^\\"\n]*\" { BEGIN(INITIAL);
+\"[^\\"\n]*\\. { BEGIN (STR);
+ line_begin ();
+ line_add_unescape (yytext + 1, yyleng - 1); }
+<STR>[^\\"\n]*\\. { line_add_unescape (yytext, yyleng); }
+<STR>[^\\"\n]*\" { BEGIN (INITIAL);
if (yyleng > 1)
- line_add(yytext, yyleng - 1);
- line_finish();
+ line_add (yytext, yyleng - 1);
+ line_finish ();
return STRING; }
/* Other characters */
{SPEC} return yytext[0];
@@ -87,18 +77,16 @@ SPEC [:@\\]
\n{WS}+/[^ \t\n] { line_num++; return CONT; }
\n { line_num++; return EOL; }
, return yytext[0];
-. { char *p;
- asprintf(&p,
- "Stray character %03o in alias file", yytext[0]);
- yyerror(p);
- free (p); }
+. { error_at_line (0, 0, file_name, line_num,
+ "Stray character %03o in alias file", yytext[0]);
+ error_count++; }
%%
int
-yywrap()
+yywrap ()
{
- fclose(yyin);
- return 1;
+ fclose (yyin);
+ return 1;
}
static char escape_transtab[] = "\\\\a\ab\bf\fn\nr\rt\t";
@@ -117,112 +105,107 @@ unescape_char (int c)
}
void
-line_add(char *text, size_t len)
+line_add (char *text, size_t len)
{
- obstack_grow(&string_stk, text, len);
+ obstack_grow (&string_stk, text, len);
}
void
-line_add_unescape(char *text, size_t len)
+line_add_unescape (char *text, size_t len)
{
- char c;
- obstack_grow(&string_stk, text, len - 2);
- c = unescape_char(text[len - 1]);
- obstack_1grow(&string_stk, c);
+ char c;
+ obstack_grow (&string_stk, text, len - 2);
+ c = unescape_char (text[len - 1]);
+ obstack_1grow (&string_stk, c);
}
void
-line_begin()
+line_begin ()
{
}
void
-line_finish()
+line_finish ()
{
- obstack_1grow(&string_stk, 0);
- yylval.string = obstack_finish(&string_stk);
+ obstack_1grow (&string_stk, 0);
+ yylval.string = obstack_finish (&string_stk);
}
void
-openaliases(char *name)
+openaliases (char *name)
{
- yyin = fopen(name, "r");
- if (!yyin) {
- fprintf(stderr, "cannot open file `%s': %s\n",
- name, strerror(errno));
- exit(1);
- }
- file_name = name;
- line_num = 0;
+ yyin = fopen (name, "r");
+ if (!yyin)
+ error (1, errno, "cannot open file `%s'", name);
+ file_name = name;
+ line_num = 0;
}
void
-openaliases_prefix(char *prefix, char *name)
+openaliases_prefix (char *prefix, char *name)
{
- char *fullname = NULL;
- struct stat st;
+ char *fullname = NULL;
+ struct stat st;
- if (stat(prefix, &st)) {
- fprintf(stderr, "cannot stat `%s': %s\n",
- prefix, strerror(errno));
- exit(1);
- }
-
- if (!S_ISDIR(st.st_mode)) {
- char *p = strrchr(prefix, '/');
- if (p)
- *p = 0;
- else
- prefix = ".";
- }
- asprintf(&fullname, "%s/%s", prefix, name);
- openaliases(fullname);
- free(fullname);
+ if (stat (prefix, &st))
+ error (1, errno, "cannot stat `%s'", prefix);
+
+ if (!S_ISDIR (st.st_mode))
+ {
+ char *p = strrchr (prefix, '/');
+ if (p)
+ *p = 0;
+ else
+ prefix = ".";
+ }
+ asprintf (&fullname, "%s/%s", prefix, name);
+ openaliases (fullname);
+ free (fullname);
}
void
-init_lex()
+init_lex ()
{
- obstack_init(&string_stk);
- yy_flex_debug = 0;
+ obstack_init (&string_stk);
+ yy_flex_debug = 0;
}
void
-lex_debug(int debug)
+lex_debug (int debug)
{
- yy_flex_debug = debug;
+ yy_flex_debug = debug;
}
void
-read_include(SLIST **plist, char *name)
+read_include (SLIST **plist, char *name)
{
- char *p;
- char buffer[256];
- FILE *fp = fopen(name, "r");
-
- if (!fp) {
- char *p;
- asprintf(&p,
- "cannot open include file `%s': %s",
- name, strerror(errno));
- yyerror(p);
- free(p);
- return;
- }
-
- while (p = fgets(buffer, sizeof buffer, fp)) {
- char *q;
-
- while (*p && isspace(*p))
- p++;
- if (*p == '#')
- continue;
- for (q = p + strlen(p) - 1; q > p && isspace(*q); q--)
- ;
- q[1] = 0;
- if (*p)
- slist_add(plist, strdup(p));
- }
- fclose(fp);
+ char *p;
+ char buffer[256];
+ FILE *fp = fopen (name, "r");
+
+ if (!fp)
+ {
+ error_at_line (0, 0, file_name, line_num,
+ "cannot open include file `%s': %s",
+ name, strerror (errno));
+ error_count++;
+ return;
+ }
+
+ while (p = fgets (buffer, sizeof buffer, fp))
+ {
+ char *q;
+
+ while (*p && isspace (*p))
+ p++;
+ if (*p == '#')
+ continue;
+ for (q = p + strlen (p) - 1; q > p && isspace (*q); q--)
+ ;
+ q[1] = 0;
+ if (*p)
+ slist_add (plist, strdup (p));
+ }
+ fclose (fp);
}

Return to:

Send suggestions and report system problems to the System administrator.