1283 |
mateusz.vi |
1 |
/* Sved, the SvarDOS editor
|
|
|
2 |
*
|
|
|
3 |
* Copyright (C) 2023 Mateusz Viste
|
|
|
4 |
*
|
|
|
5 |
* Sved is released under the terms of the MIT license.
|
|
|
6 |
*
|
|
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
8 |
* of this software and associated documentation files (the "Software"), to
|
|
|
9 |
* deal in the Software without restriction, including without limitation the
|
|
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
12 |
* furnished to do so, subject to the following conditions:
|
|
|
13 |
*
|
|
|
14 |
* The above copyright notice and this permission notice shall be included in
|
|
|
15 |
* all copies or substantial portions of the Software.
|
|
|
16 |
*
|
|
|
17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
18 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
20 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
21 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
23 |
* IN THE SOFTWARE.
|
|
|
24 |
*/
|
1275 |
mateusz.vi |
25 |
|
1282 |
mateusz.vi |
26 |
#include <dos.h>
|
|
|
27 |
#include <fcntl.h>
|
1275 |
mateusz.vi |
28 |
#include <stdlib.h>
|
|
|
29 |
#include <string.h>
|
1288 |
mateusz.vi |
30 |
#include <malloc.h> /* _fcalloc() */
|
1275 |
mateusz.vi |
31 |
|
|
|
32 |
#include "mdr\cout.h"
|
1307 |
mateusz.vi |
33 |
#include "mdr\dos.h"
|
1275 |
mateusz.vi |
34 |
#include "mdr\keyb.h"
|
|
|
35 |
|
1282 |
mateusz.vi |
36 |
#include "svarlang\svarlang.h"
|
|
|
37 |
|
1275 |
mateusz.vi |
38 |
#define COL_TXT 0
|
|
|
39 |
#define COL_STATUSBAR1 1
|
|
|
40 |
#define COL_STATUSBAR2 2
|
|
|
41 |
#define COL_SCROLLBAR 3
|
|
|
42 |
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
|
|
|
43 |
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
|
|
|
44 |
|
|
|
45 |
#define SCROLL_CURSOR 0xB1
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
struct line {
|
1288 |
mateusz.vi |
49 |
struct line far *prev;
|
|
|
50 |
struct line far *next;
|
1275 |
mateusz.vi |
51 |
unsigned short len;
|
|
|
52 |
char payload[1];
|
|
|
53 |
};
|
|
|
54 |
|
|
|
55 |
struct linedb {
|
1288 |
mateusz.vi |
56 |
struct line far *cursor;
|
1275 |
mateusz.vi |
57 |
unsigned short xoffset;
|
|
|
58 |
};
|
|
|
59 |
|
|
|
60 |
|
1287 |
mateusz.vi |
61 |
/* returns non-zero on error */
|
1289 |
mateusz.vi |
62 |
static int line_add(struct linedb *db, const char far *line) {
|
|
|
63 |
unsigned short slen;
|
1288 |
mateusz.vi |
64 |
struct line far *l;
|
1275 |
mateusz.vi |
65 |
|
1289 |
mateusz.vi |
66 |
/* slen = strlen(line) (but for far pointer) */
|
|
|
67 |
for (slen = 0; line[slen] != 0; slen++);
|
|
|
68 |
|
1275 |
mateusz.vi |
69 |
/* trim out CR/LF line endings */
|
|
|
70 |
if ((slen >= 2) && (line[slen - 2] == '\r')) {
|
|
|
71 |
slen -= 2;
|
|
|
72 |
} else if ((slen >= 1) && (line[slen - 1] == '\n')) {
|
|
|
73 |
slen--;
|
|
|
74 |
}
|
|
|
75 |
|
1288 |
mateusz.vi |
76 |
l = _fcalloc(1, sizeof(struct line) + slen + 1);
|
1287 |
mateusz.vi |
77 |
if (l == NULL) return(-1);
|
|
|
78 |
|
1275 |
mateusz.vi |
79 |
l->prev = db->cursor;
|
|
|
80 |
if (db->cursor) {
|
|
|
81 |
l->next = db->cursor->next;
|
|
|
82 |
db->cursor->next = l;
|
|
|
83 |
l->next->prev = l;
|
|
|
84 |
}
|
|
|
85 |
db->cursor = l;
|
1288 |
mateusz.vi |
86 |
_fmemcpy(l->payload, line, slen);
|
1275 |
mateusz.vi |
87 |
l->len = slen;
|
1287 |
mateusz.vi |
88 |
|
|
|
89 |
return(0);
|
1275 |
mateusz.vi |
90 |
}
|
|
|
91 |
|
|
|
92 |
|
1289 |
mateusz.vi |
93 |
static void db_rewind(struct linedb *db) {
|
1275 |
mateusz.vi |
94 |
if (db->cursor == NULL) return;
|
|
|
95 |
while (db->cursor->prev) db->cursor = db->cursor->prev;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
|
1289 |
mateusz.vi |
99 |
static void load_colorscheme(void) {
|
1275 |
mateusz.vi |
100 |
scheme[COL_TXT] = 0x17;
|
|
|
101 |
scheme[COL_STATUSBAR1] = 0x70;
|
|
|
102 |
scheme[COL_STATUSBAR2] = 0x78;
|
|
|
103 |
scheme[COL_SCROLLBAR] = 0x70;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
|
1282 |
mateusz.vi |
107 |
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname) {
|
1275 |
mateusz.vi |
108 |
unsigned char i;
|
1302 |
mateusz.vi |
109 |
const char *s = svarlang_strid(0); /* HELP */
|
1275 |
mateusz.vi |
110 |
unsigned char helpcol = screenw - (strlen(s) + 4);
|
|
|
111 |
|
|
|
112 |
/* clear screen */
|
|
|
113 |
mdr_cout_cls(scheme[COL_TXT]);
|
|
|
114 |
|
|
|
115 |
/* status bar */
|
|
|
116 |
for (i = 0; i < helpcol; i++) {
|
|
|
117 |
mdr_cout_char(screenh - 1, i, *fname, scheme[COL_STATUSBAR1]);
|
|
|
118 |
if (*fname != 0) fname++;
|
|
|
119 |
}
|
|
|
120 |
mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
|
|
|
121 |
mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
|
|
|
122 |
|
|
|
123 |
/* scroll bar */
|
|
|
124 |
for (i = 0; i < (screenh - 1); i++) {
|
|
|
125 |
mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
|
1309 |
mateusz.vi |
130 |
static void ui_help(unsigned char screenw) {
|
|
|
131 |
#define MAXLINLEN 35
|
|
|
132 |
unsigned short i, x, offset;
|
|
|
133 |
offset = (screenw - MAXLINLEN + 1) >> 1;
|
|
|
134 |
mdr_cout_cursor_hide();
|
|
|
135 |
for (i = 2; i <= 12; i++) {
|
|
|
136 |
for (x = offset - 2; x < offset + MAXLINLEN; x++) mdr_cout_char(i, x, ' ', scheme[COL_STATUSBAR1]);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
mdr_cout_str(3, offset, svarlang_str(0, 0), scheme[COL_STATUSBAR1], MAXLINLEN);
|
|
|
140 |
for (i = 0; i <= 2; i++) {
|
|
|
141 |
mdr_cout_str(5 + i, offset, svarlang_str(8, i), scheme[COL_STATUSBAR1], MAXLINLEN);
|
|
|
142 |
}
|
|
|
143 |
mdr_cout_str(9, offset, svarlang_str(8, 10), scheme[COL_STATUSBAR1], MAXLINLEN);
|
|
|
144 |
mdr_cout_str(11, offset, svarlang_str(8, 11), scheme[COL_STATUSBAR1], MAXLINLEN);
|
|
|
145 |
|
|
|
146 |
keyb_getkey();
|
|
|
147 |
mdr_cout_cursor_show();
|
|
|
148 |
#undef MAXLINLEN
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
|
1310 |
mateusz.vi |
152 |
static void ui_refresh(const struct linedb *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto, unsigned char y) {
|
1275 |
mateusz.vi |
153 |
unsigned char len;
|
1310 |
mateusz.vi |
154 |
const struct line far *l;
|
1275 |
mateusz.vi |
155 |
|
1288 |
mateusz.vi |
156 |
#ifdef DBG_REFRESH
|
1282 |
mateusz.vi |
157 |
static char m = 'a';
|
|
|
158 |
m++;
|
|
|
159 |
if (m > 'z') m = 'a';
|
1288 |
mateusz.vi |
160 |
#endif
|
1282 |
mateusz.vi |
161 |
|
1310 |
mateusz.vi |
162 |
/* rewind cursor line to first line that needs redrawing */
|
|
|
163 |
for (l = db->cursor; y > uidirtyfrom; y--) l = l->prev;
|
1282 |
mateusz.vi |
164 |
|
1310 |
mateusz.vi |
165 |
/* iterate over lines and redraw whatever needs to be redrawn */
|
|
|
166 |
for (; l != NULL; l = l->next, y++) {
|
|
|
167 |
|
|
|
168 |
/* skip lines that do not need to be refreshed */
|
1289 |
mateusz.vi |
169 |
if (y < uidirtyfrom) continue;
|
|
|
170 |
if (y > uidirtyto) break;
|
1282 |
mateusz.vi |
171 |
|
1275 |
mateusz.vi |
172 |
if (db->xoffset < l->len) {
|
1288 |
mateusz.vi |
173 |
for (len = 0; l->payload[len] != 0; len++) mdr_cout_char(y, len, l->payload[len], scheme[COL_TXT]);
|
1275 |
mateusz.vi |
174 |
} else {
|
|
|
175 |
len = 0;
|
|
|
176 |
}
|
|
|
177 |
while (len < screenw - 1) mdr_cout_char(y, len++, ' ', scheme[COL_TXT]);
|
1282 |
mateusz.vi |
178 |
|
1288 |
mateusz.vi |
179 |
#ifdef DBG_REFRESH
|
1282 |
mateusz.vi |
180 |
mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
|
1288 |
mateusz.vi |
181 |
#endif
|
1282 |
mateusz.vi |
182 |
|
1275 |
mateusz.vi |
183 |
if (y == screenh - 2) break;
|
|
|
184 |
}
|
1310 |
mateusz.vi |
185 |
|
1275 |
mateusz.vi |
186 |
}
|
|
|
187 |
|
|
|
188 |
|
1282 |
mateusz.vi |
189 |
static void check_cursor_not_after_eol(struct linedb *db, unsigned char *cursorpos, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
|
1275 |
mateusz.vi |
190 |
if (db->xoffset + *cursorpos <= db->cursor->len) return;
|
|
|
191 |
|
|
|
192 |
if (db->cursor->len < db->xoffset) {
|
|
|
193 |
*cursorpos = 0;
|
|
|
194 |
db->xoffset = db->cursor->len;
|
1282 |
mateusz.vi |
195 |
*uidirtyfrom = 0;
|
|
|
196 |
*uidirtyto = 0xff;
|
1275 |
mateusz.vi |
197 |
} else {
|
|
|
198 |
*cursorpos = db->cursor->len - db->xoffset;
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
|
1282 |
mateusz.vi |
203 |
static void cursor_up(struct linedb *db, unsigned char *cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
|
1275 |
mateusz.vi |
204 |
if (db->cursor->prev != NULL) {
|
|
|
205 |
db->cursor = db->cursor->prev;
|
|
|
206 |
if (*cursorposy == 0) {
|
1282 |
mateusz.vi |
207 |
*uidirtyfrom = 0;
|
|
|
208 |
*uidirtyto = 0xff;
|
1275 |
mateusz.vi |
209 |
} else {
|
|
|
210 |
*cursorposy -= 1;
|
|
|
211 |
}
|
|
|
212 |
}
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
|
1282 |
mateusz.vi |
216 |
static void cursor_eol(struct linedb *db, unsigned char *cursorposx, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
|
1275 |
mateusz.vi |
217 |
/* adjust xoffset to make sure eol is visible on screen */
|
1282 |
mateusz.vi |
218 |
if (db->xoffset > db->cursor->len) {
|
|
|
219 |
db->xoffset = db->cursor->len - 1;
|
|
|
220 |
*uidirtyfrom = 0;
|
|
|
221 |
*uidirtyto = 0xff;
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
if (db->xoffset + screenw - 1 <= db->cursor->len) {
|
|
|
225 |
db->xoffset = db->cursor->len - screenw + 2;
|
|
|
226 |
*uidirtyfrom = 0;
|
|
|
227 |
*uidirtyto = 0xff;
|
|
|
228 |
}
|
1275 |
mateusz.vi |
229 |
*cursorposx = db->cursor->len - db->xoffset;
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
|
1282 |
mateusz.vi |
233 |
static void cursor_down(struct linedb *db, unsigned char *cursorposy, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
|
1276 |
mateusz.vi |
234 |
if (db->cursor->next != NULL) {
|
|
|
235 |
db->cursor = db->cursor->next;
|
|
|
236 |
if (*cursorposy < screenh - 2) {
|
|
|
237 |
*cursorposy += 1;
|
|
|
238 |
} else {
|
1282 |
mateusz.vi |
239 |
*uidirtyfrom = 0;
|
|
|
240 |
*uidirtyto = 0xff;
|
1276 |
mateusz.vi |
241 |
}
|
|
|
242 |
}
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
|
1302 |
mateusz.vi |
246 |
static void cursor_left(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
|
|
|
247 |
if (*cursorposx > 0) {
|
|
|
248 |
*cursorposx -= 1;
|
|
|
249 |
} else if (db->xoffset > 0) {
|
|
|
250 |
db->xoffset -= 1;
|
|
|
251 |
*uidirtyfrom = 0;
|
|
|
252 |
*uidirtyto = 0xff;
|
|
|
253 |
} else if (db->cursor->prev != NULL) { /* jump to end of line above */
|
|
|
254 |
cursor_up(db, cursorposy, uidirtyfrom, uidirtyto);
|
|
|
255 |
cursor_eol(db, cursorposx, screenw, uidirtyfrom, uidirtyto);
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
|
1282 |
mateusz.vi |
260 |
static void cursor_home(struct linedb *db, unsigned char *cursorposx, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
|
1276 |
mateusz.vi |
261 |
*cursorposx = 0;
|
1282 |
mateusz.vi |
262 |
if (db->xoffset != 0) {
|
|
|
263 |
db->xoffset = 0;
|
|
|
264 |
*uidirtyfrom = 0;
|
|
|
265 |
*uidirtyto = 0xff;
|
|
|
266 |
}
|
1276 |
mateusz.vi |
267 |
}
|
|
|
268 |
|
|
|
269 |
|
1308 |
mateusz.vi |
270 |
static void cursor_right(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
|
|
|
271 |
if (db->cursor->len > db->xoffset + *cursorposx) {
|
|
|
272 |
if (*cursorposx < screenw - 2) {
|
|
|
273 |
*cursorposx += 1;
|
|
|
274 |
} else {
|
|
|
275 |
db->xoffset += 1;
|
|
|
276 |
*uidirtyfrom = 0;
|
|
|
277 |
*uidirtyto = 0xff;
|
|
|
278 |
}
|
|
|
279 |
} else {
|
|
|
280 |
cursor_down(db, cursorposy, screenh, uidirtyfrom, uidirtyto);
|
|
|
281 |
cursor_home(db, cursorposx, uidirtyfrom, uidirtyto);
|
|
|
282 |
}
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
|
1292 |
mateusz.vi |
286 |
static void del(struct linedb *db, unsigned char cursorposx, unsigned char cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
|
|
|
287 |
if (cursorposx + db->xoffset < db->cursor->len) {
|
|
|
288 |
_fmemmove(db->cursor->payload + cursorposx + db->xoffset, db->cursor->payload + cursorposx + db->xoffset + 1, db->cursor->len - cursorposx - db->xoffset);
|
|
|
289 |
db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
|
|
|
290 |
*uidirtyfrom = cursorposy;
|
|
|
291 |
*uidirtyto = cursorposy;
|
|
|
292 |
} else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
|
|
|
293 |
struct line far *nextline = db->cursor->next;
|
|
|
294 |
if (db->cursor->next->len > 0) {
|
|
|
295 |
void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
|
|
|
296 |
if (newptr != NULL) {
|
|
|
297 |
db->cursor = newptr;
|
|
|
298 |
_fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
|
|
|
299 |
db->cursor->len += db->cursor->next->len;
|
|
|
300 |
}
|
|
|
301 |
}
|
|
|
302 |
db->cursor->next = db->cursor->next->next;
|
|
|
303 |
db->cursor->next->prev = db->cursor;
|
|
|
304 |
if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
|
|
|
305 |
_ffree(nextline);
|
|
|
306 |
*uidirtyfrom = cursorposy;
|
|
|
307 |
*uidirtyto = 0xff;
|
|
|
308 |
}
|
|
|
309 |
}
|
|
|
310 |
|
|
|
311 |
|
1302 |
mateusz.vi |
312 |
static void bkspc(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
|
|
|
313 |
|
|
|
314 |
/* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
|
|
|
315 |
if ((*cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
|
|
|
316 |
|
|
|
317 |
cursor_left(db, cursorposx, cursorposy, screenw, uidirtyfrom, uidirtyto);
|
|
|
318 |
del(db, *cursorposx, *cursorposy, uidirtyfrom, uidirtyto);
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
|
1286 |
mateusz.vi |
322 |
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
|
|
|
323 |
* of argc and argv, saves some 330 bytes of binary size */
|
|
|
324 |
static char *parseargv(void) {
|
|
|
325 |
char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
|
|
|
326 |
unsigned char count = 0;
|
|
|
327 |
char *argv[4];
|
|
|
328 |
|
|
|
329 |
while (count < 4) {
|
|
|
330 |
/* jump to nearest arg */
|
|
|
331 |
while (*tail == ' ') {
|
|
|
332 |
*tail = 0;
|
|
|
333 |
tail++;
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
if (*tail == '\r') {
|
|
|
337 |
*tail = 0;
|
|
|
338 |
break;
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
argv[count++] = tail;
|
|
|
342 |
|
|
|
343 |
/* jump to next delimiter */
|
|
|
344 |
while ((*tail != ' ') && (*tail != '\r')) tail++;
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
/* check args now */
|
|
|
348 |
if (count != 1) return(NULL);
|
|
|
349 |
|
|
|
350 |
return(argv[0]);
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
|
1287 |
mateusz.vi |
354 |
static int loadfile(struct linedb *db, const char *fname) {
|
|
|
355 |
char buff[1024];
|
|
|
356 |
unsigned int prevlen = 0, len, llen;
|
|
|
357 |
int fd;
|
|
|
358 |
int r = 0;
|
|
|
359 |
|
|
|
360 |
if (_dos_open(fname, O_RDONLY, &fd) != 0) {
|
|
|
361 |
mdr_coutraw_puts("Failed to open file:");
|
|
|
362 |
mdr_coutraw_puts(fname);
|
|
|
363 |
return(-1);
|
|
|
364 |
}
|
|
|
365 |
|
|
|
366 |
do {
|
|
|
367 |
if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
|
|
|
368 |
len += prevlen;
|
|
|
369 |
} else {
|
|
|
370 |
len = prevlen;
|
|
|
371 |
}
|
|
|
372 |
|
|
|
373 |
/* look for nearest \n and replace with 0*/
|
|
|
374 |
for (llen = 0; buff[llen] != '\n'; llen++) {
|
|
|
375 |
if (llen == sizeof(buff)) break;
|
|
|
376 |
}
|
|
|
377 |
buff[llen] = 0;
|
|
|
378 |
if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
|
|
|
379 |
if (line_add(db, buff) != 0) {
|
|
|
380 |
mdr_coutraw_puts("out of memory");
|
|
|
381 |
r = -1;
|
|
|
382 |
break;
|
|
|
383 |
}
|
|
|
384 |
|
|
|
385 |
len -= llen + 1;
|
|
|
386 |
memmove(buff, buff + llen + 1, len);
|
|
|
387 |
prevlen = len;
|
|
|
388 |
} while (len > 0);
|
|
|
389 |
|
|
|
390 |
_dos_close(fd);
|
|
|
391 |
|
|
|
392 |
return(r);
|
|
|
393 |
}
|
|
|
394 |
|
|
|
395 |
|
1286 |
mateusz.vi |
396 |
int main(void) {
|
|
|
397 |
const char *fname;
|
1275 |
mateusz.vi |
398 |
struct linedb db;
|
|
|
399 |
unsigned char screenw = 0, screenh = 0;
|
|
|
400 |
unsigned char cursorposx = 0, cursorposy = 0;
|
1282 |
mateusz.vi |
401 |
unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
|
1275 |
mateusz.vi |
402 |
|
|
|
403 |
bzero(&db, sizeof(db));
|
|
|
404 |
|
1307 |
mateusz.vi |
405 |
{
|
|
|
406 |
char nlspath[128], lang[8];
|
|
|
407 |
svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
|
|
|
408 |
}
|
1282 |
mateusz.vi |
409 |
|
1286 |
mateusz.vi |
410 |
fname = parseargv();
|
|
|
411 |
|
|
|
412 |
if (fname == NULL) {
|
1302 |
mateusz.vi |
413 |
mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
|
1275 |
mateusz.vi |
414 |
return(0);
|
|
|
415 |
}
|
|
|
416 |
|
1282 |
mateusz.vi |
417 |
/* load file */
|
1287 |
mateusz.vi |
418 |
if (loadfile(&db, fname) != 0) return(1);
|
1282 |
mateusz.vi |
419 |
|
1275 |
mateusz.vi |
420 |
/* add an empty line at end if not present already */
|
|
|
421 |
if (db.cursor->len != 0) line_add(&db, "");
|
|
|
422 |
|
|
|
423 |
if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
|
|
|
424 |
ui_basic(screenw, screenh, fname);
|
|
|
425 |
|
|
|
426 |
db_rewind(&db);
|
|
|
427 |
|
|
|
428 |
for (;;) {
|
|
|
429 |
int k;
|
|
|
430 |
|
1282 |
mateusz.vi |
431 |
check_cursor_not_after_eol(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
|
1275 |
mateusz.vi |
432 |
mdr_cout_locate(cursorposy, cursorposx);
|
|
|
433 |
|
1282 |
mateusz.vi |
434 |
if (uidirtyfrom != 0xff) {
|
1310 |
mateusz.vi |
435 |
ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto, cursorposy);
|
1282 |
mateusz.vi |
436 |
uidirtyfrom = 0xff;
|
|
|
437 |
}
|
1275 |
mateusz.vi |
438 |
|
|
|
439 |
k = keyb_getkey();
|
1282 |
mateusz.vi |
440 |
|
1275 |
mateusz.vi |
441 |
if (k == 0x150) { /* down */
|
1282 |
mateusz.vi |
442 |
cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
|
1275 |
mateusz.vi |
443 |
|
|
|
444 |
} else if (k == 0x148) { /* up */
|
1282 |
mateusz.vi |
445 |
cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
|
1275 |
mateusz.vi |
446 |
|
|
|
447 |
} else if (k == 0x14D) { /* right */
|
1308 |
mateusz.vi |
448 |
cursor_right(&db, &cursorposx, &cursorposy, screenw, screenh, &uidirtyfrom, &uidirtyto);
|
1275 |
mateusz.vi |
449 |
|
|
|
450 |
} else if (k == 0x14B) { /* left */
|
1302 |
mateusz.vi |
451 |
cursor_left(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
|
1275 |
mateusz.vi |
452 |
|
1282 |
mateusz.vi |
453 |
} else if (k == 0x149) { /* pgup */
|
|
|
454 |
// TODO
|
|
|
455 |
|
|
|
456 |
} else if (k == 0x151) { /* pgdown */
|
|
|
457 |
// TODO
|
|
|
458 |
|
|
|
459 |
} else if (k == 0x147) { /* home */
|
|
|
460 |
cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
|
|
|
461 |
|
|
|
462 |
} else if (k == 0x14F) { /* end */
|
|
|
463 |
cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
|
|
|
464 |
|
1275 |
mateusz.vi |
465 |
} else if (k == 0x1B) { /* ESC */
|
|
|
466 |
break;
|
|
|
467 |
|
1289 |
mateusz.vi |
468 |
} else if (k == 0x0D) { /* ENTER */
|
|
|
469 |
/* add a new line */
|
|
|
470 |
if (line_add(&db, db.cursor->payload + db.xoffset + cursorposx) == 0) {
|
|
|
471 |
/* trim the line above */
|
|
|
472 |
db.cursor->prev->len = db.xoffset + cursorposx;
|
|
|
473 |
db.cursor->prev->payload[db.cursor->prev->len] = 0;
|
|
|
474 |
/* move cursor to the (new) line below */
|
|
|
475 |
cursorposx = 0;
|
|
|
476 |
if (cursorposy < screenh - 2) {
|
|
|
477 |
uidirtyfrom = cursorposy;
|
|
|
478 |
cursorposy++;
|
|
|
479 |
} else {
|
|
|
480 |
uidirtyfrom = 0;
|
|
|
481 |
}
|
|
|
482 |
uidirtyto = 0xff;
|
|
|
483 |
} else {
|
|
|
484 |
/* ERROR: OUT OF MEMORY */
|
|
|
485 |
}
|
|
|
486 |
|
1292 |
mateusz.vi |
487 |
} else if (k == 0x153) { /* DEL */
|
|
|
488 |
del(&db, cursorposx, cursorposy, &uidirtyfrom, &uidirtyto);
|
|
|
489 |
|
1302 |
mateusz.vi |
490 |
} else if (k == 0x008) { /* BKSPC */
|
|
|
491 |
bkspc(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
|
|
|
492 |
|
1308 |
mateusz.vi |
493 |
} else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
|
|
|
494 |
struct line far *n;
|
|
|
495 |
n = _frealloc(db.cursor, sizeof(struct line) + db.cursor->len);
|
|
|
496 |
if (n != NULL) {
|
|
|
497 |
unsigned short off = db.xoffset + cursorposx;
|
|
|
498 |
if (n->prev) n->prev->next = n;
|
|
|
499 |
if (n->next) n->next->prev = n;
|
|
|
500 |
db.cursor = n;
|
|
|
501 |
_fmemmove(db.cursor->payload + off + 1, db.cursor->payload + off, db.cursor->len - off + 1);
|
|
|
502 |
db.cursor->len += 1;
|
|
|
503 |
uidirtyfrom = cursorposy;
|
|
|
504 |
uidirtyto = cursorposy;
|
|
|
505 |
db.cursor->payload[off] = k;
|
|
|
506 |
cursor_right(&db, &cursorposx, &cursorposy, screenw, screenh, &uidirtyfrom, &uidirtyto);
|
|
|
507 |
}
|
|
|
508 |
|
1309 |
mateusz.vi |
509 |
} else if (k == 0x13b) { /* F1 */
|
|
|
510 |
ui_help(screenw);
|
|
|
511 |
uidirtyfrom = 0;
|
|
|
512 |
uidirtyto = 0xff;
|
|
|
513 |
|
1282 |
mateusz.vi |
514 |
} else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
|
|
|
515 |
char buff[4];
|
|
|
516 |
const char *HEX = "0123456789ABCDEF";
|
|
|
517 |
buff[0] = HEX[(k >> 8) & 15];
|
|
|
518 |
buff[1] = HEX[(k >> 4) & 15];
|
|
|
519 |
buff[2] = HEX[k & 15];
|
|
|
520 |
mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
|
|
|
521 |
mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
|
1275 |
mateusz.vi |
522 |
keyb_getkey();
|
|
|
523 |
break;
|
|
|
524 |
}
|
|
|
525 |
}
|
|
|
526 |
|
|
|
527 |
mdr_cout_close();
|
|
|
528 |
|
|
|
529 |
/* TODO free memory */
|
|
|
530 |
|
|
|
531 |
return(0);
|
|
|
532 |
}
|