1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: expandarg.c,v 2.7 2003/06/27 06:53:21 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Get additional command arguments from file or environment. |
6 |
* |
7 |
* External symbols declared in rtio.h |
8 |
*/ |
9 |
|
10 |
#include "copyright.h" |
11 |
|
12 |
#include <errno.h> |
13 |
#include <string.h> |
14 |
|
15 |
#include "rtio.h" |
16 |
#include "rtmisc.h" |
17 |
|
18 |
|
19 |
#define MAXARGEXP 512 /* maximum argument expansion */ |
20 |
|
21 |
/* set the following to suit, -1 to disable */ |
22 |
int envexpchr = '$'; /* environment expansion character */ |
23 |
int filexpchr = '@'; /* file expansion character */ |
24 |
|
25 |
|
26 |
int |
27 |
expandarg(acp, avp, n) /* expand list at argument n */ |
28 |
int *acp; |
29 |
register char ***avp; |
30 |
int n; |
31 |
{ |
32 |
int ace; |
33 |
char *ave[MAXARGEXP]; |
34 |
char **newav; |
35 |
/* check argument */ |
36 |
if (n >= *acp) |
37 |
return(0); |
38 |
errno = 0; |
39 |
if ((*avp)[n][0] == filexpchr) { /* file name */ |
40 |
ace = wordfile(ave, (*avp)[n]+1); |
41 |
if (ace < 0) |
42 |
return(-1); /* no such file */ |
43 |
} else if ((*avp)[n][0] == envexpchr) { /* env. variable */ |
44 |
ace = wordstring(ave, getenv((*avp)[n]+1)); |
45 |
if (ace < 0) |
46 |
return(-1); /* no such variable */ |
47 |
} else /* regular argument */ |
48 |
return(0); |
49 |
/* allocate new pointer list */ |
50 |
newav = (char **)bmalloc((*acp+ace)*sizeof(char *)); |
51 |
if (newav == NULL) |
52 |
return(-1); |
53 |
/* copy preceeding arguments */ |
54 |
memcpy((void *)newav, (void *)*avp, n*sizeof(char *)); |
55 |
/* copy expanded argument */ |
56 |
memcpy((void *)(newav+n), (void *)ave, ace*sizeof(char *)); |
57 |
/* copy trailing arguments + NULL */ |
58 |
memcpy((void *)(newav+n+ace), (void *)(*avp+n+1), (*acp-n)*sizeof(char *)); |
59 |
/* free old list */ |
60 |
bfree((char *)*avp, (*acp+1)*sizeof(char *)); |
61 |
/* assign new list */ |
62 |
*acp += ace-1; |
63 |
*avp = newav; |
64 |
return(1); /* return success */ |
65 |
} |