-
Notifications
You must be signed in to change notification settings - Fork 3
/
menu.c
630 lines (524 loc) · 20.8 KB
/
menu.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/***************************************************************************/
/* This menu program has been created to allow the user to create a text */
/* file that is read in and displayed as a menu. */
/* It uses curses so it should be portable across Unix's and terminals. */
/*-------------------------------------------------------------------------*/
/* Author : Steve Harris ([email protected]) */
/***************************************************************************/
/* Version 1.1 - Steve Harris (13/11/96) */
/* 1. Changed getch to scanw and added atoi to cope with string and number */
/* input. */
/* 2. Tried using switch/case for menu selection but failed. */
/* (Suspect the problem is due the variety of option for exiting the menu) */
/* 3. Added cp - change password and OFF/off/Q/q options. */
/***************************************************************************/
/* Version 1.2 - Steve Harris (14/11/96) */
/* 1. Added function to draw_bottom_line to clear the menu option after the*/
/* refresh - uses clrtoeol(). */
/* 2. Changed the quit option to use die() instead of exit(0). */
/***************************************************************************/
/* Version 1.3 - Steve Harris (14/11/96) */
/* 1. Convert the menu body drawing to a function. */
/* 2. Call it from thew construct menu function. */
/* 3. Change code to draw menu in different places and styles (spacing) */
/* depending on the number of menu options. */
/***************************************************************************/
/* Version 1.4 - Steve Harris (14/11/96) */
/* 1. Changed the menu style to do side by side columns to balance the menu*/
/* when using more than 8 menu options. */
/***************************************************************************/
/* Version 1.5 - Steve Harris (14/11/96) */
/* 1. Added comments. */
/* 2. Moved heading termination to file read function because it carried on*/
/* truncating itself when <cr> was pressed on its own. */
/***************************************************************************/
/* Version 1.6 - Steve Harris (14/11/96) */
/* 1. Added the prompt to change the password. */
/* 2. Hard coded the kerberos password option. */
/***************************************************************************/
/* Version 1.7 - Steve Harris (15/11/96) */
/* 1. Changed the if/else tests for menu choices to be tidier. */
/***************************************************************************/
/* Version 1.8 - Steve Harris (28/2/97) */
/* 1. Introduced isendwin,wrefresh,doupdate because of intermittent screen */
/* redraw problems. */
/***************************************************************************/
/* Version 1.9 - Steve Harris (02/6/2020) */
/* 1. Added a few includes to get this up and running on Ubuntu 16.4 */
/***************************************************************************/
/* Version 1.10 - Steve Harris (06/6/2020) */
/* 1. Move the check to see if the filename is valid to start of program */
/* to deal with the missing argument before initiating curses which */
/* messes the screen layout up. */
/* 2. Error handling in to capture empty TTY */
/***************************************************************************/
/* Version 1.11 - Steve Harris (07/6/2020) */
/* 1. Tidy up compiler warnings - seem to be C90 formatting - int's, */
/* function declaration and incorrect types */
/* 2. Remove the old 'OFF' exit commands - just Q/q now. */
/* 3. Replace the kerbpass with passwd. */
/* 4. Fix a bug where redrawing the menu after execution of command left */
/* the last menu option selected displayed on the screen - there was a */
/* printw after the scanw that was not needed as echo() had been set. */
/***************************************************************************/
/* Version 1.12 - Steve Harris (10/26/2020): newt branch */
/* Added in usage, option processing and CODE_PATH with newt code. */
/***************************************************************************/
/* Version 1.13 - Steve Harris (10/31/2020): FileFormatHandler branch */
/* Added in better processing of menufile to handle overlength lines. */
/***************************************************************************/
/* Version 1.14 - Steve Harris (11/1/2020): NewtColour branch */
/* Added in newt color pallette - didn;t bother adding option to select. */
/* There aren't a huge range to choose from. */
/***************************************************************************/
#include <curses.h>
#include <signal.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "newt.h"
/************************************/
/*Setup structures for the menu file*/
/*and other parameters. */
/************************************/
#define MAX_NO_ITEMS 30
struct menu_item {
char description[31];
char command[81];
};
struct menu_item menu[MAX_NO_ITEMS];
/* CURSES - Declare functions */
int read_menu_file();
int construct_menu();
int draw_menu_body();
int draw_dual_body();
int get_option();
int find_center();
int draw_bottom_line();
void die();
void usage();
/* CURSES - Setup variables to use in code*/
int center_x;
int count, total_no_of_items;
char * ch; /*Used for EOF*/
WINDOW * subone; /*Window pointer for menu*/
char from_term[3]; /*Characters read in from terminal*/
int choice; /*Used to hold number of menu choice*/
int item_step; /*Gap plus one between menu options*/
/*CURSES - Used in constructing menu - messy isn't it*/
char line[81] = " ";
char prompt_line[] = "Enter Option : ";
char exit_line[] = "(Q/q to quit)";
char password_prompt[] = "CP to change password";
char NOTTY[] = "NOTTY";
/* NEWT - Setup */
struct callbackInfo {
newtComponent en;
char * state;
};
void disableCallback(newtComponent co, void * data) {
struct callbackInfo * cbi = data;
if ( * cbi -> state == ' ') {
newtEntrySetFlags(cbi -> en, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET);
} else {
newtEntrySetFlags(cbi -> en, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);
}
newtRefresh();
}
void suspend(void * d) {
newtSuspend();
raise(SIGTSTP);
newtResume();
}
void helpCallback(newtComponent co, void * tag) {
newtWinMessage("Help", "Ok", tag);
}
/* Newt Color pallette*/
const struct newtColors newtDefaultColorPalette = {
"white", "blue", /* root fg, bg */
"black", "lightgray", /* border fg, bg */
"black", "lightgray", /* window fg, bg */
"white", "black", /* shadow fg, bg */
"red", "lightgray", /* title fg, bg */
"lightgray", "red", /* button fg, bg */
"red", "lightgray", /* active button fg, bg */
"lightgray", "blue", /* checkbox fg, bg */
"lightgray", "red", /* active checkbox fg, bg */
"lightgray", "blue", /* entry box fg, bg */
"blue", "lightgray", /* label fg, bg */
"black", "lightgray", /* listbox fg, bg */
"lightgray", "blue", /* active listbox fg, bg */
"black", "lightgray", /* textbox fg, bg */
"lightgray", "red", /* active textbox fg, bg */
"white", "blue", /* help line */
"lightgray", "blue", /* root text */
"blue", /* scale full */
"red", /* scale empty */
"blue", "lightgray", /* disabled entry fg, bg */
"black", "lightgray", /* compact button fg, bg */
"lightgray", "red", /* active & sel listbox */
"black", "brown" /* selected listbox */
};
/*BOTH - Menu file related settings*/
FILE * menu_file;
#define MENU_HEADING_LENGTH 80
char menu_heading[MENU_HEADING_LENGTH];
#define MENU_DESC_LENGTH 80
char menu_description[MENU_DESC_LENGTH];
char * file_name;
/* BOTH - Variables */
char * login_name, * login_term; /* Store settings for menu heading */
char CODE_PATH[7]; /*CURSES or NEWT */
/***********************************************/
/*This is main() - where the real work is done */
/***********************************************/
int main(int argc, char * argv[]) {
/* Process the arguments and determine the code path */
if (argc < 3) {
usage(argv[0]);
};
if (strcmp(argv[1], "-c") == 0) {
strcpy(CODE_PATH, "CURSES");
} else if (strcmp(argv[1], "-n") == 0) {
strcpy(CODE_PATH, "NEWT");
} else {
usage(argv[0]);
}
/*Record menu file name*/
file_name = argv[2];
/* Open file and read in data - send error if not there.*/
/* Based on https://man7.org/linux/man-pages/man3/getline.3.html */
char *line = NULL;
size_t len = 0;
ssize_t nread;
char desc_length = 30;
char cmd_length = 80;
menu_file = fopen(file_name, "r");
if (menu_file == NULL) {
printf("Cannot open your menu file.\n");
usage(argv[0]);
endwin();
exit(8);
};
/*Read in first two heading lines from file and then read*/
/*the rest into our array of structures - doesn't rely on code path*/
/*Truncate them as they don't seem to truncate properly */
/* Menu Heading */
nread=getline(&line, &len, menu_file);
if (nread-1 > MENU_HEADING_LENGTH) {
printf("\nMenu Heading is too long (needs to be less than %d characters it is %ld characters long.)\n", MENU_HEADING_LENGTH, nread-1);
printf(" - Description: %s\n", line);
usage(argv[0]);
} else {
/* Store the description away - replace the \n character brought in by getline*/
strcpy(menu_heading, line);
menu_heading[nread-1] = '\0';
}
/* Menu Description */
nread=getline(&line, &len, menu_file);
if (nread-1 > MENU_DESC_LENGTH) {
printf("\nMenu Description is too long (needs to be less than %d characters it is %ld characters long.)\n", MENU_DESC_LENGTH, nread-1);
printf(" - Description: %s\n", line);
usage(argv[0]);
} else {
/* Store the description away - replace the \n character brought in by getline*/
strcpy(menu_description, line);
menu_description[nread-1] = '\0';
}
/* Process the rest of the file as options and commands */
/* Process the menu_file */
count=0;
while (count < MAX_NO_ITEMS) {
/* Read a description - max 30 characters*/
nread = getline(&line, &len, menu_file);
if (nread == -1) {
/*Reached end of file*/
break;
} else {
/* Check it's length and if it's o.k. store it away - allow for string termination character*/
if (nread-1 > desc_length) {
printf("\n\nDescription is too long (needs to be less than %d characters it is %ld characters long.)\n", desc_length, nread-1);
printf(" - Description: %s\n", line);
usage(argv[0]);
} else {
/* Store the description away - replace the \n character brought in by getline*/
strcpy(menu[count].description,line);
menu[count].description[nread-1] = '\0';
}
}
/* Read a command - max 80 characters*/
nread = getline(&line, &len, menu_file);
if (nread == -1) {
/*Reached end of file */
break;
} else {
/* Check it's length and if it's o.k. store it away - allow for string termination character*/
if (nread-1 > cmd_length) {
printf("\n\nCommand is too long (needs to be less than %d chracaters it is %ld chracters long.)\n", cmd_length, nread-1);
printf(" - Command: %s\n", line);
usage(argv[0]);
} else {
/* Store the command away - replace the \n character brought in by getline */
strcpy(menu[count].command,line);
menu[count].command[nread-1] = '\0';
}
}
/* Increment menu count */
count++;
}
/* Reached MAX_NO_ITEMS or EOF */
total_no_of_items = count;
free(line);
fclose(menu_file);
/* Setup parts independent of code path */
signal(SIGINT, die);
/*Setup the variables depending on the */
/*environment settings. */
login_name = getenv("LOGNAME");
login_term = getenv("TTY");
if (login_term == NULL) {
login_term = NOTTY;
}
/* ********** CODE PATH Decision *******/
/* Follow the CURSES or NEWT code path */
/***************************************/
if (strcmp(CODE_PATH, "CURSES") == 0) {
/* CURSES Code Path */
/*Setup curses*/
initscr();
/*Draw menu*/
while (1) {
/*Setup menu outline*/
construct_menu(file_name);
if (total_no_of_items <= 8) {
/*Draw single column, double spaced*/
draw_menu_body(5, 30, 2, 1, 8);
} else {
/*Draw two column,single spaced*/
draw_dual_body(5, 7);
}
/*Position cursor in prompt and draw*/
move(23, 15);
refresh();
/*Get the menu option*/
get_option();
/*Clear the from_term and choice*/
choice = 0;
from_term[0] = '\0';
}
} else {
/* We're opn the newt code path */
/* Acknowledgment: based on source from: https://pagure.io/newt/blob/master/f/test.c */
/* Setup variables */
newtComponent lb, b1, f, t;
struct callbackInfo cbis[3];
struct newtExitStruct es;
/* Initialize the screen */
newtInit();
newtCls();
/* Using the colour pallette define earlier - set the colours and refresh*/
newtSetColors(newtDefaultColorPalette);
newtRefresh();
/* Setup call backs */
newtSetSuspendCallback(suspend, NULL);
newtSetHelpCallback(helpCallback);
/* Setup the headings and footer*/
newtDrawRootText(0, 0, menu_heading);
newtPushHelpLine("Scroll up or down and select the menu item to execute.");
newtOpenWindow(10, 3, 65, 23, menu_description);
/* Setup the form */
f = newtForm(NULL, "Scroll to the menu item, or type first letter of menu item, and press enter to execute.", 0);
/* Add the components to the form */
newtFormAddComponents(f, NULL);
/* Setup the list box */
lb = newtListbox(15, 1, 20, NEWT_FLAG_RETURNEXIT | NEWT_FLAG_BORDER |
NEWT_FLAG_SCROLL | NEWT_FLAG_SHOWCURSOR);
int i; /* Count for the loop */
/* Setup the list menu based on what's in the structure array */
for (i = 0; i < total_no_of_items; i++) {
newtListboxAppendEntry(lb, menu[i].description, (void * )(long) i + 1);
}
/* Add quit option and record number of the Quit menu item */
newtListboxAppendEntry(lb, "Quit", (void * )(long) i + 1);
int quitMenuItem = i + 1;
/* Add listbox to form and refresh screen */
newtFormAddComponents(f, lb, NULL);
newtRefresh();
newtFormSetTimer(f, 200);
/* Run the menu until an option is selected */
while (1) {
do {
newtFormRun(f, & es);
newtRefresh();
} while (es.reason != NEWT_EXIT_COMPONENT);
/* Record the item that was selected from the listbox */
int numhighlighted = (int)(long) newtListboxGetCurrent(lb);
/* Check if we're quiting */
if (numhighlighted == quitMenuItem) {
/* Tidy up and exit */
newtPopWindow();
newtFinished();
newtFormDestroy(f);
exit(0);
}
/* We're not quitting so run the command go back to the menu */
/* Had trouble with screen output of shell commands - suspend and resume */
newtSuspend();
system(menu[numhighlighted - 1].command);
newtResume();
}
}
}
/*****************************************/
/*Following function constructs the menu.*/
/*****************************************/
int construct_menu(char menu_file_name[]) {
/*Creates the main menu window*/
/*Create the top line first and the overwrite with other componentns */
move(0, 0);
attron(A_UNDERLINE);
addstr(line);
/* Add Menu heading next as it's a long string */
find_center(menu_heading);
move(0, center_x);
addstr(menu_heading);
/* Add other heading pieces */
move(0, 0);
addstr(login_name);
move(0, 80 - strlen(login_term));
addstr(login_term);
find_center(menu_description);
move(2, center_x);
addstr(menu_description);
attroff(A_UNDERLINE);
/*Put in prompt line.*/
move(21, 0);
attron(A_UNDERLINE);
addstr(line);
find_center(password_prompt);
move(21, center_x);
addstr(password_prompt);
attroff(A_UNDERLINE);
/*Draw the bottom line*/
draw_bottom_line();
}
/***********************************/
/*Function to draw a dual menu body*/
/***********************************/
int draw_dual_body(int start_y_dual, int start_x_dual) {
int right_hand, left_hand, temp_count, right_y;
/*Work out left and right hand sides*/
right_hand = total_no_of_items / 2;
left_hand = total_no_of_items - right_hand;
/*Put in menu options*/
right_y = start_y_dual;
move(start_y_dual, start_x_dual);
for (temp_count = 1; temp_count <= left_hand; temp_count++) {
/*Left hand side*/
printw("%d.", temp_count);
move(start_y_dual, start_x_dual + 4);
addstr(menu[temp_count - 1].description);
start_y_dual++;
move(start_y_dual, start_x_dual);
}
move(right_y, start_x_dual + 44);
for (temp_count = left_hand + 1; temp_count <= total_no_of_items; temp_count++) {
/*Right hand side*/
move(right_y, start_x_dual + 44);
printw("%d. ", temp_count);
move(right_y, start_x_dual + 48);
addstr(menu[temp_count - 1].description);
right_y++;
move(right_y, start_x_dual + 48);
}
}
/***************************************************/
/*Function to draw the menu body in a single column*/
/***************************************************/
int draw_menu_body(int start_y, int start_x, int item_gap, int start_option, int end_option) {
/*Draw rest of menu*/
/*Put in menu options*/
move(start_y, start_x);
while ((start_option <= end_option) && (start_option <= total_no_of_items)) {
printw("%d.", start_option);
move(start_y, start_x + 4);
addstr(menu[start_option - 1].description);
start_y += item_gap;
move(start_y, start_x);
start_option++;
}
}
/************************************************************************/
/*Draw the bottom line - in a function because its used in more than one*/
/*place. It doesn't do a refresh the calling function should do it. */
/************************************************************************/
int draw_bottom_line() {
move(23, 0);
clrtoeol();
move(23, 0);
addstr(prompt_line);
move(23, 80 - strlen(exit_line));
addstr(exit_line);
}
/************************************************************************/
/*function to get response from the user and load the appropriate option*/
/************************************************************************/
int get_option() {
/*Setup and get response from user*/
raw();
echo();
nl();
scanw("%3s", from_term);
choice = atoi(from_term);
/*Process menu choice*/
if ((strcmp(from_term, "q") == 0) ||
(strcmp(from_term, "Q") == 0)) {
die();
} else {
if ((strcmp(from_term, "cp") == 0) ||
(strcmp(from_term, "CP") == 0)) {
endwin();
system("clear;passwd $LOGNAME;clear");
doupdate();
} else {
if (choice != 0) {
endwin();
system(menu[choice - 1].command);
doupdate();
}
}
}
draw_bottom_line();
}
/*********************************/
/*Function to service interrupt */
/*or to simply terminate. */
/*********************************/
void die() {
if (strcmp(CODE_PATH, "CURSES") == 0) {
endwin();
exit(0);
} else {
/* Must be on the NEWT code path */
/* Tidy up the windows and execute the command from the array */
newtPopWindow();
newtFinished();
exit(0);
}
}
/***************************************/
/*Find the center of whats passed to it*/
/***************************************/
int find_center(char center_of_what[]) {
center_x = (80 - (strlen(center_of_what) - 1)) / 2;
}
/* Usage */
void usage(char command[]) {
printf("\nUsage: %s <-c | -n> menufile\n", command);
printf(" -c : curses base menu\n");
printf(" -n : newt based menu\n");
printf(" menufile : Input for menu, options and commands\n");
printf("\n See https://github.com/steveh250/Unix-Menu-Program for formatting.\n\n");
exit(-1);
}