]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Virtual terminal interface shell. |
2 | * Copyright (C) 2000 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with GNU Zebra; see the file COPYING. If not, write to the Free | |
18 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
19 | * 02111-1307, USA. | |
20 | */ | |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include <sys/un.h> | |
25 | #include <setjmp.h> | |
26 | #include <sys/wait.h> | |
27 | #include <pwd.h> | |
28 | ||
29 | #include <readline/readline.h> | |
30 | #include <readline/history.h> | |
31 | ||
5e4fa164 | 32 | #include <lib/version.h> |
718e3744 | 33 | #include "getopt.h" |
34 | #include "command.h" | |
35 | ||
36 | #include "vtysh/vtysh.h" | |
37 | #include "vtysh/vtysh_user.h" | |
b094d260 | 38 | |
718e3744 | 39 | /* VTY shell program name. */ |
40 | char *progname; | |
41 | ||
67e29abc | 42 | /* Configuration file name and directory. */ |
718e3744 | 43 | char *config_file = NULL; |
718e3744 | 44 | char config_default[] = SYSCONFDIR VTYSH_DEFAULT_CONFIG; |
45 | ||
46 | /* Integrated configuration file. */ | |
47 | char *integrate_file = NULL; | |
48 | char *integrate_current = NULL; | |
49 | #if 0 | |
50 | char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG; | |
51 | #endif | |
52 | ||
53 | /* Flag for indicate executing child command. */ | |
54 | int execute_flag = 0; | |
55 | ||
56 | /* For sigsetjmp() & siglongjmp(). */ | |
57 | static sigjmp_buf jmpbuf; | |
58 | ||
59 | /* Flag for avoid recursive siglongjmp() call. */ | |
60 | static int jmpflag = 0; | |
61 | ||
62 | /* A static variable for holding the line. */ | |
63 | static char *line_read; | |
64 | ||
65 | /* Master of threads. */ | |
66 | struct thread_master *master; | |
b094d260 | 67 | |
718e3744 | 68 | /* SIGTSTP handler. This function care user's ^Z input. */ |
69 | void | |
70 | sigtstp (int sig) | |
71 | { | |
72 | /* Execute "end" command. */ | |
73 | vtysh_execute ("end"); | |
74 | ||
75 | /* Initialize readline. */ | |
76 | rl_initialize (); | |
77 | printf ("\n"); | |
78 | ||
79 | /* Check jmpflag for duplicate siglongjmp(). */ | |
80 | if (! jmpflag) | |
81 | return; | |
82 | ||
83 | jmpflag = 0; | |
84 | ||
85 | /* Back to main command loop. */ | |
86 | siglongjmp (jmpbuf, 1); | |
87 | } | |
88 | ||
89 | /* SIGINT handler. This function care user's ^Z input. */ | |
90 | void | |
91 | sigint (int sig) | |
92 | { | |
93 | /* Check this process is not child process. */ | |
94 | if (! execute_flag) | |
95 | { | |
96 | rl_initialize (); | |
97 | printf ("\n"); | |
98 | rl_forced_update_display (); | |
99 | } | |
100 | } | |
101 | ||
102 | /* Signale wrapper. */ | |
103 | RETSIGTYPE * | |
104 | signal_set (int signo, void (*func)(int)) | |
105 | { | |
106 | int ret; | |
107 | struct sigaction sig; | |
108 | struct sigaction osig; | |
109 | ||
110 | sig.sa_handler = func; | |
111 | sigemptyset (&sig.sa_mask); | |
112 | sig.sa_flags = 0; | |
113 | #ifdef SA_RESTART | |
114 | sig.sa_flags |= SA_RESTART; | |
115 | #endif /* SA_RESTART */ | |
116 | ||
117 | ret = sigaction (signo, &sig, &osig); | |
118 | ||
119 | if (ret < 0) | |
120 | return (SIG_ERR); | |
121 | else | |
122 | return (osig.sa_handler); | |
123 | } | |
124 | ||
125 | /* Initialization of signal handles. */ | |
126 | void | |
127 | signal_init () | |
128 | { | |
129 | signal_set (SIGINT, sigint); | |
130 | signal_set (SIGTSTP, sigtstp); | |
131 | signal_set (SIGPIPE, SIG_IGN); | |
132 | } | |
b094d260 | 133 | |
718e3744 | 134 | /* Help information display. */ |
135 | static void | |
136 | usage (int status) | |
137 | { | |
138 | if (status != 0) | |
139 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); | |
140 | else | |
95e735b5 | 141 | printf ("Usage : %s [OPTION...]\n\n" \ |
142 | "Integrated shell for Quagga routing software suite. \n\n"\ | |
143 | "-b, --boot Execute boot startup configuration\n" \ | |
144 | "-c, --command Execute argument as command\n "\ | |
67e29abc | 145 | "-f, --config_file Set configuration file name\n"\ |
95e735b5 | 146 | "-h, --help Display this help and exit\n\n" \ |
147 | "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); | |
148 | ||
718e3744 | 149 | exit (status); |
150 | } | |
151 | ||
152 | /* VTY shell options, we use GNU getopt library. */ | |
153 | struct option longopts[] = | |
154 | { | |
b094d260 | 155 | { "boot", no_argument, NULL, 'b'}, |
4991f6ca | 156 | /* For compatibility with older zebra/quagga versions */ |
718e3744 | 157 | { "eval", required_argument, NULL, 'e'}, |
4991f6ca | 158 | { "command", required_argument, NULL, 'c'}, |
718e3744 | 159 | { "help", no_argument, NULL, 'h'}, |
67e29abc | 160 | { "config_file", required_argument, NULL, 'f'}, |
718e3744 | 161 | { 0 } |
162 | }; | |
b094d260 | 163 | |
718e3744 | 164 | /* Read a string, and return a pointer to it. Returns NULL on EOF. */ |
165 | char * | |
166 | vtysh_rl_gets () | |
167 | { | |
4991f6ca | 168 | HIST_ENTRY *last; |
718e3744 | 169 | /* If the buffer has already been allocated, return the memory |
95e735b5 | 170 | * to the free pool. */ |
718e3744 | 171 | if (line_read) |
172 | { | |
173 | free (line_read); | |
174 | line_read = NULL; | |
175 | } | |
176 | ||
177 | /* Get a line from the user. Change prompt according to node. XXX. */ | |
178 | line_read = readline (vtysh_prompt ()); | |
179 | ||
4991f6ca | 180 | /* If the line has any text in it, save it on the history. But only if |
95e735b5 | 181 | * last command in history isn't the same one. */ |
718e3744 | 182 | if (line_read && *line_read) |
4991f6ca | 183 | { |
184 | using_history(); | |
185 | last = previous_history(); | |
186 | if (!last || strcmp (last->line, line_read) != 0) | |
187 | add_history (line_read); | |
188 | } | |
718e3744 | 189 | |
190 | return (line_read); | |
191 | } | |
b094d260 | 192 | |
718e3744 | 193 | /* VTY shell main routine. */ |
194 | int | |
195 | main (int argc, char **argv, char **env) | |
196 | { | |
197 | char *p; | |
198 | int opt; | |
199 | int eval_flag = 0; | |
200 | int boot_flag = 0; | |
201 | char *eval_line = NULL; | |
202 | char *integrated_file = NULL; | |
203 | ||
204 | /* Preserve name of myself. */ | |
205 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); | |
206 | ||
207 | /* Option handling. */ | |
208 | while (1) | |
209 | { | |
67e29abc | 210 | opt = getopt_long (argc, argv, "be:c:hf:", longopts, 0); |
718e3744 | 211 | |
212 | if (opt == EOF) | |
213 | break; | |
214 | ||
215 | switch (opt) | |
216 | { | |
217 | case 0: | |
218 | break; | |
219 | case 'b': | |
220 | boot_flag = 1; | |
221 | break; | |
222 | case 'e': | |
4991f6ca | 223 | case 'c': |
718e3744 | 224 | eval_flag = 1; |
225 | eval_line = optarg; | |
226 | break; | |
227 | case 'h': | |
228 | usage (0); | |
229 | break; | |
67e29abc | 230 | /* XXX It isn't used in any way. */ |
718e3744 | 231 | case 'i': |
232 | integrated_file = strdup (optarg); | |
67e29abc | 233 | case 'f': |
234 | config_file = optarg; | |
235 | break; | |
718e3744 | 236 | default: |
237 | usage (1); | |
238 | break; | |
239 | } | |
240 | } | |
241 | ||
242 | /* Initialize user input buffer. */ | |
243 | line_read = NULL; | |
244 | ||
245 | /* Signal and others. */ | |
246 | signal_init (); | |
247 | ||
248 | /* Make vty structure and register commands. */ | |
249 | vtysh_init_vty (); | |
250 | vtysh_init_cmd (); | |
251 | vtysh_user_init (); | |
252 | vtysh_config_init (); | |
253 | ||
254 | vty_init_vtysh (); | |
255 | ||
256 | sort_node (); | |
257 | ||
258 | vtysh_connect_all (); | |
259 | ||
260 | /* Read vtysh configuration file. */ | |
67e29abc | 261 | vtysh_read_config (config_file, config_default); |
718e3744 | 262 | |
95e735b5 | 263 | /* If eval mode. */ |
718e3744 | 264 | if (eval_flag) |
265 | { | |
266 | vtysh_execute_no_pager (eval_line); | |
267 | exit (0); | |
268 | } | |
269 | ||
270 | /* Boot startup configuration file. */ | |
271 | if (boot_flag) | |
272 | { | |
67e29abc | 273 | vtysh_read_config (integrate_file, integrate_default); |
718e3744 | 274 | exit (0); |
275 | } | |
276 | ||
277 | vtysh_pager_init (); | |
278 | ||
279 | vtysh_readline_init (); | |
280 | ||
281 | vty_hello (vty); | |
282 | ||
283 | vtysh_auth (); | |
284 | ||
285 | /* Preparation for longjmp() in sigtstp(). */ | |
286 | sigsetjmp (jmpbuf, 1); | |
287 | jmpflag = 1; | |
288 | ||
289 | /* Main command loop. */ | |
290 | while (vtysh_rl_gets ()) | |
291 | vtysh_execute (line_read); | |
292 | ||
293 | printf ("\n"); | |
294 | ||
295 | /* Rest in peace. */ | |
296 | exit (0); | |
297 | } |