aboutsummaryrefslogtreecommitdiffstats
path: root/com32/modules/menu.c
blob: c9794c246c271de43781e6006ccf931d472413fb (plain)
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
#ident "$Id$"
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2004 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * menu.c
 *
 * Simple menu system which displays a list and allows the user to select
 * a command line and/or edit it.
 */

#define _GNU_SOURCE		/* Needed for asprintf() on Linux */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <consoles.h>
#include <getkey.h>
#include <minmax.h>
#ifdef __COM32__
#include <com32.h>
#endif

#include "menu.h"

struct menu_attrib {
  const char *border;		/* Border area */
  const char *title;		/* Title bar */
  const char *unsel;		/* Unselected menu item */
  const char *sel;		/* Selected */
  const char *more;		/* [More] tag */
  const char *tabmsg;		/* Press [Tab] message */
  const char *cmdmark;		/* Command line marker */
  const char *cmdline;		/* Command line */
  const char *screen;		/* Rest of the screen */
};

const struct menu_attrib default_attrib = {
  .border  = "\033[0;30;44m",
  .title   = "\033[1;36;44m",
  .unsel   = "\033[0;37;44m",
  .sel     = "\033[0;30;47m",
  .more    = "\033[0;37;44m",
  .tabmsg  = "\033[0;31;40m",
  .cmdmark = "\033[1;36;40m",
  .cmdline = "\033[0;37;40m",
  .screen  = "\033[0;37;40m",
};

const struct menu_attrib *menu_attrib = &default_attrib;

#define WIDTH		80
#define MARGIN		10
#define MENU_ROWS	12
#define TABMSG_ROW	18
#define CMDLINE_ROW	20
#define END_ROW		24

char *pad_line(const char *text, int align, int width)
{
  static char buffer[256];
  int n, p;

  if ( width >= (int) sizeof buffer )
    return NULL;		/* Can't do it */

  n = strlen(text);
  if ( n >= width )
    n = width;

  memset(buffer, ' ', width);
  buffer[width] = 0;
  p = ((width-n)*align)>>1;
  memcpy(buffer+p, text, n);

  return buffer;
}

void draw_menu(int sel, int top)
{
  int x, y;

  printf("\033[1;%dH%s\311", MARGIN+1, menu_attrib->border);
  for ( x = 2 ; x <= WIDTH-2*MARGIN-1 ; x++ )
    putchar('\315');
  putchar('\273');

  printf("\033[2;%dH\272%s %s %s\272",
	 MARGIN+1,
	 menu_attrib->title,
	 pad_line(menu_title, 1, WIDTH-2*MARGIN-4),
	 menu_attrib->border);

  printf("\033[3;%dH\307", MARGIN+1);
  for ( x = 2 ; x <= WIDTH-2*MARGIN-1 ; x++ )
    putchar('\304');
  putchar('\266');

  if ( top != 0 )
    printf("%s\033[3;%dH[-]%s",
	   menu_attrib->more, WIDTH-MARGIN-5,
	   menu_attrib->border);

  for ( y = 4 ; y < 4+MENU_ROWS ; y++ ) {
    int i = (y-4)+top;
    const char *txt = (i >= nentries) ? "" : menu_entries[i].displayname;

    printf("\033[%d;%dH\272%s %s %s\272",
	   y, MARGIN+1,
	   (i == sel) ? menu_attrib->sel : menu_attrib->unsel,
	   pad_line(txt, 0, WIDTH-2*MARGIN-4),
	   menu_attrib->border);
  }

  printf("\033[%d;%dH\310", y, MARGIN+1);
  for ( x = 2 ; x <= WIDTH-2*MARGIN-1 ; x++ )
    putchar('\315');
  putchar('\274');

  if ( top < nentries-MENU_ROWS )
    printf("%s\033[%d;%dH[+]", menu_attrib->more, y, WIDTH-MARGIN-5);

  if ( allowedit )
    printf("%s\033[%d;1H%s", menu_attrib->tabmsg, TABMSG_ROW,
	   pad_line("Press [Tab] to edit options", 1, WIDTH));

  printf("%s\033[%d;1H", menu_attrib->screen, END_ROW);
}

char *edit_cmdline(char *input)
{
  static char cmdline[MAX_CMDLINE_LEN];
  int key, len;
  int redraw = 1;

  strncpy(cmdline, input, MAX_CMDLINE_LEN);
  cmdline[MAX_CMDLINE_LEN-1] = '\0';

  len = strlen(cmdline);

  for (;;) {
    if ( redraw ) {
      printf("\033[%d;1H%s> %s%s",
	     CMDLINE_ROW, menu_attrib->cmdmark,
	     menu_attrib->cmdline, pad_line(cmdline, 0, MAX_CMDLINE_LEN-1));
      printf("%s\033[%d;3H%s",
	     menu_attrib->cmdline, CMDLINE_ROW, cmdline);
      redraw = 0;
    }

    key = get_key(stdin);

    /* FIX: should handle arrow keys and edit-in-middle */

    switch( key ) {
    case KEY_ENTER:
    case KEY_CTRL('J'):
      return cmdline;
    case KEY_ESC:
    case KEY_CTRL('C'):
      return NULL;
    case KEY_BACKSPACE:
    case KEY_DEL:
    case '\x7F':
      if ( len ) {
	cmdline[--len] = '\0';
	redraw = 1;
      }
      break;
    case KEY_CTRL('U'):
      if ( len ) {
	len = 0;
	cmdline[len] = '\0';
	redraw = 1;
      }
      break;
    case KEY_CTRL('W'):
      if ( len ) {
	int wasbs = (cmdline[len-1] <= ' ');
	while ( len && (cmdline[len-1] <= ' ' || !wasbs) ) {
	  len--;
	  wasbs = wasbs || (cmdline[len-1] <= ' ');
	}
	cmdline[len] = '\0';
	redraw = 1;
      }
      break;
    default:
      if ( key >= ' ' && key <= 0xFF && len < MAX_CMDLINE_LEN-1 ) {
	cmdline[len] = key;
	cmdline[++len] = '\0';
	putchar(key);
      }
      break;
    }
  }
}

const char *run_menu(void)
{
  int key;
  int done = 0;
  int entry = defentry;
  int top = 0;
  int clear = 1;
  char *cmdline;

  while ( !done ) {
    if ( entry < 0 )
      entry = 0;
    else if ( entry >= nentries )
      entry = nentries-1;

    if ( top < 0 || top < entry-MENU_ROWS+1 )
      top = max(0, entry-MENU_ROWS+1);
    else if ( top > entry )
      top = entry;

    /* Start with a clear screen */
    if ( clear )
      printf("%s\033[2J", menu_attrib->screen);
    clear = 0;

    draw_menu(entry, top);

    key = get_key(stdin);
    switch ( key ) {
    case KEY_ENTER:
    case KEY_CTRL('J'):
      done = 1;
      break;
    case 'P':
    case 'p':
    case KEY_UP:
      entry--;
      break;
    case 'N':
    case 'n':
    case KEY_DOWN:
      entry++;
      break;
    case KEY_CTRL('P'):
    case KEY_PGUP:
      entry -= MENU_ROWS;
      top   -= MENU_ROWS;
      break;
    case KEY_CTRL('N'):
    case KEY_PGDN:
    case ' ':
      entry += MENU_ROWS;
      top   += MENU_ROWS;
      break;
    case '-':
      entry--;
      top--;
      break;
    case '+':
      entry++;
      top++;
      break;
    case KEY_TAB:
      if ( allowedit ) {
	draw_menu(-1, top);	/* Disable bar */
	cmdline = edit_cmdline(menu_entries[entry].cmdline);
	if ( cmdline )
	  return cmdline;
	else
	  clear = 1;
      }
      break;
    case KEY_CTRL('C'):		/* Ctrl-C */
    case KEY_ESC:		/* Esc */
      if ( allowedit )
	return NULL;
    default:
      break;
    }
  }

  /* Return the label name so localboot and ipappend work */
  return menu_entries[entry].label;
}


void __attribute__((noreturn)) execute(const char *cmdline)
{
#ifdef __COM32__
  static com32sys_t ireg;

  strcpy(__com32.cs_bounce, cmdline);
  ireg.eax.w[0] = 0x0003;	/* Run command */
  ireg.ebx.w[0] = OFFS(__com32.cs_bounce);
  ireg.es = SEG(__com32.cs_bounce);
  __intcall(0x22, &ireg, NULL);
  exit(255);  /* Shouldn't return */
#else
  /* For testing... */
  printf("\n>>> %s\n", cmdline);
  exit(0);
#endif
}

int main(int argc, char *argv[])
{
  const char *cmdline;

  (void)argc;

  console_ansi_raw();
  fputs("\033%@\033(U", stdout); /* Enable CP 437 graphics on a real console */

  parse_config(argv[1]);

  cmdline = run_menu();
  printf("\033[%d;1H\033[0m", END_ROW);
  if ( cmdline )
    execute(cmdline);
  else
    return 0;
}