Subversion Repositories SvarDOS

Rev

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

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