blob: e862efd638623c36adb56609b8d28395645ca132 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* 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
gdt5e4fa162004-03-16 14:38:36 +000032#include <lib/version.h>
paul718e3742002-12-13 20:15:29 +000033#include "getopt.h"
34#include "command.h"
35
36#include "vtysh/vtysh.h"
37#include "vtysh/vtysh_user.h"
hassob094d262004-08-25 12:22:00 +000038
paul718e3742002-12-13 20:15:29 +000039/* VTY shell program name. */
40char *progname;
41
hasso67e29ab2004-08-26 22:21:31 +000042/* Configuration file name and directory. */
paul718e3742002-12-13 20:15:29 +000043char *config_file = NULL;
paul718e3742002-12-13 20:15:29 +000044char config_default[] = SYSCONFDIR VTYSH_DEFAULT_CONFIG;
45
46/* Integrated configuration file. */
47char *integrate_file = NULL;
48char *integrate_current = NULL;
49#if 0
50char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
51#endif
52
53/* Flag for indicate executing child command. */
54int execute_flag = 0;
55
56/* For sigsetjmp() & siglongjmp(). */
57static sigjmp_buf jmpbuf;
58
59/* Flag for avoid recursive siglongjmp() call. */
60static int jmpflag = 0;
61
62/* A static variable for holding the line. */
63static char *line_read;
64
65/* Master of threads. */
66struct thread_master *master;
hassob094d262004-08-25 12:22:00 +000067
paul718e3742002-12-13 20:15:29 +000068/* SIGTSTP handler. This function care user's ^Z input. */
69void
70sigtstp (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. */
90void
91sigint (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. */
103RETSIGTYPE *
104signal_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. */
126void
127signal_init ()
128{
129 signal_set (SIGINT, sigint);
130 signal_set (SIGTSTP, sigtstp);
131 signal_set (SIGPIPE, SIG_IGN);
132}
hassob094d262004-08-25 12:22:00 +0000133
paul718e3742002-12-13 20:15:29 +0000134/* Help information display. */
135static void
136usage (int status)
137{
138 if (status != 0)
139 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
140 else
hasso95e735b2004-08-26 13:08:30 +0000141 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 "\
hasso67e29ab2004-08-26 22:21:31 +0000145 "-f, --config_file Set configuration file name\n"\
hasso95e735b2004-08-26 13:08:30 +0000146 "-h, --help Display this help and exit\n\n" \
147 "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
148
paul718e3742002-12-13 20:15:29 +0000149 exit (status);
150}
151
152/* VTY shell options, we use GNU getopt library. */
153struct option longopts[] =
154{
hassob094d262004-08-25 12:22:00 +0000155 { "boot", no_argument, NULL, 'b'},
hasso4991f6c2004-04-06 11:36:17 +0000156 /* For compatibility with older zebra/quagga versions */
paul718e3742002-12-13 20:15:29 +0000157 { "eval", required_argument, NULL, 'e'},
hasso4991f6c2004-04-06 11:36:17 +0000158 { "command", required_argument, NULL, 'c'},
paul718e3742002-12-13 20:15:29 +0000159 { "help", no_argument, NULL, 'h'},
hasso67e29ab2004-08-26 22:21:31 +0000160 { "config_file", required_argument, NULL, 'f'},
paul718e3742002-12-13 20:15:29 +0000161 { 0 }
162};
hassob094d262004-08-25 12:22:00 +0000163
paul718e3742002-12-13 20:15:29 +0000164/* Read a string, and return a pointer to it. Returns NULL on EOF. */
165char *
166vtysh_rl_gets ()
167{
hasso4991f6c2004-04-06 11:36:17 +0000168 HIST_ENTRY *last;
paul718e3742002-12-13 20:15:29 +0000169 /* If the buffer has already been allocated, return the memory
hasso95e735b2004-08-26 13:08:30 +0000170 * to the free pool. */
paul718e3742002-12-13 20:15:29 +0000171 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
hasso4991f6c2004-04-06 11:36:17 +0000180 /* If the line has any text in it, save it on the history. But only if
hasso95e735b2004-08-26 13:08:30 +0000181 * last command in history isn't the same one. */
paul718e3742002-12-13 20:15:29 +0000182 if (line_read && *line_read)
hasso4991f6c2004-04-06 11:36:17 +0000183 {
184 using_history();
185 last = previous_history();
186 if (!last || strcmp (last->line, line_read) != 0)
187 add_history (line_read);
188 }
paul718e3742002-12-13 20:15:29 +0000189
190 return (line_read);
191}
hassob094d262004-08-25 12:22:00 +0000192
paul718e3742002-12-13 20:15:29 +0000193/* VTY shell main routine. */
194int
195main (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 {
hasso67e29ab2004-08-26 22:21:31 +0000210 opt = getopt_long (argc, argv, "be:c:hf:", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000211
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':
hasso4991f6c2004-04-06 11:36:17 +0000223 case 'c':
paul718e3742002-12-13 20:15:29 +0000224 eval_flag = 1;
225 eval_line = optarg;
226 break;
227 case 'h':
228 usage (0);
229 break;
hasso67e29ab2004-08-26 22:21:31 +0000230 /* XXX It isn't used in any way. */
paul718e3742002-12-13 20:15:29 +0000231 case 'i':
232 integrated_file = strdup (optarg);
hasso67e29ab2004-08-26 22:21:31 +0000233 case 'f':
234 config_file = optarg;
235 break;
paul718e3742002-12-13 20:15:29 +0000236 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. */
hasso67e29ab2004-08-26 22:21:31 +0000261 vtysh_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000262
hasso95e735b2004-08-26 13:08:30 +0000263 /* If eval mode. */
paul718e3742002-12-13 20:15:29 +0000264 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 {
hasso67e29ab2004-08-26 22:21:31 +0000273 vtysh_read_config (integrate_file, integrate_default);
paul718e3742002-12-13 20:15:29 +0000274 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}