term

Check-in [1059162a07]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Initial commit of source
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256:1059162a07a654baaa37a9d870315a3fd05a32a71238a2e1ec186e7252277983
User & Date: ajv-899-334-8894@vsta.org 2018-01-17 19:31:05
Context
2018-01-17
20:39
Merge pull request #1 from thanegill/patch-1 Add Makefile check-in: 89f652f3d5 user: noreply@github.com tags: master, trunk
20:38
Add Makefile check-in: 6f43935589 user: noreply@github.com tags: master, trunk
19:31
Initial commit of source check-in: 1059162a07 user: ajv-899-334-8894@vsta.org tags: master, trunk
19:13
Initial commit check-in: a5270b0c14 user: noreply@github.com tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added README.md.













>
>
>
>
>
>
1
2
3
4
5
6
This is a simple terminal program which keeps being helpful to me.
It can give you basic typing on a serial port, knows how to invoke
x- y- and z-modem programs within it, and can also do session capture.

See the LICENSE.  I will be very surprised if its terms keep you from
using this little bit of code.

Added term.c.















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * term.c
 *	Terminal program, so you can type into a serial port
 */
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <termios.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/fcntl.h>

/* Default TTY to access */
#define DEFAULT_TTY "/dev/ttyUSB0"

#define BUFSIZE (30)		/* # chars read in at a time, max */

static struct termios ntty, otty, ext;
static int baud = B9600;
static void proto_xfer();
static FILE *logfp = NULL;

static int ttyfd, child;	/* Needed for quit function */

static int parodd, pareven,	/* Parity? */
    seven_bits;			/* 7 bit format (else 8) */

static int raw_kbd = 0;		/* Don't map \r to \n on input typing? */

/*
 * Protocols to receive under
 */
#define PROTO_RX 1
#define PROTO_RY 2
#define PROTO_RZ 3
#define PROTO_TXT 4
static int proto = PROTO_RZ;
#define PROTO_CHAR '\32'	/* Control-Z starts protocol transfer */

/* For suspending reader/writer during file transfer */
static void
usr2(int dummy)
{
    signal(SIGUSR2, usr2);
}

static void
usr1(int dummy)
{
    signal(SIGUSR1, usr1);
    pause();
}

/* Entered when parent tells child to die */
void
do_term()
{
    if (logfp) {
	fclose(logfp);
    }
    exit(0);
}

/* Tell what the command line options are */
static void
usage(void)
{
    fprintf(stderr,
"Usage is: term [-eo7] [-s <speed>] [-p <protocol>] [-l <log>] [<tty>]\n");
    exit(1);
}

/* Complain about bad parity selection (both even & odd) */
static void
bad_parity(void)
{
    fprintf(stderr,
	"Can't select both even and odd parity.\n");
    usage();
}

/*
 * done()
 *	Clean up and exit
 */
static void
done(void)
{
    kill(child, SIGTERM);
    tcsetattr(ttyfd, TCSAFLUSH, &otty);
    write(ttyfd, "Exiting\n", 8);
    exit(0);
}

/*
 * setup_serial()
 *	Set serial port to our desired parameters
 *
 * We have this in a function so we can do it not only when we
 * start up, but also after protocol transfers.  It turns out
 * that the serial port might not be left in the same state it
 * started in.
 */
static void
setup_serial(rs232fd)
	int rs232fd;
{
    int fl;

    /*
     * Set up serial port for local access.  Once we've
     * set CLOCAL, turn off O_NDELAY.
     */
    tcgetattr(rs232fd, &ext);
    ext.c_lflag &= ~(ECHO|ICANON|ISIG);
    ext.c_cflag |= CLOCAL;	/* Ignore modem status */
    ext.c_cflag &= ~(CSIZE);	/* Set # bits */
    if (seven_bits) {
	ext.c_cflag |= (CS7);
    } else {
	ext.c_cflag |= (CS8);
    }
    ext.c_cflag &= ~(CSTOPB);
    if (!parodd && !pareven) {	/* Set parity */
	ext.c_cflag &= ~(PARENB);
    } else if (parodd) {
	ext.c_cflag |= (PARODD);
    }
    ext.c_oflag &= ~OPOST;
    ext.c_iflag = 0;
    ext.c_cc[VMIN] = BUFSIZE;
    ext.c_cc[VTIME] = 1;
    cfsetispeed(&ext, baud);
    cfsetospeed(&ext, baud);
    tcsetattr(rs232fd, TCSAFLUSH, &ext);

    /* Permit sleeps on I/O now that we're ignoring modem control */
    fl = fcntl(rs232fd, F_GETFL, 0);
    fl &= ~(O_NDELAY);
    (void)fcntl(rs232fd, F_SETFL, fl);
}

int
main(int argc, char **argv)
{
    int rs232, x;
    char c, *tty = DEFAULT_TTY;
    extern char *optarg;

    while ((x = getopt(argc, argv, "s:p:l:eo7r")) != -1) {
	switch (x) {

	/*
	 * Selection of baud rate
	 */
	case 's':
	    if (!strcmp(optarg, "300")) {
		baud = B300; 
	    } else if (!strcmp(optarg, "1200")) {
		baud = B1200; 
	    } else if (!strcmp(optarg, "2400")) {
		baud = B2400; 
	    } else if (!strcmp(optarg, "9600")) {
		baud = B9600; 
	    } else if (!strcmp(optarg, "19200")) {
		baud = B19200; 
	    } else if (!strcmp(optarg, "38400")) {
		baud = B38400; 
	    } else if (!strcmp(optarg, "115200")) {
		baud = B115200; 
	    } else {
		fprintf(stderr, "Illegal speed: %s\n", optarg);
		usage();
	    }
	    break;

	/*
	 * Selection of transfer protocol
	 */
	case 'p':
	    if (!strcmp(optarg, "x")) {
		proto = PROTO_RX;
	    } else if (!strcmp(optarg, "y")) {
		proto = PROTO_RY;
	    } else if (!strcmp(optarg, "z")) {
		proto = PROTO_RZ;
	    } else if (!strcmp(optarg, "txt")) {
		proto = PROTO_TXT;
	    } else {
		printf("Illegal protocol: %s\n", optarg);
		usage();
	    }
	    break;

	/* Choose a log file */
	case 'l':
	    if ((logfp = fopen(optarg, "w")) == NULL) {
		perror(optarg);
		exit(1);
	    }
	    break;

	/* Set odd parity */
	case 'o':
	    if (pareven) {
		bad_parity();
	    }
	    parodd = 1;
	    break;

	/* Set even parity */
	case 'e':
	    if (parodd) {
		bad_parity();
	    }
	    pareven = 1;
	    break;

	/* Set seven bit format (else 8) */
	case '7':
	    seven_bits = 1;
	    break;

	/* Raw keyboard */
	case 'r':
	    raw_kbd = 1;
	    break;

	default:
	    printf("Illegal option: %c\n", x);
	    usage();
	}
    }

    /* Trailing argument; terminal device */
    if (optind < argc) {
        tty = argv[optind++];
    }
    if (optind != argc) {
        printf("Trailing argument(s)\n");
        usage();
    }

    printf("Terminal starting up...\n");
    printf("Use ^Z-q (control-Z, followed by q) to quit.\n");

    /*
     * The next bit of code makes assumptions about
     *  how UNIX manages its file descriptors.  The goal
     *  is to make the terminal device be our standard
     *  input & output.
     */
    ttyfd = dup(1);
    close(0); 
    close(1);
    if ((rs232 = open(tty, O_RDWR|O_EXCL|O_NDELAY)) < 0) {
	perror(tty);
	exit(1);
    }
    dup(rs232);

    /*
     * Set up for raw TTY I/O
     */
    tcgetattr(ttyfd, &otty);
    ntty = otty;
    ntty.c_lflag &= ~(ECHO|ICANON|ISIG);
    ntty.c_oflag &= ~OPOST;
    ntty.c_iflag = 0;
    ntty.c_cc[VMIN] = 1;
    ntty.c_cc[VTIME] = 0;
    tcsetattr(ttyfd, TCSAFLUSH, &ntty);

    setup_serial(rs232);

    if ((child = fork()) == 0) {
	static char boot_msg[] = "Term ready.\r\n";

	/*
	 * This is the code which posts reads to the serial port
	 * and then writes them on to the user (and, if -l was
	 * used, to the log file too)
	 */
	signal(SIGUSR1, usr1);
	signal(SIGUSR2, usr2);
	signal(SIGTERM, do_term);
	write(ttyfd, boot_msg, sizeof(boot_msg)-1);
	for (;;) {
	    char *p, *q, buf[BUFSIZE];

	    if ((x = read(rs232, buf, sizeof(buf))) < 0) {
		if (errno == EINTR)
		    continue;
		perror("child");
		exit(1);
	    }
	    p = buf; 
	    q = buf+x;
	    while (p < q) {
		*p++ &= 0x7F;
	    }
	    write(ttyfd, buf, x);
	    if (logfp) {
		fwrite(buf, sizeof(char), x, logfp);
	    }
	}
    }

    if (child < 0) {
	perror("child fork");
	exit(1);
    }

    /*
     * This is the code which posts reads to the user's keyboard and
     * writes the data out to the serial port.
     */
    while ((x = read(ttyfd, &c, 1)) == 1) {
	c &= 0x7F;

	/* PROTO_CHAR (usually Control-Z) starts file transfers */
	if (c == PROTO_CHAR) {
	    kill(child, SIGUSR1);
	    proto_xfer(ttyfd, rs232);
	    kill(child, SIGUSR2);
	    continue;
	}
	if (!raw_kbd && (c == '\n')) {
	    c = '\r';
	}
	write(rs232, &c, 1);
    }
    if (x < 0) {
	perror("parent");
    }
    done();
    /*NOTREACHED*/
    return(0);
}

/*
 * Ask for and receive a line in raw mode
 */
static void
prompt_read(int ttyfd, char *prompt, char *buf, int len)
{
    char c;

    /* Leave room for null terminator */
    len -= 1;

    /* Prompt */
    write(ttyfd, prompt, strlen(prompt));

    /* Read chars until newline */
    do {
	while (read(ttyfd, &c, sizeof(c)) == 0) ;
	c &= 0x7F;
	write(ttyfd, &c, sizeof(c));
	if (len) {
	    *buf++ = c;
	    --len;
	}
    } while ((c != '\n') && (c != '\r'));

    /* Overwrite newline with terminator */
    buf -= 1;
    *buf = '\0';
}

/*
 * Execute a protocol receive
 */
static void
rx_xfer(int ttyfd)
{
    char buf[80];
    char fname[60];

    switch (proto) {

    case PROTO_RX:
	/*
	 * Xmodem doesn't send names, so we have to ask.  Bleh.
	 */
	prompt_read(ttyfd, "Receive file: ", fname, sizeof(fname));
	sprintf(buf, "lrx %s", fname);
	break;

    case PROTO_RY:
	strcpy(buf, "lry");
	break;

    case PROTO_RZ:
	strcpy(buf, "lrz");
	break;

    default:
	fprintf(stderr, "Receive not supported with this protocol.\r\n");
	return;
    }
    system(buf);
}

/*
 * Execute a protocol transmit
 */
static void
tx_xfer(int ttyfd)
{
    char buf[80];
    char fname[60];

    prompt_read(ttyfd, "Send file: ", fname, sizeof(fname));

    switch (proto) {

    case PROTO_RX:
	strcpy(buf, "lsx");
	break;

    case PROTO_RY:
	strcpy(buf, "lsy");
	break;

    case PROTO_RZ:
	strcpy(buf, "lsz");
	break;

    case PROTO_TXT:
	strcpy(buf, "cat");
	break;

    default:
	fprintf(stderr, "Transmit not supported with this protocol.\r\n");
	return;
    }

    sprintf(buf, "%s %s", buf, fname);
    system(buf);
}

/*
 * Figure out what they want to do, drive a protcol transfer
 */
static void
proto_xfer(int ttyfd, int rs232)
{
    char c;
    register char c2;
    static char helpmsg[] = "Options are: <r>eceive, <s>end, <q>uit\r\n";

    /* Get next char to see what they want to do */
    while (read(ttyfd, &c, sizeof(c)) == 0)
	    ;
    c &= 0x7F;

    /* Send char through literally */
    if (c == PROTO_CHAR) {
	write(rs232, &c, sizeof(c));
	return;
    }

    /* 'q' or 'Q' means quit */
    if ((c == 'q') || (c == 'Q')) {
	done();
    }

    /* Receive? */
    c2 = c;
    if ((c2 == 'r') || (c2 == 'R')) {
	rx_xfer(ttyfd);
	setup_serial(rs232);

    /* Send? */
    } else if ((c2 == 's') || (c2 == 'S') || (c2 == 't') ||
	    (c2 == 'T')) {
	tx_xfer(ttyfd);
	setup_serial(rs232);

    /* Dunno */
    } else {
	write(ttyfd, helpmsg, sizeof(helpmsg)-1);
    }
}