-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathname-cgi
More file actions
executable file
·156 lines (144 loc) · 7.54 KB
/
name-cgi
File metadata and controls
executable file
·156 lines (144 loc) · 7.54 KB
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
#!/bin/sh
######################################################################
#
# NAME-CGI - HTTP POST String Generator
#
# USAGE: name-cgi <file> [file...]
# ARGS : file
# A source file for you want to convert into HTTP POST string.
# The required format is
# <variable_name#1> <value#1>
# <variable_name#2> <value#2>
# : :
# * <variablename> and <value> is separated by only one space.
# And <value> can have space characters.
# * backslashes in <value> must be escaped as '\\'.
# And <value> can have LF characters as '\n'.
# RET : $?=0 (when all of the arguments are valid)
# stdout ... encoded CGI variables for HTTP POST(GET)
# LF will NOT be added at the end of string
#
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2020-05-06
#
# 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.
#
# The latest version is distributed at the following page.
# https://github.com/ShellShoccar-jpn/misc-tools
#
######################################################################
######################################################################
# Initial Configuration
######################################################################
# === Initialize shell environment ===================================
set -u
umask 0022
export LC_ALL=C
export PATH="$(command -p getconf PATH 2>/dev/null)${PATH+:}${PATH-}"
case $PATH in :*) PATH=${PATH#?};; esac
export UNIX_STD=2003 # to make HP-UX conform to POSIX
# === Define the functions for printing usage and error message ======
print_usage_and_exit () {
cat <<-USAGE 1>&2
Usage : ${0##*/} <file> [file...]
Argument: file
A source file for you want to convert into HTTP POST string.
The required format is
<variable_name#1> <value#1>
<variable_name#2> <value#2>
: :
* <variablename> and <value> is separated by only one space.
And <value> can have space characters.
* backslashes in <value> must be escaped as '\\'.
And <value> can have LF characters as '\n'.
Return : \$? ....... 0 when all of the arguments are valid
stdout ... encoded CGI variables for HTTP POST(GET)
LF will NOT be added at the end of string
Version : 2020-05-06 22:42:19 JST
USAGE
exit 1
}
error_exit() {
${2+:} false && echo "${0##*/}: $2" 1>&2
exit $1
}
######################################################################
# Argument Parsing
######################################################################
# === Print usage and exit if one of the help options is set =========
case "$# ${1:-}" in
'1 -h'|'1 --help'|'1 --version') print_usage_and_exit;;
esac
# === Set "-" into the 1st argument if no argument is given ==========
case $# in 0) set -- -;; esac
######################################################################
# Main Routine
######################################################################
use_stdin=0
for file in "$@"; do #
if [ "_$file" = '_' ] || #
[ "_$file" = '_-' ] || #
[ "_$file" = '_/dev/stdin' ] || #
[ "_$file" = '_/dev/fd/0' ] || #
[ "_$file" = '_/proc/self/fd/0' ] ; then #
use_stdin=$((use_stdin+1)) #
[ $use_stdin -le 1 ] || { #
error_exit 1 'stdin cannot be opend only once: '"$file" #
} #
file='' #
elif [ -f "$file" ] || #
[ -c "$file" ] || #
[ -p "$file" ] ; then #
[ -r "$file" ] || error_exit 1 'Cannot open the file: '"$file" #
else #
print_usage_and_exit #
case "$file" in /*|./*|../*) :;; *) file="./$file";; esac #
fi #
grep '' ${file:+"$file"} #
done |
awk ' #
BEGIN { #
# --- prepare #
OFS = ""; #
ORS = ""; #
# --- prepare str2url #
for(i= 0;i<256;i++){c2a[sprintf("%c",i)]=sprintf("%%%02X",i);} #
c2a[" "]="+"; #
for(i=48;i< 58;i++){c2a[sprintf("%c",i)]=sprintf("%c",i); } #
for(i=65;i< 91;i++){c2a[sprintf("%c",i)]=sprintf("%c",i); } #
for(i=97;i<123;i++){c2a[sprintf("%c",i)]=sprintf("%c",i); } #
c2a["-"]="-"; c2a["."]="."; c2a["_"]="_"; c2a["~"]="~"; #
# --- first delemiter #
dlm = ""; #
} #
{ #
# --- get the name and value --------------------------------- #
i = length($0); #
j = index($0, " "); #
if (i == 0) { next; } #
if (j == 0) { #
name = $0; #
value = ""; #
} else if (i == j) { #
name = substr($0, 1, i-1); #
value = ""; #
} else { #
name = substr($0, 1, j-1); #
value = substr($0, j+1 ); #
} #
# --- unescape the value ------------------------------------- #
gsub(/\\n/ , "\n", value); #
gsub(/\\\\/, "\\", value); #
# --- URL encode --------------------------------------------- #
enc_value = ""; #
for(i=1; i<=length(value); i++) { #
enc_value = enc_value c2a[substr(value,i,1)]; #
} #
# --- print -------------------------------------------------- #
print dlm, name, "=", enc_value; #
# --- set delemiter ------------------------------------------ #
dlm = "&"; #
} #
'