1 /* expand_path.c -- expand environmental variables in passed in string
3 * Copyright (C) 1995-2005 The Free Software Foundation, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * The main routine is expand_path(), it is the routine that handles
16 * the '~' character in four forms:
21 * and handles environment variables contained within the pathname
22 * which are defined by:
23 * ${var_name} (var_name is the name of the environ variable)
24 * $var_name (var_name ends w/ non-alphanumeric char other than '_')
28 #include <sys/types.h>
30 static char *expand_variable PROTO((const char *env, const char *file,
37 List *variable_list = NULL;
39 static void variable_delproc PROTO ((Node *));
42 variable_delproc (node)
48 /* Currently used by -s option; we might want a way to set user
49 variables in a file in the $CVSROOT/CVSROOT directory too. */
52 variable_set (nameval)
60 while (isalnum ((unsigned char) *p) || *p == '_')
63 error (1, 0, "illegal character in user variable name in %s", nameval);
65 error (1, 0, "empty user variable name in %s", nameval);
66 name = xmalloc (p - nameval + 1);
67 strncpy (name, nameval, p - nameval);
68 name[p - nameval] = '\0';
69 /* Make p point to the value. */
71 if (strchr (p, '\012') != NULL)
72 error (1, 0, "linefeed in user variable value in %s", nameval);
74 if (variable_list == NULL)
75 variable_list = getlist ();
77 node = findnode (variable_list, name);
81 node->type = VARIABLE;
82 node->delproc = variable_delproc;
84 node->data = xstrdup (p);
85 (void) addnode (variable_list, node);
89 /* Replace the old value. For example, this means that -s
90 options on the command line override ones from .cvsrc. */
92 node->data = xstrdup (p);
99 /* This routine will expand the pathname to account for ~ and $
100 characters as described above. Returns a pointer to a newly
101 malloc'd string. If an error occurs, an error message is printed
102 via error() and NULL is returned. FILE and LINE are the filename
103 and linenumber to include in the error message. FILE must point
104 to something; LINE can be zero to indicate the line number is not
107 expand_path (name, file, line)
116 size_t mybuf_size = 0;
122 /* Sorry this routine is so ugly; it is a head-on collision
123 between the `traditional' unix *d++ style and the need to
124 dynamically allocate. It would be much cleaner (and probably
125 faster, not that this is a bottleneck for CVS) with more use of
126 strcpy & friends, but I haven't taken the effort to rewrite it
129 /* First copy from NAME to MYBUF, expanding $<foo> as we go. */
131 while (name[s] != '\0')
136 if (name[++s] == '{')
138 while (name[++s] != '}' && name[s] != '\0')
140 expand_string (&mybuf, &mybuf_size, p + 1);
141 mybuf[p++] = name[s];
143 if (name[s] != '\0') ++s;
147 while (isalnum ((unsigned char) name[s]) || name[s] == '_')
149 expand_string (&mybuf, &mybuf_size, p + 1);
150 mybuf[p++] = name[s++];
153 expand_string (&mybuf, &mybuf_size, p + 1);
155 e = expand_variable (mybuf + d, file, line);
160 expand_string (&mybuf, &mybuf_size, d + p);
161 memcpy(mybuf + d, e, p);
165 /* expand_variable has already printed an error message. */
170 expand_string (&mybuf, &mybuf_size, d + 1);
171 mybuf[d++] = name[s++];
174 expand_string (&mybuf, &mybuf_size, d + 1);
177 /* Then copy from MYBUF to BUF, expanding ~. */
179 /* If you don't want ~username ~/ to be expanded simply remove
180 * This entire if statement including the else portion
185 while (mybuf[++s] != '/' && mybuf[s] != '\0')
187 expand_string (&buf, &buf_size, p + 1);
190 expand_string (&buf, &buf_size, p + 1);
197 #ifdef GETPWNAM_MISSING
200 "%s:%d:tilde expansion not supported on this system",
203 error (0, 0, "%s:tilde expansion not supported on this system",
208 ps = getpwnam (buf + d);
212 error (0, 0, "%s:%d: no such user %s",
213 file, line, buf + d);
215 error (0, 0, "%s: no such user %s", file, buf + d);
222 error (1, 0, "cannot find home directory");
225 expand_string (&buf, &buf_size, d + p);
226 memcpy(buf + d, e, p);
229 /* Kill up to here */
230 p = strlen(mybuf + s) + 1;
231 expand_string (&buf, &buf_size, d + p);
232 memcpy(buf + d, mybuf + s, p);
234 /* OK, buf contains the value we want to return. Clean up and return
237 /* Save a little memory with xstrdup; buf will tend to allocate
238 more than it needs to. */
239 result = xstrdup (buf);
252 expand_variable (name, file, line)
257 if (strcmp (name, CVSROOT_ENV) == 0)
258 return current_parsed_root->directory;
259 else if (strcmp (name, "RCSBIN") == 0)
261 error (0, 0, "RCSBIN internal variable is no longer supported");
264 else if (strcmp (name, EDITOR1_ENV) == 0)
266 else if (strcmp (name, EDITOR2_ENV) == 0)
268 else if (strcmp (name, EDITOR3_ENV) == 0)
270 else if (strcmp (name, "USER") == 0)
272 else if (isalpha ((unsigned char) name[0]))
274 /* These names are reserved for future versions of CVS,
275 so that is why it is an error. */
277 error (0, 0, "%s:%d: no such internal variable $%s",
280 error (0, 0, "%s: no such internal variable $%s",
284 else if (name[0] == '=')
287 /* Crazy syntax for a user variable. But we want
288 *something* that lets the user name a user variable
289 anything he wants, without interference from
290 (existing or future) internal variables. */
291 node = findnode (variable_list, name + 1);
295 error (0, 0, "%s:%d: no such user variable ${%s}",
298 error (0, 0, "%s: no such user variable ${%s}",
306 /* It is an unrecognized character. We return an error to
307 reserve these for future versions of CVS; it is plausible
308 that various crazy syntaxes might be invented for inserting
309 information about revisions, branches, etc. */
311 error (0, 0, "%s:%d: unrecognized variable syntax %s",
314 error (0, 0, "%s: unrecognized variable syntax %s",