Subversion Repositories SvarDOS

Rev

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

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