aboutsummaryrefslogtreecommitdiffstats
path: root/xenstore-editor.sh
blob: 1d5550a9706ed6dbd454e2013e30ff22fd9e35e4 (plain) (blame)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/bin/bash
### Name: Simple XenStore Editor
### Version: 1.2
### Release Date: 2012-08-18

[ "`id -u`" != "0" ] && echo "This script should be run as root." && exit 40

if [ "$1" ];then
    current="$1"
else
    current="/"
fi

should_exit=0
first_run=1
setdefault=0
prevdir="$current"
tmpfile=`mktemp`
scripttitle="               Simple XenStore Editor Version 1.2              "
scriptshorttitle="Simple XenStore Editor"

if [ -z "$DIALOG" ]; then
    for i in dialog cdialog
    do
        DIALOG=`which $i 2> /dev/null`
        [ "$DIALOG" ] && break
    done
fi

[ -z "$DIALOG" ] && echo "A required program \`dialog' cannot be found in your PATH. (You can specify it by setting the environment variable DIALOG)" && exit 42

function getxenfullpath () {
    if [ "$1" = "/" ]
    then
        echo "/$2"
    else
        echo "$1/$2"
    fi
}

function getusernewvalue () {
    local initvalue="$3"
    while true
    do
        $DIALOG --title "$1" --extra-button --extra-label "File Browser" --inputbox "$2" 0 0 "$initvalue" 2> "$tmpfile"
        local exitstat=$?
        case "$exitstat" in 
            0|1)
                return "$exitstat"
                ;;
            3)
                local initdir="/"
                local tmpfile2="`mktemp`"
                local selectedfile="/"
                local nowvalue="`cat "$tmpfile"`"
                local parentdir="`dirname "$nowvalue"`"
                if [ "`echo "$nowvalue" | cut -c 1`" != "/" ] || [ '!' -e "$parentdir" ] 
                then
                    $DIALOG --title "File Browser" --msgbox "$parentdir directory does not exits" 0 0
                    initdir="`pwd`/"
                else
                    initdir="$nowvalue"
                fi
                $DIALOG --title "Use space-bar to copy the current selection" --fselect "$initdir" 13 75 2> "$tmpfile2"
                if [ "$?" = "0" ]
                then
                    selectedfile="`cat "$tmpfile2"`"
                    initvalue="$selectedfile"
                else
                    initvalue="$nowvalue"
                fi
                rm -f "$tmpfile2"
                ;;
        esac
    done
}

while [ "$should_exit" = "0" ]
do
    unset dirlist
    unset valuelist
    unset i
    unset j
    xenstore-list "$current" > "$tmpfile"
    if [ "$?" != "0" ]; then
        $DIALOG --title "$scripttitle" --msgbox "Cannot list the directory $current" 0 0
        if [ "$first_run" != "0" ]
        then
            exit 1
        else
            current="$prevdir"
            continue
        fi
    fi
    first_run=0
    declare -a dirlist
    declare -a valuelist
    mapfile -t dirlist < "$tmpfile"
    declare -i i=0
    declare -i j=0
    if [ -z "${dirlist[0]}" ]
    then
        valuelist[0]="(Empty)"
        valuelist[1]=""
    else
        while [ "${dirlist[$i]}" ]
        do
            valuelist[$j]="${dirlist[$i]}"
            j=$j+1
            valuelist[$j]="`xenstore-read $current/${dirlist[$i]}`"
            j=$j+1
            i=$i+1
        done
    fi
    if [ "$setdefault" = "0" ]
    then
        $DIALOG --title "$scripttitle" --ok-label "Chdir" --cancel-label "Exit" --extra-button --extra-label "Edit" --menu "$current" 0 0 0 "${valuelist[@]}" 2> "$tmpfile"
    else
        $DIALOG --title "$scripttitle" --ok-label "Chdir" --cancel-label "Exit" --extra-button --extra-label "Edit" --default-item "$setdefaultvalue" --menu "$current" 0 0 0 "${valuelist[@]}" 2> "$tmpfile"
    fi
    dialogexit=$?
    dialogout="`cat "$tmpfile"`"
    setdefault=0
    case "$dialogexit" in
        0)
            unset valuepathvalid
            descending="`getxenfullpath "$current" "$dialogout"`"
            ascending="`dirname "$current"`"
            usevalue="`xenstore-read "$descending"`"
            if [ "$usevalue" ]; then
                xenstore-read "$usevalue" 2> /dev/null
                [ "$?" = "0" ] && valuepathvalid=1
            fi
            if [ "$dialogout" = "(Empty)" ]; then
                $DIALOG --title "$scriptshorttitle - Chdir" --menu "Choose from the list" 0 0 0 "Back" "Go to $ascending" "Manual" "Type a XenStore Path" 2> "$tmpfile"
                dialogexit=$?
            elif [ "$usevalue" ] &&  [ "$valuepathvalid" = "1" ]; then
                $DIALOG --title "$scriptshorttitle - Chdir" --menu "Choose from the list" 0 0 0 "Enter" "Go to $descending" "Back" "Go to $ascending" "UseValue" "Go to $usevalue" "Manual" "Type a XenStore Path" 2> "$tmpfile"
                dialogexit=$?
            else
                $DIALOG --title "$scriptshorttitle - Chdir" --menu "Choose from the list" 0 0 0 "Enter" "Go to $descending" "Back" "Go to $ascending" "Manual" "Type a XenStore Path" 2> "$tmpfile"
                dialogexit=$?
            fi
            if [ "$dialogexit" = "0" ]
            then
                dialogout2="`cat "$tmpfile"`"
                case "$dialogout2" in
                    "Enter")
                        prevdir="$current"
                        current="$descending"
                        ;;
                    "Back")
                        prevdir="$current"
                        current="$ascending"
                        ;;
                    "UseValue")
                        prevdir="$current"
                        current="$usevalue"
                        ;;
                    "Manual")
                        $DIALOG --title "$scriptshorttitle - Chdir - Manual" --inputbox "XenStore Directory Name" 0 0 "$current" 2> "$tmpfile"
                        if [ "$?" = "0" ]
                        then
                            prevdir="$current"
                            current="`cat "$tmpfile"`"
                        else 
                            setdefault=1
                            setdefaultvalue="$dialogout"
                        fi
                        ;;
                esac
            else
                setdefault=1
                setdefaultvalue="$dialogout"
            fi
            ;;
        1)
            if $DIALOG --title "$scriptshorttitle - Exit" --yesno "Do you really want to quit?" 0 0
            then
                should_exit=1
            fi
            ;;
        3)
            if [ "$dialogout" = "(Empty)" ]
            then
                $DIALOG --title "$scriptshorttitle - Edit" --menu "Choose from the list" 0 0 0 "Add" "Add a new value" 2> "$tmpfile"
                dialogexit=$?
                dialogout2="`cat "$tmpfile"`"
            else
                $DIALOG --title "$scriptshorttitle - Edit" --menu "Choose from the list" 0 0 0 "Modify" "Modify this value" "Remove" "Remove this value" "Add" "Add a new value" 2> "$tmpfile"
                dialogexit=$?
                dialogout2="`cat "$tmpfile"`"
            fi
            if [ "$dialogexit" = "0" ]
            then
                case "$dialogout2" in 
                    "Add")
                        $DIALOG --title "$scriptshorttitle - Edit - Add" --inputbox "Name" 0 0 2> "$tmpfile"
                        if [ "$?" = "0" ]
                        then
                            newname="`cat "$tmpfile"`"
                        else
                            setdefault=1
                            setdefaultvalue="$dialogout"
                            continue
                        fi
                        getusernewvalue "$scriptshorttitle - Edit - Add" "Value" ""
                        if [ "$?" = "0" ]
                        then
                            newvalue="`cat "$tmpfile"`"
                        else
                            setdefault=1
                            setdefaultvalue="$dialogout"
                            continue
                        fi
                        fullpath="`getxenfullpath "$current" "$newname"`"
                        outmsg="`xenstore-write "$fullpath" "$newvalue" 2>&1`"
                        if [ "$?" != "0" ]
                        then
                            $DIALOG --title "$scriptshorttitle - Edit - Add" --msgbox "$outmsg" 0 0
                        else
                            setdefault=1
                            setdefaultvalue="$dialogout"
                        fi
                    ;;
                    "Modify")
                        fullpath="`getxenfullpath "$current" "$dialogout"`"                     
                        getusernewvalue "$scriptshorttitle - Edit - Modify" "New value" "`xenstore-read "$fullpath"`"
                        if [ "$?" = "0" ]
                        then
                            dialogout3="`cat "$tmpfile"`"
                            outmsg="`xenstore-write "$fullpath" "$dialogout3" 2>&1`"
                            if [ "$?" != "0" ]
                            then
                                $DIALOG --title "$scriptshorttitle - Edit - Modify" --msgbox "$outmsg" 0 0
                            else
                                setdefault=1
                                setdefaultvalue="$dialogout"
                            fi
                        else
                            setdefault=1
                            setdefaultvalue="$dialogout"
                        fi
                    ;;
                    "Remove")
                        fullpath="`getxenfullpath "$current" "$dialogout"`"
                        if $DIALOG --title "$scriptshorttitle - Edit - Remove" --yesno "Do you really want to delete the value $dialogout?" 0 0 
                        then
                            outmsg="`xenstore-rm "$fullpath" 2>&1`"
                            if [ "$?" != "0" ]
                            then
                                $DIALOG --title "$scriptshorttitle - Edit - Remove" --msgbox "$outmsg" 0 0
                            else
                                setdefault=1
                                setdefaultvalue="$dialogout"
                            fi
                        else
                            setdefault=1
                            setdefaultvalue="$dialogout"
                        fi
                    ;;
                esac
            else
                setdefault=1
                setdefaultvalue="$dialogout"
            fi
            ;;
    esac
done

rm -f "$tmpfile"