• Urlencoder + Shell Alias

    From porkchop@porkchop@invalid.foo (Mike Sanders) to comp.lang.awk on Thu Nov 9 07:02:26 2023
    From Newsgroup: comp.lang.awk

    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

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From porkchop@porkchop@invalid.foo (Mike Sanders) to comp.lang.awk on Thu Nov 9 11:01:44 2023
    From Newsgroup: comp.lang.awk

    Mike Sanders <porkchop@invalid.foo> wrote:

    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)
    }

    Imprtant update (just reload the page):

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

    --- Synchronet 3.20a-Linux NewsLink 1.114