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.

297 lines
4.8 KiB

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