Subversion Repositories SvarDOS

Rev

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