-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathurlencode
executable file
·135 lines (121 loc) · 5.35 KB
/
urlencode
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
#!/bin/sh
######################################################################
#
# URLENCODE - URL Encoder on the Basis of RFC 3986
#
# Usage : urlencode [-r|--raw] <file> ...
# Args : <file> ...... Text file for URL encoding
# Options : -r ...... RAW MODE : when this option is set, all of " " are
# replaced with "%20" instead of "+".
# --raw ... same as the "-r" option
# Environs: LINE_BUFFERED
# =yes ........ Line-buffered mode if possible
# =forcible ... Line-buffered mode or exit if impossible
#
#
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2022-02-08
#
# 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 ========================
print_usage_and_exit() {
cat <<-USAGE 1>&2
Usage : ${0##*/} [-r|--raw] <file> ...
Args : <file> ...... Text file for URL encoding
Options : -r, --raw ... RAW MODE :
" " will not be converted into "+" but "%20"
Environs: LINE_BUFFERED
=yes ........ Line-buffered mode if possible
=forcible ... Line-buffered mode or exit if impossible
Version : 2022-02-08 19:51:45 JST
(POSIX Bourne Shell/POSIX commands)
USAGE
exit 1
}
######################################################################
# Parse Options
######################################################################
# === Print help message if required =================================
case "${1:-}" in
--help|--version|-h) print_usage_and_exit;;
esac
# === Other options ==================================================
instead_of_spc='+';
case $# in [!0]*)
for arg in ${1+"$@"}; do
case "$arg" in
-r|--raw) instead_of_spc='%20'; shift;break;;
--) shift;break;;
*) : ;;
esac
done
;;
esac
######################################################################
# Switch to the line-buffered mode if required
######################################################################
awkfl=''
case "${LINE_BUFFERED:-}" in
[Ff][Oo][Rr][Cc][EeIi]*|2) lbm=2;;
[Tt][Rr][Uu][Ee]|[Yy][Ee][Ss]|[Yy]|1) lbm=1;;
*) lbm=0;;
esac
case $lbm in [!0]*)
s=$(awk -W interactive 'BEGIN{}' 2>&1)
case "$?$s" in
'0') alias awk='awk -W interactive';;
*) awkfl='system("");' ;;
esac
;; esac
######################################################################
# Main
######################################################################
(cat ${1+"$@"}; echo '') |
awk ' #
BEGIN { #
# --- prepare #
OFS = ""; #
ORS = ""; #
# --- prepare encoding #
for(i= 0;i<256;i++){c2p[sprintf("%c",i)]=sprintf("%%%02X",i);} #
c2p[" "]="'"$instead_of_spc"'"; #
for(i=48;i< 58;i++){c2p[sprintf("%c",i)]=sprintf("%c",i); } #
for(i=65;i< 91;i++){c2p[sprintf("%c",i)]=sprintf("%c",i); } #
for(i=97;i<123;i++){c2p[sprintf("%c",i)]=sprintf("%c",i); } #
c2p["-"]="-"; c2p["."]="."; c2p["_"]="_"; c2p["~"]="~"; #
# --- encode #
while (getline line) { #
for (i=1; i<=length(line); i++) { #
print c2p[substr(line,i,1)]; #
} #
print "\n";'"$awkfl"' #
} #
}' |
awk ' #
BEGIN{ #
ORS=""; #
OFS=""; #
getline line; #
print line; #
while (getline line) { #
print "\n",line;'"$awkfl"' #
} #
} #
'