Subversion Repositories SvarDOS

Rev

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

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