1 /* expand_path.c -- expand environmental variables in passed in string
2  *
3  * Copyright (C) 1995-2005 The Free Software Foundation, Inc.
4  *
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)
8  * any later version.
9  *
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.
14  *
15  * The main routine is expand_path(), it is the routine that handles
16  * the '~' character in four forms:
17  *     ~name
18  *     ~name/
19  *     ~/
20  *     ~
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 '_')
25  */
26 
27 #include "cvs.h"
28 #include <sys/types.h>
29 
30 static char *expand_variable PROTO((const char *env, const char *file,
31                                     int line));
32 
33 
34 
35 /* User variables.  */
36 
37 List *variable_list = NULL;
38 
39 static void variable_delproc PROTO ((Node *));
40 
41 static void
variable_delproc(node)42 variable_delproc (node)
43     Node *node;
44 {
45     free (node->data);
46 }
47 
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.  */
50 
51 void
variable_set(nameval)52 variable_set (nameval)
53     char *nameval;
54 {
55     char *p;
56     char *name;
57     Node *node;
58 
59     p = nameval;
60     while (isalnum ((unsigned char) *p) || *p == '_')
61 	++p;
62     if (*p != '=')
63 	error (1, 0, "illegal character in user variable name in %s", nameval);
64     if (p == 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.  */
70     ++p;
71     if (strchr (p, '\012') != NULL)
72 	error (1, 0, "linefeed in user variable value in %s", nameval);
73 
74     if (variable_list == NULL)
75 	variable_list = getlist ();
76 
77     node = findnode (variable_list, name);
78     if (node == NULL)
79     {
80 	node = getnode ();
81 	node->type = VARIABLE;
82 	node->delproc = variable_delproc;
83 	node->key = name;
84 	node->data = xstrdup (p);
85 	(void) addnode (variable_list, node);
86     }
87     else
88     {
89 	/* Replace the old value.  For example, this means that -s
90 	   options on the command line override ones from .cvsrc.  */
91 	free (node->data);
92 	node->data = xstrdup (p);
93 	free (name);
94     }
95 }
96 
97 
98 
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
105    known.  */
106 char *
expand_path(name,file,line)107 expand_path (name, file, line)
108     const char *name;
109     const char *file;
110     int line;
111 {
112     size_t s, d, p;
113     char *e;
114 
115     char *mybuf = NULL;
116     size_t mybuf_size = 0;
117     char *buf = NULL;
118     size_t buf_size = 0;
119 
120     char *result;
121 
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
127        thusly.  */
128 
129     /* First copy from NAME to MYBUF, expanding $<foo> as we go.  */
130     s = d = 0;
131     while (name[s] != '\0')
132     {
133 	if (name[s] == '$')
134 	{
135 	    p = d;
136 	    if (name[++s] == '{')
137 	    {
138 		while (name[++s] != '}' && name[s] != '\0')
139 		{
140 		    expand_string (&mybuf, &mybuf_size, p + 1);
141 		    mybuf[p++] = name[s];
142 		}
143 		if (name[s] != '\0') ++s;
144 	    }
145 	    else
146 	    {
147 		while (isalnum ((unsigned char) name[s]) || name[s] == '_')
148 		{
149 		    expand_string (&mybuf, &mybuf_size, p + 1);
150 		    mybuf[p++] = name[s++];
151 		}
152 	    }
153 	    expand_string (&mybuf, &mybuf_size, p + 1);
154 	    mybuf[p] = '\0';
155 	    e = expand_variable (mybuf + d, file, line);
156 
157 	    if (e)
158 	    {
159 		p = strlen(e);
160 		expand_string (&mybuf, &mybuf_size, d + p);
161 		memcpy(mybuf + d, e, p);
162 		d += p;
163 	    }
164 	    else
165 		/* expand_variable has already printed an error message.  */
166 		goto error_exit;
167 	}
168 	else
169 	{
170 	    expand_string (&mybuf, &mybuf_size, d + 1);
171 	    mybuf[d++] = name[s++];
172 	}
173     }
174     expand_string (&mybuf, &mybuf_size, d + 1);
175     mybuf[d++] = '\0';
176 
177     /* Then copy from MYBUF to BUF, expanding ~.  */
178     s = d = 0;
179     /* If you don't want ~username ~/ to be expanded simply remove
180      * This entire if statement including the else portion
181      */
182     if (mybuf[s] == '~')
183     {
184 	p = d;
185 	while (mybuf[++s] != '/' && mybuf[s] != '\0')
186 	{
187 	    expand_string (&buf, &buf_size, p + 1);
188 	    buf[p++] = name[s];
189 	}
190 	expand_string (&buf, &buf_size, p + 1);
191 	buf[p] = '\0';
192 
193 	if (p == d)
194 	    e = get_homedir ();
195 	else
196 	{
197 #ifdef GETPWNAM_MISSING
198 	    if (line != 0)
199 		error (0, 0,
200 		       "%s:%d:tilde expansion not supported on this system",
201 		       file, line);
202 	    else
203 		error (0, 0, "%s:tilde expansion not supported on this system",
204 		       file);
205 	    goto error_exit;
206 #else
207 	    struct passwd *ps;
208 	    ps = getpwnam (buf + d);
209 	    if (ps == NULL)
210 	    {
211 		if (line != 0)
212 		    error (0, 0, "%s:%d: no such user %s",
213 			   file, line, buf + d);
214 		else
215 		    error (0, 0, "%s: no such user %s", file, buf + d);
216 		goto error_exit;
217 	    }
218 	    e = ps->pw_dir;
219 #endif
220 	}
221 	if (e == NULL)
222 	    error (1, 0, "cannot find home directory");
223 
224 	p = strlen(e);
225 	expand_string (&buf, &buf_size, d + p);
226 	memcpy(buf + d, e, p);
227 	d += p;
228     }
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);
233 
234     /* OK, buf contains the value we want to return.  Clean up and return
235        it.  */
236     free (mybuf);
237     /* Save a little memory with xstrdup; buf will tend to allocate
238        more than it needs to.  */
239     result = xstrdup (buf);
240     free (buf);
241     return result;
242 
243  error_exit:
244     if (mybuf != NULL)
245 	free (mybuf);
246     if (buf != NULL)
247 	free (buf);
248     return NULL;
249 }
250 
251 static char *
expand_variable(name,file,line)252 expand_variable (name, file, line)
253     const char *name;
254     const char *file;
255     int line;
256 {
257     if (strcmp (name, CVSROOT_ENV) == 0)
258 	return current_parsed_root->directory;
259     else if (strcmp (name, "RCSBIN") == 0)
260     {
261 	error (0, 0, "RCSBIN internal variable is no longer supported");
262 	return NULL;
263     }
264     else if (strcmp (name, EDITOR1_ENV) == 0)
265 	return Editor;
266     else if (strcmp (name, EDITOR2_ENV) == 0)
267 	return Editor;
268     else if (strcmp (name, EDITOR3_ENV) == 0)
269 	return Editor;
270     else if (strcmp (name, "USER") == 0)
271 	return getcaller ();
272     else if (isalpha ((unsigned char) name[0]))
273     {
274 	/* These names are reserved for future versions of CVS,
275 	   so that is why it is an error.  */
276 	if (line != 0)
277 	    error (0, 0, "%s:%d: no such internal variable $%s",
278 		   file, line, name);
279 	else
280 	    error (0, 0, "%s: no such internal variable $%s",
281 		   file, name);
282 	return NULL;
283     }
284     else if (name[0] == '=')
285     {
286 	Node *node;
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);
292 	if (node == NULL)
293 	{
294 	    if (line != 0)
295 		error (0, 0, "%s:%d: no such user variable ${%s}",
296 		       file, line, name);
297 	    else
298 		error (0, 0, "%s: no such user variable ${%s}",
299 		       file, name);
300 	    return NULL;
301 	}
302 	return node->data;
303     }
304     else
305     {
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.  */
310 	if (line != 0)
311 	    error (0, 0, "%s:%d: unrecognized variable syntax %s",
312 		   file, line, name);
313 	else
314 	    error (0, 0, "%s: unrecognized variable syntax %s",
315 		   file, name);
316 	return NULL;
317     }
318 }
319