aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/data/scripts/formfiller.sh
blob: 394bfbd6c4e931b2dea1dfd6e7e7cef6d38ba1a3 (plain)
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
#!/bin/sh
#
# action
# new:  add new profile template (creates file if not found), then edit
# edit: edit file (fall back to 'new' if file not found)
# load: load from file
# once: use temporary file to edit form once
# (empty): if file not available, new; otherwise, load
#

action=$1

. "$UZBL_UTIL_DIR/uzbl-dir.sh"
. "$UZBL_UTIL_DIR/editor.sh"

mkdir -p "$UZBL_FORMS_DIR" || exit

domain=${UZBL_URI#*://}
domain=${domain%%/*}

test "$domain" || exit

file=$UZBL_FORMS_DIR/$domain

GenForm ()
{
    echo 'js uzbl.formfiller.dump();' \
    | socat - unix-connect:"$UZBL_SOCKET" \
    | awk '
        /^formfillerstart$/ {
            while (getline) {
                if ( /^%!end/ ) exit
                print
            }
        }
    '
}

GetOption ()
{
    DMENU_SCHEME=formfiller

    # util/dmenu.sh doesn't handle spaces in DMENU_PROMPT. a proper fix will be
    # tricky.
    DMENU_PROMPT="choose_profile"
    DMENU_LINES=4

    . "$UZBL_UTIL_DIR/dmenu.sh"

    if [ $(grep -c '^!profile' "$1") -gt 1 ]
    then sed -n 's/^!profile=//p' "$1" | $DMENU
    else sed -n 's/^!profile=//p' "$1"
    fi
}

ParseProfile ()
{
    sed "/^>/d; /^!profile=$1$/,/^!/!d; /^!/d"
}

ParseFields ()
{
    awk '/^%/ {

        sub ( /%/, "" )

        split( $0, parts, /\(|\)|\{|\}/ )

        field = $0
        sub ( /[^:]*:/, "", field )

        if ( parts[2] ~ /^(checkbox|radio)$/ )
            printf( "js uzbl.formfiller.insert(\"%s\",\"%s\",\"%s\",%s);\n",
                    parts[1], parts[2], parts[3], field )

        else if ( parts[2] ~ /^textarea$/ ) {
            field = ""
            while (getline) {
                if ( /^%/ ) break
                sub ( /^\\/, "" )
                gsub ( /"/, "\\\"" )
                gsub ( /\\/, "\\\\" )
                field = field $0 "\\\\n"
            }
            printf( "js uzbl.formfiller.insert(\"%s\",\"%s\",\"%s\",0);\n",
                parts[1], parts[2], field )
        }

        else
            printf( "js uzbl.formfiller.insert(\"%s\",\"%s\",\"%s\",0);\n",
                    parts[1], parts[2], field )


    }'
}

New ()
{
    { echo '!profile=NAME_THIS_PROFILE'
      GenForm | sed 's/^!/\\!/'
      echo '!'
    } >> "$file"
    chmod 600 "$file"
    $UZBL_EDITOR "$file"
}

Edit ()
    if [ -e "$file" ]
    then $UZBL_EDITOR "$file"
    else New
    fi

Load ()
{
    test -e "$file" || exit

    option=$(GetOption "$file")

    case $option in *[!a-zA-Z0-9_-]*) exit 1; esac

    ParseProfile $option < "$file" \
    | ParseFields \
    | sed 's/@/\\@/g' \
    > "$UZBL_FIFO"
}

Once ()
{
    tmpfile=/tmp/${0##*/}-$$-tmpfile
    trap 'rm -f "$tmpfile"' EXIT

    GenForm > "$tmpfile"
    chmod 600 "$tmpfile"

    $UZBL_EDITOR "$tmpfile"

    test -e "$tmpfile" &&
    ParseFields < "$tmpfile" \
    | sed 's/@/\\@/g' \
    > "$UZBL_FIFO"
}

case $action in
    new) New; Load ;;
    edit) Edit; Load ;;
    load) Load ;;
    once) Once ;;
    '') if [ -e "$file" ]; then Load; else New; Load; fi ;;
    *) exit 1
esac