Subversion Repositories SvarDOS

Rev

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

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