Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libjcl/sym.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2003-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#pragma prototyped
21
22
/*
23
* jcl symbol support
24
*/
25
26
#include "jcllib.h"
27
28
/*
29
* add symbol name=value to the current step
30
* value==0 => name==name=value, got it?
31
* no diagnostic if value==0 and name!=name=value
32
*/
33
34
Jclsym_t*
35
jclsym(Jcl_t* jcl, const char* name, const char* value, int flags)
36
{
37
register Jclsym_t* v;
38
register int n;
39
int imported;
40
const char* set;
41
char* e;
42
Jclsym_t* o;
43
44
if (!(set = name))
45
return 0;
46
else if (value)
47
n = strlen(name);
48
else if (value = (const char*)strchr(name, '='))
49
n = value++ - name;
50
else
51
return 0;
52
if (name[0] == '%' && name[1] == '%')
53
{
54
name = (const char*)sfprints("%s%-.*s", JCL_AUTO, n - 2, name + 2);
55
n = strlen(name);
56
imported = (flags & JCL_SYM_SET) && (jcl->flags & JCL_IMPORT);
57
}
58
else
59
imported = 0;
60
if (!(v = vmnewof(jcl->vs, 0, Jclsym_t, 1, n + strlen(value) + 2)))
61
{
62
nospace(jcl, NiL);
63
return 0;
64
}
65
memcpy(v->name, name, n);
66
strcpy(v->value = (char*)(v + 1) + n + 1, value);
67
v->flags = flags;
68
if (o = (Jclsym_t*)dtsearch(jcl->step->syms, v))
69
{
70
if (imported && (o->flags & JCL_SYM_IMPORT) || (flags & JCL_SYM_SET) && (o->flags & JCL_SYM_READONLY))
71
{
72
vmfree(jcl->vs, v);
73
v = o;
74
goto export;
75
}
76
dtdelete(jcl->step->syms, o);
77
vmfree(jcl->vs, o);
78
}
79
else if (imported && (e = getenv(name)))
80
{
81
vmfree(jcl->vs, v);
82
if (!(v = vmnewof(jcl->vs, 0, Jclsym_t, 1, strlen(e) + 1)))
83
{
84
nospace(jcl, NiL);
85
return 0;
86
}
87
strcpy(v->name, e);
88
if (v->value = strchr(v->name, '='))
89
*v->value++ = 0;
90
v->flags |= JCL_SYM_IMPORT;
91
}
92
dtinsert(jcl->step->syms, v);
93
if (flags & (JCL_SYM_EXPORT|JCL_SYM_READONLY))
94
{
95
export:
96
if (jcl->flags & JCL_EXEC)
97
{
98
if (!(set = vmstrdup(jcl->vs, set)) || !setenviron(set))
99
{
100
nospace(jcl, NiL);
101
return 0;
102
}
103
}
104
else if (jcl->flags & JCL_VERBOSE)
105
sfprintf(sfstdout, "export %s=%s\n", v->name, fmtquote(v->value, "\"", "\"", strlen(v->value), FMT_SHELL));
106
}
107
message((-2, "set %s=%s", v->name, v->value));
108
return v;
109
}
110
111