Subversion Repositories SvarDOS

Rev

Rev 1287 | Rev 1289 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1287 Rev 1288
1
/* Sved, the SvarDOS editor
1
/* Sved, the SvarDOS editor
2
 *
2
 *
3
 * Copyright (C) 2023 Mateusz Viste
3
 * Copyright (C) 2023 Mateusz Viste
4
 *
4
 *
5
 * Sved is released under the terms of the MIT license.
5
 * Sved is released under the terms of the MIT license.
6
 *
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
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
8
 * of this software and associated documentation files (the "Software"), to
9
 * deal in the Software without restriction, including without limitation the
9
 * deal in the Software without restriction, including without limitation the
10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
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
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:
12
 * furnished to do so, subject to the following conditions:
13
 *
13
 *
14
 * The above copyright notice and this permission notice shall be included in
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
15
 * all copies or substantial portions of the Software.
16
 *
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
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,
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
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
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
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
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
 * IN THE SOFTWARE.
23
 * IN THE SOFTWARE.
24
 */
24
 */
25
 
25
 
26
#include <dos.h>
26
#include <dos.h>
27
#include <fcntl.h>
27
#include <fcntl.h>
28
#include <stdlib.h>
28
#include <stdlib.h>
29
#include <string.h>
29
#include <string.h>
-
 
30
#include <malloc.h>   /* _fcalloc() */
30
 
31
 
31
#include "mdr\cout.h"
32
#include "mdr\cout.h"
32
#include "mdr\keyb.h"
33
#include "mdr\keyb.h"
33
 
34
 
34
#include "svarlang\svarlang.h"
35
#include "svarlang\svarlang.h"
35
 
36
 
36
#define COL_TXT        0
37
#define COL_TXT        0
37
#define COL_STATUSBAR1 1
38
#define COL_STATUSBAR1 1
38
#define COL_STATUSBAR2 2
39
#define COL_STATUSBAR2 2
39
#define COL_SCROLLBAR  3
40
#define COL_SCROLLBAR  3
40
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
41
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
41
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
42
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
42
 
43
 
43
#define SCROLL_CURSOR 0xB1
44
#define SCROLL_CURSOR 0xB1
44
 
45
 
45
 
46
 
46
struct line {
47
struct line {
47
  struct line *prev;
48
  struct line far *prev;
48
  struct line *next;
49
  struct line far *next;
49
  unsigned short len;
50
  unsigned short len;
50
  char payload[1];
51
  char payload[1];
51
};
52
};
52
 
53
 
53
struct linedb {
54
struct linedb {
54
  struct line *topscreen;
55
  struct line far *topscreen;
55
  struct line *cursor;
56
  struct line far *cursor;
56
  unsigned short xoffset;
57
  unsigned short xoffset;
57
};
58
};
58
 
59
 
59
 
60
 
60
/* returns non-zero on error */
61
/* returns non-zero on error */
61
int line_add(struct linedb *db, const char *line) {
62
int line_add(struct linedb *db, const char *line) {
62
  unsigned short slen = strlen(line);
63
  unsigned short slen = strlen(line);
63
  struct line *l;
64
  struct line far *l;
64
 
65
 
65
  /* trim out CR/LF line endings */
66
  /* trim out CR/LF line endings */
66
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
67
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
67
    slen -= 2;
68
    slen -= 2;
68
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
69
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
69
    slen--;
70
    slen--;
70
  }
71
  }
71
 
72
 
72
  l = calloc(1, sizeof(struct line) + slen + 1);
73
  l = _fcalloc(1, sizeof(struct line) + slen + 1);
73
  if (l == NULL) return(-1);
74
  if (l == NULL) return(-1);
74
 
75
 
75
  l->prev = db->cursor;
76
  l->prev = db->cursor;
76
  if (db->cursor) {
77
  if (db->cursor) {
77
    l->next = db->cursor->next;
78
    l->next = db->cursor->next;
78
    db->cursor->next = l;
79
    db->cursor->next = l;
79
    l->next->prev = l;
80
    l->next->prev = l;
80
  }
81
  }
81
  db->cursor = l;
82
  db->cursor = l;
82
  memmove(l->payload, line, slen);
83
  _fmemcpy(l->payload, line, slen);
83
  l->len = slen;
84
  l->len = slen;
84
 
85
 
85
  return(0);
86
  return(0);
86
}
87
}
87
 
88
 
88
 
89
 
89
void db_rewind(struct linedb *db) {
90
void db_rewind(struct linedb *db) {
90
  if (db->cursor == NULL) return;
91
  if (db->cursor == NULL) return;
91
  while (db->cursor->prev) db->cursor = db->cursor->prev;
92
  while (db->cursor->prev) db->cursor = db->cursor->prev;
92
  db->topscreen = db->cursor;
93
  db->topscreen = db->cursor;
93
}
94
}
94
 
95
 
95
 
96
 
96
void load_colorscheme(void) {
97
void load_colorscheme(void) {
97
  scheme[COL_TXT] = 0x17;
98
  scheme[COL_TXT] = 0x17;
98
  scheme[COL_STATUSBAR1] = 0x70;
99
  scheme[COL_STATUSBAR1] = 0x70;
99
  scheme[COL_STATUSBAR2] = 0x78;
100
  scheme[COL_STATUSBAR2] = 0x78;
100
  scheme[COL_SCROLLBAR] = 0x70;
101
  scheme[COL_SCROLLBAR] = 0x70;
101
}
102
}
102
 
103
 
103
 
104
 
104
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname) {
105
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname) {
105
  unsigned char i;
106
  unsigned char i;
106
  const char *s = "HELP";
107
  const char *s = "HELP";
107
  unsigned char helpcol = screenw - (strlen(s) + 4);
108
  unsigned char helpcol = screenw - (strlen(s) + 4);
108
 
109
 
109
  /* clear screen */
110
  /* clear screen */
110
  mdr_cout_cls(scheme[COL_TXT]);
111
  mdr_cout_cls(scheme[COL_TXT]);
111
 
112
 
112
  /* status bar */
113
  /* status bar */
113
  for (i = 0; i < helpcol; i++) {
114
  for (i = 0; i < helpcol; i++) {
114
    mdr_cout_char(screenh - 1, i, *fname, scheme[COL_STATUSBAR1]);
115
    mdr_cout_char(screenh - 1, i, *fname, scheme[COL_STATUSBAR1]);
115
    if (*fname != 0) fname++;
116
    if (*fname != 0) fname++;
116
  }
117
  }
117
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
118
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
118
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
119
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
119
 
120
 
120
  /* scroll bar */
121
  /* scroll bar */
121
  for (i = 0; i < (screenh - 1); i++) {
122
  for (i = 0; i < (screenh - 1); i++) {
122
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
123
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
123
  }
124
  }
124
}
125
}
125
 
126
 
126
 
127
 
127
static void ui_refresh(const struct linedb *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto) {
128
static void ui_refresh(const struct linedb *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto) {
128
  unsigned char y = 0;
129
  unsigned char y = 0;
129
  unsigned char len;
130
  unsigned char len;
130
  struct line *l;
131
  struct line far *l;
131
 
132
 
132
  /* DEBUG TODO FIXME */
133
#ifdef DBG_REFRESH
133
  static char m = 'a';
134
  static char m = 'a';
134
  m++;
135
  m++;
135
  if (m > 'z') m = 'a';
136
  if (m > 'z') m = 'a';
-
 
137
#endif
136
 
138
 
137
  for (l = db->topscreen; l != NULL; l = l->next) {
139
  for (l = db->topscreen; l != NULL; l = l->next) {
138
 
140
 
139
    /* skip lines that do not to be refreshed */
141
    /* skip lines that do not to be refreshed */
140
    if ((y < uidirtyfrom) || (y > uidirtyto)) continue;
142
    if ((y < uidirtyfrom) || (y > uidirtyto)) continue;
141
 
143
 
142
    if (db->xoffset < l->len) {
144
    if (db->xoffset < l->len) {
143
      len = mdr_cout_str(y, 0, l->payload + db->xoffset, scheme[COL_TXT], screenw - 1);
145
      for (len = 0; l->payload[len] != 0; len++) mdr_cout_char(y, len, l->payload[len], scheme[COL_TXT]);
144
    } else {
146
    } else {
145
      len = 0;
147
      len = 0;
146
    }
148
    }
147
    while (len < screenw - 1) mdr_cout_char(y, len++, ' ', scheme[COL_TXT]);
149
    while (len < screenw - 1) mdr_cout_char(y, len++, ' ', scheme[COL_TXT]);
148
 
150
 
149
    /* FIXME DEBUG */
151
#ifdef DBG_REFRESH
150
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
152
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
-
 
153
#endif
151
 
154
 
152
    if (y == screenh - 2) break;
155
    if (y == screenh - 2) break;
153
    y++;
156
    y++;
154
  }
157
  }
155
}
158
}
156
 
159
 
157
 
160
 
158
static void check_cursor_not_after_eol(struct linedb *db, unsigned char *cursorpos, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
161
static void check_cursor_not_after_eol(struct linedb *db, unsigned char *cursorpos, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
159
  if (db->xoffset + *cursorpos <= db->cursor->len) return;
162
  if (db->xoffset + *cursorpos <= db->cursor->len) return;
160
 
163
 
161
  if (db->cursor->len < db->xoffset) {
164
  if (db->cursor->len < db->xoffset) {
162
    *cursorpos = 0;
165
    *cursorpos = 0;
163
    db->xoffset = db->cursor->len;
166
    db->xoffset = db->cursor->len;
164
    *uidirtyfrom = 0;
167
    *uidirtyfrom = 0;
165
    *uidirtyto = 0xff;
168
    *uidirtyto = 0xff;
166
  } else {
169
  } else {
167
    *cursorpos = db->cursor->len - db->xoffset;
170
    *cursorpos = db->cursor->len - db->xoffset;
168
  }
171
  }
169
}
172
}
170
 
173
 
171
 
174
 
172
static void cursor_up(struct linedb *db, unsigned char *cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
175
static void cursor_up(struct linedb *db, unsigned char *cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
173
  if (db->cursor->prev != NULL) {
176
  if (db->cursor->prev != NULL) {
174
    db->cursor = db->cursor->prev;
177
    db->cursor = db->cursor->prev;
175
    if (*cursorposy == 0) {
178
    if (*cursorposy == 0) {
176
      db->topscreen = db->cursor;
179
      db->topscreen = db->cursor;
177
      *uidirtyfrom = 0;
180
      *uidirtyfrom = 0;
178
      *uidirtyto = 0xff;
181
      *uidirtyto = 0xff;
179
    } else {
182
    } else {
180
      *cursorposy -= 1;
183
      *cursorposy -= 1;
181
    }
184
    }
182
  }
185
  }
183
}
186
}
184
 
187
 
185
 
188
 
186
static void cursor_eol(struct linedb *db, unsigned char *cursorposx, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
189
static void cursor_eol(struct linedb *db, unsigned char *cursorposx, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
187
  /* adjust xoffset to make sure eol is visible on screen */
190
  /* adjust xoffset to make sure eol is visible on screen */
188
  if (db->xoffset > db->cursor->len) {
191
  if (db->xoffset > db->cursor->len) {
189
    db->xoffset = db->cursor->len - 1;
192
    db->xoffset = db->cursor->len - 1;
190
    *uidirtyfrom = 0;
193
    *uidirtyfrom = 0;
191
    *uidirtyto = 0xff;
194
    *uidirtyto = 0xff;
192
  }
195
  }
193
 
196
 
194
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
197
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
195
    db->xoffset = db->cursor->len - screenw + 2;
198
    db->xoffset = db->cursor->len - screenw + 2;
196
    *uidirtyfrom = 0;
199
    *uidirtyfrom = 0;
197
    *uidirtyto = 0xff;
200
    *uidirtyto = 0xff;
198
  }
201
  }
199
  *cursorposx = db->cursor->len - db->xoffset;
202
  *cursorposx = db->cursor->len - db->xoffset;
200
}
203
}
201
 
204
 
202
 
205
 
203
static void cursor_down(struct linedb *db, unsigned char *cursorposy, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
206
static void cursor_down(struct linedb *db, unsigned char *cursorposy, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
204
  if (db->cursor->next != NULL) {
207
  if (db->cursor->next != NULL) {
205
    db->cursor = db->cursor->next;
208
    db->cursor = db->cursor->next;
206
    if (*cursorposy < screenh - 2) {
209
    if (*cursorposy < screenh - 2) {
207
      *cursorposy += 1;
210
      *cursorposy += 1;
208
    } else {
211
    } else {
209
      db->topscreen = db->topscreen->next;
212
      db->topscreen = db->topscreen->next;
210
      *uidirtyfrom = 0;
213
      *uidirtyfrom = 0;
211
      *uidirtyto = 0xff;
214
      *uidirtyto = 0xff;
212
    }
215
    }
213
  }
216
  }
214
}
217
}
215
 
218
 
216
 
219
 
217
static void cursor_home(struct linedb *db, unsigned char *cursorposx, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
220
static void cursor_home(struct linedb *db, unsigned char *cursorposx, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
218
  *cursorposx = 0;
221
  *cursorposx = 0;
219
  if (db->xoffset != 0) {
222
  if (db->xoffset != 0) {
220
    db->xoffset = 0;
223
    db->xoffset = 0;
221
    *uidirtyfrom = 0;
224
    *uidirtyfrom = 0;
222
    *uidirtyto = 0xff;
225
    *uidirtyto = 0xff;
223
  }
226
  }
224
}
227
}
225
 
228
 
226
 
229
 
227
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
230
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
228
 * of argc and argv, saves some 330 bytes of binary size */
231
 * of argc and argv, saves some 330 bytes of binary size */
229
static char *parseargv(void) {
232
static char *parseargv(void) {
230
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
233
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
231
  unsigned char count = 0;
234
  unsigned char count = 0;
232
  char *argv[4];
235
  char *argv[4];
233
 
236
 
234
  while (count < 4) {
237
  while (count < 4) {
235
    /* jump to nearest arg */
238
    /* jump to nearest arg */
236
    while (*tail == ' ') {
239
    while (*tail == ' ') {
237
      *tail = 0;
240
      *tail = 0;
238
      tail++;
241
      tail++;
239
    }
242
    }
240
 
243
 
241
    if (*tail == '\r') {
244
    if (*tail == '\r') {
242
      *tail = 0;
245
      *tail = 0;
243
      break;
246
      break;
244
    }
247
    }
245
 
248
 
246
    argv[count++] = tail;
249
    argv[count++] = tail;
247
 
250
 
248
    /* jump to next delimiter */
251
    /* jump to next delimiter */
249
    while ((*tail != ' ') && (*tail != '\r')) tail++;
252
    while ((*tail != ' ') && (*tail != '\r')) tail++;
250
  }
253
  }
251
 
254
 
252
  /* check args now */
255
  /* check args now */
253
  if (count != 1) return(NULL);
256
  if (count != 1) return(NULL);
254
 
257
 
255
  return(argv[0]);
258
  return(argv[0]);
256
}
259
}
257
 
260
 
258
 
261
 
259
static int loadfile(struct linedb *db, const char *fname) {
262
static int loadfile(struct linedb *db, const char *fname) {
260
  char buff[1024];
263
  char buff[1024];
261
  unsigned int prevlen = 0, len, llen;
264
  unsigned int prevlen = 0, len, llen;
262
  int fd;
265
  int fd;
263
  int r = 0;
266
  int r = 0;
264
 
267
 
265
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
268
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
266
    mdr_coutraw_puts("Failed to open file:");
269
    mdr_coutraw_puts("Failed to open file:");
267
    mdr_coutraw_puts(fname);
270
    mdr_coutraw_puts(fname);
268
    return(-1);
271
    return(-1);
269
  }
272
  }
270
 
273
 
271
  do {
274
  do {
272
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
275
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
273
      len += prevlen;
276
      len += prevlen;
274
    } else {
277
    } else {
275
      len = prevlen;
278
      len = prevlen;
276
    }
279
    }
277
 
280
 
278
    /* look for nearest \n and replace with 0*/
281
    /* look for nearest \n and replace with 0*/
279
    for (llen = 0; buff[llen] != '\n'; llen++) {
282
    for (llen = 0; buff[llen] != '\n'; llen++) {
280
      if (llen == sizeof(buff)) break;
283
      if (llen == sizeof(buff)) break;
281
    }
284
    }
282
    buff[llen] = 0;
285
    buff[llen] = 0;
283
    if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
286
    if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
284
    if (line_add(db, buff) != 0) {
287
    if (line_add(db, buff) != 0) {
285
      mdr_coutraw_puts("out of memory");
288
      mdr_coutraw_puts("out of memory");
286
      r = -1;
289
      r = -1;
287
      break;
290
      break;
288
    }
291
    }
289
 
292
 
290
    len -= llen + 1;
293
    len -= llen + 1;
291
    memmove(buff, buff + llen + 1, len);
294
    memmove(buff, buff + llen + 1, len);
292
    prevlen = len;
295
    prevlen = len;
293
  } while (len > 0);
296
  } while (len > 0);
294
 
297
 
295
  _dos_close(fd);
298
  _dos_close(fd);
296
 
299
 
297
  return(r);
300
  return(r);
298
}
301
}
299
 
302
 
300
 
303
 
301
int main(void) {
304
int main(void) {
302
  const char *fname;
305
  const char *fname;
303
  struct linedb db;
306
  struct linedb db;
304
  unsigned char screenw = 0, screenh = 0;
307
  unsigned char screenw = 0, screenh = 0;
305
  unsigned char cursorposx = 0, cursorposy = 0;
308
  unsigned char cursorposx = 0, cursorposy = 0;
306
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
309
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
307
 
310
 
308
  bzero(&db, sizeof(db));
311
  bzero(&db, sizeof(db));
309
 
312
 
310
  svarlang_autoload_nlspath("sved");
313
  svarlang_autoload_nlspath("sved");
311
 
314
 
312
  fname = parseargv();
315
  fname = parseargv();
313
 
316
 
314
  if (fname == NULL) {
317
  if (fname == NULL) {
315
    mdr_coutraw_puts("usage: sved file.txt");
318
    mdr_coutraw_puts("usage: sved file.txt");
316
    return(0);
319
    return(0);
317
  }
320
  }
318
 
321
 
319
  /* load file */
322
  /* load file */
320
  if (loadfile(&db, fname) != 0) return(1);
323
  if (loadfile(&db, fname) != 0) return(1);
321
 
324
 
322
  /* add an empty line at end if not present already */
325
  /* add an empty line at end if not present already */
323
  if (db.cursor->len != 0) line_add(&db, "");
326
  if (db.cursor->len != 0) line_add(&db, "");
324
 
327
 
325
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
328
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
326
  ui_basic(screenw, screenh, fname);
329
  ui_basic(screenw, screenh, fname);
327
 
330
 
328
  db_rewind(&db);
331
  db_rewind(&db);
329
 
332
 
330
  for (;;) {
333
  for (;;) {
331
    int k;
334
    int k;
332
 
335
 
333
    check_cursor_not_after_eol(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
336
    check_cursor_not_after_eol(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
334
    mdr_cout_locate(cursorposy, cursorposx);
337
    mdr_cout_locate(cursorposy, cursorposx);
335
 
338
 
336
    if (uidirtyfrom != 0xff) {
339
    if (uidirtyfrom != 0xff) {
337
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto);
340
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto);
338
      uidirtyfrom = 0xff;
341
      uidirtyfrom = 0xff;
339
    }
342
    }
340
 
343
 
341
    k = keyb_getkey();
344
    k = keyb_getkey();
342
 
345
 
343
    if (k == 0x150) { /* down */
346
    if (k == 0x150) { /* down */
344
      cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
347
      cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
345
 
348
 
346
    } else if (k == 0x148) { /* up */
349
    } else if (k == 0x148) { /* up */
347
      cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
350
      cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
348
 
351
 
349
    } else if (k == 0x14D) { /* right */
352
    } else if (k == 0x14D) { /* right */
350
      if (db.cursor->len > db.xoffset + cursorposx) {
353
      if (db.cursor->len > db.xoffset + cursorposx) {
351
        if (cursorposx < screenw - 2) {
354
        if (cursorposx < screenw - 2) {
352
          cursorposx++;
355
          cursorposx++;
353
        } else {
356
        } else {
354
          db.xoffset++;
357
          db.xoffset++;
355
          uidirtyfrom = 0;
358
          uidirtyfrom = 0;
356
          uidirtyto = 0xff;
359
          uidirtyto = 0xff;
357
        }
360
        }
358
      } else {
361
      } else {
359
        cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
362
        cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
360
        cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
363
        cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
361
      }
364
      }
362
 
365
 
363
    } else if (k == 0x14B) { /* left */
366
    } else if (k == 0x14B) { /* left */
364
      if (cursorposx > 0) {
367
      if (cursorposx > 0) {
365
        cursorposx--;
368
        cursorposx--;
366
      } else if (db.xoffset > 0) {
369
      } else if (db.xoffset > 0) {
367
        db.xoffset--;
370
        db.xoffset--;
368
        uidirtyfrom = 0;
371
        uidirtyfrom = 0;
369
        uidirtyto = 0xff;
372
        uidirtyto = 0xff;
370
      } else if (db.cursor->prev != NULL) { /* jump to end of line above */
373
      } else if (db.cursor->prev != NULL) { /* jump to end of line above */
371
        cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
374
        cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
372
        cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
375
        cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
373
      }
376
      }
374
 
377
 
375
    } else if (k == 0x149) { /* pgup */
378
    } else if (k == 0x149) { /* pgup */
376
      // TODO
379
      // TODO
377
 
380
 
378
    } else if (k == 0x151) { /* pgdown */
381
    } else if (k == 0x151) { /* pgdown */
379
      // TODO
382
      // TODO
380
 
383
 
381
    } else if (k == 0x147) { /* home */
384
    } else if (k == 0x147) { /* home */
382
       cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
385
       cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
383
 
386
 
384
    } else if (k == 0x14F) { /* end */
387
    } else if (k == 0x14F) { /* end */
385
       cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
388
       cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
386
 
389
 
387
    } else if (k == 0x1B) { /* ESC */
390
    } else if (k == 0x1B) { /* ESC */
388
      break;
391
      break;
389
 
392
 
390
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
393
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
391
      char buff[4];
394
      char buff[4];
392
      const char *HEX = "0123456789ABCDEF";
395
      const char *HEX = "0123456789ABCDEF";
393
      buff[0] = HEX[(k >> 8) & 15];
396
      buff[0] = HEX[(k >> 8) & 15];
394
      buff[1] = HEX[(k >> 4) & 15];
397
      buff[1] = HEX[(k >> 4) & 15];
395
      buff[2] = HEX[k & 15];
398
      buff[2] = HEX[k & 15];
396
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
399
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
397
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
400
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
398
      keyb_getkey();
401
      keyb_getkey();
399
      break;
402
      break;
400
    }
403
    }
401
  }
404
  }
402
 
405
 
403
  mdr_cout_close();
406
  mdr_cout_close();
404
 
407
 
405
  /* TODO free memory */
408
  /* TODO free memory */
406
 
409
 
407
  return(0);
410
  return(0);
408
}
411
}
409
 
412