Xinqi Bao's Git
2 #include <sys/select.h>
16 #define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
17 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
18 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
21 void cmd(const char *cmdstr
, ...);
22 void *emallocz(unsigned int size
);
23 void eprint(const char *errstr
, ...);
24 void eprintn(const char *errstr
, ...);
26 void movea(int x
, int y
);
27 void mover(int x
, int y
);
35 unsigned char data
[BUFSIZ
];
40 int cols
= 80, lines
= 25;
45 _Bool bold
, digit
, qmark
;
51 if(buf
.n
< LENGTH(buf
.data
))
54 buf
.s
= (buf
.s
+ 1) % LENGTH(buf
.data
);
55 buf
.data
[buf
.e
++] = c
;
56 buf
.e
%= LENGTH(buf
.data
);
60 cmd(const char *cmdstr
, ...) {
66 vfprintf(stdout
, cmdstr
, ap
);
71 emallocz(unsigned int size
) {
72 void *res
= calloc(1, size
);
75 eprint("fatal: could not malloc() %u bytes\n", size
);
80 eprint(const char *errstr
, ...) {
84 vfprintf(stderr
, errstr
, ap
);
90 eprintn(const char *errstr
, ...) {
94 vfprintf(stderr
, errstr
, ap
);
96 fprintf(stderr
, ": %s\n", strerror(errno
));
104 #if defined(_GNU_SOURCE)
106 #elif _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
107 ptm
= posix_openpt(O_RDWR
);
109 ptm
= open("/dev/ptmx", O_RDWR
);
111 if(openpty(&ptm
, &pts
, NULL
, NULL
, NULL
) == -1)
112 eprintn("error, cannot open pty");
114 #if defined(_XOPEN_SOURCE)
116 if(grantpt(ptm
) == -1)
117 eprintn("error, cannot grant access to pty");
118 if(unlockpt(ptm
) == -1)
119 eprintn("error, cannot unlock pty");
120 ptsdev
= ptsname(ptm
);
122 eprintn("error, slave pty name undefined");
123 pts
= open(ptsdev
, O_RDWR
);
125 eprintn("error, cannot open slave pty");
128 eprintn("error, cannot open pty");
133 movea(int x
, int y
) {
138 cmd("s %d,%d", x
, y
);
142 mover(int x
, int y
) {
143 movea(cx
+ x
, cy
+ y
);
151 memset(arg
, 0, LENGTH(arg
));
156 for(j
= 0; j
< LENGTH(arg
);) {
166 eprint("syntax error\n");
183 mover(0, j
? arg
[0] : 1);
186 mover(0, j
? -arg
[0] : -1);
189 mover(j
? arg
[0] : 1, 0);
192 mover(j
? -arg
[0] : -1, 0);
195 /* movel(j ? arg[0] : 1); */
198 /* movel(j ? -arg[0] : -1); */
202 movea(j
? arg
[0] : 1, cy
);
206 movea(arg
[1] ? arg
[1] : 1, arg
[0] ? arg
[0] : 1);
208 /* insline(j ? arg[0] : 1); */
211 /* delline(j ? arg[0] : 1); */
216 scroll(j
? arg
[0] : 1);
219 scroll(j
? -arg
[0] : -1);
222 movea(cx
, j
? arg
[0] : 1);
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);
257 cmd("s %d, %d", cx
, cy
+ l
);
262 static char *shell
= NULL
;
264 if(!shell
&& !(shell
= getenv("SHELL")))
269 eprint("error, cannot fork\n");
272 dup2(pts
, STDIN_FILENO
);
273 dup2(pts
, STDOUT_FILENO
);
274 dup2(pts
, STDERR_FILENO
);
276 putenv("TERM=vt102");
281 signal(SIGCHLD
, sigchld
);
289 if(waitpid(pid
, &ret
, 0) == -1)
290 eprintn("error, waiting for child failed");
292 exit(WEXITSTATUS(ret
));
301 c
= buf
.data
[buf
.s
++];
302 buf
.s
%= LENGTH(buf
.data
);
308 main(int argc
, char *argv
[]) {
309 if(argc
== 2 && !strcmp("-v", argv
[1]))
310 eprint("std-"VERSION
", © 2008 Matthias-Christian Ott\n");
312 eprint("usage: st [-v]\n");
315 fptm
= fdopen(ptm
, "r+");
317 eprintn("cannot open slave pty");