Subversion Repositories SvarDOS

Rev

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

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