forked from albertw/flash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
278 lines (226 loc) · 6.14 KB
/
main.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
/*
This is a program to display menus on a text tty, good for limited access
guest accounts, or just for user menus.
Copyright (C) 1996 Stephen Fegan
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; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program; see the file COPYING. If not write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
please send patches or advice to: `[email protected]'
*/
/* Include configuration data */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pwd.h>
#include <syslog.h>
#include <signal.h>
#include <time.h>
#include "parse.h"
#include "string.h"
#include "screen.h"
#include "config.h"
#include "event.h"
#include "exec.h"
#include "set.h"
#include "rc.h"
#include "debug.h"
#include "misc.h"
extern char delim;
extern char escape;
extern char comment;
void handle_children (void);
void die_by_signal(int s);
char *progname;
char *menufile;
int login_shell=0;
time_t start_time;
struct menu_items **GlobalHotKeys=NULL;
struct menu *main_menu;
/* need to fix: -v\t\t\tDisplay run-time variables, menus, environment\n\ */
static void
usage ()
{
fprintf (stderr,
"Usage: %s [-hV] [-b <directory>] [-c <command>] [menu file]\n\n\
-b <directory>\tSet base directory to <directory>\n\
-c <command>\t\tA normal shell would run <command>, we dump it to syslog\n\
-d <filename>\tSet debug file to <filename>\n\
-h\t\t\tDisplay help and exit\n\
-l\t\t\tLog all execs to syslog\n\
-V\t\t\tDisplay version and compile-time options then exit\n\
[menu file]\t\tMenu file to load from\n\n\
With no options [menu file] is set to `%s'\n",
progname, DEFAULT_MENU);
}
static void
version ()
{
fprintf (stderr,
"flash version %s, Copyright (C) 1996 Stephen Fegan <[email protected]>\n\
flash comes with ABSOLUTELY NO WARRANTY. This is free software,\n\
you are welcome to redistribute it and modify it under the\n\
conditions of the GNU General Public License.\n\n\
Compile-time options:\n\
Default menu: `%s', Base directory: `%s%s'\n",
VERSION,DEFAULT_MENU,PREFIX,LIBPREFIX);
}
int
main (int argc, char *argv[])
{
struct passwd *ppwd, pwd;
struct sigaction s_act;
char c;
char *filename = DEFAULT_MENU;
int ShowVariables=0;
pid_t shell_pgid;
sigset_t smask;
int debug_file = 0;
progname = argv[0];
login_shell=(*progname == '-');
time(&start_time);
ppwd = getpwuid (getuid ());
memcpy (&pwd, ppwd, sizeof (struct passwd));
endpwent();
#if 0
while(tcgetpgrp(0) != (shell_pgid = getpgrp()))
kill(-shell_pgid, SIGTTIN);
#endif
/* Signal Stuff */
signal (SIGINT, SIG_IGN);
signal (SIGTSTP, SIG_IGN);
signal (SIGQUIT, SIG_IGN);
signal (SIGTTIN, SIG_IGN);
signal (SIGTTOU, SIG_IGN);
s_act.sa_handler = (void (*)(int))handle_children;
sigemptyset (&s_act.sa_mask);
s_act.sa_flags = 0;
sigaction (SIGCHLD, &s_act, (struct sigaction *) NULL);
setpgid(0,0);
shell_pgid=getpgrp();
s_act.sa_handler = (void (*)(int))handle_snarfed_tty;
sigemptyset (&s_act.sa_mask);
s_act.sa_flags = 0;
if((sigaction (SIGTTIN, &s_act, (struct sigaction *) NULL)==-1)||
(sigaction (SIGTTOU, &s_act, (struct sigaction *) NULL)==-1))
{
perror("Could not set signal handler");
exit(EXIT_FAILURE);
}
sigemptyset(&smask);
sigprocmask (SIG_SETMASK, &smask, NULL);
/* Read command line options */
/* Use our own messages */
opterr = 0;
while ((c = getopt (argc, argv, "lhVvb:d:c:")) != -1)
switch (c)
{
case 'b':
if ((optarg==NULL)||(strlen(optarg)==0))
{
usage ();
exit (1);
}
setbasedirectory(optarg);
break;
case 'c':
if ((optarg==NULL)||(strlen(optarg)==0))
{
usage ();
exit (1);
}
syslog (LOG_LOCAL1 | LOG_WARNING, "%d: flash: USE OF OPTION -c: %s",
getpid (), optarg);
break;
case 'd':
if((optarg==NULL)||(strlen(optarg)==0))
{
usage();
exit(1);
}
set_debug_file(optarg);
debug_file = 1;
break;
case 'h':
usage ();
exit (0);
case 'l':
set_variable("logging",NULL,0);
break;
case 'V':
version ();
exit (0);
case 'v':
ShowVariables=1;
break;
case '?':
if (isprint (optopt))
fprintf (stderr, "Unknown option: `-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option character: `\\x%x'.\n", optopt);
usage ();
exit (1);
default:
abort ();
}
if (optind < argc)
filename = argv[optind];
global_variables();
init_schedule();
init_jobq();
signal (SIGHUP, die_by_signal);
signal (SIGTERM, die_by_signal);
init_scr ();
pause_scr();
if (parsefile (filename) == -1)
{
fprintf (stderr, "%s: error while parsing\n", progname);
exit (EXIT_FAILURE);
}
balance_menu_tree();
main_menu = find_mainmenu();
if(main_menu==NULL)
{
fprintf (stderr, "%s: No Menus !!\n", progname);
freemem (menufile);
exit (1);
}
openlog (pwd.pw_name, 0, LOG_USER);
/* Setup global HotKeys */
setupglobalhotkeys();
/*
Looking over peoples' shoulders
*/
if (find_variable("logging",NULL) == 1)
syslog (LOG_LOCAL1 | LOG_INFO, "%d: flash started %s",
getpid (), filename);
if(ShowVariables)showconfig(filename);
init_scr ();
do_menu (main_menu);
close_scr ();
signal (SIGHUP, SIG_IGN);
signal (SIGTERM, SIG_IGN);
finish_jobq();
finish_schedule();
if (find_variable("logging",NULL) == 1)
syslog (LOG_LOCAL1 | LOG_INFO, "%d: flash finished",
getpid ());
if (debug_file == 1) { close_debug_file(); }
/* we outtie */
exit (0);
}