Simple Terminal from SuckLess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
5.1 KiB

16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
  1. /* See LICENSE file for copyright and license details. */
  2. #include <sys/types.h>
  3. #include <sys/wait.h>
  4. #include <ctype.h>
  5. #include <err.h>
  6. #include <signal.h>
  7. #include <stdarg.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
  13. #define MAX(a,b) (((a) > (b)) ? (a) : (b))
  14. #define MIN(a,b) (((a) < (b)) ? (a) : (b))
  15. static void buffer(char c);
  16. static void cmd(const char *cmdstr, ...);
  17. static int getch();
  18. void getpty(void);
  19. static void movea(int x, int y);
  20. static void mover(int x, int y);
  21. static void parseesc(void);
  22. static void scroll(int l);
  23. static void shell(void);
  24. static void sigchld(int n);
  25. static char unbuffer(void);
  26. static void ungetch(int c);
  27. typedef struct {
  28. unsigned char data[BUFSIZ];
  29. int s, e;
  30. int n;
  31. } RingBuffer;
  32. typedef struct {
  33. unsigned char data[BUFSIZ];
  34. int i, n;
  35. } ReadBuffer;
  36. static int cols = 80, lines = 25;
  37. static int cx = 0, cy = 0;
  38. static int c;
  39. static int ptm, pts;
  40. static _Bool bold, digit, qmark;
  41. static pid_t pid;
  42. static RingBuffer buf;
  43. static ReadBuffer rbuf;
  44. void
  45. buffer(char c) {
  46. if(buf.n < LENGTH(buf.data))
  47. buf.n++;
  48. else
  49. buf.s = (buf.s + 1) % LENGTH(buf.data);
  50. buf.data[buf.e++] = c;
  51. buf.e %= LENGTH(buf.data);
  52. }
  53. void
  54. cmd(const char *cmdstr, ...) {
  55. va_list ap;
  56. putchar('\n');
  57. putchar(':');
  58. va_start(ap, cmdstr);
  59. vfprintf(stdout, cmdstr, ap);
  60. va_end(ap);
  61. }
  62. int
  63. getch() {
  64. if(rbuf.i++ >= rbuf.n) {
  65. rbuf.n = read(ptm, rbuf.data, LENGTH(rbuf.data));
  66. if(rbuf.n == -1)
  67. err(EXIT_FAILURE, "cannot read from slave pty");
  68. rbuf.i = 0;
  69. }
  70. return rbuf.data[rbuf.i];
  71. }
  72. void
  73. movea(int x, int y) {
  74. x = MAX(x, cols);
  75. y = MAX(y, lines);
  76. cx = x;
  77. cy = y;
  78. cmd("seek(%d,%d)", x, y);
  79. }
  80. void
  81. mover(int x, int y) {
  82. movea(cx + x, cy + y);
  83. }
  84. void
  85. parseesc(void) {
  86. int i, j;
  87. int arg[16];
  88. memset(arg, 0, LENGTH(arg));
  89. c = getch();
  90. switch(c) {
  91. case '[':
  92. c = getch();
  93. for(j = 0; j < LENGTH(arg);) {
  94. if(isdigit(c)) {
  95. digit = 1;
  96. arg[j] *= 10;
  97. arg[j] += c - '0';
  98. }
  99. else if(c == '?')
  100. qmark = 1;
  101. else if(c == ';') {
  102. if(!digit)
  103. errx(EXIT_FAILURE, "syntax error");
  104. digit = 0;
  105. j++;
  106. }
  107. else {
  108. if(digit) {
  109. digit = 0;
  110. j++;
  111. }
  112. break;
  113. }
  114. c = getch();
  115. }
  116. switch(c) {
  117. case '@':
  118. break;
  119. case 'A':
  120. mover(0, j ? arg[0] : 1);
  121. break;
  122. case 'B':
  123. mover(0, j ? -arg[0] : -1);
  124. break;
  125. case 'C':
  126. mover(j ? arg[0] : 1, 0);
  127. break;
  128. case 'D':
  129. mover(j ? -arg[0] : -1, 0);
  130. break;
  131. case 'E':
  132. /* movel(j ? arg[0] : 1); */
  133. break;
  134. case 'F':
  135. /* movel(j ? -arg[0] : -1); */
  136. break;
  137. case '`':
  138. case 'G':
  139. movea(j ? arg[0] : 1, cy);
  140. break;
  141. case 'f':
  142. case 'H':
  143. movea(arg[1] ? arg[1] : 1, arg[0] ? arg[0] : 1);
  144. case 'L':
  145. /* insline(j ? arg[0] : 1); */
  146. break;
  147. case 'M':
  148. /* delline(j ? arg[0] : 1); */
  149. break;
  150. case 'P':
  151. break;
  152. case 'S':
  153. scroll(j ? arg[0] : 1);
  154. break;
  155. case 'T':
  156. scroll(j ? -arg[0] : -1);
  157. break;
  158. case 'd':
  159. movea(cx, j ? arg[0] : 1);
  160. break;
  161. case 'm':
  162. for(i = 0; i < j; i++) {
  163. if(arg[i] >= 30 && arg[i] <= 37)
  164. cmd("#%d", arg[i] - 30);
  165. if(arg[i] >= 40 && arg[i] <= 47)
  166. cmd("|%d", arg[i] - 40);
  167. /* xterm bright colors */
  168. if(arg[i] >= 90 && arg[i] <= 97)
  169. cmd("#%d", arg[i] - 90);
  170. if(arg[i] >= 100 && arg[i] <= 107)
  171. cmd("|%d", arg[i] - 100);
  172. switch(arg[i]) {
  173. case 0:
  174. case 22:
  175. if(bold)
  176. cmd("bold");
  177. case 1:
  178. if(!bold)
  179. cmd("bold");
  180. break;
  181. }
  182. }
  183. break;
  184. }
  185. break;
  186. default:
  187. putchar('\033');
  188. ungetch(c);
  189. }
  190. }
  191. void
  192. scroll(int l) {
  193. cmd("seek(%d,%d)", cx, cy + l);
  194. }
  195. void
  196. shell(void) {
  197. static char *shell = NULL;
  198. if(!shell && !(shell = getenv("SHELL")))
  199. shell = "/bin/sh";
  200. pid = fork();
  201. switch(pid) {
  202. case -1:
  203. err(EXIT_FAILURE, "cannot fork");
  204. case 0:
  205. setsid();
  206. dup2(pts, STDIN_FILENO);
  207. dup2(pts, STDOUT_FILENO);
  208. dup2(pts, STDERR_FILENO);
  209. close(ptm);
  210. putenv("TERM=vt102");
  211. execvp(shell, NULL);
  212. break;
  213. default:
  214. close(pts);
  215. signal(SIGCHLD, sigchld);
  216. }
  217. }
  218. void
  219. sigchld(int n) {
  220. int ret;
  221. if(waitpid(pid, &ret, 0) == -1)
  222. err(EXIT_FAILURE, "waiting for child failed");
  223. if(WIFEXITED(ret))
  224. exit(WEXITSTATUS(ret));
  225. else
  226. exit(EXIT_SUCCESS);
  227. }
  228. char
  229. unbuffer(void) {
  230. char c;
  231. c = buf.data[buf.s++];
  232. buf.s %= LENGTH(buf.data);
  233. buf.n--;
  234. return c;
  235. }
  236. void
  237. ungetch(int c) {
  238. if(rbuf.i + 1 >= rbuf.n)
  239. errx(EXIT_FAILURE, "read buffer full");
  240. rbuf.data[rbuf.i++] = c;
  241. }
  242. int
  243. main(int argc, char *argv[]) {
  244. fd_set rfds;
  245. if(argc == 2 && !strcmp("-v", argv[1])) {
  246. fprintf(stderr, "std-"VERSION", © 2008 Matthias-Christian Ott\n");
  247. exit(EXIT_SUCCESS);
  248. }
  249. else if(argc == 1) {
  250. fprintf(stderr, "usage: st [-v]\n");
  251. exit(EXIT_FAILURE);
  252. }
  253. getpty();
  254. shell();
  255. FD_ZERO(&rfds);
  256. FD_SET(STDIN_FILENO, &rfds);
  257. FD_SET(ptm, &rfds);
  258. for(;;) {
  259. if(select(ptm + 1, &rfds, NULL, NULL, NULL) == -1)
  260. err(EXIT_FAILURE, "cannot select");
  261. if(FD_ISSET(ptm, &rfds)) {
  262. do {
  263. c = getch();
  264. switch(c) {
  265. case '\033':
  266. parseesc();
  267. break;
  268. default:
  269. putchar(c);
  270. }
  271. } while(rbuf.i < rbuf.n);
  272. fflush(stdout);
  273. }
  274. }
  275. return 0;
  276. }