Subversion Repositories SvarDOS

Rev

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