Subversion Repositories SvarDOS

Rev

Rev 1319 | Rev 1321 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1283 mateusz.vi 1
/* Sved, the SvarDOS editor
2
 *
3
 * Copyright (C) 2023 Mateusz Viste
4
 *
5
 * Sved is released under the terms of the MIT license.
6
 *
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
9
 * deal in the Software without restriction, including without limitation the
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
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
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,
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
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
23
 * IN THE SOFTWARE.
24
 */
1275 mateusz.vi 25
 
1282 mateusz.vi 26
#include <dos.h>
27
#include <fcntl.h>
1275 mateusz.vi 28
#include <stdlib.h>
29
#include <string.h>
1288 mateusz.vi 30
#include <malloc.h>   /* _fcalloc() */
1275 mateusz.vi 31
 
1312 mateusz.vi 32
#include "mdr\bios.h"
1275 mateusz.vi 33
#include "mdr\cout.h"
1307 mateusz.vi 34
#include "mdr\dos.h"
1275 mateusz.vi 35
#include "mdr\keyb.h"
36
 
1282 mateusz.vi 37
#include "svarlang\svarlang.h"
38
 
1275 mateusz.vi 39
#define COL_TXT        0
40
#define COL_STATUSBAR1 1
41
#define COL_STATUSBAR2 2
42
#define COL_SCROLLBAR  3
43
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
44
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
45
 
46
#define SCROLL_CURSOR 0xB1
47
 
48
 
49
struct line {
1288 mateusz.vi 50
  struct line far *prev;
51
  struct line far *next;
1275 mateusz.vi 52
  unsigned short len;
53
  char payload[1];
54
};
55
 
1315 mateusz.vi 56
struct file {
1320 mateusz.vi 57
  int fd;
1288 mateusz.vi 58
  struct line far *cursor;
1275 mateusz.vi 59
  unsigned short xoffset;
1316 mateusz.vi 60
  unsigned char cursorposx;
61
  unsigned char cursorposy;
1313 mateusz.vi 62
  char lfonly; /* set if line endings are LF (CR/LF otherwise) */
1320 mateusz.vi 63
  char fname[1]; /* dynamically sized */
1275 mateusz.vi 64
};
65
 
66
 
1287 mateusz.vi 67
/* returns non-zero on error */
1315 mateusz.vi 68
static int line_add(struct file *db, const char far *line) {
1289 mateusz.vi 69
  unsigned short slen;
1288 mateusz.vi 70
  struct line far *l;
1275 mateusz.vi 71
 
1289 mateusz.vi 72
  /* slen = strlen(line) (but for far pointer) */
73
  for (slen = 0; line[slen] != 0; slen++);
74
 
1275 mateusz.vi 75
  /* trim out CR/LF line endings */
76
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
77
    slen -= 2;
78
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
79
    slen--;
80
  }
81
 
1288 mateusz.vi 82
  l = _fcalloc(1, sizeof(struct line) + slen + 1);
1287 mateusz.vi 83
  if (l == NULL) return(-1);
84
 
1275 mateusz.vi 85
  l->prev = db->cursor;
86
  if (db->cursor) {
87
    l->next = db->cursor->next;
88
    db->cursor->next = l;
89
    l->next->prev = l;
90
  }
91
  db->cursor = l;
1288 mateusz.vi 92
  _fmemcpy(l->payload, line, slen);
1275 mateusz.vi 93
  l->len = slen;
1287 mateusz.vi 94
 
95
  return(0);
1275 mateusz.vi 96
}
97
 
98
 
1315 mateusz.vi 99
static void db_rewind(struct file *db) {
1275 mateusz.vi 100
  if (db->cursor == NULL) return;
101
  while (db->cursor->prev) db->cursor = db->cursor->prev;
102
}
103
 
104
 
1289 mateusz.vi 105
static void load_colorscheme(void) {
1275 mateusz.vi 106
  scheme[COL_TXT] = 0x17;
107
  scheme[COL_STATUSBAR1] = 0x70;
108
  scheme[COL_STATUSBAR2] = 0x78;
109
  scheme[COL_SCROLLBAR] = 0x70;
110
}
111
 
112
 
1320 mateusz.vi 113
static void ui_basic(unsigned char screenw, unsigned char screenh, const struct file *db) {
1275 mateusz.vi 114
  unsigned char i;
1302 mateusz.vi 115
  const char *s = svarlang_strid(0); /* HELP */
1275 mateusz.vi 116
  unsigned char helpcol = screenw - (strlen(s) + 4);
117
 
1313 mateusz.vi 118
  /* fill status bar with background */
119
  mdr_cout_char_rep(screenh - 1, 0, ' ', scheme[COL_STATUSBAR1], screenw);
120
 
121
  /* filename */
1320 mateusz.vi 122
  mdr_cout_str(screenh - 1, 0, db->fname, scheme[COL_STATUSBAR1], screenw);
1313 mateusz.vi 123
 
124
  /* eol type */
125
  if (db->lfonly) {
126
    mdr_cout_str(screenh - 1, helpcol - 3, "LF", scheme[COL_STATUSBAR1], 5);
127
  } else {
128
    mdr_cout_str(screenh - 1, helpcol - 5, "CRLF", scheme[COL_STATUSBAR1], 5);
1275 mateusz.vi 129
  }
1313 mateusz.vi 130
 
1275 mateusz.vi 131
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
132
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
133
 
134
  /* scroll bar */
135
  for (i = 0; i < (screenh - 1); i++) {
136
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
137
  }
138
}
139
 
140
 
1312 mateusz.vi 141
static void ui_msg(const char *msg, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
142
  unsigned short x, y, msglen, i;
143
  msglen = strlen(msg);
144
  y = (screenh - 4) >> 1;
145
  x = (screenw - msglen - 4) >> 1;
146
  for (i = y+2; i >= y; i--) mdr_cout_char_rep(i, x, ' ', scheme[COL_STATUSBAR1], msglen + 2);
147
  mdr_cout_str(y+1, x+1, msg, scheme[COL_STATUSBAR1], msglen);
148
 
149
  if (*uidirtyfrom > y) *uidirtyfrom = y;
150
  if (*uidirtyto < y+2) *uidirtyto = y+2;
151
}
152
 
153
 
1309 mateusz.vi 154
static void ui_help(unsigned char screenw) {
155
#define MAXLINLEN 35
1312 mateusz.vi 156
  unsigned short i, offset;
1309 mateusz.vi 157
  offset = (screenw - MAXLINLEN + 1) >> 1;
158
  mdr_cout_cursor_hide();
159
  for (i = 2; i <= 12; i++) {
1312 mateusz.vi 160
    mdr_cout_char_rep(i, offset - 2, ' ', scheme[COL_STATUSBAR1], MAXLINLEN);
1309 mateusz.vi 161
  }
162
 
163
  mdr_cout_str(3, offset, svarlang_str(0, 0), scheme[COL_STATUSBAR1], MAXLINLEN);
164
  for (i = 0; i <= 2; i++) {
165
    mdr_cout_str(5 + i, offset, svarlang_str(8, i), scheme[COL_STATUSBAR1], MAXLINLEN);
166
  }
167
  mdr_cout_str(9, offset, svarlang_str(8, 10), scheme[COL_STATUSBAR1], MAXLINLEN);
168
  mdr_cout_str(11, offset, svarlang_str(8, 11), scheme[COL_STATUSBAR1], MAXLINLEN);
169
 
170
  keyb_getkey();
171
  mdr_cout_cursor_show();
172
#undef MAXLINLEN
173
}
174
 
175
 
1316 mateusz.vi 176
static void ui_refresh(const struct file *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto) {
1318 mateusz.vi 177
  unsigned char x;
1310 mateusz.vi 178
  const struct line far *l;
1316 mateusz.vi 179
  unsigned char y = db->cursorposy;
1275 mateusz.vi 180
 
1288 mateusz.vi 181
#ifdef DBG_REFRESH
1282 mateusz.vi 182
  static char m = 'a';
183
  m++;
184
  if (m > 'z') m = 'a';
1288 mateusz.vi 185
#endif
1282 mateusz.vi 186
 
1310 mateusz.vi 187
  /* rewind cursor line to first line that needs redrawing */
188
  for (l = db->cursor; y > uidirtyfrom; y--) l = l->prev;
1282 mateusz.vi 189
 
1310 mateusz.vi 190
  /* iterate over lines and redraw whatever needs to be redrawn */
191
  for (; l != NULL; l = l->next, y++) {
192
 
193
    /* skip lines that do not need to be refreshed */
1289 mateusz.vi 194
    if (y < uidirtyfrom) continue;
195
    if (y > uidirtyto) break;
1282 mateusz.vi 196
 
1318 mateusz.vi 197
    x = 0;
1275 mateusz.vi 198
    if (db->xoffset < l->len) {
1318 mateusz.vi 199
      unsigned char i, limit;
200
      if (l->len - db->xoffset < screenw) {
201
        limit = l->len;
202
      } else {
203
        limit = db->xoffset + screenw - 1;
204
      }
205
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], scheme[COL_TXT]);
1275 mateusz.vi 206
    }
1282 mateusz.vi 207
 
1318 mateusz.vi 208
    /* write empty spaces until end of line */
209
    if (x < screenw - 1) mdr_cout_char_rep(y, x, ' ', scheme[COL_TXT], screenw - 1 - x);
210
 
1288 mateusz.vi 211
#ifdef DBG_REFRESH
1282 mateusz.vi 212
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
1288 mateusz.vi 213
#endif
1282 mateusz.vi 214
 
1275 mateusz.vi 215
    if (y == screenh - 2) break;
216
  }
1310 mateusz.vi 217
 
1319 mateusz.vi 218
  /* fill all lines below if empty (and they need to be redrawn) */
219
  if (l == NULL) {
220
    while ((y < screenh - 1) && (y < uidirtyto)) {
221
      mdr_cout_char_rep(y++, 0, ' ', scheme[COL_TXT], screenw - 1);
222
    }
1318 mateusz.vi 223
  }
1275 mateusz.vi 224
}
225
 
226
 
1316 mateusz.vi 227
static void check_cursor_not_after_eol(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
228
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
1275 mateusz.vi 229
 
230
  if (db->cursor->len < db->xoffset) {
1316 mateusz.vi 231
    db->cursorposx = 0;
1275 mateusz.vi 232
    db->xoffset = db->cursor->len;
1282 mateusz.vi 233
    *uidirtyfrom = 0;
234
    *uidirtyto = 0xff;
1275 mateusz.vi 235
  } else {
1316 mateusz.vi 236
    db->cursorposx = db->cursor->len - db->xoffset;
1275 mateusz.vi 237
  }
238
}
239
 
240
 
1316 mateusz.vi 241
static void cursor_up(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1275 mateusz.vi 242
  if (db->cursor->prev != NULL) {
243
    db->cursor = db->cursor->prev;
1316 mateusz.vi 244
    if (db->cursorposy == 0) {
1282 mateusz.vi 245
      *uidirtyfrom = 0;
246
      *uidirtyto = 0xff;
1275 mateusz.vi 247
    } else {
1316 mateusz.vi 248
      db->cursorposy -= 1;
1275 mateusz.vi 249
    }
250
  }
251
}
252
 
253
 
1316 mateusz.vi 254
static void cursor_eol(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1275 mateusz.vi 255
  /* adjust xoffset to make sure eol is visible on screen */
1282 mateusz.vi 256
  if (db->xoffset > db->cursor->len) {
257
    db->xoffset = db->cursor->len - 1;
258
    *uidirtyfrom = 0;
259
    *uidirtyto = 0xff;
260
  }
261
 
262
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
263
    db->xoffset = db->cursor->len - screenw + 2;
264
    *uidirtyfrom = 0;
265
    *uidirtyto = 0xff;
266
  }
1316 mateusz.vi 267
  db->cursorposx = db->cursor->len - db->xoffset;
1275 mateusz.vi 268
}
269
 
270
 
1316 mateusz.vi 271
static void cursor_down(struct file *db, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1276 mateusz.vi 272
  if (db->cursor->next != NULL) {
273
    db->cursor = db->cursor->next;
1316 mateusz.vi 274
    if (db->cursorposy < screenh - 2) {
275
      db->cursorposy += 1;
1276 mateusz.vi 276
    } else {
1282 mateusz.vi 277
      *uidirtyfrom = 0;
278
      *uidirtyto = 0xff;
1276 mateusz.vi 279
    }
280
  }
281
}
282
 
283
 
1316 mateusz.vi 284
static void cursor_left(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
285
  if (db->cursorposx > 0) {
286
    db->cursorposx -= 1;
1302 mateusz.vi 287
  } else if (db->xoffset > 0) {
288
    db->xoffset -= 1;
289
    *uidirtyfrom = 0;
290
    *uidirtyto = 0xff;
291
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
1316 mateusz.vi 292
    cursor_up(db, uidirtyfrom, uidirtyto);
293
    cursor_eol(db, screenw, uidirtyfrom, uidirtyto);
1302 mateusz.vi 294
  }
295
}
296
 
297
 
1316 mateusz.vi 298
static void cursor_home(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
299
  db->cursorposx = 0;
1282 mateusz.vi 300
  if (db->xoffset != 0) {
301
    db->xoffset = 0;
302
    *uidirtyfrom = 0;
303
    *uidirtyto = 0xff;
304
  }
1276 mateusz.vi 305
}
306
 
307
 
1316 mateusz.vi 308
static void cursor_right(struct file *db, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
309
  if (db->cursor->len > db->xoffset + db->cursorposx) {
310
    if (db->cursorposx < screenw - 2) {
311
      db->cursorposx += 1;
1308 mateusz.vi 312
    } else {
313
      db->xoffset += 1;
314
      *uidirtyfrom = 0;
315
      *uidirtyto = 0xff;
316
    }
317
  } else {
1316 mateusz.vi 318
    cursor_down(db, screenh, uidirtyfrom, uidirtyto);
319
    cursor_home(db, uidirtyfrom, uidirtyto);
1308 mateusz.vi 320
  }
321
}
322
 
323
 
1316 mateusz.vi 324
static void del(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
325
  if (db->cursorposx + db->xoffset < db->cursor->len) {
326
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
1292 mateusz.vi 327
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
1316 mateusz.vi 328
    *uidirtyfrom = db->cursorposy;
329
    *uidirtyto = db->cursorposy;
1292 mateusz.vi 330
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
331
    struct line far *nextline = db->cursor->next;
332
    if (db->cursor->next->len > 0) {
333
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
334
      if (newptr != NULL) {
335
        db->cursor = newptr;
336
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
337
        db->cursor->len += db->cursor->next->len;
338
      }
339
    }
340
    db->cursor->next = db->cursor->next->next;
341
    db->cursor->next->prev = db->cursor;
342
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
343
    _ffree(nextline);
1316 mateusz.vi 344
    *uidirtyfrom = db->cursorposy;
1292 mateusz.vi 345
    *uidirtyto = 0xff;
346
  }
347
}
348
 
349
 
1316 mateusz.vi 350
static void bkspc(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1302 mateusz.vi 351
 
352
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
1316 mateusz.vi 353
  if ((db->cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
1302 mateusz.vi 354
 
1316 mateusz.vi 355
  cursor_left(db, screenw, uidirtyfrom, uidirtyto);
356
  del(db, uidirtyfrom, uidirtyto);
1302 mateusz.vi 357
}
358
 
359
 
1286 mateusz.vi 360
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
361
 * of argc and argv, saves some 330 bytes of binary size */
362
static char *parseargv(void) {
363
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
364
  unsigned char count = 0;
365
  char *argv[4];
366
 
367
  while (count < 4) {
368
    /* jump to nearest arg */
369
    while (*tail == ' ') {
370
      *tail = 0;
371
      tail++;
372
    }
373
 
374
    if (*tail == '\r') {
375
      *tail = 0;
376
      break;
377
    }
378
 
379
    argv[count++] = tail;
380
 
381
    /* jump to next delimiter */
382
    while ((*tail != ' ') && (*tail != '\r')) tail++;
383
  }
384
 
385
  /* check args now */
386
  if (count != 1) return(NULL);
387
 
388
  return(argv[0]);
389
}
390
 
391
 
1320 mateusz.vi 392
static struct file *loadfile(const char *fname) {
1287 mateusz.vi 393
  char buff[1024];
394
  unsigned int prevlen = 0, len, llen;
395
  int fd;
1320 mateusz.vi 396
  struct file *db;
1287 mateusz.vi 397
 
398
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
399
    mdr_coutraw_puts("Failed to open file:");
400
    mdr_coutraw_puts(fname);
1320 mateusz.vi 401
    return(NULL);
1287 mateusz.vi 402
  }
403
 
1320 mateusz.vi 404
  len = strlen(fname) + 1;
405
  db = calloc(1, sizeof(struct file) + len);
406
  if (db == NULL) {
407
    _dos_close(fd);
408
    return(NULL);
409
  }
410
  memcpy(db->fname, fname, len);
411
 
1313 mateusz.vi 412
  db->lfonly = 1;
413
 
1287 mateusz.vi 414
  do {
415
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
416
      len += prevlen;
417
    } else {
418
      len = prevlen;
419
    }
420
 
421
    /* look for nearest \n and replace with 0*/
422
    for (llen = 0; buff[llen] != '\n'; llen++) {
1320 mateusz.vi 423
      if (llen == sizeof(buff)) { /* TODO line too long: handle it in some classy way */
424
        break;
425
      }
1287 mateusz.vi 426
    }
427
    buff[llen] = 0;
1313 mateusz.vi 428
    if ((llen > 0) && (buff[llen - 1] == '\r')) {
429
      buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
430
      db->lfonly = 0;
431
    }
1287 mateusz.vi 432
    if (line_add(db, buff) != 0) {
433
      mdr_coutraw_puts("out of memory");
1320 mateusz.vi 434
      free(db);
435
      db = NULL;
1287 mateusz.vi 436
      break;
437
    }
438
 
439
    len -= llen + 1;
440
    memmove(buff, buff + llen + 1, len);
441
    prevlen = len;
442
  } while (len > 0);
443
 
444
  _dos_close(fd);
445
 
1320 mateusz.vi 446
  /* add an empty line at end if not present already, also rewind cursor to top of file */
447
  if (db != NULL) {
448
    if (db->cursor->len != 0) line_add(db, "");
449
    db_rewind(db);
450
  }
451
 
452
  return(db);
1287 mateusz.vi 453
}
454
 
455
 
1320 mateusz.vi 456
static int savefile(const struct file *db) {
1311 mateusz.vi 457
  int fd;
458
  const struct line far *l;
459
  unsigned bytes;
1313 mateusz.vi 460
  unsigned char eollen;
461
  unsigned char eolbuf[2];
462
 
1320 mateusz.vi 463
  if (_dos_open(db->fname, O_WRONLY, &fd) != 0) {
1311 mateusz.vi 464
    return(-1);
465
  }
466
 
467
  l = db->cursor;
468
  while (l->prev) l = l->prev;
469
 
1313 mateusz.vi 470
  /* preset line terminators */
471
  if (db->lfonly) {
472
    eolbuf[0] = '\n';
473
    eollen = 1;
474
  } else {
475
    eolbuf[0] = '\r';
476
    eolbuf[1] = '\n';
477
    eollen = 2;
478
  }
479
 
1311 mateusz.vi 480
  while (l) {
481
    _dos_write(fd, l->payload, l->len, &bytes);
1313 mateusz.vi 482
    _dos_write(fd, eolbuf, eollen, &bytes);
1311 mateusz.vi 483
    l = l->next;
484
  }
485
 
486
  _dos_close(fd);
487
  return(0);
488
}
489
 
490
 
1317 mateusz.vi 491
static void insert_in_line(struct file *db, const char *databuf, unsigned short len, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
492
  struct line far *n;
493
  n = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + len);
494
  if (n != NULL) {
495
    unsigned short off = db->xoffset + db->cursorposx;
496
    if (n->prev) n->prev->next = n;
497
    if (n->next) n->next->prev = n;
498
    db->cursor = n;
499
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
500
    db->cursor->len += len;
501
    *uidirtyfrom = db->cursorposy;
502
    *uidirtyto = db->cursorposy;
503
    while (len--) {
504
      db->cursor->payload[off++] = *databuf;
505
      databuf++;
506
      cursor_right(db, screenw, screenh, uidirtyfrom, uidirtyto);
507
    }
508
  }
509
}
510
 
511
 
1286 mateusz.vi 512
int main(void) {
513
  const char *fname;
1320 mateusz.vi 514
  struct file *db;
1275 mateusz.vi 515
  unsigned char screenw = 0, screenh = 0;
1282 mateusz.vi 516
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
1275 mateusz.vi 517
 
1307 mateusz.vi 518
  {
519
    char nlspath[128], lang[8];
520
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
521
  }
1282 mateusz.vi 522
 
1286 mateusz.vi 523
  fname = parseargv();
524
 
525
  if (fname == NULL) {
1302 mateusz.vi 526
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
1275 mateusz.vi 527
    return(0);
528
  }
529
 
1282 mateusz.vi 530
  /* load file */
1320 mateusz.vi 531
  db = loadfile(fname);
532
  if (db == NULL) return(1);
1282 mateusz.vi 533
 
1275 mateusz.vi 534
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
1320 mateusz.vi 535
  ui_basic(screenw, screenh, db);
1275 mateusz.vi 536
 
537
  for (;;) {
538
    int k;
539
 
1320 mateusz.vi 540
    check_cursor_not_after_eol(db, &uidirtyfrom, &uidirtyto);
541
    mdr_cout_locate(db->cursorposy, db->cursorposx);
1275 mateusz.vi 542
 
1282 mateusz.vi 543
    if (uidirtyfrom != 0xff) {
1320 mateusz.vi 544
      ui_refresh(db, screenw, screenh, uidirtyfrom, uidirtyto);
1282 mateusz.vi 545
      uidirtyfrom = 0xff;
546
    }
1275 mateusz.vi 547
 
548
    k = keyb_getkey();
1282 mateusz.vi 549
 
1275 mateusz.vi 550
    if (k == 0x150) { /* down */
1320 mateusz.vi 551
      cursor_down(db, screenh, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 552
 
553
    } else if (k == 0x148) { /* up */
1320 mateusz.vi 554
      cursor_up(db, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 555
 
556
    } else if (k == 0x14D) { /* right */
1320 mateusz.vi 557
      cursor_right(db, screenw, screenh, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 558
 
559
    } else if (k == 0x14B) { /* left */
1320 mateusz.vi 560
      cursor_left(db, screenw, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 561
 
1282 mateusz.vi 562
    } else if (k == 0x149) { /* pgup */
563
      // TODO
564
 
565
    } else if (k == 0x151) { /* pgdown */
566
      // TODO
567
 
568
    } else if (k == 0x147) { /* home */
1320 mateusz.vi 569
       cursor_home(db, &uidirtyfrom, &uidirtyto);
1282 mateusz.vi 570
 
571
    } else if (k == 0x14F) { /* end */
1320 mateusz.vi 572
       cursor_eol(db, screenw, &uidirtyfrom, &uidirtyto);
1282 mateusz.vi 573
 
1275 mateusz.vi 574
    } else if (k == 0x1B) { /* ESC */
575
      break;
576
 
1289 mateusz.vi 577
    } else if (k == 0x0D) { /* ENTER */
578
      /* add a new line */
1320 mateusz.vi 579
      if (line_add(db, db->cursor->payload + db->xoffset + db->cursorposx) == 0) {
1289 mateusz.vi 580
        /* trim the line above */
1320 mateusz.vi 581
        db->cursor->prev->len = db->xoffset + db->cursorposx;
582
        db->cursor->prev->payload[db->cursor->prev->len] = 0;
1289 mateusz.vi 583
        /* move cursor to the (new) line below */
1320 mateusz.vi 584
        db->cursorposx = 0;
585
        if (db->cursorposy < screenh - 2) {
586
          uidirtyfrom = db->cursorposy;
587
          db->cursorposy++;
1289 mateusz.vi 588
        } else {
589
          uidirtyfrom = 0;
590
        }
591
        uidirtyto = 0xff;
592
      } else {
593
        /* ERROR: OUT OF MEMORY */
594
      }
595
 
1292 mateusz.vi 596
    } else if (k == 0x153) {  /* DEL */
1320 mateusz.vi 597
      del(db, &uidirtyfrom, &uidirtyto);
1292 mateusz.vi 598
 
1302 mateusz.vi 599
    } else if (k == 0x008) { /* BKSPC */
1320 mateusz.vi 600
      bkspc(db, screenw, &uidirtyfrom, &uidirtyto);
1302 mateusz.vi 601
 
1308 mateusz.vi 602
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
1317 mateusz.vi 603
      char c = k;
1320 mateusz.vi 604
      insert_in_line(db, &c, 1, screenw, screenh, &uidirtyfrom, &uidirtyto);
1308 mateusz.vi 605
 
1317 mateusz.vi 606
    } else if (k == 0x009) { /* TAB */
607
      const char *tab = "        ";
1320 mateusz.vi 608
      insert_in_line(db, tab, 8, screenw, screenh, &uidirtyfrom, &uidirtyto);
1317 mateusz.vi 609
 
1309 mateusz.vi 610
    } else if (k == 0x13b) { /* F1 */
611
      ui_help(screenw);
612
      uidirtyfrom = 0;
613
      uidirtyto = 0xff;
614
 
1311 mateusz.vi 615
    } else if (k == 0x13f) { /* F5 */
1312 mateusz.vi 616
      const char *m[] = {"SAVED", "SAVING FILE FAILED"};
617
      int i;
1320 mateusz.vi 618
      i = !!savefile(db);
1312 mateusz.vi 619
      ui_msg(m[i], screenw, screenh, &uidirtyfrom, &uidirtyto);
620
      mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
1311 mateusz.vi 621
 
1314 mateusz.vi 622
    } else if (k == 0x144) { /* F10 */
1320 mateusz.vi 623
      db->lfonly ^= 1;
624
      ui_basic(screenw, screenh, db);
1314 mateusz.vi 625
 
1282 mateusz.vi 626
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
627
      char buff[4];
628
      const char *HEX = "0123456789ABCDEF";
629
      buff[0] = HEX[(k >> 8) & 15];
630
      buff[1] = HEX[(k >> 4) & 15];
631
      buff[2] = HEX[k & 15];
632
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
633
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
1275 mateusz.vi 634
      keyb_getkey();
635
      break;
636
    }
637
  }
638
 
639
  mdr_cout_close();
640
 
1320 mateusz.vi 641
  /* no need to free memory, DOS will do it for me */
1275 mateusz.vi 642
 
643
  return(0);
644
}