serial-console.c 2.9 KB

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