diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-03-05 09:16:29 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-03-05 09:16:29 +0200 |
commit | f142ee94c2fa3d5f8ec3686b2ced2e1f0b807def (patch) | |
tree | 6a150aea4679db77420db91c26e6a93865abbe65 /gram.y | |
parent | a75760ff7d9bedcb1377fc3441f5e38178da1d6a (diff) | |
download | alck-f142ee94c2fa3d5f8ec3686b2ced2e1f0b807def.tar.gz alck-f142ee94c2fa3d5f8ec3686b2ced2e1f0b807def.tar.bz2 |
Revamp as a standalone project. Remove unneeded dependencies.
* Makefile.am: Remove
* Makefile: Restore from bc518485
* ckaliases.c: Rename to alck.c
* ckaliases.h: Rename to alck.h
* gram.y (emalloc, error)
(syserror, parserror): New functions.
(main): Use traditional getopt.
* lex.l: Use slist instead of obstack.
* slist.c: New file.
Diffstat (limited to 'gram.y')
-rw-r--r-- | gram.y | 115 |
1 files changed, 83 insertions, 32 deletions
@@ -12,14 +12,16 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 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 "ckaliases.h" - +#include "alck.h" +#include <stdarg.h> + +char *program_name; SLIST *cw_list; /* List of domain names pertaining to Sendmail 'w' class */ static int restricted; /* prohibit use of `special' aliases (pipes, file redirections and includes */ int verbose; /* Verbose mode */ int error_count; /* Number of errors detected so far */ %} @@ -108,70 +110,116 @@ string: IDENT %% int yyerror (char *s) { - error_at_line(0, 0, file_name, line_num, "%s", s); + parserror(file_name, line_num, "%s", s); error_count++; } + +void * +emalloc(size_t size) +{ + void *p = malloc(size); + if (!p) { + error("not enough memory"); + exit(1); + } + return p; +} + +void +error(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + fprintf(stderr, "%s: ", program_name); + vfprintf(stderr, fmt, ap); + fputc('\n', stderr); + va_end(ap); +} + +void +syserror(int ec, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + fprintf(stderr, "%s: ", program_name); + vfprintf(stderr, fmt, ap); + fprintf(stderr, ": %s\n", strerror(ec)); + va_end(ap); +} + +void +parserror(const char *file, int line, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + fprintf(stderr, "%s: %s:%d: ", program_name, file, line); + vfprintf(stderr, fmt, ap); + fputc('\n', stderr); + va_end(ap); +} void usage() { printf("usage: ckaliases [OPTIONS] [FILES...]\n"); printf("OPTIONS and FILES may be interspered.\n"); printf("Valid options are:\n"); - printf(" -d,--debug=SPEC Set debug level. SPEC consists of the following\n"); + printf(" -d SPEC Set debug level. SPEC consists of the following\n"); printf(" letters:\n"); printf(" y enable parser debugging\n"); printf(" l enable lexical analizer debugging\n"); printf(" Upper-case variants are also accepted. Prepending\n"); printf(" a letter with '-' reverts its sense\n"); - printf(" -f, --files-from=FILE\n"); - printf(" Read names of alias files from FILE\n"); - printf(" -h, --help Display this help list\n"); - printf(" -r, --restrict Restrict alias file syntax to aliases only (i.e.\n"); + printf(" -f FILE Read names of alias files from FILE\n"); + printf(" -h Display this help list\n"); + printf(" -r Restrict alias file syntax to aliases only (i.e.\n"); printf(" prohibit use of pipes and file redirections\n"); - printf(" -u, --unrestrict Revert the effect of the previous -r option\n"); - printf(" -v, --verbose Verbose mode\n"); - printf(" -V, --version print program version and exit\n"); + printf(" -u Revert the effect of the previous -r option\n"); + printf(" -v Verbose mode\n"); + printf(" -V print program version and exit\n"); printf(" -w FILE Read contents of Sendmail `w' class from the given\n"); printf(" file.\n"); printf("\n"); - printf("Report bugs to <%s>\n", PACKAGE_BUGREPORT); + printf("Report bugs to <%s>\n", "gray@gnu.org"); } -struct option options[] = { - { "debug", required_argument, NULL, 'd' }, - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'V' }, - { "restrict", no_argument, NULL, 'r' }, - { "unrestrict", no_argument, NULL, 'u' }, - { "verbose", no_argument, NULL, 'v' }, - { "files-from", required_argument, NULL, 'f' }, - { NULL } -}; +static char license[] = "\ +License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n\ +This is free software: you are free to change and redistribute it.\n\ +There is NO WARRANTY, to the extent permitted by law.\n"; + +#define VERSION "2.0" + +void +version() +{ + printf("alck %s\n", VERSION); + printf("Copyright (C) 2005, 2007, 2013 Sergey Poznyakoff\n"); + printf("%s\n", license); + exit(0); +} int main(int argc, char **argv) { char *p; int c; int file_count = 0; int true = 1; char *cwfile = "/etc/mail/sendmail.cw"; SLIST *file_list; /* List of files to be read */ struct string_list *s; - begin_aliases(); - init_lex (); + init_lex(); program_name = argv[0]; - while ((c = getopt_long(argc, argv, "-d:f:hp:ruvw:", - options, NULL)) != EOF) { + while ((c = getopt(argc, argv, "-d:f:hp:ruVvw:")) != EOF) { switch (c) { case 1: if (!cw_list) read_include(&cw_list, cwfile); openaliases(optarg); yyparse(); @@ -195,13 +243,14 @@ main(int argc, char **argv) case 'L': lex_debug(true); true = 1; break; default: - error(1, 0, "%s: unknown debug option %c", argv[0]); + error("%s: unknown debug option %c", + argv[0], c); } } break; case 'f': if (!cw_list) @@ -232,18 +281,18 @@ main(int argc, char **argv) case 'v': verbose++; break; case 'V': - gsc_version ("ckaliases"); + version(); exit (0); case 'w': if (file_count) - error(1, 0, "-w must be used before first non-option argument"); + error("-w must be used before first non-option argument"); cwfile = optarg; break; default: exit (1); } @@ -257,15 +306,17 @@ main(int argc, char **argv) while (argc--) { openaliases(*argv++); yyparse(); file_count++; } - if (!file_count) - error(1, 0, "no files specified"); - + if (!file_count) { + error("no files specified"); + exit(1); + } + if (verbose) printf("%d files\n", file_count); end_aliases(); check_aliases(); if (verbose) printf("%lu errors\n", error_count); |