std.c 6.2 KB

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