File: Vgetty.sh

package info (click to toggle)
mgetty 1.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,872 kB
  • sloc: ansic: 42,728; sh: 6,487; perl: 6,262; makefile: 1,457; tcl: 756; lisp: 283
file content (370 lines) | stat: -rw-r--r-- 6,534 bytes parent folder | download | duplicates (13)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# Bash version of Vgetty.pm
#
# Copyright (c)  1999  John Wehle <john@feith.com>.  All rights reserved.
# This package is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
# Derived from:
# 
# $Id: Vgetty.sh,v 1.1 2000/06/11 16:01:44 marcs Exp $
#
# Copyright (c) 1998 Jan "Yenya" Kasprzak <kas@fi.muni.cz>. All rights
# reserved. This package is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#

testing=0
log_file='/var/log/voicelog'

event_names="BONG_TONE|BUSY_TONE|CALL_WAITING|DIAL_TONE|\
	DATA_CALLING_TONE|DATA_OR_FAX_DETECTED|FAX_CALLING_TONE|\
	HANDSET_ON_HOOK|LOOP_BREAK|LOOP_POLARITY_CHANGE|NO_ANSWER|\
	NO_DIAL_TONE|NO_VOICE_ENERGY|RING_DETECTED|RINGBACK_DETECTED|\
	RECEIVED_DTMF|SILENCE_DETECTED|SIT_TONE|TDD_DETECTED|\
	VOICE_DETECTED|UNKNOWN_EVENT"

v_log () {
	case "$testing" in
		0|'') ;;
		*) echo "$*" >>"$log_file"
		   ;;
	esac
}

_received_input=""

# The basic two functions (a low-level interface);
v_receive () {
	local dtmf
	local input
	local var

	while true
	do
		read input <&$VOICE_INPUT
		v_log "received: $input"
		eval "case '$input' in		\
			$event_names) ;;	\
			*) break		\
			   ;;			\
		esac"
		# Handle the event:
		dtmf=''
		case "$input" in
			RECEIVED_DTMF) read dtmf <&"$VOICE_INPUT"
				       v_log "DTMF $dtmf"
				       ;;
		esac
		for var in `set`
		do
			var="${var%%=*}"
			case "$var" in
				EVENT_${input}_*) v_log "Running handler ${var##EVENT_${input}_} for event $input"
						  eval \$$var '"$input"' '"$dtmf"'
						  v_log "Handler ${var##EVENT_${input}_} for event $input finished."
						  ;;
			esac
		done
	done
	_received_input=$input
	return 0
}

v_send () {
	local output

	output="$1"
	echo "$output" >&$VOICE_OUTPUT
	kill -PIPE "$VOICE_PID"
	v_log "v_send: $output"
}

v_expect () {
	local expected
	local received

	v_log "expecting: $*"
	v_receive || return 1
	for expected in "$@"
	do
		if [ "$_received_input" = "$expected" ]
		then
			echo "$_received_input"
			return 0
		fi
	done
	return 1
}

v_waitfor () {
	local string

	string="$1"
	while true
	do
		if v_expect "$string" > /dev/null
		then
			break
		fi
	done
}

v_chat () {
	local cmd
	local receive

	receive=0
	for cmd in "$@"
	do
		receive=$(($receive ^ 1))
		case "$cmd" in
			'') continue
			    ;;
		esac
		case "$receive" in
			1) v_expect "$cmd" > /dev/null || return 1
			   ;;
			*) v_send "$cmd"
			   ;;
		esac
	done
	return 0
}

# Initial chat
v_init () {
	v_chat 'HELLO SHELL' 'HELLO VOICE PROGRAM' 'READY'
}

# Setting the voice device
v_device () {
	local dev

	dev="$1"
	v_log "attempting to set device $dev"
	v_chat '' "DEVICE $dev" 'READY' || return
	DEVICE="$dev"
        v_log "sucessfully set device $dev"
}

v_shutdown () {
	v_chat '' 'GOODBYE' 'GOODBYE SHELL'
}

v_enable_events () {
	v_chat '' 'ENABLE EVENTS' 'READY'
}

v_disable_events () {
	v_chat '' 'DISABLE EVENTS' 'READY'
}

v_beep () {
	local freq
	local len

	freq="$1"
	len="$2"
	v_chat '' "BEEP $freq $len" 'BEEPING'
}

v_dial () {
	local num

	num="$1"
	v_chat '' "DIAL $num" 'DIALING'
}

v_getty () {
	local id

	v_chat '' 'GET TTY' || return 1
	v_receive || return 1
	id="$_received_input"
	v_expect 'READY' > /dev/null || return 1
	echo "$id"
	return 0
}

v_modem_type () {
#	To be implemented in vgetty first.
	return 1
}

v_autostop () {
	local arg

	arg="$1"
	v_chat '' "AUTOSTOP $arg" 'READY'
}

v_play () {
	local file

	file="$1"
	v_chat '' "PLAY $file" 'PLAYING'
}

v_record () {
	local file

	file="$1"
	v_chat '' "RECORD $file" 'RECORDING'
}

v_wait () {
	local sec

	sec="$1"
	v_chat '' "WAIT $sec" 'WAITING'
}

v_stop () {
	v_send 'STOP' # Nechceme READY.
}

v_add_handler () {
	local event
	local func
	local name

	event="$1"
	name="$2"
	func="$3"
	eval "case '$event' in					\
		$event_names) ;;				\
		*) v_log 'v_add_handler: unknown event $event';	\
		   return 1					\
		   ;;						\
	esac"
	eval "EVENT_${event}_${name}"="$func"
	return 0
}

v_del_handler () {
	local event
	local name

	event="$1"
	name="$2"
	eval "case '$event' in					\
		$event_names) ;;				\
		*) v_log 'v_del_handler: unknown event $event';	\
		   return 1					\
		   ;;						\
	esac"
	eval "case \"\${EVENT_${event}_${name}}\" in	\
		'') v_log 'v_del_handler: trying to delete nonexistent handler $name' \
		    ;;					\
		*) unset 'EVENT_${event}_${name}'	\
		   ;;					\
	esac"
	return 0
}

v_play_and_wait () {
	local file

	file="$1"
	v_play "$file"
	v_waitfor 'READY'
}

#####################################################################
# The readnum routine, its private variables and the event handler. #
#####################################################################

_readnum_number=""    # The number itself. Filled in by the event handler.
_readnum_pound=0      # Was the '#' key pressed?
_readnum_recursion=0  # Is the event handler already executing?
_readnum_timeout=10   # The value of the timeout. Filled in by v_readnum.

# Event handler. Just adds key to the $_readnum_number.
_readnum_event () {
	local dtmf
	local input

	input="$1" # Unused. Should be 'RECEIVED_DTMF'.
	dtmf="$2"

	case "$_readnum_pound" in
		1) return
		   ;;
	esac
	case "$dtmf" in
		'#') _readnum_pound=1
		     ;;
		*)   _readnum_number="$_readnum_number$dtmf"
		     ;;
	esac
	case "$_readnum_recursion" in
		0) _readnum_recursion=1
		   v_stop
		   v_waitfor 'READY'
		   case "$_readnum_pound" in
			1) v_log "_readnum_event(): Got #; stopping"
			   v_send "WAIT 0"
			   v_waitfor WAITING
			   return
			   ;;
		   esac
		   v_send "WAIT $_readnum_timeout"
		   v_waitfor WAITING
		   _readnum_recursion=0
		   ;;
	esac
}

v_readnum () {
	local message
	local timeout
	local times

        message="$1"
	timeout="$2"
	times="$3"

	case "$timeout" in
		0|'') timeout=10
		      ;;
	esac
	case "$times" in
		0|'') times=3
		      ;;
	esac

	_readnum_number=""
	_readnum_pound=0
	_readnum_recursion=0
	_readnum_timeout="$timeout"

	# Install the handler.
	v_add_handler 'RECEIVED_DTMF' 'readnum' _readnum_event
	while [ -z "$_readnum_number" -a "$_readnum_pound" -eq 0 ]
	do
		times=$(($times - 1))
		if [ "$times" -eq "-1" ]
		then
			break;
		fi
		v_play_and_wait "$message"
		if [ -n "$_readnum_number" -o "$_readnum_pound" -ne 0 ]
		then
			break
		fi
		v_wait "$_readnum_timeout"
		v_waitfor 'READY'
	done
	v_del_handler 'RECEIVED_DTMF' 'readnum'
	case "$times" in
		-1) return 1
		    ;;
	esac
	echo "$_readnum_number"
	return 0
}

v_log "-----------"
v_log "### Pid $$ opening log"
v_log "----------"
v_init