2 /********************************************
4 copyright 1991, Michael D. Brennan
6 This is a source file for mawk, an implementation of
7 the AWK programming language.
9 Mawk is distributed without warranty under the terms of
10 the GNU General Public License, version 2, 1991.
11 ********************************************/
15 * Revision 1.11 1995/08/20 17:35:21 mike
16 * include <stdlib.h> for MSC, needed for environ decl
18 * Revision 1.10 1995/06/09 22:51:50 mike
19 * silently exit(0) if no program
21 * Revision 1.9 1995/06/06 00:18:30 mike
22 * change mawk_exit(1) to mawk_exit(2)
24 * Revision 1.8 1994/12/14 14:40:34 mike
27 * Revision 1.7 1994/12/11 22:43:20 mike
28 * don't assume **environ is writable
30 * Revision 1.6 1994/12/11 22:14:16 mike
31 * remove THINK_C #defines. Not a political statement, just no indication
32 * that anyone ever used it.
34 * Revision 1.5 1994/10/08 19:15:45 mike
37 * Revision 1.4 1994/03/11 02:23:49 mike
40 * Revision 1.3 1993/07/17 00:45:14 mike
43 * Revision 1.2 1993/07/04 12:52:00 mike
44 * start on autoconfig changes
46 * Revision 5.5 1993/01/07 02:50:33 mike
47 * relative vs absolute code
49 * Revision 5.4 1992/12/24 01:58:19 mike
50 * 1.1.2d changes for MsDOS
52 * Revision 5.3 1992/07/10 16:17:10 brennan
53 * MsDOS: remove NO_BINMODE macro
55 * Revision 5.2 1992/01/09 08:46:14 brennan
56 * small change for MSC
58 * Revision 5.1 91/12/05 07:56:07 brennan
81 static void PROTO(process_cmdline, (int, char **)) ;
82 static void PROTO(set_ARGV, (int, char **, int)) ;
83 static void PROTO(bad_option, (char *)) ;
84 static void PROTO(no_program, (void)) ;
86 extern void PROTO(print_version, (void)) ;
87 extern int PROTO(is_cmdline_assign, (char *)) ;
90 void PROTO(stdout_init, (void)) ;
92 void PROTO(reargv, (int *, char ***)) ;
97 short interactive_flag = 0 ;
100 #define SET_PROGNAME() \
101 {char *p = strrchr(argv[0],'/') ;\
102 progname = p ? p+1 : argv[0] ; }
106 initialize(argc, argv)
107 int argc ; char **argv ;
112 bi_vars_init() ; /* load the builtin variables */
113 bi_funct_init() ; /* load the builtin functions */
114 kw_init() ; /* load the keywords */
119 char *p = getenv("MAWKBINMODE") ;
121 if (p) set_binmode(atoi(p)) ;
126 process_cmdline(argc, argv) ;
137 int dump_code_flag ; /* if on dump internal code */
138 short posix_space_flag ;
141 int dump_RE ; /* if on dump compiled REs */
149 errmsg(0, "not an option: %s", s) ; mawk_exit(2) ;
159 process_cmdline(argc, argv)
165 PFILE dummy ; /* starts linked list of filenames */
166 PFILE *tail = &dummy ;
168 for (i = 1; i < argc && argv[i][0] == '-'; i = nextarg)
170 if (argv[i][1] == 0) /* - alone */
172 if (!pfile_name) no_program() ;
173 break ; /* the for loop */
175 /* safe to look at argv[i][2] */
179 if (i == argc - 1 && argv[i][1] != '-')
181 if (strchr("WFvf", argv[i][1]))
183 errmsg(0, "option %s lacks argument", argv[i]) ;
186 bad_option(argv[i]) ;
189 optarg = argv[i + 1] ;
192 else /* argument glued to option */
194 optarg = &argv[i][2] ;
202 if (optarg[0] >= 'a' && optarg[0] <= 'z')
203 optarg[0] += 'A' - 'a' ;
204 if (optarg[0] == 'V') print_version() ;
205 else if (optarg[0] == 'D')
209 else if (optarg[0] == 'S')
211 char *p = strchr(optarg, '=') ;
212 int x = p ? atoi(p + 1) : 0 ;
216 sprintf_buff = (char *) zmalloc(x) ;
217 sprintf_limit = sprintf_buff + x ;
221 else if (optarg[0] == 'B')
223 char *p = strchr(optarg, '=') ;
224 int x = p ? atoi(p + 1) : 0 ;
229 else if (optarg[0] == 'P')
231 posix_space_flag = 1 ;
233 else if (optarg[0] == 'E')
237 errmsg(0, "-W exec is incompatible with -f") ;
240 else if ( nextarg == argc ) no_program() ;
242 pfile_name = argv[nextarg] ;
246 else if (optarg[0] == 'I')
248 interactive_flag = 1 ;
249 setbuf(stdout,(char*)0) ;
251 else errmsg(0, "vacuous option: -W %s", optarg) ;
257 if (!is_cmdline_assign(optarg))
259 errmsg(0, "improper assignment: -v %s", optarg) ;
266 rm_escape(optarg) ; /* recognize escape sequences */
268 FS->type = C_STRING ;
269 FS->ptr = (PTR) new_STRING(optarg) ;
270 cast_for_split(cellcpy(&fs_shadow, FS)) ;
274 if (argv[i][2] != 0) bad_option(argv[i]) ;
279 /* first file goes in pfile_name ; any more go
281 if (!pfile_name) pfile_name = optarg ;
284 tail = tail->link = ZMALLOC(PFILE) ;
285 tail->fname = optarg ;
290 bad_option(argv[i]) ;
296 tail->link = (PFILE *) 0 ;
297 pfile_list = dummy.link ;
301 set_ARGV(argc, argv, i) ;
302 scan_init((char *) 0) ;
304 else /* program on command line */
306 if (i == argc) no_program() ;
307 set_ARGV(argc, argv, i + 1) ;
309 #if MSDOS && ! HAVE_REARGV /* reversed quotes */
313 for (p = argv[i]; *p; p++)
314 if (*p == '\'') *p = '\"' ;
324 set_ARGV(argc, argv, i)
325 int argc ; char **argv ;
326 int i ; /* argv[i] = ARGV[i] */
332 st_p = insert("ARGV") ;
333 st_p->type = ST_ARRAY ;
334 Argv = st_p->stval.array = new_ARRAY() ;
335 argi.type = C_DOUBLE ;
337 cp = array_find(st_p->stval.array, &argi, CREATE) ;
338 cp->type = C_STRING ;
339 cp->ptr = (PTR) new_STRING(progname) ;
341 /* ARGV[0] is set, do the rest
342 The type of ARGV[1] ... should be C_MBSTRN
343 because the user might enter numbers from the command line */
345 for (argi.dval = 1.0; i < argc; i++, argi.dval += 1.0)
347 cp = array_find(st_p->stval.array, &argi, CREATE) ;
348 cp->type = C_MBSTRN ;
349 cp->ptr = (PTR) new_STRING(argv[i]) ;
351 ARGC->type = C_DOUBLE ;
352 ARGC->dval = argi.dval ;
356 /*----- ENVIRON ----------*/
363 #ifndef MSDOS_MSC /* MSC declares it near */
364 extern char **environ ;
366 register char **p = environ ; /* walks environ */
367 char *s ; /* looks for the '=' */
368 CELL *cp ; /* pts at ENV[&c] */
374 if ((s = strchr(*p, '='))) /* shouldn't fail */
377 c.ptr = (PTR) new_STRING0(len) ;
378 memcpy(string(&c)->str, *p, len) ;
381 cp = array_find(ENV, &c, CREATE) ;
382 cp->type = C_MBSTRN ;
383 cp->ptr = (PTR) new_STRING(s) ;
385 free_STRING(string(&c)) ;