Skip to content

Commit 5cdf503

Browse files
authored
Move LOCAL_GETENV handling to var.c where the other env handling lives. (#190)
1 parent 6dd8552 commit 5cdf503

File tree

3 files changed

+63
-71
lines changed

3 files changed

+63
-71
lines changed

es.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ extern Tree *parse(char *esprompt1, char *esprompt2);
294294
extern Tree *parsestring(const char *str);
295295
extern Boolean isinteractive(void);
296296
extern Boolean isfromfd(void);
297-
extern void initgetenv(void);
298297
extern void initinput(void);
299298
extern void resetparser(void);
300299

input.c

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* input.c -- read input from files or strings ($Revision: 1.2 $) */
2-
/* stdgetenv is based on the FreeBSD getenv */
32

43
#include "es.h"
54
#include "input.h"
@@ -32,13 +31,6 @@ Boolean resetterminal = FALSE;
3231
#include <readline/readline.h>
3332
#endif
3433

35-
#if LOCAL_GETENV
36-
static char *stdgetenv(const char *);
37-
static char *esgetenv(const char *);
38-
static char *(*realgetenv)(const char *) = stdgetenv;
39-
#endif
40-
41-
4234

4335
/*
4436
* errors and warnings
@@ -174,65 +166,6 @@ static char *callreadline(char *prompt0) {
174166
}
175167
#endif
176168

177-
#if LOCAL_GETENV
178-
/* esgetenv -- fake version of getenv for readline (or other libraries) */
179-
static char *esgetenv(const char *name) {
180-
List *value = varlookup(name, NULL);
181-
if (value == NULL)
182-
return NULL;
183-
else {
184-
char *export;
185-
static Dict *envdict;
186-
static Boolean initialized = FALSE;
187-
Ref(char *, string, NULL);
188-
189-
gcdisable();
190-
if (!initialized) {
191-
initialized = TRUE;
192-
envdict = mkdict();
193-
globalroot(&envdict);
194-
}
195-
196-
string = dictget(envdict, name);
197-
if (string != NULL)
198-
efree(string);
199-
200-
export = str("%W", value);
201-
string = ealloc(strlen(export) + 1);
202-
strcpy(string, export);
203-
envdict = dictput(envdict, (char *) name, string);
204-
205-
gcenable();
206-
RefReturn(string);
207-
}
208-
}
209-
210-
static char *stdgetenv(const char *name) {
211-
extern char **environ;
212-
register int len;
213-
register const char *np;
214-
register char **p, *c;
215-
216-
if (name == NULL || environ == NULL)
217-
return (NULL);
218-
for (np = name; *np && *np != '='; ++np)
219-
continue;
220-
len = np - name;
221-
for (p = environ; (c = *p) != NULL; ++p)
222-
if (strncmp(c, name, len) == 0 && c[len] == '=') {
223-
return (c + len + 1);
224-
}
225-
return (NULL);
226-
}
227-
228-
char *getenv(const char *name) {
229-
return realgetenv(name);
230-
}
231-
232-
extern void initgetenv(void) {
233-
realgetenv = esgetenv;
234-
}
235-
#endif
236169

237170
/* fdfill -- fill input buffer by reading from a file descriptor */
238171
static int fdfill(Input *in) {

var.c

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* var.c -- es variables ($Revision: 1.1.1.1 $) */
2+
/* stdgetenv is based on the FreeBSD getenv */
23

34
#include "es.h"
45
#include "gc.h"
@@ -392,9 +393,6 @@ extern void initvars(void) {
392393
vars = mkdict();
393394
noexport = NULL;
394395
env = mkvector(ENVSIZE);
395-
#if LOCAL_GETENV
396-
initgetenv();
397-
#endif
398396
}
399397

400398
/* importvar -- import a single environment variable */
@@ -451,6 +449,64 @@ static void importvar(char *name0, char *value) {
451449
}
452450

453451
#if LOCAL_GETENV
452+
static char *stdgetenv(const char *);
453+
static char *esgetenv(const char *);
454+
static char *(*realgetenv)(const char *) = stdgetenv;
455+
456+
/* esgetenv -- fake version of getenv for readline (or other libraries) */
457+
static char *esgetenv(const char *name) {
458+
List *value = varlookup(name, NULL);
459+
if (value == NULL)
460+
return NULL;
461+
else {
462+
char *export;
463+
static Dict *envdict;
464+
static Boolean initialized = FALSE;
465+
Ref(char *, string, NULL);
466+
467+
gcdisable();
468+
if (!initialized) {
469+
initialized = TRUE;
470+
envdict = mkdict();
471+
globalroot(&envdict);
472+
}
473+
474+
string = dictget(envdict, name);
475+
if (string != NULL)
476+
efree(string);
477+
478+
export = str("%W", value);
479+
string = ealloc(strlen(export) + 1);
480+
strcpy(string, export);
481+
envdict = dictput(envdict, (char *) name, string);
482+
483+
gcenable();
484+
RefReturn(string);
485+
}
486+
}
487+
488+
static char *stdgetenv(const char *name) {
489+
extern char **environ;
490+
register int len;
491+
register const char *np;
492+
register char **p, *c;
493+
494+
if (name == NULL || environ == NULL)
495+
return (NULL);
496+
for (np = name; *np && *np != '='; ++np)
497+
continue;
498+
len = np - name;
499+
for (p = environ; (c = *p) != NULL; ++p)
500+
if (strncmp(c, name, len) == 0 && c[len] == '=') {
501+
return (c + len + 1);
502+
}
503+
return (NULL);
504+
}
505+
506+
char *getenv(const char *name) {
507+
return realgetenv(name);
508+
}
509+
454510
extern int setenv(const char *name, const char *value, int overwrite) {
455511
assert(vars != NULL);
456512
if (name == NULL || name[0] == '\0' || strchr(name, '=') != NULL) {
@@ -536,4 +592,8 @@ extern void initenv(char **envp, Boolean protected) {
536592
RefEnd2(var, imported);
537593
envmin = env->count;
538594
efree(buf);
595+
596+
#if LOCAL_GETENV
597+
realgetenv = esgetenv;
598+
#endif
539599
}

0 commit comments

Comments
 (0)