Subversion Repositories SvarDOS

Rev

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

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