Subversion Repositories SvarDOS

Rev

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