Subversion Repositories SvarDOS

Rev

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

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