-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincludefile
executable file
·214 lines (174 loc) · 4.42 KB
/
includefile
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
#!/usr/bin/env sh
### Script: includefile
##
## ファイルをインクルードする。
##
## Metadata:
##
## author - qq542vev <https://purl.org/meta/me/>
## version - 0.1.0
## date - 2022-07-05
## since - 2022-07-05
## copyright - Copyright (C) 2022-2022 qq542vev. Some rights reserved.
## license - CC-BY <https://creativecommons.org/licenses/by/4.0/>
## package - w3mplus
##
## See Also:
##
## * Project homepage - <https://github.com/qq542vev/w3mplus>
## * Bag report - <https://github.com/qq542vev/w3mplus/issues>
VERSION='includefile 0.1.0'
. 'initialize.sh'
. 'option_error.sh'
# @getoptions
parser_definition() {
setup REST abbr:true error:optuon_error plus:true no:0 help:usage \
-- 'Usage:' " ${2##*/} [OPTION]... OUTPUT_DIR [FILE]..." \
'' 'Options:'
disp :usage -h --help -- 'このヘルプを表示して終了する'
disp VERSION -v --version -- 'バージョン情報を表示して終了する'
msg -- '' 'Exit Status:' \
' 0 - successful termination' \
' 64 - command line usage error' \
' 65 - data format error' \
' 66 - cannot open input' \
' 67 - addressee unknown' \
' 68 - host name unknown' \
' 69 - service unavailable' \
' 70 - internal software error' \
" 71 - system error (e.g., can't fork)" \
' 72 - critical OS file missing' \
" 73 - can't create (user) output file" \
' 74 - input/output error' \
' 75 - temp failure; user is invited to retry' \
' 76 - remote error in protocol' \
' 77 - permission denied' \
' 78 - configuration error' \
' 129 - received SIGHUP' \
' 130 - received SIGINT' \
' 131 - received SIGQUIT' \
' 143 - received SIGTERM'
}
# @end
# @gengetoptions parser -i parser_definition parse "${1}"
# @end
eval "$(getoptions parser_definition parse "${0}")"
parse ${@+"${@}"}
eval "set -- ${REST}"
case "${#}" in
'0')
cat <<-EOF >&2
${0##*/}: 引数が不足しています。
詳細については '${0##*/} --help' を実行してください。
EOF
endCall "${EX_USAGE}"
;;
esac
output="${1}"
shift
mkdir -p "${output}"
includeShellscript=$(
cat <<-'EOF'
BEGIN {
for(i = 1; i < ARGC; i++) {
read_file(ARGV[i])
}
}
function pathto(file, len,pathlist,i,result,tmp) {
if(index(file, "/") != 0) {
return file
}
len = split(ENVIRON["PATH"], pathlist, ":")
for(i = 1; i <= len; i++) {
result = (pathlist[i] == "" ? "." : pathlist[i]) "/" file
if(0 < (getline tmp < result)) {
close(result)
return result
}
}
return ""
}
function read_file(file, record,fields) {
while(0 < (getline record < file)) {
split(record, fields)
if(fields[1] == ".") {
gsub(/["']/, "", fields[2])
fpath = pathto(fields[2])
if(fpath == "") {
printf("awk: %s: cannot find %s\n", file, fields[2]) | "cat >&2"
exit
}
read_file(fpath)
} else {
printf("%s\n", record)
}
}
close(file)
}
EOF
)
includeAwkscript=$(
cat <<-'EOF'
BEGIN {
split("", include)
for(i = 1; i < ARGC; i++) {
read_file(ARGV[i], include)
}
}
function pathto(file, len,pathlist,i,result,tmp) {
if(index(file, "/") != 0) {
return file
}
len = split(ENVIRON["AWKPATH"], pathlist, ":")
for(i = 1; i <= len; i++) {
result = (pathlist[i] == "" ? "." : pathlist[i]) "/" file
if(0 < (getline tmp < result)) {
close(result)
return result
}
}
return ""
}
function read_file(file, include, record,fields) {
include[file] = 1
while(0 < (getline record < file)) {
split(record, fields)
if(fields[1] == "@include") {
gsub(/"/, "", fields[2])
fpath = pathto(fields[2])
if(fpath == "") {
printf("awk: %s: cannot find %s\n", file, fields[2]) | "cat >&2"
exit
}
if(!(fpath in include)) {
read_file(fpath, include)
}
} else if(fields[1] == "EOF" || fields[1] == "'") {
split("", include)
printf("%s\n", record)
} else {
printf("%s\n", record)
}
}
close(file)
}
EOF
)
for file in ${@+"${@}"}; do
if [ '!' -f "${file}" ]; then
cat <<-EOF >&2
${0##*/}: '${file}' は通常ファイルではありません。
EOF
continue
elif [ '!' -r "${file}" ]; then
cat <<-EOF >&2
${0##*/}: '${file}' の読み込み許可がありません。
EOF
continue
fi
gengetoptions embed "${file}" \
| sed -e '/eval .*getoptions/{ s/^/#/; }' \
| awk -- "${includeShellscript}" - \
| awk -- "${includeAwkscript}" - \
| shfmt -mn -ln 'posix' >"${output}/${file##*/}"
done