Subversion Repositories SvarDOS

Rev

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

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