Some hacky cleanups to serial-console

This commit is contained in:
Kevin Lange 2015-08-20 17:32:42 -07:00
parent e933f61f70
commit 4996371a92

View File

@ -24,6 +24,10 @@ int fd = 0;
int child_pid = 0;
int keep_echo = 0;
int dos_lines = 0;
int keep_canon = 0;
void *print_serial_stuff(void * garbage) {
child_pid = gettid();
@ -42,7 +46,12 @@ struct termios old;
void set_unbuffered() {
tcgetattr(fileno(stdin), &old);
struct termios new = old;
new.c_lflag &= (~ICANON & ~ECHO);
if (!keep_canon) {
new.c_lflag &= (~ICANON);
}
if (!keep_echo) {
new.c_lflag &= (~ECHO);
}
tcsetattr(fileno(stdin), TCSAFLUSH, &new);
}
@ -50,13 +59,47 @@ void set_buffered() {
tcsetattr(fileno(stdin), TCSAFLUSH, &old);
}
int show_usage(int argc, char * argv[]) {
printf(
"Serial client.\n"
"\n"
"usage: %s [-e] [-r] [-c] [device path]\n"
"\n"
" -e \033[3mkeep echo enabled\033[0m\n"
" -c \033[3mkeep canon enabled\033[0m\n"
" -r \033[3mtransform line feeds to \\r\\n\033[0m\n"
" -? \033[3mshow this help text\033[0m\n"
"\n", argv[0]);
return 1;
}
int main(int argc, char ** argv) {
pthread_t receive_thread;
pthread_t flush_thread;
char * device = argv[1];
if (argc < 1) {
int arg = 1;
char * device;
while (arg < argc) {
if (argv[arg][0] != '-') break;
if (!strcmp(argv[arg], "-e")) {
keep_echo = 1;
} else if (!strcmp(argv[arg], "-r")) {
dos_lines = 1;
} else if (!strcmp(argv[arg], "-c")) {
keep_canon = 1;
} else if (!strcmp(argv[arg], "-?")) {
return show_usage(argc, argv);
} else {
fprintf(stderr, "%s: Unrecognized option: %s\n", argv[0], argv[arg]);
}
arg++;
}
if (arg == argc) {
device = "/dev/ttyS0";
} else {
device = argv[arg];
}
set_unbuffered();
@ -67,38 +110,46 @@ int main(int argc, char ** argv) {
while (1) {
char c = fgetc(stdin);
if (c == 27) {
char x = fgetc(stdin);
if (x == ']') {
while (1) {
printf("serial-console>\033[1561z ");
set_buffered();
fflush(stdout);
if (c == 0x1D) { /* ^] */
while (1) {
printf("serial-console> ");
set_buffered();
fflush(stdout);
char line[1024];
fgets(line, 1024, stdin);
char line[1024];
fgets(line, 1024, stdin);
int i = strlen(line);
line[i-1] = '\0';
if (!strcmp(line, "quit")) {
kill(child_pid, SIGKILL);
printf("Waiting for threads to shut down...\n");
while (wait(NULL) != -1);
printf("Exiting.\n");
return 0;
} else if (!strcmp(line, "continue")) {
set_unbuffered();
fflush(stdout);
break;
}
if (feof(stdin)) {
kill(child_pid, SIGKILL);
printf("Waiting for threads to shut down...\n");
while (wait(NULL) != -1);
printf("Exiting.\n");
return 0;
}
int i = strlen(line);
line[i-1] = '\0';
if (!strcmp(line, "quit")) {
kill(child_pid, SIGKILL);
printf("Waiting for threads to shut down...\n");
while (wait(NULL) != -1);
printf("Exiting.\n");
return 0;
} else if (!strcmp(line, "continue")) {
set_unbuffered();
fflush(stdout);
break;
}
} else {
ungetc(x, stdin);
}
} else {
if (dos_lines && c == '\n') {
char buf[1] = {'\r'};
write(fd, buf, 1);
}
char buf[1] = {c};
write(fd, buf, 1);
}
char buf[1] = {c};
write(fd, buf, 1);
}
close(fd);