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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
|
/*
* Copyright (c) 1990 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND CARNEGIE MELLON UNIVERSITY
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT
* SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Users of this software agree to return to Carnegie Mellon any
* improvements or extensions that they make and grant Carnegie the
* rights to redistribute these changes.
*
* Export of this software is permitted only after complying with the
* regulations of the U.S. Deptartment of Commerce relating to the
* Export of Technical Data.
*/
/*
* setpath --- smart interface for setting path variables
*
* usage: setpath(paths, cmds, localsyspath, dosuffix, printerrors)
* char **paths, **cmds, *localsyspath;
* int dosuffix, printerrors;
*
* The 'paths' argument is a list of pointers to path lists of the
* form "name=value" where name is the name of the path and value
* is a colon separated list of directories. There can never be
* more than MAXDIRS (64) directories in a path.
*
* The 'cmds' argument may be a sequence of any of the following:
* -r reset path to default
* -i newpath insert newpath before localsyspath
* -ia oldpath newpath insert newpath after oldpath
* -ib oldpath newpath insert newpath before oldpath
* -i# newpath insert newpath at position #
* -d oldpath delete oldpath
* -d# delete path at position #
* -c oldpath newpath change oldpath to newpath
* -c# newpath change position # to newpath
*
* The "-i newpath" command is equivilent to "-ib 'localsyspath' newpath".
*
* If 'dosuffix' is true, the appropriate suffix will be added to
* all command operands for any system path in 'paths'.
*
* Both of the 'paths' and 'cmds' lists are terminated by a NULL
* entry.
*
* if 'printerrors' is true, setpath will printf error diagnostics.
*
* WARNING !!!: Under no circumstances should anyone change this
* module without fully understanding the impact on the C shell.
* The C shell has it's own malloc and printf routines and this
* module was carefully written taking that into account. Do not
* use any stdio routines from this module except printf.
*
**********************************************************************
* HISTORY
*
* Revision 1.4 90/12/11 17:58:44 mja
* Add copyright/disclaimer for distribution.
*
* 05-Jun-88 Glenn Marcy (gm0w) at Carnegie-Mellon University
* Make all non-entry points static.
*
* 30-Apr-88 Glenn Marcy (gm0w) at Carnegie-Mellon University
* Added -r switch to reset paths to their default values.
*
* 06-Jan-86 Glenn Marcy (gm0w) at Carnegie-Mellon University
* Created from old setpath program for the shell.
*
**********************************************************************
*/
#include "sh.h"
RCSID("$tcsh: ma.setp.c,v 1.18 2006/03/02 18:46:44 christos Exp $")
#ifdef MACH
#define MAXDIRS 64 /* max directories on a path */
#ifndef NULL
# define NULL 0
#endif
static int npaths; /* # pathlist arguments */
static struct pelem {
struct pelem *pnext; /* pointer to next path */
char *pname; /* name of pathlist */
char *psuf; /* suffix for pathlist */
char *pdef; /* default for pathlist */
int pdirs; /* # directories on each pathlist */
char *pdir[MAXDIRS]; /* directory names for each pathlist */
} *pathhead = NULL;
static struct {
char *name;
char *suffix;
char *defalt;
} syspath[] = {
"PATH", "/bin", ":/usr/ucb:/bin:/usr/bin",
"CPATH", "/include", ":/usr/include",
"LPATH", "/lib", ":/lib:/usr/lib",
"MPATH", "/man", ":/usr/man",
"EPATH", "/maclib", "",
0, 0, 0
};
static int sflag;
static int eflag;
#define INVALID { \
if (eflag) xprintf(CGETS(10, 1, \
"setpath: invalid command '%s'.\n"), cmd); \
freepaths(); \
return(-1); \
}
#define TOOFEW { \
if (eflag) xprintf(CGETS(10, 2, \
"setpath: insufficient arguments to command '%s'.\n"), cmd); \
freepaths(); \
return(-1); \
}
static int initpaths (char **);
static void savepaths (char **);
static void freepaths (void);
static void rcmd (char *);
static void icmd (char *, char *);
static void iacmd (char *, char *);
static void ibcmd (char *, char *);
static void incmd (char *, int);
static void insert (struct pelem *, int, char *);
static void dcmd (char *);
static void dncmd (int);
static void delete (struct pelem *, int);
static void ccmd (char *, char *);
static void cncmd (char *, int);
static void change (struct pelem *, int, char *);
static int locate (struct pelem *, char *);
int
setpath(char **paths, char **cmds, char *localsyspath, int dosuffix,
int printerrors)
{
char *cmd, *cmd1, *cmd2;
int ncmd;
sflag = dosuffix;
eflag = printerrors;
if (initpaths(paths) < 0)
return(-1);
if (npaths == 0)
return(0);
for (ncmd = 0; cmd = cmds[ncmd]; ncmd++) {
if (cmd[0] != '-')
INVALID;
cmd1 = cmds[ncmd+1];
cmd2 = cmds[ncmd+2];
switch (cmd[1]) {
case 'r':
if (cmd[2] != '\0')
INVALID;
rcmd(localsyspath);
break;
case 'i':
if (cmd[2] == '\0') {
ncmd++;
if (cmd1 == NULL) TOOFEW;
icmd(cmd1, localsyspath);
} else if (isdigit(cmd[2])) {
ncmd++;
if (cmd1 == NULL) TOOFEW;
incmd(cmd1, atoi(cmd+2));
} else if (cmd[3] != '\0' || (cmd[2] != 'a' && cmd[2] != 'b')) {
INVALID;
} else {
ncmd += 2;
if (cmd1 == NULL || cmd2 == NULL) TOOFEW;
if (cmd[2] == 'a')
iacmd(cmd1, cmd2);
else
ibcmd(cmd1, cmd2);
}
break;
case 'd':
if (cmd[2] == '\0') {
ncmd++;
if (cmd1 == NULL) TOOFEW;
dcmd(cmd1);
} else if (isdigit(cmd[2]))
dncmd(atoi(cmd+2));
else {
INVALID;
}
break;
case 'c':
if (cmd[2] == '\0') {
ncmd += 2;
if (cmd1 == NULL || cmd2 == NULL) TOOFEW;
ccmd(cmd1, cmd2);
} else if (isdigit(cmd[2])) {
ncmd++;
if (cmd1 == NULL) TOOFEW;
cncmd(cmd1, atoi(cmd+2));
} else {
INVALID;
}
break;
default:
INVALID;
}
}
savepaths(paths);
freepaths();
return(0);
}
static int
initpaths(char **paths)
{
char *path, *val, *p, *q;
int i, done;
struct pelem *pe, *pathend;
freepaths();
for (npaths = 0; path = paths[npaths]; npaths++) {
val = index(path, '=');
if (val == NULL) {
if (eflag)
xprintf(CGETS(10, 3,
"setpath: value missing in path '%s'\n"), path);
freepaths();
return(-1);
}
*val++ = '\0';
pe = xmalloc(sizeof(struct pelem));
setzero(pe, sizeof(struct pelem));
if (pathhead == NULL)
pathhead = pathend = pe;
else {
pathend->pnext = pe;
pathend = pe;
}
p = strsave(path);
pe->pname = p;
pe->psuf = "";
pe->pdef = "";
for (i = 0; syspath[i].name; i++)
if (strcmp(pe->pname, syspath[i].name) == 0) {
pe->psuf = syspath[i].suffix;
pe->pdef = syspath[i].defalt;
break;
}
q = val;
for (;;) {
q = index(p = q, ':');
done = (q == NULL);
if (!done)
*q++ = '\0';
p = strsave(p);
pe->pdir[pe->pdirs] = p;
pe->pdirs++;
if (done)
break;
}
}
return(0);
}
static void
savepaths(char **paths)
{
char *p, *q;
int npath, i, len;
struct pelem *pe;
for (npath = 0, pe = pathhead; pe; npath++, pe = pe->pnext) {
len = strlen(pe->pname) + 1;
if (pe->pdirs == 0)
len++;
else for (i = 0; i < pe->pdirs; i++)
len += strlen(pe->pdir[i]) + 1;
p = xmalloc((unsigned)len);
paths[npath] = p;
for (q = pe->pname; *p = *q; p++, q++);
*p++ = '=';
if (pe->pdirs != 0) {
for (i = 0; i < pe->pdirs; i++) {
for (q = pe->pdir[i]; *p = *q; p++, q++);
*p++ = ':';
}
p--;
}
*p = '\0';
}
}
static void
freepaths(void)
{
char *p;
int i;
struct pelem *pe;
if (npaths == 0 || pathhead == NULL)
return;
while (pe = pathhead) {
if (pe->pname) {
for (i = 0; i < pe->pdirs; i++) {
if (pe->pdir[i] == NULL)
continue;
p = pe->pdir[i];
pe->pdir[i] = NULL;
xfree((ptr_t) p);
}
pe->pdirs = 0;
p = pe->pname;
pe->pname = NULL;
xfree((ptr_t) p);
}
pathhead = pe->pnext;
xfree((ptr_t) pe);
}
npaths = 0;
}
/***********************************************
*** R E S E T A P A T H N A M E ***
***********************************************/
static void
rcmd(char *localsyspath) /* reset path with localsyspath */
{
int n, done;
char *new, *p;
struct pelem *pe;
char newbuf[MAXPATHLEN+1];/*FIXBUF*/
for (pe = pathhead; pe; pe = pe->pnext) {
new = newbuf;
*new = '\0';
if (localsyspath != NULL) {
*new = ':';
(void) strcpy(new + 1, localsyspath);
(void) strcat(new, pe->psuf);
}
(void) strcat(new, pe->pdef);
for (n = 0; n < pe->pdirs; n++) {
if (pe->pdir[n] == NULL)
continue;
p = pe->pdir[n];
pe->pdir[n] = NULL;
xfree((ptr_t) p);
}
pe->pdirs = 0;
for (;;) {
new = index(p = new, ':');
done = (new == NULL);
if (!done)
*new++ = '\0';
p = strsave(p);
pe->pdir[pe->pdirs] = p;
pe->pdirs++;
if (done)
break;
}
}
}
/***********************************************
*** I N S E R T A P A T H N A M E ***
***********************************************/
static void
icmd(char *path, char *localsyspath) /* insert path before localsyspath */
{
int n;
char *new;
struct pelem *pe;
char newbuf[MAXPATHLEN+1];/*FIXBUF*/
for (pe = pathhead; pe; pe = pe->pnext) {
if (sflag)
new = localsyspath;
else {
new = newbuf;
(void) strcpy(new, localsyspath);
(void) strcat(new, pe->psuf);
}
n = locate(pe, new);
if (n >= 0)
insert(pe, n, path);
else
insert(pe, 0, path);
}
}
static void
iacmd(char *inpath, char *path) /* insert path after inpath */
{
int n;
struct pelem *pe;
for (pe = pathhead; pe; pe = pe->pnext) {
n = locate(pe, inpath);
if (n >= 0)
insert(pe, n + 1, path);
else
xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
inpath, pe->pname);
}
}
static void
ibcmd(char *inpath, char *path) /* insert path before inpath */
{
int n;
struct pelem *pe;
for (pe = pathhead; pe; pe = pe->pnext) {
n = locate(pe, inpath);
if (n >= 0)
insert(pe, n, path);
else
xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
inpath, pe->pname);
}
}
static void
incmd(char *path, int n) /* insert path at position n */
{
struct pelem *pe;
for (pe = pathhead; pe; pe = pe->pnext)
insert(pe, n, path);
}
static void
insert(struct pelem *pe, int loc, char *key)
{
int i;
char *new;
char newbuf[2000];/*FIXBUF*/
if (sflag) { /* add suffix */
new = newbuf;
(void) strcpy(new, key);
(void) strcat(new, pe->psuf);
} else
new = key;
new = strsave(new);
for (i = pe->pdirs; i > loc; --i)
pe->pdir[i] = pe->pdir[i-1];
if (loc > pe->pdirs)
loc = pe->pdirs;
pe->pdir[loc] = new;
pe->pdirs++;
}
/***********************************************
*** D E L E T E A P A T H N A M E ***
***********************************************/
static void
dcmd(char *path) /* delete path */
{
int n;
struct pelem *pe;
for (pe = pathhead; pe; pe = pe->pnext) {
n = locate(pe, path);
if (n >= 0)
delete(pe, n);
else
xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
path, pe->pname);
}
}
static void
dncmd(int n) /* delete at position n */
{
struct pelem *pe;
for (pe = pathhead; pe; pe = pe->pnext) {
if (n < pe->pdirs)
delete(pe, n);
else
xprintf(CGETS(10, 5,
"setpath: %d not valid position in %s\n"),
n, pe->pname);
}
}
static void
delete(struct pelem *pe, int n)
{
int d;
xfree((ptr_t) (pe->pdir[n]));
for (d = n; d < pe->pdirs - 1; d++)
pe->pdir[d] = pe->pdir[d+1];
--pe->pdirs;
}
/***********************************************
*** C H A N G E A P A T H N A M E ***
***********************************************/
static void
ccmd(char *inpath, char *path) /* change inpath to path */
{
int n;
struct pelem *pe;
for (pe = pathhead; pe; pe = pe->pnext) {
n = locate(pe, inpath);
if (n >= 0)
change(pe, n, path);
else
xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
inpath, pe->pname);
}
}
static void
cncmd(char *path, int n) /* change at position n to path */
{
struct pelem *pe;
for (pe = pathhead; pe; pe = pe->pnext) {
if (n < pe->pdirs)
change(pe, n, path);
else
xprintf(CGETS(10, 5,
"setpath: %d not valid position in %s\n"),
n, pe->pname);
}
}
static void
change(struct pelem *pe, int loc, char *key)
{
char *new;
char newbuf[MAXPATHLEN+1];/*FIXBUF*/
if (sflag) { /* append suffix */
new = newbuf;
(void) strcpy(new, key);
(void) strcat(new, pe->psuf);
} else
new = key;
new = strsave(new);
xfree((ptr_t) (pe->pdir[loc]));
pe->pdir[loc] = new;
}
/***************************************
*** F I N D P A T H N A M E ***
***************************************/
static int
locate(struct pelem *pe, char *key)
{
int i;
char *realkey;
char keybuf[MAXPATHLEN+1];/*FIXBUF*/
if (sflag) {
realkey = keybuf;
(void) strcpy(realkey, key);
(void) strcat(realkey, pe->psuf);
} else
realkey = key;
for (i = 0; i < pe->pdirs; i++)
if (strcmp(pe->pdir[i], realkey) == 0)
break;
return((i < pe->pdirs) ? i : -1);
}
#endif
|