Subversion Repositories SvarDOS

Rev

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