Subversion Repositories SvarDOS

Rev

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

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