Subversion Repositories SvarDOS

Rev

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

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