Subversion Repositories SvarDOS

Rev

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

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