File: do_chat.c

package info (click to toggle)
mgetty 1.2.1-6
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,880 kB
  • sloc: ansic: 42,794; perl: 6,262; sh: 4,934; makefile: 1,506; tcl: 756; lisp: 283
file content (236 lines) | stat: -rw-r--r-- 5,009 bytes parent folder | download | duplicates (6)
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
#ident "$Id: do_chat.c,v 4.4 2014/02/02 08:32:50 gert Exp $ Copyright (c) Gert Doering"

/* do_chat.c
 *
 * This module handles all the non-fax talk with the modem
 */

#include <stdio.h>
#include "syslibs.h"
#include <unistd.h>
#include <signal.h>
#include <string.h>
#ifndef sunos4
#include <sys/ioctl.h>
#endif

#ifndef EINTR
#include <errno.h>
#endif

#include "mgetty.h"
#include "policy.h"
#include "tio.h"

boolean chat_has_timeout;
static RETSIGTYPE chat_timeout(SIG_HDLR_ARGS)
{
    chat_has_timeout = TRUE;
}

extern boolean virtual_ring;

/* send one string to "fd", honouring \c, \d and \\ */

int do_chat_send _P2( (fd, p), int fd, char * p )
{
    boolean nocr = FALSE;		/* do not set CR/LF (\c) */

    /* before sending, maybe we have to pause a little */
#ifdef DO_CHAT_SEND_DELAY
    delay(DO_CHAT_SEND_DELAY);
#endif
	
    lprintf( L_MESG, "send: " );

    while ( *p != 0 ) 
    {
	if ( *p == '\\' )		/* special stuff */
	{
	    switch ( *(++p) )
	    {
	      case 'd': lputs( L_MESG, "\\d"); delay(500); break;
	      case 'c': nocr = TRUE; break;
	      default:
		write( fd, p, 1 );
		lputc( L_MESG, *p );
	    }
	}
	else
	{
	    if ( write( fd, p, 1 ) != 1 )
	    {
		lprintf( L_ERROR, "do_chat: can't write to modem!" );
		return ERROR;
	    }
	    lputc( L_MESG, *p );
	}
	p++;
    }

    if ( ! nocr )
    {
	write( fd, MODEM_CMD_SUFFIX, sizeof(MODEM_CMD_SUFFIX)-1 );
	p = MODEM_CMD_SUFFIX;
	while ( *p ) lputc( L_MESG, *(p++) );
    }

    return NOERROR;
}


int do_chat _P6((fd, expect_send, actions, action, chat_timeout_time,
		timeout_first ),
		int fd, char * expect_send[],
	        chat_action_t actions[], action_t * action,
                int chat_timeout_time, boolean timeout_first )
{
#define BUFFERSIZE 500
char	buffer[BUFFERSIZE];
int	i,cnt;
int	retcode = SUCCESS;
int	str;
int	h;
TIO	tio, save_tio;
#define	LSIZE	100
static	char	lbuf[LSIZE];
static	char	*lptr = lbuf;

    tio_get( fd, &tio );
    save_tio = tio;
    tio_mode_raw( &tio );
    tio_set( fd, &tio );

    signal( SIGALRM, chat_timeout );

    /* default "action" is timeout */
    if ( actions != NULL && action != NULL ) *action = A_TIMOUT;

    str=0;
    while ( expect_send[str] != NULL )
    {
	/* expect a string (expect_send[str] or abort[]) */
	i = 0;

	if ( strlen( expect_send[str] ) != 0 )
	{
	    lprintf( L_MESG, "waiting for ``%s''", expect_send[str] );

	    lprintf( L_NOISE, "got: " );

	    chat_has_timeout = FALSE;

	    /* set alarm timer. for the first string, the timer is only
	       set if the flag "timeout_first" is true */

	    if ( str != 0 || timeout_first )
		alarm( chat_timeout_time );
	    else
		alarm( 0 );

	    do
	    {
		if ( virtual_ring &&
		     strncmp( expect_send[str], "RING", 4 ) == 0 )
		{
		    lputs( L_MESG, " ``found''" );
		    break;
		}
		
		cnt = read( fd, &buffer[i], 1 );

		if ( cnt < 0 )
		{
		    if ( errno == EINTR ) cnt = 0;	/* signal */
		    else
		    {					/* unsp. error */
		        lprintf( L_ERROR, "do_chat: error in read()");
			retcode = FAIL;
			break;				/* -> abort */
		    }
		}

		if ( chat_has_timeout )		/* timeout */
		{
		    lprintf( L_WARN, "timeout in chat script, waiting for `%s'", expect_send[str] );
		    retcode = FAIL;
		    break;
		}

		if ( cnt > 0 )
		{
		    lputc( L_NOISE, buffer[i] );

		    /* build full lines, feed them to caller-id / connect
		     * string parsing routine in cnd.c
		     */
		    if ( buffer[i] == '\r' || buffer[i] == '\n' ||
			 (lptr >= lbuf+LSIZE-3) )
		    {
			*lptr = '\0';
			if (lbuf[0])
			    cndfind(lbuf);
			lptr = lbuf;
		    }
		    else
			*lptr++ = buffer[i];
		}

		i += cnt;
		if ( i>BUFFERSIZE-5 )	/* buffer full -> junk oldest stuff*/
		{
		    memcpy( &buffer[0], &buffer[BUFFERSIZE/2], i-BUFFERSIZE/2+1 );
		    i-=BUFFERSIZE/2;
		}

		/* look for the "expect"-string */

		cnt = strlen( expect_send[str] );
		if ( i >= cnt &&
		     memcmp( &buffer[i-cnt], expect_send[str], cnt ) == 0 )
		{
		    lputs( L_MESG, " ** found **" );
		    break;
		}

		/* look for one of the "abort"-strings */
                if ( actions != NULL && action != NULL )
		  for ( h=0; actions[h].expect != NULL; h ++ )
		{
		    cnt = strlen( actions[h].expect );
		    if ( i>=cnt && 
			 memcmp( &buffer[i-cnt], actions[h].expect, cnt ) == 0 )
		    {
			lprintf( L_MESG,"found action string: ``%s''",
			                actions[h].expect );
			*action = actions[h].action;
			retcode = FAIL;
			break;
		    }
		}
	    }
	    while ( i<BUFFERSIZE && retcode != FAIL );

	    /* disable timeout alarm clock */
	    alarm(0);

	    /* found abort string or timeout? */
	    if ( retcode == FAIL ) break;

	}		/* end if (strlen(expect_send[str] != 0) */

	str++;
	/* end of list? */

	if ( expect_send[str] == NULL ) break;

	/* send a string, translate (a few) esc-sequences */
	do_chat_send(fd,  expect_send[str++] );
	
    }				/* end while ( expect_send[str] != NULL ) */

    /* reset terminal settings */
    tio_set( fd, &save_tio );

    return retcode;
}