Subversion Repositories SvarDOS

Rev

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