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.

261 lines
4.2 KiB

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