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.

317 lines
5.4 KiB

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