Subversion Repositories SvarDOS

Rev

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