Subversion Repositories SvarDOS

Rev

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

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