scsh-0.6/scsh/process_args.c

144 lines
3.9 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#define streq(a,b) (strcmp((a),(b))==0)
static void usage(void) {
printf("
Usage: scsh [meta-arg] [vm-option+] [end-option scheme-args]
meta-arg: \\ <script-file-name>
switch: -e <entry-point> Specify top-level entry point.
-o <structure> Open structure in current package.
-m <package> Switch to package.
-n <new-package> Switch to new package.
-lm <module-file-name> Load module into config package.
-le <exec-file-name> Load file into exec package.
-l <file-name> Load file into current package.
-ll <module-file-name> As in -lm, but search the library path list.
+lp <dir> Add <dir> to front of library path list.
lp+ <dir> Add <dir> to end of library path list.
+lpe <dir> +lp, with env var and ~user expansion.
lpe+ <dir> lp+, with env var and ~user expansion.
+lpsd Add script-file's dir to front of path list.
lpsd+ Add script-file's dir to end of path list.
-lp-clear Clear library path list to ().
-lp-default Reset library path list to system default.
-ds Do script.
-dm Do script module.
-de Do script exec.
-h <cells> Set heap size.
-i <file> Specify image.
-stacksize <cells> Set stack size
end-option: -s <script> Specify script.
-sfd <num> Script is on file descriptor <num>.
-c <exp> Evaluate expression.
-- Interactive session.
");
}
static void bad_args(char* msg, char* arg) {
printf ("Error:\n");
printf (msg,arg);
printf ("\n");
usage();
exit(1);
}
void process_args(char **argv,
long *pheap_size,
long *pstack_size,
char **pobject_file,
char **pimage_name) {
extern char **process_meta_arg(char **);
/* Handle an initial \ <fname> meta-arg expansion. */
while ( *argv && streq(*argv, "\\") ) {
argv++;
if( !*argv ) bad_args("No arguments after meta-arg %s","\\"); /* die */
argv = process_meta_arg(argv);
if( !argv ) {
fprintf(stderr, "scsh: \\ <fname> expansion failed.\n");
exit(1);}}
for (; *argv; argv++){
char *arg = argv[0];
#define S48_ARGCMP(s) (streq(arg,s))
if (S48_ARGCMP ("-h")) { /* heapsize */
argv++;
if( !*argv ) bad_args("Option %s requires an argument", arg); /* die */
*pheap_size = atoi(*argv);
if( *pheap_size <= 0 ) bad_args("Invalid argument to %s", arg);
}
else if (S48_ARGCMP ("-stacksize")) { /* stacksize */
argv++;
if( !*argv ) bad_args("Option %s requires an argument", arg); /* die */
*pstack_size = atoi(*argv);
if( *pstack_size <= 0 ) bad_args("Invalid argument to %s", arg);
}
else if (S48_ARGCMP ("-i")) { /* image */
argv++;
if( !*argv ) bad_args("Option %s requires an argument", arg); /* die */
*pimage_name = *argv;
}
else if (S48_ARGCMP ("+lpsd") ||
S48_ARGCMP ("lpsd+") ||
S48_ARGCMP ("-lp-clear") ||
S48_ARGCMP ("-lp-default") ||
S48_ARGCMP ("-ds") ||
S48_ARGCMP ("-dm") ||
S48_ARGCMP ("-de")){
;
}
else if (S48_ARGCMP ("-e") ||
S48_ARGCMP ("-o") ||
S48_ARGCMP ("-m") ||
S48_ARGCMP ("-lm") ||
S48_ARGCMP ("-le") ||
S48_ARGCMP ("-l") ||
S48_ARGCMP ("-ll") ||
S48_ARGCMP ("+lp") ||
S48_ARGCMP ("lp+") ||
S48_ARGCMP ("+lpe") ||
S48_ARGCMP ("lpe+")){
argv++;
if( !*argv ) bad_args("Option %s requires an argument", arg);
}
/* These switches terminate arg scanning. */
else if (S48_ARGCMP ("-s") ||
S48_ARGCMP ("-sdf") ||
S48_ARGCMP ("-c")){
argv++;
if( !*argv ){
bad_args("Option %s requires an argument", arg);
}
return;
}
else if (S48_ARGCMP ("--")) {
return;
}
else{
bad_args("Unknown switch %s", arg);
}
}
#undef S48_ARGCMP
return;
}