Subversion Repositories SvarDOS

Rev

Rev 1319 | Rev 1321 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1319 Rev 1320
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
#define COL_TXT        0
39
#define COL_TXT        0
40
#define COL_STATUSBAR1 1
40
#define COL_STATUSBAR1 1
41
#define COL_STATUSBAR2 2
41
#define COL_STATUSBAR2 2
42
#define COL_SCROLLBAR  3
42
#define COL_SCROLLBAR  3
43
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
43
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
44
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
44
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
45
 
45
 
46
#define SCROLL_CURSOR 0xB1
46
#define SCROLL_CURSOR 0xB1
47
 
47
 
48
 
48
 
49
struct line {
49
struct line {
50
  struct line far *prev;
50
  struct line far *prev;
51
  struct line far *next;
51
  struct line far *next;
52
  unsigned short len;
52
  unsigned short len;
53
  char payload[1];
53
  char payload[1];
54
};
54
};
55
 
55
 
56
struct file {
56
struct file {
-
 
57
  int fd;
57
  struct line far *cursor;
58
  struct line far *cursor;
58
  unsigned short xoffset;
59
  unsigned short xoffset;
59
  unsigned char cursorposx;
60
  unsigned char cursorposx;
60
  unsigned char cursorposy;
61
  unsigned char cursorposy;
61
  char lfonly; /* set if line endings are LF (CR/LF otherwise) */
62
  char lfonly; /* set if line endings are LF (CR/LF otherwise) */
-
 
63
  char fname[1]; /* dynamically sized */
62
};
64
};
63
 
65
 
64
 
66
 
65
/* returns non-zero on error */
67
/* returns non-zero on error */
66
static int line_add(struct file *db, const char far *line) {
68
static int line_add(struct file *db, const char far *line) {
67
  unsigned short slen;
69
  unsigned short slen;
68
  struct line far *l;
70
  struct line far *l;
69
 
71
 
70
  /* slen = strlen(line) (but for far pointer) */
72
  /* slen = strlen(line) (but for far pointer) */
71
  for (slen = 0; line[slen] != 0; slen++);
73
  for (slen = 0; line[slen] != 0; slen++);
72
 
74
 
73
  /* trim out CR/LF line endings */
75
  /* trim out CR/LF line endings */
74
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
76
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
75
    slen -= 2;
77
    slen -= 2;
76
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
78
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
77
    slen--;
79
    slen--;
78
  }
80
  }
79
 
81
 
80
  l = _fcalloc(1, sizeof(struct line) + slen + 1);
82
  l = _fcalloc(1, sizeof(struct line) + slen + 1);
81
  if (l == NULL) return(-1);
83
  if (l == NULL) return(-1);
82
 
84
 
83
  l->prev = db->cursor;
85
  l->prev = db->cursor;
84
  if (db->cursor) {
86
  if (db->cursor) {
85
    l->next = db->cursor->next;
87
    l->next = db->cursor->next;
86
    db->cursor->next = l;
88
    db->cursor->next = l;
87
    l->next->prev = l;
89
    l->next->prev = l;
88
  }
90
  }
89
  db->cursor = l;
91
  db->cursor = l;
90
  _fmemcpy(l->payload, line, slen);
92
  _fmemcpy(l->payload, line, slen);
91
  l->len = slen;
93
  l->len = slen;
92
 
94
 
93
  return(0);
95
  return(0);
94
}
96
}
95
 
97
 
96
 
98
 
97
static void db_rewind(struct file *db) {
99
static void db_rewind(struct file *db) {
98
  if (db->cursor == NULL) return;
100
  if (db->cursor == NULL) return;
99
  while (db->cursor->prev) db->cursor = db->cursor->prev;
101
  while (db->cursor->prev) db->cursor = db->cursor->prev;
100
}
102
}
101
 
103
 
102
 
104
 
103
static void load_colorscheme(void) {
105
static void load_colorscheme(void) {
104
  scheme[COL_TXT] = 0x17;
106
  scheme[COL_TXT] = 0x17;
105
  scheme[COL_STATUSBAR1] = 0x70;
107
  scheme[COL_STATUSBAR1] = 0x70;
106
  scheme[COL_STATUSBAR2] = 0x78;
108
  scheme[COL_STATUSBAR2] = 0x78;
107
  scheme[COL_SCROLLBAR] = 0x70;
109
  scheme[COL_SCROLLBAR] = 0x70;
108
}
110
}
109
 
111
 
110
 
112
 
111
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname, const struct file *db) {
113
static void ui_basic(unsigned char screenw, unsigned char screenh, const struct file *db) {
112
  unsigned char i;
114
  unsigned char i;
113
  const char *s = svarlang_strid(0); /* HELP */
115
  const char *s = svarlang_strid(0); /* HELP */
114
  unsigned char helpcol = screenw - (strlen(s) + 4);
116
  unsigned char helpcol = screenw - (strlen(s) + 4);
115
 
117
 
116
  /* fill status bar with background */
118
  /* fill status bar with background */
117
  mdr_cout_char_rep(screenh - 1, 0, ' ', scheme[COL_STATUSBAR1], screenw);
119
  mdr_cout_char_rep(screenh - 1, 0, ' ', scheme[COL_STATUSBAR1], screenw);
118
 
120
 
119
  /* filename */
121
  /* filename */
120
  mdr_cout_str(screenh - 1, 0, fname, scheme[COL_STATUSBAR1], screenw);
122
  mdr_cout_str(screenh - 1, 0, db->fname, scheme[COL_STATUSBAR1], screenw);
121
 
123
 
122
  /* eol type */
124
  /* eol type */
123
  if (db->lfonly) {
125
  if (db->lfonly) {
124
    mdr_cout_str(screenh - 1, helpcol - 3, "LF", scheme[COL_STATUSBAR1], 5);
126
    mdr_cout_str(screenh - 1, helpcol - 3, "LF", scheme[COL_STATUSBAR1], 5);
125
  } else {
127
  } else {
126
    mdr_cout_str(screenh - 1, helpcol - 5, "CRLF", scheme[COL_STATUSBAR1], 5);
128
    mdr_cout_str(screenh - 1, helpcol - 5, "CRLF", scheme[COL_STATUSBAR1], 5);
127
  }
129
  }
128
 
130
 
129
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
131
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
130
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
132
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
131
 
133
 
132
  /* scroll bar */
134
  /* scroll bar */
133
  for (i = 0; i < (screenh - 1); i++) {
135
  for (i = 0; i < (screenh - 1); i++) {
134
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
136
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
135
  }
137
  }
136
}
138
}
137
 
139
 
138
 
140
 
139
static void ui_msg(const char *msg, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
141
static void ui_msg(const char *msg, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
140
  unsigned short x, y, msglen, i;
142
  unsigned short x, y, msglen, i;
141
  msglen = strlen(msg);
143
  msglen = strlen(msg);
142
  y = (screenh - 4) >> 1;
144
  y = (screenh - 4) >> 1;
143
  x = (screenw - msglen - 4) >> 1;
145
  x = (screenw - msglen - 4) >> 1;
144
  for (i = y+2; i >= y; i--) mdr_cout_char_rep(i, x, ' ', scheme[COL_STATUSBAR1], msglen + 2);
146
  for (i = y+2; i >= y; i--) mdr_cout_char_rep(i, x, ' ', scheme[COL_STATUSBAR1], msglen + 2);
145
  mdr_cout_str(y+1, x+1, msg, scheme[COL_STATUSBAR1], msglen);
147
  mdr_cout_str(y+1, x+1, msg, scheme[COL_STATUSBAR1], msglen);
146
 
148
 
147
  if (*uidirtyfrom > y) *uidirtyfrom = y;
149
  if (*uidirtyfrom > y) *uidirtyfrom = y;
148
  if (*uidirtyto < y+2) *uidirtyto = y+2;
150
  if (*uidirtyto < y+2) *uidirtyto = y+2;
149
}
151
}
150
 
152
 
151
 
153
 
152
static void ui_help(unsigned char screenw) {
154
static void ui_help(unsigned char screenw) {
153
#define MAXLINLEN 35
155
#define MAXLINLEN 35
154
  unsigned short i, offset;
156
  unsigned short i, offset;
155
  offset = (screenw - MAXLINLEN + 1) >> 1;
157
  offset = (screenw - MAXLINLEN + 1) >> 1;
156
  mdr_cout_cursor_hide();
158
  mdr_cout_cursor_hide();
157
  for (i = 2; i <= 12; i++) {
159
  for (i = 2; i <= 12; i++) {
158
    mdr_cout_char_rep(i, offset - 2, ' ', scheme[COL_STATUSBAR1], MAXLINLEN);
160
    mdr_cout_char_rep(i, offset - 2, ' ', scheme[COL_STATUSBAR1], MAXLINLEN);
159
  }
161
  }
160
 
162
 
161
  mdr_cout_str(3, offset, svarlang_str(0, 0), scheme[COL_STATUSBAR1], MAXLINLEN);
163
  mdr_cout_str(3, offset, svarlang_str(0, 0), scheme[COL_STATUSBAR1], MAXLINLEN);
162
  for (i = 0; i <= 2; i++) {
164
  for (i = 0; i <= 2; i++) {
163
    mdr_cout_str(5 + i, offset, svarlang_str(8, i), scheme[COL_STATUSBAR1], MAXLINLEN);
165
    mdr_cout_str(5 + i, offset, svarlang_str(8, i), scheme[COL_STATUSBAR1], MAXLINLEN);
164
  }
166
  }
165
  mdr_cout_str(9, offset, svarlang_str(8, 10), scheme[COL_STATUSBAR1], MAXLINLEN);
167
  mdr_cout_str(9, offset, svarlang_str(8, 10), scheme[COL_STATUSBAR1], MAXLINLEN);
166
  mdr_cout_str(11, offset, svarlang_str(8, 11), scheme[COL_STATUSBAR1], MAXLINLEN);
168
  mdr_cout_str(11, offset, svarlang_str(8, 11), scheme[COL_STATUSBAR1], MAXLINLEN);
167
 
169
 
168
  keyb_getkey();
170
  keyb_getkey();
169
  mdr_cout_cursor_show();
171
  mdr_cout_cursor_show();
170
#undef MAXLINLEN
172
#undef MAXLINLEN
171
}
173
}
172
 
174
 
173
 
175
 
174
static void ui_refresh(const struct file *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto) {
176
static void ui_refresh(const struct file *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto) {
175
  unsigned char x;
177
  unsigned char x;
176
  const struct line far *l;
178
  const struct line far *l;
177
  unsigned char y = db->cursorposy;
179
  unsigned char y = db->cursorposy;
178
 
180
 
179
#ifdef DBG_REFRESH
181
#ifdef DBG_REFRESH
180
  static char m = 'a';
182
  static char m = 'a';
181
  m++;
183
  m++;
182
  if (m > 'z') m = 'a';
184
  if (m > 'z') m = 'a';
183
#endif
185
#endif
184
 
186
 
185
  /* rewind cursor line to first line that needs redrawing */
187
  /* rewind cursor line to first line that needs redrawing */
186
  for (l = db->cursor; y > uidirtyfrom; y--) l = l->prev;
188
  for (l = db->cursor; y > uidirtyfrom; y--) l = l->prev;
187
 
189
 
188
  /* iterate over lines and redraw whatever needs to be redrawn */
190
  /* iterate over lines and redraw whatever needs to be redrawn */
189
  for (; l != NULL; l = l->next, y++) {
191
  for (; l != NULL; l = l->next, y++) {
190
 
192
 
191
    /* skip lines that do not need to be refreshed */
193
    /* skip lines that do not need to be refreshed */
192
    if (y < uidirtyfrom) continue;
194
    if (y < uidirtyfrom) continue;
193
    if (y > uidirtyto) break;
195
    if (y > uidirtyto) break;
194
 
196
 
195
    x = 0;
197
    x = 0;
196
    if (db->xoffset < l->len) {
198
    if (db->xoffset < l->len) {
197
      unsigned char i, limit;
199
      unsigned char i, limit;
198
      if (l->len - db->xoffset < screenw) {
200
      if (l->len - db->xoffset < screenw) {
199
        limit = l->len;
201
        limit = l->len;
200
      } else {
202
      } else {
201
        limit = db->xoffset + screenw - 1;
203
        limit = db->xoffset + screenw - 1;
202
      }
204
      }
203
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], scheme[COL_TXT]);
205
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], scheme[COL_TXT]);
204
    }
206
    }
205
 
207
 
206
    /* write empty spaces until end of line */
208
    /* write empty spaces until end of line */
207
    if (x < screenw - 1) mdr_cout_char_rep(y, x, ' ', scheme[COL_TXT], screenw - 1 - x);
209
    if (x < screenw - 1) mdr_cout_char_rep(y, x, ' ', scheme[COL_TXT], screenw - 1 - x);
208
 
210
 
209
#ifdef DBG_REFRESH
211
#ifdef DBG_REFRESH
210
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
212
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
211
#endif
213
#endif
212
 
214
 
213
    if (y == screenh - 2) break;
215
    if (y == screenh - 2) break;
214
  }
216
  }
215
 
217
 
216
  /* fill all lines below if empty (and they need to be redrawn) */
218
  /* fill all lines below if empty (and they need to be redrawn) */
217
  if (l == NULL) {
219
  if (l == NULL) {
218
    while ((y < screenh - 1) && (y < uidirtyto)) {
220
    while ((y < screenh - 1) && (y < uidirtyto)) {
219
      mdr_cout_char_rep(y++, 0, ' ', scheme[COL_TXT], screenw - 1);
221
      mdr_cout_char_rep(y++, 0, ' ', scheme[COL_TXT], screenw - 1);
220
    }
222
    }
221
  }
223
  }
222
}
224
}
223
 
225
 
224
 
226
 
225
static void check_cursor_not_after_eol(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
227
static void check_cursor_not_after_eol(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
226
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
228
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
227
 
229
 
228
  if (db->cursor->len < db->xoffset) {
230
  if (db->cursor->len < db->xoffset) {
229
    db->cursorposx = 0;
231
    db->cursorposx = 0;
230
    db->xoffset = db->cursor->len;
232
    db->xoffset = db->cursor->len;
231
    *uidirtyfrom = 0;
233
    *uidirtyfrom = 0;
232
    *uidirtyto = 0xff;
234
    *uidirtyto = 0xff;
233
  } else {
235
  } else {
234
    db->cursorposx = db->cursor->len - db->xoffset;
236
    db->cursorposx = db->cursor->len - db->xoffset;
235
  }
237
  }
236
}
238
}
237
 
239
 
238
 
240
 
239
static void cursor_up(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
241
static void cursor_up(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
240
  if (db->cursor->prev != NULL) {
242
  if (db->cursor->prev != NULL) {
241
    db->cursor = db->cursor->prev;
243
    db->cursor = db->cursor->prev;
242
    if (db->cursorposy == 0) {
244
    if (db->cursorposy == 0) {
243
      *uidirtyfrom = 0;
245
      *uidirtyfrom = 0;
244
      *uidirtyto = 0xff;
246
      *uidirtyto = 0xff;
245
    } else {
247
    } else {
246
      db->cursorposy -= 1;
248
      db->cursorposy -= 1;
247
    }
249
    }
248
  }
250
  }
249
}
251
}
250
 
252
 
251
 
253
 
252
static void cursor_eol(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
254
static void cursor_eol(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
253
  /* adjust xoffset to make sure eol is visible on screen */
255
  /* adjust xoffset to make sure eol is visible on screen */
254
  if (db->xoffset > db->cursor->len) {
256
  if (db->xoffset > db->cursor->len) {
255
    db->xoffset = db->cursor->len - 1;
257
    db->xoffset = db->cursor->len - 1;
256
    *uidirtyfrom = 0;
258
    *uidirtyfrom = 0;
257
    *uidirtyto = 0xff;
259
    *uidirtyto = 0xff;
258
  }
260
  }
259
 
261
 
260
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
262
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
261
    db->xoffset = db->cursor->len - screenw + 2;
263
    db->xoffset = db->cursor->len - screenw + 2;
262
    *uidirtyfrom = 0;
264
    *uidirtyfrom = 0;
263
    *uidirtyto = 0xff;
265
    *uidirtyto = 0xff;
264
  }
266
  }
265
  db->cursorposx = db->cursor->len - db->xoffset;
267
  db->cursorposx = db->cursor->len - db->xoffset;
266
}
268
}
267
 
269
 
268
 
270
 
269
static void cursor_down(struct file *db, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
271
static void cursor_down(struct file *db, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
270
  if (db->cursor->next != NULL) {
272
  if (db->cursor->next != NULL) {
271
    db->cursor = db->cursor->next;
273
    db->cursor = db->cursor->next;
272
    if (db->cursorposy < screenh - 2) {
274
    if (db->cursorposy < screenh - 2) {
273
      db->cursorposy += 1;
275
      db->cursorposy += 1;
274
    } else {
276
    } else {
275
      *uidirtyfrom = 0;
277
      *uidirtyfrom = 0;
276
      *uidirtyto = 0xff;
278
      *uidirtyto = 0xff;
277
    }
279
    }
278
  }
280
  }
279
}
281
}
280
 
282
 
281
 
283
 
282
static void cursor_left(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
284
static void cursor_left(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
283
  if (db->cursorposx > 0) {
285
  if (db->cursorposx > 0) {
284
    db->cursorposx -= 1;
286
    db->cursorposx -= 1;
285
  } else if (db->xoffset > 0) {
287
  } else if (db->xoffset > 0) {
286
    db->xoffset -= 1;
288
    db->xoffset -= 1;
287
    *uidirtyfrom = 0;
289
    *uidirtyfrom = 0;
288
    *uidirtyto = 0xff;
290
    *uidirtyto = 0xff;
289
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
291
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
290
    cursor_up(db, uidirtyfrom, uidirtyto);
292
    cursor_up(db, uidirtyfrom, uidirtyto);
291
    cursor_eol(db, screenw, uidirtyfrom, uidirtyto);
293
    cursor_eol(db, screenw, uidirtyfrom, uidirtyto);
292
  }
294
  }
293
}
295
}
294
 
296
 
295
 
297
 
296
static void cursor_home(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
298
static void cursor_home(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
297
  db->cursorposx = 0;
299
  db->cursorposx = 0;
298
  if (db->xoffset != 0) {
300
  if (db->xoffset != 0) {
299
    db->xoffset = 0;
301
    db->xoffset = 0;
300
    *uidirtyfrom = 0;
302
    *uidirtyfrom = 0;
301
    *uidirtyto = 0xff;
303
    *uidirtyto = 0xff;
302
  }
304
  }
303
}
305
}
304
 
306
 
305
 
307
 
306
static void cursor_right(struct file *db, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
308
static void cursor_right(struct file *db, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
307
  if (db->cursor->len > db->xoffset + db->cursorposx) {
309
  if (db->cursor->len > db->xoffset + db->cursorposx) {
308
    if (db->cursorposx < screenw - 2) {
310
    if (db->cursorposx < screenw - 2) {
309
      db->cursorposx += 1;
311
      db->cursorposx += 1;
310
    } else {
312
    } else {
311
      db->xoffset += 1;
313
      db->xoffset += 1;
312
      *uidirtyfrom = 0;
314
      *uidirtyfrom = 0;
313
      *uidirtyto = 0xff;
315
      *uidirtyto = 0xff;
314
    }
316
    }
315
  } else {
317
  } else {
316
    cursor_down(db, screenh, uidirtyfrom, uidirtyto);
318
    cursor_down(db, screenh, uidirtyfrom, uidirtyto);
317
    cursor_home(db, uidirtyfrom, uidirtyto);
319
    cursor_home(db, uidirtyfrom, uidirtyto);
318
  }
320
  }
319
}
321
}
320
 
322
 
321
 
323
 
322
static void del(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
324
static void del(struct file *db, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
323
  if (db->cursorposx + db->xoffset < db->cursor->len) {
325
  if (db->cursorposx + db->xoffset < db->cursor->len) {
324
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
326
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
325
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
327
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
326
    *uidirtyfrom = db->cursorposy;
328
    *uidirtyfrom = db->cursorposy;
327
    *uidirtyto = db->cursorposy;
329
    *uidirtyto = db->cursorposy;
328
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
330
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
329
    struct line far *nextline = db->cursor->next;
331
    struct line far *nextline = db->cursor->next;
330
    if (db->cursor->next->len > 0) {
332
    if (db->cursor->next->len > 0) {
331
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
333
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
332
      if (newptr != NULL) {
334
      if (newptr != NULL) {
333
        db->cursor = newptr;
335
        db->cursor = newptr;
334
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
336
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
335
        db->cursor->len += db->cursor->next->len;
337
        db->cursor->len += db->cursor->next->len;
336
      }
338
      }
337
    }
339
    }
338
    db->cursor->next = db->cursor->next->next;
340
    db->cursor->next = db->cursor->next->next;
339
    db->cursor->next->prev = db->cursor;
341
    db->cursor->next->prev = db->cursor;
340
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
342
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
341
    _ffree(nextline);
343
    _ffree(nextline);
342
    *uidirtyfrom = db->cursorposy;
344
    *uidirtyfrom = db->cursorposy;
343
    *uidirtyto = 0xff;
345
    *uidirtyto = 0xff;
344
  }
346
  }
345
}
347
}
346
 
348
 
347
 
349
 
348
static void bkspc(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
350
static void bkspc(struct file *db, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
349
 
351
 
350
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
352
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
351
  if ((db->cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
353
  if ((db->cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
352
 
354
 
353
  cursor_left(db, screenw, uidirtyfrom, uidirtyto);
355
  cursor_left(db, screenw, uidirtyfrom, uidirtyto);
354
  del(db, uidirtyfrom, uidirtyto);
356
  del(db, uidirtyfrom, uidirtyto);
355
}
357
}
356
 
358
 
357
 
359
 
358
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
360
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
359
 * of argc and argv, saves some 330 bytes of binary size */
361
 * of argc and argv, saves some 330 bytes of binary size */
360
static char *parseargv(void) {
362
static char *parseargv(void) {
361
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
363
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
362
  unsigned char count = 0;
364
  unsigned char count = 0;
363
  char *argv[4];
365
  char *argv[4];
364
 
366
 
365
  while (count < 4) {
367
  while (count < 4) {
366
    /* jump to nearest arg */
368
    /* jump to nearest arg */
367
    while (*tail == ' ') {
369
    while (*tail == ' ') {
368
      *tail = 0;
370
      *tail = 0;
369
      tail++;
371
      tail++;
370
    }
372
    }
371
 
373
 
372
    if (*tail == '\r') {
374
    if (*tail == '\r') {
373
      *tail = 0;
375
      *tail = 0;
374
      break;
376
      break;
375
    }
377
    }
376
 
378
 
377
    argv[count++] = tail;
379
    argv[count++] = tail;
378
 
380
 
379
    /* jump to next delimiter */
381
    /* jump to next delimiter */
380
    while ((*tail != ' ') && (*tail != '\r')) tail++;
382
    while ((*tail != ' ') && (*tail != '\r')) tail++;
381
  }
383
  }
382
 
384
 
383
  /* check args now */
385
  /* check args now */
384
  if (count != 1) return(NULL);
386
  if (count != 1) return(NULL);
385
 
387
 
386
  return(argv[0]);
388
  return(argv[0]);
387
}
389
}
388
 
390
 
389
 
391
 
390
static int loadfile(struct file *db, const char *fname) {
392
static struct file *loadfile(const char *fname) {
391
  char buff[1024];
393
  char buff[1024];
392
  unsigned int prevlen = 0, len, llen;
394
  unsigned int prevlen = 0, len, llen;
393
  int fd;
395
  int fd;
394
  int r = 0;
396
  struct file *db;
395
 
397
 
396
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
398
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
397
    mdr_coutraw_puts("Failed to open file:");
399
    mdr_coutraw_puts("Failed to open file:");
398
    mdr_coutraw_puts(fname);
400
    mdr_coutraw_puts(fname);
399
    return(-1);
401
    return(NULL);
400
  }
402
  }
401
 
403
 
-
 
404
  len = strlen(fname) + 1;
-
 
405
  db = calloc(1, sizeof(struct file) + len);
-
 
406
  if (db == NULL) {
-
 
407
    _dos_close(fd);
-
 
408
    return(NULL);
-
 
409
  }
-
 
410
  memcpy(db->fname, fname, len);
-
 
411
 
402
  db->lfonly = 1;
412
  db->lfonly = 1;
403
 
413
 
404
  do {
414
  do {
405
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
415
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
406
      len += prevlen;
416
      len += prevlen;
407
    } else {
417
    } else {
408
      len = prevlen;
418
      len = prevlen;
409
    }
419
    }
410
 
420
 
411
    /* look for nearest \n and replace with 0*/
421
    /* look for nearest \n and replace with 0*/
412
    for (llen = 0; buff[llen] != '\n'; llen++) {
422
    for (llen = 0; buff[llen] != '\n'; llen++) {
413
      if (llen == sizeof(buff)) break;
423
      if (llen == sizeof(buff)) { /* TODO line too long: handle it in some classy way */
-
 
424
        break;
-
 
425
      }
414
    }
426
    }
415
    buff[llen] = 0;
427
    buff[llen] = 0;
416
    if ((llen > 0) && (buff[llen - 1] == '\r')) {
428
    if ((llen > 0) && (buff[llen - 1] == '\r')) {
417
      buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
429
      buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
418
      db->lfonly = 0;
430
      db->lfonly = 0;
419
    }
431
    }
420
    if (line_add(db, buff) != 0) {
432
    if (line_add(db, buff) != 0) {
421
      mdr_coutraw_puts("out of memory");
433
      mdr_coutraw_puts("out of memory");
-
 
434
      free(db);
422
      r = -1;
435
      db = NULL;
423
      break;
436
      break;
424
    }
437
    }
425
 
438
 
426
    len -= llen + 1;
439
    len -= llen + 1;
427
    memmove(buff, buff + llen + 1, len);
440
    memmove(buff, buff + llen + 1, len);
428
    prevlen = len;
441
    prevlen = len;
429
  } while (len > 0);
442
  } while (len > 0);
430
 
443
 
431
  _dos_close(fd);
444
  _dos_close(fd);
432
 
445
 
-
 
446
  /* add an empty line at end if not present already, also rewind cursor to top of file */
-
 
447
  if (db != NULL) {
-
 
448
    if (db->cursor->len != 0) line_add(db, "");
-
 
449
    db_rewind(db);
-
 
450
  }
-
 
451
 
433
  return(r);
452
  return(db);
434
}
453
}
435
 
454
 
436
 
455
 
437
static int savefile(const struct file *db, const char *fname) {
456
static int savefile(const struct file *db) {
438
  int fd;
457
  int fd;
439
  const struct line far *l;
458
  const struct line far *l;
440
  unsigned bytes;
459
  unsigned bytes;
441
  unsigned char eollen;
460
  unsigned char eollen;
442
  unsigned char eolbuf[2];
461
  unsigned char eolbuf[2];
443
 
462
 
444
  if (_dos_open(fname, O_WRONLY, &fd) != 0) {
463
  if (_dos_open(db->fname, O_WRONLY, &fd) != 0) {
445
    return(-1);
464
    return(-1);
446
  }
465
  }
447
 
466
 
448
  l = db->cursor;
467
  l = db->cursor;
449
  while (l->prev) l = l->prev;
468
  while (l->prev) l = l->prev;
450
 
469
 
451
  /* preset line terminators */
470
  /* preset line terminators */
452
  if (db->lfonly) {
471
  if (db->lfonly) {
453
    eolbuf[0] = '\n';
472
    eolbuf[0] = '\n';
454
    eollen = 1;
473
    eollen = 1;
455
  } else {
474
  } else {
456
    eolbuf[0] = '\r';
475
    eolbuf[0] = '\r';
457
    eolbuf[1] = '\n';
476
    eolbuf[1] = '\n';
458
    eollen = 2;
477
    eollen = 2;
459
  }
478
  }
460
 
479
 
461
  while (l) {
480
  while (l) {
462
    _dos_write(fd, l->payload, l->len, &bytes);
481
    _dos_write(fd, l->payload, l->len, &bytes);
463
    _dos_write(fd, eolbuf, eollen, &bytes);
482
    _dos_write(fd, eolbuf, eollen, &bytes);
464
    l = l->next;
483
    l = l->next;
465
  }
484
  }
466
 
485
 
467
  _dos_close(fd);
486
  _dos_close(fd);
468
  return(0);
487
  return(0);
469
}
488
}
470
 
489
 
471
 
490
 
472
static void insert_in_line(struct file *db, const char *databuf, unsigned short len, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
491
static void insert_in_line(struct file *db, const char *databuf, unsigned short len, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
473
  struct line far *n;
492
  struct line far *n;
474
  n = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + len);
493
  n = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + len);
475
  if (n != NULL) {
494
  if (n != NULL) {
476
    unsigned short off = db->xoffset + db->cursorposx;
495
    unsigned short off = db->xoffset + db->cursorposx;
477
    if (n->prev) n->prev->next = n;
496
    if (n->prev) n->prev->next = n;
478
    if (n->next) n->next->prev = n;
497
    if (n->next) n->next->prev = n;
479
    db->cursor = n;
498
    db->cursor = n;
480
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
499
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
481
    db->cursor->len += len;
500
    db->cursor->len += len;
482
    *uidirtyfrom = db->cursorposy;
501
    *uidirtyfrom = db->cursorposy;
483
    *uidirtyto = db->cursorposy;
502
    *uidirtyto = db->cursorposy;
484
    while (len--) {
503
    while (len--) {
485
      db->cursor->payload[off++] = *databuf;
504
      db->cursor->payload[off++] = *databuf;
486
      databuf++;
505
      databuf++;
487
      cursor_right(db, screenw, screenh, uidirtyfrom, uidirtyto);
506
      cursor_right(db, screenw, screenh, uidirtyfrom, uidirtyto);
488
    }
507
    }
489
  }
508
  }
490
}
509
}
491
 
510
 
492
 
511
 
493
int main(void) {
512
int main(void) {
494
  const char *fname;
513
  const char *fname;
495
  struct file db;
514
  struct file *db;
496
  unsigned char screenw = 0, screenh = 0;
515
  unsigned char screenw = 0, screenh = 0;
497
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
516
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
498
 
517
 
499
  bzero(&db, sizeof(db));
-
 
500
 
-
 
501
  {
518
  {
502
    char nlspath[128], lang[8];
519
    char nlspath[128], lang[8];
503
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
520
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
504
  }
521
  }
505
 
522
 
506
  fname = parseargv();
523
  fname = parseargv();
507
 
524
 
508
  if (fname == NULL) {
525
  if (fname == NULL) {
509
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
526
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
510
    return(0);
527
    return(0);
511
  }
528
  }
512
 
529
 
513
  /* load file */
530
  /* load file */
514
  if (loadfile(&db, fname) != 0) return(1);
531
  db = loadfile(fname);
515
 
-
 
516
  /* add an empty line at end if not present already */
-
 
517
  if (db.cursor->len != 0) line_add(&db, "");
532
  if (db == NULL) return(1);
518
 
533
 
519
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
534
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
520
  ui_basic(screenw, screenh, fname, &db);
535
  ui_basic(screenw, screenh, db);
521
 
-
 
522
  db_rewind(&db);
-
 
523
 
536
 
524
  for (;;) {
537
  for (;;) {
525
    int k;
538
    int k;
526
 
539
 
527
    check_cursor_not_after_eol(&db, &uidirtyfrom, &uidirtyto);
540
    check_cursor_not_after_eol(db, &uidirtyfrom, &uidirtyto);
528
    mdr_cout_locate(db.cursorposy, db.cursorposx);
541
    mdr_cout_locate(db->cursorposy, db->cursorposx);
529
 
542
 
530
    if (uidirtyfrom != 0xff) {
543
    if (uidirtyfrom != 0xff) {
531
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto);
544
      ui_refresh(db, screenw, screenh, uidirtyfrom, uidirtyto);
532
      uidirtyfrom = 0xff;
545
      uidirtyfrom = 0xff;
533
    }
546
    }
534
 
547
 
535
    k = keyb_getkey();
548
    k = keyb_getkey();
536
 
549
 
537
    if (k == 0x150) { /* down */
550
    if (k == 0x150) { /* down */
538
      cursor_down(&db, screenh, &uidirtyfrom, &uidirtyto);
551
      cursor_down(db, screenh, &uidirtyfrom, &uidirtyto);
539
 
552
 
540
    } else if (k == 0x148) { /* up */
553
    } else if (k == 0x148) { /* up */
541
      cursor_up(&db, &uidirtyfrom, &uidirtyto);
554
      cursor_up(db, &uidirtyfrom, &uidirtyto);
542
 
555
 
543
    } else if (k == 0x14D) { /* right */
556
    } else if (k == 0x14D) { /* right */
544
      cursor_right(&db, screenw, screenh, &uidirtyfrom, &uidirtyto);
557
      cursor_right(db, screenw, screenh, &uidirtyfrom, &uidirtyto);
545
 
558
 
546
    } else if (k == 0x14B) { /* left */
559
    } else if (k == 0x14B) { /* left */
547
      cursor_left(&db, screenw, &uidirtyfrom, &uidirtyto);
560
      cursor_left(db, screenw, &uidirtyfrom, &uidirtyto);
548
 
561
 
549
    } else if (k == 0x149) { /* pgup */
562
    } else if (k == 0x149) { /* pgup */
550
      // TODO
563
      // TODO
551
 
564
 
552
    } else if (k == 0x151) { /* pgdown */
565
    } else if (k == 0x151) { /* pgdown */
553
      // TODO
566
      // TODO
554
 
567
 
555
    } else if (k == 0x147) { /* home */
568
    } else if (k == 0x147) { /* home */
556
       cursor_home(&db, &uidirtyfrom, &uidirtyto);
569
       cursor_home(db, &uidirtyfrom, &uidirtyto);
557
 
570
 
558
    } else if (k == 0x14F) { /* end */
571
    } else if (k == 0x14F) { /* end */
559
       cursor_eol(&db, screenw, &uidirtyfrom, &uidirtyto);
572
       cursor_eol(db, screenw, &uidirtyfrom, &uidirtyto);
560
 
573
 
561
    } else if (k == 0x1B) { /* ESC */
574
    } else if (k == 0x1B) { /* ESC */
562
      break;
575
      break;
563
 
576
 
564
    } else if (k == 0x0D) { /* ENTER */
577
    } else if (k == 0x0D) { /* ENTER */
565
      /* add a new line */
578
      /* add a new line */
566
      if (line_add(&db, db.cursor->payload + db.xoffset + db.cursorposx) == 0) {
579
      if (line_add(db, db->cursor->payload + db->xoffset + db->cursorposx) == 0) {
567
        /* trim the line above */
580
        /* trim the line above */
568
        db.cursor->prev->len = db.xoffset + db.cursorposx;
581
        db->cursor->prev->len = db->xoffset + db->cursorposx;
569
        db.cursor->prev->payload[db.cursor->prev->len] = 0;
582
        db->cursor->prev->payload[db->cursor->prev->len] = 0;
570
        /* move cursor to the (new) line below */
583
        /* move cursor to the (new) line below */
571
        db.cursorposx = 0;
584
        db->cursorposx = 0;
572
        if (db.cursorposy < screenh - 2) {
585
        if (db->cursorposy < screenh - 2) {
573
          uidirtyfrom = db.cursorposy;
586
          uidirtyfrom = db->cursorposy;
574
          db.cursorposy++;
587
          db->cursorposy++;
575
        } else {
588
        } else {
576
          uidirtyfrom = 0;
589
          uidirtyfrom = 0;
577
        }
590
        }
578
        uidirtyto = 0xff;
591
        uidirtyto = 0xff;
579
      } else {
592
      } else {
580
        /* ERROR: OUT OF MEMORY */
593
        /* ERROR: OUT OF MEMORY */
581
      }
594
      }
582
 
595
 
583
    } else if (k == 0x153) {  /* DEL */
596
    } else if (k == 0x153) {  /* DEL */
584
      del(&db, &uidirtyfrom, &uidirtyto);
597
      del(db, &uidirtyfrom, &uidirtyto);
585
 
598
 
586
    } else if (k == 0x008) { /* BKSPC */
599
    } else if (k == 0x008) { /* BKSPC */
587
      bkspc(&db, screenw, &uidirtyfrom, &uidirtyto);
600
      bkspc(db, screenw, &uidirtyfrom, &uidirtyto);
588
 
601
 
589
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
602
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
590
      char c = k;
603
      char c = k;
591
      insert_in_line(&db, &c, 1, screenw, screenh, &uidirtyfrom, &uidirtyto);
604
      insert_in_line(db, &c, 1, screenw, screenh, &uidirtyfrom, &uidirtyto);
592
 
605
 
593
    } else if (k == 0x009) { /* TAB */
606
    } else if (k == 0x009) { /* TAB */
594
      const char *tab = "        ";
607
      const char *tab = "        ";
595
      insert_in_line(&db, tab, 8, screenw, screenh, &uidirtyfrom, &uidirtyto);
608
      insert_in_line(db, tab, 8, screenw, screenh, &uidirtyfrom, &uidirtyto);
596
 
609
 
597
    } else if (k == 0x13b) { /* F1 */
610
    } else if (k == 0x13b) { /* F1 */
598
      ui_help(screenw);
611
      ui_help(screenw);
599
      uidirtyfrom = 0;
612
      uidirtyfrom = 0;
600
      uidirtyto = 0xff;
613
      uidirtyto = 0xff;
601
 
614
 
602
    } else if (k == 0x13f) { /* F5 */
615
    } else if (k == 0x13f) { /* F5 */
603
      const char *m[] = {"SAVED", "SAVING FILE FAILED"};
616
      const char *m[] = {"SAVED", "SAVING FILE FAILED"};
604
      int i;
617
      int i;
605
      i = !!savefile(&db, fname);
618
      i = !!savefile(db);
606
      ui_msg(m[i], screenw, screenh, &uidirtyfrom, &uidirtyto);
619
      ui_msg(m[i], screenw, screenh, &uidirtyfrom, &uidirtyto);
607
      mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
620
      mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
608
 
621
 
609
    } else if (k == 0x144) { /* F10 */
622
    } else if (k == 0x144) { /* F10 */
610
      db.lfonly ^= 1;
623
      db->lfonly ^= 1;
611
      ui_basic(screenw, screenh, fname, &db);
624
      ui_basic(screenw, screenh, db);
612
 
625
 
613
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
626
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
614
      char buff[4];
627
      char buff[4];
615
      const char *HEX = "0123456789ABCDEF";
628
      const char *HEX = "0123456789ABCDEF";
616
      buff[0] = HEX[(k >> 8) & 15];
629
      buff[0] = HEX[(k >> 8) & 15];
617
      buff[1] = HEX[(k >> 4) & 15];
630
      buff[1] = HEX[(k >> 4) & 15];
618
      buff[2] = HEX[k & 15];
631
      buff[2] = HEX[k & 15];
619
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
632
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
620
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
633
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
621
      keyb_getkey();
634
      keyb_getkey();
622
      break;
635
      break;
623
    }
636
    }
624
  }
637
  }
625
 
638
 
626
  mdr_cout_close();
639
  mdr_cout_close();
627
 
640
 
628
  /* TODO free memory */
641
  /* no need to free memory, DOS will do it for me */
629
 
642
 
630
  return(0);
643
  return(0);
631
}
644
}
632
 
645