-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincludefile
executable file
·259 lines (217 loc) · 5.78 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
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
#!/usr/bin/env sh
### Script: includefile
##
## ファイルをインクルードする。
##
## Metadata:
##
## id - 58a1cbfd-8bc9-40d5-8013-cb765d3fd182
## author - <qq542vev at https://purl.org/meta/me/>
## version - 0.2.0
## date - 2022-09-13
## since - 2022-07-05
## copyright - Copyright (C) 2022-2022 qq542vev. Some rights reserved.
## license - <CC-BY at https://creativecommons.org/licenses/by/4.0/>
## package - w3mplus
##
## See Also:
##
## * <Project homepage at https://github.com/qq542vev/w3mplus>
## * <Bug report at https://github.com/qq542vev/w3mplus/issues>
##
## Help Output:
##
## ------ Text ------
## Usage:
## includefile [OPTION]... OUTPUT_DIR [FILE]...
##
## Options:
## -V, --{no-}verbose 冗長な表示を行う
## -h, --help このヘルプを表示して終了する
## -v, --version バージョン情報を表示して終了する
##
## 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
## ------------------
readonly 'VERSION=includefile 0.2.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:'
flag verbose -V --{no-}verbose init:@no -- '冗長な表示を行う'
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__
end_call "${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
case "${verbose}" in
'1') printf "Converting '%s'...\\n" "${file}" >&2
esac
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 -s -ln 'posix' \
| sed -e '1b; /^[[:blank:]]*#/d' >"${output}/${file##*/}"
case "${verbose}" in
'1') printf 'Done\n' >&2
esac
done