]>
git.saurik.com Git - wxWidgets.git/blob - utils/Install/packzip/envargs.c
1 /*----------------------------------------------------------------*
2 | envargs - add default options from environment to command line
3 |----------------------------------------------------------------
4 | Author: Bill Davidsen, original 10/13/91, revised 23 Oct 1991.
5 | This program is in the public domain.
6 |----------------------------------------------------------------
8 | 1. Yes, the indirection is a tad complex
9 | 2. Parentheses were added where not needed in some cases
10 | to make the action of the code less obscure.
11 |----------------------------------------------------------------
12 | UnZip notes: 24 May 92 ("v1.4"):
13 | 1. #include "unzip.h" for prototypes (24 May 92)
14 | 2. changed ch to type char (24 May 92)
15 | 3. added an ifdef to avoid Borland warnings (24 May 92)
16 | 4. included Rich Wales' mksargs() routine (for MS-DOS, maybe
17 | OS/2? NT?) (4 Dec 93)
18 | 5. added alternate-variable string envstr2 (21 Apr 94)
19 | 6. added support for quoted arguments (6 Jul 96)
20 *----------------------------------------------------------------*/
24 #define UNZIP_INTERNAL
27 #ifdef __EMX__ /* emx isspace() returns TRUE on extended ASCII !! */
28 # define ISspace(c) ((c) & 0x80 ? 0 : isspace((unsigned)c))
30 # define ISspace(c) isspace((unsigned)c)
33 static int count_args
OF((ZCONST
char *));
34 static void mem_err
OF((__GPRO
));
36 static ZCONST
char Far NoMemArguments
[] =
37 "envargs: cannot get memory for arguments";
40 void envargs(__G__ Pargc
, Pargv
, envstr
, envstr2
)
44 ZCONST
char *envstr
, *envstr2
;
49 char *envptr
; /* value returned by getenv */
50 char *bufptr
; /* copy of env info */
51 int argc
= 0; /* internal arg count */
52 register int ch
; /* spare temp value */
53 char **argv
; /* internal arg vector */
54 char **argvect
; /* copy of vector address */
56 /* see if anything in the environment */
57 if ((envptr
= getenv(envstr
)) != (char *)NULL
) /* usual var */
58 while (ISspace(*envptr
)) /* must discard leading spaces */
60 if (envptr
== (char *)NULL
|| *envptr
== '\0')
61 if ((envptr
= getenv(envstr2
)) != (char *)NULL
) /* alternate var */
62 while (ISspace(*envptr
))
64 if (envptr
== (char *)NULL
|| *envptr
== '\0')
67 bufptr
= malloc(1 + strlen(envptr
));
68 if (bufptr
== (char *)NULL
)
70 #if (defined(WIN32) || defined(WINDLL))
73 /* SPC: don't know codepage of 'real' WinNT console */
74 strcpy(bufptr
, envptr
);
76 /* Win95 environment is DOS and uses OEM character coding */
77 OEM_TO_INTERN(envptr
, bufptr
);
80 /* DOS environment uses OEM codepage */
81 OEM_TO_INTERN(envptr
, bufptr
);
83 #else /* !(WIN32 || WINDLL) */
84 strcpy(bufptr
, envptr
);
85 #endif /* ?(WIN32 || WINDLL) */
87 /* count the args so we can allocate room for them */
88 argc
= count_args(bufptr
);
89 /* allocate a vector large enough for all args */
90 argv
= (char **)malloc((argc
+ *Pargc
+ 1) * sizeof(char *));
91 if (argv
== (char **)NULL
) {
97 /* copy the program name first, that's always true */
98 *(argv
++) = *((*Pargv
)++);
100 /* copy the environment args next, may be changed */
102 #if defined(AMIGA) || defined(UNIX)
103 if (*bufptr
== '"') {
104 char *argstart
= ++bufptr
;
106 *(argv
++) = argstart
;
107 for (ch
= *bufptr
; ch
!= '\0' && ch
!= '\"'; ch
= *(++bufptr
))
108 if (ch
== '\\' && bufptr
[1] != '\0')
109 ++bufptr
; /* skip char after backslash */
111 *(bufptr
++) = '\0'; /* overwrite trailing " */
113 /* remove escape characters */
114 while ((argstart
= strchr(argstart
, '\\')) != (char *)NULL
) {
115 strcpy(argstart
, argstart
+ 1);
121 while ((ch
= *bufptr
) != '\0' && !ISspace(ch
))
127 #ifdef DOS_FLX_OS2_W32
128 /* we do not support backslash-quoting of quotes in quoted
129 * strings under DOS_OS2_W32, because backslashes are directory
130 * separators and double quotes are illegal in filenames */
131 if (*bufptr
== '"') {
132 *(argv
++) = ++bufptr
;
133 while ((ch
= *bufptr
) != '\0' && ch
!= '\"')
139 while ((ch
= *bufptr
) != '\0' && !ISspace(ch
))
146 while ((ch
= *bufptr
) != '\0' && !ISspace(ch
))
150 #endif /* ?DOS_FLX_OS2_W32 */
151 #endif /* ?(AMIGA || UNIX) */
152 while ((ch
= *bufptr
) != '\0' && ISspace(ch
))
156 /* now save old argc and copy in the old args */
159 *(argv
++) = *((*Pargv
)++);
161 /* finally, add a NULL after the last arg, like Unix */
162 *argv
= (char *)NULL
;
164 /* save the values and return */
171 static int count_args(s
)
178 /* count and skip args */
180 #if defined(AMIGA) || defined(UNIX)
182 for (ch
= *(++s
); ch
!= '\0' && ch
!= '\"'; ch
= *(++s
))
183 if (ch
== '\\' && s
[1] != '\0')
186 ++s
; /* trailing quote */
189 #ifdef DOS_FLX_OS2_W32
191 ++s
; /* leading quote */
192 while ((ch
= *s
) != '\0' && ch
!= '\"')
195 ++s
; /* trailing quote */
197 #endif /* DOS_FLX_OS2_W32 */
198 #endif /* ?(AMIGA || UNIX) */
199 while ((ch
= *s
) != '\0' && !ISspace(ch
)) /* note else-clauses above */
201 while ((ch
= *s
) != '\0' && ISspace(ch
))
210 static void mem_err(__G
)
213 perror(LoadFarString(NoMemArguments
));
228 pipeit("Orig argv: %p\n", argv
);
229 dump_args(argc
, argv
);
230 envargs(__G__
&argc
, &argv
, "ENVTEST");
231 pipeit(" New argv: %p\n", argv
);
232 dump_args(argc
, argv
);
237 dump_args(argc
, argv
)
243 pipeit("\nDump %d args:\n", argc
);
244 for (i
= 0; i
< argc
; ++i
)
245 pipeit("%3d %s\n", i
, argv
[i
]);
252 #ifdef MSDOS /* DOS_OS2? DOS_OS2_W32? */
255 * void mksargs(int *argcp, char ***argvp)
257 * Substitutes the extended command line argument list produced by
258 * the MKS Korn Shell in place of the command line info from DOS.
260 * The MKS shell gets around DOS's 128-byte limit on the length of
261 * a command line by passing the "real" command line in the envi-
262 * ronment. The "real" arguments are flagged by prepending a tilde
265 * This "mksargs" routine creates a new argument list by scanning
266 * the environment from the beginning, looking for strings begin-
267 * ning with a tilde character. The new list replaces the original
268 * "argv" (pointed to by "argvp"), and the number of arguments
269 * in the new list replaces the original "argc" (pointed to by
274 void mksargs(argcp
, argvp
)
278 #ifndef MSC /* declared differently in MSC 7.0 headers, at least */
280 extern char **environ
; /* environment */
283 char **envp
; /* pointer into environment */
284 char **newargv
; /* new argument list */
285 char **argp
; /* pointer into new arg list */
286 int newargc
; /* new argument count */
289 if (environ
== NULL
|| argcp
== NULL
|| argvp
== NULL
|| *argvp
== NULL
)
292 /* find out how many environment arguments there are */
293 for (envp
= environ
, newargc
= 0; *envp
!= NULL
&& (*envp
)[0] == '~';
297 return; /* no environment arguments */
299 /* set up new argument list */
300 newargv
= (char **) malloc(sizeof(char **) * (newargc
+1));
302 return; /* malloc failed */
304 for (argp
= newargv
, envp
= environ
; *envp
!= NULL
&& (*envp
)[0] == '~';
305 *argp
++ = &(*envp
++)[1])
307 *argp
= NULL
; /* null-terminate the list */
309 /* substitute new argument list in place of old one */