serial-console.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2013-2014 K. Lange
  5. *
  6. * serial console
  7. *
  8. * Runs a dumb console on a serial port or something similar.
  9. *
  10. */
  11. #include <stdio.h>
  12. #include <stdint.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #include <signal.h>
  16. #include <string.h>
  17. #include <termios.h>
  18. #include <fcntl.h>
  19. #include <sys/wait.h>
  20. #include <sys/fswait.h>
  21. int fd = 0;
  22. int keep_echo = 0;
  23. int dos_lines = 0;
  24. int keep_canon = 0;
  25. struct termios old;
  26. void set_unbuffered() {
  27. tcgetattr(fileno(stdin), &old);
  28. struct termios new = old;
  29. if (!keep_canon) {
  30. new.c_lflag &= (~ICANON);
  31. }
  32. if (!keep_echo) {
  33. new.c_lflag &= (~ECHO);
  34. }
  35. tcsetattr(fileno(stdin), TCSAFLUSH, &new);
  36. }
  37. void set_buffered() {
  38. tcsetattr(fileno(stdin), TCSAFLUSH, &old);
  39. }
  40. int show_usage(int argc, char * argv[]) {
  41. printf(
  42. "Serial client.\n"
  43. "\n"
  44. "usage: %s [-e] [-r] [-c] [device path]\n"
  45. "\n"
  46. " -e \033[3mkeep echo enabled\033[0m\n"
  47. " -c \033[3mkeep canon enabled\033[0m\n"
  48. " -r \033[3mtransform line feeds to \\r\\n\033[0m\n"
  49. " -? \033[3mshow this help text\033[0m\n"
  50. "\n", argv[0]);
  51. return 1;
  52. }
  53. int main(int argc, char ** argv) {
  54. int arg = 1;
  55. char * device;
  56. while (arg < argc) {
  57. if (argv[arg][0] != '-') break;
  58. if (!strcmp(argv[arg], "-e")) {
  59. keep_echo = 1;
  60. } else if (!strcmp(argv[arg], "-r")) {
  61. dos_lines = 1;
  62. } else if (!strcmp(argv[arg], "-c")) {
  63. keep_canon = 1;
  64. } else if (!strcmp(argv[arg], "-?")) {
  65. return show_usage(argc, argv);
  66. } else {
  67. fprintf(stderr, "%s: Unrecognized option: %s\n", argv[0], argv[arg]);
  68. }
  69. arg++;
  70. }
  71. if (arg == argc) {
  72. device = "/dev/ttyS0";
  73. } else {
  74. device = argv[arg];
  75. }
  76. set_unbuffered();
  77. fd = open(device, 0, 0);
  78. int fds[2] = {STDIN_FILENO, fd};
  79. while (1) {
  80. int index = fswait(2, fds);
  81. if (index == -1) {
  82. fprintf(stderr, "serial-console: fswait: erroneous file descriptor\n");
  83. fprintf(stderr, "serial-console: (did you try to open a file that isn't a serial console?\n");
  84. return 1;
  85. }
  86. if (index == 0) {
  87. char c = fgetc(stdin);
  88. if (c == 0x1D) { /* ^] */
  89. while (1) {
  90. printf("serial-console> ");
  91. set_buffered();
  92. fflush(stdout);
  93. char line[1024];
  94. fgets(line, 1024, stdin);
  95. if (feof(stdin)) {
  96. return 0;
  97. }
  98. int i = strlen(line);
  99. line[i-1] = '\0';
  100. if (!strcmp(line, "quit")) {
  101. return 0;
  102. } else if (!strcmp(line, "continue")) {
  103. set_unbuffered();
  104. fflush(stdout);
  105. break;
  106. }
  107. }
  108. } else {
  109. if (dos_lines && c == '\n') {
  110. char buf[1] = {'\r'};
  111. write(fd, buf, 1);
  112. }
  113. char buf[1] = {c};
  114. write(fd, buf, 1);
  115. }
  116. } else {
  117. char buf[1024];
  118. size_t r = read(fd, buf, 1024);
  119. fwrite(buf, 1, r, stdout);
  120. fflush(stdout);
  121. }
  122. }
  123. close(fd);
  124. set_buffered();
  125. return 0;
  126. }