aboutsummaryrefslogtreecommitdiff
path: root/unix/boot/bootlib/envinit.c
blob: e70a8d864191dbd0205767859c9174e542cb8429 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
 */

#include <stdio.h>
#include <string.h>
#define	import_spp
#define	import_xnames
#include <iraf.h>

#define	isspace(c)	((c)==' '||(c)=='\t'||(c)=='\n')
#define	SETENV		"zzsetenv.def"
#define	SZ_VALUE	SZ_COMMAND
#define	MAXLEV		8
#define	PKGLIBS		"pkglibs"
#define	IRAFARCH	"IRAFARCH"
#define	ARCH		"arch"

extern	char  *_os_getenv (char *envvar, char *outstr, int maxch);
extern	char  *os_getenv (char *envvar);
extern	char  *os_strpak (XCHAR *sppstr, char *cstr, int maxch);
extern	char  *vfn2osfn (char *vfn, int new);
extern  XCHAR *os_strupk (char *str, XCHAR *outstr, int maxch);
extern	void   os_putenv (char *name, char *value);
extern	int bdebug;

void 	_envinit (void);
void 	loadenv (char *osfn);



/* LOADPKGENV -- Load the environment definitions for the named package.
 * [e.g., loadpkgenv ("noao")].  This assumes that the root directory of
 * the named package is already defined, and that this directory contains
 * a subdirectory lib containing the file zzsetenv.def.  If none of these
 * assumptions are true, call loadenv(osfn) with the host filename of the
 * file to be loaded.
 */
void
loadpkgenv (char *pkg)
{
	char	vfn[SZ_PATHNAME+1];
	char	pkglibs[SZ_COMMAND+1];
	char	newlibs[SZ_COMMAND+1];

	/* Initialize the default IRAF environment. */
	_envinit();

	/* If no package name is given or the IRAF environment is being
	 * loaded we are done.
	 */
	if (!pkg || strcmp(pkg,"iraf")==0)
	    return;

	strcpy (vfn, pkg);
	strcat (vfn, "$lib/");
	strcat (vfn, SETENV);

	/* Load the package environment.  The new values are added to the
	 * environment in the conventional way except for the value of
	 * "pkglibs".  As each package environment is loaded we want to
	 * add the newly defined package libraries to the current list
	 * of package libraries, otherwise the most recent package environment
	 * overrides the earlier ones.  It is still possible that user
	 * defined environment variables will be redefined but there is
	 * little we can do about that; "pkglibs" is special though since
	 * it is a part of the loadpkgenv facility.
	 */
	_os_getenv (PKGLIBS, pkglibs, SZ_COMMAND);
	loadenv (vfn2osfn (vfn, 0));
	_os_getenv (PKGLIBS, newlibs, SZ_COMMAND);

	if (strlen(newlibs) > 0 && strcmp (newlibs, pkglibs)) {
	    char    *ip, *op;
	    char    *otop;

	    /* Find the end of the current pkglibs file list. */
	    for (ip=op=pkglibs;  *ip;  ip++)
		if (!isspace(*ip))
		    op = ip + 1;

	    /* Concatenate the new files list segment. */
	    if (op > pkglibs)
		*op++ = ',';
	    for (ip=newlibs, otop=pkglibs+SZ_COMMAND;  *ip && op < otop;  ip++)
		if (!isspace(*ip))
		    *op++ = *ip;

	    /* Blank fill to the next SZ_LINE increment to optimize resets. */
	    while (op < otop && ((op-pkglibs) % SZ_LINE))
		*op++ = ' ';
	    *op++ = EOS;
		
	    /* Reset the stored value in the environment. */
	    os_putenv (PKGLIBS, pkglibs);
	}
}


#ifdef NOVOS
void _envinit (void) {}
void loadenv (char *osfn) { printf ("HSI is compiled NOVOS\n"); }
#else

/* ENVINIT -- Initialize the VOS environment list by scanning the file
 * hlib$zzsetenv.def.  HLIB is defined in terms of HOST which is sufficiently
 * well known to have a value before the environment list is loaded.
 */
void
_envinit (void)
{
	static	int initialized = 0;
	char	osfn[SZ_PATHNAME+1], *hlib;
	char	irafarch[SZ_PATHNAME+1];

	extern  void ENVINIT(), ENVRESET();


	if (initialized++)
	    return;

	if ( (hlib = os_getenv ("hlib")) ) {
	    strcpy (osfn, hlib);
	    strcat (osfn, SETENV);
	} else {
	    fprintf (stderr, "cannot translate logical name `hlib'");
	    fflush (stderr);
	}

	ENVINIT();
	loadenv (osfn);

	/* If the variable "IRAFARCH" is defined and "arch" is not, add
	 * a definition for the latter.  "arch" is used to construct
	 * pathnames but the HSI architecture support requires only that
	 * IRAFARCH be predefined.
	 */
	if (_os_getenv (IRAFARCH, irafarch, SZ_PATHNAME))
	    if (!_os_getenv (ARCH, osfn, SZ_PATHNAME)) {
		XCHAR   x_name[SZ_PATHNAME+1];
		XCHAR   x_value[SZ_PATHNAME+1];

		sprintf (osfn, ".%s", irafarch);
		os_strupk (ARCH, x_name, SZ_PATHNAME);
		os_strupk (osfn, x_value, SZ_PATHNAME);
		ENVRESET (x_name, x_value);
	    }
}


/* LOADENV -- Load environment definitions from the named host file.
 */
void
loadenv (char *osfn)
{
	register char	*ip;
	register XCHAR	*op;

	char	lbuf[SZ_LINE+1];
	char	pkname[SZ_FNAME+1], old_value[SZ_VALUE+1];
	XCHAR	name[SZ_FNAME+1], value[SZ_VALUE+1];
	FILE	*fp, *sv_fp[MAXLEV];
	int	lev=0;

	extern  void ENVRESET();


	if ((fp = fopen (osfn, "r")) == NULL) {
	    printf ("envinit: cannot open `%s'\n", osfn);
	    fflush (stdout);
	    return;
	}

	for (;;) {
	    /* Get next line from input file. */
	    if (fgets (lbuf, SZ_LINE, fp) == NULL) {
		/* End of file. */
		if (lev > 0) {
		    fclose (fp);
		    fp = sv_fp[--lev];
		    continue;
		} else
		    break;

	    } else {
		/* Skip comments and blank lines. */
		for (ip=lbuf;  isspace(*ip);  ip++)
		    ;
		if (strncmp (lbuf, "set", 3) != 0) {
		    if (strncmp (lbuf, "reset", 5) != 0)
			continue;
		    else
			ip += 5;
		} else
		    ip += 3;

		/* Check for @file inclusion. */
		while (isspace(*ip))
		    ip++;

		if (*ip == '@') {
		    sv_fp[lev++] = fp;
		    if (lev >= MAXLEV) {
			printf ("envinit: nesting too deep\n");
			fflush (stdout);
			break;

		    } else {
			char *fname;
			fname = ++ip;

			while (*ip)
			    if (isspace(*ip)) {
				*ip = '\0';
				break;
			    } else
				ip++;

			if ((fp = fopen (vfn2osfn(fname,0), "r")) == NULL) {
			    printf ("envinit: cannot open `%s'\n", fname);
			    fflush (stdout);
			    break;
			}
		    }
		    continue;
		}

		/* fall through */
	    }

	    /* Extract name field. */
	    for (op=name;  *ip && *ip != '=' && !isspace(*ip);  op++)
		*op = *ip++;
	    *op = XEOS;

	    /* Extract value field; may be quoted.  Newline may be escaped
	     * to break a long value string over several lines of the input
	     * file.
	     */
	    for (; *ip && (*ip == '=' || *ip == '"' || isspace (*ip));  ip++)
		;
	    for (op=value;  *ip && *ip != '"' && *ip != '\n';  op++)
		if (*ip == '\\' && *(ip+1) == '\n') {
again:		    if (fgets (lbuf, SZ_LINE, fp) == NULL)
			break;
		    for (ip=lbuf;  isspace(*ip);  ip++)
			;
		    if (*ip == '#')
			goto again;
		} else
		    *op = *ip++;
	    *op = XEOS;

	    /* Allow the user to override the values of environment variables
	     * by defining them in their host environment.  Once again,
	     * "pkglibs" requires special treatment as we want to permit
	     * redefinitions to allow concatenation in loadpkgenv().
	     */
	    os_strpak (name, pkname, SZ_FNAME);
	    if (strcmp (pkname, PKGLIBS) &&
		_os_getenv (pkname, old_value, SZ_VALUE)) {
		if (bdebug)
		    printf ("%s = %s\n", pkname, old_value);
	    } else
		ENVRESET (name, value);
	}

	fclose (fp);
}
#endif