Subversion Repositories SvarDOS

Rev

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