Subversion Repositories SvarDOS

Rev

Rev 1775 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1283 mateusz.vi 1
/* Sved, the SvarDOS editor
2
 *
1759 mateusz.vi 3
 * Copyright (C) 2023-2024 Mateusz Viste
1283 mateusz.vi 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
 
1556 mateusz.vi 26
#include <i86.h> /* MK_FP() */
1275 mateusz.vi 27
 
1312 mateusz.vi 28
#include "mdr\bios.h"
1275 mateusz.vi 29
#include "mdr\cout.h"
1307 mateusz.vi 30
#include "mdr\dos.h"
1275 mateusz.vi 31
 
1282 mateusz.vi 32
#include "svarlang\svarlang.h"
33
 
1326 mateusz.vi 34
 
1775 mateusz.vi 35
#define PVER "2024.1"
1759 mateusz.vi 36
#define PDATE "2023-2024"
1356 mateusz.vi 37
 
1326 mateusz.vi 38
/*****************************************************************************
39
 * global variables and definitions                                          *
40
 *****************************************************************************/
41
 
1275 mateusz.vi 42
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
1333 mateusz.vi 43
static unsigned char SCHEME_TEXT   = 0x07,
1354 mateusz.vi 44
                     SCHEME_MENU   = 0x70,
45
                     SCHEME_MENU_CUR= 0x0f,
46
                     SCHEME_MENU_SEL= 0x00,
1333 mateusz.vi 47
                     SCHEME_STBAR1 = 0x70,
1358 mateusz.vi 48
                     SCHEME_STBAR2 = 0x70, /* greyed out information */
49
                     SCHEME_STBAR3 = 0x70, /* query */
1333 mateusz.vi 50
                     SCHEME_SCROLL = 0x70,
51
                     SCHEME_MSG    = 0x70,
1358 mateusz.vi 52
                     SCHEME_ERR    = 0x70;
1327 mateusz.vi 53
 
1427 mateusz.vi 54
static unsigned char screenw, screenh, screenlastrow, screenlastcol;
1412 mateusz.vi 55
static unsigned char glob_monomode, glob_tablessmode;
1275 mateusz.vi 56
 
1548 mateusz.vi 57
static char buff[512]; /* short lived buffer for whatever needed */
58
 
1327 mateusz.vi 59
static struct {
60
    unsigned char from;
61
    unsigned char to;
1342 mateusz.vi 62
    unsigned char statusbar;
63
} uidirty = {0, 0xff, 1}; /* make sure to redraw entire UI at first run */
1327 mateusz.vi 64
 
1275 mateusz.vi 65
#define SCROLL_CURSOR 0xB1
66
 
67
struct line {
1430 mateusz.vi 68
  unsigned short len;
69
  struct line far *next;
1288 mateusz.vi 70
  struct line far *prev;
1275 mateusz.vi 71
  char payload[1];
72
};
73
 
1315 mateusz.vi 74
struct file {
1288 mateusz.vi 75
  struct line far *cursor;
1275 mateusz.vi 76
  unsigned short xoffset;
1348 mateusz.vi 77
  unsigned short cursorposx;
78
  unsigned short cursorposy;
1328 mateusz.vi 79
  unsigned short totlines;
80
  unsigned short curline;
1436 mateusz.vi 81
  unsigned short curline_prev;
1330 mateusz.vi 82
  char lfonly;   /* set if line endings are LF (CR/LF otherwise) */
83
  char modflag;  /* non-zero if file has been modified since last save */
1355 mateusz.vi 84
  char modflagprev;
1549 mateusz.vi 85
  char slotid; /* 0..9 */
1332 mateusz.vi 86
  char fname[128];
1275 mateusz.vi 87
};
88
 
89
 
1572 mateusz.vi 90
 
1326 mateusz.vi 91
/*****************************************************************************
1578 mateusz.vi 92
 * assembly pragma "functions"                                               *
1572 mateusz.vi 93
 *****************************************************************************/
94
 
1578 mateusz.vi 95
 
1580 mateusz.vi 96
unsigned short dos_alloc(unsigned short siz);
1572 mateusz.vi 97
 
1580 mateusz.vi 98
#pragma aux dos_alloc = \
1572 mateusz.vi 99
"mov ah, 0x48" \
100
"int 0x21" \
101
"jnc done" \
102
"xor ax, ax" \
103
"done:" \
104
parm [bx] \
105
value [ax];
106
 
107
 
1580 mateusz.vi 108
unsigned short dos_free(unsigned short segn);
1572 mateusz.vi 109
 
1580 mateusz.vi 110
#pragma aux dos_free = \
1572 mateusz.vi 111
"mov ah, 0x49" \
112
"int 0x21" \
113
"jc done" \
114
"xor ax, ax" \
115
"done:" \
116
parm [es] \
117
value [ax];
118
 
1776 mateusz.vi 119
/* mode 0x3C = create or truncate file ; model 0x3D = open for read
120
 * returns 0 on failure, file handle otherwise */
121
unsigned short dos_fopen(const char *fname, unsigned char mode);
1578 mateusz.vi 122
 
1776 mateusz.vi 123
#pragma aux dos_fopen = \
124
"xor al, al" \
125
"xor cx, cx" \
126
"int 0x21" \
127
"jnc DONE" \
128
"xor ax, ax" \
129
"DONE:" \
130
parm [dx] [ah] \
131
modify [cx] \
132
value [ax];
133
 
1580 mateusz.vi 134
unsigned short dos_fclose(unsigned short handle);
1572 mateusz.vi 135
 
1580 mateusz.vi 136
#pragma aux dos_fclose = \
1573 mateusz.vi 137
"mov ah,0x3e" \
138
"int 0x21" \
139
"jc done" \
140
"xor ax, ax" \
141
"done:" \
142
parm [bx] \
143
value [ax];
144
 
1578 mateusz.vi 145
 
1580 mateusz.vi 146
unsigned short dos_fread(unsigned short handle, void *buf, unsigned short count, unsigned short *bytes);
1573 mateusz.vi 147
 
1580 mateusz.vi 148
#pragma aux dos_fread = \
1574 mateusz.vi 149
"mov ah, 0x3f" \
150
"int 0x21" \
151
"jc done" \
152
"mov [di], ax" \
153
"xor ax, ax" \
154
"done:" \
155
parm [bx] [dx] [cx] [di] \
156
value [ax]
157
 
1578 mateusz.vi 158
 
1576 mateusz.vi 159
unsigned short dos_resizeblock(unsigned short siz, unsigned short segn);
1574 mateusz.vi 160
 
1576 mateusz.vi 161
#pragma aux dos_resizeblock = \
162
"mov ah, 0x4a" \
163
"int 0x21" \
164
"jc done" \
165
"xor ax, ax" \
166
"done:" \
167
parm [bx] [es] \
168
value [ax]
1574 mateusz.vi 169
 
1576 mateusz.vi 170
 
1580 mateusz.vi 171
unsigned short dos_fwrite(unsigned short handle, unsigned short count, unsigned short *bytes, const void far *buf);
1578 mateusz.vi 172
/* NOTE: last parameter (far pointer buf) is passed on the stack */
1580 mateusz.vi 173
#pragma aux dos_fwrite = \
1579 mateusz.vi 174
"push ds"   /* save DS into AX */ \
1578 mateusz.vi 175
"pop ax" \
176
"pop dx" \
177
"pop ds" \
1579 mateusz.vi 178
"push ax"   /* save DS into stack for later restoration */ \
1578 mateusz.vi 179
"mov ah, 0x40" \
180
"int 0x21" \
181
"pop ds" \
182
"jc done" \
183
"mov [di], ax" \
184
"xor ax, ax" \
185
"done:" \
186
parm [bx] [cx] [di] \
1776 mateusz.vi 187
modify [dx] \
1578 mateusz.vi 188
value [ax]
189
 
190
 
1572 mateusz.vi 191
/*****************************************************************************
1326 mateusz.vi 192
 * functions                                                                 *
193
 *****************************************************************************/
194
 
1575 mateusz.vi 195
 
196
static size_t strlen(const char *s) {
197
  const char *ptr = s;
198
  while (*ptr != 0) ptr++;
199
  return(ptr - s);
200
}
201
 
202
 
203
static void bzero(void *ptr, size_t len) {
204
  char *p = ptr;
205
  while (len > 0) {
206
    *p = 0;
207
    p++;
208
    len--;
209
  }
210
}
211
 
212
 
213
static void fmemmove(void far *dst, const void far *src, size_t len) {
1579 mateusz.vi 214
  short delta = 1;
215
  /* detect possible overlap in the forward direction */
216
  if (dst >= src) {
217
    dst = (char far *)dst + len - 1;
218
    src = (char far *)src + len - 1;
219
    delta = -1;
220
  }
1575 mateusz.vi 221
  while (len-- > 0) {
222
    *(char far *)dst = *(char far *)src;
1579 mateusz.vi 223
    dst = (char far *)dst + delta;
224
    src = (char far *)src + delta;
1575 mateusz.vi 225
  }
226
}
227
 
228
 
1339 mateusz.vi 229
static struct line far *line_calloc(unsigned short siz) {
1342 mateusz.vi 230
  struct line far *res;
1556 mateusz.vi 231
  unsigned short seg;
1461 mateusz.vi 232
 
1580 mateusz.vi 233
  seg = dos_alloc((sizeof(struct line) + siz + 15) / 16);
1556 mateusz.vi 234
  if (seg == 0) return(NULL);
1342 mateusz.vi 235
  res = MK_FP(seg, 0);
1461 mateusz.vi 236
  res->len = 0;
237
  res->next = NULL;
238
  res->prev = NULL;
239
 
240
  return(res);
1339 mateusz.vi 241
}
242
 
243
 
244
static void line_free(struct line far *ptr) {
1580 mateusz.vi 245
  dos_free(FP_SEG(ptr));
1339 mateusz.vi 246
}
247
 
248
 
1550 mateusz.vi 249
static int curline_resize(struct file *db, unsigned short newsiz) {
1339 mateusz.vi 250
  struct line far *newptr;
251
 
1341 mateusz.vi 252
  /* try resizing the block (much faster) */
1576 mateusz.vi 253
  if (dos_resizeblock((sizeof(struct line) + newsiz + 15) / 16, FP_SEG(db->cursor)) == 0) return(0);
1339 mateusz.vi 254
 
255
  /* create a new block and copy data over */
256
  newptr = line_calloc(newsiz);
1340 mateusz.vi 257
  if (newptr == NULL) return(-1);
1568 mateusz.vi 258
  fmemmove(newptr, db->cursor, sizeof(struct line) + db->cursor->len);
1340 mateusz.vi 259
 
260
  /* rewire the linked list */
261
  db->cursor = newptr;
262
  if (newptr->next) newptr->next->prev = newptr;
263
  if (newptr->prev) newptr->prev->next = newptr;
264
 
265
  return(0);
1339 mateusz.vi 266
}
267
 
268
 
1328 mateusz.vi 269
/* adds a new line at cursor position into file linked list and advance cursor
1324 mateusz.vi 270
 * returns non-zero on error */
1328 mateusz.vi 271
static int line_add(struct file *db, const char far *line, unsigned short slen) {
1288 mateusz.vi 272
  struct line far *l;
1275 mateusz.vi 273
 
1339 mateusz.vi 274
  l = line_calloc(slen);
1287 mateusz.vi 275
  if (l == NULL) return(-1);
276
 
1275 mateusz.vi 277
  l->prev = db->cursor;
278
  if (db->cursor) {
279
    l->next = db->cursor->next;
280
    db->cursor->next = l;
281
    l->next->prev = l;
282
  }
283
  db->cursor = l;
1342 mateusz.vi 284
  if (slen > 0) {
1568 mateusz.vi 285
    fmemmove(l->payload, line, slen);
1342 mateusz.vi 286
    l->len = slen;
287
  }
1287 mateusz.vi 288
 
1328 mateusz.vi 289
  db->totlines += 1;
290
  db->curline += 1;
291
 
1287 mateusz.vi 292
  return(0);
1275 mateusz.vi 293
}
294
 
295
 
1348 mateusz.vi 296
static void ui_getstring(const char *query, char *s, unsigned short maxlen) {
297
  unsigned short len = 0;
1544 mateusz.vi 298
  unsigned char x;
1332 mateusz.vi 299
  int k;
300
 
301
  if (maxlen == 0) return;
302
  maxlen--; /* make room for the nul terminator */
303
 
304
  /* print query string */
1544 mateusz.vi 305
  x = mdr_cout_str(screenlastrow, 0, query, SCHEME_STBAR3, 40);
306
  mdr_cout_char_rep(screenlastrow, x++, ' ', SCHEME_STBAR3, screenw - x);
1332 mateusz.vi 307
 
308
  for (;;) {
1544 mateusz.vi 309
    mdr_cout_locate(screenlastrow, x + len);
1410 mateusz.vi 310
    k = mdr_dos_getkey2();
1332 mateusz.vi 311
 
1345 mateusz.vi 312
    switch (k) {
313
      case 0x1b: /* ESC */
314
        s[0] = 0;
315
        return;
316
      case '\r':
317
        s[len] = 0;
318
        return;
319
      case 0x08: /* BKSPC */
320
        if (len > 0) {
321
          len--;
1544 mateusz.vi 322
          mdr_cout_char(screenlastrow, x + len, ' ', SCHEME_STBAR3);
1345 mateusz.vi 323
        }
324
        break;
325
      default:
326
        if ((k <= 0xff) && (k >= ' ') && (len < maxlen)) {
1544 mateusz.vi 327
          mdr_cout_char(screenlastrow, x + len, k, SCHEME_STBAR3);
1345 mateusz.vi 328
          s[len++] = k;
329
        }
1332 mateusz.vi 330
    }
1345 mateusz.vi 331
  }
1332 mateusz.vi 332
 
333
}
334
 
335
 
1324 mateusz.vi 336
/* append a nul-terminated string to line at cursor position */
1550 mateusz.vi 337
static int line_append(struct file *f, const char *buf, unsigned short len) {
1427 mateusz.vi 338
  if (sizeof(struct line) + f->cursor->len + len < len) goto ERR; /* overflow check */
339
  if (curline_resize(f, f->cursor->len + len) != 0) goto ERR;
340
 
1568 mateusz.vi 341
  fmemmove(f->cursor->payload + f->cursor->len, buf, len);
1324 mateusz.vi 342
  f->cursor->len += len;
343
 
344
  return(0);
1427 mateusz.vi 345
  ERR:
346
  return(-1);
1324 mateusz.vi 347
}
348
 
349
 
1315 mateusz.vi 350
static void db_rewind(struct file *db) {
1275 mateusz.vi 351
  if (db->cursor == NULL) return;
352
  while (db->cursor->prev) db->cursor = db->cursor->prev;
1328 mateusz.vi 353
  db->curline = 0;
1275 mateusz.vi 354
}
355
 
356
 
1541 mateusz.vi 357
/* saves db file, resets db->modflag on success and overwrites the filename
358
 * field if saveas is not NULL */
359
static int savefile(struct file *db, const char *saveas) {
1539 mateusz.vi 360
  int fd = 0;
361
  const struct line far *l;
1556 mateusz.vi 362
  unsigned short bytes;
1539 mateusz.vi 363
  unsigned char eollen = 2;
364
  const unsigned char *eolbuf = "\r\n";
365
  int errflag = 0;
366
 
367
  /* if filename not overloaded then use the fname in db */
368
  if (saveas == NULL) saveas = db->fname;
369
 
1776 mateusz.vi 370
  fd = dos_fopen(saveas, 0x3C /* create or truncate file */);
371
  if (fd == 0) return(-1);
1539 mateusz.vi 372
 
373
  l = db->cursor;
374
  while (l->prev) l = l->prev;
375
 
376
  /* preset line terminators */
377
  if (db->lfonly) {
378
    eolbuf++;
379
    eollen--;
380
  }
381
 
382
  while (l) {
383
    /* do not write the last empty line, it is only useful for edition */
384
    if (l->len != 0) {
1580 mateusz.vi 385
      errflag |= dos_fwrite(fd, l->len, &bytes, l->payload);
1539 mateusz.vi 386
    } else if (l->next == NULL) {
387
      break;
388
    }
1580 mateusz.vi 389
    errflag |= dos_fwrite(fd, eollen, &bytes, eolbuf);
1539 mateusz.vi 390
    l = l->next;
391
  }
392
 
1580 mateusz.vi 393
  errflag |= dos_fclose(fd);
1539 mateusz.vi 394
 
1541 mateusz.vi 395
  /* did it all work? */
396
  if (errflag == 0) {
397
    db->modflag = 0;
398
    if (saveas != db->fname) mdr_dos_truename(db->fname, saveas);
399
  }
400
 
1539 mateusz.vi 401
  return(errflag);
402
}
403
 
404
 
1549 mateusz.vi 405
static void ui_statusbar(const struct file *db) {
1356 mateusz.vi 406
  const char *s = svarlang_strid(0); /* ESC=MENU */
1348 mateusz.vi 407
  unsigned short helpcol = screenw - strlen(s);
1440 mateusz.vi 408
  unsigned short col;
1275 mateusz.vi 409
 
1425 mateusz.vi 410
  /* slot number (guaranteed to be 0-9) */
1355 mateusz.vi 411
  {
412
    char slot[4] = "#00";
1549 mateusz.vi 413
    if (db->slotid == 9) {
1425 mateusz.vi 414
      slot[1] = '1';
415
    } else {
1549 mateusz.vi 416
      slot[2] += db->slotid + 1;
1425 mateusz.vi 417
    }
1427 mateusz.vi 418
    mdr_cout_str(screenlastrow, 0, slot, SCHEME_STBAR2, 3);
1355 mateusz.vi 419
  }
1313 mateusz.vi 420
 
1355 mateusz.vi 421
  /* fill rest of status bar with background */
1427 mateusz.vi 422
  mdr_cout_char_rep(screenlastrow, 3, ' ', SCHEME_STBAR1, helpcol - 3);
1355 mateusz.vi 423
 
1313 mateusz.vi 424
  /* eol type */
1330 mateusz.vi 425
  {
426
    const char *eoltype = "CRLF";
1353 mateusz.vi 427
    if (db->lfonly) eoltype += 2;
1440 mateusz.vi 428
    mdr_cout_str(screenlastrow, helpcol - 5, eoltype, SCHEME_STBAR1, 5);
1275 mateusz.vi 429
  }
1313 mateusz.vi 430
 
1438 mateusz.vi 431
  /* line numbers */
1436 mateusz.vi 432
  {
1438 mateusz.vi 433
    unsigned short x;
434
    unsigned char count = 0;
1440 mateusz.vi 435
    col = helpcol - 7;
1438 mateusz.vi 436
 
1439 mateusz.vi 437
    x = db->totlines;
1438 mateusz.vi 438
    AGAIN:
1436 mateusz.vi 439
    do {
1439 mateusz.vi 440
      mdr_cout_char(screenlastrow, col--, '0' + (x % 10), SCHEME_STBAR1);
1436 mateusz.vi 441
      x /= 10;
442
    } while (x);
1438 mateusz.vi 443
    /* redo same exercise, but printing the current line now */
444
    if (count == 0) {
445
      count = 1;
1439 mateusz.vi 446
      mdr_cout_char(screenlastrow, col--, '/', SCHEME_STBAR1);
1438 mateusz.vi 447
      x = 1 + db->curline;
448
      goto AGAIN;
449
    }
1440 mateusz.vi 450
  }
1438 mateusz.vi 451
 
1440 mateusz.vi 452
  /* filename and modflag */
453
  {
454
    const char *fn;
455
    unsigned short x;
456
    unsigned short maxfnlen = col - 6;
457
    if (db->fname[0] == 0) {
458
      fn = svarlang_str(0, 1); /* "UNTITLED" */
459
    } else {
460
      /* display filename up to maxfnlen chars */
461
      fn = db->fname;
462
      x = strlen(fn);
463
      if (x > maxfnlen) fn += x - maxfnlen;
464
    }
465
    x = mdr_cout_str(screenlastrow, 4, fn, SCHEME_STBAR1, maxfnlen);
466
    if (db->modflag) mdr_cout_char(screenlastrow, 5 + x, '!', SCHEME_STBAR2);
1436 mateusz.vi 467
  }
468
 
1427 mateusz.vi 469
  mdr_cout_str(screenlastrow, helpcol, s, SCHEME_STBAR2, 40);
1275 mateusz.vi 470
}
471
 
472
 
1547 mateusz.vi 473
static void ui_msg(unsigned short msgid1, unsigned short msgid2, unsigned short msgid3, unsigned char attr) {
474
  unsigned short x, y, maxmsglen, i;
475
  unsigned short msgcount = 1;
476
  const char *msg[3];
1331 mateusz.vi 477
 
1547 mateusz.vi 478
  msg[0] = svarlang_strid(msgid1);
479
 
480
  if (msgid2 != 0) {
481
    msgcount = 2;
482
    msg[1] = svarlang_strid(msgid2);
1331 mateusz.vi 483
  }
1547 mateusz.vi 484
  if (msgid3 != 0) {
485
    msgcount = 3;
486
    msg[2] = svarlang_strid(msgid3);
487
  }
1331 mateusz.vi 488
 
1547 mateusz.vi 489
  /* find longest msg */
490
  maxmsglen = 0;
491
  for (i = 0; i < msgcount; i++) {
492
    y = strlen(msg[i]);
493
    if (y > maxmsglen) maxmsglen = y;
494
  }
495
 
1331 mateusz.vi 496
  y = (screenh - 6) >> 1;
1547 mateusz.vi 497
  x = (screenw - maxmsglen - 3) >> 1;
498
  for (i = y+1+msgcount; i >= y; i--) mdr_cout_char_rep(i, x, ' ', attr, maxmsglen + 2);
1331 mateusz.vi 499
  x++;
1345 mateusz.vi 500
 
1547 mateusz.vi 501
  for (i = 0; i < msgcount; i++) {
502
    mdr_cout_str(y+1+i, x, msg[i], attr, maxmsglen);
503
  }
1312 mateusz.vi 504
 
1327 mateusz.vi 505
  if (uidirty.from > y) uidirty.from = y;
1331 mateusz.vi 506
  if (uidirty.to < y+4) uidirty.to = y+4;
1312 mateusz.vi 507
}
508
 
509
 
1539 mateusz.vi 510
/* returns 0 if operation may proceed, non-zero to cancel */
1541 mateusz.vi 511
static unsigned char ui_confirm_if_unsaved(struct file *db) {
1539 mateusz.vi 512
  int k;
513
 
1342 mateusz.vi 514
  if (db->modflag == 0) return(0);
515
 
1355 mateusz.vi 516
  mdr_cout_cursor_hide();
517
 
1539 mateusz.vi 518
  /* if file has been modified then ask for confirmation:
519
   * ENTER        : agree to data loss
520
   * SPACE        : SAVE file before quit (only if valid filename present)
521
   * anything else: ABORT */
1547 mateusz.vi 522
  ui_msg(4, 5, (db->fname[0])?9:8, SCHEME_MSG);
1342 mateusz.vi 523
 
1539 mateusz.vi 524
  k = mdr_dos_getkey2();
1355 mateusz.vi 525
  mdr_cout_cursor_show();
526
 
1539 mateusz.vi 527
  /* ENTER = agree to loose unsaved data */
528
  if (k == '\r') return(0);
529
 
530
  /* SPACE = save file and continue */
531
  if ((k == ' ') && (db->fname[0] != 0) && (savefile(db, NULL) == 0)) return(0);
532
 
533
  /* any other key = cancel operation */
534
  return(1);
1342 mateusz.vi 535
}
536
 
537
 
1327 mateusz.vi 538
static void ui_refresh(const struct file *db) {
1318 mateusz.vi 539
  unsigned char x;
1310 mateusz.vi 540
  const struct line far *l;
1316 mateusz.vi 541
  unsigned char y = db->cursorposy;
1275 mateusz.vi 542
 
1392 mateusz.vi 543
  /* quit early if nothing to refresh */
544
  if (uidirty.from == 0xff) return;
545
 
1288 mateusz.vi 546
#ifdef DBG_REFRESH
1282 mateusz.vi 547
  static char m = 'a';
548
  m++;
549
  if (m > 'z') m = 'a';
1288 mateusz.vi 550
#endif
1282 mateusz.vi 551
 
1310 mateusz.vi 552
  /* rewind cursor line to first line that needs redrawing */
1327 mateusz.vi 553
  for (l = db->cursor; y > uidirty.from; y--) l = l->prev;
1282 mateusz.vi 554
 
1310 mateusz.vi 555
  /* iterate over lines and redraw whatever needs to be redrawn */
556
  for (; l != NULL; l = l->next, y++) {
557
 
558
    /* skip lines that do not need to be refreshed */
1327 mateusz.vi 559
    if (y < uidirty.from) continue;
560
    if (y > uidirty.to) break;
1282 mateusz.vi 561
 
1318 mateusz.vi 562
    x = 0;
1275 mateusz.vi 563
    if (db->xoffset < l->len) {
1318 mateusz.vi 564
      unsigned char i, limit;
565
      if (l->len - db->xoffset < screenw) {
566
        limit = l->len;
567
      } else {
1427 mateusz.vi 568
        limit = db->xoffset + screenlastcol;
1318 mateusz.vi 569
      }
1333 mateusz.vi 570
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], SCHEME_TEXT);
1275 mateusz.vi 571
    }
1282 mateusz.vi 572
 
1318 mateusz.vi 573
    /* write empty spaces until end of line */
1427 mateusz.vi 574
    if (x < screenlastcol) mdr_cout_char_rep(y, x, ' ', SCHEME_TEXT, screenlastcol - x);
1318 mateusz.vi 575
 
1288 mateusz.vi 576
#ifdef DBG_REFRESH
1333 mateusz.vi 577
    mdr_cout_char(y, 0, m, SCHEME_STBAR1);
1288 mateusz.vi 578
#endif
1282 mateusz.vi 579
 
1275 mateusz.vi 580
    if (y == screenh - 2) break;
581
  }
1310 mateusz.vi 582
 
1319 mateusz.vi 583
  /* fill all lines below if empty (and they need to be redrawn) */
584
  if (l == NULL) {
1427 mateusz.vi 585
    while ((y < screenlastrow) && (y < uidirty.to)) {
586
      mdr_cout_char_rep(y++, 0, ' ', SCHEME_TEXT, screenlastcol);
1319 mateusz.vi 587
    }
1318 mateusz.vi 588
  }
1328 mateusz.vi 589
 
590
  /* scroll bar */
1427 mateusz.vi 591
  for (y = 0; y < screenlastrow; y++) {
592
    mdr_cout_char(y, screenlastcol, SCROLL_CURSOR, SCHEME_SCROLL);
1328 mateusz.vi 593
  }
594
 
595
  /* scroll cursor */
596
  if (db->totlines >= screenh) {
597
    unsigned short topline = db->curline - db->cursorposy;
598
    unsigned short col;
599
    unsigned short totlines = db->totlines - screenh + 1;
600
    if (db->totlines - screenh > screenh) {
1427 mateusz.vi 601
      col = topline / (totlines / screenlastrow);
1328 mateusz.vi 602
    } else {
1427 mateusz.vi 603
      col = topline * screenlastrow / totlines;
1328 mateusz.vi 604
    }
1427 mateusz.vi 605
    if (col >= screenlastrow) col = screenh - 2;
606
    mdr_cout_char(col, screenlastcol, ' ', SCHEME_SCROLL);
1328 mateusz.vi 607
  }
1392 mateusz.vi 608
 
609
  /* clear out the dirty flag */
610
  uidirty.from = 0xff;
1275 mateusz.vi 611
}
612
 
613
 
1327 mateusz.vi 614
static void check_cursor_not_after_eol(struct file *db) {
1316 mateusz.vi 615
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
1275 mateusz.vi 616
 
617
  if (db->cursor->len < db->xoffset) {
1316 mateusz.vi 618
    db->cursorposx = 0;
1275 mateusz.vi 619
    db->xoffset = db->cursor->len;
1327 mateusz.vi 620
    uidirty.from = 0;
621
    uidirty.to = 0xff;
1275 mateusz.vi 622
  } else {
1316 mateusz.vi 623
    db->cursorposx = db->cursor->len - db->xoffset;
1275 mateusz.vi 624
  }
625
}
626
 
627
 
1327 mateusz.vi 628
static void cursor_up(struct file *db) {
1422 mateusz.vi 629
  if (db->cursor->prev == NULL) return;
630
 
631
  db->curline -= 1;
632
  db->cursor = db->cursor->prev;
633
  if (db->cursorposy == 0) {
634
    uidirty.from = 0;
635
    uidirty.to = 0xff;
636
  } else {
637
    db->cursorposy -= 1;
1275 mateusz.vi 638
  }
639
}
640
 
641
 
1327 mateusz.vi 642
static void cursor_eol(struct file *db) {
1275 mateusz.vi 643
  /* adjust xoffset to make sure eol is visible on screen */
1282 mateusz.vi 644
  if (db->xoffset > db->cursor->len) {
645
    db->xoffset = db->cursor->len - 1;
1327 mateusz.vi 646
    uidirty.from = 0;
647
    uidirty.to = 0xff;
1282 mateusz.vi 648
  }
649
 
1427 mateusz.vi 650
  if (db->xoffset + screenlastcol <= db->cursor->len) {
1282 mateusz.vi 651
    db->xoffset = db->cursor->len - screenw + 2;
1327 mateusz.vi 652
    uidirty.from = 0;
653
    uidirty.to = 0xff;
1282 mateusz.vi 654
  }
1316 mateusz.vi 655
  db->cursorposx = db->cursor->len - db->xoffset;
1275 mateusz.vi 656
}
657
 
658
 
1327 mateusz.vi 659
static void cursor_down(struct file *db) {
1422 mateusz.vi 660
  if (db->cursor->next == NULL) return;
661
 
662
  db->curline += 1;
663
  db->cursor = db->cursor->next;
664
  if (db->cursorposy < screenh - 2) {
665
    db->cursorposy += 1;
666
  } else {
667
    uidirty.from = 0;
668
    uidirty.to = 0xff;
1276 mateusz.vi 669
  }
670
}
671
 
672
 
1327 mateusz.vi 673
static void cursor_home(struct file *db) {
1316 mateusz.vi 674
  db->cursorposx = 0;
1282 mateusz.vi 675
  if (db->xoffset != 0) {
676
    db->xoffset = 0;
1327 mateusz.vi 677
    uidirty.from = 0;
678
    uidirty.to = 0xff;
1282 mateusz.vi 679
  }
1276 mateusz.vi 680
}
681
 
682
 
1327 mateusz.vi 683
static void cursor_right(struct file *db) {
1316 mateusz.vi 684
  if (db->cursor->len > db->xoffset + db->cursorposx) {
685
    if (db->cursorposx < screenw - 2) {
686
      db->cursorposx += 1;
1308 mateusz.vi 687
    } else {
688
      db->xoffset += 1;
1327 mateusz.vi 689
      uidirty.from = 0;
690
      uidirty.to = 0xff;
1308 mateusz.vi 691
    }
1357 mateusz.vi 692
  } else if (db->cursor->next != NULL) { /* jump to start of next line */
1327 mateusz.vi 693
    cursor_down(db);
694
    cursor_home(db);
1308 mateusz.vi 695
  }
696
}
697
 
698
 
1760 mateusz.vi 699
static void cursor_left(struct file *db) {
700
  if (db->cursorposx > 0) {
701
    db->cursorposx -= 1;
702
  } else if (db->xoffset > 0) {
703
    db->xoffset -= 1;
704
    uidirty.from = 0;
705
    uidirty.to = 0xff;
706
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
707
    cursor_up(db);
708
    cursor_eol(db);
709
  }
710
}
711
 
712
 
1327 mateusz.vi 713
static void del(struct file *db) {
1316 mateusz.vi 714
  if (db->cursorposx + db->xoffset < db->cursor->len) {
1568 mateusz.vi 715
    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 716
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
1327 mateusz.vi 717
    uidirty.from = db->cursorposy;
718
    uidirty.to = db->cursorposy;
1393 mateusz.vi 719
    db->modflag = 1;
1292 mateusz.vi 720
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
721
    struct line far *nextline = db->cursor->next;
722
    if (db->cursor->next->len > 0) {
1340 mateusz.vi 723
      if (curline_resize(db, db->cursor->len + db->cursor->next->len + 1) == 0) {
1568 mateusz.vi 724
        fmemmove(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
1292 mateusz.vi 725
        db->cursor->len += db->cursor->next->len;
726
      }
727
    }
1340 mateusz.vi 728
 
1292 mateusz.vi 729
    db->cursor->next = db->cursor->next->next;
730
    db->cursor->next->prev = db->cursor;
1340 mateusz.vi 731
 
1339 mateusz.vi 732
    line_free(nextline);
1327 mateusz.vi 733
    uidirty.from = db->cursorposy;
734
    uidirty.to = 0xff;
1328 mateusz.vi 735
    db->totlines -= 1;
1393 mateusz.vi 736
    db->modflag = 1;
1292 mateusz.vi 737
  }
738
}
739
 
740
 
1327 mateusz.vi 741
static void bkspc(struct file *db) {
1302 mateusz.vi 742
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
1421 mateusz.vi 743
  if ((db->cursorposx + db->xoffset == 0) && (db->cursor->prev == NULL)) return;
1302 mateusz.vi 744
 
1327 mateusz.vi 745
  cursor_left(db);
746
  del(db);
1302 mateusz.vi 747
}
748
 
749
 
1450 mateusz.vi 750
#define LOADFILE_FILENOTFOUND 2
751
 
1346 mateusz.vi 752
/* returns 0 on success, 1 on file not found, 2 on other error */
1449 mateusz.vi 753
static unsigned char loadfile(struct file *db, const char *fname) {
1324 mateusz.vi 754
  char *buffptr;
1556 mateusz.vi 755
  unsigned short len, llen;
756
  unsigned short fd;
1324 mateusz.vi 757
  unsigned char eolfound;
1449 mateusz.vi 758
  unsigned char err = 0;
1287 mateusz.vi 759
 
1355 mateusz.vi 760
  /* free the entire linked list of lines */
761
  db_rewind(db);
762
  while (db->cursor) {
763
    struct line far *victim;
764
    victim = db->cursor;
765
    db->cursor = db->cursor->next;
766
    line_free(victim);
767
  }
1321 mateusz.vi 768
 
1551 mateusz.vi 769
  /* zero out the struct (take care to preserve the id of the slot, though) */
770
  {
771
    unsigned char slotid = db->slotid;
772
    bzero(db, sizeof(struct file));
773
    db->slotid = slotid;
774
  }
1355 mateusz.vi 775
 
1448 mateusz.vi 776
  /* start by adding an empty line */
777
  if (line_add(db, NULL, 0) != 0) return(2);
778
 
1418 mateusz.vi 779
  if (fname == NULL) goto SKIPLOADING;
1321 mateusz.vi 780
 
1543 mateusz.vi 781
  /* make the filename canonical (DOS 3+ only, on earlier versions it just copies the filename) */
782
  mdr_dos_truename(db->fname, fname);
1355 mateusz.vi 783
 
1565 mateusz.vi 784
  /* fopen file */
1776 mateusz.vi 785
  fd = dos_fopen(fname, 0x3D /* open for read */);
786
  if (fd == 0) err = LOADFILE_FILENOTFOUND;
1565 mateusz.vi 787
 
1450 mateusz.vi 788
  if (err != 0) goto SKIPLOADING;
1287 mateusz.vi 789
 
1313 mateusz.vi 790
  db->lfonly = 1;
791
 
1324 mateusz.vi 792
  for (eolfound = 0;;) {
793
    unsigned short consumedbytes;
794
 
1580 mateusz.vi 795
    if ((dos_fread(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
1324 mateusz.vi 796
    buffptr = buff;
797
 
798
    FINDLINE:
799
 
1361 mateusz.vi 800
    /* look for nearest \n (also expand tabs) */
1324 mateusz.vi 801
    for (consumedbytes = 0;; consumedbytes++) {
1361 mateusz.vi 802
 
1773 mateusz.vi 803
      if ((buffptr[consumedbytes] == '\t') && (!glob_tablessmode)) {
1361 mateusz.vi 804
        llen = consumedbytes;
805
        break;
806
      }
807
 
1324 mateusz.vi 808
      if (consumedbytes == len) {
809
        llen = consumedbytes;
810
        break;
1323 mateusz.vi 811
      }
1324 mateusz.vi 812
      if (buffptr[consumedbytes] == '\r') {
813
        llen = consumedbytes;
814
        consumedbytes++;
815
        db->lfonly = 0;
1320 mateusz.vi 816
        break;
817
      }
1324 mateusz.vi 818
      if (buffptr[consumedbytes] == '\n') {
819
        eolfound = 1;
820
        llen = consumedbytes;
821
        consumedbytes++;
822
        break;
823
      }
1287 mateusz.vi 824
    }
1324 mateusz.vi 825
 
826
    /* consumedbytes is the amount of bytes processed from buffptr,
827
     * llen is the length of line's payload (without its line terminator) */
828
 
829
    /* append content, if line is non-empty */
830
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
1339 mateusz.vi 831
      goto IOERR;
1287 mateusz.vi 832
    }
833
 
1419 mateusz.vi 834
    /* allocate the next line if current line ended */
1324 mateusz.vi 835
    if (eolfound) {
1328 mateusz.vi 836
      if (line_add(db, NULL, 0) != 0) {
1339 mateusz.vi 837
        goto IOERR;
1324 mateusz.vi 838
      }
839
      eolfound = 0;
840
    }
1287 mateusz.vi 841
 
1759 mateusz.vi 842
    /* tab: append enough spaces to land on next 8-chars boundary */
1412 mateusz.vi 843
    if ((consumedbytes < len) && (buffptr[consumedbytes] == '\t') && (glob_tablessmode == 0)) {
1361 mateusz.vi 844
      consumedbytes++;
1759 mateusz.vi 845
      if (line_append(db, "        ", 8 - (db->cursor->len & 7)) != 0) {
1361 mateusz.vi 846
        goto IOERR;
847
      }
848
    }
849
 
1324 mateusz.vi 850
    /* anything left? process the buffer leftover again */
851
    if (consumedbytes < len) {
852
      len -= consumedbytes;
853
      buffptr += consumedbytes;
854
      goto FINDLINE;
855
    }
856
 
857
  }
858
 
1580 mateusz.vi 859
  dos_fclose(fd);
1287 mateusz.vi 860
 
1321 mateusz.vi 861
  SKIPLOADING:
862
 
1448 mateusz.vi 863
  /* rewind cursor to top of file because it has been used by line_add() */
1339 mateusz.vi 864
  db_rewind(db);
1320 mateusz.vi 865
 
1448 mateusz.vi 866
  return(err);
1339 mateusz.vi 867
 
868
  IOERR:
1580 mateusz.vi 869
  dos_fclose(fd);
1450 mateusz.vi 870
  return(1);
1287 mateusz.vi 871
}
872
 
873
 
1356 mateusz.vi 874
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
875
 * of argc and argv, saves some 330 bytes of binary size
876
 * returns non-zero on error */
877
static int parseargv(struct file *dbarr) {
878
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
879
  unsigned short count = 0;
880
  char *arg;
881
  unsigned short lastarg = 0;
1449 mateusz.vi 882
  unsigned char err;
1356 mateusz.vi 883
 
884
  while (!lastarg) {
885
    /* jump to nearest arg */
886
    while (*tail == ' ') {
887
      *tail = 0;
888
      tail++;
889
    }
890
 
891
    if (*tail == '\r') {
892
      *tail = 0;
893
      break;
894
    }
895
 
896
    arg = tail;
897
 
898
    /* jump to next delimiter */
899
    while ((*tail != ' ') && (*tail != '\r')) tail++;
900
 
901
    /* if \r then remember this is the last arg */
902
    if (*tail == '\r') lastarg = 1;
903
 
904
    *tail = 0;
905
    tail++;
906
 
907
    /* look at the arg now */
908
    if (*arg == '/') {
1412 mateusz.vi 909
      if (arg[1] == 't') { /* /t = do not expand tabs */
910
        glob_tablessmode = 1;
911
 
912
      } else if (arg[1] == 'm') { /* /m = force mono mode */
913
        glob_monomode = 1;
914
 
915
      } else {  /* help screen */
1419 mateusz.vi 916
        mdr_coutraw_str(svarlang_str(1,3)); /* Sved, the SvarDOS editor */
1412 mateusz.vi 917
        mdr_coutraw_str(" [");
1419 mateusz.vi 918
        mdr_coutraw_str(svarlang_str(1,4)); /* ver */
1760 mateusz.vi 919
        mdr_coutraw_str(" " PVER "]\r\n"
920
                         "Copyright (C) " PDATE " Mateusz Viste\n"
921
                         "\r\n"
922
                         "sved [/m] [/t] ");
1412 mateusz.vi 923
        mdr_coutraw_puts(svarlang_str(1,1)); /* args syntax */
924
        mdr_coutraw_crlf();
925
        mdr_coutraw_puts(svarlang_str(1,10)); /* /m */
926
        mdr_coutraw_puts(svarlang_str(1,11)); /* /t */
927
        return(-1);
1387 mateusz.vi 928
      }
1412 mateusz.vi 929
      continue;
1356 mateusz.vi 930
    }
931
 
932
    /* looks to be a filename */
933
    if (count == 10) {
1420 mateusz.vi 934
      mdr_coutraw_puts(svarlang_str(0,12)); /* too many files */
935
      return(-1);
1356 mateusz.vi 936
    }
937
 
938
    /* try loading it */
1387 mateusz.vi 939
    mdr_coutraw_str(svarlang_str(1,2));
940
    mdr_coutraw_char(' ');
1356 mateusz.vi 941
    mdr_coutraw_puts(arg);
942
    err = loadfile(&(dbarr[count]), arg);
943
    if (err) {
1450 mateusz.vi 944
      if (err == LOADFILE_FILENOTFOUND) { /* file not found */
1396 mateusz.vi 945
        if ((count == 0) && (lastarg != 0)) { /* a 'file not found' is fine if only one file was given */
946
          err = 0;
947
        } else {
948
          err = 11;
949
        }
1356 mateusz.vi 950
      } else { /* general error */
951
        err = 10;
952
      }
1396 mateusz.vi 953
      if (err) {
954
        mdr_coutraw_puts(svarlang_str(0,err));
955
        return(-1);
956
      }
1356 mateusz.vi 957
    }
958
    count++;
959
  }
960
 
961
  return(0);
962
}
963
 
964
 
1327 mateusz.vi 965
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
1340 mateusz.vi 966
  if (curline_resize(db, db->cursor->len + len) == 0) {
1317 mateusz.vi 967
    unsigned short off = db->xoffset + db->cursorposx;
1393 mateusz.vi 968
    db->modflag = 1;
1568 mateusz.vi 969
    fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
1317 mateusz.vi 970
    db->cursor->len += len;
1327 mateusz.vi 971
    uidirty.from = db->cursorposy;
972
    uidirty.to = db->cursorposy;
1317 mateusz.vi 973
    while (len--) {
974
      db->cursor->payload[off++] = *databuf;
975
      databuf++;
1327 mateusz.vi 976
      cursor_right(db);
1317 mateusz.vi 977
    }
978
  }
979
}
980
 
981
 
1343 mateusz.vi 982
/* recompute db->curline by counting nodes in linked list */
983
static void recompute_curline(struct file *db) {
984
  const struct line far *l = db->cursor;
985
 
986
  db->curline = 0;
987
  while (l->prev != NULL) {
988
    db->curline += 1;
989
    l = l->prev;
990
  }
991
}
992
 
993
 
1354 mateusz.vi 994
enum MENU_ACTION {
1393 mateusz.vi 995
  MENU_NONE   = 0,
996
  MENU_OPEN   = 1,
997
  MENU_SAVE   = 2,
998
  MENU_SAVEAS = 3,
999
  MENU_CLOSE  = 4,
1000
  MENU_CHGEOL = 5,
1418 mateusz.vi 1001
  MENU_QUIT   = 6
1354 mateusz.vi 1002
};
1003
 
1004
static enum MENU_ACTION ui_menu(void) {
1005
  unsigned short i, curchoice, attr, x, slen;
1393 mateusz.vi 1006
  unsigned short xorigin, yorigin;
1354 mateusz.vi 1007
 
1008
  /* find out the longest string */
1009
  slen = 0;
1355 mateusz.vi 1010
  for (i = MENU_OPEN; i <= MENU_QUIT; i++) {
1354 mateusz.vi 1011
    x = strlen(svarlang_str(8, i));
1012
    if (x > slen) slen = x;
1013
  }
1014
 
1393 mateusz.vi 1015
  /* calculate where to draw the menu on screen */
1016
  xorigin = (screenw - (slen + 5)) / 2;
1395 mateusz.vi 1017
  yorigin = (screenh - (MENU_QUIT - MENU_OPEN + 6)) / 2;
1393 mateusz.vi 1018
 
1395 mateusz.vi 1019
  /* */
1020
  uidirty.from = yorigin;
1021
  uidirty.to = 0xff;
1022
  uidirty.statusbar = 1;
1023
 
1024
  /* hide the cursor */
1025
  mdr_cout_cursor_hide();
1026
 
1355 mateusz.vi 1027
  curchoice = MENU_OPEN;
1354 mateusz.vi 1028
  for (;;) {
1029
    /* render menu */
1393 mateusz.vi 1030
    for (i = MENU_NONE; i <= MENU_QUIT + 1; i++) {
1031
      mdr_cout_char_rep(yorigin + i, xorigin, ' ', SCHEME_MENU, slen+4);
1354 mateusz.vi 1032
      if (i == curchoice) {
1033
        attr = SCHEME_MENU_CUR;
1415 mateusz.vi 1034
        mdr_cout_char_rep(yorigin + i, xorigin + 1, ' ', SCHEME_MENU_SEL, slen + 2);
1354 mateusz.vi 1035
      } else {
1036
        attr = SCHEME_MENU;
1037
      }
1416 mateusz.vi 1038
      mdr_cout_str(yorigin + i, xorigin + 2, svarlang_str(8, i), attr, slen);
1354 mateusz.vi 1039
    }
1040
    /* wait for key */
1410 mateusz.vi 1041
    switch (mdr_dos_getkey2()) {
1354 mateusz.vi 1042
      case 0x150: /* down */
1043
        if (curchoice == MENU_QUIT) {
1355 mateusz.vi 1044
          curchoice = MENU_OPEN;
1354 mateusz.vi 1045
        } else {
1046
          curchoice++;
1047
        }
1048
        break;
1049
      case 0x148: /* up */
1355 mateusz.vi 1050
        if (curchoice == MENU_OPEN) {
1354 mateusz.vi 1051
          curchoice = MENU_QUIT;
1052
        } else {
1053
          curchoice--;
1054
        }
1055
        break;
1395 mateusz.vi 1056
      default:
1057
        curchoice = MENU_NONE;
1058
        /* FALLTHRU */
1059
      case '\r': /* ENTER */
1060
        mdr_cout_cursor_show();
1061
        return(curchoice);
1354 mateusz.vi 1062
    }
1063
  }
1064
}
1065
 
1066
 
1760 mateusz.vi 1067
static void new_slot_selected(const struct file *dbarr) {
1355 mateusz.vi 1068
  uidirty.from = 0;
1069
  uidirty.to = 0xff;
1070
  uidirty.statusbar = 1;
1071
 
1415 mateusz.vi 1072
  /* force redraw now, because the main() routine might not if this is exit
1073
   * time and we want to show the user which file has unsaved changes */
1549 mateusz.vi 1074
  ui_statusbar(dbarr);
1355 mateusz.vi 1075
  ui_refresh(dbarr);
1076
}
1077
 
1078
 
1347 mateusz.vi 1079
/* main returns nothing, ie. sved always exits with a zero exit code
1080
 * (this saves 20 bytes of executable footprint) */
1081
void main(void) {
1355 mateusz.vi 1082
  static struct file dbarr[10];
1083
  struct file *db = dbarr; /* visible file is the first slot by default */
1548 mateusz.vi 1084
  static struct line far *clipboard;
1538 mateusz.vi 1085
  static unsigned char original_breakflag;
1275 mateusz.vi 1086
 
1418 mateusz.vi 1087
  { /* load NLS resource */
1364 mateusz.vi 1088
    unsigned short i = 0;
1089
    const char far *selfptr;
1548 mateusz.vi 1090
    char lang[8];
1364 mateusz.vi 1091
    selfptr = mdr_dos_selfexe();
1092
    if (selfptr != NULL) {
1093
      do {
1548 mateusz.vi 1094
        buff[i] = selfptr[i];
1095
      } while (buff[i++] != 0);
1096
      svarlang_autoload_exepath(buff, mdr_dos_getenv(lang, "LANG", sizeof(lang)));
1364 mateusz.vi 1097
    }
1307 mateusz.vi 1098
  }
1282 mateusz.vi 1099
 
1355 mateusz.vi 1100
  /* preload all slots with empty files */
1549 mateusz.vi 1101
  {
1102
    unsigned short i;
1103
    for (i = 0; i < 10; i++) {
1104
      loadfile(&(dbarr[i]), NULL);
1105
      dbarr[i].slotid = i;
1106
    }
1355 mateusz.vi 1107
  }
1108
 
1356 mateusz.vi 1109
  /* parse argv (and load files, if any passed on) */
1110
  if (parseargv(dbarr) != 0) return;
1282 mateusz.vi 1111
 
1412 mateusz.vi 1112
  if ((mdr_cout_init(&screenw, &screenh) != 0) && (glob_monomode == 0)) {
1333 mateusz.vi 1113
    /* load color scheme if mdr_cout_init returns a color flag */
1114
    SCHEME_TEXT = 0x17;
1354 mateusz.vi 1115
    SCHEME_MENU = 0x70;
1388 mateusz.vi 1116
    SCHEME_MENU_CUR = 0x6f;
1117
    SCHEME_MENU_SEL = 0x66;
1333 mateusz.vi 1118
    SCHEME_STBAR1 = 0x70;
1119
    SCHEME_STBAR2 = 0x78;
1392 mateusz.vi 1120
    SCHEME_STBAR3 = 0x3f;
1333 mateusz.vi 1121
    SCHEME_SCROLL = 0x70;
1388 mateusz.vi 1122
    SCHEME_MSG = 0x6f;
1333 mateusz.vi 1123
    SCHEME_ERR = 0x4f;
1124
  }
1427 mateusz.vi 1125
  screenlastrow = screenh - 1;
1126
  screenlastcol = screenw - 1;
1275 mateusz.vi 1127
 
1414 mateusz.vi 1128
  /* instruct DOS to stop detecting CTRL+C because user needs it for
1129
   * copy/paste operations. also remember the original status of the BREAK
1130
   * flag so I can restore it as it was before quitting. */
1131
  original_breakflag = mdr_dos_ctrlc_disable();
1410 mateusz.vi 1132
 
1275 mateusz.vi 1133
  for (;;) {
1134
    int k;
1135
 
1362 mateusz.vi 1136
    /* add an extra empty line if cursor is on last line and this line is not empty */
1137
    if ((db->cursor->next == NULL) && (db->cursor->len != 0)) {
1138
      if (line_add(db, NULL, 0) == 0) {
1139
        db->cursor = db->cursor->prev; /* line_add() changes the cursor pointer */
1437 mateusz.vi 1140
        db->curline -= 1;
1362 mateusz.vi 1141
      }
1142
    }
1143
 
1327 mateusz.vi 1144
    check_cursor_not_after_eol(db);
1320 mateusz.vi 1145
    mdr_cout_locate(db->cursorposy, db->cursorposx);
1275 mateusz.vi 1146
 
1392 mateusz.vi 1147
    ui_refresh(db);
1148
 
1436 mateusz.vi 1149
    if ((uidirty.statusbar != 0) || (db->modflagprev != db->modflag) || (db->curline_prev != db->curline)) {
1549 mateusz.vi 1150
      ui_statusbar(db);
1342 mateusz.vi 1151
      uidirty.statusbar = 0;
1355 mateusz.vi 1152
      db->modflagprev = db->modflag;
1436 mateusz.vi 1153
      db->curline_prev = db->curline;
1342 mateusz.vi 1154
    }
1328 mateusz.vi 1155
#ifdef DBG_LINENUM
1156
      {
1157
        char ddd[10];
1158
        db->curline += 1;
1159
        ddd[0] = '0' + db->curline / 100;
1160
        ddd[1] = '0' + (db->curline % 100) / 10;
1161
        ddd[2] = '0' + (db->curline % 10);
1162
        db->curline -= 1;
1163
        ddd[3] = '/';
1164
        ddd[4] = '0' + db->totlines / 100;
1165
        ddd[5] = '0' + (db->totlines % 100) / 10;
1166
        ddd[6] = '0' + (db->totlines % 10);
1167
        ddd[7] = 0;
1333 mateusz.vi 1168
        mdr_cout_str(screenh - 1, 40, ddd, SCHEME_STBAR1, sizeof(ddd));
1328 mateusz.vi 1169
      }
1170
#endif
1275 mateusz.vi 1171
 
1410 mateusz.vi 1172
    k = mdr_dos_getkey2();
1282 mateusz.vi 1173
 
1275 mateusz.vi 1174
    if (k == 0x150) { /* down */
1327 mateusz.vi 1175
      cursor_down(db);
1275 mateusz.vi 1176
 
1177
    } else if (k == 0x148) { /* up */
1327 mateusz.vi 1178
      cursor_up(db);
1275 mateusz.vi 1179
 
1180
    } else if (k == 0x14D) { /* right */
1327 mateusz.vi 1181
      cursor_right(db);
1275 mateusz.vi 1182
 
1183
    } else if (k == 0x14B) { /* left */
1327 mateusz.vi 1184
      cursor_left(db);
1275 mateusz.vi 1185
 
1282 mateusz.vi 1186
    } else if (k == 0x149) { /* pgup */
1334 mateusz.vi 1187
      unsigned char dist = db->cursorposy + screenh - 1;
1188
      while ((dist != 0) && (db->cursor->prev != NULL)) {
1189
        db->cursor = db->cursor->prev;
1190
        dist--;
1191
      }
1192
      if (dist != 0) {
1193
        db->cursorposy = 0;
1194
        db->cursorposx = 0;
1195
      } else {
1196
        dist = db->cursorposy;
1197
        while ((dist--) && (db->cursor->next)) db->cursor = db->cursor->next;
1198
      }
1199
      uidirty.from = 0;
1200
      uidirty.to = 0xff;
1343 mateusz.vi 1201
      recompute_curline(db);
1282 mateusz.vi 1202
 
1203
    } else if (k == 0x151) { /* pgdown */
1334 mateusz.vi 1204
      unsigned char dist = screenh + screenh - db->cursorposy - 3;
1205
      while ((dist != 0) && (db->cursor->next != NULL)) {
1206
        db->cursor = db->cursor->next;
1207
        dist--;
1208
      }
1209
      if (dist != 0) {
1210
        db->cursorposy = screenh - 2;
1211
        if (db->totlines <= db->cursorposy) db->cursorposy = db->totlines - 1;
1212
        db->cursorposx = 0;
1213
      } else {
1214
        dist = screenh - 2 - db->cursorposy;
1215
        while ((dist--) && (db->cursor->prev)) db->cursor = db->cursor->prev;
1216
      }
1217
      uidirty.from = 0;
1218
      uidirty.to = 0xff;
1343 mateusz.vi 1219
      recompute_curline(db);
1282 mateusz.vi 1220
 
1221
    } else if (k == 0x147) { /* home */
1327 mateusz.vi 1222
       cursor_home(db);
1282 mateusz.vi 1223
 
1224
    } else if (k == 0x14F) { /* end */
1327 mateusz.vi 1225
       cursor_eol(db);
1282 mateusz.vi 1226
 
1275 mateusz.vi 1227
    } else if (k == 0x1B) { /* ESC */
1354 mateusz.vi 1228
      int quitnow = 0;
1394 mateusz.vi 1229
      char fname[64];
1354 mateusz.vi 1230
      int saveflag = 0;
1393 mateusz.vi 1231
      enum MENU_ACTION ui_action;
1275 mateusz.vi 1232
 
1392 mateusz.vi 1233
      /* collect the exact menu action and clear the screen */
1234
      ui_action = ui_menu();
1235
      ui_refresh(db);
1354 mateusz.vi 1236
 
1392 mateusz.vi 1237
      switch (ui_action) {
1238
 
1354 mateusz.vi 1239
        case MENU_NONE:
1240
          break;
1241
 
1242
        case MENU_OPEN:
1243
          /* display a warning if unsaved changes are pending */
1547 mateusz.vi 1244
          if (db->modflag != 0) ui_msg(4, 8, 0, SCHEME_MSG);
1354 mateusz.vi 1245
 
1246
          /* ask for filename */
1247
          ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
1248
          if (fname[0] != 0) {
1449 mateusz.vi 1249
            unsigned char err;
1354 mateusz.vi 1250
            err = loadfile(db, fname);
1251
            if (err != 0) {
1450 mateusz.vi 1252
              if (err == LOADFILE_FILENOTFOUND) {
1547 mateusz.vi 1253
                ui_msg(11, 0, 0, SCHEME_ERR); /* file not found */
1354 mateusz.vi 1254
              } else {
1547 mateusz.vi 1255
                ui_msg(10, 0, 0, SCHEME_ERR);  /* ERROR */
1354 mateusz.vi 1256
              }
1257
              mdr_bios_tickswait(44); /* 3s */
1418 mateusz.vi 1258
              loadfile(db, NULL);
1354 mateusz.vi 1259
            }
1260
          }
1393 mateusz.vi 1261
          uidirty.from = 0;
1262
          uidirty.to = 0xff;
1263
          uidirty.statusbar = 1;
1354 mateusz.vi 1264
          break;
1265
 
1266
        case MENU_SAVEAS:
1267
          saveflag = 1;
1268
          /* FALLTHRU */
1269
        case MENU_SAVE:
1270
          if ((saveflag != 0) || (db->fname[0] == 0)) { /* save as... */
1271
            ui_getstring(svarlang_str(0,6), fname, sizeof(fname));
1272
            if (*fname == 0) break;
1273
            saveflag = savefile(db, fname);
1274
          } else {
1275
            saveflag = savefile(db, NULL);
1276
          }
1277
 
1395 mateusz.vi 1278
          mdr_cout_cursor_hide();
1279
 
1354 mateusz.vi 1280
          if (saveflag == 0) {
1547 mateusz.vi 1281
            ui_msg(2, 0, 0, SCHEME_MSG);
1354 mateusz.vi 1282
            mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
1283
          } else {
1547 mateusz.vi 1284
            ui_msg(10, 0, 0, SCHEME_ERR);
1354 mateusz.vi 1285
            mdr_bios_tickswait(36); /* 2s */
1286
          }
1395 mateusz.vi 1287
          mdr_cout_cursor_show();
1354 mateusz.vi 1288
          break;
1289
 
1355 mateusz.vi 1290
        case MENU_CLOSE:
1291
          if (ui_confirm_if_unsaved(db) == 0) {
1418 mateusz.vi 1292
            loadfile(db, NULL);
1355 mateusz.vi 1293
          }
1294
          uidirty.from = 0;
1295
          uidirty.to = 0xff;
1296
          uidirty.statusbar = 1;
1297
          break;
1298
 
1354 mateusz.vi 1299
        case MENU_CHGEOL:
1393 mateusz.vi 1300
          db->modflag = 1;
1354 mateusz.vi 1301
          db->lfonly ^= 1;
1302
          break;
1303
 
1304
        case MENU_QUIT:
1355 mateusz.vi 1305
          quitnow = 1;
1549 mateusz.vi 1306
          {
1307
            unsigned short curfile;
1308
            for (curfile = 0; curfile < 10; curfile++) {
1309
              if (dbarr[curfile].modflag == 0) continue; /* skip unmodified slots */
1760 mateusz.vi 1310
              db = dbarr + curfile;
1311
              new_slot_selected(db);
1441 mateusz.vi 1312
              if (ui_confirm_if_unsaved(db) != 0) {
1313
                quitnow = 0;
1314
                break;
1315
              }
1355 mateusz.vi 1316
            }
1317
          }
1354 mateusz.vi 1318
          break;
1319
      }
1320
 
1321
      if (quitnow) break;
1322
 
1289 mateusz.vi 1323
    } else if (k == 0x0D) { /* ENTER */
1328 mateusz.vi 1324
      unsigned short off = db->xoffset + db->cursorposx;
1289 mateusz.vi 1325
      /* add a new line */
1328 mateusz.vi 1326
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
1393 mateusz.vi 1327
        db->modflag = 1;
1328 mateusz.vi 1328
        db->cursor = db->cursor->prev; /* back to original line */
1427 mateusz.vi 1329
        db->curline -= 1;
1289 mateusz.vi 1330
        /* trim the line above */
1328 mateusz.vi 1331
        db->cursor->len = off;
1289 mateusz.vi 1332
        /* move cursor to the (new) line below */
1328 mateusz.vi 1333
        uidirty.from = db->cursorposy;
1327 mateusz.vi 1334
        uidirty.to = 0xff;
1328 mateusz.vi 1335
        cursor_down(db);
1336
        cursor_home(db);
1289 mateusz.vi 1337
      } else {
1338
        /* ERROR: OUT OF MEMORY */
1339
      }
1340
 
1292 mateusz.vi 1341
    } else if (k == 0x153) {  /* DEL */
1327 mateusz.vi 1342
      del(db);
1292 mateusz.vi 1343
 
1302 mateusz.vi 1344
    } else if (k == 0x008) { /* BKSPC */
1327 mateusz.vi 1345
      bkspc(db);
1302 mateusz.vi 1346
 
1308 mateusz.vi 1347
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
1317 mateusz.vi 1348
      char c = k;
1327 mateusz.vi 1349
      insert_in_line(db, &c, 1);
1308 mateusz.vi 1350
 
1759 mateusz.vi 1351
    } else if (k == 0x009) { /* TAB: fill blanks to next 8-chars boundary */
1412 mateusz.vi 1352
      if (glob_tablessmode == 0) {
1759 mateusz.vi 1353
        insert_in_line(db, "        ", 8 - ((db->xoffset + db->cursorposx) & 7));
1412 mateusz.vi 1354
      } else {
1355
        insert_in_line(db, "\t", 1);
1356
      }
1317 mateusz.vi 1357
 
1355 mateusz.vi 1358
    } else if ((k >= 0x13b) && (k <= 0x144)) { /* F1..F10 */
1760 mateusz.vi 1359
      db = dbarr + (k - 0x13b);
1360
      new_slot_selected(db);
1309 mateusz.vi 1361
 
1325 mateusz.vi 1362
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
1363
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
1364
      for (;;) {
1365
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
1366
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
1327 mateusz.vi 1367
        cursor_right(db);
1325 mateusz.vi 1368
      }
1369
      /* now skip to next non-space or end of file */
1370
      for (;;) {
1327 mateusz.vi 1371
        cursor_right(db);
1325 mateusz.vi 1372
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1373
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
1374
      }
1375
 
1376
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
1327 mateusz.vi 1377
      cursor_left(db);
1325 mateusz.vi 1378
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
1379
      for (;;) {
1380
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
1381
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1327 mateusz.vi 1382
        cursor_left(db);
1325 mateusz.vi 1383
      }
1384
      /* now skip to next space or start of file */
1385
      for (;;) {
1327 mateusz.vi 1386
        cursor_left(db);
1325 mateusz.vi 1387
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
1327 mateusz.vi 1388
          cursor_right(db);
1325 mateusz.vi 1389
          break;
1390
        }
1391
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
1392
      }
1393
 
1410 mateusz.vi 1394
    } else if ((k == 0x003) || (k == 0x018)) { /* CTRL+C or CTRL+X */
1395
      /* free clipboard if anything in it */
1396
      if (clipboard != NULL) line_free(clipboard);
1397
 
1398
      /* copy cursor line to clipboard */
1399
      clipboard = line_calloc(db->cursor->len);
1400
      if (clipboard == NULL) {
1547 mateusz.vi 1401
        ui_msg(10, 0, 0, SCHEME_ERR); /* ERROR */
1410 mateusz.vi 1402
        mdr_bios_tickswait(18); /* 1s */
1403
      } else {
1427 mateusz.vi 1404
        mdr_cout_char_rep(db->cursorposy, 0, ' ', ((SCHEME_TEXT >> 4) | (SCHEME_TEXT << 4)) & 0xff, screenlastcol);
1410 mateusz.vi 1405
        uidirty.from = db->cursorposy;
1406
        uidirty.to = db->cursorposy;
1407
        if (db->cursor->len != 0) {
1568 mateusz.vi 1408
          fmemmove(clipboard->payload, db->cursor->payload, db->cursor->len);
1410 mateusz.vi 1409
          clipboard->len = db->cursor->len;
1410
        }
1411
        mdr_bios_tickswait(2); /* ca 100ms */
1412
 
1413
        /* if this is about cutting the line (CTRL+X) then delete cur line */
1414
        if ((k == 0x018) && ((db->cursor->next != NULL) || (db->cursor->prev != NULL))) {
1415
          if (db->cursor->next) db->cursor->next->prev = db->cursor->prev;
1416
          if (db->cursor->prev) db->cursor->prev->next = db->cursor->next;
1417
          clipboard->prev = db->cursor;
1418
          if (db->cursor->next) {
1419
            db->cursor = db->cursor->next;
1420
          } else {
1421
            cursor_up(db);
1422
          }
1423
          line_free(clipboard->prev);
1423 mateusz.vi 1424
          db->totlines -= 1;
1552 mateusz.vi 1425
          db->modflag = 1;
1410 mateusz.vi 1426
          uidirty.from = 0;
1427
          uidirty.to = 0xff;
1412 mateusz.vi 1428
          recompute_curline(db);
1410 mateusz.vi 1429
        }
1430
      }
1431
 
1420 mateusz.vi 1432
    } else if ((k == 0x016) && (clipboard != NULL)) { /* CTRL+V */
1433
      if (line_add(db, clipboard->payload, clipboard->len) != 0) {
1547 mateusz.vi 1434
        ui_msg(10, 0, 0, SCHEME_ERR); /* ERROR */
1420 mateusz.vi 1435
        mdr_bios_tickswait(18); /* 1s */
1436
      } else {
1437
        /* rewire the linked list so the new line is on top of the previous one */
1438
        clipboard->prev = db->cursor->prev;
1439
        /* remove prev node from list */
1440
        db->cursor->prev = db->cursor->prev->prev;
1441
        if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor;
1442
        /* insert the node after cursor now */
1443
        clipboard->prev->next = db->cursor->next;
1444
        if (db->cursor->next != NULL) db->cursor->next->prev = clipboard->prev;
1445
        clipboard->prev->prev = db->cursor;
1446
        db->cursor->next = clipboard->prev;
1447
        cursor_down(db);
1552 mateusz.vi 1448
        db->modflag = 1;
1410 mateusz.vi 1449
      }
1450
      uidirty.from = 0;
1451
      uidirty.to = 0xff;
1412 mateusz.vi 1452
      recompute_curline(db);
1410 mateusz.vi 1453
 
1333 mateusz.vi 1454
#ifdef DBG_UNHKEYS
1282 mateusz.vi 1455
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
1456
      char buff[4];
1457
      const char *HEX = "0123456789ABCDEF";
1458
      buff[0] = HEX[(k >> 8) & 15];
1459
      buff[1] = HEX[(k >> 4) & 15];
1460
      buff[2] = HEX[k & 15];
1333 mateusz.vi 1461
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", SCHEME_STBAR1, 17);
1462
      mdr_cout_str(screenh - 1, 17, buff, SCHEME_STBAR1, 3);
1410 mateusz.vi 1463
      mdr_dos_getkey2();
1275 mateusz.vi 1464
      break;
1333 mateusz.vi 1465
#endif
1275 mateusz.vi 1466
    }
1467
  }
1468
 
1469
  mdr_cout_close();
1470
 
1414 mateusz.vi 1471
  /* restore the DOS BREAK flag if it was originally set */
1472
  if (original_breakflag != 0) mdr_dos_ctrlc_enable();
1473
 
1320 mateusz.vi 1474
  /* no need to free memory, DOS will do it for me */
1275 mateusz.vi 1475
 
1347 mateusz.vi 1476
  return;
1275 mateusz.vi 1477
}