File: Xmessage

package info (click to toggle)
xdialog 2.0.6-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,616 kB
  • ctags: 1,064
  • sloc: ansic: 8,505; sh: 4,202; yacc: 318; makefile: 80
file content (148 lines) | stat: -rwxr-xr-x 3,917 bytes parent folder | download | duplicates (2)
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
#!/bin/sh

# This is a sample wrapper allowing to emulate the "xmessage" utility by
# using "Xdialog" in the closest possible way. Note that when the "-buttons"
# option is passed to this wrapper, "xmessage" is actually invoked as there
# is (currently) no way to emulate exactly this option with Xdialog...

OPTIONS="$*"
BUTTONS=""
FILE=""
TITLE="Xmessage"
WMCLASS="xmessage"
XSIZE=80
YSIZE=40
PLACEMENT="--auto-placement"
# It looks like "xmessage" does not like non-monospacing fonts and refuses
# any "-fn" option with proportional fonts as parameter. We will not have to
# worry about parsing the X "-fn" option then...
FONT="--fixed-font"
TIMEOUT=""

if [ "$1" == "" ] ; then
	echo "usage: Xmessage [-options] [message]

where options include:
  -file filename			file to read message from, \"-\" for stdin
  -center				pop up at center of screen
  -nearmouse				pop up near the mouse cursor
  -timeout secs				exit with status 0 after "secs" seconds

as well as folowing X generic options (the X options not listed are ignored):
  -geometry WIDTHxHEIGHT[+X0+Y0]	size and origin for Xmessage window
  -name ressource_name			sets the ressource name for Xmessage
					(defaults to \"xmessage\")
  -title string				title for the Xmessage window
"
	exit 0
fi

for i in $OPTIONS ; do

	case $1 in
		-file)
			shift 1
			FILE="$1"
			;;
		-center)
			PLACEMENT="--screen-center"
			;;
		-nearmouse)
			PLACEMENT="--under-mouse"
			;;
		-timeout)
			shift 1
			TIMEOUT="$1"
			;;
		-buttons|-default)
			shift 1
			BUTTONS="true"
			;;
		-print)
			BUTTONS="true"
			;;
		-geometry)
			shift 1
			YSIZE="$1"
			XSIZE=""
			;;
		-title)
			shift 1
			TITLE="$1"
			;;
		-name)
			shift 1
			WMCLASS="$1"
			;;
		-display|-bg|-background|-bd|-bordercolor|-bw|-borderwidth|-fg|-foreground|-fn|-font|-xnllanguage|-xrm)
			echo "Xmessage: '$1' option ignored." 1>&2
			shift 1
			;;
		-iconic|-rv|-reverse|+rv|-selectionTimeout|-synchronous)
			echo "Xmessage: '$1' option ignored." 1>&2
			;;
		*)
			if [ "$1" != "" ] ; then
				TEXT="$1"
			fi
			;;
	esac

	shift 1
done

# We cannot emulate the "-buttons" option, so give up and use the true "xmessage"
# if this option was specified in the command line...
if [ "$BUTTONS" == "true" ] ; then
	echo "Buttons related options (-buttons, -default, -print) not supported,"
	echo "invoking \"xmessage\" intead..." 1>&2
	xmessage "$OPTIONS"
	exit $?
fi

# Now use "Xdialog"...
if [ "$TIMEOUT" != "" ] ; then
	# If a "-timeout" option was passed, then we must emulate it because
	# only infoboxes use a timeout in Xdialog and we will use here either
	# the textbox or the tailbox.
	#
	# So first start Xdialog as an asynchronous process...
	if [ "$FILE" == "" ] ; then
		echo "$TEXT" | \
		Xdialog --title "$TITLE" --wmclass "$WMCLASS" $PLACEMENT $FONT --no-cancel \
			--tailbox "-" $YSIZE $XSIZE &
	else
		Xdialog --title "$TITLE" --wmclass "$WMCLASS" $PLACEMENT $FONT --no-cancel \
			--textbox "$FILE" $YSIZE $XSIZE &
	fi
	# Get Xdialog PID and build the source for the awk command that
	# will be used to see if Xdialog is still running.
	XDIALOG_PID=$!
	SOURCE="{ if ( \$1 == $XDIALOG_PID ) print \$1 }"
	# Now, as long as the TIMEOUT is not 0, sleep for one second, check
	# for Xdialog still being there (if not then exit immediately) and
	# decrement the TIMEOUT.
	while (( $TIMEOUT > 0 )) ; do
		sleep 1
		STILL_THERE=`ps | awk --source "$SOURCE"`
		if [ "$STILL_THERE" == "" ] ; then
			exit 0
		fi
		let TIMEOUT=$TIMEOUT-1
	done
	# Time is over !   Kill Xdialog and exit.
	kill $XDIALOG_PID 2>/dev/null
	exit 0
else
	# No timeout, just start Xdialog synchronously then...
	if [ "$FILE" == "" ] ; then
		echo "$TEXT" | \
		Xdialog --title "$TITLE" --wmclass "$WMCLASS" $PLACEMENT $FONT --no-cancel \
			--tailbox "-" $YSIZE $XSIZE
	else
		Xdialog --title "$TITLE" --wmclass "$WMCLASS" $PLACEMENT $FONT --no-cancel \
			--textbox "$FILE" $YSIZE $XSIZE
	fi
fi

exit $?