1 /* expand_path.c -- expand environmental variables in passed in string
3 * The main routine is expand_path(), it is the routine that handles
4 * the '~' character in four forms:
9 * and handles environment variables contained within the pathname
10 * which are defined by:
11 * ${var_name} (var_name is the name of the environ variable)
12 * $var_name (var_name ends w/ non-alphanumeric char other than '_')
16 #include <sys/types.h>
18 static char *expand_variable (const char *env, const char *file, int line);
22 List *variable_list = NULL;
24 static void variable_delproc (Node *);
27 variable_delproc (Node *node)
32 /* Currently used by -s option; we might want a way to set user
33 variables in a file in the $CVSROOT/CVSROOT directory too. */
36 variable_set (char *nameval)
43 while (isalnum ((unsigned char) *p) || *p == '_')
46 error ( 1, 0, "invalid character in user variable name in %s",
49 error (1, 0, "empty user variable name in %s", nameval);
50 name = xmalloc (p - nameval + 1);
51 strncpy (name, nameval, p - nameval);
52 name[p - nameval] = '\0';
53 /* Make p point to the value. */
55 if (strchr (p, '\012') != NULL)
56 error (1, 0, "linefeed in user variable value in %s", nameval);
58 if (variable_list == NULL)
59 variable_list = getlist ();
61 node = findnode (variable_list, name);
65 node->type = VARIABLE;
66 node->delproc = variable_delproc;
68 node->data = xstrdup (p);
69 (void) addnode (variable_list, node);
73 /* Replace the old value. For example, this means that -s
74 options on the command line override ones from .cvsrc. */
76 node->data = xstrdup (p);
83 /* This routine will expand the pathname to account for ~ and $
84 characters as described above. Returns a pointer to a newly
85 malloc'd string. If an error occurs, an error message is printed
86 via error() and NULL is returned. FILE and LINE are the filename
87 and linenumber to include in the error message. FILE must point
88 to something; LINE can be zero to indicate the line number is not
91 expand_path (const char *name, const char *file, int line, int formatsafe)
97 size_t mybuf_size = 0;
101 char inquotes = '\0';
105 /* Sorry this routine is so ugly; it is a head-on collision
106 between the `traditional' unix *d++ style and the need to
107 dynamically allocate. It would be much cleaner (and probably
108 faster, not that this is a bottleneck for CVS) with more use of
109 strcpy & friends, but I haven't taken the effort to rewrite it
112 /* First copy from NAME to MYBUF, expanding $<foo> as we go. */
114 expand_string (&mybuf, &mybuf_size, d + 1);
115 while ((mybuf[d++] = name[s]) != '\0')
119 /* The next character is a literal. Leave the \ in the string
120 * since it will be needed again when the string is split into
123 /* if we have a \ as the last character of the string, just leave
124 * it there - this is where we would set the escape flag to tell
125 * our parent we want another line if we cared.
129 expand_string (&mybuf, &mybuf_size, d + 1);
130 mybuf[d++] = name[s++];
133 /* skip $ variable processing for text inside single quotes */
134 else if (inquotes == '\'')
136 if (name[s++] == '\'')
141 else if (name[s] == '\'')
146 else if (name[s] == '"')
149 if (inquotes) inquotes = '\0';
152 else if (name[s++] == '$')
154 int flag = (name[s] == '{');
157 expand_string (&mybuf, &mybuf_size, d + 1);
158 for (; (mybuf[d++] = name[s]); s++)
162 : isalnum ((unsigned char) name[s]) == 0 && name[s] != '_')
164 expand_string (&mybuf, &mybuf_size, d + 1);
167 e = expand_variable (&mybuf[p+flag], file, line);
171 expand_string (&mybuf, &mybuf_size, d + 1);
172 for (d = p - 1; (mybuf[d++] = *e++); )
174 expand_string (&mybuf, &mybuf_size, d + 1);
175 if (mybuf[d-1] == '"')
177 /* escape the double quotes if we're between a matched
178 * pair of double quotes so that this sub will be
179 * passed inside as or as part of a single argument
180 * during the argument split later.
185 expand_string (&mybuf, &mybuf_size, d + 1);
189 else if (formatsafe && mybuf[d-1] == '%')
191 /* escape '%' to get past printf style format strings
192 * later (in make_cmdline).
194 expand_string (&mybuf, &mybuf_size, d + 1);
195 mybuf[d] = mybuf[d-1];
204 /* expand_variable has already printed an error message. */
207 expand_string (&mybuf, &mybuf_size, d + 1);
209 expand_string (&mybuf, &mybuf_size, d + 1);
212 /* Then copy from MYBUF to BUF, expanding ~. */
214 /* If you don't want ~username ~/ to be expanded simply remove
215 * This entire if statement including the else portion
220 while (mybuf[++s] != '/' && mybuf[s] != '\0')
222 expand_string (&buf, &buf_size, p + 1);
225 expand_string (&buf, &buf_size, p + 1);
232 #ifdef GETPWNAM_MISSING
235 "%s:%d:tilde expansion not supported on this system",
238 error (0, 0, "%s:tilde expansion not supported on this system",
243 ps = getpwnam (buf + d);
247 error (0, 0, "%s:%d: no such user %s",
248 file, line, buf + d);
250 error (0, 0, "%s: no such user %s", file, buf + d);
257 error (1, 0, "cannot find home directory");
260 expand_string (&buf, &buf_size, d + p);
261 memcpy(buf + d, e, p);
264 /* Kill up to here */
265 p = strlen(mybuf + s) + 1;
266 expand_string (&buf, &buf_size, d + p);
267 memcpy(buf + d, mybuf + s, p);
269 /* OK, buf contains the value we want to return. Clean up and return
272 /* Save a little memory with xstrdup; buf will tend to allocate
273 more than it needs to. */
274 result = xstrdup (buf);
289 expand_variable (const char *name, const char *file, int line)
291 if (strcmp (name, CVSROOT_ENV) == 0)
292 return current_parsed_root->directory;
293 else if (strcmp (name, "RCSBIN") == 0)
295 error (0, 0, "RCSBIN internal variable is no longer supported");
298 else if (strcmp (name, EDITOR1_ENV) == 0)
300 else if (strcmp (name, EDITOR2_ENV) == 0)
302 else if (strcmp (name, EDITOR3_ENV) == 0)
304 else if (strcmp (name, "USER") == 0)
306 else if (isalpha ((unsigned char) name[0]))
308 /* These names are reserved for future versions of CVS,
309 so that is why it is an error. */
311 error (0, 0, "%s:%d: no such internal variable $%s",
314 error (0, 0, "%s: no such internal variable $%s",
318 else if (name[0] == '=')
321 /* Crazy syntax for a user variable. But we want
322 *something* that lets the user name a user variable
323 anything he wants, without interference from
324 (existing or future) internal variables. */
325 node = findnode (variable_list, name + 1);
329 error (0, 0, "%s:%d: no such user variable ${%s}",
332 error (0, 0, "%s: no such user variable ${%s}",
340 /* It is an unrecognized character. We return an error to
341 reserve these for future versions of CVS; it is plausible
342 that various crazy syntaxes might be invented for inserting
343 information about revisions, branches, etc. */
345 error (0, 0, "%s:%d: unrecognized variable syntax %s",
348 error (0, 0, "%s: unrecognized variable syntax %s",