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