Subversion Repositories SvarDOS

Rev

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