Subversion Repositories SvarDOS

Rev

Rev 1302 | Rev 1308 | 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
 
32
#include "mdr\cout.h"
1307 mateusz.vi 33
#include "mdr\dos.h"
1275 mateusz.vi 34
#include "mdr\keyb.h"
35
 
1282 mateusz.vi 36
#include "svarlang\svarlang.h"
37
 
1275 mateusz.vi 38
#define COL_TXT        0
39
#define COL_STATUSBAR1 1
40
#define COL_STATUSBAR2 2
41
#define COL_SCROLLBAR  3
42
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
43
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
44
 
45
#define SCROLL_CURSOR 0xB1
46
 
47
 
48
struct line {
1288 mateusz.vi 49
  struct line far *prev;
50
  struct line far *next;
1275 mateusz.vi 51
  unsigned short len;
52
  char payload[1];
53
};
54
 
55
struct linedb {
1288 mateusz.vi 56
  struct line far *topscreen;
57
  struct line far *cursor;
1275 mateusz.vi 58
  unsigned short xoffset;
59
};
60
 
61
 
1287 mateusz.vi 62
/* returns non-zero on error */
1289 mateusz.vi 63
static int line_add(struct linedb *db, const char far *line) {
64
  unsigned short slen;
1288 mateusz.vi 65
  struct line far *l;
1275 mateusz.vi 66
 
1289 mateusz.vi 67
  /* slen = strlen(line) (but for far pointer) */
68
  for (slen = 0; line[slen] != 0; slen++);
69
 
1275 mateusz.vi 70
  /* trim out CR/LF line endings */
71
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
72
    slen -= 2;
73
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
74
    slen--;
75
  }
76
 
1288 mateusz.vi 77
  l = _fcalloc(1, sizeof(struct line) + slen + 1);
1287 mateusz.vi 78
  if (l == NULL) return(-1);
79
 
1275 mateusz.vi 80
  l->prev = db->cursor;
81
  if (db->cursor) {
82
    l->next = db->cursor->next;
83
    db->cursor->next = l;
84
    l->next->prev = l;
85
  }
86
  db->cursor = l;
1288 mateusz.vi 87
  _fmemcpy(l->payload, line, slen);
1275 mateusz.vi 88
  l->len = slen;
1287 mateusz.vi 89
 
90
  return(0);
1275 mateusz.vi 91
}
92
 
93
 
1289 mateusz.vi 94
static void db_rewind(struct linedb *db) {
1275 mateusz.vi 95
  if (db->cursor == NULL) return;
96
  while (db->cursor->prev) db->cursor = db->cursor->prev;
97
  db->topscreen = db->cursor;
98
}
99
 
100
 
1289 mateusz.vi 101
static void load_colorscheme(void) {
1275 mateusz.vi 102
  scheme[COL_TXT] = 0x17;
103
  scheme[COL_STATUSBAR1] = 0x70;
104
  scheme[COL_STATUSBAR2] = 0x78;
105
  scheme[COL_SCROLLBAR] = 0x70;
106
}
107
 
108
 
1282 mateusz.vi 109
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname) {
1275 mateusz.vi 110
  unsigned char i;
1302 mateusz.vi 111
  const char *s = svarlang_strid(0); /* HELP */
1275 mateusz.vi 112
  unsigned char helpcol = screenw - (strlen(s) + 4);
113
 
114
  /* clear screen */
115
  mdr_cout_cls(scheme[COL_TXT]);
116
 
117
  /* status bar */
118
  for (i = 0; i < helpcol; i++) {
119
    mdr_cout_char(screenh - 1, i, *fname, scheme[COL_STATUSBAR1]);
120
    if (*fname != 0) fname++;
121
  }
122
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
123
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
124
 
125
  /* scroll bar */
126
  for (i = 0; i < (screenh - 1); i++) {
127
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
128
  }
129
}
130
 
131
 
1282 mateusz.vi 132
static void ui_refresh(const struct linedb *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto) {
1275 mateusz.vi 133
  unsigned char y = 0;
134
  unsigned char len;
1288 mateusz.vi 135
  struct line far *l;
1275 mateusz.vi 136
 
1288 mateusz.vi 137
#ifdef DBG_REFRESH
1282 mateusz.vi 138
  static char m = 'a';
139
  m++;
140
  if (m > 'z') m = 'a';
1288 mateusz.vi 141
#endif
1282 mateusz.vi 142
 
1289 mateusz.vi 143
  for (l = db->topscreen; l != NULL; l = l->next, y++) {
1282 mateusz.vi 144
 
145
    /* skip lines that do not to be refreshed */
1289 mateusz.vi 146
    if (y < uidirtyfrom) continue;
147
    if (y > uidirtyto) break;
1282 mateusz.vi 148
 
1275 mateusz.vi 149
    if (db->xoffset < l->len) {
1288 mateusz.vi 150
      for (len = 0; l->payload[len] != 0; len++) mdr_cout_char(y, len, l->payload[len], scheme[COL_TXT]);
1275 mateusz.vi 151
    } else {
152
      len = 0;
153
    }
154
    while (len < screenw - 1) mdr_cout_char(y, len++, ' ', scheme[COL_TXT]);
1282 mateusz.vi 155
 
1288 mateusz.vi 156
#ifdef DBG_REFRESH
1282 mateusz.vi 157
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
1288 mateusz.vi 158
#endif
1282 mateusz.vi 159
 
1275 mateusz.vi 160
    if (y == screenh - 2) break;
161
  }
162
}
163
 
164
 
1282 mateusz.vi 165
static void check_cursor_not_after_eol(struct linedb *db, unsigned char *cursorpos, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1275 mateusz.vi 166
  if (db->xoffset + *cursorpos <= db->cursor->len) return;
167
 
168
  if (db->cursor->len < db->xoffset) {
169
    *cursorpos = 0;
170
    db->xoffset = db->cursor->len;
1282 mateusz.vi 171
    *uidirtyfrom = 0;
172
    *uidirtyto = 0xff;
1275 mateusz.vi 173
  } else {
174
    *cursorpos = db->cursor->len - db->xoffset;
175
  }
176
}
177
 
178
 
1282 mateusz.vi 179
static void cursor_up(struct linedb *db, unsigned char *cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1275 mateusz.vi 180
  if (db->cursor->prev != NULL) {
181
    db->cursor = db->cursor->prev;
182
    if (*cursorposy == 0) {
183
      db->topscreen = db->cursor;
1282 mateusz.vi 184
      *uidirtyfrom = 0;
185
      *uidirtyto = 0xff;
1275 mateusz.vi 186
    } else {
187
      *cursorposy -= 1;
188
    }
189
  }
190
}
191
 
192
 
1282 mateusz.vi 193
static void cursor_eol(struct linedb *db, unsigned char *cursorposx, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1275 mateusz.vi 194
  /* adjust xoffset to make sure eol is visible on screen */
1282 mateusz.vi 195
  if (db->xoffset > db->cursor->len) {
196
    db->xoffset = db->cursor->len - 1;
197
    *uidirtyfrom = 0;
198
    *uidirtyto = 0xff;
199
  }
200
 
201
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
202
    db->xoffset = db->cursor->len - screenw + 2;
203
    *uidirtyfrom = 0;
204
    *uidirtyto = 0xff;
205
  }
1275 mateusz.vi 206
  *cursorposx = db->cursor->len - db->xoffset;
207
}
208
 
209
 
1282 mateusz.vi 210
static void cursor_down(struct linedb *db, unsigned char *cursorposy, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1276 mateusz.vi 211
  if (db->cursor->next != NULL) {
212
    db->cursor = db->cursor->next;
213
    if (*cursorposy < screenh - 2) {
214
      *cursorposy += 1;
215
    } else {
216
      db->topscreen = db->topscreen->next;
1282 mateusz.vi 217
      *uidirtyfrom = 0;
218
      *uidirtyto = 0xff;
1276 mateusz.vi 219
    }
220
  }
221
}
222
 
223
 
1302 mateusz.vi 224
static void cursor_left(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
225
  if (*cursorposx > 0) {
226
    *cursorposx -= 1;
227
  } else if (db->xoffset > 0) {
228
    db->xoffset -= 1;
229
    *uidirtyfrom = 0;
230
    *uidirtyto = 0xff;
231
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
232
    cursor_up(db, cursorposy, uidirtyfrom, uidirtyto);
233
    cursor_eol(db, cursorposx, screenw, uidirtyfrom, uidirtyto);
234
  }
235
}
236
 
237
 
1282 mateusz.vi 238
static void cursor_home(struct linedb *db, unsigned char *cursorposx, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1276 mateusz.vi 239
  *cursorposx = 0;
1282 mateusz.vi 240
  if (db->xoffset != 0) {
241
    db->xoffset = 0;
242
    *uidirtyfrom = 0;
243
    *uidirtyto = 0xff;
244
  }
1276 mateusz.vi 245
}
246
 
247
 
1292 mateusz.vi 248
static void del(struct linedb *db, unsigned char cursorposx, unsigned char cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
249
  if (cursorposx + db->xoffset < db->cursor->len) {
250
    _fmemmove(db->cursor->payload + cursorposx + db->xoffset, db->cursor->payload + cursorposx + db->xoffset + 1, db->cursor->len - cursorposx - db->xoffset);
251
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
252
    *uidirtyfrom = cursorposy;
253
    *uidirtyto = cursorposy;
254
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
255
    struct line far *nextline = db->cursor->next;
256
    if (db->cursor->next->len > 0) {
257
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
258
      if (newptr != NULL) {
259
        db->cursor = newptr;
260
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
261
        db->cursor->len += db->cursor->next->len;
262
        /* update db->topscreen if needed */
263
        if (cursorposy == 0) db->topscreen = db->cursor;
264
      }
265
    }
266
    db->cursor->next = db->cursor->next->next;
267
    db->cursor->next->prev = db->cursor;
268
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
269
    _ffree(nextline);
270
    *uidirtyfrom = cursorposy;
271
    *uidirtyto = 0xff;
272
  }
273
}
274
 
275
 
1302 mateusz.vi 276
static void bkspc(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
277
 
278
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
279
  if ((*cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
280
 
281
  cursor_left(db, cursorposx, cursorposy, screenw, uidirtyfrom, uidirtyto);
282
  del(db, *cursorposx, *cursorposy, uidirtyfrom, uidirtyto);
283
}
284
 
285
 
1286 mateusz.vi 286
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
287
 * of argc and argv, saves some 330 bytes of binary size */
288
static char *parseargv(void) {
289
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
290
  unsigned char count = 0;
291
  char *argv[4];
292
 
293
  while (count < 4) {
294
    /* jump to nearest arg */
295
    while (*tail == ' ') {
296
      *tail = 0;
297
      tail++;
298
    }
299
 
300
    if (*tail == '\r') {
301
      *tail = 0;
302
      break;
303
    }
304
 
305
    argv[count++] = tail;
306
 
307
    /* jump to next delimiter */
308
    while ((*tail != ' ') && (*tail != '\r')) tail++;
309
  }
310
 
311
  /* check args now */
312
  if (count != 1) return(NULL);
313
 
314
  return(argv[0]);
315
}
316
 
317
 
1287 mateusz.vi 318
static int loadfile(struct linedb *db, const char *fname) {
319
  char buff[1024];
320
  unsigned int prevlen = 0, len, llen;
321
  int fd;
322
  int r = 0;
323
 
324
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
325
    mdr_coutraw_puts("Failed to open file:");
326
    mdr_coutraw_puts(fname);
327
    return(-1);
328
  }
329
 
330
  do {
331
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
332
      len += prevlen;
333
    } else {
334
      len = prevlen;
335
    }
336
 
337
    /* look for nearest \n and replace with 0*/
338
    for (llen = 0; buff[llen] != '\n'; llen++) {
339
      if (llen == sizeof(buff)) break;
340
    }
341
    buff[llen] = 0;
342
    if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
343
    if (line_add(db, buff) != 0) {
344
      mdr_coutraw_puts("out of memory");
345
      r = -1;
346
      break;
347
    }
348
 
349
    len -= llen + 1;
350
    memmove(buff, buff + llen + 1, len);
351
    prevlen = len;
352
  } while (len > 0);
353
 
354
  _dos_close(fd);
355
 
356
  return(r);
357
}
358
 
359
 
1286 mateusz.vi 360
int main(void) {
361
  const char *fname;
1275 mateusz.vi 362
  struct linedb db;
363
  unsigned char screenw = 0, screenh = 0;
364
  unsigned char cursorposx = 0, cursorposy = 0;
1282 mateusz.vi 365
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
1275 mateusz.vi 366
 
367
  bzero(&db, sizeof(db));
368
 
1307 mateusz.vi 369
  {
370
    char nlspath[128], lang[8];
371
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
372
  }
1282 mateusz.vi 373
 
1286 mateusz.vi 374
  fname = parseargv();
375
 
376
  if (fname == NULL) {
1302 mateusz.vi 377
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
1275 mateusz.vi 378
    return(0);
379
  }
380
 
1282 mateusz.vi 381
  /* load file */
1287 mateusz.vi 382
  if (loadfile(&db, fname) != 0) return(1);
1282 mateusz.vi 383
 
1275 mateusz.vi 384
  /* add an empty line at end if not present already */
385
  if (db.cursor->len != 0) line_add(&db, "");
386
 
387
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
388
  ui_basic(screenw, screenh, fname);
389
 
390
  db_rewind(&db);
391
 
392
  for (;;) {
393
    int k;
394
 
1282 mateusz.vi 395
    check_cursor_not_after_eol(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 396
    mdr_cout_locate(cursorposy, cursorposx);
397
 
1282 mateusz.vi 398
    if (uidirtyfrom != 0xff) {
399
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto);
400
      uidirtyfrom = 0xff;
401
    }
1275 mateusz.vi 402
 
403
    k = keyb_getkey();
1282 mateusz.vi 404
 
1275 mateusz.vi 405
    if (k == 0x150) { /* down */
1282 mateusz.vi 406
      cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 407
 
408
    } else if (k == 0x148) { /* up */
1282 mateusz.vi 409
      cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 410
 
411
    } else if (k == 0x14D) { /* right */
412
      if (db.cursor->len > db.xoffset + cursorposx) {
413
        if (cursorposx < screenw - 2) {
414
          cursorposx++;
415
        } else {
416
          db.xoffset++;
1282 mateusz.vi 417
          uidirtyfrom = 0;
418
          uidirtyto = 0xff;
1275 mateusz.vi 419
        }
1276 mateusz.vi 420
      } else {
1282 mateusz.vi 421
        cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
422
        cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 423
      }
424
 
425
    } else if (k == 0x14B) { /* left */
1302 mateusz.vi 426
      cursor_left(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 427
 
1282 mateusz.vi 428
    } else if (k == 0x149) { /* pgup */
429
      // TODO
430
 
431
    } else if (k == 0x151) { /* pgdown */
432
      // TODO
433
 
434
    } else if (k == 0x147) { /* home */
435
       cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
436
 
437
    } else if (k == 0x14F) { /* end */
438
       cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
439
 
1275 mateusz.vi 440
    } else if (k == 0x1B) { /* ESC */
441
      break;
442
 
1289 mateusz.vi 443
    } else if (k == 0x0D) { /* ENTER */
444
      /* add a new line */
445
      if (line_add(&db, db.cursor->payload + db.xoffset + cursorposx) == 0) {
446
        /* trim the line above */
447
        db.cursor->prev->len = db.xoffset + cursorposx;
448
        db.cursor->prev->payload[db.cursor->prev->len] = 0;
449
        /* move cursor to the (new) line below */
450
        cursorposx = 0;
451
        if (cursorposy < screenh - 2) {
452
          uidirtyfrom = cursorposy;
453
          cursorposy++;
454
        } else {
455
          db.topscreen = db.topscreen->next;
456
          uidirtyfrom = 0;
457
        }
458
        uidirtyto = 0xff;
459
      } else {
460
        /* ERROR: OUT OF MEMORY */
461
      }
462
 
1292 mateusz.vi 463
    } else if (k == 0x153) {  /* DEL */
464
      del(&db, cursorposx, cursorposy, &uidirtyfrom, &uidirtyto);
465
 
1302 mateusz.vi 466
    } else if (k == 0x008) { /* BKSPC */
467
      bkspc(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
468
 
1282 mateusz.vi 469
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
470
      char buff[4];
471
      const char *HEX = "0123456789ABCDEF";
472
      buff[0] = HEX[(k >> 8) & 15];
473
      buff[1] = HEX[(k >> 4) & 15];
474
      buff[2] = HEX[k & 15];
475
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
476
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
1275 mateusz.vi 477
      keyb_getkey();
478
      break;
479
    }
480
  }
481
 
482
  mdr_cout_close();
483
 
484
  /* TODO free memory */
485
 
486
  return(0);
487
}