Subversion Repositories SvarDOS

Rev

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

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