Subversion Repositories SvarDOS

Rev

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