Subversion Repositories SvarDOS

Rev

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