Subversion Repositories SvarDOS

Rev

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