2
* vms_gawk.c -- parse GAWK command line using DCL syntax ]
6
* Copyright (C) 1991 the Free Software Foundation, Inc.
8
* This file is part of GAWK, the GNU implementation of the
9
* AWK Progamming Language.
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)
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.
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.
27
* vms_gawk.c - routines to parse the command line as a native DCL command
28
* rather than as a foreign command string.
30
* [ revised for 2.12, May'91 ]
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
42
#define Present(arg) vmswork(Cli_Present(arg))
43
#define Get_Value(arg,buf,siz) vmswork(Cli_Get_Value(arg,buf,siz))
45
extern void gawk_cmd(); /* created with $ SET COMMAND/OBJECT */
46
static int vms_usage(int);
49
union arg_w_prefix { /* structure used to simplify prepending of "-" */
50
char value[3+ARG_SIZ+1];
52
char prefix[3]; /* for "-? " */
54
char suffix[1]; /* room for '\0' */
58
#define chk_option(qualifier,optname) \
59
if (Present(qualifier)) \
60
strcat(strcat(buf.arg.buf, W_cnt++ ? "," : ""), optname)
63
/* vms_gawk() - parse GAWK command line using DCL and convert it into the */
64
/* appropriate "-arg" values for compatability with GNU code */
69
union arg_w_prefix buf;
70
char misc_args[10], *misc_argp;
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.
80
sts = Cli_Parse_Command(gawk_cmd, COMMAND_NAME);
82
sts = Cli_Present("GAWK_P1");
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) */
93
return 0; /* forced to rely on original parsing */
95
if (Present("USAGE")) /* give usage message and quit */
97
else if (! (Present("PROGRAM") || Present("PROGFILE")) )
98
return vms_usage(USAGE_PROG_RQRD); /* missing required option */
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 */
105
else if (Present("REG_EXPR.EGREP") /* /reg_exp=egrep -> -e */
106
|| Present("REG_EXPR.POSIX")) /* /reg_exp=posix -> -e */
109
#if 0 /* gawk 2.11.1 */
110
if (Present("STRICT")) /* /strict -> -c */
112
if (Present("COPYRIGHT")) /* /copyright -> -C */
114
if (Present("VERSION")) /* /version -> -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));
128
if (Present("DEBUG")) {
130
int both = Present("DEBUG.ALL");
131
if (both || Present("DEBUG.EXECUTION"))
133
if (both || Present("DEBUG.PARSE"))
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 */
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));
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));
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));
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));
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));
173
return ++argc; /*(increment to account for arg[0])*/
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 )
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";
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;
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] */
211
v_add_arg(++argc, Present("COPYRIGHT") ? "-C" : "-V");
213
v_add_arg(++argc, "-W");
214
v_add_arg(++argc, Present("COPYRIGHT") ? "copyright" : "version");
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 */
220
fprintf(stderr, "\n%%%s-W-%s, %s \n", COMMAND_NAME, "FILE_RQRD", no_file);
221
status = CLI$_INSFPRM | STS$M_INHIB_MSG;
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;
229
fprintf(stderr, "\n%%%s-W-%s, %s \n", COMMAND_NAME, "RUN_CMD", run_used);
230
status = CLI$_NOOPTPRS | STS$M_INHIB_MSG;
236
fprintf(stderr, usage_txt, COMMAND_NAME, COMMAND_NAME, COMMAND_NAME);
237
fprintf(stderr, options_txt);