Subversion Repositories SvarDOS

Rev

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

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