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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1996, by Sun Microsystems, Inc.
* All Rights Reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <locale.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/param.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "rules.h"
char * lex(FILE *);
extern char *mstrdup(const char *);
extern void *mmalloc(size_t size);
void
read_rules(FILE *file, int (*rulefunc)())
{
char *s;
int base_active = 0;
int list_ent_cnt = 0;
int gign_ent_cnt = 0;
int lign_ent_cnt = 0;
struct item *add_item();
struct item *fitem, *sitem;
char version[20];
last_gign = &gign_hd;
gign_hd.i_next = (struct item *)0;
gign_hd.i_str = (char *)0;
list_hd.i_next = (struct item *)0;
list_hd.i_str = (char *)0;
while (s = lex(file)) {
if (s == (char *)0)
break;
if (*s == '#')
continue;
if (*s == '*')
continue;
if (strcmp(s, BASE) == 0) {
#ifdef DEBUG
printf("BASE base_active = %d\n", base_active);
#endif /* DEBUG */
if (base_active) {
/*
* Tack local IGNORE strings to end of globals
*/
if (lign_hd.i_next != (struct item *)0) {
last_gign->i_next = &lign_hd;
}
/*
* Process directives for previous BASE command
* if there was one. Also free up LIST items
* and local IGNORE items.
*/
do_base_dir(basedir, &list_hd, &gign_hd,
rulefunc);
/*
* Free up space from LIST item list
*/
fitem = list_hd.i_next;
if (fitem != (struct item *)0) {
while (fitem != (struct item *)0) {
free(fitem->i_str);
sitem = fitem->i_next;
free(fitem);
fitem = sitem;
}
}
/*
* Free up space from local IGNORE item list
*/
fitem = lign_hd.i_next;
if (fitem != (struct item *)0) {
while (fitem != (struct item *)0) {
free(fitem->i_str);
sitem = fitem->i_next;
free(fitem);
fitem = sitem;
}
}
last_gign->i_next = (struct item *)0;
}
base_active = 1;
/*
* Reset LIST item list and local IGNORE item
* list to be empty.
*/
last_list = &list_hd;
list_hd.i_next = (struct item *)0;
list_hd.i_str = (char *)0;
last_lign = &lign_hd;
lign_hd.i_next = (struct item *)0;
lign_hd.i_str = (char *)0;
/*
* Get BASE directory specified
*/
s = lex(0);
if (s == (char *)0) {
fprintf(stderr, gettext("cachefspack: "));
fprintf(stderr, gettext(
"illegal BASE command\n"));
return;
}
if (*s == '$') {
/*
* String starts with a '$', it must be an
* environment variable
*/
s = getenv(&s[1]);
if (s == (char *)NULL) {
fprintf(stderr,
gettext("cachefspack: "));
fprintf(stderr,
gettext("Can't find "
"environment variable\n"));
exit(1);
}
}
basedir = mstrdup(s);
#ifdef DEBUG
printf("basedir = %s\n", basedir);
#endif /* DEBUG */
continue;
}
if (strcmp(s, IGNORE) == 0) {
#ifdef DEBUG
printf("IGNORE - base_active = %d\n", base_active);
#endif /* DEBUG */
if (base_active) {
/*
* Local IGNORE rule
*/
while ((s = lex(0))
!= 0) {
last_lign = add_item(last_lign, s,
def_lign_flags);
}
} else {
/*
* Global IGNORE rule
*/
while ((s = lex(0)) != 0) {
last_gign = add_item(last_gign, s,
def_gign_flags);
}
}
continue;
}
if (strcmp(s, LIST) == 0) {
#ifdef DEBUG
printf("LIST\n");
#endif /* DEBUG */
if (!base_active) {
fprintf(stderr,
gettext(
"cachefspack: skipping LIST command - "));
fprintf(stderr,
gettext(" no active base\n"));
continue;
}
while ((s = lex(0)) != 0) {
last_list = add_item(last_list, s,
def_list_flags);
}
continue;
}
if (strcmp(s, VERSION) == 0) {
sprintf(version, "%d.%d", VERMAJOR, VERMINOR);
s = lex(0);
if (s == (char *)0) {
fprintf(stderr, gettext("cachefspack: "));
fprintf(stderr, gettext("missing version\n"));
fprintf(stderr, gettext("cachefspack: "));
fprintf(stderr, gettext(
"version = %d.%d\n"), VERMAJOR, VERMINOR);
exit(1);
}
if (strcmp(version, s) != 0) {
fprintf(stderr, gettext(
"cachefspack: "));
fprintf(stderr, gettext(
"WARNING - version of packing rules "));
fprintf(stderr, gettext(
"does not match cachefspack version\n"));
fprintf(stderr, gettext(
"version = %d.%d\n"), VERMAJOR, VERMINOR);
}
}
}
/*
* Tack local IGNORE strings to end of globals
*/
if (lign_hd.i_next != (struct item *)0) {
last_gign->i_next = &lign_hd;
}
do_base_dir(basedir, &list_hd, &gign_hd, rulefunc);
}
struct item *
add_item(struct item *last_item, char *str, int flags)
{
struct item * add_cmd_items();
if (*str == CMDCHAR) {
last_item = add_cmd_items(last_item, &str[1], bang_list_flags);
} else {
last_item->i_next = (struct item *)mmalloc(
sizeof (struct item));
last_item = last_item->i_next;
last_item->i_str = mstrdup(str);
last_item->i_flag = flags;
last_item->i_next = (struct item *)0;
}
return (last_item);
}
struct item *
add_cmd_items(struct item *last_item, char *str, int flags)
{
FILE *fd;
char inbuf[MAX_RULE_SZ];
char *olddir = NULL;
char *s;
void getcmd(char *, char *);
if ((basedir != NULL) && (basedir[0] != '\0')) {
olddir = getcwd(NULL, MAXPATHLEN + 1);
if (olddir == NULL) {
fprintf(stderr, gettext("cannot malloc buffer\n"));
exit(1);
}
if (chdir(basedir) != 0) {
fprintf(stderr, gettext("cannot chdir to %s: %s\n"),
basedir, strerror(errno));
exit(1);
}
}
getcmd(str, inbuf);
fd = popen(inbuf, "r");
if (fd == NULL) {
fprintf(stderr, gettext("cachefspack: LIST can't execute - "));
fprintf(stderr, "%s\n", inbuf);
exit(1);
}
while (s = lex(fd)) {
last_item = add_item(last_item, s, flags);
while (s = lex(0)) {
last_item = add_item(last_item, s, flags);
}
}
if (pclose(fd) < 0) {
fprintf(stderr, gettext("cachefspack: can't close pipe\n"));
}
if (olddir != NULL) {
if (chdir(olddir) != 0) {
fprintf(stderr, gettext("cannot return to %s: %s\n"),
olddir, strerror(errno));
exit(1);
}
free(olddir);
}
return (last_item);
}
void
getcmd(char *str, char *buf)
{
char *s;
strcpy(buf, str);
strcat(buf, " ");
while (s = lex(0)) {
strcat(buf, s);
strcat(buf, " ");
}
#ifdef DEBUG
printf("getcmd: cmd = %s\n", buf);
#endif /* DEBUG */
}
/*
* routine:
* lex
*
* purpose:
* my own version of strtok that handles quoting and escaping
*
* parameters:
* string to be lexed (or 0 for same string)
*
* returns:
* pointer to next token
*
* notes:
* this routine makes no changes to the string it is passed,
* copying tokens into a static buffer.
*/
char *
lex(FILE *fd)
{ char c, delim;
char *p;
const char *s;
static const char *savep = 0;
static char namebuf[MAX_RULE_SZ];
static char inbuf[MAX_RULE_SZ];
int len, space_left;
char *err;
/*
* if the file descriptor is non-zero read a new command. Otherwise
* get fields from current line.
*/
if (fd != 0) {
len = 0;
space_left = sizeof (inbuf);
while ((err = fgets(&inbuf[len], space_left, fd)) != NULL) {
len = strlen(inbuf);
if (len == 1) {
/*
* must be a blank line starting command.
* If a blank line occurs after the start of
* a command, blanks will be included in the
* command.
*/
len = 0;
continue;
}
len -= 2;
space_left -= len;
s = (char *)((int)inbuf + len);
/*
* Continuation character
*/
if (strcmp(s, "\\\n") == 0) {
continue;
}
break;
}
if (err == NULL) {
return (err);
}
s = inbuf;
} else {
if (savep == 0)
return (0);
s = savep;
}
savep = 0;
/* skip over leading white space */
while (isspace(*s))
s++;
if (*s == 0) {
return (0);
}
/* see if this is a quoted string */
c = *s;
if (c == '\'' || c == '"') {
delim = c;
s++;
} else
delim = 0;
/* copy the token into the buffer */
for (p = namebuf; (c = *s) != 0; s++) {
if ((p - namebuf) >= sizeof (namebuf)) {
savep = 0;
return (0);
}
/* literal escape */
if (c == '\\') {
s++;
*p++ = *s;
continue;
}
/* closing delimiter */
if (c == delim) {
s++;
break;
}
/* delimiting white space */
if (delim == 0 && isspace(c))
break;
/* ordinary characters */
*p++ = *s;
}
/* remember where we left off */
savep = *s ? s : 0;
/* null terminate and return the buffer */
*p = 0;
return (namebuf);
}
char *
mk_base_dir(char *path, char *linkpath)
{
static char pathb[MAXPATHLEN];
char *dnam;
char *get_dirname(char *);
int len;
/*
* absolute path name
*/
if (*linkpath == '/') {
strcpy(pathb, linkpath);
} else {
/*
* relative path
*/
dnam = get_dirname(path);
if (dnam == (char *)0) {
return ((char *) 0);
}
strcpy(pathb, dnam);
len = strlen(pathb);
if (len == 0)
return (pathb);
if (pathb[len-1] != '/')
strcat(pathb, "/");
if (strncmp(linkpath, "../", 3) == 0) {
/*
* path is relative to directory containing sym link
* remove "../" from beginning of linkpath
*/
strcat(pathb, &linkpath[3]);
} else {
/*
* path is relative to directory containing sym link
*/
strcat(pathb, linkpath);
}
}
return (pathb);
}
|