std.c 5.8 KB

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