/********************************************
copyright 1991, Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 2, 1991.
********************************************/
* Revision 5.2 92/01/09 08:46:14 brennan
* Revision 5.1 91/12/05 07:56:07 brennan
static void PROTO( process_cmdline
, (int, char **) ) ;
static void PROTO( set_ARGV
, (int, char **, int)) ;
static void PROTO( bad_option
, (char *)) ;
extern void PROTO( print_version
, (void) ) ;
extern int PROTO( is_cmdline_assign
, (char*)) ;
#if MSDOS && ! HAVE_REARGV
static void PROTO(emit_prompt
, (void) ) ;
void initialize(argc
, argv
)
bi_vars_init() ; /* load the builtin variables */
bi_funct_init() ; /* load the builtin functions */
kw_init() ; /* load the keywords */
fputc('\n',stderr
); /* (causes Think C console window to appear) */
SetWTitle( FrontWindow(), "\pMacMAWK" );
#if MSDOS && NO_BINMODE==0
{ char *p
= getenv("MAWKBINMODE") ;
if ( p
) set_binmode( atoi(p
) ) ;
process_cmdline(argc
, argv
) ;
/* program has parsed OK, free some memory
int dump_code
; /* if on dump internal code */
int dump_RE
; /* if on dump compiled REs */
static void bad_option(s
)
{ errmsg(0, "not an option: %s", s
) ; mawk_exit(1) ; }
static void process_cmdline(argc
, argv
)
PFILE dummy
; /* starts linked list of filenames */
for( i
= 1 ; i
< argc
&& argv
[i
][0] == '-' ; i
= nextarg
)
if ( argv
[i
][1] == 0 ) /* - alone */
{ errmsg(0, "no program") ; exit(1) ; }
break ; /* the for loop */
/* safe to look at argv[i][2] */
if ( i
== argc
-1 && argv
[i
][1] != '-' )
if ( strchr("WFvf", argv
[i
][1]) )
{ errmsg(0, "option %s lacks argument", argv
[i
]) ;
else /* argument glued to option */
if ( optarg
[0] >= 'a' && optarg
[0] <= 'z')
if ( optarg
[0] == 'V' ) print_version() ;
errmsg(0, "-W dump option unavailable in small model") ;
char *p
= strchr(optarg
,'=') ;
int x
= p
? atoi(p
+1) : 0 ;
sprintf_buff
= (char *) zmalloc(x
) ;
sprintf_limit
= sprintf_buff
+ x
;
#if MSDOS && NO_BINMODE==0
char *p
= strchr(optarg
,'=') ;
int x
= p
? atoi(p
+1) : 0 ;
errmsg(0,"vacuous option: -W %s", optarg
) ;
if ( ! is_cmdline_assign(optarg
) )
{ errmsg(0 , "improper assignment: -v %s" , optarg
) ;
rm_escape(optarg
) ; /* recognize escape sequences */
FS
->ptr
= (PTR
) new_STRING(optarg
) ;
cast_for_split( cellcpy(&fs_shadow
, FS
) ) ;
if ( argv
[i
][2] != 0 ) bad_option(argv
[i
]) ;
/* first file goes in pfile_name ; any more go
if ( ! pfile_name
) pfile_name
= optarg
;
{ tail
= tail
->link
= ZMALLOC(PFILE
) ;
default : bad_option(argv
[i
]) ;
tail
->link
= (PFILE
*) 0 ;
pfile_list
= dummy
.link
;
set_ARGV(argc
, argv
, i
) ;
scan_init( (char *) 0 ) ;
else /* program on command line */
#if MSDOS && ! HAVE_REARGV /* prompt for program */
set_ARGV(argc
, argv
, i
) ;
scan_init( (char *) 0 ) ;
#else /* the real world */
{ errmsg(0, "no program") ; mawk_exit(1) ; }
set_ARGV(argc
, argv
, i
+1) ;
#if MSDOS && ! HAVE_REARGV /* reversed quotes */
for( p
= argv
[i
] ; *p
; p
++ )
if ( *p
== '\'' ) *p
= '\"' ;
static void set_ARGV(argc
, argv
, i
)
int i
; /* argv[i] = ARGV[1] */
st_p
= insert( "ARGV" ) ;
Argv
= st_p
->stval
.array
= new_ARRAY() ;
cp
= array_find( st_p
->stval
.array
, &argi
, CREATE
) ;
cp
->ptr
= (PTR
) new_STRING( progname
) ;
/* ARGV[0] is set, do the rest
The type of ARGV[1] ... should be C_MBSTRN
because the user might enter numbers from the command line */
for( argi
.dval
= 1.0 ; i
< argc
; i
++, argi
.dval
+= 1.0 )
cp
= array_find( st_p
->stval
.array
, &argi
, CREATE
) ;
cp
->ptr
= (PTR
) new_STRING( argv
[i
] ) ;
#if MSDOS && ! HAVE_REARGV
static void emit_prompt()
{ static char prompt
[] = "mawk> " ;
int fd
= open("CON", O_WRONLY
, 0) ;
(void) write(fd
, prompt
, strlen(prompt
)) ;
/*----- ENVIRON ----------*/
#ifndef MSDOS_MSC /* MSC declares it near */
register char **p
= environ
; /* walks environ */
char *s
; /* looks for the '=' */
CELL
*cp
; /* pts at ENV[&c] */
if ( s
= strchr(*p
, '=') ) /* shouldn't fail */
c
.ptr
= (PTR
) new_STRING(*p
) ;
cp
= array_find(ENV
, &c
, CREATE
) ;
cp
->ptr
= (PTR
) new_STRING(s
) ;
free_STRING(string(&c
)) ;