-
Notifications
You must be signed in to change notification settings - Fork 7
/
text.c
318 lines (280 loc) · 5.81 KB
/
text.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <lib.h>
#include <reg.h>
/*
* tiny text file stream
*/
#define TEXT_BUFSIZE 4096
#define TEXT_BUF_END (&text_buf[TEXT_BUFSIZE])
#define TEXT_BUF_START text_buf
#define TEXT_LINE 0
#define TEXT_CHAR 1
/* text file */
static int text_fd = -1;
static int text_eof;
/* text buffer */
#define TEXT_REAL_BUFSIZE (TEXT_BUFSIZE + 1) /* add '\0' at tail */
static char text_buf[TEXT_REAL_BUFSIZE];
static char *text_pos;
static char *text_end;
static char *text_prev_pos; /* prev pos for error output */
static int text_prev_op; /* prev operation: getchar or getline */
/* line */
static int text_lineno;
/* backup line */
static char *text_prev_linetail;
static int text_prev_lineno = 0;
/* backup terminal */
#define TEXT_TERMINAL '\0'
static char text_term_char;
static char *text_term_pos;
void text_err(char *str)
{
char *p;
fprintf(stderr, "Syntax error:%s.", str);
fprintf(stderr, " At line %d:", text_lineno);
if (text_prev_op == TEXT_CHAR) {
fputc(text_prev_pos ? *text_prev_pos : ' ', stderr);
} else if (text_prev_op == TEXT_LINE && text_prev_pos) {
for (p = text_prev_pos;
p < TEXT_BUF_END && *p && *p != '\n';
p++)
fputc(*p, stderr);
}
fputc('\n', stderr);
}
void text_errx(char *str)
{
text_err(str);
exit(EXIT_FAILURE);
}
/* get current text position */
char *text_getpos(void)
{
return text_pos;
}
/* mark terminal at @pos */
void text_term(char *pos)
{
text_term_pos = pos;
text_term_char = *pos;
*pos = TEXT_TERMINAL;
}
/* restore pos */
void text_unterm(void)
{
if (text_term_pos) {
*text_term_pos = text_term_char;
text_term_char = TEXT_TERMINAL;
text_term_pos = NULL;
}
}
/* default save for error output */
void text_save_pos(int op)
{
text_prev_pos = text_pos;
text_prev_op = op;
}
static void text_save(void)
{
char *p;
*TEXT_BUF_START = text_pos[-1]; /* save prev char */
p = TEXT_BUF_START + 1;
while (text_pos < text_end)
*p++ = *text_pos++;
text_pos = TEXT_BUF_START + 1;
text_end = p; /* text_end[0] is invalid */
}
static void text_fill_buf(void)
{
int n;
if (text_eof)
return;
text_save();
n = read(text_fd, text_end, TEXT_BUF_END - text_end);
if (n < 0) {
errexit("text_fill_buf read error");
} else if (n == 0) {
text_eof = 1;
/* safe for text_getline */
*text_end = EOF;
} else {
text_end += n;
}
}
char text_getchar(void)
{
text_unterm();
if (text_pos >= text_end)
text_fill_buf();
if (text_eof && text_pos >= text_end)
return EOF;
text_save_pos(TEXT_CHAR);
return *text_pos++;
}
/* look ahead @len chars */
char *text_lookahead(int len)
{
if (len < 1)
return NULL;
text_unterm();
if (text_pos + len - 1 >= text_end)
text_fill_buf();
if (text_eof && (text_pos + len - 1 >= text_end))
return NULL;
return text_pos;
}
/* It can only run once after text_getchar()! */
void text_backchar(void)
{
/*
* No call text_unterm() here ,
* it will be called in text_get*().
*/
text_pos--;
}
int text_lookback(void)
{
return text_pos[-1];
}
int text_prevline(char **linp)
{
int len = 0;
if (text_prev_linetail) {
/* Can backed line be usable still? */
if (text_prev_lineno == text_lineno + 1) {
/* default save for error output */
text_save_pos(TEXT_LINE);
/* return value */
if (linp)
*linp = text_pos;
len = text_prev_linetail - text_pos;
/* get prev line */
text_pos = text_prev_linetail;
text_lineno++;
text_term(text_pos);
}
text_prev_linetail = NULL;
}
return len;
}
int text_getline(char **line)
{
int len = 0;
text_unterm();
if (len = text_prevline(line))
return len;
/* when EOF, it is safe, because text_end[0] == EOF */
while (text_pos[len] != '\n') {
if (&text_pos[len] >= text_end)
text_fill_buf();
if (text_eof && &text_pos[len] >= text_end) {
if (text_pos >= text_end)
return 0;
/* for line which has no tailing '\n' */
len--;
break;
}
len++;
}
if (text_lookback() == '\n')
text_lineno++;
if (line)
*line = text_pos;
text_save_pos(TEXT_LINE);
text_pos += len + 1; /* 1 for skipping '\n' */
text_term(text_pos);
return len + 1; /* contain '\n' char */
}
/*
* line buffer:
* |<....>|\n|?|
* | `---> lex_line_end
* `-------------> line
*/
/* not check line */
void text_backline(char *line)
{
text_unterm();
text_prev_linetail = text_pos;
text_pos = line;
text_prev_lineno = text_lineno;
text_lineno--;
}
void text_open(char *file)
{
/* open file */
if (!file)
errexit("open NULL file");
text_fd = open(file, O_RDONLY);
if (text_fd == -1)
errexit("cannot open file");
/* buf */
text_pos = TEXT_BUF_START + 1; /* skip prev line tail */
text_end = TEXT_BUF_START + 1;
*text_pos = TEXT_TERMINAL; /* for text_getline */
text_prev_pos = NULL;
/* terminal */
text_term_pos = NULL;
text_term_char = TEXT_TERMINAL;
/* line */
*TEXT_BUF_START = '\n'; /* dummy prev line tail */
*TEXT_BUF_END = TEXT_TERMINAL;
text_lineno = 0;
}
int skip_whitespace(void)
{
int c;
/* skip space char and space line */
do {
c = text_getchar();
} while (isspace(c));
if (c != EOF)
text_backchar();
return c;
}
#ifdef LEX_TEXT_TEST
int main(int argc, char **argv)
{
char *line;
int len;
int c, k;
if (argc != 2) {
dbg("Usage: %s file", argv[0]);
exit(EXIT_FAILURE);
}
text_open(argv[1]);
/*
while ((c = text_getchar()) != EOF) {
putchar(c);
len = text_getline(&line);
if (!len)
break;
printf("%s", line);
}
*/
/*
while ((c = text_getchar()) != EOF) {
putchar(c);
}
*/
/*
while (len = text_getline(&line)) {
printf("%s", line);
}
*/
/* double line
k = 0;
while (len = text_getline(&line)) {
printf(green(%-4d)red(:)"%s", text_lineno, line);
if (++k & 1)
text_backline(line);
}
*/
while (len = text_getline(&line))
printf(green(%-4d)red(:)"%s", text_lineno, line);
return 0;
}
#endif /* LEX_TEXT_TEST */