File: efaxio.c

package info (click to toggle)
efax-gtk 3.2.8-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,688 kB
  • sloc: cpp: 23,990; ansic: 5,252; sh: 4,968; makefile: 149; sed: 16; xml: 7
file content (528 lines) | stat: -rw-r--r-- 14,274 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#include <ctype.h>		/* ANSI C */
#include <signal.h>    
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

#include <config.h>             /* For NLS */
#ifdef ENABLE_NLS
#include <libintl.h>
#include <glib.h>
#endif

#include "efaxio.h"		/* EFAX */
#include "efaxmsg.h"
#include "efaxos.h"
#include "efaxlib.h"

#define MAXRESPB 1024	    /* maximum bytes of modem responses saved */

char *prompts[] = {		/* modem responses that are prompts */
  "OOK", "-CONNECT FAX", "CCONNECT", "NNO CARRIER", "EERROR",
  "NNO DIALTONE", "BBUSY", "NNO ANSWER", "M+FCERROR", "VVCON", 
  "DDATA", 0 } ;

int lockpolldelay = 8000 ;	/* milliseconds between checks of lock files */

			    /* signals to be caught so can hang up phone */
int catch [] = { CATCHSIGS, 0 } ;


/* Modem features */

int c1=0, c20=0 ;		/* use class 1/class 2.0 */
int c2=0 ;			/* force class 2 */
int cmdpause = T_CMD ;		/* delay before each init command */
int vfc = 0 ;			/* virtual flow control */
uchar startchar = DC2 ;	/* character to start reception */

				/* response detector lookup tables */
uchar rd_nexts [ 256 ] = { 0 }, rd_allowed [ 256 ] = { 0 } ;

/* Provide dummy gettext() function if there is no internationalisation support */

#ifndef ENABLE_NLS
static const char *gettext ( const char *text )
{
  return text;
}
#endif

/* Initialize two lookup tables used by a state machine to detect
   modem responses embedded in data.  The first shows which
   characters are allowed in each state.  The second shows which
   characters in each state increment the state.  Each table is
   indexed by character.  The detector sequences through 6 states
   corresponding to sequences of the form: <CR><LF>AX...<CR><LF>
   where A is an upper-case letter and X is an u.c. letter or
   space.  The state values are 01, 02, 04, 08, 10 and 20 (hex)
   and are used to mask in a bit from the tables.  When the state
   reaches 0x20 a modem response has been detected.  With random
   data there is a small O(10^-10) chance of spurious
   detection. */

void rd_init ( void )
{
  int c ;

  rd_nexts[CR] = rd_allowed[CR] = 0x01 | 0x08 ;
  rd_nexts[LF] = rd_allowed[LF] = 0x02 | 0x10 ;
  for ( c='A' ; c<'Z' ; c++ ) {
      rd_allowed[c] = 0x04 | 0x08 ;
      rd_nexts[c] = 0x04 ;
    }
  rd_allowed[' '] = 0x08 ;
}


/* Get a modem response into buffer s, storing up to n bytes.
   The response includes characters from the most recent control
   character until the first LF following some text.  Returns s
   or null if times-out in t deciseconds or on i/o error. Trace
   messages are buffered to reduce possible timing problems. */

char *tgets( TFILE *f, char *s, int len, int t )
{
  int i, n, c ;

  for ( i=n=0 ; 1 ; i++ ) {
    if ( ( c = tgetc ( f, t ) ) == EOF ) break ;
    if ( i == 0 ) msg ( "M-+ .%03d [", time_ms ( ) ) ;
    msg ( "M-+ %s", cname ( c ) ) ;
    if ( n > 0 && c == LF ) break ;
    if ( ! iscntrl ( c ) && n < len ) s[n++] = c ;
  }

  if ( n >= len ) msg ( "W- modem response overflow" ) ;
  s[ n < len ? n : len-1 ] = '\0' ;
  if ( i > 0 ) {
    if ( c == EOF ) msg ( "M- <...%.1f s>]", (float)t/10 ) ;
    else msg ( "M- ]" ) ;
  }

  return c == EOF ? 0 : s ;
}


/* Send bytes to the modem, doing bit-reversal and escaping DLEs.
   Returns 0 or 2 on errors. */

int sendbuf ( TFILE *f, uchar *p, int n, int dcecps )
{
  int err=0, c, over ;
  uchar *order = f->obitorder ;
  uchar buf [ MINWRITE+1 ] ;
  int i ;

  for ( i=0 ; ! err && n > 0 ; n-- ) {
    c  = order [ *p++ ] ;
    if ( c == DLE ) buf[i++] = DLE ;
    buf[i++] = c ;
    if ( i >= MINWRITE || n == 1 ) {

      /* ``virtual'' flow control */

      if ( vfc && dcecps > 0 ) {
	over = f->bytes - ( proc_ms ( ) - f->mstart ) * dcecps / 1000 
	  - MAXDCEBUF ;
	if ( over > 0 ) msleep ( over * 1000 / dcecps ) ;
      }

      if ( tput ( f, buf, i ) < 0 )
	err = msg ( "ES2 %s", gettext ( "fax device write error:" ) ) ;

      i = 0 ;
    }
  }

  return err ;
}


/* Scan responses since giving previous command (by cmd()) for a
   match to string 's' at start of response.  If a matching
   response is found it finds the start of the data field which
   is defined as the next non-space character in the current or
   any subsequent responses. If ip is not null, reads one integer
   (decimal format) into ip. [problem: Class 2.0 status responses
   are in hex.] Returns pointer to start of data field of
   response string or NULL if not found. */

char responses [ MAXRESPB ], *lresponse = responses ;

char *sresponse ( char *s, int *ip )
{
  char *p, *r = 0 ;
  int lens, lenr ;
  
  lens = strlen ( s ) ;
  for ( p=responses ; p<lresponse ; p += strlen(p) + 1 ) {

    if ( ! strncmp ( p, s, lens ) ) {
      r = p + lens ;

      lenr = strlen ( r ) ;
      if ( strspn ( r, " \r\n" ) == lenr && r+lenr < lresponse ) r += lenr ;

      if ( ip && sscanf ( r, "%d", ip ) > 0 )
	msg ( "R read value %d from \"%s\"", *ip, r ) ;
    }
    
  }

  return r ;
}


/* Search for the string s in responses since last command.
   Skips lines beginning with "AT" (command echo) and removes
   trailing spaces.  Returns pointer to the string or NULL if not
   found. */

char *strinresp ( char *s )
{
  char *p, *r = 0 ;

  for ( p=responses ; p<lresponse && !r ; p += strlen(p) + 1 )
    if ( strncmp ( p, "AT", 2 ) )
      r = strstr ( p, s ) ;

  return r ;
}


/* Appends the value of the first response that includes the
   string s to buffer buf of length len.  Skips characters before
   and including "=" in the response and doesn't copy trailing
   spaces.  */

int getresp ( char *s, char *buf, int len )
{
  int err=0, n ;
  char *p, *q ;
  
  if ( ( p = strinresp ( s ) ) ) {
    if ( ( q = strchr ( p, '=' ) ) ) p = q+1 ;
    for ( q = p+strlen(p)-1 ; q>=p && isspace(*q) ; q-- ) ;
    n = q - p + 1 ;
    if ( n + strlen(buf) < len )
      strncat ( buf, p, n ) ;
    else
      strncat ( buf, p, len - strlen(buf) - 1 ) ;
  } else {
    err = 1 ;
  }
  return err ;
}


/* Search for a match to the string s in a null-terminated array of
   possible prefix strings pointed to by p.  The first character of each
   prefix string is skipped.  Returns pointer to the table entry or NULL if
   not found. */

char *strtabmatch ( char **p, char *s )
{
  while ( *p && strncmp ( *p+1, s, strlen ( *p+1 ) ) ) p++ ;
  return ( ! *p || **p == '-' ) ? NULL : *p ;
}


/* Send command to modem and check responses.  All modem commands
   go through this function. Collects pending (unexpected)
   responses and then pauses for inter-command delay (cmdpause)
   if t is negative.  Writes command s to modem if s is not null.
   Reads responses and terminates when a response is one of the
   prompts in prompts[] or if times out in t deciseconds.
   Repeats command if detects a RING response (probable
   collision). Returns the first character of the matching prefix
   string (e.g. 'O' for OK, 'C' for CONNECT, etc.)  or EOF if no
   such response was received within timeout t. */

int cmd ( TFILE *f, char *s, int t )
{
  char buf [ CMDBUFSIZE ], *p = "" ;
  int resplen=0, pause=0 ;
  char *message ;

  if ( t < 0 ) {
    pause = cmdpause ;
    t = -t ;
  }

  lresponse = responses ;

  retry:

  if ( s ) { 

    while ( tgets ( f, buf, CMDBUFSIZE, pause ) ) {
      /* Translator: the %s format item refers to the text of a modem response */
      message = strdup2 ( "W- ", gettext ( "unexpected response \"%s\"" ) ) ;
      if ( message ) {
	msg ( message, buf ) ;
	free ( message ) ;
      }
    }

    msg ( "C- command  \"%s\"", s ) ;

    if ( strlen(s) >= CMDBUFSIZE-4 ) {
      message = strdup2 ( "E ", gettext ( "modem command \"%s\" too long" ) ) ;
      if ( message ) {
	msg ( message, s ) ;
	free ( message ) ;
      }
    } else {
      sprintf ( buf, "AT%s\r", s ) ;
      tput ( f, (uchar*) buf, strlen(buf) ) ;
    }
  }

  if ( t ) {
    
    msg ( "C- waiting %.1f s", ((float) t)/10 ) ;

    while ( ( p = tgets ( f, buf, CMDBUFSIZE, t ) ) ) {

      if ( ( resplen += strlen ( buf ) + 1 ) <= MAXRESPB ) {
	strcpy ( lresponse, buf ) ;
	lresponse += strlen ( buf ) + 1 ;
      }
      
      if ( ( p = strtabmatch ( (char**) prompts, buf ) ) ) {
	msg ( "C- response \"%s\"", buf ) ;
	break ;
      }
      
      if ( ! strcmp ( buf, "RING" ) ) { msleep ( 100 ) ; goto retry ; }
    }
  }

  return p ? *p : EOF ;
}


/* Send command to modem and wait for reply after testing (and
   possibly setting) current error status via err
   pointer. Returns 0 if err is already set, command response, or
   EOF on timeout. */

int ckcmd ( TFILE *f, int *err, char *s, int t, int r )
{
  int c=0 ;
  char *message ;

  if ( ( ! err || ! *err ) && ( c = cmd ( f, s, t ) ) != r && r ) {
    if ( c == EOF ) {
      if ( s ) {
	message = strdup2 ( err ? "E " : "W ", gettext ( "timed out after command: %s" ) ) ;
	if ( message ) {
	  msg ( message, s ) ;
	  free ( message ) ;
	}
      } else {
	msg ( err ? "E %s" : "W %s", gettext ( "timed out after waiting" ) ) ;
      }
    } else {
      if ( s ) {
	message = strdup2 ( err ? "E " : "W ", gettext ( "wrong response after command: %s" ) ) ;
	if ( message ) {
	  msg ( message, s ) ;
	  free ( message ) ;
	}
      } else {
	msg ( err ? "E %s" : "W %s", gettext ( "wrong response after waiting" ) ) ;
      }
    }

    if ( err ) *err = 3 ;
  }
  return c ;
}


/* Resynchronize modem from an unknown state.  If no immediate
   response, try pulsing DTR low (needs &D{2,3,4}), and then
   cancelling data or fax data modes.  In each case, discards any
   responses for about 2 seconds and then tries command ATQ0V1 to
   enable text responses.  Returns 0 if OK or 4 if no response.
   */

int modemsync ( TFILE *f )
{
  int err=0, method=0 ;

  for ( method=0 ; ! err ; method++ ) {
    switch ( method ) {
    case 0 : 
      break ;
    case 1 :
      ttymode ( f, VOICECOMMAND ) ;
      break ;
    case 2 : 
      msg ("I %s", gettext ( "sync: dropping DTR" ) ) ;
      ttymode ( f, COMMAND ) ; msleep ( 200 ) ;
      ttymode ( f, DROPDTR ) ; msleep ( 200 ) ;
      ttymode ( f, COMMAND ) ; 
      break ;
    case 3 :
      msg ("I %s", gettext ( "sync: sending escapes" ) ) ;
      ttymode ( f, VOICECOMMAND ) ;
      tput ( f, (uchar*) CAN_STR, 1 ) ;
      tput ( f, (uchar*) DLE_ETX, 2 ) ; 
      msleep ( 100 ) ;
      ttymode ( f, COMMAND ) ;
      tput ( f, (uchar*) CAN_STR, 1 ) ;
      tput ( f, (uchar*) DLE_ETX, 2 ) ; 
      msleep ( 1500 ) ;
      tput ( f, (uchar*) "+++", 3 ) ; 
      break ;
    case 4 :
      err = msg ("E4 %s", gettext ( "sync: modem not responding" ) ) ;
      continue ;
    }
    while ( method && cmd ( f, 0, 20 ) != EOF ) ;
    if ( cmd ( f, "Q0V1", -20 ) == OK ) break ;
  }
  return err ;
} 


/* Set up modem by sending initialization/reset commands.
   Accepts either OK or CONNECT responses. Optionally changes
   baud rate if a command begins with a number. Returns 0 if OK,
   3 on errors. */

int setup ( TFILE *f, char **cmds, int ignerr )
{
  int err=0 ;
  char c, *message ;

  for ( ; ! err && *cmds ; cmds++ ) {
#if 0
    if ( *cmds && isdigit( (unsigned char) **cmds ) ) {
      
    }
#endif
    if ( ( c = cmd ( f, *cmds, -TO_RESET ) ) != OK && c !=  VCONNECT && 
	! ignerr ) {
      if ( *cmds ) {
	/* Translator: the %s format item refers to the text of the failed command */
	message = strdup2 ( "E3 ", gettext ( "modem command (%s) failed" ) ) ;
	if ( message ) {
	  err = msg ( message, *cmds ) ;
	  free ( message ) ;
	}
      } else {
	err = msg ( "E3 %s", gettext ( "modem command failed" ) ) ;
      }
    }
  }

  return err ;
}


/* Terminate session.  Makes sure modem is responding, sends
   modem reset commands or hang-up command if none, removes lock
   files. Returns 0 if OK, 3 on error.*/

int end_session ( TFILE *f, char **zcmd,  char **lkfile, int sync )
{
  int err = 0 ;

  if ( f && sync ) 
    err = modemsync ( f ) ;
  if ( f && zcmd && ! err && sync ) 
    err = setup ( f, zcmd, 0 ) ;
  if ( f )
    ttymode ( f, ORIGINAL ) ;
  if ( lkfile )
    unlockall ( lkfile ) ;
  return err ;
} 
    

/* Initialize session.  Try locking and opening fax device until opened or
   get error. Then set tty modes, register signal handler, set up
   modem. Returns 0 if OK, 2 on errors, 3 if initialization failed, 4 if no
   modem response. */

int begin_session ( TFILE *f, char *fname, int reverse, int hwfc, 
		    char **lkfile, ttymodes mode, void (*onsig) (int) )
{
  int i, err=0, busy=0, minbusy=0 ;
  char *message ;

#ifdef ENABLE_NLS
  gsize written = 0 ;
  char *conv_fname ;
#endif

  do {
    err = lockall ( lkfile, busy >= minbusy ) ;
    if ( ! err ) err = ttyopen ( f, fname, reverse, hwfc ) ;
    if ( err == 1 ) { 
      if ( busy++ >= minbusy ) {
	/* Translator: the %s formatting item refers to the device name which is locked or busy */
	message = strdup2 ( "W ", gettext ( "%s locked or busy - waiting" ) ) ;
	if ( message ) {
#ifdef ENABLE_NLS
	  if ( use_utf8 && !g_utf8_validate ( fname, -1, NULL ) ) {
	    conv_fname = g_filename_to_utf8 ( fname, -1, NULL, &written, NULL ) ;
	    if ( conv_fname ) {
	      msg ( message, conv_fname ) ;
	      g_free ( conv_fname ) ;
	    }
	  } else {
	    msg ( message, fname ) ;
	  }
#else
	  msg ( message, fname ) ;
#endif
	  free ( message ) ;
	}
	minbusy = minbusy ? minbusy*2 : 1 ;
      }
      msleep ( lockpolldelay ) ;
    }
  } while ( err == 1 ) ;
  
  if ( ! err ) {
    /* Translator: the %s formatting item refers to the device name which has been opened */
    message = strdup2 ( "I ", gettext ( "opened %s" ) ) ;
    if ( message ) {
#ifdef ENABLE_NLS
      if ( use_utf8 && !g_utf8_validate ( fname, -1, NULL ) ) {
	conv_fname = g_filename_to_utf8 ( fname, -1, NULL, &written, NULL ) ;
	if ( conv_fname ) {
	  msg ( message, conv_fname ) ;
	  g_free ( conv_fname ) ;
	}
      } else {
	msg ( message, fname ) ;
      }
#else
      msg ( message, fname ) ;
#endif
      free ( message ) ;
    }
  }
  
  if ( ! err ) err = ttymode ( f, mode ) ;

  if ( ! err ) {
    rd_init ( ) ;
    f->rd_state = RD_BEGIN ;
  }
  
  for ( i=0 ; ! err && catch [ i ] ; i++ ) 
    if ( signal ( catch [ i ], onsig ) == SIG_ERR ) 
      err = msg ( "ES2can't set signal %d handler:", catch [ i ] ) ;
  
  if ( !err ) err = modemsync ( f ) ;

  return err ;
}