386BSD 0.1 development
[unix-history] / usr / src / usr.bin / awk / init.c
CommitLineData
a7e60862
WJ
1
2/********************************************
3init.c
4copyright 1991, Michael D. Brennan
5
6This is a source file for mawk, an implementation of
7the AWK programming language.
8
9Mawk is distributed without warranty under the terms of
10the GNU General Public License, version 2, 1991.
11********************************************/
12
13
14/* $Log: init.c,v $
15 * Revision 5.2 92/01/09 08:46:14 brennan
16 * small change for MSC
17 *
18 * Revision 5.1 91/12/05 07:56:07 brennan
19 * 1.1 pre-release
20 *
21*/
22
23
24/* init.c */
25#include "mawk.h"
26#include "code.h"
27#include "memory.h"
28#include "symtype.h"
29#include "init.h"
30#include "bi_vars.h"
31#include "field.h"
32
33#ifdef THINK_C
34#include <console.h>
35#endif
36
37
38static void PROTO( process_cmdline , (int, char **) ) ;
39static void PROTO( set_ARGV, (int, char **, int)) ;
40static void PROTO( bad_option, (char *)) ;
41
42extern void PROTO( print_version, (void) ) ;
43extern int PROTO( is_cmdline_assign, (char*)) ;
44
45#if 0
46#if MSDOS && ! HAVE_REARGV
47#include <fcntl.h>
48static void PROTO(emit_prompt, (void) ) ;
49#endif
50#endif
51
52
53void initialize(argc, argv)
54 int argc ; char **argv ;
55{
56 bi_vars_init() ; /* load the builtin variables */
57 bi_funct_init() ; /* load the builtin functions */
58 kw_init() ; /* load the keywords */
59 field_init() ;
60
61#ifdef THINK_C
62 fputc('\n',stderr); /* (causes Think C console window to appear) */
63 SetWTitle( FrontWindow(), "\pMacMAWK" );
64 argc = ccommand(&argv);
65#endif
66
67#if MSDOS && NO_BINMODE==0
68 { char *p = getenv("MAWKBINMODE") ;
69
70 if ( p ) set_binmode( atoi(p) ) ;
71 }
72#endif
73
74
75 process_cmdline(argc, argv) ;
76
77 code_init() ;
78 fpe_init() ;
79 set_stderr() ;
80}
81
82void compile_cleanup()
83/* program has parsed OK, free some memory
84 we don't need anymore */
85{
86 scan_cleanup() ;
87 code_cleanup() ;
88}
89
90int dump_code ; /* if on dump internal code */
91int posix_space_flag ;
92
93#ifdef DEBUG
94int dump_RE ; /* if on dump compiled REs */
95#endif
96
97
98static void bad_option(s)
99 char *s ;
100{ errmsg(0, "not an option: %s", s) ; mawk_exit(1) ; }
101
102
103static void process_cmdline(argc, argv)
104 int argc ;
105 char **argv ;
106{
107 int i , nextarg ;
108 char *optarg ;
109 PFILE dummy ; /* starts linked list of filenames */
110 PFILE *tail = &dummy ;
111
112 for( i = 1 ; i < argc && argv[i][0] == '-' ; i = nextarg )
113 {
114 if ( argv[i][1] == 0 ) /* - alone */
115 {
116 if ( ! pfile_name )
117 { errmsg(0, "no program") ; exit(1) ; }
118 break ; /* the for loop */
119 }
120 /* safe to look at argv[i][2] */
121
122 if ( argv[i][2] == 0 )
123 {
124 if ( i == argc-1 && argv[i][1] != '-' )
125 {
126 if ( strchr("WFvf", argv[i][1]) )
127 { errmsg(0, "option %s lacks argument", argv[i]) ;
128 mawk_exit(1) ;
129 }
130 bad_option(argv[i]) ;
131 }
132
133 optarg = argv[i+1] ;
134 nextarg = i+2 ;
135 }
136 else /* argument glued to option */
137 {
138 optarg = &argv[i][2] ;
139 nextarg = i+1 ;
140 }
141
142 switch( argv[i][1] )
143 {
144 case 'W' :
145
146 if ( optarg[0] >= 'a' && optarg[0] <= 'z')
147 optarg[0] += 'A' - 'a' ;
148 if ( optarg[0] == 'V' ) print_version() ;
149 else
150 if ( optarg[0] == 'D' )
151 { dump_code = 1 ;
152#if SM_DOS
153 errmsg(0, "-W dump option unavailable in small model") ;
154 dump_code=0 ;
155#endif
156 }
157 else
158 if ( optarg[0] == 'S' )
159 {
160 char *p = strchr(optarg,'=') ;
161 int x = p ? atoi(p+1) : 0 ;
162
163 if ( x > SPRINTF_SZ )
164 {
165 sprintf_buff = (char *) zmalloc(x) ;
166 sprintf_limit = sprintf_buff + x ;
167 }
168 }
169#if MSDOS && NO_BINMODE==0
170 else
171 if ( optarg[0] == 'B' )
172 {
173 char *p = strchr(optarg,'=') ;
174 int x = p ? atoi(p+1) : 0 ;
175
176 set_binmode(x) ;
177 }
178#endif
179 else
180 if ( optarg[0] == 'P' )
181 {
182 posix_space_flag = 1 ;
183 }
184 else
185 errmsg(0,"vacuous option: -W %s", optarg) ;
186
187
188 break ;
189
190 case 'v' :
191 if ( ! is_cmdline_assign(optarg) )
192 { errmsg(0 , "improper assignment: -v %s" , optarg) ;
193 exit(1) ;
194 }
195 break ;
196
197 case 'F' :
198
199 rm_escape(optarg) ; /* recognize escape sequences */
200 cell_destroy(FS) ;
201 FS->type = C_STRING ;
202 FS->ptr = (PTR) new_STRING(optarg) ;
203 cast_for_split( cellcpy(&fs_shadow, FS) ) ;
204 break ;
205
206 case '-' :
207 if ( argv[i][2] != 0 ) bad_option(argv[i]) ;
208 i++ ;
209 goto no_more_opts ;
210
211 case 'f' :
212 /* first file goes in pfile_name ; any more go
213 on a list */
214 if ( ! pfile_name ) pfile_name = optarg ;
215 else
216 { tail = tail->link = ZMALLOC(PFILE) ;
217 tail->fname = optarg ;
218 }
219 break ;
220
221 default : bad_option(argv[i]) ;
222
223 }
224
225 i = nextarg ;
226 }
227
228no_more_opts:
229
230 tail->link = (PFILE*) 0 ;
231 pfile_list = dummy.link ;
232
233 if ( pfile_name )
234 {
235 set_ARGV(argc, argv, i) ;
236 scan_init( (char *) 0 ) ;
237 }
238 else /* program on command line */
239 {
240#if 0
241#if MSDOS && ! HAVE_REARGV /* prompt for program */
242 set_ARGV(argc, argv, i) ;
243 emit_prompt() ;
244 pfile_name = "CON" ;
245 scan_init( (char *) 0 ) ;
246#else /* the real world */
247#endif
248#endif /* 0 */
249 if ( i == argc )
250 { errmsg(0, "no program") ; mawk_exit(1) ; }
251 set_ARGV(argc, argv, i+1) ;
252
253#if MSDOS && ! HAVE_REARGV /* reversed quotes */
254 { char *p ;
255
256 for( p = argv[i] ; *p ; p++ )
257 if ( *p == '\'' ) *p = '\"' ;
258 }
259#endif
260 scan_init( argv[i]) ;
261/* #endif */
262 }
263}
264
265
266static void set_ARGV(argc, argv, i)
267 int argc ; char **argv ;
268 int i ; /* argv[i] = ARGV[1] */
269{
270 SYMTAB *st_p ;
271 CELL argi ;
272 register CELL *cp ;
273
274 st_p = insert( "ARGV" ) ;
275 st_p->type = ST_ARRAY ;
276 Argv = st_p->stval.array = new_ARRAY() ;
277 argi.type = C_DOUBLE ;
278 argi.dval = 0.0 ;
279 cp = array_find( st_p->stval.array, &argi, CREATE) ;
280 cp->type = C_STRING ;
281 cp->ptr = (PTR) new_STRING( progname ) ;
282
283 /* ARGV[0] is set, do the rest
284 The type of ARGV[1] ... should be C_MBSTRN
285 because the user might enter numbers from the command line */
286
287 for( argi.dval = 1.0 ; i < argc ; i++, argi.dval += 1.0 )
288 {
289 cp = array_find( st_p->stval.array, &argi, CREATE) ;
290 cp->type = C_MBSTRN ;
291 cp->ptr = (PTR) new_STRING( argv[i] ) ;
292 }
293 ARGC->type = C_DOUBLE ;
294 ARGC->dval = argi.dval ;
295}
296
297#if 0
298#if MSDOS && ! HAVE_REARGV
299
300static void emit_prompt()
301{ static char prompt[] = "mawk> " ;
302 int fd = open("CON", O_WRONLY, 0) ;
303
304 (void) write(fd, prompt, strlen(prompt)) ;
305 (void) close(fd) ;
306}
307#endif
308#endif
309
310
311/*----- ENVIRON ----------*/
312
313void load_environ( ENV )
314 ARRAY ENV ;
315{
316 CELL c ;
317#ifndef MSDOS_MSC /* MSC declares it near */
318 extern char **environ ;
319#endif
320 register char **p = environ ; /* walks environ */
321 char *s ; /* looks for the '=' */
322 CELL *cp ; /* pts at ENV[&c] */
323
324 c.type = C_STRING ;
325
326 while ( *p )
327 {
328 if ( s = strchr(*p, '=') ) /* shouldn't fail */
329 {
330 *s = 0 ;
331 c.ptr = (PTR) new_STRING(*p) ;
332 *s++ = '=' ;
333
334 cp = array_find(ENV, &c, CREATE) ;
335 cp->type = C_MBSTRN ;
336 cp->ptr = (PTR) new_STRING(s) ;
337
338 free_STRING(string(&c)) ;
339 }
340 p++ ;
341 }
342}