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.

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