Subversion Repositories SvarDOS

Rev

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