Subversion Repositories SvarDOS

Rev

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