-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgi-name
executable file
·300 lines (280 loc) · 10.4 KB
/
cgi-name
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
#!/bin/sh
######################################################################
#
# CGI-NAME - Convert "application/x-www-form-urlencoded" into Key-Value
# Format (Unicage "Name Format") And URL Decode
#
# USAGE: cgi-name [options] <param_file>
#
# <param_file> "application/x-www-form-urlencoded" text data file
# -e<c> ....... Escapes all of the character <c>s in value part of
# CGI variables strings with backshash
# -s<c> ....... Replaces all of the space " "s in value part of
# strings with <c>
# -n<string> .. Gives <string> as the value if its value part of
# CGI variables is nothing
# --template <html>
# ....... Adds variable records (value is decided by -n
# option if exists) when the variables do not exist
# in the data from <param_file> even though the same
# variables exist <html> as a <input type="radio">
# or "checkbox}"
#
# Designed originally by Nobuaki Tounaka
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2017-04-04
#
# This is a public-domain software (CC0). It means that all of the
# people can use this for any purposes with no restrictions at all.
# By the way, We are fed up with the side effects which are brought
# about by the major licenses.
#
######################################################################
######################################################################
# Initial Configuration
######################################################################
# === Initialize shell environment ===================================
set -u
umask 0022
export LC_ALL=C
export PATH="$(command -p getconf PATH)${PATH:+:}${PATH:-}"
# === Define the functions for printing usage and error message ======
print_usage_and_exit () {
cat <<-USAGE 1>&2
Usage : ${0##*/} [options] <param_file>
Args : <param_file> "application/x-www-form-urlencoded" text data file
Options : -e<c> ....... Escapes all of the character <c>s in value part of
CGI variables strings with backshash
-s<c> ....... Replaces all of the space " "s in value part of
strings with <c>
-n<string> .. Gives <string> as the value if its value part of
CGI variables is nothing
--template <html>
....... Adds variable records (value is decided by -n
option if exists) when the variables do not exist
in the data from <param_file> even though the same
variables exist <html> as a <input type="radio">
or "checkbox}"
Version : 2017-04-04 14:29:11 JST
Open usp Tukubai (POSIX Bourne Shell/POSIX commands)
USAGE
exit 1
}
error_exit() {
${2+:} false && echo "${0##*/}: $2" 1>&2
exit $1
}
# === Define some chrs. to escape some special chrs. temporarily =====
LFs=$(printf '\\\n_');LFs=${LFs%_} # Use as a "\n" in s-command of sed
######################################################################
# Parse Arguments
######################################################################
# === Get the options and the filepath ===============================
# --- initialize option parameters -----------------------------------
opte=''
opte_is_set=0
opts=''
opts_is_set=0
optn=''
tmpl_file=''
param_file=''
#
# --- get them -------------------------------------------------------
case $# in [!0]*)
optmode=''
i=0
for arg in "$@"; do
i=$((i+1))
case "$optmode" in '')
case "$arg" in
-e*)
optmode='e'
s=$(printf '%s' "${arg#??}_" | dd bs=1 count=2 2>/dev/null)
arg=${s%?}
opte_is_set=1
;;
-[sd]*)
optmode='s'
s=$(printf '%s' "${arg#??}_" | dd bs=1 count=2 2>/dev/null)
arg=${s%?}
opts_is_set=1
;;
-[ni]*)
optmode='n'
arg=${arg#-[ni]}
case "$arg" in '') continue;; esac
;;
--template)
optmode='tmpl'
continue
;;
*)
[ $i -eq $# ] || print_usage_and_exit
optmode='param'
;;
esac
;;
esac
case "$optmode" in
e) opte=$arg
optmode=''
;;
s) opts=$arg
optmode=''
;;
n) optn=$arg
optmode=''
;;
tmpl) tmpl_file=$arg
optmode=''
;;
param) param_file=$arg
optmode=''
;;
esac
done
;;
esac
# === Validate the arguments =========================================
use_stdin=0
case "$param_file" in '') param_file='-';; esac
case "$param_file" in '-'|'/dev/stdin'|'/dev/fd/0'|'/proc/self/fd/0')
use_stdin=$((use_stdin+1));;
esac
case "$tmpl_file" in '-'|'/dev/stdin'|'/dev/fd/0'|'/proc/self/fd/0')
use_stdin=$((use_stdin+1));;
esac
if [ $use_stdin -gt 1 ]; then print_usage_and_exit; fi
case "$tmpl_file" in ''|-|/*|./*|../*) :;; *) tmpl_file="./$tmpl_file" ;; esac
case "$param_file" in ''|-|/*|./*|../*) :;; *) param_file="./$param_file";; esac
[ ! -f "$param_file" ] &&
[ ! -c "$param_file" ] &&
[ ! -p "$param_file" ] &&
[ "_$param_file" != '_-' ] && {
print_usage_and_exit
}
[ ! -r "$param_file" ] && [ "_$param_file" != '_-' ] && {
error_exit 1 'Cannot open the file as a param_file'
}
######################################################################
# Prepare for the Main Routine
######################################################################
# === Extract variable name from the template file if exists =========
tmplvars=''
if [ -f "$tmpl_file" ] ||
[ -c "$tmpl_file" ] ||
[ -p "$tmpl_file" ] ||
[ "_$tmpl_file" = '_-' ] ;
then
[ ! -r "$tmpl_file" ] && [ "_$tmpl_file" != '_-' ] && {
error_exit 1 'Cannot open the file as a template file'
}
tmplvars=$(cat "$tmpl_file" |
tr '\r\n' ' ' |
grep ^ |
sed 's/\(<[Ii][Nn][Pp][Uu][Tt] [^>]*>\)/'"$LFs"'\1'"$LFs"'</g' |
grep '<[Ii][Nn][Pp][Uu][Tt] ' |
grep -iE ' type=["'"'"']?(radio\>|checkbox\>)' |
grep -iE ' name="[^"]*"| name='"'"'[^'"'"']*'"'"'| name=[^ ] ' |
sed 's/^.*[Nn][Aa][Mm][Ee]="\([^"]*\)".*$/\1/' |
sed 's/^.*[Nn][Aa][Mm][Ee]='"'"'\([^'"'"']*\)'"'"'.*$/\1/' |
sed 's/^.*[Nn][Aa][Mm][Ee]=\([^ ]*\) .*$/\1/' |
grep -v '[^][A-Za-z0-9_.:-]' |
sort |
uniq |
tr '\n' ' ' |
grep ^ |
sed 's/ $//' )
elif [ -n "$tmpl_file" ]; then
print_usage_and_exit
fi
######################################################################
# Main Routine
######################################################################
# === transfer the CGI variables string to the "Unicage name format" =
export opte opte_is_set opts opts_is_set optn tmplvars
grep ^ "$param_file" |
sed 's/\(%[0-9A-Fa-f][0-9A-Fa-f]\)/'"$LFs"'\1'"$LFs"'/g' |
sed '/^%/y/abcdef/ABCDEF/' |
tr -d '\n' |
grep ^ |
case $opte_is_set in #
0) cat ;; #
*) sed 's/%5C/\\/g' | sed 's/\\/\\\\/g';; #
esac |
sed 's/%0D%0A/\\n/g' |
sed 's/&/'"$LFs"'/g' |
sed 's/+/ /g' |
grep '^[^=]\{1,\}=' |
exec awk '
BEGIN {
for (i = 0; i < 256; i++) {
s = sprintf("%02X",i);
chr[s] = sprintf("%c",i);
}
opte = ENVIRON["opte" ];
opte_is_set = ENVIRON["opte_is_set"];
if (opte_is_set==1 && opte=="") {opte="\\";}
opts = ENVIRON["opts" ];
opts_is_set = ENVIRON["opts_is_set"];
optn = ENVIRON["optn" ];
tmplvars = ENVIRON["tmplvars" ];
split(tmplvars, array0);
for (var in array0) { tmplvar[array0[var]]=""; }
split("", array0);
}
{
# --- separate key and value(contains %XX) -------------------------
i = index($0,"=");
key = substr($0,1 ,i-1);
val0 = substr($0,i+1 );
val = "";
# --- convert the value(contains %XX) into the value (not contains %XX)
while (match(val0,/%[0-9A-F][0-9A-F]/)) {
hex = substr(val0, RSTART+1, 2);
val = val substr(val0, 1, RSTART-1) chr[hex];
val0 = substr(val0, RSTART+3);
}
val = val val0;
# --- treat the val for option "-e", "-s", "-n" --------------------
if (val == "") {
# --- [A] val is null
# 1) set default value (optn) if the val is not set and optn is set
if (optn != "") {val=optn;}
} else {
# --- [B] val is set
# 1) escape the charater which is set by opte if opte is set
if ((opte_is_set) && (opte!="\\")) {
s = "";
while (1) {
i = index(val,opte);
if (i==0) {break;}
s = s substr(val, 1 , i-1) "\\" opte;
val = substr(val, i+1 ) ;
}
val = s val;
}
# 2) replace " " into the string which is set by opts if opts is set
if (opts_is_set) {
s = "";
while (1) {
i = index(val," ");
if (i==0) {break;}
s = s substr(val, 1 , i-1) opts;
val = substr(val, i+1 ) ;
}
val = s val;
}
}
# --- delete the key from complement keys for the option "--template"
if (key in tmplvar) {delete tmplvar[key];}
# --- print key and value ------------------------------------------
if (val!="") {print key, val;} else {print key;}
}
END {
if (optn != "") {
for (var in tmplvar) {print var, optn;}
} else {
for (var in tmplvar) {print var ;}
}
}
'