1 /* Environment-hacking for GNU Emacs subprocess
2 Copyright (C) 1986 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #ifdef MAINTAIN_ENVIRONMENT
27 you lose
-- this is un
*x
-only
30 /* alist of (name-string . value-string) */
31 Lisp_Object Venvironment_alist
;
32 extern char **environ
;
35 set_environment_alist (str
, val
)
36 register Lisp_Object str
, val
;
38 register Lisp_Object tem
;
40 tem
= Fassoc (str
, Venvironment_alist
);
45 Venvironment_alist
= Fcons (Fcons (str
, val
), Venvironment_alist
);
48 Venvironment_alist
= Fdelq (tem
, Venvironment_alist
);
50 XCONS (tem
)->cdr
= val
;
56 initialize_environment_alist ()
58 register unsigned char **e
, *s
;
59 extern char *index ();
61 for (e
= (unsigned char **) environ
; *e
; e
++)
63 s
= (unsigned char *) index (*e
, '=');
65 set_environment_alist (make_string (*e
, s
- *e
),
66 build_string (s
+ 1));
72 getenv_1 (str
, ephemeral
)
73 register unsigned char *str
;
74 int ephemeral
; /* if ephmeral, don't need to gc-proof */
76 register Lisp_Object env
;
77 int len
= strlen (str
);
79 for (env
= Venvironment_alist
; CONSP (env
); env
= XCONS (env
)->cdr
)
81 register Lisp_Object car
= XCONS (env
)->car
;
82 register Lisp_Object tem
= XCONS (car
)->car
;
84 if ((len
== XSTRING (tem
)->size
) &&
85 (!bcmp (str
, XSTRING (tem
)->data
, len
)))
87 /* Found it in the lisp environment */
88 tem
= XCONS (car
)->cdr
;
90 /* Caller promises that gc won't make him lose */
91 return XSTRING (tem
)->data
;
94 register unsigned char **e
;
96 int ll
= XSTRING (tem
)->size
;
98 /* Look for element in the original unix environment */
99 for (e
= (unsigned char **) environ
; *e
; e
++)
100 if (!bcmp (str
, *e
, len
) && *(*e
+ len
) == '=')
103 if (strlen (s
) >= ll
)
104 /* User hasn't either hasn't munged it or has set it
105 to something shorter -- we don't have to cons */
111 /* User has setenv'ed it to a diferent value, and our caller
112 isn't guaranteeing that he won't stash it away somewhere.
113 We can't just return a pointer to the lisp string, as that
114 will be corrupted when gc happens. So, we cons (in such
115 a way that it can't be freed -- though this isn't such a
116 problem since the only callers of getenv (as opposed to
117 those of egetenv) are very early, before the user -could-
118 have frobbed the environment. */
119 s
= (unsigned char *) xmalloc (ll
+ 1);
121 bcopy (XSTRING (tem
)->data
, s
, ll
+ 1);
126 return ((unsigned char *) 0);
129 /* unsigned -- stupid delcaration in lisp.h */ char *
131 register unsigned char *str
;
133 return ((char *) getenv_1 (str
, 0));
138 register unsigned char *str
;
140 return (getenv_1 (str
, 1));
144 #if (1 == 1) /* use caller-alloca versions, rather than callee-malloc */
146 size_of_current_environ ()
151 tem
= Flength (Venvironment_alist
);
153 size
= (XINT (tem
) + 1) * sizeof (unsigned char *);
154 /* + 1 for environment-terminating 0 */
156 for (tem
= Venvironment_alist
; !NULL (tem
); tem
= XCONS (tem
)->cdr
)
158 register Lisp_Object str
, val
;
160 str
= XCONS (XCONS (tem
)->car
)->car
;
161 val
= XCONS (XCONS (tem
)->car
)->cdr
;
163 size
+= (XSTRING (str
)->size
+
164 XSTRING (val
)->size
+
165 2); /* 1 for '=', 1 for '\000' */
171 get_current_environ (memory_block
)
172 unsigned char **memory_block
;
174 register unsigned char **e
, *s
;
176 register Lisp_Object tem
;
180 tem
= Flength (Venvironment_alist
);
182 s
= (unsigned char *) memory_block
183 + (XINT (tem
) + 1) * sizeof (unsigned char *);
185 for (tem
= Venvironment_alist
; !NULL (tem
); tem
= XCONS (tem
)->cdr
)
187 register Lisp_Object str
, val
;
189 str
= XCONS (XCONS (tem
)->car
)->car
;
190 val
= XCONS (XCONS (tem
)->car
)->cdr
;
193 len
= XSTRING (str
)->size
;
194 bcopy (XSTRING (str
)->data
, s
, len
);
197 len
= XSTRING (val
)->size
;
198 bcopy (XSTRING (val
)->data
, s
, len
);
206 /* dead code (this function mallocs, caller frees) superseded by above (which allows caller to use alloca) */
211 register unsigned char **e
, *s
;
212 register int len
, env_len
;
214 Lisp_Object str
, val
;
216 tem
= Flength (Venvironment_alist
);
218 env_len
= (XINT (tem
) + 1) * sizeof (char *);
219 /* + 1 for terminating 0 */
222 for (tem
= Venvironment_alist
; !NULL (tem
); tem
= XCONS (tem
)->cdr
)
224 str
= XCONS (XCONS (tem
)->car
)->car
;
225 val
= XCONS (XCONS (tem
)->car
)->cdr
;
227 len
+= (XSTRING (str
)->size
+
228 XSTRING (val
)->size
+
232 e
= env
= (unsigned char **) xmalloc (env_len
+ len
);
233 s
= (unsigned char *) env
+ env_len
;
235 for (tem
= Venvironment_alist
; !NULL (tem
); tem
= XCONS (tem
)->cdr
)
237 str
= XCONS (XCONS (tem
)->car
)->car
;
238 val
= XCONS (XCONS (tem
)->car
)->cdr
;
241 len
= XSTRING (str
)->size
;
242 bcopy (XSTRING (str
)->data
, s
, len
);
245 len
= XSTRING (val
)->size
;
246 bcopy (XSTRING (val
)->data
, s
, len
);
255 #endif /* dead code */
258 DEFUN ("getenv", Fgetenv
, Sgetenv
, 1, 2, "sEnvironment variable: \np",
259 "Return the value of environment variable VAR, as a string.\n\
260 When invoked interactively, print the value in the echo area.\n\
261 VAR is a string, the name of the variable,\n\
262 or the symbol t, meaning to return an alist representing the\n\
263 current environment.")
265 Lisp_Object str
, interactivep
;
269 if (str
== Qt
) /* If arg is t, return whole environment */
270 return (Fcopy_alist (Venvironment_alist
));
272 CHECK_STRING (str
, 0);
273 val
= Fcdr (Fassoc (str
, Venvironment_alist
));
274 if (!NULL (interactivep
))
277 message ("%s not defined in environment", XSTRING (str
)->data
);
279 message ("\"%s\"", XSTRING (val
)->data
);
284 DEFUN ("setenv", Fsetenv
, Ssetenv
, 1, 2,
285 "sEnvironment variable: \nsSet %s to value: ",
286 "Set the value of environment variable VAR to VALUE.\n\
287 Both args must be strings. Returns VALUE.")
294 CHECK_STRING (str
, 0);
296 CHECK_STRING (val
, 0);
298 set_environment_alist (str
, val
);
305 staticpro (&Venvironment_alist
);
312 Venvironment_alist
= Qnil
;
313 initialize_environment_alist ();
316 #endif /* MAINTAIN_ENVIRONMENT */