about summary refs log tree commit diff stats
path: root/shin
blob: 9ba488b988b22ebbdf0358bcedfae864921e9175 (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
61
62
63
64
65
66
67
#!/bin/sh
# SHIN v. 0/1
# Copyright (C) Case Duckworth <acdw@acdw.net>
# Licensed under the Fair License.  See COPYING for details.

_shin() {
	awk '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) > 0) print(l) > outfile
	close(inclfile)
	sub(/</, ">", $0)
	print > outfile
}
function shin_test(filename) {
	 if (! system("test -f \"" f "\"")) return filename
	print("Cannot find \"" filename "\" in " sp) > (STDERR ? STDERR : "/dev/stderr")
	exit 1
}
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)
	}
}'
}

if [ "x$1" = -h ]; then
	cat <<\EOF
SHIN: include shell files in other shell files
USAGE: shin [-h] FILE...

FLAGS:
 -h	Show this help and exit.

PARAMETERS:
 FILE...	Input files. FILEs named FILE.shin will be built to
		FILE.sh in the same directory.  To include files in
		shin files, use the following comment syntax:

		#< INCLUDE

		If INCLUDE begins with / or ~, it's taken as a literal
		file name; otherwise SHINPATH will be searched for the
		file.  SHINPATH is a colon-separated list of paths
		like $PATH.  If SHINPATH is unset, it defaults to the
		current directory.

		If INCLUDE is not found, shin will error and quit.
		Otherwise, shin will add INCLUDE below that comment,
		as well as a comment denoting the end of the INCLUDE.
EOF
else
	_shin "$@"
fi