-
Notifications
You must be signed in to change notification settings - Fork 0
/
nameread
executable file
·224 lines (208 loc) · 6.87 KB
/
nameread
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
#!/bin/sh
######################################################################
#
# NAMEREAD - A Key-Value Format (Unicage "Name Format") Data Reader
#
# USAGE: nameread [options] <name> <name_file>
#
# <name> ...... key name you want to get
# <name_file> . Key-Value Format (Unicage "Name Format") text data file
# -e .......... Regards <name> as a regexp (ERE) string
# -l .......... Prints not only value but also key (=variable) name
# -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
#
# Designed originally by Nobuaki Tounaka
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2017-03-05
#
# 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] <name> <name_file>
Args : <name> ...... key name you want to get
<name_file> . Key-Value Format (Unicage "Name Format") text data file
Options : -e .......... Regards <name> as a regexp (ERE) string
-l .......... Prints not only value but also key (=variable) name
-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
Version : 2017-03-05 04:49:02 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 =====
TAB=$(printf '\t')
######################################################################
# Parse Arguments
######################################################################
# === Get the options and the filepath ===============================
# --- initialize option parameters -----------------------------------
opts=''
opts_is_set=0
optn=''
opte_is_set=0
optl_is_set=0
name=''
name_file=''
#
# --- get them -------------------------------------------------------
[ $# -ge 1 ] || print_usage_and_exit
optmode=''
i=0
for arg in "$@"; do
i=$((i+1))
if [ -z "$optmode" ]; then
case "$arg" in
-[elnsdi]*)
ret=$(echo "_${arg#-}" |
awk '{
e = "_";
l = "_";
n = "_";
s = "_";
opt_str = "";
for (i=2;i<=length($0);i++) {
c = substr($0,i,1);
if (c == "e") {
e = "e";
} else if (c == "l") {
l = "l";
} else if ((c == "n") || (c == "i")) {
n = "n";
opt_str = substr($0,i+1);
break;
} else if ((c == "s") || (c == "d")) {
s = "s";
opt_str = substr($0,i+1);
break;
}
}
printf("%s%s%s%s %s",e,l,n,s,opt_str);
}')
ret1=${ret%% *}
ret2=${ret#* }
case "$ret1" in *e*) opte_is_set=1;; esac
case "$ret1" in *l*) optl_is_set=1;; esac
case "$ret1" in *n*) case "$ret2" in
'') optmode='n';;
*) optn=$ret2 ;;
esac
;;
esac
case "$ret1" in *s*) opts=$ret2
opts_is_set=1
;;
esac
;;
*)
if [ -z "$name" ]; then
[ $i -ge $(($#-1)) ] || print_usage_and_exit
name=$arg
elif [ -z "$name_file" ]; then
[ $i -eq $# ] || print_usage_and_exit
name_file=$arg
else
print_usage_and_exit
fi
;;
esac
elif [ "$optmode" = 'n' ]; then
optn=$arg
optmode=''
else
print_usage_and_exit
fi
done
# === Validate the arguments =========================================
case "$name" in '') print_usage_and_exit;; esac
if [ -z "$name_file" ]; then
name_file='-'
elif [ ! -f "$name_file" ] &&
[ ! -c "$name_file" ] &&
[ ! -p "$name_file" ] &&
[ "_$name_file" != '_-' ]
then
print_usage_and_exit
fi
if [ ! -r "$name_file" ] && [ "_$name_file" != '_-' ]; then
error_exit 1 'Cannot open the file'
exit 1
fi
case "$name_file" in ''|-|/*|./*|../*) :;; *) file="./$name_file";; esac
######################################################################
# Prepare for the Main Routine
######################################################################
# === Generate the AWK code for nameread operation ===================
awk_code='
BEGIN {
opte_is_set = ENVIRON["opte_is_set"];
optl_is_set = ENVIRON["optl_is_set"];
optn = ENVIRON["optn" ];
opts = ENVIRON["opts" ];
opts_is_set = ENVIRON["opts_is_set"];
target_name = ENVIRON["name" ];
}
{
i = index($0," ");
name = (i > 1) ? $1 : (length($0)>0) ? $0 : "";
if (opte_is_set == 0 && name != target_name) {
next;
}
var = (i > 0) ? substr($0,i+1) : "";
if (length(var) == 0 && length(optn)) {
var = optn;
} else if (opts_is_set != 0) {
s = "";
while (match(var,/ /)) {
s = s substr(var, 1, RSTART-1) opts;
var = substr(var, RSTART+1);
}
var = s var;
}
if (optl_is_set) {
print name, var;
} else {
print var;
}
}
'
######################################################################
# Main Routine
######################################################################
case $opte_is_set in
0) export opte_is_set optl_is_set optn opts opts_is_set name
exec awk "$awk_code" "$name_file"
;;
*) case "${name#^}" in "$name") name="[^$TAB ]*${name}";;
*) name="${name#^}" ;;
esac
case "${name#$}" in "$name") name="${name}[^$TAB ]*";;
*) name="${name%$}" ;;
esac
export opte_is_set optl_is_set optn opts opts_is_set name
grep -E "^${name}([$TAB ]+.*|)\$" "$name_file" |
exec awk "$awk_code"
;;
esac