Subversion Repositories SvarDOS

Rev

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

Rev 1544 Rev 1545
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>      /* _dos_open(), _dos_read(), _dos_close(), ... */
26
#include <dos.h>      /* _dos_open(), _dos_read(), _dos_close(), ... */
27
#include <fcntl.h>    /* O_RDONLY, O_WRONLY */
27
#include <fcntl.h>    /* O_RDONLY, O_WRONLY */
28
#include <stdlib.h>   /* _osmajor symbol provided by Open Watcom */
28
#include <stdlib.h>   /* _osmajor symbol provided by Open Watcom */
29
#include <string.h>
29
#include <string.h>
-
 
30
#include <strings.h>
30
 
31
 
31
#include "mdr\bios.h"
32
#include "mdr\bios.h"
32
#include "mdr\cout.h"
33
#include "mdr\cout.h"
33
#include "mdr\dos.h"
34
#include "mdr\dos.h"
34
 
35
 
35
#include "svarlang\svarlang.h"
36
#include "svarlang\svarlang.h"
36
 
37
 
37
 
38
 
38
#define PVER "2023.5"
39
#define PVER "2023.5"
39
#define PDATE "2023"
40
#define PDATE "2023"
40
 
41
 
41
/*****************************************************************************
42
/*****************************************************************************
42
 * global variables and definitions                                          *
43
 * global variables and definitions                                          *
43
 *****************************************************************************/
44
 *****************************************************************************/
44
 
45
 
45
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
46
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
46
static unsigned char SCHEME_TEXT   = 0x07,
47
static unsigned char SCHEME_TEXT   = 0x07,
47
                     SCHEME_MENU   = 0x70,
48
                     SCHEME_MENU   = 0x70,
48
                     SCHEME_MENU_CUR= 0x0f,
49
                     SCHEME_MENU_CUR= 0x0f,
49
                     SCHEME_MENU_SEL= 0x00,
50
                     SCHEME_MENU_SEL= 0x00,
50
                     SCHEME_STBAR1 = 0x70,
51
                     SCHEME_STBAR1 = 0x70,
51
                     SCHEME_STBAR2 = 0x70, /* greyed out information */
52
                     SCHEME_STBAR2 = 0x70, /* greyed out information */
52
                     SCHEME_STBAR3 = 0x70, /* query */
53
                     SCHEME_STBAR3 = 0x70, /* query */
53
                     SCHEME_SCROLL = 0x70,
54
                     SCHEME_SCROLL = 0x70,
54
                     SCHEME_MSG    = 0x70,
55
                     SCHEME_MSG    = 0x70,
55
                     SCHEME_ERR    = 0x70;
56
                     SCHEME_ERR    = 0x70;
56
 
57
 
57
static unsigned char screenw, screenh, screenlastrow, screenlastcol;
58
static unsigned char screenw, screenh, screenlastrow, screenlastcol;
58
static unsigned char glob_monomode, glob_tablessmode;
59
static unsigned char glob_monomode, glob_tablessmode;
59
 
60
 
60
static struct {
61
static struct {
61
    unsigned char from;
62
    unsigned char from;
62
    unsigned char to;
63
    unsigned char to;
63
    unsigned char statusbar;
64
    unsigned char statusbar;
64
} uidirty = {0, 0xff, 1}; /* make sure to redraw entire UI at first run */
65
} uidirty = {0, 0xff, 1}; /* make sure to redraw entire UI at first run */
65
 
66
 
66
#define SCROLL_CURSOR 0xB1
67
#define SCROLL_CURSOR 0xB1
67
 
68
 
68
struct line {
69
struct line {
69
  unsigned short len;
70
  unsigned short len;
70
  struct line far *next;
71
  struct line far *next;
71
  struct line far *prev;
72
  struct line far *prev;
72
  char payload[1];
73
  char payload[1];
73
};
74
};
74
 
75
 
75
struct file {
76
struct file {
76
  struct line far *cursor;
77
  struct line far *cursor;
77
  unsigned short xoffset;
78
  unsigned short xoffset;
78
  unsigned short cursorposx;
79
  unsigned short cursorposx;
79
  unsigned short cursorposy;
80
  unsigned short cursorposy;
80
  unsigned short totlines;
81
  unsigned short totlines;
81
  unsigned short curline;
82
  unsigned short curline;
82
  unsigned short curline_prev;
83
  unsigned short curline_prev;
83
  char lfonly;   /* set if line endings are LF (CR/LF otherwise) */
84
  char lfonly;   /* set if line endings are LF (CR/LF otherwise) */
84
  char modflag;  /* non-zero if file has been modified since last save */
85
  char modflag;  /* non-zero if file has been modified since last save */
85
  char modflagprev;
86
  char modflagprev;
86
  char fname[128];
87
  char fname[128];
87
};
88
};
88
 
89
 
89
 
90
 
90
/*****************************************************************************
91
/*****************************************************************************
91
 * functions                                                                 *
92
 * functions                                                                 *
92
 *****************************************************************************/
93
 *****************************************************************************/
93
 
94
 
94
static struct line far *line_calloc(unsigned short siz) {
95
static struct line far *line_calloc(unsigned short siz) {
95
  struct line far *res;
96
  struct line far *res;
96
  unsigned int seg;
97
  unsigned int seg;
97
 
98
 
98
  if (_dos_allocmem((sizeof(struct line) + siz + 15) / 16, &seg) != 0) return(NULL);
99
  if (_dos_allocmem((sizeof(struct line) + siz + 15) / 16, &seg) != 0) return(NULL);
99
 
100
 
100
  res = MK_FP(seg, 0);
101
  res = MK_FP(seg, 0);
101
  res->len = 0;
102
  res->len = 0;
102
  res->next = NULL;
103
  res->next = NULL;
103
  res->prev = NULL;
104
  res->prev = NULL;
104
 
105
 
105
  return(res);
106
  return(res);
106
}
107
}
107
 
108
 
108
 
109
 
109
static void line_free(struct line far *ptr) {
110
static void line_free(struct line far *ptr) {
110
  _dos_freemem(FP_SEG(ptr));
111
  _dos_freemem(FP_SEG(ptr));
111
}
112
}
112
 
113
 
113
 
114
 
114
static int curline_resize(struct file far *db, unsigned short newsiz) {
115
static int curline_resize(struct file far *db, unsigned short newsiz) {
115
  unsigned int maxavail;
116
  unsigned int maxavail;
116
  struct line far *newptr;
117
  struct line far *newptr;
117
 
118
 
118
  /* try resizing the block (much faster) */
119
  /* try resizing the block (much faster) */
119
  if (_dos_setblock((sizeof(struct line) + newsiz + 15) / 16, FP_SEG(db->cursor), &maxavail) == 0) return(0);
120
  if (_dos_setblock((sizeof(struct line) + newsiz + 15) / 16, FP_SEG(db->cursor), &maxavail) == 0) return(0);
120
 
121
 
121
  /* create a new block and copy data over */
122
  /* create a new block and copy data over */
122
  newptr = line_calloc(newsiz);
123
  newptr = line_calloc(newsiz);
123
  if (newptr == NULL) return(-1);
124
  if (newptr == NULL) return(-1);
124
  _fmemmove(newptr, db->cursor, sizeof(struct line) + db->cursor->len);
125
  _fmemmove(newptr, db->cursor, sizeof(struct line) + db->cursor->len);
125
 
126
 
126
  /* rewire the linked list */
127
  /* rewire the linked list */
127
  db->cursor = newptr;
128
  db->cursor = newptr;
128
  if (newptr->next) newptr->next->prev = newptr;
129
  if (newptr->next) newptr->next->prev = newptr;
129
  if (newptr->prev) newptr->prev->next = newptr;
130
  if (newptr->prev) newptr->prev->next = newptr;
130
 
131
 
131
  return(0);
132
  return(0);
132
}
133
}
133
 
134
 
134
 
135
 
135
/* adds a new line at cursor position into file linked list and advance cursor
136
/* adds a new line at cursor position into file linked list and advance cursor
136
 * returns non-zero on error */
137
 * returns non-zero on error */
137
static int line_add(struct file *db, const char far *line, unsigned short slen) {
138
static int line_add(struct file *db, const char far *line, unsigned short slen) {
138
  struct line far *l;
139
  struct line far *l;
139
 
140
 
140
  l = line_calloc(slen);
141
  l = line_calloc(slen);
141
  if (l == NULL) return(-1);
142
  if (l == NULL) return(-1);
142
 
143
 
143
  l->prev = db->cursor;
144
  l->prev = db->cursor;
144
  if (db->cursor) {
145
  if (db->cursor) {
145
    l->next = db->cursor->next;
146
    l->next = db->cursor->next;
146
    db->cursor->next = l;
147
    db->cursor->next = l;
147
    l->next->prev = l;
148
    l->next->prev = l;
148
  }
149
  }
149
  db->cursor = l;
150
  db->cursor = l;
150
  if (slen > 0) {
151
  if (slen > 0) {
151
    _fmemmove(l->payload, line, slen);
152
    _fmemmove(l->payload, line, slen);
152
    l->len = slen;
153
    l->len = slen;
153
  }
154
  }
154
 
155
 
155
  db->totlines += 1;
156
  db->totlines += 1;
156
  db->curline += 1;
157
  db->curline += 1;
157
 
158
 
158
  return(0);
159
  return(0);
159
}
160
}
160
 
161
 
161
 
162
 
162
static void ui_getstring(const char *query, char *s, unsigned short maxlen) {
163
static void ui_getstring(const char *query, char *s, unsigned short maxlen) {
163
  unsigned short len = 0;
164
  unsigned short len = 0;
164
  unsigned char x;
165
  unsigned char x;
165
  int k;
166
  int k;
166
 
167
 
167
  if (maxlen == 0) return;
168
  if (maxlen == 0) return;
168
  maxlen--; /* make room for the nul terminator */
169
  maxlen--; /* make room for the nul terminator */
169
 
170
 
170
  /* print query string */
171
  /* print query string */
171
  x = mdr_cout_str(screenlastrow, 0, query, SCHEME_STBAR3, 40);
172
  x = mdr_cout_str(screenlastrow, 0, query, SCHEME_STBAR3, 40);
172
  mdr_cout_char_rep(screenlastrow, x++, ' ', SCHEME_STBAR3, screenw - x);
173
  mdr_cout_char_rep(screenlastrow, x++, ' ', SCHEME_STBAR3, screenw - x);
173
 
174
 
174
  for (;;) {
175
  for (;;) {
175
    mdr_cout_locate(screenlastrow, x + len);
176
    mdr_cout_locate(screenlastrow, x + len);
176
    k = mdr_dos_getkey2();
177
    k = mdr_dos_getkey2();
177
 
178
 
178
    switch (k) {
179
    switch (k) {
179
      case 0x1b: /* ESC */
180
      case 0x1b: /* ESC */
180
        s[0] = 0;
181
        s[0] = 0;
181
        return;
182
        return;
182
      case '\r':
183
      case '\r':
183
        s[len] = 0;
184
        s[len] = 0;
184
        return;
185
        return;
185
      case 0x08: /* BKSPC */
186
      case 0x08: /* BKSPC */
186
        if (len > 0) {
187
        if (len > 0) {
187
          len--;
188
          len--;
188
          mdr_cout_char(screenlastrow, x + len, ' ', SCHEME_STBAR3);
189
          mdr_cout_char(screenlastrow, x + len, ' ', SCHEME_STBAR3);
189
        }
190
        }
190
        break;
191
        break;
191
      default:
192
      default:
192
        if ((k <= 0xff) && (k >= ' ') && (len < maxlen)) {
193
        if ((k <= 0xff) && (k >= ' ') && (len < maxlen)) {
193
          mdr_cout_char(screenlastrow, x + len, k, SCHEME_STBAR3);
194
          mdr_cout_char(screenlastrow, x + len, k, SCHEME_STBAR3);
194
          s[len++] = k;
195
          s[len++] = k;
195
        }
196
        }
196
    }
197
    }
197
  }
198
  }
198
 
199
 
199
}
200
}
200
 
201
 
201
 
202
 
202
/* append a nul-terminated string to line at cursor position */
203
/* append a nul-terminated string to line at cursor position */
203
static int line_append(struct file *f, const char far *buf, unsigned short len) {
204
static int line_append(struct file *f, const char far *buf, unsigned short len) {
204
  if (sizeof(struct line) + f->cursor->len + len < len) goto ERR; /* overflow check */
205
  if (sizeof(struct line) + f->cursor->len + len < len) goto ERR; /* overflow check */
205
  if (curline_resize(f, f->cursor->len + len) != 0) goto ERR;
206
  if (curline_resize(f, f->cursor->len + len) != 0) goto ERR;
206
 
207
 
207
  _fmemmove(f->cursor->payload + f->cursor->len, buf, len);
208
  _fmemmove(f->cursor->payload + f->cursor->len, buf, len);
208
  f->cursor->len += len;
209
  f->cursor->len += len;
209
 
210
 
210
  return(0);
211
  return(0);
211
  ERR:
212
  ERR:
212
  return(-1);
213
  return(-1);
213
}
214
}
214
 
215
 
215
 
216
 
216
static void db_rewind(struct file *db) {
217
static void db_rewind(struct file *db) {
217
  if (db->cursor == NULL) return;
218
  if (db->cursor == NULL) return;
218
  while (db->cursor->prev) db->cursor = db->cursor->prev;
219
  while (db->cursor->prev) db->cursor = db->cursor->prev;
219
  db->curline = 0;
220
  db->curline = 0;
220
}
221
}
221
 
222
 
222
 
223
 
223
/* saves db file, resets db->modflag on success and overwrites the filename
224
/* saves db file, resets db->modflag on success and overwrites the filename
224
 * field if saveas is not NULL */
225
 * field if saveas is not NULL */
225
static int savefile(struct file *db, const char *saveas) {
226
static int savefile(struct file *db, const char *saveas) {
226
  int fd = 0;
227
  int fd = 0;
227
  const struct line far *l;
228
  const struct line far *l;
228
  unsigned int bytes;
229
  unsigned int bytes;
229
  unsigned char eollen = 2;
230
  unsigned char eollen = 2;
230
  const unsigned char *eolbuf = "\r\n";
231
  const unsigned char *eolbuf = "\r\n";
231
  int errflag = 0;
232
  int errflag = 0;
232
 
233
 
233
  /* if filename not overloaded then use the fname in db */
234
  /* if filename not overloaded then use the fname in db */
234
  if (saveas == NULL) saveas = db->fname;
235
  if (saveas == NULL) saveas = db->fname;
235
 
236
 
236
  _asm {
237
  _asm {
237
    push ax
238
    push ax
238
    push cx
239
    push cx
239
    push dx
240
    push dx
240
 
241
 
241
    mov ah, 0x3C    /* create or truncate file */
242
    mov ah, 0x3C    /* create or truncate file */
242
    xor cx, cx      /* file attributes */
243
    xor cx, cx      /* file attributes */
243
    mov dx, saveas  /* works only in SMALL/TINY mode */
244
    mov dx, saveas  /* works only in SMALL/TINY mode */
244
    int 0x21
245
    int 0x21
245
    jnc DONE
246
    jnc DONE
246
    mov errflag, ax
247
    mov errflag, ax
247
    DONE:
248
    DONE:
248
    mov fd, ax
249
    mov fd, ax
249
 
250
 
250
    pop dx
251
    pop dx
251
    pop cx
252
    pop cx
252
    pop ax
253
    pop ax
253
  }
254
  }
254
 
255
 
255
  if (errflag != 0) return(-1);
256
  if (errflag != 0) return(-1);
256
 
257
 
257
  l = db->cursor;
258
  l = db->cursor;
258
  while (l->prev) l = l->prev;
259
  while (l->prev) l = l->prev;
259
 
260
 
260
  /* preset line terminators */
261
  /* preset line terminators */
261
  if (db->lfonly) {
262
  if (db->lfonly) {
262
    eolbuf++;
263
    eolbuf++;
263
    eollen--;
264
    eollen--;
264
  }
265
  }
265
 
266
 
266
  while (l) {
267
  while (l) {
267
    /* do not write the last empty line, it is only useful for edition */
268
    /* do not write the last empty line, it is only useful for edition */
268
    if (l->len != 0) {
269
    if (l->len != 0) {
269
      errflag |= _dos_write(fd, l->payload, l->len, &bytes);
270
      errflag |= _dos_write(fd, l->payload, l->len, &bytes);
270
    } else if (l->next == NULL) {
271
    } else if (l->next == NULL) {
271
      break;
272
      break;
272
    }
273
    }
273
    errflag |= _dos_write(fd, eolbuf, eollen, &bytes);
274
    errflag |= _dos_write(fd, eolbuf, eollen, &bytes);
274
    l = l->next;
275
    l = l->next;
275
  }
276
  }
276
 
277
 
277
  errflag |= _dos_close(fd);
278
  errflag |= _dos_close(fd);
278
 
279
 
279
  /* did it all work? */
280
  /* did it all work? */
280
  if (errflag == 0) {
281
  if (errflag == 0) {
281
    db->modflag = 0;
282
    db->modflag = 0;
282
    if (saveas != db->fname) mdr_dos_truename(db->fname, saveas);
283
    if (saveas != db->fname) mdr_dos_truename(db->fname, saveas);
283
  }
284
  }
284
 
285
 
285
  return(errflag);
286
  return(errflag);
286
}
287
}
287
 
288
 
288
 
289
 
289
static void ui_statusbar(const struct file *db, unsigned char slotnum) {
290
static void ui_statusbar(const struct file *db, unsigned char slotnum) {
290
  const char *s = svarlang_strid(0); /* ESC=MENU */
291
  const char *s = svarlang_strid(0); /* ESC=MENU */
291
  unsigned short helpcol = screenw - strlen(s);
292
  unsigned short helpcol = screenw - strlen(s);
292
  unsigned short col;
293
  unsigned short col;
293
 
294
 
294
  /* slot number (guaranteed to be 0-9) */
295
  /* slot number (guaranteed to be 0-9) */
295
  {
296
  {
296
    char slot[4] = "#00";
297
    char slot[4] = "#00";
297
    if (slotnum == 9) {
298
    if (slotnum == 9) {
298
      slot[1] = '1';
299
      slot[1] = '1';
299
    } else {
300
    } else {
300
      slotnum++;
301
      slotnum++;
301
      slot[2] += slotnum;
302
      slot[2] += slotnum;
302
    }
303
    }
303
    mdr_cout_str(screenlastrow, 0, slot, SCHEME_STBAR2, 3);
304
    mdr_cout_str(screenlastrow, 0, slot, SCHEME_STBAR2, 3);
304
  }
305
  }
305
 
306
 
306
  /* fill rest of status bar with background */
307
  /* fill rest of status bar with background */
307
  mdr_cout_char_rep(screenlastrow, 3, ' ', SCHEME_STBAR1, helpcol - 3);
308
  mdr_cout_char_rep(screenlastrow, 3, ' ', SCHEME_STBAR1, helpcol - 3);
308
 
309
 
309
  /* eol type */
310
  /* eol type */
310
  {
311
  {
311
    const char *eoltype = "CRLF";
312
    const char *eoltype = "CRLF";
312
    if (db->lfonly) eoltype += 2;
313
    if (db->lfonly) eoltype += 2;
313
    mdr_cout_str(screenlastrow, helpcol - 5, eoltype, SCHEME_STBAR1, 5);
314
    mdr_cout_str(screenlastrow, helpcol - 5, eoltype, SCHEME_STBAR1, 5);
314
  }
315
  }
315
 
316
 
316
  /* line numbers */
317
  /* line numbers */
317
  {
318
  {
318
    unsigned short x;
319
    unsigned short x;
319
    unsigned char count = 0;
320
    unsigned char count = 0;
320
    col = helpcol - 7;
321
    col = helpcol - 7;
321
 
322
 
322
    x = db->totlines;
323
    x = db->totlines;
323
    AGAIN:
324
    AGAIN:
324
    do {
325
    do {
325
      mdr_cout_char(screenlastrow, col--, '0' + (x % 10), SCHEME_STBAR1);
326
      mdr_cout_char(screenlastrow, col--, '0' + (x % 10), SCHEME_STBAR1);
326
      x /= 10;
327
      x /= 10;
327
    } while (x);
328
    } while (x);
328
    /* redo same exercise, but printing the current line now */
329
    /* redo same exercise, but printing the current line now */
329
    if (count == 0) {
330
    if (count == 0) {
330
      count = 1;
331
      count = 1;
331
      mdr_cout_char(screenlastrow, col--, '/', SCHEME_STBAR1);
332
      mdr_cout_char(screenlastrow, col--, '/', SCHEME_STBAR1);
332
      x = 1 + db->curline;
333
      x = 1 + db->curline;
333
      goto AGAIN;
334
      goto AGAIN;
334
    }
335
    }
335
  }
336
  }
336
 
337
 
337
  /* filename and modflag */
338
  /* filename and modflag */
338
  {
339
  {
339
    const char *fn;
340
    const char *fn;
340
    unsigned short x;
341
    unsigned short x;
341
    unsigned short maxfnlen = col - 6;
342
    unsigned short maxfnlen = col - 6;
342
    if (db->fname[0] == 0) {
343
    if (db->fname[0] == 0) {
343
      fn = svarlang_str(0, 1); /* "UNTITLED" */
344
      fn = svarlang_str(0, 1); /* "UNTITLED" */
344
    } else {
345
    } else {
345
      /* display filename up to maxfnlen chars */
346
      /* display filename up to maxfnlen chars */
346
      fn = db->fname;
347
      fn = db->fname;
347
      x = strlen(fn);
348
      x = strlen(fn);
348
      if (x > maxfnlen) fn += x - maxfnlen;
349
      if (x > maxfnlen) fn += x - maxfnlen;
349
    }
350
    }
350
    x = mdr_cout_str(screenlastrow, 4, fn, SCHEME_STBAR1, maxfnlen);
351
    x = mdr_cout_str(screenlastrow, 4, fn, SCHEME_STBAR1, maxfnlen);
351
    if (db->modflag) mdr_cout_char(screenlastrow, 5 + x, '!', SCHEME_STBAR2);
352
    if (db->modflag) mdr_cout_char(screenlastrow, 5 + x, '!', SCHEME_STBAR2);
352
  }
353
  }
353
 
354
 
354
  mdr_cout_str(screenlastrow, helpcol, s, SCHEME_STBAR2, 40);
355
  mdr_cout_str(screenlastrow, helpcol, s, SCHEME_STBAR2, 40);
355
}
356
}
356
 
357
 
357
 
358
 
358
static void ui_msg(const char *msg1, const char *msg2, unsigned char attr) {
359
static void ui_msg(const char *msg1, const char *msg2, unsigned char attr) {
359
  unsigned short x, y, msglen, i;
360
  unsigned short x, y, msglen, i;
360
  unsigned short msg2flag = 0;
361
  unsigned short msg2flag = 0;
361
 
362
 
362
  msglen = strlen(msg1);
363
  msglen = strlen(msg1);
363
  if (msg2) {
364
  if (msg2) {
364
    msg2flag = 1;
365
    msg2flag = 1;
365
    i = strlen(msg2);
366
    i = strlen(msg2);
366
    if (i > msglen) msglen = i;
367
    if (i > msglen) msglen = i;
367
  }
368
  }
368
 
369
 
369
  y = (screenh - 6) >> 1;
370
  y = (screenh - 6) >> 1;
370
  x = (screenw - msglen - 3) >> 1;
371
  x = (screenw - msglen - 3) >> 1;
371
  for (i = y+2+msg2flag; i >= y; i--) mdr_cout_char_rep(i, x, ' ', attr, msglen + 2);
372
  for (i = y+2+msg2flag; i >= y; i--) mdr_cout_char_rep(i, x, ' ', attr, msglen + 2);
372
  x++;
373
  x++;
373
 
374
 
374
  mdr_cout_str(y+1, x, msg1, attr, msglen);
375
  mdr_cout_str(y+1, x, msg1, attr, msglen);
375
  if (msg2) mdr_cout_str(y+2, x, msg2, attr, msglen);
376
  if (msg2) mdr_cout_str(y+2, x, msg2, attr, msglen);
376
 
377
 
377
  if (uidirty.from > y) uidirty.from = y;
378
  if (uidirty.from > y) uidirty.from = y;
378
  if (uidirty.to < y+4) uidirty.to = y+4;
379
  if (uidirty.to < y+4) uidirty.to = y+4;
379
}
380
}
380
 
381
 
381
 
382
 
382
/* returns 0 if operation may proceed, non-zero to cancel */
383
/* returns 0 if operation may proceed, non-zero to cancel */
383
static unsigned char ui_confirm_if_unsaved(struct file *db) {
384
static unsigned char ui_confirm_if_unsaved(struct file *db) {
384
  int k;
385
  int k;
385
 
386
 
386
  if (db->modflag == 0) return(0);
387
  if (db->modflag == 0) return(0);
387
 
388
 
388
  mdr_cout_cursor_hide();
389
  mdr_cout_cursor_hide();
389
 
390
 
390
  /* if file has been modified then ask for confirmation:
391
  /* if file has been modified then ask for confirmation:
391
   * ENTER        : agree to data loss
392
   * ENTER        : agree to data loss
392
   * SPACE        : SAVE file before quit (only if valid filename present)
393
   * SPACE        : SAVE file before quit (only if valid filename present)
393
   * anything else: ABORT */
394
   * anything else: ABORT */
394
  ui_msg(svarlang_str(0,4), svarlang_str(0,5), SCHEME_MSG);
395
  ui_msg(svarlang_str(0,4), svarlang_str(0,5), SCHEME_MSG);
395
 
396
 
396
  k = mdr_dos_getkey2();
397
  k = mdr_dos_getkey2();
397
  mdr_cout_cursor_show();
398
  mdr_cout_cursor_show();
398
 
399
 
399
  /* ENTER = agree to loose unsaved data */
400
  /* ENTER = agree to loose unsaved data */
400
  if (k == '\r') return(0);
401
  if (k == '\r') return(0);
401
 
402
 
402
  /* SPACE = save file and continue */
403
  /* SPACE = save file and continue */
403
  if ((k == ' ') && (db->fname[0] != 0) && (savefile(db, NULL) == 0)) return(0);
404
  if ((k == ' ') && (db->fname[0] != 0) && (savefile(db, NULL) == 0)) return(0);
404
 
405
 
405
  /* any other key = cancel operation */
406
  /* any other key = cancel operation */
406
  return(1);
407
  return(1);
407
}
408
}
408
 
409
 
409
 
410
 
410
static void ui_refresh(const struct file *db) {
411
static void ui_refresh(const struct file *db) {
411
  unsigned char x;
412
  unsigned char x;
412
  const struct line far *l;
413
  const struct line far *l;
413
  unsigned char y = db->cursorposy;
414
  unsigned char y = db->cursorposy;
414
 
415
 
415
  /* quit early if nothing to refresh */
416
  /* quit early if nothing to refresh */
416
  if (uidirty.from == 0xff) return;
417
  if (uidirty.from == 0xff) return;
417
 
418
 
418
#ifdef DBG_REFRESH
419
#ifdef DBG_REFRESH
419
  static char m = 'a';
420
  static char m = 'a';
420
  m++;
421
  m++;
421
  if (m > 'z') m = 'a';
422
  if (m > 'z') m = 'a';
422
#endif
423
#endif
423
 
424
 
424
  /* rewind cursor line to first line that needs redrawing */
425
  /* rewind cursor line to first line that needs redrawing */
425
  for (l = db->cursor; y > uidirty.from; y--) l = l->prev;
426
  for (l = db->cursor; y > uidirty.from; y--) l = l->prev;
426
 
427
 
427
  /* iterate over lines and redraw whatever needs to be redrawn */
428
  /* iterate over lines and redraw whatever needs to be redrawn */
428
  for (; l != NULL; l = l->next, y++) {
429
  for (; l != NULL; l = l->next, y++) {
429
 
430
 
430
    /* skip lines that do not need to be refreshed */
431
    /* skip lines that do not need to be refreshed */
431
    if (y < uidirty.from) continue;
432
    if (y < uidirty.from) continue;
432
    if (y > uidirty.to) break;
433
    if (y > uidirty.to) break;
433
 
434
 
434
    x = 0;
435
    x = 0;
435
    if (db->xoffset < l->len) {
436
    if (db->xoffset < l->len) {
436
      unsigned char i, limit;
437
      unsigned char i, limit;
437
      if (l->len - db->xoffset < screenw) {
438
      if (l->len - db->xoffset < screenw) {
438
        limit = l->len;
439
        limit = l->len;
439
      } else {
440
      } else {
440
        limit = db->xoffset + screenlastcol;
441
        limit = db->xoffset + screenlastcol;
441
      }
442
      }
442
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], SCHEME_TEXT);
443
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], SCHEME_TEXT);
443
    }
444
    }
444
 
445
 
445
    /* write empty spaces until end of line */
446
    /* write empty spaces until end of line */
446
    if (x < screenlastcol) mdr_cout_char_rep(y, x, ' ', SCHEME_TEXT, screenlastcol - x);
447
    if (x < screenlastcol) mdr_cout_char_rep(y, x, ' ', SCHEME_TEXT, screenlastcol - x);
447
 
448
 
448
#ifdef DBG_REFRESH
449
#ifdef DBG_REFRESH
449
    mdr_cout_char(y, 0, m, SCHEME_STBAR1);
450
    mdr_cout_char(y, 0, m, SCHEME_STBAR1);
450
#endif
451
#endif
451
 
452
 
452
    if (y == screenh - 2) break;
453
    if (y == screenh - 2) break;
453
  }
454
  }
454
 
455
 
455
  /* fill all lines below if empty (and they need to be redrawn) */
456
  /* fill all lines below if empty (and they need to be redrawn) */
456
  if (l == NULL) {
457
  if (l == NULL) {
457
    while ((y < screenlastrow) && (y < uidirty.to)) {
458
    while ((y < screenlastrow) && (y < uidirty.to)) {
458
      mdr_cout_char_rep(y++, 0, ' ', SCHEME_TEXT, screenlastcol);
459
      mdr_cout_char_rep(y++, 0, ' ', SCHEME_TEXT, screenlastcol);
459
    }
460
    }
460
  }
461
  }
461
 
462
 
462
  /* scroll bar */
463
  /* scroll bar */
463
  for (y = 0; y < screenlastrow; y++) {
464
  for (y = 0; y < screenlastrow; y++) {
464
    mdr_cout_char(y, screenlastcol, SCROLL_CURSOR, SCHEME_SCROLL);
465
    mdr_cout_char(y, screenlastcol, SCROLL_CURSOR, SCHEME_SCROLL);
465
  }
466
  }
466
 
467
 
467
  /* scroll cursor */
468
  /* scroll cursor */
468
  if (db->totlines >= screenh) {
469
  if (db->totlines >= screenh) {
469
    unsigned short topline = db->curline - db->cursorposy;
470
    unsigned short topline = db->curline - db->cursorposy;
470
    unsigned short col;
471
    unsigned short col;
471
    unsigned short totlines = db->totlines - screenh + 1;
472
    unsigned short totlines = db->totlines - screenh + 1;
472
    if (db->totlines - screenh > screenh) {
473
    if (db->totlines - screenh > screenh) {
473
      col = topline / (totlines / screenlastrow);
474
      col = topline / (totlines / screenlastrow);
474
    } else {
475
    } else {
475
      col = topline * screenlastrow / totlines;
476
      col = topline * screenlastrow / totlines;
476
    }
477
    }
477
    if (col >= screenlastrow) col = screenh - 2;
478
    if (col >= screenlastrow) col = screenh - 2;
478
    mdr_cout_char(col, screenlastcol, ' ', SCHEME_SCROLL);
479
    mdr_cout_char(col, screenlastcol, ' ', SCHEME_SCROLL);
479
  }
480
  }
480
 
481
 
481
  /* clear out the dirty flag */
482
  /* clear out the dirty flag */
482
  uidirty.from = 0xff;
483
  uidirty.from = 0xff;
483
}
484
}
484
 
485
 
485
 
486
 
486
static void check_cursor_not_after_eol(struct file *db) {
487
static void check_cursor_not_after_eol(struct file *db) {
487
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
488
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
488
 
489
 
489
  if (db->cursor->len < db->xoffset) {
490
  if (db->cursor->len < db->xoffset) {
490
    db->cursorposx = 0;
491
    db->cursorposx = 0;
491
    db->xoffset = db->cursor->len;
492
    db->xoffset = db->cursor->len;
492
    uidirty.from = 0;
493
    uidirty.from = 0;
493
    uidirty.to = 0xff;
494
    uidirty.to = 0xff;
494
  } else {
495
  } else {
495
    db->cursorposx = db->cursor->len - db->xoffset;
496
    db->cursorposx = db->cursor->len - db->xoffset;
496
  }
497
  }
497
}
498
}
498
 
499
 
499
 
500
 
500
static void cursor_up(struct file *db) {
501
static void cursor_up(struct file *db) {
501
  if (db->cursor->prev == NULL) return;
502
  if (db->cursor->prev == NULL) return;
502
 
503
 
503
  db->curline -= 1;
504
  db->curline -= 1;
504
  db->cursor = db->cursor->prev;
505
  db->cursor = db->cursor->prev;
505
  if (db->cursorposy == 0) {
506
  if (db->cursorposy == 0) {
506
    uidirty.from = 0;
507
    uidirty.from = 0;
507
    uidirty.to = 0xff;
508
    uidirty.to = 0xff;
508
  } else {
509
  } else {
509
    db->cursorposy -= 1;
510
    db->cursorposy -= 1;
510
  }
511
  }
511
}
512
}
512
 
513
 
513
 
514
 
514
static void cursor_eol(struct file *db) {
515
static void cursor_eol(struct file *db) {
515
  /* adjust xoffset to make sure eol is visible on screen */
516
  /* adjust xoffset to make sure eol is visible on screen */
516
  if (db->xoffset > db->cursor->len) {
517
  if (db->xoffset > db->cursor->len) {
517
    db->xoffset = db->cursor->len - 1;
518
    db->xoffset = db->cursor->len - 1;
518
    uidirty.from = 0;
519
    uidirty.from = 0;
519
    uidirty.to = 0xff;
520
    uidirty.to = 0xff;
520
  }
521
  }
521
 
522
 
522
  if (db->xoffset + screenlastcol <= db->cursor->len) {
523
  if (db->xoffset + screenlastcol <= db->cursor->len) {
523
    db->xoffset = db->cursor->len - screenw + 2;
524
    db->xoffset = db->cursor->len - screenw + 2;
524
    uidirty.from = 0;
525
    uidirty.from = 0;
525
    uidirty.to = 0xff;
526
    uidirty.to = 0xff;
526
  }
527
  }
527
  db->cursorposx = db->cursor->len - db->xoffset;
528
  db->cursorposx = db->cursor->len - db->xoffset;
528
}
529
}
529
 
530
 
530
 
531
 
531
static void cursor_down(struct file *db) {
532
static void cursor_down(struct file *db) {
532
  if (db->cursor->next == NULL) return;
533
  if (db->cursor->next == NULL) return;
533
 
534
 
534
  db->curline += 1;
535
  db->curline += 1;
535
  db->cursor = db->cursor->next;
536
  db->cursor = db->cursor->next;
536
  if (db->cursorposy < screenh - 2) {
537
  if (db->cursorposy < screenh - 2) {
537
    db->cursorposy += 1;
538
    db->cursorposy += 1;
538
  } else {
539
  } else {
539
    uidirty.from = 0;
540
    uidirty.from = 0;
540
    uidirty.to = 0xff;
541
    uidirty.to = 0xff;
541
  }
542
  }
542
}
543
}
543
 
544
 
544
 
545
 
545
static void cursor_left(struct file *db) {
546
static void cursor_left(struct file *db) {
546
  if (db->cursorposx > 0) {
547
  if (db->cursorposx > 0) {
547
    db->cursorposx -= 1;
548
    db->cursorposx -= 1;
548
  } else if (db->xoffset > 0) {
549
  } else if (db->xoffset > 0) {
549
    db->xoffset -= 1;
550
    db->xoffset -= 1;
550
    uidirty.from = 0;
551
    uidirty.from = 0;
551
    uidirty.to = 0xff;
552
    uidirty.to = 0xff;
552
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
553
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
553
    cursor_up(db);
554
    cursor_up(db);
554
    cursor_eol(db);
555
    cursor_eol(db);
555
  }
556
  }
556
}
557
}
557
 
558
 
558
 
559
 
559
static void cursor_home(struct file *db) {
560
static void cursor_home(struct file *db) {
560
  db->cursorposx = 0;
561
  db->cursorposx = 0;
561
  if (db->xoffset != 0) {
562
  if (db->xoffset != 0) {
562
    db->xoffset = 0;
563
    db->xoffset = 0;
563
    uidirty.from = 0;
564
    uidirty.from = 0;
564
    uidirty.to = 0xff;
565
    uidirty.to = 0xff;
565
  }
566
  }
566
}
567
}
567
 
568
 
568
 
569
 
569
static void cursor_right(struct file *db) {
570
static void cursor_right(struct file *db) {
570
  if (db->cursor->len > db->xoffset + db->cursorposx) {
571
  if (db->cursor->len > db->xoffset + db->cursorposx) {
571
    if (db->cursorposx < screenw - 2) {
572
    if (db->cursorposx < screenw - 2) {
572
      db->cursorposx += 1;
573
      db->cursorposx += 1;
573
    } else {
574
    } else {
574
      db->xoffset += 1;
575
      db->xoffset += 1;
575
      uidirty.from = 0;
576
      uidirty.from = 0;
576
      uidirty.to = 0xff;
577
      uidirty.to = 0xff;
577
    }
578
    }
578
  } else if (db->cursor->next != NULL) { /* jump to start of next line */
579
  } else if (db->cursor->next != NULL) { /* jump to start of next line */
579
    cursor_down(db);
580
    cursor_down(db);
580
    cursor_home(db);
581
    cursor_home(db);
581
  }
582
  }
582
}
583
}
583
 
584
 
584
 
585
 
585
static void del(struct file *db) {
586
static void del(struct file *db) {
586
  if (db->cursorposx + db->xoffset < db->cursor->len) {
587
  if (db->cursorposx + db->xoffset < db->cursor->len) {
587
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
588
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
588
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
589
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
589
    uidirty.from = db->cursorposy;
590
    uidirty.from = db->cursorposy;
590
    uidirty.to = db->cursorposy;
591
    uidirty.to = db->cursorposy;
591
    db->modflag = 1;
592
    db->modflag = 1;
592
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
593
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
593
    struct line far *nextline = db->cursor->next;
594
    struct line far *nextline = db->cursor->next;
594
    if (db->cursor->next->len > 0) {
595
    if (db->cursor->next->len > 0) {
595
      if (curline_resize(db, db->cursor->len + db->cursor->next->len + 1) == 0) {
596
      if (curline_resize(db, db->cursor->len + db->cursor->next->len + 1) == 0) {
596
        _fmemmove(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
597
        _fmemmove(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
597
        db->cursor->len += db->cursor->next->len;
598
        db->cursor->len += db->cursor->next->len;
598
      }
599
      }
599
    }
600
    }
600
 
601
 
601
    db->cursor->next = db->cursor->next->next;
602
    db->cursor->next = db->cursor->next->next;
602
    db->cursor->next->prev = db->cursor;
603
    db->cursor->next->prev = db->cursor;
603
 
604
 
604
    line_free(nextline);
605
    line_free(nextline);
605
    uidirty.from = db->cursorposy;
606
    uidirty.from = db->cursorposy;
606
    uidirty.to = 0xff;
607
    uidirty.to = 0xff;
607
    db->totlines -= 1;
608
    db->totlines -= 1;
608
    db->modflag = 1;
609
    db->modflag = 1;
609
  }
610
  }
610
}
611
}
611
 
612
 
612
 
613
 
613
static void bkspc(struct file *db) {
614
static void bkspc(struct file *db) {
614
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
615
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
615
  if ((db->cursorposx + db->xoffset == 0) && (db->cursor->prev == NULL)) return;
616
  if ((db->cursorposx + db->xoffset == 0) && (db->cursor->prev == NULL)) return;
616
 
617
 
617
  cursor_left(db);
618
  cursor_left(db);
618
  del(db);
619
  del(db);
619
}
620
}
620
 
621
 
621
 
622
 
622
#define LOADFILE_FILENOTFOUND 2
623
#define LOADFILE_FILENOTFOUND 2
623
 
624
 
624
/* returns 0 on success, 1 on file not found, 2 on other error */
625
/* returns 0 on success, 1 on file not found, 2 on other error */
625
static unsigned char loadfile(struct file *db, const char *fname) {
626
static unsigned char loadfile(struct file *db, const char *fname) {
626
  char buff[512]; /* read one entire sector at a time (faster) */
627
  char buff[512]; /* read one entire sector at a time (faster) */
627
  char *buffptr;
628
  char *buffptr;
628
  unsigned int len, llen;
629
  unsigned int len, llen;
629
  int fd;
630
  int fd;
630
  unsigned char eolfound;
631
  unsigned char eolfound;
631
  unsigned char err = 0;
632
  unsigned char err = 0;
632
 
633
 
633
  /* free the entire linked list of lines */
634
  /* free the entire linked list of lines */
634
  db_rewind(db);
635
  db_rewind(db);
635
  while (db->cursor) {
636
  while (db->cursor) {
636
    struct line far *victim;
637
    struct line far *victim;
637
    victim = db->cursor;
638
    victim = db->cursor;
638
    db->cursor = db->cursor->next;
639
    db->cursor = db->cursor->next;
639
    line_free(victim);
640
    line_free(victim);
640
  }
641
  }
641
 
642
 
642
  /* zero out the struct */
643
  /* zero out the struct */
643
  bzero(db, sizeof(struct file));
644
  bzero(db, sizeof(struct file));
644
 
645
 
645
  /* start by adding an empty line */
646
  /* start by adding an empty line */
646
  if (line_add(db, NULL, 0) != 0) return(2);
647
  if (line_add(db, NULL, 0) != 0) return(2);
647
 
648
 
648
  if (fname == NULL) goto SKIPLOADING;
649
  if (fname == NULL) goto SKIPLOADING;
649
 
650
 
650
  /* make the filename canonical (DOS 3+ only, on earlier versions it just copies the filename) */
651
  /* make the filename canonical (DOS 3+ only, on earlier versions it just copies the filename) */
651
  mdr_dos_truename(db->fname, fname);
652
  mdr_dos_truename(db->fname, fname);
652
 
653
 
653
  err = _dos_open(fname, O_RDONLY, &fd);
654
  err = _dos_open(fname, O_RDONLY, &fd);
654
  if (err != 0) goto SKIPLOADING;
655
  if (err != 0) goto SKIPLOADING;
655
 
656
 
656
  db->lfonly = 1;
657
  db->lfonly = 1;
657
 
658
 
658
  for (eolfound = 0;;) {
659
  for (eolfound = 0;;) {
659
    unsigned short consumedbytes;
660
    unsigned short consumedbytes;
660
 
661
 
661
    if ((_dos_read(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
662
    if ((_dos_read(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
662
    buffptr = buff;
663
    buffptr = buff;
663
 
664
 
664
    FINDLINE:
665
    FINDLINE:
665
 
666
 
666
    /* look for nearest \n (also expand tabs) */
667
    /* look for nearest \n (also expand tabs) */
667
    for (consumedbytes = 0;; consumedbytes++) {
668
    for (consumedbytes = 0;; consumedbytes++) {
668
 
669
 
669
      if (buffptr[consumedbytes] == '\t') {
670
      if (buffptr[consumedbytes] == '\t') {
670
        llen = consumedbytes;
671
        llen = consumedbytes;
671
        break;
672
        break;
672
      }
673
      }
673
 
674
 
674
      if (consumedbytes == len) {
675
      if (consumedbytes == len) {
675
        llen = consumedbytes;
676
        llen = consumedbytes;
676
        break;
677
        break;
677
      }
678
      }
678
      if (buffptr[consumedbytes] == '\r') {
679
      if (buffptr[consumedbytes] == '\r') {
679
        llen = consumedbytes;
680
        llen = consumedbytes;
680
        consumedbytes++;
681
        consumedbytes++;
681
        db->lfonly = 0;
682
        db->lfonly = 0;
682
        break;
683
        break;
683
      }
684
      }
684
      if (buffptr[consumedbytes] == '\n') {
685
      if (buffptr[consumedbytes] == '\n') {
685
        eolfound = 1;
686
        eolfound = 1;
686
        llen = consumedbytes;
687
        llen = consumedbytes;
687
        consumedbytes++;
688
        consumedbytes++;
688
        break;
689
        break;
689
      }
690
      }
690
    }
691
    }
691
 
692
 
692
    /* consumedbytes is the amount of bytes processed from buffptr,
693
    /* consumedbytes is the amount of bytes processed from buffptr,
693
     * llen is the length of line's payload (without its line terminator) */
694
     * llen is the length of line's payload (without its line terminator) */
694
 
695
 
695
    /* append content, if line is non-empty */
696
    /* append content, if line is non-empty */
696
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
697
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
697
      goto IOERR;
698
      goto IOERR;
698
    }
699
    }
699
 
700
 
700
    /* allocate the next line if current line ended */
701
    /* allocate the next line if current line ended */
701
    if (eolfound) {
702
    if (eolfound) {
702
      if (line_add(db, NULL, 0) != 0) {
703
      if (line_add(db, NULL, 0) != 0) {
703
        goto IOERR;
704
        goto IOERR;
704
      }
705
      }
705
      eolfound = 0;
706
      eolfound = 0;
706
    }
707
    }
707
 
708
 
708
    /* append 8 spaces if tab char found */
709
    /* append 8 spaces if tab char found */
709
    if ((consumedbytes < len) && (buffptr[consumedbytes] == '\t') && (glob_tablessmode == 0)) {
710
    if ((consumedbytes < len) && (buffptr[consumedbytes] == '\t') && (glob_tablessmode == 0)) {
710
      consumedbytes++;
711
      consumedbytes++;
711
      if (line_append(db, "        ", 8) != 0) {
712
      if (line_append(db, "        ", 8) != 0) {
712
        goto IOERR;
713
        goto IOERR;
713
      }
714
      }
714
    }
715
    }
715
 
716
 
716
    /* anything left? process the buffer leftover again */
717
    /* anything left? process the buffer leftover again */
717
    if (consumedbytes < len) {
718
    if (consumedbytes < len) {
718
      len -= consumedbytes;
719
      len -= consumedbytes;
719
      buffptr += consumedbytes;
720
      buffptr += consumedbytes;
720
      goto FINDLINE;
721
      goto FINDLINE;
721
    }
722
    }
722
 
723
 
723
  }
724
  }
724
 
725
 
725
  _dos_close(fd);
726
  _dos_close(fd);
726
 
727
 
727
  SKIPLOADING:
728
  SKIPLOADING:
728
 
729
 
729
  /* rewind cursor to top of file because it has been used by line_add() */
730
  /* rewind cursor to top of file because it has been used by line_add() */
730
  db_rewind(db);
731
  db_rewind(db);
731
 
732
 
732
  return(err);
733
  return(err);
733
 
734
 
734
  IOERR:
735
  IOERR:
735
  _dos_close(fd);
736
  _dos_close(fd);
736
  return(1);
737
  return(1);
737
}
738
}
738
 
739
 
739
 
740
 
740
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
741
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
741
 * of argc and argv, saves some 330 bytes of binary size
742
 * of argc and argv, saves some 330 bytes of binary size
742
 * returns non-zero on error */
743
 * returns non-zero on error */
743
static int parseargv(struct file *dbarr) {
744
static int parseargv(struct file *dbarr) {
744
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
745
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
745
  unsigned short count = 0;
746
  unsigned short count = 0;
746
  char *arg;
747
  char *arg;
747
  unsigned short lastarg = 0;
748
  unsigned short lastarg = 0;
748
  unsigned char err;
749
  unsigned char err;
749
 
750
 
750
  while (!lastarg) {
751
  while (!lastarg) {
751
    /* jump to nearest arg */
752
    /* jump to nearest arg */
752
    while (*tail == ' ') {
753
    while (*tail == ' ') {
753
      *tail = 0;
754
      *tail = 0;
754
      tail++;
755
      tail++;
755
    }
756
    }
756
 
757
 
757
    if (*tail == '\r') {
758
    if (*tail == '\r') {
758
      *tail = 0;
759
      *tail = 0;
759
      break;
760
      break;
760
    }
761
    }
761
 
762
 
762
    arg = tail;
763
    arg = tail;
763
 
764
 
764
    /* jump to next delimiter */
765
    /* jump to next delimiter */
765
    while ((*tail != ' ') && (*tail != '\r')) tail++;
766
    while ((*tail != ' ') && (*tail != '\r')) tail++;
766
 
767
 
767
    /* if \r then remember this is the last arg */
768
    /* if \r then remember this is the last arg */
768
    if (*tail == '\r') lastarg = 1;
769
    if (*tail == '\r') lastarg = 1;
769
 
770
 
770
    *tail = 0;
771
    *tail = 0;
771
    tail++;
772
    tail++;
772
 
773
 
773
    /* look at the arg now */
774
    /* look at the arg now */
774
    if (*arg == '/') {
775
    if (*arg == '/') {
775
      if (arg[1] == 't') { /* /t = do not expand tabs */
776
      if (arg[1] == 't') { /* /t = do not expand tabs */
776
        glob_tablessmode = 1;
777
        glob_tablessmode = 1;
777
 
778
 
778
      } else if (arg[1] == 'm') { /* /m = force mono mode */
779
      } else if (arg[1] == 'm') { /* /m = force mono mode */
779
        glob_monomode = 1;
780
        glob_monomode = 1;
780
 
781
 
781
      } else {  /* help screen */
782
      } else {  /* help screen */
782
        mdr_coutraw_str(svarlang_str(1,3)); /* Sved, the SvarDOS editor */
783
        mdr_coutraw_str(svarlang_str(1,3)); /* Sved, the SvarDOS editor */
783
        mdr_coutraw_str(" [");
784
        mdr_coutraw_str(" [");
784
        mdr_coutraw_str(svarlang_str(1,4)); /* ver */
785
        mdr_coutraw_str(svarlang_str(1,4)); /* ver */
785
        mdr_coutraw_puts(" " PVER "]");
786
        mdr_coutraw_puts(" " PVER "]");
786
        mdr_coutraw_puts("Copyright (C) " PDATE " Mateusz Viste");
787
        mdr_coutraw_puts("Copyright (C) " PDATE " Mateusz Viste");
787
        mdr_coutraw_crlf();
788
        mdr_coutraw_crlf();
788
        mdr_coutraw_str("sved [/m] [/t] ");
789
        mdr_coutraw_str("sved [/m] [/t] ");
789
        mdr_coutraw_puts(svarlang_str(1,1)); /* args syntax */
790
        mdr_coutraw_puts(svarlang_str(1,1)); /* args syntax */
790
        mdr_coutraw_crlf();
791
        mdr_coutraw_crlf();
791
        mdr_coutraw_puts(svarlang_str(1,10)); /* /m */
792
        mdr_coutraw_puts(svarlang_str(1,10)); /* /m */
792
        mdr_coutraw_puts(svarlang_str(1,11)); /* /t */
793
        mdr_coutraw_puts(svarlang_str(1,11)); /* /t */
793
        return(-1);
794
        return(-1);
794
      }
795
      }
795
      continue;
796
      continue;
796
    }
797
    }
797
 
798
 
798
    /* looks to be a filename */
799
    /* looks to be a filename */
799
    if (count == 10) {
800
    if (count == 10) {
800
      mdr_coutraw_puts(svarlang_str(0,12)); /* too many files */
801
      mdr_coutraw_puts(svarlang_str(0,12)); /* too many files */
801
      return(-1);
802
      return(-1);
802
    }
803
    }
803
 
804
 
804
    /* try loading it */
805
    /* try loading it */
805
    mdr_coutraw_str(svarlang_str(1,2));
806
    mdr_coutraw_str(svarlang_str(1,2));
806
    mdr_coutraw_char(' ');
807
    mdr_coutraw_char(' ');
807
    mdr_coutraw_puts(arg);
808
    mdr_coutraw_puts(arg);
808
    err = loadfile(&(dbarr[count]), arg);
809
    err = loadfile(&(dbarr[count]), arg);
809
    if (err) {
810
    if (err) {
810
      if (err == LOADFILE_FILENOTFOUND) { /* file not found */
811
      if (err == LOADFILE_FILENOTFOUND) { /* file not found */
811
        if ((count == 0) && (lastarg != 0)) { /* a 'file not found' is fine if only one file was given */
812
        if ((count == 0) && (lastarg != 0)) { /* a 'file not found' is fine if only one file was given */
812
          err = 0;
813
          err = 0;
813
        } else {
814
        } else {
814
          err = 11;
815
          err = 11;
815
        }
816
        }
816
      } else { /* general error */
817
      } else { /* general error */
817
        err = 10;
818
        err = 10;
818
      }
819
      }
819
      if (err) {
820
      if (err) {
820
        mdr_coutraw_puts(svarlang_str(0,err));
821
        mdr_coutraw_puts(svarlang_str(0,err));
821
        return(-1);
822
        return(-1);
822
      }
823
      }
823
    }
824
    }
824
    count++;
825
    count++;
825
  }
826
  }
826
 
827
 
827
  return(0);
828
  return(0);
828
}
829
}
829
 
830
 
830
 
831
 
831
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
832
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
832
  if (curline_resize(db, db->cursor->len + len) == 0) {
833
  if (curline_resize(db, db->cursor->len + len) == 0) {
833
    unsigned short off = db->xoffset + db->cursorposx;
834
    unsigned short off = db->xoffset + db->cursorposx;
834
    db->modflag = 1;
835
    db->modflag = 1;
835
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
836
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
836
    db->cursor->len += len;
837
    db->cursor->len += len;
837
    uidirty.from = db->cursorposy;
838
    uidirty.from = db->cursorposy;
838
    uidirty.to = db->cursorposy;
839
    uidirty.to = db->cursorposy;
839
    while (len--) {
840
    while (len--) {
840
      db->cursor->payload[off++] = *databuf;
841
      db->cursor->payload[off++] = *databuf;
841
      databuf++;
842
      databuf++;
842
      cursor_right(db);
843
      cursor_right(db);
843
    }
844
    }
844
  }
845
  }
845
}
846
}
846
 
847
 
847
 
848
 
848
/* recompute db->curline by counting nodes in linked list */
849
/* recompute db->curline by counting nodes in linked list */
849
static void recompute_curline(struct file *db) {
850
static void recompute_curline(struct file *db) {
850
  const struct line far *l = db->cursor;
851
  const struct line far *l = db->cursor;
851
 
852
 
852
  db->curline = 0;
853
  db->curline = 0;
853
  while (l->prev != NULL) {
854
  while (l->prev != NULL) {
854
    db->curline += 1;
855
    db->curline += 1;
855
    l = l->prev;
856
    l = l->prev;
856
  }
857
  }
857
}
858
}
858
 
859
 
859
 
860
 
860
enum MENU_ACTION {
861
enum MENU_ACTION {
861
  MENU_NONE   = 0,
862
  MENU_NONE   = 0,
862
  MENU_OPEN   = 1,
863
  MENU_OPEN   = 1,
863
  MENU_SAVE   = 2,
864
  MENU_SAVE   = 2,
864
  MENU_SAVEAS = 3,
865
  MENU_SAVEAS = 3,
865
  MENU_CLOSE  = 4,
866
  MENU_CLOSE  = 4,
866
  MENU_CHGEOL = 5,
867
  MENU_CHGEOL = 5,
867
  MENU_QUIT   = 6
868
  MENU_QUIT   = 6
868
};
869
};
869
 
870
 
870
static enum MENU_ACTION ui_menu(void) {
871
static enum MENU_ACTION ui_menu(void) {
871
  unsigned short i, curchoice, attr, x, slen;
872
  unsigned short i, curchoice, attr, x, slen;
872
  unsigned short xorigin, yorigin;
873
  unsigned short xorigin, yorigin;
873
 
874
 
874
  /* find out the longest string */
875
  /* find out the longest string */
875
  slen = 0;
876
  slen = 0;
876
  for (i = MENU_OPEN; i <= MENU_QUIT; i++) {
877
  for (i = MENU_OPEN; i <= MENU_QUIT; i++) {
877
    x = strlen(svarlang_str(8, i));
878
    x = strlen(svarlang_str(8, i));
878
    if (x > slen) slen = x;
879
    if (x > slen) slen = x;
879
  }
880
  }
880
 
881
 
881
  /* calculate where to draw the menu on screen */
882
  /* calculate where to draw the menu on screen */
882
  xorigin = (screenw - (slen + 5)) / 2;
883
  xorigin = (screenw - (slen + 5)) / 2;
883
  yorigin = (screenh - (MENU_QUIT - MENU_OPEN + 6)) / 2;
884
  yorigin = (screenh - (MENU_QUIT - MENU_OPEN + 6)) / 2;
884
 
885
 
885
  /* */
886
  /* */
886
  uidirty.from = yorigin;
887
  uidirty.from = yorigin;
887
  uidirty.to = 0xff;
888
  uidirty.to = 0xff;
888
  uidirty.statusbar = 1;
889
  uidirty.statusbar = 1;
889
 
890
 
890
  /* hide the cursor */
891
  /* hide the cursor */
891
  mdr_cout_cursor_hide();
892
  mdr_cout_cursor_hide();
892
 
893
 
893
  curchoice = MENU_OPEN;
894
  curchoice = MENU_OPEN;
894
  for (;;) {
895
  for (;;) {
895
    /* render menu */
896
    /* render menu */
896
    for (i = MENU_NONE; i <= MENU_QUIT + 1; i++) {
897
    for (i = MENU_NONE; i <= MENU_QUIT + 1; i++) {
897
      mdr_cout_char_rep(yorigin + i, xorigin, ' ', SCHEME_MENU, slen+4);
898
      mdr_cout_char_rep(yorigin + i, xorigin, ' ', SCHEME_MENU, slen+4);
898
      if (i == curchoice) {
899
      if (i == curchoice) {
899
        attr = SCHEME_MENU_CUR;
900
        attr = SCHEME_MENU_CUR;
900
        mdr_cout_char_rep(yorigin + i, xorigin + 1, ' ', SCHEME_MENU_SEL, slen + 2);
901
        mdr_cout_char_rep(yorigin + i, xorigin + 1, ' ', SCHEME_MENU_SEL, slen + 2);
901
      } else {
902
      } else {
902
        attr = SCHEME_MENU;
903
        attr = SCHEME_MENU;
903
      }
904
      }
904
      mdr_cout_str(yorigin + i, xorigin + 2, svarlang_str(8, i), attr, slen);
905
      mdr_cout_str(yorigin + i, xorigin + 2, svarlang_str(8, i), attr, slen);
905
    }
906
    }
906
    /* wait for key */
907
    /* wait for key */
907
    switch (mdr_dos_getkey2()) {
908
    switch (mdr_dos_getkey2()) {
908
      case 0x150: /* down */
909
      case 0x150: /* down */
909
        if (curchoice == MENU_QUIT) {
910
        if (curchoice == MENU_QUIT) {
910
          curchoice = MENU_OPEN;
911
          curchoice = MENU_OPEN;
911
        } else {
912
        } else {
912
          curchoice++;
913
          curchoice++;
913
        }
914
        }
914
        break;
915
        break;
915
      case 0x148: /* up */
916
      case 0x148: /* up */
916
        if (curchoice == MENU_OPEN) {
917
        if (curchoice == MENU_OPEN) {
917
          curchoice = MENU_QUIT;
918
          curchoice = MENU_QUIT;
918
        } else {
919
        } else {
919
          curchoice--;
920
          curchoice--;
920
        }
921
        }
921
        break;
922
        break;
922
      default:
923
      default:
923
        curchoice = MENU_NONE;
924
        curchoice = MENU_NONE;
924
        /* FALLTHRU */
925
        /* FALLTHRU */
925
      case '\r': /* ENTER */
926
      case '\r': /* ENTER */
926
        mdr_cout_cursor_show();
927
        mdr_cout_cursor_show();
927
        return(curchoice);
928
        return(curchoice);
928
    }
929
    }
929
  }
930
  }
930
}
931
}
931
 
932
 
932
 
933
 
933
static struct file *select_slot(struct file *dbarr, unsigned char curfile) {
934
static struct file *select_slot(struct file *dbarr, unsigned char curfile) {
934
  uidirty.from = 0;
935
  uidirty.from = 0;
935
  uidirty.to = 0xff;
936
  uidirty.to = 0xff;
936
  uidirty.statusbar = 1;
937
  uidirty.statusbar = 1;
937
 
938
 
938
  dbarr = &(dbarr[curfile]);
939
  dbarr = &(dbarr[curfile]);
939
  /* force redraw now, because the main() routine might not if this is exit
940
  /* force redraw now, because the main() routine might not if this is exit
940
   * time and we want to show the user which file has unsaved changes */
941
   * time and we want to show the user which file has unsaved changes */
941
  ui_statusbar(dbarr, curfile);
942
  ui_statusbar(dbarr, curfile);
942
  ui_refresh(dbarr);
943
  ui_refresh(dbarr);
943
  return(dbarr);
944
  return(dbarr);
944
}
945
}
945
 
946
 
946
 
947
 
947
/* main returns nothing, ie. sved always exits with a zero exit code
948
/* main returns nothing, ie. sved always exits with a zero exit code
948
 * (this saves 20 bytes of executable footprint) */
949
 * (this saves 20 bytes of executable footprint) */
949
void main(void) {
950
void main(void) {
950
  static struct file dbarr[10];
951
  static struct file dbarr[10];
951
  unsigned char curfile;
952
  unsigned char curfile;
952
  struct file *db = dbarr; /* visible file is the first slot by default */
953
  struct file *db = dbarr; /* visible file is the first slot by default */
953
  struct line far *clipboard = NULL;
954
  struct line far *clipboard = NULL;
954
  static unsigned char original_breakflag;
955
  static unsigned char original_breakflag;
955
 
956
 
956
  { /* load NLS resource */
957
  { /* load NLS resource */
957
    unsigned short i = 0;
958
    unsigned short i = 0;
958
    const char far *selfptr;
959
    const char far *selfptr;
959
    char self[128], lang[8];
960
    char self[128], lang[8];
960
    selfptr = mdr_dos_selfexe();
961
    selfptr = mdr_dos_selfexe();
961
    if (selfptr != NULL) {
962
    if (selfptr != NULL) {
962
      do {
963
      do {
963
        self[i] = selfptr[i];
964
        self[i] = selfptr[i];
964
      } while (self[i++] != 0);
965
      } while (self[i++] != 0);
965
      svarlang_autoload_exepath(self, mdr_dos_getenv(lang, "LANG", sizeof(lang)));
966
      svarlang_autoload_exepath(self, mdr_dos_getenv(lang, "LANG", sizeof(lang)));
966
    }
967
    }
967
  }
968
  }
968
 
969
 
969
  /* preload all slots with empty files */
970
  /* preload all slots with empty files */
970
  for (curfile = 9;; curfile--) {
971
  for (curfile = 9;; curfile--) {
971
    loadfile(&(dbarr[curfile]), NULL);
972
    loadfile(&(dbarr[curfile]), NULL);
972
    if (curfile == 0) break;
973
    if (curfile == 0) break;
973
  }
974
  }
974
 
975
 
975
  /* parse argv (and load files, if any passed on) */
976
  /* parse argv (and load files, if any passed on) */
976
  if (parseargv(dbarr) != 0) return;
977
  if (parseargv(dbarr) != 0) return;
977
 
978
 
978
  if ((mdr_cout_init(&screenw, &screenh) != 0) && (glob_monomode == 0)) {
979
  if ((mdr_cout_init(&screenw, &screenh) != 0) && (glob_monomode == 0)) {
979
    /* load color scheme if mdr_cout_init returns a color flag */
980
    /* load color scheme if mdr_cout_init returns a color flag */
980
    SCHEME_TEXT = 0x17;
981
    SCHEME_TEXT = 0x17;
981
    SCHEME_MENU = 0x70;
982
    SCHEME_MENU = 0x70;
982
    SCHEME_MENU_CUR = 0x6f;
983
    SCHEME_MENU_CUR = 0x6f;
983
    SCHEME_MENU_SEL = 0x66;
984
    SCHEME_MENU_SEL = 0x66;
984
    SCHEME_STBAR1 = 0x70;
985
    SCHEME_STBAR1 = 0x70;
985
    SCHEME_STBAR2 = 0x78;
986
    SCHEME_STBAR2 = 0x78;
986
    SCHEME_STBAR3 = 0x3f;
987
    SCHEME_STBAR3 = 0x3f;
987
    SCHEME_SCROLL = 0x70;
988
    SCHEME_SCROLL = 0x70;
988
    SCHEME_MSG = 0x6f;
989
    SCHEME_MSG = 0x6f;
989
    SCHEME_ERR = 0x4f;
990
    SCHEME_ERR = 0x4f;
990
  }
991
  }
991
  screenlastrow = screenh - 1;
992
  screenlastrow = screenh - 1;
992
  screenlastcol = screenw - 1;
993
  screenlastcol = screenw - 1;
993
 
994
 
994
  /* instruct DOS to stop detecting CTRL+C because user needs it for
995
  /* instruct DOS to stop detecting CTRL+C because user needs it for
995
   * copy/paste operations. also remember the original status of the BREAK
996
   * copy/paste operations. also remember the original status of the BREAK
996
   * flag so I can restore it as it was before quitting. */
997
   * flag so I can restore it as it was before quitting. */
997
  original_breakflag = mdr_dos_ctrlc_disable();
998
  original_breakflag = mdr_dos_ctrlc_disable();
998
 
999
 
999
  for (;;) {
1000
  for (;;) {
1000
    int k;
1001
    int k;
1001
 
1002
 
1002
    /* add an extra empty line if cursor is on last line and this line is not empty */
1003
    /* add an extra empty line if cursor is on last line and this line is not empty */
1003
    if ((db->cursor->next == NULL) && (db->cursor->len != 0)) {
1004
    if ((db->cursor->next == NULL) && (db->cursor->len != 0)) {
1004
      if (line_add(db, NULL, 0) == 0) {
1005
      if (line_add(db, NULL, 0) == 0) {
1005
        db->cursor = db->cursor->prev; /* line_add() changes the cursor pointer */
1006
        db->cursor = db->cursor->prev; /* line_add() changes the cursor pointer */
1006
        db->curline -= 1;
1007
        db->curline -= 1;
1007
      }
1008
      }
1008
    }
1009
    }
1009
 
1010
 
1010
    check_cursor_not_after_eol(db);
1011
    check_cursor_not_after_eol(db);
1011
    mdr_cout_locate(db->cursorposy, db->cursorposx);
1012
    mdr_cout_locate(db->cursorposy, db->cursorposx);
1012
 
1013
 
1013
    ui_refresh(db);
1014
    ui_refresh(db);
1014
 
1015
 
1015
    if ((uidirty.statusbar != 0) || (db->modflagprev != db->modflag) || (db->curline_prev != db->curline)) {
1016
    if ((uidirty.statusbar != 0) || (db->modflagprev != db->modflag) || (db->curline_prev != db->curline)) {
1016
      ui_statusbar(db, curfile);
1017
      ui_statusbar(db, curfile);
1017
      uidirty.statusbar = 0;
1018
      uidirty.statusbar = 0;
1018
      db->modflagprev = db->modflag;
1019
      db->modflagprev = db->modflag;
1019
      db->curline_prev = db->curline;
1020
      db->curline_prev = db->curline;
1020
    }
1021
    }
1021
#ifdef DBG_LINENUM
1022
#ifdef DBG_LINENUM
1022
      {
1023
      {
1023
        char ddd[10];
1024
        char ddd[10];
1024
        db->curline += 1;
1025
        db->curline += 1;
1025
        ddd[0] = '0' + db->curline / 100;
1026
        ddd[0] = '0' + db->curline / 100;
1026
        ddd[1] = '0' + (db->curline % 100) / 10;
1027
        ddd[1] = '0' + (db->curline % 100) / 10;
1027
        ddd[2] = '0' + (db->curline % 10);
1028
        ddd[2] = '0' + (db->curline % 10);
1028
        db->curline -= 1;
1029
        db->curline -= 1;
1029
        ddd[3] = '/';
1030
        ddd[3] = '/';
1030
        ddd[4] = '0' + db->totlines / 100;
1031
        ddd[4] = '0' + db->totlines / 100;
1031
        ddd[5] = '0' + (db->totlines % 100) / 10;
1032
        ddd[5] = '0' + (db->totlines % 100) / 10;
1032
        ddd[6] = '0' + (db->totlines % 10);
1033
        ddd[6] = '0' + (db->totlines % 10);
1033
        ddd[7] = 0;
1034
        ddd[7] = 0;
1034
        mdr_cout_str(screenh - 1, 40, ddd, SCHEME_STBAR1, sizeof(ddd));
1035
        mdr_cout_str(screenh - 1, 40, ddd, SCHEME_STBAR1, sizeof(ddd));
1035
      }
1036
      }
1036
#endif
1037
#endif
1037
 
1038
 
1038
    k = mdr_dos_getkey2();
1039
    k = mdr_dos_getkey2();
1039
 
1040
 
1040
    if (k == 0x150) { /* down */
1041
    if (k == 0x150) { /* down */
1041
      cursor_down(db);
1042
      cursor_down(db);
1042
 
1043
 
1043
    } else if (k == 0x148) { /* up */
1044
    } else if (k == 0x148) { /* up */
1044
      cursor_up(db);
1045
      cursor_up(db);
1045
 
1046
 
1046
    } else if (k == 0x14D) { /* right */
1047
    } else if (k == 0x14D) { /* right */
1047
      cursor_right(db);
1048
      cursor_right(db);
1048
 
1049
 
1049
    } else if (k == 0x14B) { /* left */
1050
    } else if (k == 0x14B) { /* left */
1050
      cursor_left(db);
1051
      cursor_left(db);
1051
 
1052
 
1052
    } else if (k == 0x149) { /* pgup */
1053
    } else if (k == 0x149) { /* pgup */
1053
      unsigned char dist = db->cursorposy + screenh - 1;
1054
      unsigned char dist = db->cursorposy + screenh - 1;
1054
      while ((dist != 0) && (db->cursor->prev != NULL)) {
1055
      while ((dist != 0) && (db->cursor->prev != NULL)) {
1055
        db->cursor = db->cursor->prev;
1056
        db->cursor = db->cursor->prev;
1056
        dist--;
1057
        dist--;
1057
      }
1058
      }
1058
      if (dist != 0) {
1059
      if (dist != 0) {
1059
        db->cursorposy = 0;
1060
        db->cursorposy = 0;
1060
        db->cursorposx = 0;
1061
        db->cursorposx = 0;
1061
      } else {
1062
      } else {
1062
        dist = db->cursorposy;
1063
        dist = db->cursorposy;
1063
        while ((dist--) && (db->cursor->next)) db->cursor = db->cursor->next;
1064
        while ((dist--) && (db->cursor->next)) db->cursor = db->cursor->next;
1064
      }
1065
      }
1065
      uidirty.from = 0;
1066
      uidirty.from = 0;
1066
      uidirty.to = 0xff;
1067
      uidirty.to = 0xff;
1067
      recompute_curline(db);
1068
      recompute_curline(db);
1068
 
1069
 
1069
    } else if (k == 0x151) { /* pgdown */
1070
    } else if (k == 0x151) { /* pgdown */
1070
      unsigned char dist = screenh + screenh - db->cursorposy - 3;
1071
      unsigned char dist = screenh + screenh - db->cursorposy - 3;
1071
      while ((dist != 0) && (db->cursor->next != NULL)) {
1072
      while ((dist != 0) && (db->cursor->next != NULL)) {
1072
        db->cursor = db->cursor->next;
1073
        db->cursor = db->cursor->next;
1073
        dist--;
1074
        dist--;
1074
      }
1075
      }
1075
      if (dist != 0) {
1076
      if (dist != 0) {
1076
        db->cursorposy = screenh - 2;
1077
        db->cursorposy = screenh - 2;
1077
        if (db->totlines <= db->cursorposy) db->cursorposy = db->totlines - 1;
1078
        if (db->totlines <= db->cursorposy) db->cursorposy = db->totlines - 1;
1078
        db->cursorposx = 0;
1079
        db->cursorposx = 0;
1079
      } else {
1080
      } else {
1080
        dist = screenh - 2 - db->cursorposy;
1081
        dist = screenh - 2 - db->cursorposy;
1081
        while ((dist--) && (db->cursor->prev)) db->cursor = db->cursor->prev;
1082
        while ((dist--) && (db->cursor->prev)) db->cursor = db->cursor->prev;
1082
      }
1083
      }
1083
      uidirty.from = 0;
1084
      uidirty.from = 0;
1084
      uidirty.to = 0xff;
1085
      uidirty.to = 0xff;
1085
      recompute_curline(db);
1086
      recompute_curline(db);
1086
 
1087
 
1087
    } else if (k == 0x147) { /* home */
1088
    } else if (k == 0x147) { /* home */
1088
       cursor_home(db);
1089
       cursor_home(db);
1089
 
1090
 
1090
    } else if (k == 0x14F) { /* end */
1091
    } else if (k == 0x14F) { /* end */
1091
       cursor_eol(db);
1092
       cursor_eol(db);
1092
 
1093
 
1093
    } else if (k == 0x1B) { /* ESC */
1094
    } else if (k == 0x1B) { /* ESC */
1094
      int quitnow = 0;
1095
      int quitnow = 0;
1095
      char fname[64];
1096
      char fname[64];
1096
      int saveflag = 0;
1097
      int saveflag = 0;
1097
      enum MENU_ACTION ui_action;
1098
      enum MENU_ACTION ui_action;
1098
 
1099
 
1099
      /* collect the exact menu action and clear the screen */
1100
      /* collect the exact menu action and clear the screen */
1100
      ui_action = ui_menu();
1101
      ui_action = ui_menu();
1101
      ui_refresh(db);
1102
      ui_refresh(db);
1102
 
1103
 
1103
      switch (ui_action) {
1104
      switch (ui_action) {
1104
 
1105
 
1105
        case MENU_NONE:
1106
        case MENU_NONE:
1106
          break;
1107
          break;
1107
 
1108
 
1108
        case MENU_OPEN:
1109
        case MENU_OPEN:
1109
          /* display a warning if unsaved changes are pending */
1110
          /* display a warning if unsaved changes are pending */
1110
          if (db->modflag != 0) ui_msg(svarlang_str(0,4), svarlang_str(0,8), SCHEME_MSG);
1111
          if (db->modflag != 0) ui_msg(svarlang_str(0,4), svarlang_str(0,8), SCHEME_MSG);
1111
 
1112
 
1112
          /* ask for filename */
1113
          /* ask for filename */
1113
          ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
1114
          ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
1114
          if (fname[0] != 0) {
1115
          if (fname[0] != 0) {
1115
            unsigned char err;
1116
            unsigned char err;
1116
            err = loadfile(db, fname);
1117
            err = loadfile(db, fname);
1117
            if (err != 0) {
1118
            if (err != 0) {
1118
              if (err == LOADFILE_FILENOTFOUND) {
1119
              if (err == LOADFILE_FILENOTFOUND) {
1119
                ui_msg(svarlang_str(0,11), NULL, SCHEME_ERR); /* file not found */
1120
                ui_msg(svarlang_str(0,11), NULL, SCHEME_ERR); /* file not found */
1120
              } else {
1121
              } else {
1121
                ui_msg(svarlang_str(0,10), NULL, SCHEME_ERR);  /* ERROR */
1122
                ui_msg(svarlang_str(0,10), NULL, SCHEME_ERR);  /* ERROR */
1122
              }
1123
              }
1123
              mdr_bios_tickswait(44); /* 3s */
1124
              mdr_bios_tickswait(44); /* 3s */
1124
              loadfile(db, NULL);
1125
              loadfile(db, NULL);
1125
            }
1126
            }
1126
          }
1127
          }
1127
          uidirty.from = 0;
1128
          uidirty.from = 0;
1128
          uidirty.to = 0xff;
1129
          uidirty.to = 0xff;
1129
          uidirty.statusbar = 1;
1130
          uidirty.statusbar = 1;
1130
          break;
1131
          break;
1131
 
1132
 
1132
        case MENU_SAVEAS:
1133
        case MENU_SAVEAS:
1133
          saveflag = 1;
1134
          saveflag = 1;
1134
          /* FALLTHRU */
1135
          /* FALLTHRU */
1135
        case MENU_SAVE:
1136
        case MENU_SAVE:
1136
          if ((saveflag != 0) || (db->fname[0] == 0)) { /* save as... */
1137
          if ((saveflag != 0) || (db->fname[0] == 0)) { /* save as... */
1137
            ui_getstring(svarlang_str(0,6), fname, sizeof(fname));
1138
            ui_getstring(svarlang_str(0,6), fname, sizeof(fname));
1138
            if (*fname == 0) break;
1139
            if (*fname == 0) break;
1139
            saveflag = savefile(db, fname);
1140
            saveflag = savefile(db, fname);
1140
          } else {
1141
          } else {
1141
            saveflag = savefile(db, NULL);
1142
            saveflag = savefile(db, NULL);
1142
          }
1143
          }
1143
 
1144
 
1144
          mdr_cout_cursor_hide();
1145
          mdr_cout_cursor_hide();
1145
 
1146
 
1146
          if (saveflag == 0) {
1147
          if (saveflag == 0) {
1147
            ui_msg(svarlang_str(0, 2), NULL, SCHEME_MSG);
1148
            ui_msg(svarlang_str(0, 2), NULL, SCHEME_MSG);
1148
            mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
1149
            mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
1149
          } else {
1150
          } else {
1150
            ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR);
1151
            ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR);
1151
            mdr_bios_tickswait(36); /* 2s */
1152
            mdr_bios_tickswait(36); /* 2s */
1152
          }
1153
          }
1153
          mdr_cout_cursor_show();
1154
          mdr_cout_cursor_show();
1154
          break;
1155
          break;
1155
 
1156
 
1156
        case MENU_CLOSE:
1157
        case MENU_CLOSE:
1157
          if (ui_confirm_if_unsaved(db) == 0) {
1158
          if (ui_confirm_if_unsaved(db) == 0) {
1158
            loadfile(db, NULL);
1159
            loadfile(db, NULL);
1159
          }
1160
          }
1160
          uidirty.from = 0;
1161
          uidirty.from = 0;
1161
          uidirty.to = 0xff;
1162
          uidirty.to = 0xff;
1162
          uidirty.statusbar = 1;
1163
          uidirty.statusbar = 1;
1163
          break;
1164
          break;
1164
 
1165
 
1165
        case MENU_CHGEOL:
1166
        case MENU_CHGEOL:
1166
          db->modflag = 1;
1167
          db->modflag = 1;
1167
          db->lfonly ^= 1;
1168
          db->lfonly ^= 1;
1168
          break;
1169
          break;
1169
 
1170
 
1170
        case MENU_QUIT:
1171
        case MENU_QUIT:
1171
          quitnow = 1;
1172
          quitnow = 1;
1172
          for (curfile = 0; curfile < 10; curfile++) {
1173
          for (curfile = 0; curfile < 10; curfile++) {
1173
            if (dbarr[curfile].modflag) {
1174
            if (dbarr[curfile].modflag) {
1174
              db = select_slot(dbarr, curfile);
1175
              db = select_slot(dbarr, curfile);
1175
              if (ui_confirm_if_unsaved(db) != 0) {
1176
              if (ui_confirm_if_unsaved(db) != 0) {
1176
                quitnow = 0;
1177
                quitnow = 0;
1177
                break;
1178
                break;
1178
              }
1179
              }
1179
            }
1180
            }
1180
          }
1181
          }
1181
          break;
1182
          break;
1182
      }
1183
      }
1183
 
1184
 
1184
      if (quitnow) break;
1185
      if (quitnow) break;
1185
 
1186
 
1186
    } else if (k == 0x0D) { /* ENTER */
1187
    } else if (k == 0x0D) { /* ENTER */
1187
      unsigned short off = db->xoffset + db->cursorposx;
1188
      unsigned short off = db->xoffset + db->cursorposx;
1188
      /* add a new line */
1189
      /* add a new line */
1189
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
1190
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
1190
        db->modflag = 1;
1191
        db->modflag = 1;
1191
        db->cursor = db->cursor->prev; /* back to original line */
1192
        db->cursor = db->cursor->prev; /* back to original line */
1192
        db->curline -= 1;
1193
        db->curline -= 1;
1193
        /* trim the line above */
1194
        /* trim the line above */
1194
        db->cursor->len = off;
1195
        db->cursor->len = off;
1195
        /* move cursor to the (new) line below */
1196
        /* move cursor to the (new) line below */
1196
        uidirty.from = db->cursorposy;
1197
        uidirty.from = db->cursorposy;
1197
        uidirty.to = 0xff;
1198
        uidirty.to = 0xff;
1198
        cursor_down(db);
1199
        cursor_down(db);
1199
        cursor_home(db);
1200
        cursor_home(db);
1200
      } else {
1201
      } else {
1201
        /* ERROR: OUT OF MEMORY */
1202
        /* ERROR: OUT OF MEMORY */
1202
      }
1203
      }
1203
 
1204
 
1204
    } else if (k == 0x153) {  /* DEL */
1205
    } else if (k == 0x153) {  /* DEL */
1205
      del(db);
1206
      del(db);
1206
 
1207
 
1207
    } else if (k == 0x008) { /* BKSPC */
1208
    } else if (k == 0x008) { /* BKSPC */
1208
      bkspc(db);
1209
      bkspc(db);
1209
 
1210
 
1210
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
1211
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
1211
      char c = k;
1212
      char c = k;
1212
      insert_in_line(db, &c, 1);
1213
      insert_in_line(db, &c, 1);
1213
 
1214
 
1214
    } else if (k == 0x009) { /* TAB */
1215
    } else if (k == 0x009) { /* TAB */
1215
      if (glob_tablessmode == 0) {
1216
      if (glob_tablessmode == 0) {
1216
        insert_in_line(db, "        ", 8);
1217
        insert_in_line(db, "        ", 8);
1217
      } else {
1218
      } else {
1218
        insert_in_line(db, "\t", 1);
1219
        insert_in_line(db, "\t", 1);
1219
      }
1220
      }
1220
 
1221
 
1221
    } else if ((k >= 0x13b) && (k <= 0x144)) { /* F1..F10 */
1222
    } else if ((k >= 0x13b) && (k <= 0x144)) { /* F1..F10 */
1222
      curfile = k - 0x13b;
1223
      curfile = k - 0x13b;
1223
      db = select_slot(dbarr, curfile);
1224
      db = select_slot(dbarr, curfile);
1224
 
1225
 
1225
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
1226
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
1226
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
1227
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
1227
      for (;;) {
1228
      for (;;) {
1228
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
1229
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
1229
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
1230
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
1230
        cursor_right(db);
1231
        cursor_right(db);
1231
      }
1232
      }
1232
      /* now skip to next non-space or end of file */
1233
      /* now skip to next non-space or end of file */
1233
      for (;;) {
1234
      for (;;) {
1234
        cursor_right(db);
1235
        cursor_right(db);
1235
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1236
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1236
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
1237
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
1237
      }
1238
      }
1238
 
1239
 
1239
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
1240
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
1240
      cursor_left(db);
1241
      cursor_left(db);
1241
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
1242
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
1242
      for (;;) {
1243
      for (;;) {
1243
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
1244
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
1244
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1245
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1245
        cursor_left(db);
1246
        cursor_left(db);
1246
      }
1247
      }
1247
      /* now skip to next space or start of file */
1248
      /* now skip to next space or start of file */
1248
      for (;;) {
1249
      for (;;) {
1249
        cursor_left(db);
1250
        cursor_left(db);
1250
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
1251
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
1251
          cursor_right(db);
1252
          cursor_right(db);
1252
          break;
1253
          break;
1253
        }
1254
        }
1254
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
1255
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
1255
      }
1256
      }
1256
 
1257
 
1257
    } else if ((k == 0x003) || (k == 0x018)) { /* CTRL+C or CTRL+X */
1258
    } else if ((k == 0x003) || (k == 0x018)) { /* CTRL+C or CTRL+X */
1258
      /* free clipboard if anything in it */
1259
      /* free clipboard if anything in it */
1259
      if (clipboard != NULL) line_free(clipboard);
1260
      if (clipboard != NULL) line_free(clipboard);
1260
 
1261
 
1261
      /* copy cursor line to clipboard */
1262
      /* copy cursor line to clipboard */
1262
      clipboard = line_calloc(db->cursor->len);
1263
      clipboard = line_calloc(db->cursor->len);
1263
      if (clipboard == NULL) {
1264
      if (clipboard == NULL) {
1264
        ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR); /* ERROR */
1265
        ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR); /* ERROR */
1265
        mdr_bios_tickswait(18); /* 1s */
1266
        mdr_bios_tickswait(18); /* 1s */
1266
      } else {
1267
      } else {
1267
        mdr_cout_char_rep(db->cursorposy, 0, ' ', ((SCHEME_TEXT >> 4) | (SCHEME_TEXT << 4)) & 0xff, screenlastcol);
1268
        mdr_cout_char_rep(db->cursorposy, 0, ' ', ((SCHEME_TEXT >> 4) | (SCHEME_TEXT << 4)) & 0xff, screenlastcol);
1268
        uidirty.from = db->cursorposy;
1269
        uidirty.from = db->cursorposy;
1269
        uidirty.to = db->cursorposy;
1270
        uidirty.to = db->cursorposy;
1270
        if (db->cursor->len != 0) {
1271
        if (db->cursor->len != 0) {
1271
          _fmemmove(clipboard->payload, db->cursor->payload, db->cursor->len);
1272
          _fmemmove(clipboard->payload, db->cursor->payload, db->cursor->len);
1272
          clipboard->len = db->cursor->len;
1273
          clipboard->len = db->cursor->len;
1273
        }
1274
        }
1274
        mdr_bios_tickswait(2); /* ca 100ms */
1275
        mdr_bios_tickswait(2); /* ca 100ms */
1275
 
1276
 
1276
        /* if this is about cutting the line (CTRL+X) then delete cur line */
1277
        /* if this is about cutting the line (CTRL+X) then delete cur line */
1277
        if ((k == 0x018) && ((db->cursor->next != NULL) || (db->cursor->prev != NULL))) {
1278
        if ((k == 0x018) && ((db->cursor->next != NULL) || (db->cursor->prev != NULL))) {
1278
          if (db->cursor->next) db->cursor->next->prev = db->cursor->prev;
1279
          if (db->cursor->next) db->cursor->next->prev = db->cursor->prev;
1279
          if (db->cursor->prev) db->cursor->prev->next = db->cursor->next;
1280
          if (db->cursor->prev) db->cursor->prev->next = db->cursor->next;
1280
          clipboard->prev = db->cursor;
1281
          clipboard->prev = db->cursor;
1281
          if (db->cursor->next) {
1282
          if (db->cursor->next) {
1282
            db->cursor = db->cursor->next;
1283
            db->cursor = db->cursor->next;
1283
          } else {
1284
          } else {
1284
            cursor_up(db);
1285
            cursor_up(db);
1285
          }
1286
          }
1286
          line_free(clipboard->prev);
1287
          line_free(clipboard->prev);
1287
          db->totlines -= 1;
1288
          db->totlines -= 1;
1288
          uidirty.from = 0;
1289
          uidirty.from = 0;
1289
          uidirty.to = 0xff;
1290
          uidirty.to = 0xff;
1290
          recompute_curline(db);
1291
          recompute_curline(db);
1291
        }
1292
        }
1292
      }
1293
      }
1293
 
1294
 
1294
    } else if ((k == 0x016) && (clipboard != NULL)) { /* CTRL+V */
1295
    } else if ((k == 0x016) && (clipboard != NULL)) { /* CTRL+V */
1295
      if (line_add(db, clipboard->payload, clipboard->len) != 0) {
1296
      if (line_add(db, clipboard->payload, clipboard->len) != 0) {
1296
        ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR); /* ERROR */
1297
        ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR); /* ERROR */
1297
        mdr_bios_tickswait(18); /* 1s */
1298
        mdr_bios_tickswait(18); /* 1s */
1298
      } else {
1299
      } else {
1299
        /* rewire the linked list so the new line is on top of the previous one */
1300
        /* rewire the linked list so the new line is on top of the previous one */
1300
        clipboard->prev = db->cursor->prev;
1301
        clipboard->prev = db->cursor->prev;
1301
        /* remove prev node from list */
1302
        /* remove prev node from list */
1302
        db->cursor->prev = db->cursor->prev->prev;
1303
        db->cursor->prev = db->cursor->prev->prev;
1303
        if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor;
1304
        if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor;
1304
        /* insert the node after cursor now */
1305
        /* insert the node after cursor now */
1305
        clipboard->prev->next = db->cursor->next;
1306
        clipboard->prev->next = db->cursor->next;
1306
        if (db->cursor->next != NULL) db->cursor->next->prev = clipboard->prev;
1307
        if (db->cursor->next != NULL) db->cursor->next->prev = clipboard->prev;
1307
        clipboard->prev->prev = db->cursor;
1308
        clipboard->prev->prev = db->cursor;
1308
        db->cursor->next = clipboard->prev;
1309
        db->cursor->next = clipboard->prev;
1309
        cursor_down(db);
1310
        cursor_down(db);
1310
      }
1311
      }
1311
      uidirty.from = 0;
1312
      uidirty.from = 0;
1312
      uidirty.to = 0xff;
1313
      uidirty.to = 0xff;
1313
      recompute_curline(db);
1314
      recompute_curline(db);
1314
 
1315
 
1315
#ifdef DBG_UNHKEYS
1316
#ifdef DBG_UNHKEYS
1316
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
1317
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
1317
      char buff[4];
1318
      char buff[4];
1318
      const char *HEX = "0123456789ABCDEF";
1319
      const char *HEX = "0123456789ABCDEF";
1319
      buff[0] = HEX[(k >> 8) & 15];
1320
      buff[0] = HEX[(k >> 8) & 15];
1320
      buff[1] = HEX[(k >> 4) & 15];
1321
      buff[1] = HEX[(k >> 4) & 15];
1321
      buff[2] = HEX[k & 15];
1322
      buff[2] = HEX[k & 15];
1322
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", SCHEME_STBAR1, 17);
1323
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", SCHEME_STBAR1, 17);
1323
      mdr_cout_str(screenh - 1, 17, buff, SCHEME_STBAR1, 3);
1324
      mdr_cout_str(screenh - 1, 17, buff, SCHEME_STBAR1, 3);
1324
      mdr_dos_getkey2();
1325
      mdr_dos_getkey2();
1325
      break;
1326
      break;
1326
#endif
1327
#endif
1327
    }
1328
    }
1328
  }
1329
  }
1329
 
1330
 
1330
  mdr_cout_close();
1331
  mdr_cout_close();
1331
 
1332
 
1332
  /* restore the DOS BREAK flag if it was originally set */
1333
  /* restore the DOS BREAK flag if it was originally set */
1333
  if (original_breakflag != 0) mdr_dos_ctrlc_enable();
1334
  if (original_breakflag != 0) mdr_dos_ctrlc_enable();
1334
 
1335
 
1335
  /* no need to free memory, DOS will do it for me */
1336
  /* no need to free memory, DOS will do it for me */
1336
 
1337
 
1337
  return;
1338
  return;
1338
}
1339
}
1339
 
1340