Wednesday, November 5, 2008

Freebsd Hw3_3

Requirement

Use the tool dialog(1) to create required graphical user interface
(GUI) program. Your program must contain the items listed below:

GUI lists:

a. Center GUI:

Control all GUIs. It has some options as following:

Options list:

Wikipedia: If you choose this option, it will invoke the Wiki GUI.

Dictionary:If you choose this option, it will invoke the Dict GUI.

Exit: If you choose this option, it will shutdown this Center GUI.

b. Wiki GUI:

Read the input string.

Search the string from http://wikipedia.org/, and show the result web pages.

(hint: just use "w3m" or other text web browsers)

When Wiki GUI shutdown, it will return to Center GUI.

c. Dict GUI:

Read the input string.

connect to google dictionary, look up the string, and finally show the data on the Data GUI.

http://www.google.com.tw/dictionary?hl=en

d. Data GUI:

Show the data coming from the google dictionary.

(just show the first definition or the shortest definition)

When Data GUI shutdown, it will return to Center GUI.

Solution:



#!/bin/sh

while (true)
do
DIALOG=${DIALOG=/usr/bin/dialog}
$DIALOG --clear --title "Problem 3 GUI" \
--hline "Press Up/Down,first letter or Enter" \
--menu "Choose the program you want:" -1 -1 3 \
"Wiki" "Search Wiki Through The Internet" \
"Dict" "Using Google On-line Dictionary" \
"Exit" "Exit GUI" 2> /tmp/menu.tmp.$$

retval=$?
choice=`cat /tmp/menu.tmp.$$`
rm -f /tmp/menu.tmp.$$

case $retval in
0)
#echo "'$choice' chosen."
case $choice in
Wiki)
echo "WIKI"

wikiDIALOG=${DIALOG=/usr/bin/dialog}

$wikiDIALOG --title "Wiki GUI" --clear \
--inputbox "Hi, you can search input keyword through wiki. \n\
Try input keyword below:" -1 -1 "" 2> /tmp/inputbox.tmp.$$

wikiretval=$?

input=`cat /tmp/inputbox.tmp.$$`
rm -f /tmp/inputbox.tmp.$$

case $wikiretval in
0)
echo "Input string is '$input'"
lynx http://wikipedia.org/wiki/Special:Search?search=$input
;;
1)
echo "Cancel pressed."

;;
255)
[ -z "$input" ] || echo $input ;
echo "ESC pressed."
;;
esac

;;

Dict)
echo "Dict is selected."
dictDIALOG=${DIALOG=/usr/bin/dialog}

$dictDIALOG --title "Dict GUI" --clear \
--inputbox "Hi, you can search meaning of input word . \n\
Try input keyword below:" -1 -1 "" 2> /tmp/dictinputbox.tmp.$$

dictretval=$?

dictinput=`cat /tmp/dictinputbox.tmp.$$`
rm -f /tmp/dictinputbox.tmp.$$

case $dictretval in
0)
echo "Input string is '$dictinput'"

# ==== call messageBox ==== #
msgDIALOG=${DIALOG=/usr/bin/dialog}

# ===== Search the net ===== #
curl -s "http://www.google.com.tw/dictionary?aq=f&langpair=en|en&q=$dictinput&hl=en" | \
sed -n '/Web definitions/,$p' | \
sed -n '6p' | \
sed -e :a -e 's/<[^>]*>//g;/ tmpdict

dictInfo=$(cat tmpdict)
$msgDIALOG --title "DATA GUI" --clear \
--msgbox "$dictinput : $dictInfo" -1 -1

case $? in
0)
echo "OK";;

255)
echo "ESC pressed.";;
esac
;;
1)
echo "Cancel pressed."
;;
255)
[ -z "$dictinput" ] || echo $dictinput ;
echo "ESC pressed."
;;
esac
;;

Exit)
#echo "Exit"
break
;;
esac
;;
1)
echo "Cancel pressed."
break
;;

255)
[ -z "$choice" ] || echo $choice ;
echo "ESC pressed."
break
;;
esac
done

0 comments:

Post a Comment