-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmenu-gemini3-rc.c
More file actions
343 lines (279 loc) · 8.83 KB
/
menu-gemini3-rc.c
File metadata and controls
343 lines (279 loc) · 8.83 KB
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
/* * Refactored Menu Program
* Original Author: Steve Harris
* Refactored for Modularity and Safety
*/
#define _GNU_SOURCE // For getline
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <stdbool.h>
#include <curses.h>
#include <newt.h>
/* --- Constants & Configuration --- */
#define MAX_MENU_ITEMS 30
#define MAX_DESC_LEN 31
#define MAX_CMD_LEN 81
#define SCREEN_WIDTH 80
#define MENU_HEADING_LEN 80
typedef enum {
UI_MODE_CURSES,
UI_MODE_NEWT
} UIMode;
/* --- Data Structures --- */
typedef struct {
char description[MAX_DESC_LEN];
char command[MAX_CMD_LEN];
} MenuItem;
typedef struct {
MenuItem items[MAX_MENU_ITEMS];
int count;
char heading[MENU_HEADING_LEN];
char description[MENU_HEADING_LEN];
char *filename;
UIMode mode;
char *user_login;
char *user_term;
} MenuContext;
/* --- Prototypes --- */
// Core Logic
void init_context(MenuContext *ctx, int argc, char *argv[]);
void load_menu_file(MenuContext *ctx);
void execute_item(const MenuItem *item);
void change_password(const char *user);
void cleanup_and_exit(MenuContext *ctx, int exit_code);
// UI: Curses
void run_curses_ui(MenuContext *ctx);
// UI: Newt
void run_newt_ui(MenuContext *ctx);
/* --- Main Entry Point --- */
int main(int argc, char *argv[]) {
MenuContext ctx;
// 1. Initialize and Parse Arguments
init_context(&ctx, argc, argv);
// 2. Load Data
load_menu_file(&ctx);
// 3. Dispatch UI
if (ctx.mode == UI_MODE_CURSES) {
run_curses_ui(&ctx);
} else {
run_newt_ui(&ctx);
}
cleanup_and_exit(&ctx, 0);
return 0;
}
/* --- Core Logic Implementation --- */
void usage(const char *prog_name) {
fprintf(stderr, "\nUsage: %s <-c | -n> menufile\n", prog_name);
fprintf(stderr, " -c : curses based menu\n");
fprintf(stderr, " -n : newt based menu\n");
exit(EXIT_FAILURE);
}
void init_context(MenuContext *ctx, int argc, char *argv[]) {
if (argc < 3) usage(argv[0]);
// Set Mode
if (strcmp(argv[1], "-c") == 0) ctx->mode = UI_MODE_CURSES;
else if (strcmp(argv[1], "-n") == 0) ctx->mode = UI_MODE_NEWT;
else usage(argv[0]);
// Set Filename
ctx->filename = argv[2];
ctx->count = 0;
// Environment Variables
ctx->user_login = getenv("LOGNAME");
if (!ctx->user_login) ctx->user_login = "UNKNOWN";
ctx->user_term = getenv("TTY");
if (!ctx->user_term) ctx->user_term = "NOTTY";
}
void trim_newline(char *str) {
size_t len = strlen(str);
if (len > 0 && str[len - 1] == '\n') {
str[len - 1] = '\0';
}
}
void load_menu_file(MenuContext *ctx) {
FILE *file = fopen(ctx->filename, "r");
if (!file) {
perror("Cannot open menu file");
exit(EXIT_FAILURE);
}
char *line = NULL;
size_t len = 0;
ssize_t read;
// Read Header
if ((read = getline(&line, &len, file)) != -1) {
trim_newline(line);
snprintf(ctx->heading, MENU_HEADING_LEN, "%s", line);
}
// Read Description
if ((read = getline(&line, &len, file)) != -1) {
trim_newline(line);
snprintf(ctx->description, MENU_HEADING_LEN, "%s", line);
}
// Read Items
while (ctx->count < MAX_MENU_ITEMS) {
// Read Description line
read = getline(&line, &len, file);
if (read == -1) break;
trim_newline(line);
snprintf(ctx->items[ctx->count].description, MAX_DESC_LEN, "%s", line);
// Read Command line
read = getline(&line, &len, file);
if (read == -1) break;
trim_newline(line);
snprintf(ctx->items[ctx->count].command, MAX_CMD_LEN, "%s", line);
ctx->count++;
}
free(line);
fclose(file);
}
void execute_item(const MenuItem *item) {
// Suspend UI handled by caller usually, but good to clear screen
system("clear");
system(item->command);
printf("\nPress Enter to return...");
getchar();
}
void change_password(const char *user) {
char cmd[100];
snprintf(cmd, sizeof(cmd), "passwd %s", user);
system("clear");
system(cmd);
printf("\nPress Enter to return...");
getchar();
}
void cleanup_and_exit(MenuContext *ctx, int exit_code) {
if (ctx->mode == UI_MODE_CURSES) {
endwin();
} else {
newtFinished();
}
exit(exit_code);
}
/* --- Curses UI Implementation --- */
int get_center_x(const char *text) {
return (SCREEN_WIDTH - strlen(text)) / 2;
}
void curses_draw_header(MenuContext *ctx) {
char line_sep[] = " "; // 80 spaces
move(0, 0);
attron(A_UNDERLINE);
addstr(line_sep);
mvaddstr(0, get_center_x(ctx->heading), ctx->heading);
// Env info
mvaddstr(0, 0, ctx->user_login);
mvaddstr(0, SCREEN_WIDTH - strlen(ctx->user_term), ctx->user_term);
// Description
attroff(A_UNDERLINE);
mvaddstr(2, get_center_x(ctx->description), ctx->description);
}
void curses_draw_footer() {
char prompt[] = "Enter Option : ";
char exit_msg[] = "(Q/q to quit)";
char pass_msg[] = "CP to change password";
char line_sep[] = " ";
move(21, 0);
attron(A_UNDERLINE);
addstr(line_sep);
mvaddstr(21, get_center_x(pass_msg), pass_msg);
attroff(A_UNDERLINE);
move(23, 0);
clrtoeol();
mvaddstr(23, 0, prompt);
mvaddstr(23, SCREEN_WIDTH - strlen(exit_msg), exit_msg);
}
void curses_draw_body(MenuContext *ctx) {
int start_y = 5;
if (ctx->count <= 8) {
// Single Column
for (int i = 0; i < ctx->count; i++) {
mvprintw(start_y + (i * 2), 30, "%d. %s", i + 1, ctx->items[i].description);
}
} else {
// Dual Column
int split = ctx->count / 2;
for (int i = 0; i < split; i++) {
mvprintw(start_y + i, 5, "%d. %s", i + 1, ctx->items[i].description);
}
for (int i = split; i < ctx->count; i++) {
mvprintw(start_y + (i - split), 45, "%d. %s", i + 1, ctx->items[i].description);
}
}
}
void run_curses_ui(MenuContext *ctx) {
initscr();
cbreak();
noecho(); // We will handle echo manually via scanw if needed, or usually just getch
char input[10];
int choice;
while (1) {
clear();
curses_draw_header(ctx);
curses_draw_body(ctx);
curses_draw_footer(ctx);
move(23, 15); // Cursor at prompt
echo();
refresh();
getnstr(input, 3);
noecho();
if (strcasecmp(input, "q") == 0) {
break;
} else if (strcasecmp(input, "cp") == 0) {
def_prog_mode(); // Save curses state
endwin();
change_password(ctx->user_login);
reset_prog_mode(); // Restore curses state
} else {
choice = atoi(input);
if (choice > 0 && choice <= ctx->count) {
def_prog_mode();
endwin();
execute_item(&ctx->items[choice - 1]);
reset_prog_mode();
}
}
}
}
/* --- Newt UI Implementation --- */
void run_newt_ui(MenuContext *ctx) {
newtComponent form, listbox, button_quit;
struct newtExitStruct es;
newtInit();
newtCls();
// Using standard color palette handled by library defaults usually
// but explicit palette code can be re-added here if desired.
newtDrawRootText(0, 0, ctx->heading);
newtPushHelpLine("Scroll and select item to execute.");
// Window centered
newtOpenWindow(10, 3, 65, 20, ctx->description);
form = newtForm(NULL, NULL, 0);
listbox = newtListbox(15, 1, 10, NEWT_FLAG_RETURNEXIT | NEWT_FLAG_BORDER | NEWT_FLAG_SCROLL);
// Populate List
for (int i = 0; i < ctx->count; i++) {
// We pass the index + 1 as the key
newtListboxAppendEntry(listbox, ctx->items[i].description, (void *)(long)(i + 1));
}
// Add components
newtFormAddComponents(form, listbox, NULL);
while (1) {
newtFormRun(form, &es);
if (es.reason == NEWT_EXIT_COMPONENT && es.u.co == listbox) {
int selection = (int)(long)newtListboxGetCurrent(listbox);
if (selection > 0 && selection <= ctx->count) {
newtSuspend();
execute_item(&ctx->items[selection - 1]);
newtResume();
// Redraw after return
newtDrawRootText(0, 0, ctx->heading);
newtPushHelpLine("Scroll and select item to execute.");
}
} else {
// Exit on F12 or specific quit keys if configured,
// or we can add a "Quit" button.
// For now, assume F12/Esc exits form logic
break;
}
}
newtFormDestroy(form);
newtPopWindow();
}