Subversion Repositories SvarDOS

Rev

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

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