Subversion Repositories SvarDOS

Rev

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