File: ltest.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 (159 lines) | stat: -rw-r--r-- 3,730 bytes parent folder | download | duplicates (12)
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
#ident "$Id: ltest.c,v 1.6 2002/11/23 20:35:50 gert Exp $ Copyright (c) Gert Doering"

/* ltest.c
 *
 * show status of all the RS232 lines (RTS, CTS, ...)
 * Calls routines in io.c, tio.c
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>

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

char * Device;
int delay_time = 0;			/* in milliseconds, 0 = one-shot */

/* we don't want logging here */
#ifdef USE_VARARGS
int lprintf() { return 0; }
#else
int lprintf(int level, const char *format, ...) { return 0; }
#endif
int lputs( int level, char * string ) { return 0; }

int main( int argc, char ** argv )
{
int opt, fd, f, last_f;
time_t ti;
struct tm * tm;
boolean	opt_delta = FALSE;		/* show only deltas */
boolean opt_keyboard = FALSE;		/* read commands from keyboard */
TIO tio, save_tio;			/* for stdin */

    while ((opt = getopt(argc, argv, "i:m:dk")) != EOF)
    {
	switch( opt )
	{
	    case 'i': delay_time = 1000 * atoi(optarg); break;	/* secs */
	    case 'm': delay_time = atoi(optarg); break;		/* msecs */
	    case 'd': opt_delta = TRUE; break;
	    case 'k': opt_keyboard = TRUE; break;
	    default:
		fprintf( stderr, "Valid options: -i <seconds-delay>, -m <msec-delay>, -d, -k\n" ); exit(7);
	}
    }

    if ( optind < argc )		/* argument == tty to use */
    {
	Device = argv[optind++];
	fd = open( Device, O_RDONLY | O_NDELAY );

	if ( fd < 0 )
		{ perror( "Opening device failed" ); exit(17); }

	fcntl( fd, F_SETFL, O_RDONLY );
    }
    else				/* default: use stdin */
    {
	Device = "stdin"; fd = fileno(stdin);
    }

    /* if input from keyboard allowed, set stdin to "raw"
     */
    if ( opt_keyboard )
    {
	if ( fd == fileno(stdin) )
	{
	    fprintf( stderr, "can't read modem + keyboard data from stdin\n");
	    exit(7);
	}
	if ( tio_get( 0, &tio ) == ERROR )
	{
	    fprintf( stderr, "can't read termios settings for keyboard\n" );
	    exit(1);
	}
	save_tio = tio;
	tio_mode_raw( &tio );
	if ( tio_set( 0, &tio ) == ERROR )
	{
	    fprintf( stderr, "can't set termios settings for keyboard\n" );
	    exit(2);
	}
	printf( "keyboard active. Press 'D' to change DTR, 'R' to change RTS.\r\n" );
    }

    last_f = -1;
    do
    {
	f = tio_get_rs232_lines( fd );

	if ( f == -1 )
	{
	    printf( "%s: can't read RS232 line status (-1)\n", Device );
	    exit(17);
	}

        /* display data only if something changed *or* if (not opt_delta)
	 */
	if ( ! opt_delta || f != last_f )
	{
	    ti = time(NULL);
	    tm = localtime(&ti);
	    printf( "%s, %02d:%02d:%02d: active lines:", Device, 
			    tm->tm_hour, tm->tm_min, tm->tm_sec );

	    if ( f & TIO_F_DTR ) printf( " DTR" );
	    if ( f & TIO_F_DSR ) printf( " DSR" );
	    if ( f & TIO_F_RTS ) printf( " RTS" );
	    if ( f & TIO_F_CTS ) printf( " CTS" );
	    if ( f & TIO_F_DCD ) printf( " DCD" );
	    if ( f & TIO_F_RI  ) printf( " RI" );

	    if ( f == 0 ) printf( " <none>" );

	    printf( "\r\n" );
	    
	    last_f = f;
	}

	if ( delay_time ) delay( delay_time );

	/* check keyboard */
        while ( opt_keyboard && check_for_input(0) )
	{
	    char ch;
	    if ( read( 0, &ch, 1 ) != 1 )
	    {
		fprintf( stderr, "error reading from keyboard, abort.\n");
		delay_time=0; break;
	    }
	    switch(ch)
	    {
	      case 'd':		/* toggle DTR */
	      case 'D': 
		tio_set_rs232_lines( fd, last_f & TIO_F_DTR? 0: 1, -1 );
		break;
	      case 'r':		/* toggle RTS */
	      case 'R':
		tio_set_rs232_lines( fd, -1, last_f & TIO_F_RTS? 0: 1 );
		break;
	      case 3:		/* exit */
	      case 'q':
	      case 'x':
		delay_time = 0; break;
	    }
	}
    }
    while( delay_time );

    if ( opt_keyboard )		/* reset termios settings */
	tio_set( 0, &save_tio );

    return 0;
}