home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuawk.zip / vms / vms_gawk.c < prev    next >
C/C++ Source or Header  |  1997-05-01  |  9KB  |  245 lines

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