Subversion Repositories SvarDOS

Rev

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

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