Subversion Repositories SvarDOS

Rev

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

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