home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / gawk213s.lzh / GAWK213S / VMS_GAWK.C < prev    next >
C/C++ Source or Header  |  1993-07-29  |  9KB  |  246 lines

  1. /*
  2.  * vms_gawk.c -- parse GAWK command line using DCL syntax ]
  3.  */
  4.  
  5. /*
  6.  * Copyright (C) 1991 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 1, or (at your option)
  14.  * 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, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  */
  25.  
  26. /*
  27.  * vms_gawk.c - routines to parse the command line as a native DCL command
  28.  *           rather than as a foreign command string.
  29.  *                            Pat Rankin, Nov'89
  30.  *                        [ revised for 2.12, May'91 ]
  31.  */
  32.  
  33. #include "awk.h"
  34. #include "vms.h"
  35. #define COMMAND_NAME "GAWK"    /* verb name & for 'usage' message(s) */
  36. #define USAGE_PROG_RQRD 1
  37. #define USAGE_FILE_RQRD 2
  38. #define USAGE_BAD_COMBO 3
  39. #define USAGE_RUN_CMD    4
  40. #define STS$M_INHIB_MSG 0x10000000
  41.  
  42. #define Present(arg)        vmswork(Cli_Present(arg))
  43. #define Get_Value(arg,buf,siz)    vmswork(Cli_Get_Value(arg,buf,siz))
  44.  
  45. extern void   gawk_cmd();    /* created with $ SET COMMAND/OBJECT */
  46. static int    vms_usage(int);
  47.  
  48. #define ARG_SIZ 250
  49. union arg_w_prefix {    /* structure used to simplify prepending of "-" */
  50.     char     value[3+ARG_SIZ+1];
  51.     struct {
  52.     char prefix[3];        /* for "-? " */
  53.     char buf[ARG_SIZ];
  54.     char suffix[1];        /* room for '\0' */
  55.     } arg;
  56. };
  57.  
  58. #define chk_option(qualifier,optname)    \
  59.     if (Present(qualifier))    \
  60.     strcat(strcat(buf.arg.buf, W_cnt++ ? "," : ""), optname)
  61.  
  62.  
  63. /* vms_gawk() - parse GAWK command line using DCL and convert it into the */
  64. /*           appropriate "-arg" values for compatability with GNU code  */
  65. int
  66. vms_gawk()
  67. {
  68.     u_long sts;
  69.     union arg_w_prefix buf;
  70.     char misc_args[10], *misc_argp;
  71.     int  argc, W_cnt;
  72.  
  73.     /* check "GAWK_P1"--it's required; its presence will tip us off */
  74.     sts = Cli_Present("GAWK_P1");
  75.     if (CondVal(sts) == CondVal(CLI$_SYNTAX)) {
  76.     /* syntax error indicates that we weren't invoked as a native DCL
  77.        command, so we'll now attempt to generate a command from the
  78.        foreign command string and parse that.
  79.     */
  80.     sts = Cli_Parse_Command(gawk_cmd, COMMAND_NAME);
  81.     if (vmswork(sts))
  82.         sts = Cli_Present("GAWK_P1");
  83.     }
  84.     if (vmswork(sts))        /* command parsed successfully */
  85.     v_add_arg(argc = 0, COMMAND_NAME);    /* save "GAWK" as argv[0] */
  86.     else if (CondVal(sts) == CondVal(CLI$_INSFPRM))
  87.     return vms_usage(USAGE_FILE_RQRD);  /* insufficient parameters */
  88.     else if (CondVal(sts) == CondVal(CLI$_CONFLICT))
  89.     return vms_usage(USAGE_BAD_COMBO);  /* conflicting qualifiers (/input+/command) */
  90.     else if (CondVal(sts) == CondVal(CLI$_RUNUSED))
  91.     return vms_usage(USAGE_RUN_CMD);    /* RUN GAWK won't work (no command line) */
  92.     else
  93.     return 0;    /* forced to rely on original parsing */
  94.  
  95.     if (Present("USAGE"))    /* give usage message and quit */
  96.     return vms_usage(0);
  97.     else if (! (Present("PROGRAM") || Present("PROGFILE")) )
  98.     return vms_usage(USAGE_PROG_RQRD);  /* missing required option */
  99.  
  100.     misc_argp = misc_args;
  101.     *misc_argp++ = '-';        /* now points at &misc_args[1] */
  102.     if (Present("REG_EXPR")) {
  103.     if (Present("REG_EXPR.AWK"))        /* /reg_exp=awk -> -a */
  104.         *misc_argp++ = 'a';
  105.     else if (Present("REG_EXPR.EGREP")    /* /reg_exp=egrep -> -e */
  106.           || Present("REG_EXPR.POSIX"))    /* /reg_exp=posix -> -e */
  107.         *misc_argp++ = 'e';
  108.     }
  109. #if 0    /* gawk 2.11.1 */
  110.     if (Present("STRICT"))        /* /strict -> -c */
  111.     *misc_argp++ = 'c';
  112.     if (Present("COPYRIGHT"))        /* /copyright -> -C */
  113.     *misc_argp++ = 'C';
  114.     if (Present("VERSION"))        /* /version -> -V */
  115.     *misc_argp++ = 'V';
  116. #else    /* gawk 2.12 */
  117.     W_cnt = 0,    buf.arg.buf[0] = '\0';
  118.     strncpy(buf.arg.prefix, "-W ", 3);
  119.     chk_option("LINT","lint");
  120.     chk_option("POSIX","posix");
  121.     chk_option("STRICT","compat");
  122.     chk_option("COPYRIGHT","copyright");
  123.     chk_option("VERSION","version");
  124.     if (W_cnt > 0)            /* got something */
  125.     v_add_arg(++argc, strdup(buf.value));
  126. #endif    /*0*/
  127. #ifdef DEBUG
  128.     if (Present("DEBUG")) {
  129. #if 0
  130.     int both = Present("DEBUG.ALL");
  131.     if (both || Present("DEBUG.EXECUTION"))
  132.         *misc_argp++ = 'd';
  133.     if (both || Present("DEBUG.PARSE"))
  134. #endif
  135.         *misc_argp++ = 'D';
  136.     }
  137. #endif
  138.     *misc_argp = '\0';        /* terminate misc_args[] */
  139.     if (misc_argp > &misc_args[1])    /* got something */
  140.     v_add_arg(++argc, misc_args);    /* store it/them */
  141.  
  142.     if (Present("FIELD_SEP")) {     /* field separator */
  143.     strncpy(buf.arg.prefix, "-F ", 3);
  144.     if (Get_Value("FIELD_SEP", buf.arg.buf, sizeof buf.arg.buf))
  145.         v_add_arg(++argc, strdup(buf.value));
  146.     }
  147.     if (Present("VARIABLES")) {     /* variables to init prior to BEGIN */
  148.     strncpy(buf.arg.prefix, "-v ", 3);
  149.     while (Get_Value("VARIABLES", buf.arg.buf, sizeof buf.arg.buf))
  150.         v_add_arg(++argc, strdup(buf.value));
  151.     }
  152.     if (Present("PROGFILE")) {        /* program files, /input=file -> -f file */
  153.     strncpy(buf.arg.prefix, "-f ", 3);
  154.     while (Get_Value("PROGFILE", buf.arg.buf, sizeof buf.arg.buf))
  155.         v_add_arg(++argc, strdup(buf.value));
  156.     v_add_arg(++argc, "--");
  157.     } else if (Present("PROGRAM")) {    /* program text, /program -> 'text' */
  158.     v_add_arg(++argc, "--");
  159.     if (Get_Value("PROGRAM", buf.value, sizeof buf.value))
  160.         v_add_arg(++argc, strdup(buf.value));
  161.     }
  162.  
  163.     /* we know that "GAWK_P1" is present [data files and/or 'var=value'] */
  164.     while (Get_Value("GAWK_P1", buf.value, sizeof buf.value))
  165.     v_add_arg(++argc, strdup(buf.value));
  166.  
  167.     if (Present("OUTPUT")) {    /* let other parser treat this as 'stdout' */
  168.     strncpy(buf.arg.prefix, ">$ ", 3);
  169.     if (Get_Value("OUTPUT", buf.arg.buf, sizeof buf.arg.buf))
  170.         v_add_arg(++argc, strdup(buf.value));
  171.     }
  172.  
  173.     return ++argc;        /*(increment to account for arg[0])*/
  174. }
  175.  
  176. /* vms_usage() - display one or more messages and then terminate */
  177. static int    /* note: doesn't return anything; allows 'return vms_usage()' */
  178. vms_usage( int complaint )
  179. {
  180. static char
  181.     *usage_txt = "\n\
  182. usage:    %s  /COMMANDS=\"awk program text\"  data_file[,data_file,...] \n\
  183.    or    %s  /INPUT=awk_file  data_file[,\"Var=value\",data_file,...] \n\
  184.    or    %s  /INPUT=(awk_file1,awk_file2,...)  data_file[,...] \n\
  185. ",  *options_txt = "\n\
  186. options:  /FIELD_SEPARATOR=\"FS_value\" \n\
  187.    -      /VARIABLES=(\"Var1=value1\",\"Var2=value2\",...) \n\
  188.    -      /REG_EXPR= AWK or EGREP or POSIX \n\
  189.    -      /LINT  /POSIX  /[NO]STRICT  /VERSION    /COPYRIGHT  /USAGE \n\
  190.    -      /OUTPUT=out_file \n\
  191. ",  *no_prog = "missing required element: /COMMANDS or /INPUT",
  192.     *no_file = "missing required element: data_file \n\
  193.        (use \"SYS$INPUT:\" to read data lines from the terminal)",
  194.     *bad_combo = "invalid combination of qualifiers \n\
  195.        (/INPUT=awk_file and /COMMANDS=\"awk program\" are mutually exclusive)",
  196.     *run_used = "\"RUN\" was used; required command components missing";
  197. int status, argc;
  198.  
  199.     fflush(stdout);
  200.     switch (complaint) {
  201.       case USAGE_PROG_RQRD:
  202.     fprintf(stderr, "\n%%%s-W-%s, %s \n", COMMAND_NAME, "PROG_RQRD", no_prog);
  203.     status = CLI$_VALREQ | STS$M_INHIB_MSG;
  204.     break;
  205.       case USAGE_FILE_RQRD:
  206.     if (Present("USAGE")) {
  207.         status = 1;        /* clean exit */
  208.     } else if (Present("COPYRIGHT") || Present("VERSION")) {
  209.         v_add_arg(argc=0, COMMAND_NAME);    /* save "GAWK" as argv[0] */
  210. #if 0
  211.         v_add_arg(++argc, Present("COPYRIGHT") ? "-C" : "-V");
  212. #else
  213.         v_add_arg(++argc, "-W");
  214.         v_add_arg(++argc, Present("COPYRIGHT") ? "copyright" : "version");
  215. #endif
  216.         v_add_arg(++argc, "{}");        /* kludge to suppress 'usage' */
  217.         v_add_arg(++argc, "NL:");        /* dummy input for kludge */
  218.         return ++argc;            /* count argv[0] too */
  219.     } else {
  220.         fprintf(stderr, "\n%%%s-W-%s, %s \n", COMMAND_NAME, "FILE_RQRD", no_file);
  221.         status = CLI$_INSFPRM | STS$M_INHIB_MSG;
  222.     }
  223.     break;
  224.       case USAGE_BAD_COMBO:
  225.     fprintf(stderr, "\n%%%s-W-%s, %s \n", COMMAND_NAME, "BAD_COMBO", bad_combo);
  226.     status = CLI$_CONFLICT | STS$M_INHIB_MSG;
  227.     break;
  228.       case USAGE_RUN_CMD:
  229.     fprintf(stderr, "\n%%%s-W-%s, %s \n", COMMAND_NAME, "RUN_CMD", run_used);
  230.     status = CLI$_NOOPTPRS | STS$M_INHIB_MSG;
  231.     break;
  232.       default:
  233.     status = 1;
  234.     break;
  235.     }
  236.     fprintf(stderr, usage_txt, COMMAND_NAME, COMMAND_NAME, COMMAND_NAME);
  237.     fprintf(stderr, options_txt);
  238.     fflush(stderr);
  239.  
  240.     errno = EVMSERR;
  241.     vaxc$errno = status;
  242.     _exit(status);
  243.     /* NOTREACHED */
  244.     return 0;
  245. }
  246.