Subversion Repositories SvarDOS

Rev

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

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