forked from npat-efault/picocom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
picocom.c
1632 lines (1456 loc) · 37.3 KB
/
picocom.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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi: set sw=4 ts=4:
*
* picocom.c
*
* simple dumb-terminal program. Helps you manually configure and test
* stuff like modems, devices w. serial ports etc.
*
* by Nick Patavalis ([email protected])
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <stdarg.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <limits.h>
#ifdef USE_FLOCK
#include <sys/file.h>
#endif
#ifdef LINENOISE
#include <dirent.h>
#include <libgen.h>
#endif
#define _GNU_SOURCE
#include <getopt.h>
#include "fdio.h"
#include "split.h"
#include "term.h"
#ifdef LINENOISE
#include "linenoise-1.0/linenoise.h"
#endif
/**********************************************************************/
/* parity modes names */
const char *parity_str[] = {
[P_NONE] = "none",
[P_EVEN] = "even",
[P_ODD] = "odd",
[P_MARK] = "mark",
[P_SPACE] = "space",
};
/* flow control modes names */
const char *flow_str[] = {
[FC_NONE] = "none",
[FC_RTSCTS] = "RTS/CTS",
[FC_XONXOFF] = "xon/xoff",
[FC_OTHER] = "other",
};
/**********************************************************************/
/* control-key to printable character (lowcase) */
#define KEYC(k) ((k) | 0x60)
/* printable character to control-key */
#define CKEY(c) ((c) & 0x1f)
#define KEY_EXIT CKEY('x') /* exit picocom */
#define KEY_QUIT CKEY('q') /* exit picocom without reseting port */
#define KEY_PULSE CKEY('p') /* pulse DTR */
#define KEY_TOG_DTR CKEY('t') /* toggle DTR */
#define KEY_TOG_RTS CKEY('g') /* toggle RTS */
#define KEY_BAUD CKEY('b') /* set baudrate */
#define KEY_BAUD_UP CKEY('u') /* increase baudrate (up) */
#define KEY_BAUD_DN CKEY('d') /* decrase baudrate (down) */
#define KEY_FLOW CKEY('f') /* change flowcntrl mode */
#define KEY_PARITY CKEY('y') /* change parity mode */
#define KEY_BITS CKEY('i') /* change number of databits */
#define KEY_STOP CKEY('j') /* change number of stopbits */
#define KEY_LECHO CKEY('c') /* toggle local echo */
#define KEY_STATUS CKEY('v') /* show program options */
#define KEY_HELP CKEY('h') /* show help (same as [C-k]) */
#define KEY_KEYS CKEY('k') /* show available command keys */
#define KEY_SEND CKEY('s') /* send file */
#define KEY_RECEIVE CKEY('r') /* receive file */
#define KEY_BREAK CKEY('\\') /* break */
/**********************************************************************/
/* implemented caracter mappings */
#define M_CRLF (1 << 0) /* map CR --> LF */
#define M_CRCRLF (1 << 1) /* map CR --> CR + LF */
#define M_IGNCR (1 << 2) /* map CR --> <nothing> */
#define M_LFCR (1 << 3) /* map LF --> CR */
#define M_LFCRLF (1 << 4) /* map LF --> CR + LF */
#define M_IGNLF (1 << 5) /* map LF --> <nothing> */
#define M_DELBS (1 << 6) /* map DEL --> BS */
#define M_BSDEL (1 << 7) /* map BS --> DEL */
#define M_NFLAGS 8
/* default character mappings */
#define M_I_DFL 0
#define M_O_DFL 0
#define M_E_DFL (M_DELBS | M_CRCRLF)
/* character mapping names */
struct map_names_s {
char *name;
int flag;
} map_names[] = {
{ "crlf", M_CRLF },
{ "crcrlf", M_CRCRLF },
{ "igncr", M_IGNCR },
{ "lfcr", M_LFCR },
{ "lfcrlf", M_LFCRLF },
{ "ignlf", M_IGNLF },
{ "delbs", M_DELBS },
{ "bsdel", M_BSDEL },
/* Sentinel */
{ NULL, 0 }
};
int
parse_map (char *s)
{
char *m, *t;
int f, flags, i;
flags = 0;
while ( (t = strtok(s, ", \t")) ) {
for (i=0; (m = map_names[i].name); i++) {
if ( ! strcmp(t, m) ) {
f = map_names[i].flag;
break;
}
}
if ( m ) flags |= f;
else { flags = -1; break; }
s = NULL;
}
return flags;
}
void
print_map (int flags)
{
int i;
for (i = 0; i < M_NFLAGS; i++)
if ( flags & (1 << i) )
printf("%s,", map_names[i].name);
printf("\n");
}
/**********************************************************************/
struct {
char port[128];
int baud;
enum flowcntrl_e flow;
enum parity_e parity;
int databits;
int stopbits;
int lecho;
int noinit;
int noreset;
#if defined (UUCP_LOCK_DIR) || defined (USE_FLOCK)
int nolock;
#endif
unsigned char escape;
char send_cmd[128];
char receive_cmd[128];
int imap;
int omap;
int emap;
} opts = {
.port = "",
.baud = 9600,
.flow = FC_NONE,
.parity = P_NONE,
.databits = 8,
.stopbits = 1,
.lecho = 0,
.noinit = 0,
.noreset = 0,
#if defined (UUCP_LOCK_DIR) || defined (USE_FLOCK)
.nolock = 0,
#endif
.escape = CKEY('a'),
.send_cmd = "sz -vv",
.receive_cmd = "rz -vv -E",
.imap = M_I_DFL,
.omap = M_O_DFL,
.emap = M_E_DFL
};
int sig_exit = 0;
#define STO STDOUT_FILENO
#define STI STDIN_FILENO
int tty_fd;
#ifndef TTY_Q_SZ
#define TTY_Q_SZ 256
#endif
struct tty_q {
int len;
unsigned char buff[TTY_Q_SZ];
} tty_q;
#define TTY_RD_SZ 128
int tty_write_sz;
#define TTY_WRITE_SZ_DIV 10
#define TTY_WRITE_SZ_MIN 8
#define set_tty_write_sz(baud) \
do { \
tty_write_sz = (baud) / TTY_WRITE_SZ_DIV; \
if ( tty_write_sz < TTY_WRITE_SZ_MIN ) \
tty_write_sz = TTY_WRITE_SZ_MIN; \
} while (0)
/**********************************************************************/
#ifdef UUCP_LOCK_DIR
/* use HDB UUCP locks .. see
* <http://www.faqs.org/faqs/uucp-internals> for details
*/
char lockname[_POSIX_PATH_MAX] = "";
int
uucp_lockname(const char *dir, const char *file)
{
char *p, *cp;
struct stat sb;
if ( ! dir || *dir == '\0' || stat(dir, &sb) != 0 )
return -1;
/* cut-off initial "/dev/" from file-name */
p = strchr(file + 1, '/');
p = p ? p + 1 : (char *)file;
/* replace '/'s with '_'s in what remains (after making a copy) */
p = cp = strdup(p);
do { if ( *p == '/' ) *p = '_'; } while(*p++);
/* build lockname */
snprintf(lockname, sizeof(lockname), "%s/LCK..%s", dir, cp);
/* destroy the copy */
free(cp);
return 0;
}
int
uucp_lock(void)
{
int r, fd, pid;
char buf[16];
mode_t m;
if ( lockname[0] == '\0' ) return 0;
fd = open(lockname, O_RDONLY);
if ( fd >= 0 ) {
r = read(fd, buf, sizeof(buf));
close(fd);
/* if r == 4, lock file is binary (old-style) */
pid = (r == 4) ? *(int *)buf : strtol(buf, NULL, 10);
if ( pid > 0
&& kill((pid_t)pid, 0) < 0
&& errno == ESRCH ) {
/* stale lock file */
printf("Removing stale lock: %s\n", lockname);
sleep(1);
unlink(lockname);
} else {
lockname[0] = '\0';
errno = EEXIST;
return -1;
}
}
/* lock it */
m = umask(022);
fd = open(lockname, O_WRONLY|O_CREAT|O_EXCL, 0666);
if ( fd < 0 ) { lockname[0] = '\0'; return -1; }
umask(m);
snprintf(buf, sizeof(buf), "%04d\n", getpid());
write(fd, buf, strlen(buf));
close(fd);
return 0;
}
int
uucp_unlock(void)
{
if ( lockname[0] ) unlink(lockname);
return 0;
}
#endif /* of UUCP_LOCK_DIR */
/**********************************************************************/
void
fatal (const char *format, ...)
{
char *s, buf[256];
va_list args;
int len;
term_reset(STO);
term_reset(STI);
va_start(args, format);
len = vsnprintf(buf, sizeof(buf), format, args);
buf[sizeof(buf) - 1] = '\0';
va_end(args);
s = "\r\nFATAL: ";
writen_ni(STO, s, strlen(s));
writen_ni(STO, buf, len);
s = "\r\n";
writen_ni(STO, s, strlen(s));
/* wait a bit for output to drain */
sleep(1);
#ifdef UUCP_LOCK_DIR
uucp_unlock();
#endif
exit(EXIT_FAILURE);
}
/**********************************************************************/
#ifndef LINENOISE
char *
read_filename (void)
{
char fname[_POSIX_PATH_MAX];
int r;
fd_printf(STO, "\r\n*** file: ");
r = fd_readline(STI, STO, fname, sizeof(fname));
fd_printf(STO, "\r\n");
if ( r < 0 )
return NULL;
else
return strdup(fname);
}
int
read_baud (void)
{
char baudstr[9], *ep;
int baud = -1, r;
do {
fd_printf(STO, "\r\n*** baud: ");
r = fd_readline(STI, STO, baudstr, sizeof(baudstr));
fd_printf(STO, "\r\n");
if ( r < 0 )
break;
baud = strtol(baudstr, &ep, 0);
if ( ! ep || *ep != '\0' || ! term_baud_ok(baud) || baud == 0 ) {
fd_printf(STO, "*** Invalid baudrate!");
baud = -1;
}
} while (baud < 0);
return baud;
}
#else /* LINENOISE defined */
void
file_completion_cb (const char *buf, linenoiseCompletions *lc)
{
DIR *dirp;
struct dirent *dp;
char *basec, *basen, *dirc, *dirn;
int baselen, dirlen;
char *fullpath;
struct stat filestat;
basec = strdup(buf);
dirc = strdup(buf);
dirn = dirname(dirc);
dirlen = strlen(dirn);
basen = basename(basec);
baselen = strlen(basen);
dirp = opendir(dirn);
if (dirp) {
while ((dp = readdir(dirp)) != NULL) {
if (strncmp(basen, dp->d_name, baselen) == 0) {
/* add 2 extra bytes for possible / in middle & at end */
fullpath = (char *) malloc(strlen(dp->d_name) + dirlen + 3);
strcpy(fullpath, dirn);
if (fullpath[dirlen-1] != '/')
strcat(fullpath, "/");
strcat(fullpath, dp->d_name);
if (stat(fullpath, &filestat) == 0) {
if (S_ISDIR(filestat.st_mode)) {
strcat(fullpath, "/");
}
linenoiseAddCompletion(lc,fullpath);
}
free(fullpath);
}
}
closedir(dirp);
}
free(basec);
free(dirc);
}
static char *history_file_path = NULL;
void
init_history (void)
{
char *home_directory;
home_directory = getenv("HOME");
if (home_directory) {
history_file_path = malloc(strlen(home_directory) + 2 +
strlen(HISTFILE));
strcpy(history_file_path, home_directory);
if (home_directory[strlen(home_directory)-1] != '/') {
strcat(history_file_path, "/");
}
strcat(history_file_path, HISTFILE);
linenoiseHistoryLoad(history_file_path);
}
}
void
cleanup_history (void)
{
if (history_file_path)
free(history_file_path);
}
void
add_history (char *fname)
{
linenoiseHistoryAdd(fname);
if (history_file_path)
linenoiseHistorySave(history_file_path);
}
char *
read_filename (void)
{
char *fname;
linenoiseSetCompletionCallback(file_completion_cb);
fd_printf(STO, "\r\n");
fname = linenoise("*** file: ");
fd_printf(STO, "\r");
linenoiseSetCompletionCallback(NULL);
if (fname != NULL)
add_history(fname);
return fname;
}
int
read_baud (void)
{
char *baudstr, *ep;
int baud = -1;
do {
fd_printf(STO, "\r\n");
baudstr = linenoise("*** baud: ");
fd_printf(STO, "\r");
if ( baudstr == NULL )
break;
baud = strtol(baudstr, &ep, 0);
if ( ! ep || *ep != '\0' || ! term_baud_ok(baud) || baud == 0 ) {
fd_printf(STO, "*** Invalid baudrate!");
baud = -1;
}
free(baudstr);
} while (baud < 0);
if (baudstr != NULL)
add_history(baudstr);
return baud;
}
#endif /* of ifndef LINENOISE */
/**********************************************************************/
/* maximum number of chars that can replace a single characted
due to mapping */
#define M_MAXMAP 4
int
do_map (char *b, int map, char c)
{
int n;
switch (c) {
case '\x7f':
/* DEL mapings */
if ( map & M_DELBS ) {
b[0] = '\x08'; n = 1;
} else {
b[0] = c; n = 1;
}
break;
case '\x08':
/* BS mapings */
if ( map & M_BSDEL ) {
b[0] = '\x7f'; n = 1;
} else {
b[0] = c; n = 1;
}
break;
case '\x0d':
/* CR mappings */
if ( map & M_CRLF ) {
b[0] = '\x0a'; n = 1;
} else if ( map & M_CRCRLF ) {
b[0] = '\x0d'; b[1] = '\x0a'; n = 2;
} else if ( map & M_IGNCR ) {
n = 0;
} else {
b[0] = c; n = 1;
}
break;
case '\x0a':
/* LF mappings */
if ( map & M_LFCR ) {
b[0] = '\x0d'; n = 1;
} else if ( map & M_LFCRLF ) {
b[0] = '\x0d'; b[1] = '\x0a'; n = 2;
} else if ( map & M_IGNLF ) {
n = 0;
} else {
b[0] = c; n = 1;
}
break;
default:
b[0] = c; n = 1;
break;
}
return n;
}
void
map_and_write (int fd, int map, char c)
{
char b[M_MAXMAP];
int n;
n = do_map(b, map, c);
if ( n )
if ( writen_ni(fd, b, n) < n )
fatal("write to stdout failed: %s", strerror(errno));
}
/**********************************************************************/
int
baud_up (int baud)
{
return term_baud_up(baud);
}
int
baud_down (int baud)
{
int nb;
nb = term_baud_down(baud);
if (nb == 0)
nb = baud;
return nb;
}
int
flow_next (int flow)
{
switch(flow) {
case FC_NONE:
flow = FC_RTSCTS;
break;
case FC_RTSCTS:
flow = FC_XONXOFF;
break;
case FC_XONXOFF:
flow = FC_NONE;
break;
default:
flow = FC_NONE;
break;
}
return flow;
}
int
parity_next (int parity)
{
switch(parity) {
case P_NONE:
parity = P_EVEN;
break;
case P_EVEN:
parity = P_ODD;
break;
case P_ODD:
parity = P_NONE;
break;
default:
parity = P_NONE;
break;
}
return parity;
}
int
bits_next (int bits)
{
bits++;
if (bits > 8) bits = 5;
return bits;
}
int
stopbits_next (int bits)
{
bits++;
if (bits > 2) bits = 1;
return bits;
}
void
show_status (int dtr_up, int rts_up)
{
int baud, bits, stopbits, mctl;
enum flowcntrl_e flow;
enum parity_e parity;
term_refresh(tty_fd);
baud = term_get_baudrate(tty_fd, NULL);
flow = term_get_flowcntrl(tty_fd);
parity = term_get_parity(tty_fd);
bits = term_get_databits(tty_fd);
stopbits = term_get_stopbits(tty_fd);
fd_printf(STO, "\r\n");
if ( baud != opts.baud ) {
fd_printf(STO, "*** baud: %d (%d)\r\n", opts.baud, baud);
} else {
fd_printf(STO, "*** baud: %d\r\n", opts.baud);
}
if ( flow != opts.flow ) {
fd_printf(STO, "*** flow: %s (%s)\r\n",
flow_str[opts.flow], flow_str[flow]);
} else {
fd_printf(STO, "*** flow: %s\r\n", flow_str[opts.flow]);
}
if ( parity != opts.parity ) {
fd_printf(STO, "*** parity: %s (%s)\r\n",
parity_str[opts.parity], parity_str[parity]);
} else {
fd_printf(STO, "*** parity: %s\r\n", parity_str[opts.parity]);
}
if ( bits != opts.databits ) {
fd_printf(STO, "*** databits: %d (%d)\r\n", opts.databits, bits);
} else {
fd_printf(STO, "*** databits: %d\r\n", opts.databits);
}
if ( stopbits != opts.stopbits ) {
fd_printf(STO, "*** stopbits: %d (%d)\r\n", opts.stopbits, stopbits);
} else {
fd_printf(STO, "*** stopbits: %d\r\n", opts.stopbits);
}
mctl = term_get_mctl(tty_fd);
if (mctl >= 0 && mctl != MCTL_UNAVAIL) {
if ( ((mctl & MCTL_DTR) ? 1 : 0) == dtr_up )
fd_printf(STO, "*** dtr: %s\r\n", dtr_up ? "up" : "down");
else
fd_printf(STO, "*** dtr: %s (%s)\r\n",
dtr_up ? "up" : "down",
(mctl & MCTL_DTR) ? "up" : "down");
if ( ((mctl & MCTL_RTS) ? 1 : 0) == rts_up )
fd_printf(STO, "*** rts: %s\r\n", rts_up ? "up" : "down");
else
fd_printf(STO, "*** rts: %s (%s)\r\n",
rts_up ? "up" : "down",
(mctl & MCTL_RTS) ? "up" : "down");
fd_printf(STO, "*** mctl: ");
fd_printf(STO, "DTR:%c DSR:%c DCD:%c RTS:%c CTS:%c RI:%c\r\n",
(mctl & MCTL_DTR) ? '1' : '0',
(mctl & MCTL_DSR) ? '1' : '0',
(mctl & MCTL_DCD) ? '1' : '0',
(mctl & MCTL_RTS) ? '1' : '0',
(mctl & MCTL_CTS) ? '1' : '0',
(mctl & MCTL_RI) ? '1' : '0');
} else {
fd_printf(STO, "*** dtr: %s\r\n", dtr_up ? "up" : "down");
fd_printf(STO, "*** rts: %s\r\n", rts_up ? "up" : "down");
}
}
void
show_keys()
{
#ifndef NO_HELP
fd_printf(STO, "\r\n");
fd_printf(STO, "*** Picocom commands (all prefixed by [C-%c])\r\n",
KEYC(opts.escape));
fd_printf(STO, "\r\n");
fd_printf(STO, "*** [C-%c] : Exit picocom\r\n",
KEYC(KEY_EXIT));
fd_printf(STO, "*** [C-%c] : Exit without reseting serial port\r\n",
KEYC(KEY_QUIT));
fd_printf(STO, "*** [C-%c] : Set baudrate\r\n",
KEYC(KEY_BAUD));
fd_printf(STO, "*** [C-%c] : Increase baudrate (baud-up)\r\n",
KEYC(KEY_BAUD_UP));
fd_printf(STO, "*** [C-%c] : Decrease baudrate (baud-down)\r\n",
KEYC(KEY_BAUD_DN));;
fd_printf(STO, "*** [C-%c] : Change number of databits\r\n",
KEYC(KEY_BITS));
fd_printf(STO, "*** [C-%c] : Change number of stopbits\r\n",
KEYC(KEY_STOP));
fd_printf(STO, "*** [C-%c] : Change flow-control mode\r\n",
KEYC(KEY_FLOW));
fd_printf(STO, "*** [C-%c] : Change parity mode\r\n",
KEYC(KEY_PARITY));
fd_printf(STO, "*** [C-%c] : Pulse DTR\r\n",
KEYC(KEY_PULSE));
fd_printf(STO, "*** [C-%c] : Toggle DTR\r\n",
KEYC(KEY_TOG_DTR));
fd_printf(STO, "*** [C-%c] : Toggle RTS\r\n",
KEYC(KEY_TOG_RTS));
fd_printf(STO, "*** [C-%c] : Send break\r\n",
KEYC(KEY_BREAK));
fd_printf(STO, "*** [C-%c] : Toggle local echo\r\n",
KEYC(KEY_LECHO));
fd_printf(STO, "*** [C-%c] : Send file\r\n",
KEYC(KEY_SEND));
fd_printf(STO, "*** [C-%c] : Receive file\r\n",
KEYC(KEY_RECEIVE));
fd_printf(STO, "*** [C-%c] : Show port settings\r\n",
KEYC(KEY_STATUS));
fd_printf(STO, "*** [C-%c] : Show this message\r\n",
KEYC(KEY_HELP));
fd_printf(STO, "\r\n");
#else /* defined NO_HELP */
fd_printf(STO, "*** Help is disabled.\r\n");
#endif /* of NO_HELP */
}
/**********************************************************************/
#define RUNCMD_ARGS_MAX 32
#define RUNCMD_EXEC_FAIL 126
void
establish_child_signal_handlers (void)
{
struct sigaction dfl_action;
/* Set up the structure to specify the default action. */
dfl_action.sa_handler = SIG_DFL;
sigemptyset (&dfl_action.sa_mask);
dfl_action.sa_flags = 0;
sigaction (SIGINT, &dfl_action, NULL);
sigaction (SIGTERM, &dfl_action, NULL);
}
int
run_cmd(int fd, const char *cmd, const char *args_extra)
{
pid_t pid;
sigset_t sigm, sigm_old;
/* block signals, let child establish its own handlers */
sigemptyset(&sigm);
sigaddset(&sigm, SIGTERM);
sigprocmask(SIG_BLOCK, &sigm, &sigm_old);
pid = fork();
if ( pid < 0 ) {
sigprocmask(SIG_SETMASK, &sigm_old, NULL);
fd_printf(STO, "*** cannot fork: %s ***\r\n", strerror(errno));
return -1;
} else if ( pid ) {
/* father: picocom */
int status, r;
/* reset the mask */
sigprocmask(SIG_SETMASK, &sigm_old, NULL);
/* wait for child to finish */
do {
r = waitpid(pid, &status, 0);
} while ( r < 0 && errno == EINTR );
/* reset terminal (back to raw mode) */
term_apply(STI, 0);
/* check and report child return status */
if ( WIFEXITED(status) ) {
fd_printf(STO, "\r\n*** exit status: %d ***\r\n",
WEXITSTATUS(status));
return WEXITSTATUS(status);
} else if ( WIFSIGNALED(status) ) {
fd_printf(STO, "\r\n*** killed by signal: %d ***\r\n",
WTERMSIG(status));
return -1;
} else {
fd_printf(STO, "\r\n*** abnormal termination: 0x%x ***\r\n", r);
return -1;
}
} else {
/* child: external program */
long fl;
int argc;
char *argv[RUNCMD_ARGS_MAX + 1];
int r;
/* unmanage terminal, and reset it to canonical mode */
term_remove(STI);
/* unmanage serial port fd, without reset */
term_erase(fd);
/* set serial port fd to blocking mode */
fl = fcntl(fd, F_GETFL);
fl &= ~O_NONBLOCK;
fcntl(fd, F_SETFL, fl);
/* connect stdin and stdout to serial port */
close(STI);
close(STO);
dup2(fd, STI);
dup2(fd, STO);
/* build command arguments vector */
argc = 0;
r = split_quoted(cmd, &argc, argv, RUNCMD_ARGS_MAX);
if ( r < 0 ) {
fd_printf(STDERR_FILENO, "Cannot parse command\n");
exit(RUNCMD_EXEC_FAIL);
}
r = split_quoted(args_extra, &argc, argv, RUNCMD_ARGS_MAX);
if ( r < 0 ) {
fd_printf(STDERR_FILENO, "Cannot parse extra args\n");
exit(RUNCMD_EXEC_FAIL);
}
if ( argc < 1 ) {
fd_printf(STDERR_FILENO, "No command given\n");
exit(RUNCMD_EXEC_FAIL);
}
argv[argc] = NULL;
/* run extenral command */
fd_printf(STDERR_FILENO, "$ %s %s\n", cmd, args_extra);
establish_child_signal_handlers();
sigprocmask(SIG_SETMASK, &sigm_old, NULL);
execvp(argv[0], argv);
fd_printf(STDERR_FILENO, "exec: %s\n", strerror(errno));
exit(RUNCMD_EXEC_FAIL);
}
}
/**********************************************************************/
/* Process command key. Returns non-zero if command results in picocom
exit, zero otherwise. */
int
do_command (unsigned char c)
{
static int dtr_up = 0;
static int rts_up = 0;
int newbaud, newflow, newparity, newbits, newstopbits;
const char *xfr_cmd;
char *fname;
int r;
switch (c) {
case KEY_EXIT:
return 1;
case KEY_QUIT:
term_set_hupcl(tty_fd, 0);
term_flush(tty_fd);
term_apply(tty_fd, 1);
term_erase(tty_fd);
return 1;
case KEY_STATUS:
show_status(dtr_up, rts_up);
break;
case KEY_HELP:
case KEY_KEYS:
show_keys();
break;
case KEY_PULSE:
fd_printf(STO, "\r\n*** pulse DTR ***\r\n");
if ( term_pulse_dtr(tty_fd) < 0 )
fd_printf(STO, "*** FAILED\r\n");
break;
case KEY_TOG_DTR:
if ( dtr_up )
r = term_lower_dtr(tty_fd);
else
r = term_raise_dtr(tty_fd);
if ( r >= 0 ) dtr_up = ! dtr_up;
fd_printf(STO, "\r\n*** DTR: %s ***\r\n",
dtr_up ? "up" : "down");
break;
case KEY_TOG_RTS:
if ( rts_up )
r = term_lower_rts(tty_fd);
else
r = term_raise_rts(tty_fd);
if ( r >= 0 ) rts_up = ! rts_up;
fd_printf(STO, "\r\n*** RTS: %s ***\r\n",
rts_up ? "up" : "down");
break;
case KEY_BAUD:
case KEY_BAUD_UP:
case KEY_BAUD_DN:
if ( c== KEY_BAUD) {
newbaud = read_baud();
if ( newbaud < 0 ) {
fd_printf(STO, "*** cannot read baudrate ***\r\n");
break;
}
opts.baud = newbaud;
} else if (c == KEY_BAUD_UP) {
opts.baud = baud_up(opts.baud);
} else {
opts.baud = baud_down(opts.baud);
}
term_set_baudrate(tty_fd, opts.baud);
tty_q.len = 0; term_flush(tty_fd);
term_apply(tty_fd, 1);
newbaud = term_get_baudrate(tty_fd, NULL);
if ( opts.baud != newbaud ) {
fd_printf(STO, "\r\n*** baud: %d (%d) ***\r\n",
opts.baud, newbaud);
} else {
fd_printf(STO, "\r\n*** baud: %d ***\r\n", opts.baud);
}
set_tty_write_sz(newbaud);
break;
case KEY_FLOW:
opts.flow = flow_next(opts.flow);
term_set_flowcntrl(tty_fd, opts.flow);
tty_q.len = 0; term_flush(tty_fd);
term_apply(tty_fd, 1);
newflow = term_get_flowcntrl(tty_fd);
if ( opts.flow != newflow ) {
fd_printf(STO, "\r\n*** flow: %s (%s) ***\r\n",
flow_str[opts.flow], flow_str[newflow]);
} else {
fd_printf(STO, "\r\n*** flow: %s ***\r\n",
flow_str[opts.flow]);
}
break;
case KEY_PARITY:
opts.parity = parity_next(opts.parity);