Discussion:
Urlencoder + Shell Alias
(too old to reply)
Mike Sanders
2023-11-09 07:02:26 UTC
Permalink
Beware wordwrap.

# tags: urlencode, url, search, lynx, alias, shell, awk, code
#
# awk script urlencodes output - ASCII chars only,
# doesn't deal with multibyte UTF-8 chars
# Michael Sanders 2023
# https://busybox.neocities.org/notes/urlencode.txt
#
# handy shell function & alias...
#
# search() {
# query=$(echo "$*" | awk -f urlencode.txt)
# lynx "https://lite.duckduckgo.com/lite/?q=$query"
# }
#
# alias ?=search
#
# usage example: ? what is awk

{ print urlencode($0) }

BEGIN { FS = OFS = "" }

function urlencode(str, x, y, c, encoded) {
encoded = ""
y = length(str)
for (x = 1; x <= y; x++) {
c = substr(str, x, 1)
if (c ~ /[0-9A-Za-z]/) encoded = encoded c # alphanumeric chars are not encoded
else encoded = encoded "%" hexify(c) # other chars are percent-encoded
}
return encoded
}

function hexify(c) {
hex = "0123456789ABCDEF"
asc = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
pos = index(asc, c)
if (pos == 0) return "00" # handle non-printable chars (like \n, which shouldn't appear in URL)
hib = int((pos + 31) / 16)
lob = int((pos + 31) % 16)
return substr(hex, hib + 1, 1) substr(hex, lob + 1, 1)
}

# eof
--
:wq
Mike Sanders
Mike Sanders
2023-11-09 11:01:44 UTC
Permalink
Post by Mike Sanders
function hexify(c) {
hex = "0123456789ABCDEF"
pos = index(asc, c)
if (pos == 0) return "00" # handle non-printable chars (like \n, which shouldn't appear in URL)
hib = int((pos + 31) / 16)
lob = int((pos + 31) % 16)
return substr(hex, hib + 1, 1) substr(hex, lob + 1, 1)
}
Imprtant update (just reload the page):

https://busybox.neocities.org/notes/urlencode.txt
--
:wq
Mike Sanders
Loading...