Subversion Repositories SvarDOS

Rev

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