My build of suckless st terminal
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.

322 lines
5.4 KiB

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