Subversion Repositories SvarDOS

Rev

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

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