about summary refs log tree commit diff stats
path: root/shin.awk
blob: 80c76aa0ab59cafe132f65747dbb6574820658c1 (plain)
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
#!/usr/bin/awk -f
# SHIN: include files in shell scripts
# by Case Duckworth <acdw@acdw.net>
# usage: shin -- FILE.shin...
# each FILE.shin will output to FILE.sh in the same directory
BEGIN {
	if (ENVIRON["SHINPATH"]) {
		split(ENVIRON["SHINPATH"], SHINPATH, ":")
	} else {
		SHINPATH[1] = "."
	}
}

FNR == 1 {
	outfile = FILENAME
	sub(/in$/, "", outfile)
}

{
	print($0) > outfile
}

/^#</ {
	inclfile = shin_resolve(substr($0, 3))
	while (getline l < inclfile) {
		print(l) > outfile
	}
	close(inclfile)
	sub(/</, ">", $0)
	print > outfile
}


function shin_resolve(filename)
{
	if (match(filename, "^/")) {
		return shin_test(filename)
	}
	if (match(filename, "^~")) {
		return shin_test(ENVIRON["HOME"] "/" substr(filename, 2))
	}
	sub(/^[ \t]*/, "", filename)
	sub(/[ \t]*$/, "", filename)
	sp = ""
	for (p in SHINPATH) {
		sp = sp (sp ? ", " : "") "\"" SHINPATH[p] "\""
		f = SHINPATH[p] "/" filename
		gsub("//", "/", f)
		return shin_test(f)
	}
}

function shin_test(filename)
{
	if (! system("test -f \"" f "\"")) {
		return filename
	}
	print("Cannot find \"" filename "\" in " sp) > (STDERR ? STDERR : "/dev/stderr")
	exit 1
}