Subversion Repositories SvarDOS

Rev

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

Rev 1317 Rev 1318
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
#include <malloc.h>   /* _fcalloc() */
31
 
31
 
32
#include "mdr\bios.h"
32
#include "mdr\bios.h"
33
#include "mdr\cout.h"
33
#include "mdr\cout.h"
34
#include "mdr\dos.h"
34
#include "mdr\dos.h"
35
#include "mdr\keyb.h"
35
#include "mdr\keyb.h"
36
 
36
 
37
#include "svarlang\svarlang.h"
37
#include "svarlang\svarlang.h"
38
 
38
 
39
#define COL_TXT        0
39
#define COL_TXT        0
40
#define COL_STATUSBAR1 1
40
#define COL_STATUSBAR1 1
41
#define COL_STATUSBAR2 2
41
#define COL_STATUSBAR2 2
42
#define COL_SCROLLBAR  3
42
#define COL_SCROLLBAR  3
43
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
43
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
44
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
44
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
45
 
45
 
46
#define SCROLL_CURSOR 0xB1
46
#define SCROLL_CURSOR 0xB1
47
 
47
 
48
 
48
 
49
struct line {
49
struct line {
50
  struct line far *prev;
50
  struct line far *prev;
51
  struct line far *next;
51
  struct line far *next;
52
  unsigned short len;
52
  unsigned short len;
53
  char payload[1];
53
  char payload[1];
54
};
54
};
55
 
55
 
56
struct file {
56
struct file {
57
  struct line far *cursor;
57
  struct line far *cursor;
58
  unsigned short xoffset;
58
  unsigned short xoffset;
59
  unsigned char cursorposx;
59
  unsigned char cursorposx;
60
  unsigned char cursorposy;
60
  unsigned char cursorposy;
61
  char lfonly; /* set if line endings are LF (CR/LF otherwise) */
61
  char lfonly; /* set if line endings are LF (CR/LF otherwise) */
62
};
62
};
63
 
63
 
64
 
64
 
65
/* returns non-zero on error */
65
/* returns non-zero on error */
66
static int line_add(struct file *db, const char far *line) {
66
static int line_add(struct file *db, const char far *line) {
67
  unsigned short slen;
67
  unsigned short slen;
68
  struct line far *l;
68
  struct line far *l;
69
 
69
 
70
  /* slen = strlen(line) (but for far pointer) */
70
  /* slen = strlen(line) (but for far pointer) */
71
  for (slen = 0; line[slen] != 0; slen++);
71
  for (slen = 0; line[slen] != 0; slen++);
72
 
72
 
73
  /* trim out CR/LF line endings */
73
  /* trim out CR/LF line endings */
74
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
74
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
75
    slen -= 2;
75
    slen -= 2;
76
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
76
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
77
    slen--;
77
    slen--;
78
  }
78
  }
79
 
79
 
80
  l = _fcalloc(1, sizeof(struct line) + slen + 1);
80
  l = _fcalloc(1, sizeof(struct line) + slen + 1);
81
  if (l == NULL) return(-1);
81
  if (l == NULL) return(-1);
82
 
82
 
83
  l->prev = db->cursor;
83
  l->prev = db->cursor;
84
  if (db->cursor) {
84
  if (db->cursor) {
85
    l->next = db->cursor->next;
85
    l->next = db->cursor->next;
86
    db->cursor->next = l;
86
    db->cursor->next = l;
87
    l->next->prev = l;
87
    l->next->prev = l;
88
  }
88
  }
89
  db->cursor = l;
89
  db->cursor = l;
90
  _fmemcpy(l->payload, line, slen);
90
  _fmemcpy(l->payload, line, slen);
91
  l->len = slen;
91
  l->len = slen;
92
 
92
 
93
  return(0);
93
  return(0);
94
}
94
}
95
 
95
 
96
 
96
 
97
static void db_rewind(struct file *db) {
97
static void db_rewind(struct file *db) {
98
  if (db->cursor == NULL) return;
98
  if (db->cursor == NULL) return;
99
  while (db->cursor->prev) db->cursor = db->cursor->prev;
99
  while (db->cursor->prev) db->cursor = db->cursor->prev;
100
}
100
}
101
 
101
 
102
 
102
 
103
static void load_colorscheme(void) {
103
static void load_colorscheme(void) {
104
  scheme[COL_TXT] = 0x17;
104
  scheme[COL_TXT] = 0x17;
105
  scheme[COL_STATUSBAR1] = 0x70;
105
  scheme[COL_STATUSBAR1] = 0x70;
106
  scheme[COL_STATUSBAR2] = 0x78;
106
  scheme[COL_STATUSBAR2] = 0x78;
107
  scheme[COL_SCROLLBAR] = 0x70;
107
  scheme[COL_SCROLLBAR] = 0x70;
108
}
108
}
109
 
109
 
110
 
110
 
111
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname, const struct file *db) {
111
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname, const struct file *db) {
112
  unsigned char i;
112
  unsigned char i;
113
  const char *s = svarlang_strid(0); /* HELP */
113
  const char *s = svarlang_strid(0); /* HELP */
114
  unsigned char helpcol = screenw - (strlen(s) + 4);
114
  unsigned char helpcol = screenw - (strlen(s) + 4);
115
 
115
 
116
  /* fill status bar with background */
116
  /* fill status bar with background */
117
  mdr_cout_char_rep(screenh - 1, 0, ' ', scheme[COL_STATUSBAR1], screenw);
117
  mdr_cout_char_rep(screenh - 1, 0, ' ', scheme[COL_STATUSBAR1], screenw);
118
 
118
 
119
  /* filename */
119
  /* filename */
120
  mdr_cout_str(screenh - 1, 0, fname, scheme[COL_STATUSBAR1], screenw);
120
  mdr_cout_str(screenh - 1, 0, fname, scheme[COL_STATUSBAR1], screenw);
121
 
121
 
122
  /* eol type */
122
  /* eol type */
123
  if (db->lfonly) {
123
  if (db->lfonly) {
124
    mdr_cout_str(screenh - 1, helpcol - 3, "LF", scheme[COL_STATUSBAR1], 5);
124
    mdr_cout_str(screenh - 1, helpcol - 3, "LF", scheme[COL_STATUSBAR1], 5);
125
  } else {
125
  } else {
126
    mdr_cout_str(screenh - 1, helpcol - 5, "CRLF", scheme[COL_STATUSBAR1], 5);
126
    mdr_cout_str(screenh - 1, helpcol - 5, "CRLF", scheme[COL_STATUSBAR1], 5);
127
  }
127
  }
128
 
128
 
129
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
129
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
130
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
130
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
131
 
131
 
132
  /* scroll bar */
132
  /* scroll bar */
133
  for (i = 0; i < (screenh - 1); i++) {
133
  for (i = 0; i < (screenh - 1); i++) {
134
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
134
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
135
  }
135
  }
136
}
136
}
137
 
137
 
138
 
138
 
139
static void ui_msg(const char *msg, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
139
static void ui_msg(const char *msg, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
140
  unsigned short x, y, msglen, i;
140
  unsigned short x, y, msglen, i;
141
  msglen = strlen(msg);
141
  msglen = strlen(msg);
142
  y = (screenh - 4) >> 1;
142
  y = (screenh - 4) >> 1;
143
  x = (screenw - msglen - 4) >> 1;
143
  x = (screenw - msglen - 4) >> 1;
144
  for (i = y+2; i >= y; i--) mdr_cout_char_rep(i, x, ' ', scheme[COL_STATUSBAR1], msglen + 2);
144
  for (i = y+2; i >= y; i--) mdr_cout_char_rep(i, x, ' ', scheme[COL_STATUSBAR1], msglen + 2);
145
  mdr_cout_str(y+1, x+1, msg, scheme[COL_STATUSBAR1], msglen);
145
  mdr_cout_str(y+1, x+1, msg, scheme[COL_STATUSBAR1], msglen);
146
 
146
 
147
  if (*uidirtyfrom > y) *uidirtyfrom = y;
147
  if (*uidirtyfrom > y) *uidirtyfrom = y;
148
  if (*uidirtyto < y+2) *uidirtyto = y+2;
148
  if (*uidirtyto < y+2) *uidirtyto = y+2;
149
}
149
}
150
 
150
 
151
 
151
 
152
static void ui_help(unsigned char screenw) {
152
static void ui_help(unsigned char screenw) {
153
#define MAXLINLEN 35
153
#define MAXLINLEN 35
154
  unsigned short i, offset;
154
  unsigned short i, offset;
155
  offset = (screenw - MAXLINLEN + 1) >> 1;
155
  offset = (screenw - MAXLINLEN + 1) >> 1;
156
  mdr_cout_cursor_hide();
156
  mdr_cout_cursor_hide();
157
  for (i = 2; i <= 12; i++) {
157
  for (i = 2; i <= 12; i++) {
158
    mdr_cout_char_rep(i, offset - 2, ' ', scheme[COL_STATUSBAR1], MAXLINLEN);
158
    mdr_cout_char_rep(i, offset - 2, ' ', scheme[COL_STATUSBAR1], MAXLINLEN);
159
  }
159
  }
160
 
160
 
161
  mdr_cout_str(3, offset, svarlang_str(0, 0), scheme[COL_STATUSBAR1], MAXLINLEN);
161
  mdr_cout_str(3, offset, svarlang_str(0, 0), scheme[COL_STATUSBAR1], MAXLINLEN);
162
  for (i = 0; i <= 2; i++) {
162
  for (i = 0; i <= 2; i++) {
163
    mdr_cout_str(5 + i, offset, svarlang_str(8, i), scheme[COL_STATUSBAR1], MAXLINLEN);
163
    mdr_cout_str(5 + i, offset, svarlang_str(8, i), scheme[COL_STATUSBAR1], MAXLINLEN);
164
  }
164
  }
165
  mdr_cout_str(9, offset, svarlang_str(8, 10), scheme[COL_STATUSBAR1], MAXLINLEN);
165
  mdr_cout_str(9, offset, svarlang_str(8, 10), scheme[COL_STATUSBAR1], MAXLINLEN);
166
  mdr_cout_str(11, offset, svarlang_str(8, 11), scheme[COL_STATUSBAR1], MAXLINLEN);
166
  mdr_cout_str(11, offset, svarlang_str(8, 11), scheme[COL_STATUSBAR1], MAXLINLEN);
167
 
167
 
168
  keyb_getkey();
168
  keyb_getkey();
169
  mdr_cout_cursor_show();
169
  mdr_cout_cursor_show();
170
#undef MAXLINLEN
170
#undef MAXLINLEN
171
}
171
}
172
 
172
 
173
 
173
 
174
static void ui_refresh(const struct file *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto) {
174
static void ui_refresh(const struct file *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto) {
175
  unsigned char len;
175
  unsigned char x;
176
  const struct line far *l;
176
  const struct line far *l;
177
  unsigned char y = db->cursorposy;
177
  unsigned char y = db->cursorposy;
178
 
178
 
179
#ifdef DBG_REFRESH
179
#ifdef DBG_REFRESH
180
  static char m = 'a';
180
  static char m = 'a';
181
  m++;
181
  m++;
182
  if (m > 'z') m = 'a';
182
  if (m > 'z') m = 'a';
183
#endif
183
#endif
184
 
184
 
185
  /* rewind cursor line to first line that needs redrawing */
185
  /* rewind cursor line to first line that needs redrawing */
186
  for (l = db->cursor; y > uidirtyfrom; y--) l = l->prev;
186
  for (l = db->cursor; y > uidirtyfrom; y--) l = l->prev;
187
 
187
 
188
  /* iterate over lines and redraw whatever needs to be redrawn */
188
  /* iterate over lines and redraw whatever needs to be redrawn */
189
  for (; l != NULL; l = l->next, y++) {
189
  for (; l != NULL; l = l->next, y++) {
190
 
190
 
191
    /* skip lines that do not need to be refreshed */
191
    /* skip lines that do not need to be refreshed */
192
    if (y < uidirtyfrom) continue;
192
    if (y < uidirtyfrom) continue;
193
    if (y > uidirtyto) break;
193
    if (y > uidirtyto) break;
194
 
194
 
-
 
195
    x = 0;
195
    if (db->xoffset < l->len) {
196
    if (db->xoffset < l->len) {
-
 
197
      unsigned char i, limit;
196
      for (len = 0; l->payload[len] != 0; len++) mdr_cout_char(y, len, l->payload[len], scheme[COL_TXT]);
198
      if (l->len - db->xoffset < screenw) {
-
 
199
        limit = l->len;
197
    } else {
200
      } else {
-
 
201
        limit = db->xoffset + screenw - 1;
198
      len = 0;
202
      }
-
 
203
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], scheme[COL_TXT]);
199
    }
204
    }
-
 
205
 
-
 
206
    /* write empty spaces until end of line */
200
    while (len < screenw - 1) mdr_cout_char(y, len++, ' ', scheme[COL_TXT]);
207
    if (x < screenw - 1) mdr_cout_char_rep(y, x, ' ', scheme[COL_TXT], screenw - 1 - x);
201
 
208
 
202
#ifdef DBG_REFRESH
209
#ifdef DBG_REFRESH
203
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
210
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
204
#endif
211
#endif
205
 
212
 
206
    if (y == screenh - 2) break;
213
    if (y == screenh - 2) break;
207
  }
214
  }
208
 
215
 
-
 
216
  while (y < screenh - 1) {
-
 
217
    mdr_cout_char_rep(y++, 0, ' ', scheme[COL_TXT], screenw - 1);
-
 
218
  }
209
}
219
}
210
 
220
 
211
 
221
 
212
static void check_cursor_not_after_eol(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
222
static void check_cursor_not_after_eol(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
213
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
223
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
214
 
224
 
215
  if (db->cursor->len < db->xoffset) {
225
  if (db->cursor->len < db->xoffset) {
216
    db->cursorposx = 0;
226
    db->cursorposx = 0;
217
    db->xoffset = db->cursor->len;
227
    db->xoffset = db->cursor->len;
218
    *uidirtyfrom = 0;
228
    *uidirtyfrom = 0;
219
    *uidirtyto = 0xff;
229
    *uidirtyto = 0xff;
220
  } else {
230
  } else {
221
    db->cursorposx = db->cursor->len - db->xoffset;
231
    db->cursorposx = db->cursor->len - db->xoffset;
222
  }
232
  }
223
}
233
}
224
 
234
 
225
 
235
 
226
static void cursor_up(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
236
static void cursor_up(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
227
  if (db->cursor->prev != NULL) {
237
  if (db->cursor->prev != NULL) {
228
    db->cursor = db->cursor->prev;
238
    db->cursor = db->cursor->prev;
229
    if (db->cursorposy == 0) {
239
    if (db->cursorposy == 0) {
230
      *uidirtyfrom = 0;
240
      *uidirtyfrom = 0;
231
      *uidirtyto = 0xff;
241
      *uidirtyto = 0xff;
232
    } else {
242
    } else {
233
      db->cursorposy -= 1;
243
      db->cursorposy -= 1;
234
    }
244
    }
235
  }
245
  }
236
}
246
}
237
 
247
 
238
 
248
 
239
static void cursor_eol(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
249
static void cursor_eol(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
240
  /* adjust xoffset to make sure eol is visible on screen */
250
  /* adjust xoffset to make sure eol is visible on screen */
241
  if (db->xoffset > db->cursor->len) {
251
  if (db->xoffset > db->cursor->len) {
242
    db->xoffset = db->cursor->len - 1;
252
    db->xoffset = db->cursor->len - 1;
243
    *uidirtyfrom = 0;
253
    *uidirtyfrom = 0;
244
    *uidirtyto = 0xff;
254
    *uidirtyto = 0xff;
245
  }
255
  }
246
 
256
 
247
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
257
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
248
    db->xoffset = db->cursor->len - screenw + 2;
258
    db->xoffset = db->cursor->len - screenw + 2;
249
    *uidirtyfrom = 0;
259
    *uidirtyfrom = 0;
250
    *uidirtyto = 0xff;
260
    *uidirtyto = 0xff;
251
  }
261
  }
252
  db->cursorposx = db->cursor->len - db->xoffset;
262
  db->cursorposx = db->cursor->len - db->xoffset;
253
}
263
}
254
 
264
 
255
 
265
 
256
static void cursor_down(struct file *db, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
266
static void cursor_down(struct file *db, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
257
  if (db->cursor->next != NULL) {
267
  if (db->cursor->next != NULL) {
258
    db->cursor = db->cursor->next;
268
    db->cursor = db->cursor->next;
259
    if (db->cursorposy < screenh - 2) {
269
    if (db->cursorposy < screenh - 2) {
260
      db->cursorposy += 1;
270
      db->cursorposy += 1;
261
    } else {
271
    } else {
262
      *uidirtyfrom = 0;
272
      *uidirtyfrom = 0;
263
      *uidirtyto = 0xff;
273
      *uidirtyto = 0xff;
264
    }
274
    }
265
  }
275
  }
266
}
276
}
267
 
277
 
268
 
278
 
269
static void cursor_left(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
279
static void cursor_left(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
270
  if (db->cursorposx > 0) {
280
  if (db->cursorposx > 0) {
271
    db->cursorposx -= 1;
281
    db->cursorposx -= 1;
272
  } else if (db->xoffset > 0) {
282
  } else if (db->xoffset > 0) {
273
    db->xoffset -= 1;
283
    db->xoffset -= 1;
274
    *uidirtyfrom = 0;
284
    *uidirtyfrom = 0;
275
    *uidirtyto = 0xff;
285
    *uidirtyto = 0xff;
276
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
286
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
277
    cursor_up(db, uidirtyfrom, uidirtyto);
287
    cursor_up(db, uidirtyfrom, uidirtyto);
278
    cursor_eol(db, screenw, uidirtyfrom, uidirtyto);
288
    cursor_eol(db, screenw, uidirtyfrom, uidirtyto);
279
  }
289
  }
280
}
290
}
281
 
291
 
282
 
292
 
283
static void cursor_home(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
293
static void cursor_home(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
284
  db->cursorposx = 0;
294
  db->cursorposx = 0;
285
  if (db->xoffset != 0) {
295
  if (db->xoffset != 0) {
286
    db->xoffset = 0;
296
    db->xoffset = 0;
287
    *uidirtyfrom = 0;
297
    *uidirtyfrom = 0;
288
    *uidirtyto = 0xff;
298
    *uidirtyto = 0xff;
289
  }
299
  }
290
}
300
}
291
 
301
 
292
 
302
 
293
static void cursor_right(struct file *db, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
303
static void cursor_right(struct file *db, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
294
  if (db->cursor->len > db->xoffset + db->cursorposx) {
304
  if (db->cursor->len > db->xoffset + db->cursorposx) {
295
    if (db->cursorposx < screenw - 2) {
305
    if (db->cursorposx < screenw - 2) {
296
      db->cursorposx += 1;
306
      db->cursorposx += 1;
297
    } else {
307
    } else {
298
      db->xoffset += 1;
308
      db->xoffset += 1;
299
      *uidirtyfrom = 0;
309
      *uidirtyfrom = 0;
300
      *uidirtyto = 0xff;
310
      *uidirtyto = 0xff;
301
    }
311
    }
302
  } else {
312
  } else {
303
    cursor_down(db, screenh, uidirtyfrom, uidirtyto);
313
    cursor_down(db, screenh, uidirtyfrom, uidirtyto);
304
    cursor_home(db, uidirtyfrom, uidirtyto);
314
    cursor_home(db, uidirtyfrom, uidirtyto);
305
  }
315
  }
306
}
316
}
307
 
317
 
308
 
318
 
309
static void del(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
319
static void del(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
310
  if (db->cursorposx + db->xoffset < db->cursor->len) {
320
  if (db->cursorposx + db->xoffset < db->cursor->len) {
311
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
321
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
312
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
322
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
313
    *uidirtyfrom = db->cursorposy;
323
    *uidirtyfrom = db->cursorposy;
314
    *uidirtyto = db->cursorposy;
324
    *uidirtyto = db->cursorposy;
315
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
325
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
316
    struct line far *nextline = db->cursor->next;
326
    struct line far *nextline = db->cursor->next;
317
    if (db->cursor->next->len > 0) {
327
    if (db->cursor->next->len > 0) {
318
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
328
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
319
      if (newptr != NULL) {
329
      if (newptr != NULL) {
320
        db->cursor = newptr;
330
        db->cursor = newptr;
321
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
331
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
322
        db->cursor->len += db->cursor->next->len;
332
        db->cursor->len += db->cursor->next->len;
323
      }
333
      }
324
    }
334
    }
325
    db->cursor->next = db->cursor->next->next;
335
    db->cursor->next = db->cursor->next->next;
326
    db->cursor->next->prev = db->cursor;
336
    db->cursor->next->prev = db->cursor;
327
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
337
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
328
    _ffree(nextline);
338
    _ffree(nextline);
329
    *uidirtyfrom = db->cursorposy;
339
    *uidirtyfrom = db->cursorposy;
330
    *uidirtyto = 0xff;
340
    *uidirtyto = 0xff;
331
  }
341
  }
332
}
342
}
333
 
343
 
334
 
344
 
335
static void bkspc(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
345
static void bkspc(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
336
 
346
 
337
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
347
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
338
  if ((db->cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
348
  if ((db->cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
339
 
349
 
340
  cursor_left(db, screenw, uidirtyfrom, uidirtyto);
350
  cursor_left(db, screenw, uidirtyfrom, uidirtyto);
341
  del(db, uidirtyfrom, uidirtyto);
351
  del(db, uidirtyfrom, uidirtyto);
342
}
352
}
343
 
353
 
344
 
354
 
345
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
355
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
346
 * of argc and argv, saves some 330 bytes of binary size */
356
 * of argc and argv, saves some 330 bytes of binary size */
347
static char *parseargv(void) {
357
static char *parseargv(void) {
348
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
358
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
349
  unsigned char count = 0;
359
  unsigned char count = 0;
350
  char *argv[4];
360
  char *argv[4];
351
 
361
 
352
  while (count < 4) {
362
  while (count < 4) {
353
    /* jump to nearest arg */
363
    /* jump to nearest arg */
354
    while (*tail == ' ') {
364
    while (*tail == ' ') {
355
      *tail = 0;
365
      *tail = 0;
356
      tail++;
366
      tail++;
357
    }
367
    }
358
 
368
 
359
    if (*tail == '\r') {
369
    if (*tail == '\r') {
360
      *tail = 0;
370
      *tail = 0;
361
      break;
371
      break;
362
    }
372
    }
363
 
373
 
364
    argv[count++] = tail;
374
    argv[count++] = tail;
365
 
375
 
366
    /* jump to next delimiter */
376
    /* jump to next delimiter */
367
    while ((*tail != ' ') && (*tail != '\r')) tail++;
377
    while ((*tail != ' ') && (*tail != '\r')) tail++;
368
  }
378
  }
369
 
379
 
370
  /* check args now */
380
  /* check args now */
371
  if (count != 1) return(NULL);
381
  if (count != 1) return(NULL);
372
 
382
 
373
  return(argv[0]);
383
  return(argv[0]);
374
}
384
}
375
 
385
 
376
 
386
 
377
static int loadfile(struct file *db, const char *fname) {
387
static int loadfile(struct file *db, const char *fname) {
378
  char buff[1024];
388
  char buff[1024];
379
  unsigned int prevlen = 0, len, llen;
389
  unsigned int prevlen = 0, len, llen;
380
  int fd;
390
  int fd;
381
  int r = 0;
391
  int r = 0;
382
 
392
 
383
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
393
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
384
    mdr_coutraw_puts("Failed to open file:");
394
    mdr_coutraw_puts("Failed to open file:");
385
    mdr_coutraw_puts(fname);
395
    mdr_coutraw_puts(fname);
386
    return(-1);
396
    return(-1);
387
  }
397
  }
388
 
398
 
389
  db->lfonly = 1;
399
  db->lfonly = 1;
390
 
400
 
391
  do {
401
  do {
392
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
402
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
393
      len += prevlen;
403
      len += prevlen;
394
    } else {
404
    } else {
395
      len = prevlen;
405
      len = prevlen;
396
    }
406
    }
397
 
407
 
398
    /* look for nearest \n and replace with 0*/
408
    /* look for nearest \n and replace with 0*/
399
    for (llen = 0; buff[llen] != '\n'; llen++) {
409
    for (llen = 0; buff[llen] != '\n'; llen++) {
400
      if (llen == sizeof(buff)) break;
410
      if (llen == sizeof(buff)) break;
401
    }
411
    }
402
    buff[llen] = 0;
412
    buff[llen] = 0;
403
    if ((llen > 0) && (buff[llen - 1] == '\r')) {
413
    if ((llen > 0) && (buff[llen - 1] == '\r')) {
404
      buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
414
      buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
405
      db->lfonly = 0;
415
      db->lfonly = 0;
406
    }
416
    }
407
    if (line_add(db, buff) != 0) {
417
    if (line_add(db, buff) != 0) {
408
      mdr_coutraw_puts("out of memory");
418
      mdr_coutraw_puts("out of memory");
409
      r = -1;
419
      r = -1;
410
      break;
420
      break;
411
    }
421
    }
412
 
422
 
413
    len -= llen + 1;
423
    len -= llen + 1;
414
    memmove(buff, buff + llen + 1, len);
424
    memmove(buff, buff + llen + 1, len);
415
    prevlen = len;
425
    prevlen = len;
416
  } while (len > 0);
426
  } while (len > 0);
417
 
427
 
418
  _dos_close(fd);
428
  _dos_close(fd);
419
 
429
 
420
  return(r);
430
  return(r);
421
}
431
}
422
 
432
 
423
 
433
 
424
static int savefile(const struct file *db, const char *fname) {
434
static int savefile(const struct file *db, const char *fname) {
425
  int fd;
435
  int fd;
426
  const struct line far *l;
436
  const struct line far *l;
427
  unsigned bytes;
437
  unsigned bytes;
428
  unsigned char eollen;
438
  unsigned char eollen;
429
  unsigned char eolbuf[2];
439
  unsigned char eolbuf[2];
430
 
440
 
431
  if (_dos_open(fname, O_WRONLY, &fd) != 0) {
441
  if (_dos_open(fname, O_WRONLY, &fd) != 0) {
432
    return(-1);
442
    return(-1);
433
  }
443
  }
434
 
444
 
435
  l = db->cursor;
445
  l = db->cursor;
436
  while (l->prev) l = l->prev;
446
  while (l->prev) l = l->prev;
437
 
447
 
438
  /* preset line terminators */
448
  /* preset line terminators */
439
  if (db->lfonly) {
449
  if (db->lfonly) {
440
    eolbuf[0] = '\n';
450
    eolbuf[0] = '\n';
441
    eollen = 1;
451
    eollen = 1;
442
  } else {
452
  } else {
443
    eolbuf[0] = '\r';
453
    eolbuf[0] = '\r';
444
    eolbuf[1] = '\n';
454
    eolbuf[1] = '\n';
445
    eollen = 2;
455
    eollen = 2;
446
  }
456
  }
447
 
457
 
448
  while (l) {
458
  while (l) {
449
    _dos_write(fd, l->payload, l->len, &bytes);
459
    _dos_write(fd, l->payload, l->len, &bytes);
450
    _dos_write(fd, eolbuf, eollen, &bytes);
460
    _dos_write(fd, eolbuf, eollen, &bytes);
451
    l = l->next;
461
    l = l->next;
452
  }
462
  }
453
 
463
 
454
  _dos_close(fd);
464
  _dos_close(fd);
455
  return(0);
465
  return(0);
456
}
466
}
457
 
467
 
458
 
468
 
459
static void insert_in_line(struct file *db, const char *databuf, unsigned short len, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
469
static void insert_in_line(struct file *db, const char *databuf, unsigned short len, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
460
  struct line far *n;
470
  struct line far *n;
461
  n = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + len);
471
  n = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + len);
462
  if (n != NULL) {
472
  if (n != NULL) {
463
    unsigned short off = db->xoffset + db->cursorposx;
473
    unsigned short off = db->xoffset + db->cursorposx;
464
    if (n->prev) n->prev->next = n;
474
    if (n->prev) n->prev->next = n;
465
    if (n->next) n->next->prev = n;
475
    if (n->next) n->next->prev = n;
466
    db->cursor = n;
476
    db->cursor = n;
467
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
477
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
468
    db->cursor->len += len;
478
    db->cursor->len += len;
469
    *uidirtyfrom = db->cursorposy;
479
    *uidirtyfrom = db->cursorposy;
470
    *uidirtyto = db->cursorposy;
480
    *uidirtyto = db->cursorposy;
471
    while (len--) {
481
    while (len--) {
472
      db->cursor->payload[off++] = *databuf;
482
      db->cursor->payload[off++] = *databuf;
473
      databuf++;
483
      databuf++;
474
      cursor_right(db, screenw, screenh, uidirtyfrom, uidirtyto);
484
      cursor_right(db, screenw, screenh, uidirtyfrom, uidirtyto);
475
    }
485
    }
476
  }
486
  }
477
}
487
}
478
 
488
 
479
 
489
 
480
int main(void) {
490
int main(void) {
481
  const char *fname;
491
  const char *fname;
482
  struct file db;
492
  struct file db;
483
  unsigned char screenw = 0, screenh = 0;
493
  unsigned char screenw = 0, screenh = 0;
484
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
494
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
485
 
495
 
486
  bzero(&db, sizeof(db));
496
  bzero(&db, sizeof(db));
487
 
497
 
488
  {
498
  {
489
    char nlspath[128], lang[8];
499
    char nlspath[128], lang[8];
490
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
500
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
491
  }
501
  }
492
 
502
 
493
  fname = parseargv();
503
  fname = parseargv();
494
 
504
 
495
  if (fname == NULL) {
505
  if (fname == NULL) {
496
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
506
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
497
    return(0);
507
    return(0);
498
  }
508
  }
499
 
509
 
500
  /* load file */
510
  /* load file */
501
  if (loadfile(&db, fname) != 0) return(1);
511
  if (loadfile(&db, fname) != 0) return(1);
502
 
512
 
503
  /* add an empty line at end if not present already */
513
  /* add an empty line at end if not present already */
504
  if (db.cursor->len != 0) line_add(&db, "");
514
  if (db.cursor->len != 0) line_add(&db, "");
505
 
515
 
506
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
516
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
507
  ui_basic(screenw, screenh, fname, &db);
517
  ui_basic(screenw, screenh, fname, &db);
508
 
518
 
509
  db_rewind(&db);
519
  db_rewind(&db);
510
 
520
 
511
  for (;;) {
521
  for (;;) {
512
    int k;
522
    int k;
513
 
523
 
514
    check_cursor_not_after_eol(&db, &uidirtyfrom, &uidirtyto);
524
    check_cursor_not_after_eol(&db, &uidirtyfrom, &uidirtyto);
515
    mdr_cout_locate(db.cursorposy, db.cursorposx);
525
    mdr_cout_locate(db.cursorposy, db.cursorposx);
516
 
526
 
517
    if (uidirtyfrom != 0xff) {
527
    if (uidirtyfrom != 0xff) {
518
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto);
528
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto);
519
      uidirtyfrom = 0xff;
529
      uidirtyfrom = 0xff;
520
    }
530
    }
521
 
531
 
522
    k = keyb_getkey();
532
    k = keyb_getkey();
523
 
533
 
524
    if (k == 0x150) { /* down */
534
    if (k == 0x150) { /* down */
525
      cursor_down(&db, screenh, &uidirtyfrom, &uidirtyto);
535
      cursor_down(&db, screenh, &uidirtyfrom, &uidirtyto);
526
 
536
 
527
    } else if (k == 0x148) { /* up */
537
    } else if (k == 0x148) { /* up */
528
      cursor_up(&db, &uidirtyfrom, &uidirtyto);
538
      cursor_up(&db, &uidirtyfrom, &uidirtyto);
529
 
539
 
530
    } else if (k == 0x14D) { /* right */
540
    } else if (k == 0x14D) { /* right */
531
      cursor_right(&db, screenw, screenh, &uidirtyfrom, &uidirtyto);
541
      cursor_right(&db, screenw, screenh, &uidirtyfrom, &uidirtyto);
532
 
542
 
533
    } else if (k == 0x14B) { /* left */
543
    } else if (k == 0x14B) { /* left */
534
      cursor_left(&db, screenw, &uidirtyfrom, &uidirtyto);
544
      cursor_left(&db, screenw, &uidirtyfrom, &uidirtyto);
535
 
545
 
536
    } else if (k == 0x149) { /* pgup */
546
    } else if (k == 0x149) { /* pgup */
537
      // TODO
547
      // TODO
538
 
548
 
539
    } else if (k == 0x151) { /* pgdown */
549
    } else if (k == 0x151) { /* pgdown */
540
      // TODO
550
      // TODO
541
 
551
 
542
    } else if (k == 0x147) { /* home */
552
    } else if (k == 0x147) { /* home */
543
       cursor_home(&db, &uidirtyfrom, &uidirtyto);
553
       cursor_home(&db, &uidirtyfrom, &uidirtyto);
544
 
554
 
545
    } else if (k == 0x14F) { /* end */
555
    } else if (k == 0x14F) { /* end */
546
       cursor_eol(&db, screenw, &uidirtyfrom, &uidirtyto);
556
       cursor_eol(&db, screenw, &uidirtyfrom, &uidirtyto);
547
 
557
 
548
    } else if (k == 0x1B) { /* ESC */
558
    } else if (k == 0x1B) { /* ESC */
549
      break;
559
      break;
550
 
560
 
551
    } else if (k == 0x0D) { /* ENTER */
561
    } else if (k == 0x0D) { /* ENTER */
552
      /* add a new line */
562
      /* add a new line */
553
      if (line_add(&db, db.cursor->payload + db.xoffset + db.cursorposx) == 0) {
563
      if (line_add(&db, db.cursor->payload + db.xoffset + db.cursorposx) == 0) {
554
        /* trim the line above */
564
        /* trim the line above */
555
        db.cursor->prev->len = db.xoffset + db.cursorposx;
565
        db.cursor->prev->len = db.xoffset + db.cursorposx;
556
        db.cursor->prev->payload[db.cursor->prev->len] = 0;
566
        db.cursor->prev->payload[db.cursor->prev->len] = 0;
557
        /* move cursor to the (new) line below */
567
        /* move cursor to the (new) line below */
558
        db.cursorposx = 0;
568
        db.cursorposx = 0;
559
        if (db.cursorposy < screenh - 2) {
569
        if (db.cursorposy < screenh - 2) {
560
          uidirtyfrom = db.cursorposy;
570
          uidirtyfrom = db.cursorposy;
561
          db.cursorposy++;
571
          db.cursorposy++;
562
        } else {
572
        } else {
563
          uidirtyfrom = 0;
573
          uidirtyfrom = 0;
564
        }
574
        }
565
        uidirtyto = 0xff;
575
        uidirtyto = 0xff;
566
      } else {
576
      } else {
567
        /* ERROR: OUT OF MEMORY */
577
        /* ERROR: OUT OF MEMORY */
568
      }
578
      }
569
 
579
 
570
    } else if (k == 0x153) {  /* DEL */
580
    } else if (k == 0x153) {  /* DEL */
571
      del(&db, &uidirtyfrom, &uidirtyto);
581
      del(&db, &uidirtyfrom, &uidirtyto);
572
 
582
 
573
    } else if (k == 0x008) { /* BKSPC */
583
    } else if (k == 0x008) { /* BKSPC */
574
      bkspc(&db, screenw, &uidirtyfrom, &uidirtyto);
584
      bkspc(&db, screenw, &uidirtyfrom, &uidirtyto);
575
 
585
 
576
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
586
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
577
      char c = k;
587
      char c = k;
578
      insert_in_line(&db, &c, 1, screenw, screenh, &uidirtyfrom, &uidirtyto);
588
      insert_in_line(&db, &c, 1, screenw, screenh, &uidirtyfrom, &uidirtyto);
579
 
589
 
580
    } else if (k == 0x009) { /* TAB */
590
    } else if (k == 0x009) { /* TAB */
581
      const char *tab = "        ";
591
      const char *tab = "        ";
582
      insert_in_line(&db, tab, 8, screenw, screenh, &uidirtyfrom, &uidirtyto);
592
      insert_in_line(&db, tab, 8, screenw, screenh, &uidirtyfrom, &uidirtyto);
583
 
593
 
584
    } else if (k == 0x13b) { /* F1 */
594
    } else if (k == 0x13b) { /* F1 */
585
      ui_help(screenw);
595
      ui_help(screenw);
586
      uidirtyfrom = 0;
596
      uidirtyfrom = 0;
587
      uidirtyto = 0xff;
597
      uidirtyto = 0xff;
588
 
598
 
589
    } else if (k == 0x13f) { /* F5 */
599
    } else if (k == 0x13f) { /* F5 */
590
      const char *m[] = {"SAVED", "SAVING FILE FAILED"};
600
      const char *m[] = {"SAVED", "SAVING FILE FAILED"};
591
      int i;
601
      int i;
592
      i = !!savefile(&db, fname);
602
      i = !!savefile(&db, fname);
593
      ui_msg(m[i], screenw, screenh, &uidirtyfrom, &uidirtyto);
603
      ui_msg(m[i], screenw, screenh, &uidirtyfrom, &uidirtyto);
594
      mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
604
      mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
595
 
605
 
596
    } else if (k == 0x144) { /* F10 */
606
    } else if (k == 0x144) { /* F10 */
597
      db.lfonly ^= 1;
607
      db.lfonly ^= 1;
598
      ui_basic(screenw, screenh, fname, &db);
608
      ui_basic(screenw, screenh, fname, &db);
599
 
609
 
600
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
610
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
601
      char buff[4];
611
      char buff[4];
602
      const char *HEX = "0123456789ABCDEF";
612
      const char *HEX = "0123456789ABCDEF";
603
      buff[0] = HEX[(k >> 8) & 15];
613
      buff[0] = HEX[(k >> 8) & 15];
604
      buff[1] = HEX[(k >> 4) & 15];
614
      buff[1] = HEX[(k >> 4) & 15];
605
      buff[2] = HEX[k & 15];
615
      buff[2] = HEX[k & 15];
606
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
616
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
607
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
617
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
608
      keyb_getkey();
618
      keyb_getkey();
609
      break;
619
      break;
610
    }
620
    }
611
  }
621
  }
612
 
622
 
613
  mdr_cout_close();
623
  mdr_cout_close();
614
 
624
 
615
  /* TODO free memory */
625
  /* TODO free memory */
616
 
626
 
617
  return(0);
627
  return(0);
618
}
628
}
619
 
629