home *** CD-ROM | disk | FTP | other *** search
/ ftp.uni-stuttgart.de/pub/systems/acorn/ / Acorn.tar / Acorn / acornet / dev / gawk.spk / gawk-2154 / c / main < prev    next >
Text File  |  1994-01-04  |  20KB  |  796 lines

  1. /*
  2.  * main.c -- Expression tree constructors and main program for gawk. 
  3.  */
  4.  
  5. /* 
  6.  * Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993 the Free Software Foundation, Inc.
  7.  * 
  8.  * This file is part of GAWK, the GNU implementation of the
  9.  * AWK Progamming Language.
  10.  * 
  11.  * GAWK is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  * 
  16.  * GAWK is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  * 
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with GAWK; see the file COPYING.  If not, write to
  23.  * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  */
  25.  
  26. #include "getopt.h"
  27. #include "awk.h"
  28. #include "patchlevel.h"
  29.  
  30. static void usage P((int exitval));
  31. static void copyleft P((void));
  32. static void cmdline_fs P((char *str));
  33. static void init_args P((int argc0, int argc, char *argv0, char **argv));
  34. static void init_vars P((void));
  35. static void pre_assign P((char *v));
  36. SIGTYPE catchsig P((int sig, int code));
  37. static void gawk_option P((char *optstr));
  38. static void nostalgia P((void));
  39. static void version P((void));
  40. char *gawk_name P((char *filespec));
  41.  
  42. #ifdef MSDOS
  43. extern int isatty P((int));
  44. #endif
  45.  
  46. extern void resetup P((void));
  47.  
  48. /* These nodes store all the special variables AWK uses */
  49. NODE *FS_node, *NF_node, *RS_node, *NR_node;
  50. NODE *FILENAME_node, *OFS_node, *ORS_node, *OFMT_node;
  51. NODE *CONVFMT_node;
  52. NODE *ERRNO_node;
  53. NODE *FNR_node, *RLENGTH_node, *RSTART_node, *SUBSEP_node;
  54. NODE *ENVIRON_node, *IGNORECASE_node;
  55. NODE *ARGC_node, *ARGV_node, *ARGIND_node;
  56. NODE *FIELDWIDTHS_node;
  57.  
  58. int NF;
  59. int NR;
  60. int FNR;
  61. int IGNORECASE;
  62. char *RS;
  63. char *OFS;
  64. char *ORS;
  65. char *OFMT;
  66. char *CONVFMT;
  67.  
  68. /*
  69.  * The parse tree and field nodes are stored here.  Parse_end is a dummy item
  70.  * used to free up unneeded fields without freeing the program being run 
  71.  */
  72. int errcount = 0;    /* error counter, used by yyerror() */
  73.  
  74. /* The global null string */
  75. NODE *Nnull_string;
  76.  
  77. /* The name the program was invoked under, for error messages */
  78. const char *myname;
  79.  
  80. /* A block of AWK code to be run before running the program */
  81. NODE *begin_block = 0;
  82.  
  83. /* A block of AWK code to be run after the last input file */
  84. NODE *end_block = 0;
  85.  
  86. int exiting = 0;        /* Was an "exit" statement executed? */
  87. int exit_val = 0;        /* optional exit value */
  88.  
  89. #if defined(YYDEBUG) || defined(DEBUG)
  90. extern int yydebug;
  91. #endif
  92.  
  93. struct src *srcfiles = NULL;        /* source file name(s) */
  94. int numfiles = -1;        /* how many source files */
  95.  
  96. int do_unix = 0;        /* turn off gnu extensions */
  97. int do_posix = 0;        /* turn off gnu and unix extensions */
  98. int do_lint = 0;        /* provide warnings about questionable stuff */
  99. int do_nostalgia = 0;        /* provide a blast from the past */
  100.  
  101. int in_begin_rule = 0;        /* we're in a BEGIN rule */
  102. int in_end_rule = 0;        /* we're in a END rule */
  103.  
  104. int output_is_tty = 0;        /* control flushing of output */
  105.  
  106. extern char *version_string;    /* current version, for printing */
  107.  
  108. NODE *expression_value;
  109.  
  110. static struct option optab[] = {
  111.     { "compat",        no_argument,        & do_unix,    1 },
  112.     { "lint",        no_argument,        & do_lint,    1 },
  113.     { "posix",        no_argument,        & do_posix,    1 },
  114.     { "nostalgia",        no_argument,        & do_nostalgia,    1 },
  115.     { "copyleft",        no_argument,        NULL,        'C' },
  116.     { "copyright",        no_argument,        NULL,        'C' },
  117.     { "field-separator",    required_argument,    NULL,        'F' },
  118.     { "file",        required_argument,    NULL,        'f' },
  119.     { "assign",        required_argument,    NULL,        'v' },
  120.     { "version",        no_argument,        NULL,        'V' },
  121.     { "usage",        no_argument,        NULL,        'u' },
  122.     { "help",        no_argument,        NULL,        'u' },
  123.     { "source",        required_argument,    NULL,        's' },
  124. #ifdef DEBUG
  125.     { "parsedebug",        no_argument,        NULL,        'D' },
  126. #endif
  127.     { 0, 0, 0, 0 }
  128. };
  129.  
  130. int
  131. main(argc, argv)
  132. int argc;
  133. char **argv;
  134. {
  135.     int c;
  136.     char *scan;
  137.     extern int optind;
  138.     extern int opterr;
  139.     extern char *optarg;
  140.     const char *optlist = "+F:f:v:W:m:";
  141.     int stopped_early = 0;
  142.  
  143. #ifdef __EMX__
  144.     _response(&argc, &argv);
  145.     _wildcard(&argc, &argv);
  146.     setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
  147. #endif
  148.  
  149.     (void) signal(SIGFPE,  (SIGTYPE (*) P((int))) catchsig);
  150.     (void) signal(SIGSEGV, (SIGTYPE (*) P((int))) catchsig);
  151. #ifdef SIGBUS
  152.     (void) signal(SIGBUS,  (SIGTYPE (*) P((int))) catchsig);
  153. #endif
  154.  
  155.     myname = gawk_name(argv[0]);
  156.         argv[0] = (char *)myname;
  157. #ifdef VMS
  158.     vms_arg_fixup(&argc, &argv); /* emulate redirection, expand wildcards */
  159. #endif
  160.  
  161.     /* remove sccs gunk */
  162.     if (strncmp(version_string, "@(#)", 4) == 0)
  163.         version_string += 4;
  164.  
  165.     if (argc < 2)
  166.         usage(1);
  167.  
  168.     /* initialize the null string */
  169.     Nnull_string = make_string("", 0);
  170.     Nnull_string->numbr = 0.0;
  171.     Nnull_string->type = Node_val;
  172.     Nnull_string->flags = (PERM|STR|STRING|NUM|NUMBER);
  173.  
  174.     /* Set up the special variables */
  175.     /*
  176.      * Note that this must be done BEFORE arg parsing else -F
  177.      * breaks horribly 
  178.      */
  179.     init_vars();
  180.  
  181.     /* worst case */
  182.     emalloc(srcfiles, struct src *, argc * sizeof(struct src), "main");
  183.     memset(srcfiles, '\0', argc * sizeof(struct src));
  184.  
  185.     /* Tell the regex routines how they should work. . . */
  186.     resetup();
  187.  
  188.     /* we do error messages ourselves on invalid options */
  189.     opterr = 0;
  190.  
  191.     /* the + on the front tells GNU getopt not to rearrange argv */
  192.     for (optopt = 0;
  193.          (c = getopt_long(argc, argv, optlist, optab, NULL)) != EOF;
  194.          optopt = 0) {
  195.         if (do_posix)
  196.             opterr = 1;
  197.         switch (c) {
  198.         case 'F':
  199.             cmdline_fs(optarg);
  200.             break;
  201.  
  202.         case 'f':
  203.             /*
  204.              * a la MKS awk, allow multiple -f options.
  205.              * this makes function libraries real easy.
  206.              * most of the magic is in the scanner.
  207.              */
  208.             /* The following is to allow for whitespace at the end
  209.              * of a #! /bin/gawk line in an executable file
  210.              */
  211.             scan = optarg;
  212.             while (isspace(*scan))
  213.                 scan++;
  214.             ++numfiles;
  215.             srcfiles[numfiles].stype = SOURCEFILE;
  216.             if (*scan == '\0')
  217.                 srcfiles[numfiles].val = argv[optind++];
  218.             else
  219.                 srcfiles[numfiles].val = optarg;
  220.             break;
  221.  
  222.         case 'v':
  223.             pre_assign(optarg);
  224.             break;
  225.  
  226.         case 'm':
  227.             /*
  228.              * Research awk extension.
  229.              *    -mf=nnn        set # fields, gawk ignores
  230.              *    -mr=nnn        set record length, ditto
  231.              */
  232.             if (do_lint)
  233.                 warning("-m[fr] option irrelevant");
  234.             if ((optarg[0] != 'r' && optarg[0] != 'f')
  235.                 || optarg[1] != '=')
  236.                 warning("-m option usage: -m[fn]=nnn");
  237.             break;
  238.  
  239.         case 'W':       /* gawk specific options */
  240.             gawk_option(optarg);
  241.             break;
  242.  
  243.         /* These can only come from long form options */
  244.         case 'V':
  245.             version();
  246.             break;
  247.  
  248.         case 'C':
  249.             copyleft();
  250.             break;
  251.  
  252.         case 'u':
  253.             usage(0);
  254.             break;
  255.  
  256.         case 's':
  257.             if (strlen(optarg) == 0)
  258.                 warning("empty argument to --source ignored");
  259.             else {
  260.                 srcfiles[++numfiles].stype = CMDLINE;
  261.                 srcfiles[numfiles].val = optarg;
  262.             }
  263.             break;
  264.  
  265. #ifdef DEBUG
  266.         case 'D':
  267.             yydebug = 2;
  268.             break;
  269. #endif
  270.  
  271.         case 0:
  272.             /*
  273.              * getopt_long found an option that sets a variable
  274.              * instead of returning a letter. Do nothing, just
  275.              * cycle around for the next one.
  276.              */
  277.             break;
  278.  
  279.         case '?':
  280.         default:
  281.             /*
  282.              * New behavior.  If not posix, an unrecognized
  283.              * option stops argument processing so that it can
  284.              * go into ARGV for the awk program to see. This
  285.              * makes use of ``#! /bin/gawk -f'' easier.
  286.              *
  287.              * However, it's never simple. If optopt is set,
  288.              * an option that requires an argument didn't get the
  289.              * argument. We care because if opterr is 0, then
  290.              * getopt_long won't print the error message for us.
  291.              */
  292.             if (! do_posix
  293.                 && (optopt == 0 || strchr(optlist, optopt) == NULL)) {
  294.                 optind--;
  295.                 stopped_early = 1;
  296.                 goto out;
  297.             } else if (optopt)
  298.                 /* Use 1003.2 required message format */
  299.                 fprintf (stderr,
  300.                 "%s: option requires an argument -- %c\n",
  301.                     myname, optopt);
  302.             /* else
  303.                 let getopt print error message for us */
  304.             break;
  305.         }
  306.     }
  307. out:
  308.  
  309.     if (do_nostalgia)
  310.         nostalgia();
  311.  
  312.     /* POSIX compliance also implies no Unix extensions either */
  313.     if (do_posix)
  314.         do_unix = 1;
  315.  
  316. #ifdef DEBUG
  317.     setbuf(stdout, (char *) NULL);    /* make debugging easier */
  318. #endif
  319.     if (isatty(fileno(stdout)))
  320.         output_is_tty = 1;
  321.     /* No -f or --source options, use next arg */
  322.     if (numfiles == -1) {
  323.         if (optind > argc - 1 || stopped_early) /* no args left or no program */
  324.             usage(1);
  325.         srcfiles[++numfiles].stype = CMDLINE;
  326.         srcfiles[numfiles].val = argv[optind];
  327.         optind++;
  328.     }
  329.     init_args(optind, argc, (char *) myname, argv);
  330.     (void) tokexpand();
  331.  
  332.     /* Read in the program */
  333.     if (yyparse() || errcount)
  334.         exit(1);
  335.  
  336.     /* Set up the field variables */
  337.     init_fields();
  338.  
  339.     if (begin_block) {
  340.         in_begin_rule = 1;
  341.         (void) interpret(begin_block);
  342.     }
  343.     in_begin_rule = 0;
  344.     if (!exiting && (expression_value || end_block))
  345.         do_input();
  346.     if (end_block) {
  347.         in_end_rule = 1;
  348.         (void) interpret(end_block);
  349.     }
  350.     in_end_rule = 0;
  351.     if (close_io() != 0 && exit_val == 0)
  352.         exit_val = 1;
  353.     exit(exit_val);        /* more portable */
  354.     return exit_val;    /* to suppress warnings */
  355. }
  356.  
  357. /* usage --- print usage information and exit */
  358.  
  359. static void
  360. usage(exitval)
  361. int exitval;
  362. {
  363.     const char *opt1 = " -f progfile [--]";
  364. #if defined(MSDOS) || defined(OS2) || defined(VMS)
  365.     const char *opt2 = " [--] \"program\"";
  366. #else
  367.     const char *opt2 = " [--] 'program'";
  368. #endif
  369.     const char *regops = " [POSIX or GNU style options]";
  370.  
  371.     fprintf(stderr, "Usage:\t%s%s%s file ...\n\t%s%s%s file ...\n",
  372.         myname, regops, opt1, myname, regops, opt2);
  373.  
  374.     /* GNU long options info. Gack. */
  375.     fputs("POSIX options:\t\tGNU long options:\n", stderr);
  376.     fputs("\t-f progfile\t\t--file=progfile\n", stderr);
  377.     fputs("\t-F fs\t\t\t--field-separator=fs\n", stderr);
  378.     fputs("\t-v var=val\t\t--assign=var=val\n", stderr);
  379.     fputs("\t-m[fr]=val\n", stderr);
  380.     fputs("\t-W compat\t\t--compat\n", stderr);
  381.     fputs("\t-W copyleft\t\t--copyleft\n", stderr);
  382.     fputs("\t-W copyright\t\t--copyright\n", stderr);
  383.     fputs("\t-W help\t\t\t--help\n", stderr);
  384.     fputs("\t-W lint\t\t\t--lint\n", stderr);
  385. #ifdef NOSTALGIA
  386.     fputs("\t-W nostalgia\t\t--nostalgia\n", stderr);
  387. #endif
  388. #ifdef DEBUG
  389.     fputs("\t-W parsedebug\t\t--parsedebug\n", stderr);
  390. #endif
  391.     fputs("\t-W posix\t\t--posix\n", stderr);
  392.     fputs("\t-W source=program-text\t--source=program-text\n", stderr);
  393.     fputs("\t-W usage\t\t--usage\n", stderr);
  394.     fputs("\t-W version\t\t--version\n", stderr);
  395.     exit(exitval);
  396. }
  397.  
  398. static void
  399. copyleft ()
  400. {
  401.     static char blurb_part1[] =
  402. "Copyright (C) 1989, 1991, 1992, Free Software Foundation.\n\
  403. \n\
  404. This program is free software; you can redistribute it and/or modify\n\
  405. it under the terms of the GNU General Public License as published by\n\
  406. the Free Software Foundation; either version 2 of the License, or\n\
  407. (at your option) any later version.\n\
  408. \n";
  409.     static char blurb_part2[] =
  410. "This program is distributed in the hope that it will be useful,\n\
  411. but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
  412. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
  413. GNU General Public License for more details.\n\
  414. \n";
  415.     static char blurb_part3[] =
  416. "You should have received a copy of the GNU General Public License\n\
  417. along with this program; if not, write to the Free Software\n\
  418. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n";
  419.  
  420.     fputs(blurb_part1, stderr);
  421.     fputs(blurb_part2, stderr);
  422.     fputs(blurb_part3, stderr);
  423.     fflush(stderr);
  424. }
  425.  
  426. static void
  427. cmdline_fs(str)
  428. char *str;
  429. {
  430.     register NODE **tmp;
  431.     /* int len = strlen(str); *//* don't do that - we want to
  432.                                    avoid mismatched types */
  433.  
  434.     tmp = get_lhs(FS_node, (Func_ptr *) 0);
  435.     unref(*tmp);
  436.     /*
  437.      * Only if in full compatibility mode check for the stupid special
  438.      * case so -F\t works as documented in awk even though the shell
  439.      * hands us -Ft.  Bleah!
  440.      *
  441.      * Thankfully, Posix didn't propogate this "feature".
  442.      */
  443.     if (str[0] == 't' && str[1] == '\0') {
  444.         if (do_lint)
  445.             warning("-Ft does not set FS to tab in POSIX awk");
  446.         if (do_unix && ! do_posix)
  447.             str[0] = '\t';
  448.     }
  449.     *tmp = make_str_node(str, strlen(str), SCAN); /* do process escapes */
  450.     set_FS();
  451. }
  452.  
  453. static void
  454. init_args(argc0, argc, argv0, argv)
  455. int argc0, argc;
  456. char *argv0;
  457. char **argv;
  458. {
  459.     int i, j;
  460.     NODE **aptr;
  461.  
  462.     ARGV_node = install("ARGV", node(Nnull_string, Node_var, (NODE *)NULL));
  463.     aptr = assoc_lookup(ARGV_node, tmp_number(0.0));
  464.     *aptr = make_string(argv0, strlen(argv0));
  465.     (*aptr)->flags |= MAYBE_NUM;
  466.     for (i = argc0, j = 1; i < argc; i++) {
  467.         aptr = assoc_lookup(ARGV_node, tmp_number((AWKNUM) j));
  468.         *aptr = make_string(argv[i], strlen(argv[i]));
  469.         (*aptr)->flags |= MAYBE_NUM;
  470.         j++;
  471.     }
  472.     ARGC_node = install("ARGC",
  473.             node(make_number((AWKNUM) j), Node_var, (NODE *) NULL));
  474. }
  475.  
  476. /*
  477.  * Set all the special variables to their initial values.
  478.  */
  479. struct varinit {
  480.     NODE **spec;
  481.     const char *name;
  482.     NODETYPE type;
  483.     const char *strval;
  484.     AWKNUM numval;
  485.     Func_ptr assign;
  486. };
  487. static struct varinit varinit[] = {
  488. {&NF_node,    "NF",        Node_NF,        0,    -1, set_NF },
  489. {&FIELDWIDTHS_node, "FIELDWIDTHS", Node_FIELDWIDTHS,    "",    0,  0 },
  490. {&NR_node,    "NR",        Node_NR,        0,    0,  set_NR },
  491. {&FNR_node,    "FNR",        Node_FNR,        0,    0,  set_FNR },
  492. {&FS_node,    "FS",        Node_FS,        " ",    0,  0 },
  493. {&RS_node,    "RS",        Node_RS,        "\n",    0,  set_RS },
  494. {&IGNORECASE_node, "IGNORECASE", Node_IGNORECASE,    0,    0,  set_IGNORECASE },
  495. {&FILENAME_node, "FILENAME",    Node_var,        "",    0,  0 },
  496. {&OFS_node,    "OFS",        Node_OFS,        " ",    0,  set_OFS },
  497. {&ORS_node,    "ORS",        Node_ORS,        "\n",    0,  set_ORS },
  498. {&OFMT_node,    "OFMT",        Node_OFMT,        "%.6g",    0,  set_OFMT },
  499. {&CONVFMT_node,    "CONVFMT",    Node_CONVFMT,        "%.6g",    0,  set_CONVFMT },
  500. {&RLENGTH_node, "RLENGTH",    Node_var,        0,    0,  0 },
  501. {&RSTART_node,    "RSTART",    Node_var,        0,    0,  0 },
  502. {&SUBSEP_node,    "SUBSEP",    Node_var,        "\034",    0,  0 },
  503. {&ARGIND_node,    "ARGIND",    Node_var,        0,    0,  0 },
  504. {&ERRNO_node,    "ERRNO",    Node_var,        0,    0,  0 },
  505. {0,        0,        Node_illegal,        0,    0,  0 },
  506. };
  507.  
  508. static void
  509. init_vars()
  510. {
  511.     register struct varinit *vp;
  512.  
  513.     for (vp = varinit; vp->name; vp++) {
  514.         *(vp->spec) = install((char *) vp->name,
  515.           node(vp->strval == 0 ? make_number(vp->numval)
  516.                 : make_string((char *) vp->strval,
  517.                     strlen(vp->strval)),
  518.                vp->type, (NODE *) NULL));
  519.         if (vp->assign)
  520.             (*(vp->assign))();
  521.     }
  522. }
  523.  
  524. void
  525. load_environ()
  526. {
  527. #if !defined(MSDOS) && !defined(OS2) && !(defined(VMS) && defined(__DECC))
  528.     extern char **environ;
  529. #endif
  530.     register char *var, *val;
  531.     NODE **aptr;
  532.     register int i;
  533.  
  534.     ENVIRON_node = install("ENVIRON", 
  535.             node(Nnull_string, Node_var, (NODE *) NULL));
  536.     for (i = 0; environ[i]; i++) {
  537.         static char nullstr[] = "";
  538.  
  539.         var = environ[i];
  540.         val = strchr(var, '=');
  541.         if (val)
  542.             *val++ = '\0';
  543.         else
  544.             val = nullstr;
  545.         aptr = assoc_lookup(ENVIRON_node, tmp_string(var, strlen (var)));
  546.         *aptr = make_string(val, strlen (val));
  547.         (*aptr)->flags |= MAYBE_NUM;
  548.  
  549.         /* restore '=' so that system() gets a valid environment */
  550.         if (val != nullstr)
  551.             *--val = '=';
  552.     }
  553. }
  554.  
  555. /* Process a command-line assignment */
  556. char *
  557. arg_assign(arg)
  558. char *arg;
  559. {
  560.     char *cp, *cp2;
  561.     int badvar;
  562.     Func_ptr after_assign = NULL;
  563.     NODE *var;
  564.     NODE *it;
  565.     NODE **lhs;
  566.  
  567.     cp = strchr(arg, '=');
  568.     if (cp != NULL) {
  569.         *cp++ = '\0';
  570.         /* first check that the variable name has valid syntax */
  571.         badvar = 0;
  572.         if (! isalpha(arg[0]) && arg[0] != '_')
  573.             badvar = 1;
  574.         else
  575.             for (cp2 = arg+1; *cp2; cp2++)
  576.                 if (! isalnum(*cp2) && *cp2 != '_') {
  577.                     badvar = 1;
  578.                     break;
  579.                 }
  580.         if (badvar)
  581.             fatal("illegal name `%s' in variable assignment", arg);
  582.  
  583.         /*
  584.          * Recent versions of nawk expand escapes inside assignments.
  585.          * This makes sense, so we do it too.
  586.          */
  587.         it = make_str_node(cp, strlen(cp), SCAN);
  588.         it->flags |= MAYBE_NUM;
  589.         var = variable(arg, 0);
  590.         lhs = get_lhs(var, &after_assign);
  591.         unref(*lhs);
  592.         *lhs = it;
  593.         if (after_assign)
  594.             (*after_assign)();
  595.         *--cp = '=';    /* restore original text of ARGV */
  596.     }
  597.     return cp;
  598. }
  599.  
  600. static void
  601. pre_assign(v)
  602. char *v;
  603. {
  604.     if (!arg_assign(v)) {
  605.         fprintf (stderr,
  606.             "%s: '%s' argument to -v not in 'var=value' form\n",
  607.                 myname, v);
  608.         usage(1);
  609.     }
  610. }
  611.  
  612. SIGTYPE
  613. catchsig(sig, code)
  614. int sig, code;
  615. {
  616. #ifdef lint
  617.     code = 0; sig = code; code = sig;
  618. #endif
  619.     if (sig == SIGFPE) {
  620.         fatal("floating point exception");
  621.     } else if (sig == SIGSEGV
  622. #ifdef SIGBUS
  623.             || sig == SIGBUS
  624. #endif
  625.     ) {
  626.         msg("fatal error: internal error");
  627.         /* fatal won't abort() if not compiled for debugging */
  628.         abort();
  629.     } else
  630.         cant_happen();
  631.     /* NOTREACHED */
  632. }
  633.  
  634. /* gawk_option --- do gawk specific things */
  635.  
  636. static void
  637. gawk_option(optstr)
  638. char *optstr;
  639. {
  640.     char *cp;
  641.  
  642.     for (cp = optstr; *cp; cp++) {
  643.         switch (*cp) {
  644.         case ' ':
  645.         case '\t':
  646.         case ',':
  647.             break;
  648.         case 'v':
  649.         case 'V':
  650.             /* print version */
  651.             if (strncasecmp(cp, "version", 7) != 0)
  652.                 goto unknown;
  653.             else
  654.                 cp += 6;
  655.             version();
  656.             break;
  657.         case 'c':
  658.         case 'C':
  659.             if (strncasecmp(cp, "copyright", 9) == 0) {
  660.                 cp += 8;
  661.                 copyleft();
  662.             } else if (strncasecmp(cp, "copyleft", 8) == 0) {
  663.                 cp += 7;
  664.                 copyleft();
  665.             } else if (strncasecmp(cp, "compat", 6) == 0) {
  666.                 cp += 5;
  667.                 do_unix = 1;
  668.             } else
  669.                 goto unknown;
  670.             break;
  671.         case 'n':
  672.         case 'N':
  673.             /*
  674.              * Undocumented feature,
  675.              * inspired by nostalgia, and a T-shirt
  676.              */
  677.             if (strncasecmp(cp, "nostalgia", 9) != 0)
  678.                 goto unknown;
  679.             nostalgia();
  680.             break;
  681.         case 'p':
  682.         case 'P':
  683. #ifdef DEBUG
  684.             if (strncasecmp(cp, "parsedebug", 10) == 0) {
  685.                 cp += 9;
  686.                 yydebug = 2;
  687.                 break;
  688.             }
  689. #endif
  690.             if (strncasecmp(cp, "posix", 5) != 0)
  691.                 goto unknown;
  692.             cp += 4;
  693.             do_posix = do_unix = 1;
  694.             break;
  695.         case 'l':
  696.         case 'L':
  697.             if (strncasecmp(cp, "lint", 4) != 0)
  698.                 goto unknown;
  699.             cp += 3;
  700.             do_lint = 1;
  701.             break;
  702.         case 'H':
  703.         case 'h':
  704.             if (strncasecmp(cp, "help", 4) != 0)
  705.                 goto unknown;
  706.             cp += 3;
  707.             usage(0);
  708.             break;
  709.         case 'U':
  710.         case 'u':
  711.             if (strncasecmp(cp, "usage", 5) != 0)
  712.                 goto unknown;
  713.             cp += 4;
  714.             usage(0);
  715.             break;
  716.         case 's':
  717.         case 'S':
  718.             if (strncasecmp(cp, "source=", 7) != 0)
  719.                 goto unknown;
  720.             cp += 7;
  721.             if (strlen(cp) == 0)
  722.                 warning("empty argument to -Wsource ignored");
  723.             else {
  724.                 srcfiles[++numfiles].stype = CMDLINE;
  725.                 srcfiles[numfiles].val = cp;
  726.                 return;
  727.             }
  728.             break;
  729.         default:
  730.         unknown:
  731.             fprintf(stderr, "'%c' -- unknown option, ignored\n",
  732.                 *cp);
  733.             break;
  734.         }
  735.     }
  736. }
  737.  
  738. /* nostalgia --- print the famous error message and die */
  739.  
  740. static void
  741. nostalgia()
  742. {
  743.     fprintf(stderr, "awk: bailing out near line 1\n");
  744.     abort();
  745. }
  746.  
  747. /* version --- print version message */
  748.  
  749. static void
  750. version()
  751. {
  752.     fprintf(stderr, "%s, patchlevel %d\n", version_string, PATCHLEVEL);
  753.     /* per GNU coding standards, exit successfully, do nothing else */
  754.     exit(0);
  755. }
  756.  
  757. /* this mess will improve in 2.16 */
  758. char *
  759. gawk_name(filespec)
  760. char *filespec;
  761. {
  762.     char *p;
  763.     
  764. #ifdef VMS    /* "device:[root.][directory.subdir]GAWK.EXE;n" -> "GAWK" */
  765.     char *q;
  766.  
  767.     p = strrchr(filespec, ']');  /* directory punctuation */
  768.     q = strrchr(filespec, '>');  /* alternate <international> punct */
  769.  
  770.     if (p == NULL || q > p) p = q;
  771.     p = strdup(p == NULL ? filespec : (p + 1));
  772.     if ((q = strrchr(p, '.')) != NULL)  *q = '\0';  /* strip .typ;vers */
  773.  
  774.     return p;
  775. #endif /*VMS*/
  776.  
  777. #if defined(MSDOS) || defined(OS2) || defined(atarist)
  778.     char *q;
  779.  
  780.     for (p = filespec; (p = strchr(p, '\\')); *p = '/')
  781.         ;
  782.     p = filespec;
  783.     if ((q = strrchr(p, '/')))
  784.         p = q + 1;
  785.     if ((q = strchr(p, '.')))
  786.         *q = '\0';
  787.     strlwr(p);
  788.  
  789.     return (p == NULL ? filespec : p);
  790. #endif /* MSDOS || atarist */
  791.  
  792.     /* "path/name" -> "name" */
  793.     p = strrchr(filespec, '/');
  794.     return (p == NULL ? filespec : p + 1);
  795. }
  796.