-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathansicurses.c
More file actions
176 lines (149 loc) · 2.4 KB
/
Copy pathansicurses.c
File metadata and controls
176 lines (149 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <sys/ioctl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include "ansicurses.h"
#define CSI "\033["
static struct termios saved_attr;
static void
getcurpos(WINDOW *win, int *y, int *x)
{
struct termios tmp_attr, attr;
(void)win;
tcgetattr(0, &tmp_attr);
tcgetattr(0, &attr);
attr.c_lflag &= ~(ECHO | ICANON);
tcsetattr(0, TCSAFLUSH, &attr);
printf("\033[6n"); /* TODO write()? */
scanf("\033[%d;%dR", y, x);
*y -= 1;
*x -= 1;
tcsetattr(0, TCSAFLUSH, &tmp_attr);
}
static void
unsetlflag(tcflag_t lflag)
{
struct termios attr;
tcgetattr(STDIN_FILENO, &attr);
attr.c_lflag &= ~lflag;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr);
}
static void
updatesize(void)
{
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
LINES = w.ws_row;
COLS = w.ws_col;
}
static void
winchhandler(int signo)
{
updatesize();
(void)signo;
}
int
cbreak(void)
{
unsetlflag(ICANON);
/* TODO ISIG */
/* TODO IXOFF */
return OK;
}
int
endwin(void)
{
tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_attr);
printf("\n");
return OK;
}
int
erase(void)
{
printf(CSI"2J");
return OK;
}
int
getcurx(WINDOW *win)
{
int x, y;
(void)win;
getyx(stdscr, y, x);
return x;
}
int
getcury(WINDOW *win)
{
int x, y;
(void)win;
getyx(stdscr, y, x);
return y;
}
WINDOW *
initscr(void)
{
tcgetattr(STDIN_FILENO, &saved_attr);
updatesize();
if (signal(SIGWINCH, winchhandler) == SIG_ERR) {
/* TODO ^ not portable! use sigaction? */
fprintf(stderr, "cant catch SIGWINCH");
exit(EXIT_FAILURE);
}
noecho();
cbreak(); /* TODO deviate from ncurses default? */
erase();
move(0, 0);
return stdscr;
}
int
move(int y, int x)
{
printf(CSI"%d;%dH", y+1, x+1);
return OK;
}
int
noecho(void)
{
unsetlflag(ECHO);
/* TODO check result? */
return OK;
}
int
printw(const char *fmt, ...)
{
char buf[BUFSIZ + 1]; /* TODO watch the stack? */
int i;
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, BUFSIZ, fmt, ap);
buf[BUFSIZ] = '\0';
va_end(ap);
for (i = 0; buf[i]; i++) {
if (buf[i] == '\n') {
printf(CSI"0K");
if (getcury(stdscr) == LINES - 1)
continue;
}
if (getcury(stdscr) == LINES -1 && getcurx(stdscr) == COLS -1)
break;
putchar(buf[i]); /* TODO utf */
}
return OK;
}
int
raw(void)
{
unsetlflag(ISIG);
unsetlflag(ICANON);
/* TODO IXOFF */
return OK;
}
int
refresh(void)
{
/* TODO is this a dummy function? */
return OK;
}