File: mkpath.awk

package info (click to toggle)
smail 3.2.0.102-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 4,228 kB
  • ctags: 3,924
  • sloc: ansic: 41,366; sh: 3,434; makefile: 2,349; awk: 689; perl: 598; yacc: 427; sed: 2
file content (363 lines) | stat: -rw-r--r-- 7,053 bytes parent folder | download | duplicates (4)
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
# @(#) mkpath.awk,v 1.4 1992/07/11 11:40:10 tron Exp
#
# mkpath.awk perform awk pre-processing of an input file
#
#    Copyright (C) 1988 Ronald S. Karr and Landon Curt Noll
#    Copyright (C) 1992 Ronald S. Karr
#
# See the file COPYING, distributed with smail, for restriction
# and warranty information.
#
# Some awk's don't process paragraphs in the order that they
# appear in the file, so we are forced to make this program
# non-deterministic.

### initial setup
#
BEGIN {

#   clear line counts
    lineno = 0;
#   not in a block initially
    inblock = 0;
#   no errors so far
    errno = 0;
#   line modes
    mode_file = 1;
    mode_cmd = 2;
    mode_lit = 3;
    mode = 0;

#   preset shell commands
    gleem = "$GLEEM";
}

### per line debug and processing
#
{

#   note another line, echo it if -v
    ++lineno;
    if ( VERBOSE > 0 && NF > 0 ) {
	print PROG": line", lineno":", $0 | "cat 1>&2";
    }
}

### ignore blank lines
#
NF < 1 {
    next;
}

### pre-processing of lines with args
#
NF > 1 {

#   find string beyond the first field
    args = substr($0, index($0,$1)+length($1));	# remove field 1
    if ( $2 ~ /^`/ && $NF ~ /`$/ ) {
	mode = mode_cmd;				# cmd output
	# remove backquotes
	args = substr(args, index(args,$2)+1, length(args)-index(args,$2)-1);
    } else {
	if ( $2 ~ /^'/ && $NF ~ /'$/ ) {
	    mode = mode_lit;				# text literal
	} else {
	    mode = mode_file;				# data file
	}
        args = substr(args, index(args,$2));
    }
}

### map - store map commands into WORK
#
$1 == "map" {

    if ( NF <= 1 ) {
	print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
	errno = 1;
	if (ERR > 0) {
	    exit;		# END will be processed
	}
    } else {
	if ( inblock == 0 ) {
	    print "(";
	    inblock = 1;
	}
	if (mode == mode_cmd) {
	    print args, "|", gleem, "-p -c -f -";
	}
	if (mode == mode_lit) {
	    print "echo", args "|", gleem, "-p -c -f -";
	}
	if (mode == mode_file) {
	    print gleem, "-p -c -d $CWD -f /dev/null", args;
	}
    }
    next;
}

### safemap - store safe map commands into WORK
#
$1 == "safemap" {

    if ( NF <= 1 ) {
	print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
	errno = 11;
	if (ERR > 0) {
	    exit;		# END will be processed
	}
    } else {
	if ( inblock == 0 ) {
	    print "(";
	    inblock = 1;
	}
	if (mode == mode_cmd) {
	    print args, "|", gleem, "-s -F -p -c -f -";
	}
	if (mode == mode_lit) {
	    print "echo", args "|", gleem, "-s -F -p -c -f -";
	}
	if (mode == mode_file) {
	    print gleem, "-s -F -p -c -d $CWD -f /dev/null", args;
	}
    }
    next;
}

### delete - store delete commands into WORK
#
$1 == "delete" {

    if ( NF <= 1 ) {
	print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
	errno = 2;
	if (ERR > 0) {
	    exit;		# END will be processed
	}
    } else {
	if ( inblock == 0 ) {
	    print "(";
	    inblock = 1;
	}
	if (mode == mode_cmd) {
	    print args, "|", gleem, "-n delete -f -";
	}
	if (mode == mode_lit) {
	    print "echo", args "|", gleem, "-n delete -f -";
	}
	if (mode == mode_file) {
	    print gleem, "-p -n delete -d $CWD -f /dev/null", args;
	}
    }
    next;
}

### adjust - store adjust commands into WORK
#
$1 == "adjust" {

    if ( NF <= 1 ) {
	print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
	errno = 3;
	if (ERR > 0) {
	    exit;		# END will be processed
	}
    } else {
	if ( inblock == 0 ) {
	    print "(";
	    inblock = 1;
	}
	if (mode == mode_cmd) {
	    print args, "|", gleem, "-n adjust -f -";
	}
	if (mode == mode_lit) {
	    print "echo", args "|", gleem, "-n adjust -f -";
	}
	if (mode == mode_file) {
	    print gleem, "-p -n adjust -d $CWD -f /dev/null", args;
	}
    }
    next;
}

### dead - store dead commands into WORK
#
$1 == "dead" {

    if ( NF <= 1 ) {
	print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
	errno = 4;
	if (ERR > 0) {
	    exit;		# END will be processed
	}
    } else {
	if ( inblock == 0 ) {
	    print "(";
	    inblock = 1;
	}
	if (mode == mode_cmd) {
	    print args, "|", gleem, "-n dead -f -";
	}
	if (mode == mode_lit) {
	    print "echo", args "|", gleem, "-n dead -f -";
	}
	if (mode == mode_file) {
	    print gleem, "-p -n dead -d $CWD -f /dev/null", args;
	}
    }
    next;
}

### text - store commands to inject
#
$1 == "text" {

    if ( NF <= 1 ) {
	print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
	errno = 5;
	if (ERR > 0) {
	    exit;		# END will be processed
	}
    } else {
	if (mode == mode_cmd) {
	    print args;
	}
	if (mode == mode_lit) {
	    print "echo", args;
	}
	if (mode == mode_file) {
	    print "cat /dev/null", args;
	}
    }
    next;
}

### file - store file changing commands into WORK
#
$1 == "file" {

    if ( NF > 2 ) {
	print "echo", PROG, ": line", lineno":", $0, "too many args 1>&2";
	errno = 6;
	if (ERR > 0) {
	    exit;		# END will be processed
	}
    }
    if ( NF <= 1 ) {
	print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
	errno = 7;
	if (ERR > 0) {
	    exit;		# END will be processed
	}
    }
    if ( NF == 2 ) {
	if ( inblock == 0 ) {
	    print "(";
	    inblock = 1;
	}
	if ( $2 ~ /^\// ) {
	    print "echo \"file {", $2, "}\"";
	} else {
	    print "echo \"file { `pwd`/"$2, "}\"";
	}
    }
    next;
}

### cd - store cd directory changing commands into WORK
#
$1 == "cd" {

    if ( NF > 2 ) {
	print "echo", PROG, ": line", lineno":", $0, "too many args 1>&2";
	errno = 8;
	if (ERR > 0) {
	    exit;		# END will be processed
	}
    }
#   cd by itself refers to the dir from where mkpath was started
    if ( NF == 1 ) {
	print "cd $PWD";
	print "CWD=$PWD";
    }
    if ( NF == 2 ) {
#	cd with a '-' refers to the dir that the input file is in
	if ( $2 == "-" ) {
	    print "cd $CD";
	    print "CWD=$CD";
#	otherwise cd to the arg
	} else {
	    print "cd", $2;
	    print "CWD="$2;
	}
    }
    next;
}

### sh - store a SHELL command into a file
#
$1 == "sh" {

    if ( NF <= 1 ) {
	print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
	errno = 9;
	if (ERR > 0) {
	    exit;		# END will be processed
	}
    } else {
	print args;
    }
    next;
}

### pathalias - end a block with pathalias(8) plus optional args
#
$1 == "pathalias" {

    if ( inblock > 0 ) {
	if ( NF > 1 ) {
	    print ") | eval $PATHALIAS", args;
	} else {
	    print ") | eval $PATHALIAS";
	}
    }
    inblock = 0;
    next;
}

### pathsort - end a block with pathalias(8) plus post-processing and args
#
$1 == "pathsort" {

    if ( inblock > 0 ) {
	if ( NF > 1 ) {
	    print ") | eval $PATHSORT", args;
	} else {
	    print ") | eval $PATHSORT";
	}
    }
    inblock = 0;
    next;
}

### ERROR - catch unknown lines, known lines resulted in 'next' skipping ERROR
#
{
    errno = 10;
    print PROG": line", lineno": unknown command:", $0 | "cat 1>&2";
#   if the -e flag was igven to mkpath, insert a exit right now
    if (ERR > 0) {
	exit;		# END will be processed
    }
}

### END - the end of a config file has an implied pathsort if it needs one
#
END {

    if ( inblock > 0 ) {
	print ") | eval $PATHSORT";
    }
    print "exit", errno;
}