Subversion Repositories SvarDOS

Rev

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

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