Subversion Repositories SvarDOS

Rev

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

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