diff options
author | Xavier Leroy <xavier.leroy@inria.fr> | 1996-09-06 16:51:56 +0000 |
---|---|---|
committer | Xavier Leroy <xavier.leroy@inria.fr> | 1996-09-06 16:51:56 +0000 |
commit | 4426de9a130b4abef0f417b3a396a3aed70528c2 (patch) | |
tree | 39a9744b5d9d97255a93b265ca65b5e279f22ea9 | |
parent | 44b82ae723b6a824cfee75e452b2ac55e6940bac (diff) |
Ajout expansion des arguments de la ligne de commande
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@962 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
-rw-r--r-- | byterun/Makefile.nt | 3 | ||||
-rw-r--r-- | byterun/main.c | 7 | ||||
-rw-r--r-- | byterun/wincmdline.c | 127 |
3 files changed, 136 insertions, 1 deletions
diff --git a/byterun/Makefile.nt b/byterun/Makefile.nt index 33037300c..11a97b65c 100644 --- a/byterun/Makefile.nt +++ b/byterun/Makefile.nt @@ -7,7 +7,8 @@ OBJS=interp.obj misc.obj stacks.obj fix_code.obj startup.obj main.obj \ fail.obj signals.obj freelist.obj major_gc.obj minor_gc.obj \ memory.obj alloc.obj roots.obj compare.obj ints.obj floats.obj \ str.obj array.obj io.obj extern.obj intern.obj hash.obj sys.obj \ - meta.obj parsing.obj gc_ctrl.obj terminfo.obj md5.obj obj.obj lexing.obj + meta.obj parsing.obj gc_ctrl.obj terminfo.obj md5.obj obj.obj lexing.obj \ + wincmdline.obj PRIMS=array.c compare.c extern.c floats.c gc_ctrl.c hash.c \ intern.c interp.c ints.c io.c lexing.c md5.c meta.c obj.c parsing.c \ diff --git a/byterun/main.c b/byterun/main.c index 6538049d4..e0e8ade64 100644 --- a/byterun/main.c +++ b/byterun/main.c @@ -18,9 +18,16 @@ extern int caml_main P((int, char **)); +#ifdef _WIN32 +extern void expand_command_line P((int *, char ***)); +#endif + int main(argc, argv) int argc; char ** argv; { +#ifdef _WIN32 + expand_command_line(&argc, &argv); +#endif return caml_main(argc, argv); } diff --git a/byterun/wincmdline.c b/byterun/wincmdline.c new file mode 100644 index 000000000..a125fb7fa --- /dev/null +++ b/byterun/wincmdline.c @@ -0,0 +1,127 @@ +/***********************************************************************/ +/* */ +/* Objective Caml */ +/* */ +/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */ +/* */ +/* Copyright 1996 Institut National de Recherche en Informatique et */ +/* Automatique. Distributed only by permission. */ +/* */ +/***********************************************************************/ + +/* $Id$ */ + +/* Expansion of @responsefile and *? file patterns in the command line */ + +#include <stdlib.h> +#include <stdio.h> +#include <io.h> +#include <fcntl.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <ctype.h> +#include <string.h> + +static int argc; +static char ** argv; +static int argvsize; + +static void store_argument(), expand_argument(); +static void expand_pattern(), expand_diversion(); + +static void out_of_memory() +{ + fprintf(stderr, "Out of memory while expanding command line\n"); + exit(2); +} + +static void store_argument(arg) + char * arg; +{ + if (argc + 1 >= argvsize) { + argvsize *= 2; + argv = (char **) realloc(argv, argvsize * sizeof(char *)); + if (argv == NULL) out_of_memory; + } + argv[argc++] = arg; +} + +static void expand_argument(arg) + char * arg; +{ + char * p; + + if (arg[0] == '@') { + expand_diversion(arg + 1); + return; + } + for (p = arg; *p != 0; p++) { + if (*p == '*' || *p == '?') { + expand_pattern(arg); + return; + } + } + store_argument(arg); +} + +static void expand_pattern(pat) + char * pat; +{ + int handle; + struct _finddata_t ffblk; + + handle = _findfirst(pat, &ffblk); + if (handle == -1) { + store_argument(pat); /* a la Bourne shell */ + return; + } + do { + store_argument(strdup(ffblk.name)); + } while (_findnext(handle, &ffblk) != -1); + _findclose(handle); +} + +static void expand_diversion(filename) + char * filename; +{ + struct _stat stat; + int fd; + char * buf, * endbuf, * p, * s; + + if (_stat(filename, &stat) == -1 || + (fd = _open(filename, O_RDONLY | O_BINARY, 0)) == -1) { + fprintf(stderr, "Cannot open file %s\n", filename); + exit(2); + } + buf = (char *) malloc(stat.st_size + 1); + if (buf == NULL) out_of_memory(); + _read(fd, buf, stat.st_size); + endbuf = buf + stat.st_size; + _close(fd); + for (p = buf; p < endbuf; /*nothing*/) { + /* Skip leading blanks */ + while (p < endbuf && isspace(*p)) p++; + if (p >= endbuf) break; + s = p; + /* Skip to next blank or end of buffer */ + while (p < endbuf && !isspace(*p)) p++; + /* Delimit argument and expand it */ + *p++ = 0; + expand_argument(s); + } +} + +void expand_command_line(argcp, argvp) + int * argcp; + char *** argvp; +{ + int i; + argc = 0; + argvsize = 16; + argv = (char **) malloc(argvsize * sizeof(char *)); + if (argv == NULL) out_of_memory(); + for (i = 0; i < *argcp; i++) expand_argument((*argvp)[i]); + argv[argc] = NULL; + *argcp = argc; + *argvp = argv; +} |