Subversion Repositories SvarDOS

Rev

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

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