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.

320 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. #endif
  100. #if defined(_XOPEN_SOURCE)
  101. if(ptm != -1) {
  102. if(grantpt(ptm) == -1)
  103. eprintn("error, cannot grant access to pty");
  104. if(unlockpt(ptm) == -1)
  105. eprintn("error, cannot unlock pty");
  106. ptsdev = ptsname(ptm);
  107. if(!ptsdev)
  108. eprintn("error, slave pty name undefined");
  109. pts = open(ptsdev, O_RDWR);
  110. if(pts == -1)
  111. eprintn("error, cannot open slave pty");
  112. }
  113. else
  114. eprintn("error, cannot open pty");
  115. #endif
  116. }
  117. void
  118. movea(int x, int y) {
  119. x = MAX(x, cols);
  120. y = MAX(y, lines);
  121. cx = x;
  122. cy = y;
  123. cmd("s %d,%d", x, y);
  124. }
  125. void
  126. mover(int x, int y) {
  127. movea(cx + x, cy + y);
  128. }
  129. void
  130. parseesc(void) {
  131. int i, j;
  132. int arg[16];
  133. memset(arg, 0, LENGTH(arg));
  134. s = 0;
  135. c = getc(fptm);
  136. switch(c) {
  137. case '[':
  138. c = getc(fptm);
  139. for(j = 0; j < LENGTH(arg);) {
  140. if(isdigit(c)) {
  141. s |= Digit;
  142. arg[j] *= 10;
  143. arg[j] += c - '0';
  144. }
  145. else if(c == '?')
  146. s |= QuestionMark;
  147. else if(c == ';') {
  148. if(!(s & Digit))
  149. eprint("syntax error");
  150. s &= ~Digit;
  151. j++;
  152. }
  153. else {
  154. if(s & Digit) {
  155. s &= ~Digit;
  156. j++;
  157. }
  158. break;
  159. }
  160. c = getc(fptm);
  161. }
  162. switch(c) {
  163. case '@':
  164. break;
  165. case 'A':
  166. mover(0, j ? arg[0] : 1);
  167. break;
  168. case 'B':
  169. mover(0, j ? -arg[0] : -1);
  170. break;
  171. case 'C':
  172. mover(j ? arg[0] : 1, 0);
  173. break;
  174. case 'D':
  175. mover(j ? -arg[0] : -1, 0);
  176. break;
  177. case 'E':
  178. /* movel(j ? arg[0] : 1); */
  179. break;
  180. case 'F':
  181. /* movel(j ? -arg[0] : -1); */
  182. break;
  183. case '`':
  184. case 'G':
  185. movea(j ? arg[0] : 1, cy);
  186. break;
  187. case 'f':
  188. case 'H':
  189. movea(arg[1] ? arg[1] : 1, arg[0] ? arg[0] : 1);
  190. case 'L':
  191. /* insline(j ? arg[0] : 1); */
  192. break;
  193. case 'M':
  194. /* delline(j ? arg[0] : 1); */
  195. break;
  196. case 'P':
  197. break;
  198. case 'S':
  199. scroll(j ? arg[0] : 1);
  200. break;
  201. case 'T':
  202. scroll(j ? -arg[0] : -1);
  203. break;
  204. case 'd':
  205. movea(cx, j ? arg[0] : 1);
  206. break;
  207. case 'm':
  208. for(i = 0; i < j; i++) {
  209. if(arg[i] >= 30 && arg[i] <= 37)
  210. cmd("#%d", arg[i] - 30);
  211. if(arg[i] >= 40 && arg[i] <= 47)
  212. cmd("|%d", arg[i] - 40);
  213. /* xterm bright colors */
  214. if(arg[i] >= 90 && arg[i] <= 97)
  215. cmd("#%d", arg[i] - 90);
  216. if(arg[i] >= 100 && arg[i] <= 107)
  217. cmd("|%d", arg[i] - 100);
  218. switch(arg[i]) {
  219. case 0:
  220. case 22:
  221. if(bold)
  222. cmd("b");
  223. case 1:
  224. if(!bold)
  225. cmd("b");
  226. break;
  227. }
  228. }
  229. break;
  230. }
  231. break;
  232. default:
  233. putchar('\033');
  234. ungetc(c, fptm);
  235. }
  236. }
  237. void
  238. scroll(int l) {
  239. cmd("s %d, %d", cx, cy + l);
  240. }
  241. void
  242. shell(void) {
  243. static char *shell = NULL;
  244. if(!shell && !(shell = getenv("SHELL")))
  245. shell = "/bin/sh";
  246. pid = fork();
  247. switch(pid) {
  248. case -1:
  249. eprint("error, cannot fork\n");
  250. case 0:
  251. setsid();
  252. dup2(pts, STDIN_FILENO);
  253. dup2(pts, STDOUT_FILENO);
  254. dup2(pts, STDERR_FILENO);
  255. close(ptm);
  256. putenv("TERM=vt102");
  257. execvp(shell, NULL);
  258. break;
  259. default:
  260. close(pts);
  261. signal(SIGCHLD, sigchld);
  262. }
  263. }
  264. void
  265. sigchld(int n) {
  266. int ret;
  267. if(waitpid(pid, &ret, 0) == -1)
  268. eprintn("error, waiting for child failed");
  269. if(WIFEXITED(ret))
  270. exit(WEXITSTATUS(ret));
  271. else
  272. exit(EXIT_SUCCESS);
  273. }
  274. char
  275. unbuffer(void) {
  276. char c;
  277. c = buf.data[buf.s++];
  278. buf.s %= LENGTH(buf.data);
  279. buf.n--;
  280. return c;
  281. }
  282. int
  283. main(int argc, char *argv[]) {
  284. fd_set rd;
  285. if(argc == 2 && !strcmp("-v", argv[1]))
  286. eprint("std-"VERSION", © 2008 Matthias-Christian Ott\n");
  287. else if(argc == 1)
  288. eprint("usage: st [-v]\n");
  289. getpty();
  290. shell();
  291. return 0;
  292. }