Subversion Repositories SvarDOS

Rev

Rev 1292 | Rev 1307 | 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;
1302 mateusz.vi 110
  const char *s = svarlang_strid(0); /* HELP */
1275 mateusz.vi 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
 
1302 mateusz.vi 223
static void cursor_left(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
224
  if (*cursorposx > 0) {
225
    *cursorposx -= 1;
226
  } else if (db->xoffset > 0) {
227
    db->xoffset -= 1;
228
    *uidirtyfrom = 0;
229
    *uidirtyto = 0xff;
230
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
231
    cursor_up(db, cursorposy, uidirtyfrom, uidirtyto);
232
    cursor_eol(db, cursorposx, screenw, uidirtyfrom, uidirtyto);
233
  }
234
}
235
 
236
 
1282 mateusz.vi 237
static void cursor_home(struct linedb *db, unsigned char *cursorposx, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1276 mateusz.vi 238
  *cursorposx = 0;
1282 mateusz.vi 239
  if (db->xoffset != 0) {
240
    db->xoffset = 0;
241
    *uidirtyfrom = 0;
242
    *uidirtyto = 0xff;
243
  }
1276 mateusz.vi 244
}
245
 
246
 
1292 mateusz.vi 247
static void del(struct linedb *db, unsigned char cursorposx, unsigned char cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
248
  if (cursorposx + db->xoffset < db->cursor->len) {
249
    _fmemmove(db->cursor->payload + cursorposx + db->xoffset, db->cursor->payload + cursorposx + db->xoffset + 1, db->cursor->len - cursorposx - db->xoffset);
250
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
251
    *uidirtyfrom = cursorposy;
252
    *uidirtyto = cursorposy;
253
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
254
    struct line far *nextline = db->cursor->next;
255
    if (db->cursor->next->len > 0) {
256
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
257
      if (newptr != NULL) {
258
        db->cursor = newptr;
259
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
260
        db->cursor->len += db->cursor->next->len;
261
        /* update db->topscreen if needed */
262
        if (cursorposy == 0) db->topscreen = db->cursor;
263
      }
264
    }
265
    db->cursor->next = db->cursor->next->next;
266
    db->cursor->next->prev = db->cursor;
267
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
268
    _ffree(nextline);
269
    *uidirtyfrom = cursorposy;
270
    *uidirtyto = 0xff;
271
  }
272
}
273
 
274
 
1302 mateusz.vi 275
static void bkspc(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
276
 
277
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
278
  if ((*cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
279
 
280
  cursor_left(db, cursorposx, cursorposy, screenw, uidirtyfrom, uidirtyto);
281
  del(db, *cursorposx, *cursorposy, uidirtyfrom, uidirtyto);
282
}
283
 
284
 
1286 mateusz.vi 285
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
286
 * of argc and argv, saves some 330 bytes of binary size */
287
static char *parseargv(void) {
288
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
289
  unsigned char count = 0;
290
  char *argv[4];
291
 
292
  while (count < 4) {
293
    /* jump to nearest arg */
294
    while (*tail == ' ') {
295
      *tail = 0;
296
      tail++;
297
    }
298
 
299
    if (*tail == '\r') {
300
      *tail = 0;
301
      break;
302
    }
303
 
304
    argv[count++] = tail;
305
 
306
    /* jump to next delimiter */
307
    while ((*tail != ' ') && (*tail != '\r')) tail++;
308
  }
309
 
310
  /* check args now */
311
  if (count != 1) return(NULL);
312
 
313
  return(argv[0]);
314
}
315
 
316
 
1287 mateusz.vi 317
static int loadfile(struct linedb *db, const char *fname) {
318
  char buff[1024];
319
  unsigned int prevlen = 0, len, llen;
320
  int fd;
321
  int r = 0;
322
 
323
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
324
    mdr_coutraw_puts("Failed to open file:");
325
    mdr_coutraw_puts(fname);
326
    return(-1);
327
  }
328
 
329
  do {
330
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
331
      len += prevlen;
332
    } else {
333
      len = prevlen;
334
    }
335
 
336
    /* look for nearest \n and replace with 0*/
337
    for (llen = 0; buff[llen] != '\n'; llen++) {
338
      if (llen == sizeof(buff)) break;
339
    }
340
    buff[llen] = 0;
341
    if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
342
    if (line_add(db, buff) != 0) {
343
      mdr_coutraw_puts("out of memory");
344
      r = -1;
345
      break;
346
    }
347
 
348
    len -= llen + 1;
349
    memmove(buff, buff + llen + 1, len);
350
    prevlen = len;
351
  } while (len > 0);
352
 
353
  _dos_close(fd);
354
 
355
  return(r);
356
}
357
 
358
 
1286 mateusz.vi 359
int main(void) {
360
  const char *fname;
1275 mateusz.vi 361
  struct linedb db;
362
  unsigned char screenw = 0, screenh = 0;
363
  unsigned char cursorposx = 0, cursorposy = 0;
1282 mateusz.vi 364
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
1275 mateusz.vi 365
 
366
  bzero(&db, sizeof(db));
367
 
1282 mateusz.vi 368
  svarlang_autoload_nlspath("sved");
369
 
1286 mateusz.vi 370
  fname = parseargv();
371
 
372
  if (fname == NULL) {
1302 mateusz.vi 373
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
1275 mateusz.vi 374
    return(0);
375
  }
376
 
1282 mateusz.vi 377
  /* load file */
1287 mateusz.vi 378
  if (loadfile(&db, fname) != 0) return(1);
1282 mateusz.vi 379
 
1275 mateusz.vi 380
  /* add an empty line at end if not present already */
381
  if (db.cursor->len != 0) line_add(&db, "");
382
 
383
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
384
  ui_basic(screenw, screenh, fname);
385
 
386
  db_rewind(&db);
387
 
388
  for (;;) {
389
    int k;
390
 
1282 mateusz.vi 391
    check_cursor_not_after_eol(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 392
    mdr_cout_locate(cursorposy, cursorposx);
393
 
1282 mateusz.vi 394
    if (uidirtyfrom != 0xff) {
395
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto);
396
      uidirtyfrom = 0xff;
397
    }
1275 mateusz.vi 398
 
399
    k = keyb_getkey();
1282 mateusz.vi 400
 
1275 mateusz.vi 401
    if (k == 0x150) { /* down */
1282 mateusz.vi 402
      cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 403
 
404
    } else if (k == 0x148) { /* up */
1282 mateusz.vi 405
      cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 406
 
407
    } else if (k == 0x14D) { /* right */
408
      if (db.cursor->len > db.xoffset + cursorposx) {
409
        if (cursorposx < screenw - 2) {
410
          cursorposx++;
411
        } else {
412
          db.xoffset++;
1282 mateusz.vi 413
          uidirtyfrom = 0;
414
          uidirtyto = 0xff;
1275 mateusz.vi 415
        }
1276 mateusz.vi 416
      } else {
1282 mateusz.vi 417
        cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
418
        cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 419
      }
420
 
421
    } else if (k == 0x14B) { /* left */
1302 mateusz.vi 422
      cursor_left(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 423
 
1282 mateusz.vi 424
    } else if (k == 0x149) { /* pgup */
425
      // TODO
426
 
427
    } else if (k == 0x151) { /* pgdown */
428
      // TODO
429
 
430
    } else if (k == 0x147) { /* home */
431
       cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
432
 
433
    } else if (k == 0x14F) { /* end */
434
       cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
435
 
1275 mateusz.vi 436
    } else if (k == 0x1B) { /* ESC */
437
      break;
438
 
1289 mateusz.vi 439
    } else if (k == 0x0D) { /* ENTER */
440
      /* add a new line */
441
      if (line_add(&db, db.cursor->payload + db.xoffset + cursorposx) == 0) {
442
        /* trim the line above */
443
        db.cursor->prev->len = db.xoffset + cursorposx;
444
        db.cursor->prev->payload[db.cursor->prev->len] = 0;
445
        /* move cursor to the (new) line below */
446
        cursorposx = 0;
447
        if (cursorposy < screenh - 2) {
448
          uidirtyfrom = cursorposy;
449
          cursorposy++;
450
        } else {
451
          db.topscreen = db.topscreen->next;
452
          uidirtyfrom = 0;
453
        }
454
        uidirtyto = 0xff;
455
      } else {
456
        /* ERROR: OUT OF MEMORY */
457
      }
458
 
1292 mateusz.vi 459
    } else if (k == 0x153) {  /* DEL */
460
      del(&db, cursorposx, cursorposy, &uidirtyfrom, &uidirtyto);
461
 
1302 mateusz.vi 462
    } else if (k == 0x008) { /* BKSPC */
463
      bkspc(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
464
 
1282 mateusz.vi 465
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
466
      char buff[4];
467
      const char *HEX = "0123456789ABCDEF";
468
      buff[0] = HEX[(k >> 8) & 15];
469
      buff[1] = HEX[(k >> 4) & 15];
470
      buff[2] = HEX[k & 15];
471
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
472
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
1275 mateusz.vi 473
      keyb_getkey();
474
      break;
475
    }
476
  }
477
 
478
  mdr_cout_close();
479
 
480
  /* TODO free memory */
481
 
482
  return(0);
483
}