Subversion Repositories SvarDOS

Rev

Rev 1332 | Rev 1334 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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