1 |
|
2 |
#include <config.h>
|
3 |
|
4 |
#include <sys/types.h>
|
5 |
#include <assert.h>
|
6 |
#include <fcntl.h>
|
7 |
#include <stdio.h>
|
8 |
#include <stdlib.h>
|
9 |
#include <string.h>
|
10 |
#ifndef _MSC_VER
|
11 |
#include <unistd.h>
|
12 |
#endif /* _MSC_VER */
|
13 |
#include "wordexp.h"
|
14 |
#include "glob.h"
|
15 |
|
16 |
|
17 |
static int
|
18 |
is_valid_variable_char(char c, int pos)
|
19 |
{
|
20 |
if ( (pos && c >= '0' && c <= '9') ||
|
21 |
c == '_' ||
|
22 |
(c >= 'a' && c <= 'z') ||
|
23 |
(c >= 'A' && c <= 'Z'))
|
24 |
return 1;
|
25 |
return 0;
|
26 |
}
|
27 |
|
28 |
/*
|
29 |
* @brief replace all names of $NAME ${NAME}
|
30 |
* with the corresponding environment variable
|
31 |
* @ param in: the string to be checked
|
32 |
* @ return the expanded string or a copy of the existing string
|
33 |
* must be free() by the calling function
|
34 |
*/
|
35 |
static char *
|
36 |
expand_variables(const char *in)
|
37 |
{
|
38 |
char *var,*pos,*ret=g_strdup(in);
|
39 |
char *val,*str;
|
40 |
pos=ret;
|
41 |
while ((var=strchr(pos, '$'))) {
|
42 |
char *name,*begin=var+1;
|
43 |
int npos=0,bpos=0,slen,elen;
|
44 |
*var='\0';
|
45 |
if (var[1] == '{') {
|
46 |
begin++;
|
47 |
while (begin[npos]) {
|
48 |
bpos=npos;
|
49 |
if (begin[npos++] == '}')
|
50 |
break;
|
51 |
}
|
52 |
} else {
|
53 |
while (is_valid_variable_char(begin[npos],npos))
|
54 |
npos++;
|
55 |
bpos=npos;
|
56 |
}
|
57 |
name=g_strdup(begin);
|
58 |
name[bpos]='\0';
|
59 |
val=getenv(name);
|
60 |
free(name);
|
61 |
if (! val)
|
62 |
val="";
|
63 |
slen=strlen(ret)+strlen(val);
|
64 |
elen=strlen(begin+npos);
|
65 |
str=malloc(slen+elen+1);
|
66 |
strcpy(str,ret);
|
67 |
strcat(str,val);
|
68 |
strcat(str,begin+npos);
|
69 |
free(ret);
|
70 |
ret=str;
|
71 |
pos=ret+slen;
|
72 |
}
|
73 |
return ret;
|
74 |
}
|
75 |
|
76 |
/*
|
77 |
* @brief minimal realization of wordexp according to IEEE standard
|
78 |
* shall perform word expansion as described in the Shell
|
79 |
* expansion of ´$NAME´ or ´${NAME}´
|
80 |
* expansion of ´*´ and ´?´
|
81 |
* @param words: pointer to a string containing one or more words to be expanded
|
82 |
* but here only one word supported
|
83 |
*/
|
84 |
int
|
85 |
wordexp(const char *words, wordexp_t *we, int flags)
|
86 |
{
|
87 |
int i;
|
88 |
int error = 0;
|
89 |
char *words_expanded;
|
90 |
#ifdef HAVE_API_WIN32_BASE
|
91 |
glob_t pglob;
|
92 |
#endif
|
93 |
|
94 |
assert(we != NULL);
|
95 |
assert(words != NULL);
|
96 |
|
97 |
/* expansion of ´$NAME´ or ´${NAME}´ */
|
98 |
words_expanded=expand_variables(words);
|
99 |
#ifdef HAVE_API_WIN32_BASE
|
100 |
/* expansion of ´*´, ´?´ */
|
101 |
error=glob(words_expanded, 0, NULL, &pglob);
|
102 |
if (!error)
|
103 |
{
|
104 |
/* copy the content of struct of glob into struct of wordexp */
|
105 |
we->we_wordc = pglob.gl_pathc;
|
106 |
we->we_offs = pglob.gl_offs;
|
107 |
we->we_wordv = malloc(we->we_wordc * sizeof(char*));
|
108 |
for (i=0; i<we->we_wordc; i++)
|
109 |
{
|
110 |
we->we_wordv[i] = g_strdup(pglob.gl_pathv[i]);
|
111 |
}
|
112 |
globfree(&pglob);
|
113 |
free(words_expanded);
|
114 |
}
|
115 |
else
|
116 |
{
|
117 |
#endif
|
118 |
we->we_wordc = 1;
|
119 |
we->we_wordv = malloc(sizeof(char*));
|
120 |
we->we_wordv[0] = words_expanded;
|
121 |
#ifdef HAVE_API_WIN32_BASE
|
122 |
}
|
123 |
#endif
|
124 |
|
125 |
|
126 |
return error;
|
127 |
}
|
128 |
|
129 |
|
130 |
void wordfree(wordexp_t *we)
|
131 |
{
|
132 |
int i;
|
133 |
|
134 |
for (i=0; i < we->we_wordc; i++)
|
135 |
{
|
136 |
free (we->we_wordv[i]);
|
137 |
}
|
138 |
|
139 |
free (we->we_wordv);
|
140 |
we->we_wordc = 0;
|
141 |
}
|