Subversion Repositories SvarDOS

Rev

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

Rev 1394 Rev 1395
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
        err = 11;
704
        err = 11;
705
      } else { /* general error */
705
      } else { /* general error */
706
        err = 10;
706
        err = 10;
707
      }
707
      }
708
      mdr_coutraw_puts(svarlang_str(0,err));
708
      mdr_coutraw_puts(svarlang_str(0,err));
709
      return(-1);
709
      return(-1);
710
    }
710
    }
711
    count++;
711
    count++;
712
  }
712
  }
713
 
713
 
714
  return(0);
714
  return(0);
715
}
715
}
716
 
716
 
717
 
717
 
718
static int savefile(const struct file *db, const char *newfname) {
718
static int savefile(const struct file *db, const char *newfname) {
719
  int fd;
719
  int fd;
720
  const struct line far *l;
720
  const struct line far *l;
721
  unsigned bytes;
721
  unsigned bytes;
722
  unsigned char eollen;
722
  unsigned char eollen;
723
  unsigned char eolbuf[2];
723
  unsigned char eolbuf[2];
724
  int errflag = 0;
724
  int errflag = 0;
725
 
725
 
726
  /* either create a new file if newfname provided, or... */
726
  /* either create a new file if newfname provided, or... */
727
  if (newfname) {
727
  if (newfname) {
728
    if (_dos_creatnew(newfname, _A_NORMAL, &fd) != 0) return(-1);
728
    if (_dos_creatnew(newfname, _A_NORMAL, &fd) != 0) return(-1);
729
  } else { /* ...open db->fname */
729
  } else { /* ...open db->fname */
730
    if (_dos_open(db->fname, O_WRONLY, &fd) != 0) return(-1);
730
    if (_dos_open(db->fname, O_WRONLY, &fd) != 0) return(-1);
731
  }
731
  }
732
 
732
 
733
  l = db->cursor;
733
  l = db->cursor;
734
  while (l->prev) l = l->prev;
734
  while (l->prev) l = l->prev;
735
 
735
 
736
  /* preset line terminators */
736
  /* preset line terminators */
737
  if (db->lfonly) {
737
  if (db->lfonly) {
738
    eolbuf[0] = '\n';
738
    eolbuf[0] = '\n';
739
    eollen = 1;
739
    eollen = 1;
740
  } else {
740
  } else {
741
    eolbuf[0] = '\r';
741
    eolbuf[0] = '\r';
742
    eolbuf[1] = '\n';
742
    eolbuf[1] = '\n';
743
    eollen = 2;
743
    eollen = 2;
744
  }
744
  }
745
 
745
 
746
  while (l) {
746
  while (l) {
747
    /* 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 */
748
    if (l->len != 0) {
748
    if (l->len != 0) {
749
      errflag |= _dos_write(fd, l->payload, l->len, &bytes);
749
      errflag |= _dos_write(fd, l->payload, l->len, &bytes);
750
    } else if (l->next == NULL) {
750
    } else if (l->next == NULL) {
751
      break;
751
      break;
752
    }
752
    }
753
    errflag |= _dos_write(fd, eolbuf, eollen, &bytes);
753
    errflag |= _dos_write(fd, eolbuf, eollen, &bytes);
754
    l = l->next;
754
    l = l->next;
755
  }
755
  }
756
 
756
 
757
  errflag |= _dos_close(fd);
757
  errflag |= _dos_close(fd);
758
 
758
 
759
  return(errflag);
759
  return(errflag);
760
}
760
}
761
 
761
 
762
 
762
 
763
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) {
764
  if (curline_resize(db, db->cursor->len + len) == 0) {
764
  if (curline_resize(db, db->cursor->len + len) == 0) {
765
    unsigned short off = db->xoffset + db->cursorposx;
765
    unsigned short off = db->xoffset + db->cursorposx;
766
    db->modflag = 1;
766
    db->modflag = 1;
767
    _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);
768
    db->cursor->len += len;
768
    db->cursor->len += len;
769
    uidirty.from = db->cursorposy;
769
    uidirty.from = db->cursorposy;
770
    uidirty.to = db->cursorposy;
770
    uidirty.to = db->cursorposy;
771
    while (len--) {
771
    while (len--) {
772
      db->cursor->payload[off++] = *databuf;
772
      db->cursor->payload[off++] = *databuf;
773
      databuf++;
773
      databuf++;
774
      cursor_right(db);
774
      cursor_right(db);
775
    }
775
    }
776
  }
776
  }
777
}
777
}
778
 
778
 
779
 
779
 
780
/* recompute db->curline by counting nodes in linked list */
780
/* recompute db->curline by counting nodes in linked list */
781
static void recompute_curline(struct file *db) {
781
static void recompute_curline(struct file *db) {
782
  const struct line far *l = db->cursor;
782
  const struct line far *l = db->cursor;
783
 
783
 
784
  db->curline = 0;
784
  db->curline = 0;
785
  while (l->prev != NULL) {
785
  while (l->prev != NULL) {
786
    db->curline += 1;
786
    db->curline += 1;
787
    l = l->prev;
787
    l = l->prev;
788
  }
788
  }
789
}
789
}
790
 
790
 
791
 
791
 
792
enum MENU_ACTION {
792
enum MENU_ACTION {
793
  MENU_NONE   = 0,
793
  MENU_NONE   = 0,
794
  MENU_OPEN   = 1,
794
  MENU_OPEN   = 1,
795
  MENU_SAVE   = 2,
795
  MENU_SAVE   = 2,
796
  MENU_SAVEAS = 3,
796
  MENU_SAVEAS = 3,
797
  MENU_CLOSE  = 4,
797
  MENU_CLOSE  = 4,
798
  MENU_CHGEOL = 5,
798
  MENU_CHGEOL = 5,
799
  MENU_QUIT   = 6,
799
  MENU_QUIT   = 6,
800
};
800
};
801
 
801
 
802
static enum MENU_ACTION ui_menu(void) {
802
static enum MENU_ACTION ui_menu(void) {
803
  unsigned short i, curchoice, attr, x, slen;
803
  unsigned short i, curchoice, attr, x, slen;
804
  unsigned short xorigin, yorigin;
804
  unsigned short xorigin, yorigin;
805
  uidirty.from = 0;
-
 
806
  uidirty.to = 0xff;
-
 
807
  uidirty.statusbar = 1;
-
 
808
 
805
 
809
  /* find out the longest string */
806
  /* find out the longest string */
810
  slen = 0;
807
  slen = 0;
811
  for (i = MENU_OPEN; i <= MENU_QUIT; i++) {
808
  for (i = MENU_OPEN; i <= MENU_QUIT; i++) {
812
    x = strlen(svarlang_str(8, i));
809
    x = strlen(svarlang_str(8, i));
813
    if (x > slen) slen = x;
810
    if (x > slen) slen = x;
814
  }
811
  }
815
 
812
 
816
  /* calculate where to draw the menu on screen */
813
  /* calculate where to draw the menu on screen */
817
  xorigin = (screenw - (slen + 5)) / 2;
814
  xorigin = (screenw - (slen + 5)) / 2;
818
  yorigin = (screenh - (MENU_QUIT - MENU_OPEN + 5)) / 2;
815
  yorigin = (screenh - (MENU_QUIT - MENU_OPEN + 6)) / 2;
-
 
816
 
-
 
817
  /* */
-
 
818
  uidirty.from = yorigin;
-
 
819
  uidirty.to = 0xff;
-
 
820
  uidirty.statusbar = 1;
-
 
821
 
-
 
822
  /* hide the cursor */
-
 
823
  mdr_cout_cursor_hide();
819
 
824
 
820
  curchoice = MENU_OPEN;
825
  curchoice = MENU_OPEN;
821
  for (;;) {
826
  for (;;) {
822
    /* render menu */
827
    /* render menu */
823
    for (i = MENU_NONE; i <= MENU_QUIT + 1; i++) {
828
    for (i = MENU_NONE; i <= MENU_QUIT + 1; i++) {
824
      mdr_cout_char_rep(yorigin + i, xorigin, ' ', SCHEME_MENU, slen+4);
829
      mdr_cout_char_rep(yorigin + i, xorigin, ' ', SCHEME_MENU, slen+4);
825
      if (i == curchoice) {
830
      if (i == curchoice) {
826
        attr = SCHEME_MENU_CUR;
831
        attr = SCHEME_MENU_CUR;
827
        mdr_cout_char(yorigin + i, xorigin + 1, '>', SCHEME_MENU_SEL);
832
        mdr_cout_char(yorigin + i, xorigin + 1, '>', SCHEME_MENU_SEL);
828
      } else {
833
      } else {
829
        attr = SCHEME_MENU;
834
        attr = SCHEME_MENU;
830
      }
835
      }
831
      x = mdr_cout_str(yorigin + i, xorigin + 2, svarlang_str(8, i), attr, slen);
836
      x = mdr_cout_str(yorigin + i, xorigin + 2, svarlang_str(8, i), attr, slen);
832
      if (i == curchoice) {
837
      if (i == curchoice) {
833
        mdr_cout_char_rep(yorigin + i, xorigin + x + 2, ' ', SCHEME_MENU_SEL, slen - x + 1);
838
        mdr_cout_char_rep(yorigin + i, xorigin + x + 2, ' ', SCHEME_MENU_SEL, slen - x + 1);
834
        mdr_cout_locate(yorigin + i, xorigin + x + 2);
-
 
835
      }
839
      }
836
    }
840
    }
837
    /* wait for key */
841
    /* wait for key */
838
    switch (keyb_getkey()) {
842
    switch (keyb_getkey()) {
839
      case '\r': return(curchoice); /* ENTER */
-
 
840
      case 0x150: /* down */
843
      case 0x150: /* down */
841
        if (curchoice == MENU_QUIT) {
844
        if (curchoice == MENU_QUIT) {
842
          curchoice = MENU_OPEN;
845
          curchoice = MENU_OPEN;
843
        } else {
846
        } else {
844
          curchoice++;
847
          curchoice++;
845
        }
848
        }
846
        break;
849
        break;
847
      case 0x148: /* up */
850
      case 0x148: /* up */
848
        if (curchoice == MENU_OPEN) {
851
        if (curchoice == MENU_OPEN) {
849
          curchoice = MENU_QUIT;
852
          curchoice = MENU_QUIT;
850
        } else {
853
        } else {
851
          curchoice--;
854
          curchoice--;
852
        }
855
        }
853
        break;
856
        break;
-
 
857
      default:
854
      default: return(MENU_NONE);
858
        curchoice = MENU_NONE;
-
 
859
        /* FALLTHRU */
-
 
860
      case '\r': /* ENTER */
-
 
861
        mdr_cout_cursor_show();
-
 
862
        return(curchoice);
855
    }
863
    }
856
  }
864
  }
857
}
865
}
858
 
866
 
859
 
867
 
860
static struct file *select_slot(struct file *dbarr, unsigned short curfile) {
868
static struct file *select_slot(struct file *dbarr, unsigned short curfile) {
861
  uidirty.from = 0;
869
  uidirty.from = 0;
862
  uidirty.to = 0xff;
870
  uidirty.to = 0xff;
863
  uidirty.statusbar = 1;
871
  uidirty.statusbar = 1;
864
 
872
 
865
  dbarr = &(dbarr[curfile]);
873
  dbarr = &(dbarr[curfile]);
866
  ui_basic(dbarr, curfile);
874
  ui_basic(dbarr, curfile);
867
  ui_refresh(dbarr);
875
  ui_refresh(dbarr);
868
  return(dbarr);
876
  return(dbarr);
869
}
877
}
870
 
878
 
871
 
879
 
872
/* main returns nothing, ie. sved always exits with a zero exit code
880
/* main returns nothing, ie. sved always exits with a zero exit code
873
 * (this saves 20 bytes of executable footprint) */
881
 * (this saves 20 bytes of executable footprint) */
874
void main(void) {
882
void main(void) {
875
  static struct file dbarr[10];
883
  static struct file dbarr[10];
876
  unsigned short curfile;
884
  unsigned short curfile;
877
  struct file *db = dbarr; /* visible file is the first slot by default */
885
  struct file *db = dbarr; /* visible file is the first slot by default */
878
 
886
 
879
  {
887
  {
880
    unsigned short i = 0;
888
    unsigned short i = 0;
881
    const char far *selfptr;
889
    const char far *selfptr;
882
    char self[128], lang[8];
890
    char self[128], lang[8];
883
    selfptr = mdr_dos_selfexe();
891
    selfptr = mdr_dos_selfexe();
884
    if (selfptr != NULL) {
892
    if (selfptr != NULL) {
885
      do {
893
      do {
886
        self[i] = selfptr[i];
894
        self[i] = selfptr[i];
887
      } while (self[i++] != 0);
895
      } while (self[i++] != 0);
888
      svarlang_autoload_exepath(self, mdr_dos_getenv(lang, "LANG", sizeof(lang)));
896
      svarlang_autoload_exepath(self, mdr_dos_getenv(lang, "LANG", sizeof(lang)));
889
    }
897
    }
890
  }
898
  }
891
 
899
 
892
  /* preload all slots with empty files */
900
  /* preload all slots with empty files */
893
  for (curfile = 9;; curfile--) {
901
  for (curfile = 9;; curfile--) {
894
    loadfile(&(dbarr[curfile]), "");
902
    loadfile(&(dbarr[curfile]), "");
895
    if (curfile == 0) break;
903
    if (curfile == 0) break;
896
  }
904
  }
897
 
905
 
898
  /* parse argv (and load files, if any passed on) */
906
  /* parse argv (and load files, if any passed on) */
899
  if (parseargv(dbarr) != 0) return;
907
  if (parseargv(dbarr) != 0) return;
900
 
908
 
901
  if (mdr_cout_init(&screenw, &screenh)) {
909
  if (mdr_cout_init(&screenw, &screenh)) {
902
    /* load color scheme if mdr_cout_init returns a color flag */
910
    /* load color scheme if mdr_cout_init returns a color flag */
903
    SCHEME_TEXT = 0x17;
911
    SCHEME_TEXT = 0x17;
904
    SCHEME_MENU = 0x70;
912
    SCHEME_MENU = 0x70;
905
    SCHEME_MENU_CUR = 0x6f;
913
    SCHEME_MENU_CUR = 0x6f;
906
    SCHEME_MENU_SEL = 0x66;
914
    SCHEME_MENU_SEL = 0x66;
907
    SCHEME_STBAR1 = 0x70;
915
    SCHEME_STBAR1 = 0x70;
908
    SCHEME_STBAR2 = 0x78;
916
    SCHEME_STBAR2 = 0x78;
909
    SCHEME_STBAR3 = 0x3f;
917
    SCHEME_STBAR3 = 0x3f;
910
    SCHEME_SCROLL = 0x70;
918
    SCHEME_SCROLL = 0x70;
911
    SCHEME_MSG = 0x6f;
919
    SCHEME_MSG = 0x6f;
912
    SCHEME_ERR = 0x4f;
920
    SCHEME_ERR = 0x4f;
913
  }
921
  }
914
 
922
 
915
  for (;;) {
923
  for (;;) {
916
    int k;
924
    int k;
917
 
925
 
918
    /* add an extra empty line if cursor is on last line and this line is not empty */
926
    /* add an extra empty line if cursor is on last line and this line is not empty */
919
    if ((db->cursor->next == NULL) && (db->cursor->len != 0)) {
927
    if ((db->cursor->next == NULL) && (db->cursor->len != 0)) {
920
      if (line_add(db, NULL, 0) == 0) {
928
      if (line_add(db, NULL, 0) == 0) {
921
        db->cursor = db->cursor->prev; /* line_add() changes the cursor pointer */
929
        db->cursor = db->cursor->prev; /* line_add() changes the cursor pointer */
922
      }
930
      }
923
    }
931
    }
924
 
932
 
925
    check_cursor_not_after_eol(db);
933
    check_cursor_not_after_eol(db);
926
    mdr_cout_locate(db->cursorposy, db->cursorposx);
934
    mdr_cout_locate(db->cursorposy, db->cursorposx);
927
 
935
 
928
    ui_refresh(db);
936
    ui_refresh(db);
929
 
937
 
930
    if ((uidirty.statusbar != 0) || (db->modflagprev != db->modflag)) {
938
    if ((uidirty.statusbar != 0) || (db->modflagprev != db->modflag)) {
931
      ui_basic(db, curfile);
939
      ui_basic(db, curfile);
932
      uidirty.statusbar = 0;
940
      uidirty.statusbar = 0;
933
      db->modflagprev = db->modflag;
941
      db->modflagprev = db->modflag;
934
    }
942
    }
935
#ifdef DBG_LINENUM
943
#ifdef DBG_LINENUM
936
      {
944
      {
937
        char ddd[10];
945
        char ddd[10];
938
        db->curline += 1;
946
        db->curline += 1;
939
        ddd[0] = '0' + db->curline / 100;
947
        ddd[0] = '0' + db->curline / 100;
940
        ddd[1] = '0' + (db->curline % 100) / 10;
948
        ddd[1] = '0' + (db->curline % 100) / 10;
941
        ddd[2] = '0' + (db->curline % 10);
949
        ddd[2] = '0' + (db->curline % 10);
942
        db->curline -= 1;
950
        db->curline -= 1;
943
        ddd[3] = '/';
951
        ddd[3] = '/';
944
        ddd[4] = '0' + db->totlines / 100;
952
        ddd[4] = '0' + db->totlines / 100;
945
        ddd[5] = '0' + (db->totlines % 100) / 10;
953
        ddd[5] = '0' + (db->totlines % 100) / 10;
946
        ddd[6] = '0' + (db->totlines % 10);
954
        ddd[6] = '0' + (db->totlines % 10);
947
        ddd[7] = 0;
955
        ddd[7] = 0;
948
        mdr_cout_str(screenh - 1, 40, ddd, SCHEME_STBAR1, sizeof(ddd));
956
        mdr_cout_str(screenh - 1, 40, ddd, SCHEME_STBAR1, sizeof(ddd));
949
      }
957
      }
950
#endif
958
#endif
951
 
959
 
952
    k = keyb_getkey();
960
    k = keyb_getkey();
953
 
961
 
954
    if (k == 0x150) { /* down */
962
    if (k == 0x150) { /* down */
955
      cursor_down(db);
963
      cursor_down(db);
956
 
964
 
957
    } else if (k == 0x148) { /* up */
965
    } else if (k == 0x148) { /* up */
958
      cursor_up(db);
966
      cursor_up(db);
959
 
967
 
960
    } else if (k == 0x14D) { /* right */
968
    } else if (k == 0x14D) { /* right */
961
      cursor_right(db);
969
      cursor_right(db);
962
 
970
 
963
    } else if (k == 0x14B) { /* left */
971
    } else if (k == 0x14B) { /* left */
964
      cursor_left(db);
972
      cursor_left(db);
965
 
973
 
966
    } else if (k == 0x149) { /* pgup */
974
    } else if (k == 0x149) { /* pgup */
967
      unsigned char dist = db->cursorposy + screenh - 1;
975
      unsigned char dist = db->cursorposy + screenh - 1;
968
      while ((dist != 0) && (db->cursor->prev != NULL)) {
976
      while ((dist != 0) && (db->cursor->prev != NULL)) {
969
        db->cursor = db->cursor->prev;
977
        db->cursor = db->cursor->prev;
970
        dist--;
978
        dist--;
971
      }
979
      }
972
      if (dist != 0) {
980
      if (dist != 0) {
973
        db->cursorposy = 0;
981
        db->cursorposy = 0;
974
        db->cursorposx = 0;
982
        db->cursorposx = 0;
975
      } else {
983
      } else {
976
        dist = db->cursorposy;
984
        dist = db->cursorposy;
977
        while ((dist--) && (db->cursor->next)) db->cursor = db->cursor->next;
985
        while ((dist--) && (db->cursor->next)) db->cursor = db->cursor->next;
978
      }
986
      }
979
      uidirty.from = 0;
987
      uidirty.from = 0;
980
      uidirty.to = 0xff;
988
      uidirty.to = 0xff;
981
      recompute_curline(db);
989
      recompute_curline(db);
982
 
990
 
983
    } else if (k == 0x151) { /* pgdown */
991
    } else if (k == 0x151) { /* pgdown */
984
      unsigned char dist = screenh + screenh - db->cursorposy - 3;
992
      unsigned char dist = screenh + screenh - db->cursorposy - 3;
985
      while ((dist != 0) && (db->cursor->next != NULL)) {
993
      while ((dist != 0) && (db->cursor->next != NULL)) {
986
        db->cursor = db->cursor->next;
994
        db->cursor = db->cursor->next;
987
        dist--;
995
        dist--;
988
      }
996
      }
989
      if (dist != 0) {
997
      if (dist != 0) {
990
        db->cursorposy = screenh - 2;
998
        db->cursorposy = screenh - 2;
991
        if (db->totlines <= db->cursorposy) db->cursorposy = db->totlines - 1;
999
        if (db->totlines <= db->cursorposy) db->cursorposy = db->totlines - 1;
992
        db->cursorposx = 0;
1000
        db->cursorposx = 0;
993
      } else {
1001
      } else {
994
        dist = screenh - 2 - db->cursorposy;
1002
        dist = screenh - 2 - db->cursorposy;
995
        while ((dist--) && (db->cursor->prev)) db->cursor = db->cursor->prev;
1003
        while ((dist--) && (db->cursor->prev)) db->cursor = db->cursor->prev;
996
      }
1004
      }
997
      uidirty.from = 0;
1005
      uidirty.from = 0;
998
      uidirty.to = 0xff;
1006
      uidirty.to = 0xff;
999
      recompute_curline(db);
1007
      recompute_curline(db);
1000
 
1008
 
1001
    } else if (k == 0x147) { /* home */
1009
    } else if (k == 0x147) { /* home */
1002
       cursor_home(db);
1010
       cursor_home(db);
1003
 
1011
 
1004
    } else if (k == 0x14F) { /* end */
1012
    } else if (k == 0x14F) { /* end */
1005
       cursor_eol(db);
1013
       cursor_eol(db);
1006
 
1014
 
1007
    } else if (k == 0x1B) { /* ESC */
1015
    } else if (k == 0x1B) { /* ESC */
1008
      int quitnow = 0;
1016
      int quitnow = 0;
1009
      char fname[64];
1017
      char fname[64];
1010
      int saveflag = 0;
1018
      int saveflag = 0;
1011
      enum MENU_ACTION ui_action;
1019
      enum MENU_ACTION ui_action;
1012
 
1020
 
1013
      /* collect the exact menu action and clear the screen */
1021
      /* collect the exact menu action and clear the screen */
1014
      ui_action = ui_menu();
1022
      ui_action = ui_menu();
1015
      ui_refresh(db);
1023
      ui_refresh(db);
1016
 
1024
 
1017
      switch (ui_action) {
1025
      switch (ui_action) {
1018
 
1026
 
1019
        case MENU_NONE:
1027
        case MENU_NONE:
1020
          break;
1028
          break;
1021
 
1029
 
1022
        case MENU_OPEN:
1030
        case MENU_OPEN:
1023
          /* display a warning if unsaved changes are pending */
1031
          /* display a warning if unsaved changes are pending */
1024
          if (db->modflag != 0) ui_msg(svarlang_str(0,4), svarlang_str(0,8), SCHEME_MSG);
1032
          if (db->modflag != 0) ui_msg(svarlang_str(0,4), svarlang_str(0,8), SCHEME_MSG);
1025
 
1033
 
1026
          /* ask for filename */
1034
          /* ask for filename */
1027
          ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
1035
          ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
1028
          if (fname[0] != 0) {
1036
          if (fname[0] != 0) {
1029
            int err;
1037
            int err;
1030
            err = loadfile(db, fname);
1038
            err = loadfile(db, fname);
1031
            if (err != 0) {
1039
            if (err != 0) {
1032
              if (err == 1) {
1040
              if (err == 1) {
1033
                ui_msg(svarlang_str(0,11), NULL, SCHEME_ERR); /* file not found */
1041
                ui_msg(svarlang_str(0,11), NULL, SCHEME_ERR); /* file not found */
1034
              } else {
1042
              } else {
1035
                ui_msg(svarlang_str(0,10), NULL, SCHEME_ERR);  /* ERROR */
1043
                ui_msg(svarlang_str(0,10), NULL, SCHEME_ERR);  /* ERROR */
1036
              }
1044
              }
1037
              mdr_bios_tickswait(44); /* 3s */
1045
              mdr_bios_tickswait(44); /* 3s */
1038
              loadfile(db, "");
1046
              loadfile(db, "");
1039
            }
1047
            }
1040
          }
1048
          }
1041
          uidirty.from = 0;
1049
          uidirty.from = 0;
1042
          uidirty.to = 0xff;
1050
          uidirty.to = 0xff;
1043
          uidirty.statusbar = 1;
1051
          uidirty.statusbar = 1;
1044
          break;
1052
          break;
1045
 
1053
 
1046
        case MENU_SAVEAS:
1054
        case MENU_SAVEAS:
1047
          saveflag = 1;
1055
          saveflag = 1;
1048
          /* FALLTHRU */
1056
          /* FALLTHRU */
1049
        case MENU_SAVE:
1057
        case MENU_SAVE:
1050
          if ((saveflag != 0) || (db->fname[0] == 0)) { /* save as... */
1058
          if ((saveflag != 0) || (db->fname[0] == 0)) { /* save as... */
1051
            ui_getstring(svarlang_str(0,6), fname, sizeof(fname));
1059
            ui_getstring(svarlang_str(0,6), fname, sizeof(fname));
1052
            if (*fname == 0) break;
1060
            if (*fname == 0) break;
1053
            saveflag = savefile(db, fname);
1061
            saveflag = savefile(db, fname);
1054
            if (saveflag == 0) memcpy(db->fname, fname, sizeof(fname));
1062
            if (saveflag == 0) memcpy(db->fname, fname, sizeof(fname));
1055
          } else {
1063
          } else {
1056
            saveflag = savefile(db, NULL);
1064
            saveflag = savefile(db, NULL);
1057
          }
1065
          }
1058
 
1066
 
-
 
1067
          mdr_cout_cursor_hide();
-
 
1068
 
1059
          if (saveflag == 0) {
1069
          if (saveflag == 0) {
1060
            db->modflag = 0;
1070
            db->modflag = 0;
1061
            ui_msg(svarlang_str(0, 2), NULL, SCHEME_MSG);
1071
            ui_msg(svarlang_str(0, 2), NULL, SCHEME_MSG);
1062
            mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
1072
            mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
1063
          } else {
1073
          } else {
1064
            ui_msg(svarlang_str(0, 3), NULL, SCHEME_ERR);
1074
            ui_msg(svarlang_str(0, 3), NULL, SCHEME_ERR);
1065
            mdr_bios_tickswait(36); /* 2s */
1075
            mdr_bios_tickswait(36); /* 2s */
1066
          }
1076
          }
-
 
1077
          mdr_cout_cursor_show();
1067
          break;
1078
          break;
1068
 
1079
 
1069
        case MENU_CLOSE:
1080
        case MENU_CLOSE:
1070
          if (ui_confirm_if_unsaved(db) == 0) {
1081
          if (ui_confirm_if_unsaved(db) == 0) {
1071
            loadfile(db, "");
1082
            loadfile(db, "");
1072
          }
1083
          }
1073
          uidirty.from = 0;
1084
          uidirty.from = 0;
1074
          uidirty.to = 0xff;
1085
          uidirty.to = 0xff;
1075
          uidirty.statusbar = 1;
1086
          uidirty.statusbar = 1;
1076
          break;
1087
          break;
1077
 
1088
 
1078
        case MENU_CHGEOL:
1089
        case MENU_CHGEOL:
1079
          db->modflag = 1;
1090
          db->modflag = 1;
1080
          db->lfonly ^= 1;
1091
          db->lfonly ^= 1;
1081
          break;
1092
          break;
1082
 
1093
 
1083
        case MENU_QUIT:
1094
        case MENU_QUIT:
1084
          quitnow = 1;
1095
          quitnow = 1;
1085
          for (curfile = 0; curfile < 10; curfile++) {
1096
          for (curfile = 0; curfile < 10; curfile++) {
1086
            if (dbarr[curfile].modflag) {
1097
            if (dbarr[curfile].modflag) {
1087
              db = select_slot(dbarr, curfile);
1098
              db = select_slot(dbarr, curfile);
1088
              if (ui_confirm_if_unsaved(db) != 0) quitnow = 0;
1099
              if (ui_confirm_if_unsaved(db) != 0) quitnow = 0;
1089
            }
1100
            }
1090
          }
1101
          }
1091
          break;
1102
          break;
1092
      }
1103
      }
1093
 
1104
 
1094
      if (quitnow) break;
1105
      if (quitnow) break;
1095
 
1106
 
1096
    } else if (k == 0x0D) { /* ENTER */
1107
    } else if (k == 0x0D) { /* ENTER */
1097
      unsigned short off = db->xoffset + db->cursorposx;
1108
      unsigned short off = db->xoffset + db->cursorposx;
1098
      /* add a new line */
1109
      /* add a new line */
1099
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
1110
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
1100
        db->modflag = 1;
1111
        db->modflag = 1;
1101
        db->cursor = db->cursor->prev; /* back to original line */
1112
        db->cursor = db->cursor->prev; /* back to original line */
1102
        /* trim the line above */
1113
        /* trim the line above */
1103
        db->cursor->len = off;
1114
        db->cursor->len = off;
1104
        /* move cursor to the (new) line below */
1115
        /* move cursor to the (new) line below */
1105
        db->curline -= 1;
1116
        db->curline -= 1;
1106
        uidirty.from = db->cursorposy;
1117
        uidirty.from = db->cursorposy;
1107
        uidirty.to = 0xff;
1118
        uidirty.to = 0xff;
1108
        cursor_down(db);
1119
        cursor_down(db);
1109
        cursor_home(db);
1120
        cursor_home(db);
1110
      } else {
1121
      } else {
1111
        /* ERROR: OUT OF MEMORY */
1122
        /* ERROR: OUT OF MEMORY */
1112
      }
1123
      }
1113
 
1124
 
1114
    } else if (k == 0x153) {  /* DEL */
1125
    } else if (k == 0x153) {  /* DEL */
1115
      del(db);
1126
      del(db);
1116
 
1127
 
1117
    } else if (k == 0x008) { /* BKSPC */
1128
    } else if (k == 0x008) { /* BKSPC */
1118
      bkspc(db);
1129
      bkspc(db);
1119
 
1130
 
1120
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
1131
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
1121
      char c = k;
1132
      char c = k;
1122
      insert_in_line(db, &c, 1);
1133
      insert_in_line(db, &c, 1);
1123
 
1134
 
1124
    } else if (k == 0x009) { /* TAB */
1135
    } else if (k == 0x009) { /* TAB */
1125
      insert_in_line(db, "        ", 8);
1136
      insert_in_line(db, "        ", 8);
1126
 
1137
 
1127
    } else if ((k >= 0x13b) && (k <= 0x144)) { /* F1..F10 */
1138
    } else if ((k >= 0x13b) && (k <= 0x144)) { /* F1..F10 */
1128
      curfile = k - 0x13b;
1139
      curfile = k - 0x13b;
1129
      db = select_slot(dbarr, curfile);
1140
      db = select_slot(dbarr, curfile);
1130
 
1141
 
1131
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
1142
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
1132
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
1143
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
1133
      for (;;) {
1144
      for (;;) {
1134
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
1145
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
1135
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
1146
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
1136
        cursor_right(db);
1147
        cursor_right(db);
1137
      }
1148
      }
1138
      /* now skip to next non-space or end of file */
1149
      /* now skip to next non-space or end of file */
1139
      for (;;) {
1150
      for (;;) {
1140
        cursor_right(db);
1151
        cursor_right(db);
1141
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1152
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1142
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
1153
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
1143
      }
1154
      }
1144
 
1155
 
1145
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
1156
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
1146
      cursor_left(db);
1157
      cursor_left(db);
1147
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
1158
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
1148
      for (;;) {
1159
      for (;;) {
1149
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
1160
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
1150
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1161
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1151
        cursor_left(db);
1162
        cursor_left(db);
1152
      }
1163
      }
1153
      /* now skip to next space or start of file */
1164
      /* now skip to next space or start of file */
1154
      for (;;) {
1165
      for (;;) {
1155
        cursor_left(db);
1166
        cursor_left(db);
1156
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
1167
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
1157
          cursor_right(db);
1168
          cursor_right(db);
1158
          break;
1169
          break;
1159
        }
1170
        }
1160
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
1171
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
1161
      }
1172
      }
1162
 
1173
 
1163
#ifdef DBG_UNHKEYS
1174
#ifdef DBG_UNHKEYS
1164
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
1175
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
1165
      char buff[4];
1176
      char buff[4];
1166
      const char *HEX = "0123456789ABCDEF";
1177
      const char *HEX = "0123456789ABCDEF";
1167
      buff[0] = HEX[(k >> 8) & 15];
1178
      buff[0] = HEX[(k >> 8) & 15];
1168
      buff[1] = HEX[(k >> 4) & 15];
1179
      buff[1] = HEX[(k >> 4) & 15];
1169
      buff[2] = HEX[k & 15];
1180
      buff[2] = HEX[k & 15];
1170
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", SCHEME_STBAR1, 17);
1181
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", SCHEME_STBAR1, 17);
1171
      mdr_cout_str(screenh - 1, 17, buff, SCHEME_STBAR1, 3);
1182
      mdr_cout_str(screenh - 1, 17, buff, SCHEME_STBAR1, 3);
1172
      keyb_getkey();
1183
      keyb_getkey();
1173
      break;
1184
      break;
1174
#endif
1185
#endif
1175
    }
1186
    }
1176
  }
1187
  }
1177
 
1188
 
1178
  mdr_cout_close();
1189
  mdr_cout_close();
1179
 
1190
 
1180
  /* no need to free memory, DOS will do it for me */
1191
  /* no need to free memory, DOS will do it for me */
1181
 
1192
 
1182
  return;
1193
  return;
1183
}
1194
}
1184
 
1195