Subversion Repositories SvarDOS

Rev

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

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