Subversion Repositories SvarDOS

Rev

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