File: viewglob.in

package info (click to toggle)
viewglob 2.0.4-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,740 kB
  • ctags: 968
  • sloc: ansic: 13,940; sh: 897; makefile: 315
file content (317 lines) | stat: -rw-r--r-- 7,625 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#! @BASH_FULL_PATH@
# Copyright (C) 2004, 2005 Stephen Bach
# This is script is a wrapper interface to the Viewglob package.
#
# Viewglob is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Viewglob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Viewglob; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

SCRIPT_NAME="${0##*/}"
VG_LIB_DIR="@pkglibdir@"
VGPING="$VG_LIB_DIR/vgping"

HOST_DEFAULT=localhost
PORT_DEFAULT=16108

# Execution configuration
REMOTE=
VGSEER_OPTS=
VGSEER_EXEC=vgseer
VGD_OPTS=

# We need extended globbing.
shopt -s extglob

vg_usage() {
	cat >&2 << EOF
usage: viewglob [-h <host>] [-p <port>] [-c <shell mode>] [-d <display>]
                [-s <sort style>] [-r <dir ordering>] [-t <on/off>]
                [-i <on/off>] [-z <font size modifier>]

-h, --host                  Host to connect to.            [localhost]
-p, --port                  Port to use or connect to.     [16108]
-c, --shell-mode            Shell to use (bash or zsh).    [bash]
-d, --display               Display program.               [vgmini]
-s, --sort-style            Windows (dirs first) or ls.    [ls]
-r, --dir-order             Directory list ordering.       [ascending]

-t, --shell-star            Asterisk character at prompt.  [on]
-i, --file-icons            File type icons in display.    [on]

-z, --font-size-modifier    Increase/decrease display font size.

-h, --help                  This usage.
-V, --version               Print the version.
EOF

	exit 0
}

vg_error() {
	echo "$SCRIPT_NAME: $1" >&2
	exit $2
}

vg_version() {
cat << EOF
@PACKAGE_NAME@ @PACKAGE_VERSION@
Released @RELEASE_DATE@
EOF
	exit 0
}


# Option verification functions

vg_test_shell_mode() {
	if [ "$1" != bash ] && [ "$1" != zsh ]
		then vg_error "Shell mode must be \"bash\" or \"zsh\"" 1
	fi
}

vg_test_display() {
	if [ ! "$1" ]
		then vg_error "No display program specified" 1
	elif [ "$1" = vgmini ] || [ "$1" = vgclassic ]
		then vg_disp="$VG_LIB_DIR/$1"
	else
		vg_disp="$1"
	fi

	if [ ! -x "$vg_disp" ]
		then vg_error "$1 is not present or not executable" 1
	fi
}

vg_test_sort_style() {
	if [ "$1" != ls ] && [ "$1" != win ]
		then vg_error "Unknown sort style \"$1\"" 1
	fi
}

vg_test_dir_order() {
	if [ "$1" != descending ] && \
	   [ "$1" != ascending ] && \
	   [ "$1" != ascending-pwd-first ]
		then vg_error "Invalid directory ordering \"$1\"" 1
	fi
}

vg_test_font_size_modifier() {
	if [[ "$1" != ?([-+])*([0-9]) ]]
		then vg_error "Invalid font size modifier" 1
	fi
}

vg_test_onoff() {
	if [[ "$OPTARG" != on ]] && [[ "$OPTARG" != off ]]
		then vg_error "Argument must be \"on\" or \"off\"" 1
	fi
}


if [[ "$VG_VIEWGLOB_ACTIVE" = yep ]]
	then vg_error "This shell is already being monitored" 2
elif [[ -z "$DISPLAY" ]]
	then REMOTE=yep
elif [[ -z "$TERM" ]] || [[ "$TERM" = dumb ]]; then
	# There's no terminal associated with the shell, but an X display is
	# present, so make a new xterm.  Try to use gconf-tool2 to determine
	# a usable terminal program.

	GCONF='gconftool-2'
	if [ `which ${GCONF} 2>/dev/null` ]; then
		TERM_EXEC="`${GCONF} -g \
			/desktop/gnome/applications/terminal/exec \
			2>/dev/null`"
		TERM_EXEC_ARG="`${GCONF} -g \
			/desktop/gnome/applications/terminal/exec_arg \
			2>/dev/null`"
	fi

	if [ -z "$TERM_EXEC" ] || [ -z "$TERM_EXEC_ARG" ]; then
		# Fallback to xterm.
		TERM_EXEC=xterm
		TERM_EXEC_ARG=-e
	fi

	VGSEER_EXEC="$TERM_EXEC $TERM_EXEC_ARG $VGSEER_EXEC"
fi

# This is an excellent script to replace bash's getopts with one which allows
# long names.  It's written by Grigoriy Strokin.
. $VG_LIB_DIR/getopt.sh

# Parse the options.
while getoptex "h: p: i: c: d: r: s: t: H; v; V; z: host: port: shell-mode: display: font-size-modifier: sort-style: dir-order: shell-star: file-icons: help; version;" "$@"
	do case "$OPTOPT" in

		h|host)
			REMOTE=yep
			HOST="$OPTARG"
			VGSEER_OPTS="$VGSEER_OPTS --host=$OPTARG"
			;;

		p|port)
			VGSEER_OPTS="$VGSEER_OPTS --port=$OPTARG"
			PORT="$OPTARG"
			VGD_OPTS="$VGD_OPTS --port=$OPTARG"
			;;

		i|file-icons)
			vg_test_onoff "$OPTARG"
			VGD_OPTS="$VGD_OPTS --file-icons $OPTARG"
			;;

		c|shell-mode)
			vg_test_shell_mode "$OPTARG"
			VGSEER_OPTS="$VGSEER_OPTS --shell-mode=$OPTARG"
			;;

		d|display)
			vg_test_display "$OPTARG"
			VGD_OPTS="$VGD_OPTS --display=$OPTARG"
			;;

		s|sort-style)
			[ "$OPTARG" == windows ] && OPTARG=win
			vg_test_sort_style "$OPTARG"
			VGD_OPTS="$VGD_OPTS --sort-style=$OPTARG"
			;;

		r|dir-order)
			vg_test_dir_order "$OPTARG"
			VGD_OPTS="$VGD_OPTS --dir-order=$OPTARG"
			;;

		t|shell-star)
			vg_test_onoff "$OPTARG"
			VGSEER_OPTS="$VGSEER_OPTS --shell-star $OPTARG"
			;;

		z|font-size-modifier)
			vg_test_font_size_modifier "$OPTARG"
			VGD_OPTS="$VGD_OPTS --font-size-modifier=$OPTARG"
			;;

		H|help) vg_usage ;;

		v|V|version) vg_version ;;

		*) vg_error "invalid argument" 3 ;;
	esac
done
shift $((OPTIND - 1))

if [ "$OPTOPT" = \? ]
	# getopt.sh already emitted an error message.
	then exit 5
fi

# Just in case the user has turned daemonizing off in vgd.conf.
VGD_OPTS="$VGD_OPTS --daemon=on"

if [ -z "${HOST}${PORT}" ]; then
	# A local connection is assumed.

	VGSEER_OPTS="$VGSEER_OPTS --unix-socket=on"
	
	vgd_ports=
	nvgds=0

	# Create list of active sockets, and clean out inactive ones (if any).
	for file in $HOME/.viewglob/.*; do
		if [[ $file == */.+([0-9]) ]]; then
			if $VGPING dummy ${file##*/}; then
				vgd_ports="$vgd_ports ${file##*/.}"
				nvgds=`expr $nvgds + 1`
			else
				rm -f $file
			fi
		fi
	done

	if [ $nvgds -eq 0 ]; then
		# There are no active vgds.

		# Find an unused port for vgd and create new vgd and vgseer
		# processes.
		port=$PORT_DEFAULT
		while true; do

			while true; do
			    if $VGPING localhost $port
				then port=`expr $port + 1`
			    else
				break
			    fi
			done

			vgd $VGD_OPTS --port $port

			case "$?" in
				0)
					exec $VGSEER_EXEC $VGSEER_OPTS \
						--port $port ;;
				2)
					# Socket error -- try another port.
					continue ;;
				*)
					# Other error -- give up.
					break ;;
			esac
		done

	elif [ $nvgds -eq 1 ]; then
		# There is a single active vgd.
		exec $VGSEER_EXEC $VGSEER_OPTS --port $vgd_ports
	else
		echo "There are active vgds on the following local ports:"
		for port in $vgd_ports; do
			echo "	$port"
		done
	fi
else
	test -z "$HOST" && HOST=$HOST_DEFAULT
	test -z "$PORT" && PORT=$PORT_DEFAULT

	if [ "$REMOTE" = yep ]; then
		# We're running in a displayless environment or the user
		# specified a host to connect to.  Either case implies we're
		# not connecting locally, and so we shouldn't create a vgd.

		VGSEER_OPTS="$VGSEER_OPTS --unix-socket=off"

		if ! $VGPING $HOST $PORT ; then
			vg_error "Couldn't handshake with a vgd at \
				${HOST}:${PORT}" 6
		fi

		exec $VGSEER_EXEC $VGSEER_OPTS --unix-socket=off

	else
		VGSEER_OPTS="$VGSEER_OPTS --unix-socket=on"
		
		# First check for an existing vgd.  If there is one, connect
		# to it.
		if $VGPING $HOST .$PORT ; then
			exec $VGSEER_EXEC $VGSEER_OPTS
		fi

		# We'll have to setup a new vgd, then exec.
		if vgd $VGD_OPTS ; then
			exec $VGSEER_EXEC $VGSEER_OPTS
		fi
	fi
fi