-
Notifications
You must be signed in to change notification settings - Fork 1
/
dosmenu.c
404 lines (363 loc) · 12.3 KB
/
dosmenu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <dir.h>
#include <ctype.h>
#define true 1
#define false 0
void abortMsg(char *msg);
void * xmalloc(size_t size) {
void *r = malloc(size);
if (r == NULL)
abortMsg("Memory allocation failed");
return r;
}
void resetVideo(void) {
textmode(C80);
textcolor(LIGHTGRAY);
textbackground(BLACK);
_setcursortype(_NORMALCURSOR);
clrscr();
}
void abortMsg(char *msg) {
resetVideo();
printf("ERROR: %s\n\n", msg);
exit(1);
}
struct setting {
char *name;
char *value;
struct setting *next;
};
struct section {
char *name;
struct setting *settings;
struct section *next;
};
struct section * newSection(char *name) {
struct section * s = (struct section*) xmalloc(sizeof(struct section));
s->name = strdup(name);
s->settings = NULL;
s->next = NULL;
return s;
}
struct setting * newSetting(char *name, char* value) {
struct setting * s = (struct setting*) xmalloc(sizeof(struct setting));
s->name = strdup(name);
s->value = strdup(value);
s->next = NULL;
return s;
}
void addSetting(struct section *section, char* name, char* value) {
struct setting *cur = section->settings;
if (cur == NULL) {
section->settings = newSetting(name,value);
return;
}
while (cur->next != NULL)
cur = cur->next;
cur->next = newSetting(name,value);
}
enum COLORS colorByName(char *name) {
if (strcmp("BLACK",name) == 0) return BLACK;
if (strcmp("BLUE",name) == 0) return BLUE;
if (strcmp("GREEN",name) == 0) return GREEN;
if (strcmp("CYAN",name) == 0) return CYAN;
if (strcmp("RED",name) == 0) return RED;
if (strcmp("MAGENTA",name) == 0) return MAGENTA;
if (strcmp("BROWN",name) == 0) return BROWN;
if (strcmp("LIGHTGRAY",name) == 0) return LIGHTGRAY;
if (strcmp("DARKGRAY",name) == 0) return DARKGRAY;
if (strcmp("LIGHTBLUE",name) == 0) return LIGHTBLUE;
if (strcmp("LIGHTGREEN",name) == 0) return LIGHTGREEN;
if (strcmp("LIGHTCYAN",name) == 0) return LIGHTCYAN;
if (strcmp("LIGHTRED",name) == 0) return LIGHTRED;
if (strcmp("LIGHTMAGENTA",name) == 0) return LIGHTMAGENTA;
if (strcmp("YELLOW",name) == 0) return YELLOW;
if (strcmp("WHITE",name) == 0) return WHITE;
return -1;
}
#define LINE_MAX 256
char *readLine(FILE *fp) {
char *buf;
int ch, i = 0;
buf = xmalloc(LINE_MAX);
memset(buf, 0, LINE_MAX);
while (true) {
ch = fgetc(fp);
if (ch == EOF || ch == '\n')
return buf;
if (i < (LINE_MAX-1) && ch != '\r')
buf[i++] = ch;
}
}
struct section *addSection(struct section *sections, char *name) {
while (sections->next != NULL)
sections = sections->next;
return sections->next = newSection(name);
}
struct section *getSectionByIndex(struct section *sections, int index) {
while (true) {
if (sections == NULL || index < 0)
return NULL;
if (index == 0)
return sections;
sections = sections->next;
index--;
}
}
struct section *getSection(struct section *sections, char *name) {
while (true) {
if (sections == NULL)
return NULL;
if (strcmp(sections->name,name) == 0)
return sections;
sections = sections->next;
}
}
char * getSettingInSection(struct section *section, char *name) {
struct setting *setting;
if (section == NULL)
return NULL;
setting = section->settings;
while (true) {
if (setting == NULL)
return NULL;
if (strcmp(setting->name,name) == 0)
return setting->value;
setting = setting->next;
}
}
char *getSetting(struct section *sections, char *section, char*name) {
sections = getSection(sections,section);
if (sections == NULL)
return NULL;
return getSettingInSection(sections,name);
}
int isCommentLine(char *line) {
for (; *line != 0; line++) {
if (*line == ';')
return true;
if (!isspace(*line))
return false;
}
return false;
}
struct section *loadConfig(void) {
FILE *cfg = NULL;
struct section *sections = newSection("");
struct section *curSection = sections;
cfg = fopen("dosmenu.cfg","r");
if (cfg == NULL)
abortMsg("Opening dosMenu.cfg failed");
while (true) {
char *line = readLine(cfg);
if (strlen(line) == 0) {
free(line);
break;
}
// Skip comment lines
if (isCommentLine(line)) {
free(line);
continue;
}
if (strchr(line,'[') == line && strchr(line,']') != NULL) {
char *sect = line + 1;
char *endSect = strchr(line,']');
*endSect = 0;
curSection = addSection(sections,sect);
}
else {
char *kvs = strchr(line,'=');
char *val = kvs+1;
if (kvs == NULL) {
resetVideo();
printf("ERROR: Bad syntax in input line [%s]\n", line);
exit(1);
}
*kvs = 0;
addSetting(curSection, line, val);
}
free(line);
}
fclose(cfg);
return sections;
}
void centerText(int line, char *msg) {
int len = strlen(msg);
int col = ((80 - len) / 2) + 1;
gotoxy(col,line);
cputs(msg);
}
int readScanCode() {
union REGS in, out;
in.h.ah = 0;
int86(0x16, &in, &out);
return out.h.ah;
}
#define UP 72
#define DOWN 80
#define ENTER 28
#define SPACE 57
#define ESC 1
void drawScreen(struct section *sections, char *title, int selected, int borderColor, int horizGap) {
int i;
char attrByte;
clrscr();
gotoxy(1 + horizGap,1);
textbackground(BLACK);
textcolor(borderColor);
gotoxy(1 + horizGap,1);
putch(201);
for (i = 1 + horizGap; i < 79 - horizGap; i++)
putch(205);
putch(187);
gotoxy(1 + horizGap,2);
putch(186);
gotoxy(80 - horizGap,2);
putch(186);
gotoxy(1 + horizGap,3);
putch(204);
for (i = 1 + horizGap; i < 79 - horizGap; i++)
putch(205);
putch(185);
for (i = 4; i < 25; i++) {
gotoxy(1+horizGap,i);
putch(186);
gotoxy(80-horizGap,i);
putch(186);
}
gotoxy(1+horizGap,25);
putch(200);
for (i = 1+horizGap; i < 79-horizGap; i++)
putch(205);
if (horizGap == 0) {
attrByte = peekb(0xB800, 1);
pokeb(0xB800, ((25*80)-1)*2, 188);
pokeb(0xB800, (((25*80)-1)*2)+1, attrByte);
}
else {
gotoxy(80-horizGap,25);
putch(188);
}
textcolor(YELLOW);
centerText(2, title);
for (i = 1; i < 22; i++) {
struct section * nth = getSectionByIndex(sections, i);
if (nth == NULL)
break;
textbackground(i==selected ? RED: BLACK);
textcolor(YELLOW);
centerText(3+i, nth->name);
}
}
void redrawLine(struct section *sections, int line, int isSelected) {
struct section * nth = getSectionByIndex(sections, line);
if (nth == NULL)
return;
textbackground(isSelected ? RED: BLACK);
textcolor(YELLOW);
centerText(3+line, nth->name);
}
int countSections(struct section *sections) {
int i = 0;
while (sections != NULL) {
i++;
sections = sections->next;
}
return i;
}
// Check if a key is pending.
// Key is pending if head and tail pointers of keyboard buffer in
// BIOS data area (BDA) are different.
int keyPending() {
int head, tail;
disable();
head = peek(0x40,0x1A); // 40:1A = keybuf head pointer
tail = peek(0x40,0x1C); // 40:1C = keybuf tail pointer
enable();
return head != tail;
}
// Ignore all pending keys
// This helps consume any stray keys after exiting
void drainInput() {
while (keyPending())
readScanCode();
}
int main(int argc, char**argv) {
struct section *sections;
char *title, *saveCWD, *borderColorName, *strHBorder;
int scanCode, inputLoop,selected, actionEnter, sectionCount, borderColor;
int horizGap=0;
saveCWD = xmalloc(256);
getcwd(saveCWD, 256);
resetVideo();
_setcursortype(_NOCURSOR);
sections = loadConfig();
sectionCount = countSections(sections);
title = getSetting(sections, "", "title");
if (title == NULL)
abortMsg("Setting 'title' not set");
strHBorder = getSetting(sections, "", "horizgap");
if (strHBorder != NULL)
horizGap = atoi(strHBorder);
if (horizGap < 0)
abortMsg("Setting 'horizgap' has invalid value");
borderColor = LIGHTGREEN;
borderColorName = getSetting(sections, "", "border");
if (borderColorName != NULL) {
borderColor = colorByName(borderColorName);
if (borderColor < 0)
abortMsg("Setting 'border' has invalid value");
}
while (true) {
actionEnter = false;
selected = 1;
inputLoop = true;
drawScreen(sections, title, selected, borderColor, horizGap);
while (inputLoop) {
scanCode = readScanCode();
switch (scanCode) {
case DOWN:
redrawLine(sections,selected,false);
if (selected < 21 && selected < (sectionCount-1))
selected++;
redrawLine(sections,selected,true);
break;
case UP:
if (selected > 1) {
redrawLine(sections,selected,false);
selected--;
redrawLine(sections,selected,true);
}
break;
case SPACE:
case ENTER:
actionEnter = true;
inputLoop = false;
break;
case ESC:
resetVideo();
return 0;
default:
// Ignore unknown char
break;
}
}
resetVideo();
if (actionEnter) {
struct section * nth = getSectionByIndex(sections, selected);
char *dir = getSettingInSection(nth, "dir");
char *run = getSettingInSection(nth, "run");
if (dir != NULL)
chdir(dir);
system(run);
chdir(saveCWD);
resetVideo();
_setcursortype(_NOCURSOR);
drainInput();
}
}
}