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.

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