Subversion Repositories SvarDOS

Rev

Rev 1331 | Rev 1333 | 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
 
1312 mateusz.vi 32
#include "mdr\bios.h"
1275 mateusz.vi 33
#include "mdr\cout.h"
1307 mateusz.vi 34
#include "mdr\dos.h"
1275 mateusz.vi 35
#include "mdr\keyb.h"
36
 
1282 mateusz.vi 37
#include "svarlang\svarlang.h"
38
 
1326 mateusz.vi 39
 
40
/*****************************************************************************
41
 * global variables and definitions                                          *
42
 *****************************************************************************/
1275 mateusz.vi 43
#define COL_TXT        0
44
#define COL_STATUSBAR1 1
45
#define COL_STATUSBAR2 2
1332 mateusz.vi 46
#define COL_STATUSBAR3 3
47
#define COL_SCROLLBAR  4
48
#define COL_MSG        5
49
#define COL_ERR        6
1326 mateusz.vi 50
 
1275 mateusz.vi 51
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
1332 mateusz.vi 52
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0xf0, 0x70, 0x70, 0xf0};
1327 mateusz.vi 53
 
1326 mateusz.vi 54
static unsigned char screenw, screenh;
1275 mateusz.vi 55
 
1327 mateusz.vi 56
static struct {
57
    unsigned char from;
58
    unsigned char to;
59
} uidirty = {0, 0xff}; /* make sure to redraw entire UI at first run */
60
 
1275 mateusz.vi 61
#define SCROLL_CURSOR 0xB1
62
 
63
struct line {
1288 mateusz.vi 64
  struct line far *prev;
65
  struct line far *next;
1275 mateusz.vi 66
  unsigned short len;
67
  char payload[1];
68
};
69
 
1315 mateusz.vi 70
struct file {
1320 mateusz.vi 71
  int fd;
1288 mateusz.vi 72
  struct line far *cursor;
1275 mateusz.vi 73
  unsigned short xoffset;
1316 mateusz.vi 74
  unsigned char cursorposx;
75
  unsigned char cursorposy;
1328 mateusz.vi 76
  unsigned short totlines;
77
  unsigned short curline;
1330 mateusz.vi 78
  char lfonly;   /* set if line endings are LF (CR/LF otherwise) */
79
  char modflag;  /* non-zero if file has been modified since last save */
1332 mateusz.vi 80
  char fname[128];
1275 mateusz.vi 81
};
82
 
83
 
1326 mateusz.vi 84
/*****************************************************************************
85
 * functions                                                                 *
86
 *****************************************************************************/
87
 
1328 mateusz.vi 88
/* adds a new line at cursor position into file linked list and advance cursor
1324 mateusz.vi 89
 * returns non-zero on error */
1328 mateusz.vi 90
static int line_add(struct file *db, const char far *line, unsigned short slen) {
1288 mateusz.vi 91
  struct line far *l;
1275 mateusz.vi 92
 
1328 mateusz.vi 93
  l = _fcalloc(1, sizeof(struct line) + slen);
1287 mateusz.vi 94
  if (l == NULL) return(-1);
95
 
1275 mateusz.vi 96
  l->prev = db->cursor;
97
  if (db->cursor) {
98
    l->next = db->cursor->next;
99
    db->cursor->next = l;
100
    l->next->prev = l;
101
  }
102
  db->cursor = l;
1288 mateusz.vi 103
  _fmemcpy(l->payload, line, slen);
1275 mateusz.vi 104
  l->len = slen;
1287 mateusz.vi 105
 
1328 mateusz.vi 106
  db->totlines += 1;
107
  db->curline += 1;
108
 
1287 mateusz.vi 109
  return(0);
1275 mateusz.vi 110
}
111
 
112
 
1332 mateusz.vi 113
static void ui_getstring(const char *query, char *s, unsigned char maxlen) {
114
  unsigned char len = 0, y, x;
115
  int k;
116
 
117
  if (maxlen == 0) return;
118
  maxlen--; /* make room for the nul terminator */
119
 
120
  y = screenh - 1;
121
 
122
  /* print query string */
123
  x = mdr_cout_str(y, 0, query, scheme[COL_STATUSBAR3], 40);
124
  mdr_cout_char_rep(y, x++, ' ', scheme[COL_STATUSBAR3], screenw - x);
125
 
126
  for (;;) {
127
    mdr_cout_locate(y, x + len);
128
    k = keyb_getkey();
129
 
130
    if (k == 0x1b) return; /* ESC */
131
 
132
    if (k == '\r') {
133
      s[len] = 0;
134
      return;
135
    }
136
 
137
    if ((k == 0x08) && (len > 0)) { /* BKSPC */
138
      len--;
139
      mdr_cout_char(y, x + len, ' ', scheme[COL_STATUSBAR3]);
140
      continue;
141
    }
142
 
143
    if ((k <= 0xff) && (k >= ' ') && (len < maxlen)) {
144
      mdr_cout_char(y, x + len, k, scheme[COL_STATUSBAR3]);
145
      s[len++] = k;
146
    }
147
 
148
  }
149
}
150
 
151
 
1324 mateusz.vi 152
/* append a nul-terminated string to line at cursor position */
153
static int line_append(struct file *f, const char far *buf, unsigned short len) {
154
  struct line far *n;
155
  if (sizeof(struct line) + f->cursor->len + len < len) return(-1); /* overflow check */
156
  n = _frealloc(f->cursor, sizeof(struct line) + f->cursor->len + len);
157
  if (n == NULL) return(-1);
158
  f->cursor = n;
159
  _fmemcpy(f->cursor->payload + f->cursor->len, buf, len);
160
  f->cursor->len += len;
161
 
162
  /* rewire the linked list */
163
  if (f->cursor->next) f->cursor->next->prev = f->cursor;
164
  if (f->cursor->prev) f->cursor->prev->next = f->cursor;
165
 
166
  return(0);
167
}
168
 
169
 
1315 mateusz.vi 170
static void db_rewind(struct file *db) {
1275 mateusz.vi 171
  if (db->cursor == NULL) return;
172
  while (db->cursor->prev) db->cursor = db->cursor->prev;
1328 mateusz.vi 173
  db->curline = 0;
1275 mateusz.vi 174
}
175
 
176
 
1289 mateusz.vi 177
static void load_colorscheme(void) {
1275 mateusz.vi 178
  scheme[COL_TXT] = 0x17;
179
  scheme[COL_STATUSBAR1] = 0x70;
180
  scheme[COL_STATUSBAR2] = 0x78;
1332 mateusz.vi 181
  scheme[COL_STATUSBAR3] = 0xf0;
1275 mateusz.vi 182
  scheme[COL_SCROLLBAR] = 0x70;
1324 mateusz.vi 183
  scheme[COL_MSG] = 0xf0;
1322 mateusz.vi 184
  scheme[COL_ERR] = 0x4f;
1275 mateusz.vi 185
}
186
 
187
 
1326 mateusz.vi 188
static void ui_basic(const struct file *db) {
1302 mateusz.vi 189
  const char *s = svarlang_strid(0); /* HELP */
1275 mateusz.vi 190
  unsigned char helpcol = screenw - (strlen(s) + 4);
191
 
1330 mateusz.vi 192
  /* fill status bar with background (without modflag as it is refreshed by ui_refresh) */
193
  mdr_cout_char_rep(screenh - 1, 1, ' ', scheme[COL_STATUSBAR1], screenw - 1);
1313 mateusz.vi 194
 
195
  /* filename */
1330 mateusz.vi 196
  {
197
    const char *fn = db->fname;
198
    if (*fn == 0) fn = svarlang_str(0, 1);
199
    mdr_cout_str(screenh - 1, 1, fn, scheme[COL_STATUSBAR1], screenw);
1321 mateusz.vi 200
  }
1313 mateusz.vi 201
 
202
  /* eol type */
1330 mateusz.vi 203
  {
204
    const char *eoltype = "CRLF";
205
    if (db->lfonly) eoltype = "LF";
206
    mdr_cout_str(screenh - 1, helpcol - 5, eoltype, scheme[COL_STATUSBAR1], 5);
1275 mateusz.vi 207
  }
1313 mateusz.vi 208
 
1275 mateusz.vi 209
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
210
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
211
}
212
 
213
 
1331 mateusz.vi 214
static void ui_msg(const char *msg1, const char *msg2, unsigned char attr) {
1312 mateusz.vi 215
  unsigned short x, y, msglen, i;
1331 mateusz.vi 216
  unsigned char msg2flag = 0;
217
 
218
  msglen = strlen(msg1);
219
  if (msg2) {
220
    msg2flag = 1;
221
    i = strlen(msg2);
222
    if (i > msglen) msglen = i;
223
  }
224
 
225
  y = (screenh - 6) >> 1;
1312 mateusz.vi 226
  x = (screenw - msglen - 4) >> 1;
1331 mateusz.vi 227
  for (i = y+2+msg2flag; i >= y; i--) mdr_cout_char_rep(i, x, ' ', attr, msglen + 2);
228
  x++;
229
  mdr_cout_str(y+1, x, msg1, attr, msglen);
230
  if (msg2) mdr_cout_str(y+2, x, msg2, attr, msglen);
1312 mateusz.vi 231
 
1327 mateusz.vi 232
  if (uidirty.from > y) uidirty.from = y;
1331 mateusz.vi 233
  if (uidirty.to < y+4) uidirty.to = y+4;
1312 mateusz.vi 234
}
235
 
236
 
1326 mateusz.vi 237
static void ui_help(void) {
1309 mateusz.vi 238
#define MAXLINLEN 35
1312 mateusz.vi 239
  unsigned short i, offset;
1325 mateusz.vi 240
  offset = (screenw - MAXLINLEN + 2) >> 1;
1309 mateusz.vi 241
  mdr_cout_cursor_hide();
1325 mateusz.vi 242
  for (i = 2; i <= 15; i++) {
243
    mdr_cout_char_rep(i, offset - 2, ' ', scheme[COL_STATUSBAR1], MAXLINLEN + 2);
1309 mateusz.vi 244
  }
245
 
246
  mdr_cout_str(3, offset, svarlang_str(0, 0), scheme[COL_STATUSBAR1], MAXLINLEN);
1325 mateusz.vi 247
  for (i = 0; i <= 4; i++) {
1309 mateusz.vi 248
    mdr_cout_str(5 + i, offset, svarlang_str(8, i), scheme[COL_STATUSBAR1], MAXLINLEN);
249
  }
1325 mateusz.vi 250
  mdr_cout_str(5 + 1 + i, offset, svarlang_str(8, 10), scheme[COL_STATUSBAR1], MAXLINLEN);
1309 mateusz.vi 251
 
1325 mateusz.vi 252
  /* Press any key */
253
  mdr_cout_str(14, offset, svarlang_str(8, 11), scheme[COL_STATUSBAR1], MAXLINLEN);
254
 
1309 mateusz.vi 255
  keyb_getkey();
256
  mdr_cout_cursor_show();
257
#undef MAXLINLEN
258
}
259
 
260
 
1327 mateusz.vi 261
static void ui_refresh(const struct file *db) {
1318 mateusz.vi 262
  unsigned char x;
1310 mateusz.vi 263
  const struct line far *l;
1316 mateusz.vi 264
  unsigned char y = db->cursorposy;
1275 mateusz.vi 265
 
1288 mateusz.vi 266
#ifdef DBG_REFRESH
1282 mateusz.vi 267
  static char m = 'a';
268
  m++;
269
  if (m > 'z') m = 'a';
1288 mateusz.vi 270
#endif
1282 mateusz.vi 271
 
1310 mateusz.vi 272
  /* rewind cursor line to first line that needs redrawing */
1327 mateusz.vi 273
  for (l = db->cursor; y > uidirty.from; y--) l = l->prev;
1282 mateusz.vi 274
 
1310 mateusz.vi 275
  /* iterate over lines and redraw whatever needs to be redrawn */
276
  for (; l != NULL; l = l->next, y++) {
277
 
278
    /* skip lines that do not need to be refreshed */
1327 mateusz.vi 279
    if (y < uidirty.from) continue;
280
    if (y > uidirty.to) break;
1282 mateusz.vi 281
 
1318 mateusz.vi 282
    x = 0;
1275 mateusz.vi 283
    if (db->xoffset < l->len) {
1318 mateusz.vi 284
      unsigned char i, limit;
285
      if (l->len - db->xoffset < screenw) {
286
        limit = l->len;
287
      } else {
288
        limit = db->xoffset + screenw - 1;
289
      }
290
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], scheme[COL_TXT]);
1275 mateusz.vi 291
    }
1282 mateusz.vi 292
 
1318 mateusz.vi 293
    /* write empty spaces until end of line */
294
    if (x < screenw - 1) mdr_cout_char_rep(y, x, ' ', scheme[COL_TXT], screenw - 1 - x);
295
 
1288 mateusz.vi 296
#ifdef DBG_REFRESH
1282 mateusz.vi 297
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
1288 mateusz.vi 298
#endif
1282 mateusz.vi 299
 
1275 mateusz.vi 300
    if (y == screenh - 2) break;
301
  }
1310 mateusz.vi 302
 
1319 mateusz.vi 303
  /* fill all lines below if empty (and they need to be redrawn) */
304
  if (l == NULL) {
1327 mateusz.vi 305
    while ((y < screenh - 1) && (y < uidirty.to)) {
1319 mateusz.vi 306
      mdr_cout_char_rep(y++, 0, ' ', scheme[COL_TXT], screenw - 1);
307
    }
1318 mateusz.vi 308
  }
1328 mateusz.vi 309
 
1330 mateusz.vi 310
  /* "file changed" flag */
311
  {
312
    char flg = ' ';
313
    if (db->modflag) flg = '*';
314
    mdr_cout_char(screenh - 1, 0, flg, scheme[COL_STATUSBAR1]);
315
  }
316
 
1328 mateusz.vi 317
  /* scroll bar */
318
  for (y = 0; y < (screenh - 1); y++) {
319
    mdr_cout_char(y, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
320
  }
321
 
322
  /* scroll cursor */
323
  if (db->totlines >= screenh) {
324
    unsigned short topline = db->curline - db->cursorposy;
325
    unsigned short col;
326
    unsigned short totlines = db->totlines - screenh + 1;
327
    if (db->totlines - screenh > screenh) {
328
      col = topline / (totlines / (screenh - 1));
329
    } else {
330
      col = topline * (screenh - 1) / totlines;
331
    }
332
    if (col >= screenh - 1) col = screenh - 2;
333
    mdr_cout_char(col, screenw - 1, ' ', scheme[COL_SCROLLBAR]);
334
  }
1275 mateusz.vi 335
}
336
 
337
 
1327 mateusz.vi 338
static void check_cursor_not_after_eol(struct file *db) {
1316 mateusz.vi 339
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
1275 mateusz.vi 340
 
341
  if (db->cursor->len < db->xoffset) {
1316 mateusz.vi 342
    db->cursorposx = 0;
1275 mateusz.vi 343
    db->xoffset = db->cursor->len;
1327 mateusz.vi 344
    uidirty.from = 0;
345
    uidirty.to = 0xff;
1275 mateusz.vi 346
  } else {
1316 mateusz.vi 347
    db->cursorposx = db->cursor->len - db->xoffset;
1275 mateusz.vi 348
  }
349
}
350
 
351
 
1327 mateusz.vi 352
static void cursor_up(struct file *db) {
1275 mateusz.vi 353
  if (db->cursor->prev != NULL) {
1328 mateusz.vi 354
    db->curline -= 1;
1275 mateusz.vi 355
    db->cursor = db->cursor->prev;
1316 mateusz.vi 356
    if (db->cursorposy == 0) {
1327 mateusz.vi 357
      uidirty.from = 0;
358
      uidirty.to = 0xff;
1275 mateusz.vi 359
    } else {
1316 mateusz.vi 360
      db->cursorposy -= 1;
1275 mateusz.vi 361
    }
362
  }
363
}
364
 
365
 
1327 mateusz.vi 366
static void cursor_eol(struct file *db) {
1275 mateusz.vi 367
  /* adjust xoffset to make sure eol is visible on screen */
1282 mateusz.vi 368
  if (db->xoffset > db->cursor->len) {
369
    db->xoffset = db->cursor->len - 1;
1327 mateusz.vi 370
    uidirty.from = 0;
371
    uidirty.to = 0xff;
1282 mateusz.vi 372
  }
373
 
374
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
375
    db->xoffset = db->cursor->len - screenw + 2;
1327 mateusz.vi 376
    uidirty.from = 0;
377
    uidirty.to = 0xff;
1282 mateusz.vi 378
  }
1316 mateusz.vi 379
  db->cursorposx = db->cursor->len - db->xoffset;
1275 mateusz.vi 380
}
381
 
382
 
1327 mateusz.vi 383
static void cursor_down(struct file *db) {
1276 mateusz.vi 384
  if (db->cursor->next != NULL) {
1328 mateusz.vi 385
    db->curline += 1;
1276 mateusz.vi 386
    db->cursor = db->cursor->next;
1316 mateusz.vi 387
    if (db->cursorposy < screenh - 2) {
388
      db->cursorposy += 1;
1276 mateusz.vi 389
    } else {
1327 mateusz.vi 390
      uidirty.from = 0;
391
      uidirty.to = 0xff;
1276 mateusz.vi 392
    }
393
  }
394
}
395
 
396
 
1327 mateusz.vi 397
static void cursor_left(struct file *db) {
1316 mateusz.vi 398
  if (db->cursorposx > 0) {
399
    db->cursorposx -= 1;
1302 mateusz.vi 400
  } else if (db->xoffset > 0) {
401
    db->xoffset -= 1;
1327 mateusz.vi 402
    uidirty.from = 0;
403
    uidirty.to = 0xff;
1302 mateusz.vi 404
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
1327 mateusz.vi 405
    cursor_up(db);
406
    cursor_eol(db);
1302 mateusz.vi 407
  }
408
}
409
 
410
 
1327 mateusz.vi 411
static void cursor_home(struct file *db) {
1316 mateusz.vi 412
  db->cursorposx = 0;
1282 mateusz.vi 413
  if (db->xoffset != 0) {
414
    db->xoffset = 0;
1327 mateusz.vi 415
    uidirty.from = 0;
416
    uidirty.to = 0xff;
1282 mateusz.vi 417
  }
1276 mateusz.vi 418
}
419
 
420
 
1327 mateusz.vi 421
static void cursor_right(struct file *db) {
1316 mateusz.vi 422
  if (db->cursor->len > db->xoffset + db->cursorposx) {
423
    if (db->cursorposx < screenw - 2) {
424
      db->cursorposx += 1;
1308 mateusz.vi 425
    } else {
426
      db->xoffset += 1;
1327 mateusz.vi 427
      uidirty.from = 0;
428
      uidirty.to = 0xff;
1308 mateusz.vi 429
    }
430
  } else {
1327 mateusz.vi 431
    cursor_down(db);
432
    cursor_home(db);
1308 mateusz.vi 433
  }
434
}
435
 
436
 
1327 mateusz.vi 437
static void del(struct file *db) {
1316 mateusz.vi 438
  if (db->cursorposx + db->xoffset < db->cursor->len) {
439
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
1292 mateusz.vi 440
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
1327 mateusz.vi 441
    uidirty.from = db->cursorposy;
442
    uidirty.to = db->cursorposy;
1330 mateusz.vi 443
    db->modflag = 1;
1292 mateusz.vi 444
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
445
    struct line far *nextline = db->cursor->next;
446
    if (db->cursor->next->len > 0) {
447
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
448
      if (newptr != NULL) {
449
        db->cursor = newptr;
450
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
451
        db->cursor->len += db->cursor->next->len;
452
      }
453
    }
454
    db->cursor->next = db->cursor->next->next;
455
    db->cursor->next->prev = db->cursor;
456
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
457
    _ffree(nextline);
1327 mateusz.vi 458
    uidirty.from = db->cursorposy;
459
    uidirty.to = 0xff;
1328 mateusz.vi 460
    db->totlines -= 1;
1330 mateusz.vi 461
    db->modflag = 1;
1292 mateusz.vi 462
  }
463
}
464
 
465
 
1327 mateusz.vi 466
static void bkspc(struct file *db) {
1302 mateusz.vi 467
 
468
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
1316 mateusz.vi 469
  if ((db->cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
1302 mateusz.vi 470
 
1327 mateusz.vi 471
  cursor_left(db);
472
  del(db);
1302 mateusz.vi 473
}
474
 
475
 
1286 mateusz.vi 476
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
477
 * of argc and argv, saves some 330 bytes of binary size */
478
static char *parseargv(void) {
479
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
480
  unsigned char count = 0;
481
  char *argv[4];
482
 
483
  while (count < 4) {
484
    /* jump to nearest arg */
485
    while (*tail == ' ') {
486
      *tail = 0;
487
      tail++;
488
    }
489
 
490
    if (*tail == '\r') {
491
      *tail = 0;
492
      break;
493
    }
494
 
495
    argv[count++] = tail;
496
 
497
    /* jump to next delimiter */
498
    while ((*tail != ' ') && (*tail != '\r')) tail++;
499
  }
500
 
501
  /* check args now */
1321 mateusz.vi 502
  if (count == 0) return("");
1286 mateusz.vi 503
 
504
  return(argv[0]);
505
}
506
 
507
 
1320 mateusz.vi 508
static struct file *loadfile(const char *fname) {
1324 mateusz.vi 509
  char buff[512]; /* read one entire sector at a time (faster) */
510
  char *buffptr;
511
  unsigned int len, llen;
1287 mateusz.vi 512
  int fd;
1324 mateusz.vi 513
  unsigned char eolfound;
1320 mateusz.vi 514
  struct file *db;
1287 mateusz.vi 515
 
1321 mateusz.vi 516
  len = strlen(fname) + 1;
1332 mateusz.vi 517
  db = calloc(1, sizeof(struct file));
1321 mateusz.vi 518
  if (db == NULL) return(NULL);
519
  memcpy(db->fname, fname, len);
520
 
521
  if (*fname == 0) goto SKIPLOADING;
522
 
1287 mateusz.vi 523
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
524
    mdr_coutraw_puts("Failed to open file:");
525
    mdr_coutraw_puts(fname);
1321 mateusz.vi 526
    free(db);
1320 mateusz.vi 527
    return(NULL);
1287 mateusz.vi 528
  }
529
 
1313 mateusz.vi 530
  db->lfonly = 1;
531
 
1324 mateusz.vi 532
  /* start by adding an empty line */
1328 mateusz.vi 533
  if (line_add(db, NULL, 0) != 0) {
1324 mateusz.vi 534
    /* TODO ERROR HANDLING */
535
  }
1287 mateusz.vi 536
 
1324 mateusz.vi 537
  for (eolfound = 0;;) {
538
    unsigned short consumedbytes;
539
 
540
    if ((_dos_read(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
541
    buffptr = buff;
542
 
543
    FINDLINE:
544
 
545
    /* look for nearest \n */
546
    for (consumedbytes = 0;; consumedbytes++) {
547
      if (consumedbytes == len) {
548
        llen = consumedbytes;
549
        break;
1323 mateusz.vi 550
      }
1324 mateusz.vi 551
      if (buffptr[consumedbytes] == '\r') {
552
        llen = consumedbytes;
553
        consumedbytes++;
554
        db->lfonly = 0;
1320 mateusz.vi 555
        break;
556
      }
1324 mateusz.vi 557
      if (buffptr[consumedbytes] == '\n') {
558
        eolfound = 1;
559
        llen = consumedbytes;
560
        consumedbytes++;
561
        break;
562
      }
1287 mateusz.vi 563
    }
1324 mateusz.vi 564
 
565
    /* consumedbytes is the amount of bytes processed from buffptr,
566
     * llen is the length of line's payload (without its line terminator) */
567
 
568
    /* append content, if line is non-empty */
569
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
1287 mateusz.vi 570
      mdr_coutraw_puts("out of memory");
1320 mateusz.vi 571
      free(db);
572
      db = NULL;
1287 mateusz.vi 573
      break;
574
    }
575
 
1324 mateusz.vi 576
    /* add a new line if necessary */
577
    if (eolfound) {
1328 mateusz.vi 578
      if (line_add(db, NULL, 0) != 0) {
1324 mateusz.vi 579
      /* TODO ERROR HANDLING */
580
        mdr_coutraw_puts("out of memory");
581
        free(db);
582
        db = NULL;
583
        break;
584
      }
585
      eolfound = 0;
586
    }
1287 mateusz.vi 587
 
1324 mateusz.vi 588
    /* anything left? process the buffer leftover again */
589
    if (consumedbytes < len) {
590
      len -= consumedbytes;
591
      buffptr += consumedbytes;
592
      goto FINDLINE;
593
    }
594
 
595
  }
596
 
1287 mateusz.vi 597
  _dos_close(fd);
598
 
1321 mateusz.vi 599
  SKIPLOADING:
600
 
1320 mateusz.vi 601
  /* add an empty line at end if not present already, also rewind cursor to top of file */
602
  if (db != NULL) {
1328 mateusz.vi 603
    if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, NULL, 0);
1320 mateusz.vi 604
    db_rewind(db);
605
  }
606
 
607
  return(db);
1287 mateusz.vi 608
}
609
 
610
 
1332 mateusz.vi 611
static int savefile(const struct file *db, const char *newfname) {
1311 mateusz.vi 612
  int fd;
613
  const struct line far *l;
614
  unsigned bytes;
1313 mateusz.vi 615
  unsigned char eollen;
616
  unsigned char eolbuf[2];
1332 mateusz.vi 617
  int errflag = 0;
1313 mateusz.vi 618
 
1332 mateusz.vi 619
  /* either create a new file if newfname provided, or... */
620
  if (newfname) {
621
    if (_dos_creatnew(newfname, _A_NORMAL, &fd) != 0) return(-1);
622
  } else { /* ...open db->fname */
623
    if (_dos_open(db->fname, O_WRONLY, &fd) != 0) return(-1);
1311 mateusz.vi 624
  }
625
 
626
  l = db->cursor;
627
  while (l->prev) l = l->prev;
628
 
1313 mateusz.vi 629
  /* preset line terminators */
630
  if (db->lfonly) {
631
    eolbuf[0] = '\n';
632
    eollen = 1;
633
  } else {
634
    eolbuf[0] = '\r';
635
    eolbuf[1] = '\n';
636
    eollen = 2;
637
  }
638
 
1311 mateusz.vi 639
  while (l) {
1324 mateusz.vi 640
    /* do not write the last empty line, it is only useful for edition */
641
    if (l->len != 0) {
1332 mateusz.vi 642
      errflag |= _dos_write(fd, l->payload, l->len, &bytes);
1324 mateusz.vi 643
    } else if (l->next == NULL) {
644
      break;
645
    }
1332 mateusz.vi 646
    errflag |= _dos_write(fd, eolbuf, eollen, &bytes);
1311 mateusz.vi 647
    l = l->next;
648
  }
649
 
1332 mateusz.vi 650
  errflag |= _dos_close(fd);
1330 mateusz.vi 651
 
1332 mateusz.vi 652
  return(errflag);
1311 mateusz.vi 653
}
654
 
655
 
1327 mateusz.vi 656
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
1317 mateusz.vi 657
  struct line far *n;
658
  n = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + len);
659
  if (n != NULL) {
660
    unsigned short off = db->xoffset + db->cursorposx;
1330 mateusz.vi 661
    db->modflag = 1;
1317 mateusz.vi 662
    if (n->prev) n->prev->next = n;
663
    if (n->next) n->next->prev = n;
664
    db->cursor = n;
665
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
666
    db->cursor->len += len;
1327 mateusz.vi 667
    uidirty.from = db->cursorposy;
668
    uidirty.to = db->cursorposy;
1317 mateusz.vi 669
    while (len--) {
670
      db->cursor->payload[off++] = *databuf;
671
      databuf++;
1327 mateusz.vi 672
      cursor_right(db);
1317 mateusz.vi 673
    }
674
  }
675
}
676
 
677
 
1286 mateusz.vi 678
int main(void) {
679
  const char *fname;
1320 mateusz.vi 680
  struct file *db;
1275 mateusz.vi 681
 
1307 mateusz.vi 682
  {
683
    char nlspath[128], lang[8];
684
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
685
  }
1282 mateusz.vi 686
 
1286 mateusz.vi 687
  fname = parseargv();
688
 
689
  if (fname == NULL) {
1302 mateusz.vi 690
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
1275 mateusz.vi 691
    return(0);
692
  }
693
 
1282 mateusz.vi 694
  /* load file */
1320 mateusz.vi 695
  db = loadfile(fname);
696
  if (db == NULL) return(1);
1282 mateusz.vi 697
 
1275 mateusz.vi 698
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
1326 mateusz.vi 699
  ui_basic(db);
1275 mateusz.vi 700
 
701
  for (;;) {
702
    int k;
703
 
1327 mateusz.vi 704
    check_cursor_not_after_eol(db);
1320 mateusz.vi 705
    mdr_cout_locate(db->cursorposy, db->cursorposx);
1275 mateusz.vi 706
 
1327 mateusz.vi 707
    if (uidirty.from != 0xff) {
708
      ui_refresh(db);
709
      uidirty.from = 0xff;
1282 mateusz.vi 710
    }
1328 mateusz.vi 711
#ifdef DBG_LINENUM
712
      {
713
        char ddd[10];
714
        db->curline += 1;
715
        ddd[0] = '0' + db->curline / 100;
716
        ddd[1] = '0' + (db->curline % 100) / 10;
717
        ddd[2] = '0' + (db->curline % 10);
718
        db->curline -= 1;
719
        ddd[3] = '/';
720
        ddd[4] = '0' + db->totlines / 100;
721
        ddd[5] = '0' + (db->totlines % 100) / 10;
722
        ddd[6] = '0' + (db->totlines % 10);
723
        ddd[7] = 0;
724
        mdr_cout_str(screenh - 1, 40, ddd, scheme[COL_STATUSBAR1], sizeof(ddd));
725
      }
726
#endif
1275 mateusz.vi 727
 
728
    k = keyb_getkey();
1282 mateusz.vi 729
 
1275 mateusz.vi 730
    if (k == 0x150) { /* down */
1327 mateusz.vi 731
      cursor_down(db);
1275 mateusz.vi 732
 
733
    } else if (k == 0x148) { /* up */
1327 mateusz.vi 734
      cursor_up(db);
1275 mateusz.vi 735
 
736
    } else if (k == 0x14D) { /* right */
1327 mateusz.vi 737
      cursor_right(db);
1275 mateusz.vi 738
 
739
    } else if (k == 0x14B) { /* left */
1327 mateusz.vi 740
      cursor_left(db);
1275 mateusz.vi 741
 
1282 mateusz.vi 742
    } else if (k == 0x149) { /* pgup */
743
      // TODO
744
 
745
    } else if (k == 0x151) { /* pgdown */
746
      // TODO
747
 
748
    } else if (k == 0x147) { /* home */
1327 mateusz.vi 749
       cursor_home(db);
1282 mateusz.vi 750
 
751
    } else if (k == 0x14F) { /* end */
1327 mateusz.vi 752
       cursor_eol(db);
1282 mateusz.vi 753
 
1275 mateusz.vi 754
    } else if (k == 0x1B) { /* ESC */
1331 mateusz.vi 755
      if (db->modflag == 0) break;
756
      /* if file has been modified then ask for confirmation */
757
      ui_msg(svarlang_str(0,4), svarlang_str(0,5), scheme[COL_MSG]);
758
      if (keyb_getkey() == '\r') break;
1275 mateusz.vi 759
 
1289 mateusz.vi 760
    } else if (k == 0x0D) { /* ENTER */
1328 mateusz.vi 761
      unsigned short off = db->xoffset + db->cursorposx;
1289 mateusz.vi 762
      /* add a new line */
1328 mateusz.vi 763
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
1330 mateusz.vi 764
        db->modflag = 1;
1328 mateusz.vi 765
        db->cursor = db->cursor->prev; /* back to original line */
766
        db->curline -= 1;
1289 mateusz.vi 767
        /* trim the line above */
1328 mateusz.vi 768
        db->cursor->len = off;
1289 mateusz.vi 769
        /* move cursor to the (new) line below */
1328 mateusz.vi 770
        uidirty.from = db->cursorposy;
1327 mateusz.vi 771
        uidirty.to = 0xff;
1328 mateusz.vi 772
        cursor_down(db);
773
        cursor_home(db);
1289 mateusz.vi 774
      } else {
775
        /* ERROR: OUT OF MEMORY */
776
      }
777
 
1292 mateusz.vi 778
    } else if (k == 0x153) {  /* DEL */
1327 mateusz.vi 779
      del(db);
1292 mateusz.vi 780
 
1302 mateusz.vi 781
    } else if (k == 0x008) { /* BKSPC */
1327 mateusz.vi 782
      bkspc(db);
1302 mateusz.vi 783
 
1308 mateusz.vi 784
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
1317 mateusz.vi 785
      char c = k;
1327 mateusz.vi 786
      insert_in_line(db, &c, 1);
1308 mateusz.vi 787
 
1317 mateusz.vi 788
    } else if (k == 0x009) { /* TAB */
789
      const char *tab = "        ";
1327 mateusz.vi 790
      insert_in_line(db, tab, 8);
1317 mateusz.vi 791
 
1309 mateusz.vi 792
    } else if (k == 0x13b) { /* F1 */
1326 mateusz.vi 793
      ui_help();
1327 mateusz.vi 794
      uidirty.from = 0;
795
      uidirty.to = 0xff;
1309 mateusz.vi 796
 
1332 mateusz.vi 797
    } else if ((k == 0x13f) || (k == 0x140)) { /* F5 or F6 */
798
      int saveres;
799
 
800
      if ((k == 0x140) || (db->fname[0] == 0)) { /* save as... */
801
        char fname[25];
802
        ui_getstring(svarlang_str(0,6), fname, sizeof(fname));
803
        if (*fname == 0) continue;
804
        saveres = savefile(db, fname);
805
        if (saveres == 0) memcpy(db->fname, fname, sizeof(fname));
806
      } else {
807
        saveres = savefile(db, NULL);
808
      }
809
 
810
      if (saveres == 0) {
1330 mateusz.vi 811
        db->modflag = 0;
1331 mateusz.vi 812
        ui_msg(svarlang_str(0, 2), NULL, scheme[COL_MSG]);
1322 mateusz.vi 813
        mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
814
      } else {
1331 mateusz.vi 815
        ui_msg(svarlang_str(0, 3), NULL, scheme[COL_ERR]);
1322 mateusz.vi 816
        mdr_bios_tickswait(36); /* 2s */
817
      }
1311 mateusz.vi 818
 
1332 mateusz.vi 819
      ui_basic(db);
820
      ui_refresh(db);
821
 
1314 mateusz.vi 822
    } else if (k == 0x144) { /* F10 */
1330 mateusz.vi 823
      db->modflag = 1;
1320 mateusz.vi 824
      db->lfonly ^= 1;
1326 mateusz.vi 825
      ui_basic(db);
1314 mateusz.vi 826
 
1325 mateusz.vi 827
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
828
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
829
      for (;;) {
830
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
831
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
1327 mateusz.vi 832
        cursor_right(db);
1325 mateusz.vi 833
      }
834
      /* now skip to next non-space or end of file */
835
      for (;;) {
1327 mateusz.vi 836
        cursor_right(db);
1325 mateusz.vi 837
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
838
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
839
      }
840
 
841
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
1327 mateusz.vi 842
      cursor_left(db);
1325 mateusz.vi 843
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
844
      for (;;) {
845
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
846
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1327 mateusz.vi 847
        cursor_left(db);
1325 mateusz.vi 848
      }
849
      /* now skip to next space or start of file */
850
      for (;;) {
1327 mateusz.vi 851
        cursor_left(db);
1325 mateusz.vi 852
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
1327 mateusz.vi 853
          cursor_right(db);
1325 mateusz.vi 854
          break;
855
        }
856
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
857
      }
858
 
1282 mateusz.vi 859
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
860
      char buff[4];
861
      const char *HEX = "0123456789ABCDEF";
862
      buff[0] = HEX[(k >> 8) & 15];
863
      buff[1] = HEX[(k >> 4) & 15];
864
      buff[2] = HEX[k & 15];
865
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
866
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
1275 mateusz.vi 867
      keyb_getkey();
868
      break;
869
    }
870
  }
871
 
872
  mdr_cout_close();
873
 
1320 mateusz.vi 874
  /* no need to free memory, DOS will do it for me */
1275 mateusz.vi 875
 
876
  return(0);
877
}