Subversion Repositories SvarDOS

Rev

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

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