Subversion Repositories SvarDOS

Rev

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