Subversion Repositories SvarDOS

Rev

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

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