blob: 223f0c6e142732cc97c9d6942658b0c212f1079e [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
hassoe42f5a32004-08-28 17:04:33 +0000102/* Signale wrapper for vtysh. We don't use sigevent because
103 * vtysh doesn't use threads. TODO */
paul718e3742002-12-13 20:15:29 +0000104RETSIGTYPE *
hassoe42f5a32004-08-28 17:04:33 +0000105vtysh_signal_set (int signo, void (*func)(int))
paul718e3742002-12-13 20:15:29 +0000106{
107 int ret;
108 struct sigaction sig;
109 struct sigaction osig;
110
111 sig.sa_handler = func;
112 sigemptyset (&sig.sa_mask);
113 sig.sa_flags = 0;
114#ifdef SA_RESTART
115 sig.sa_flags |= SA_RESTART;
116#endif /* SA_RESTART */
117
118 ret = sigaction (signo, &sig, &osig);
119
120 if (ret < 0)
121 return (SIG_ERR);
122 else
123 return (osig.sa_handler);
124}
125
126/* Initialization of signal handles. */
127void
hassoe42f5a32004-08-28 17:04:33 +0000128vtysh_signal_init ()
paul718e3742002-12-13 20:15:29 +0000129{
hassoe42f5a32004-08-28 17:04:33 +0000130 vtysh_signal_set (SIGINT, sigint);
131 vtysh_signal_set (SIGTSTP, sigtstp);
132 vtysh_signal_set (SIGPIPE, SIG_IGN);
paul718e3742002-12-13 20:15:29 +0000133}
hassob094d262004-08-25 12:22:00 +0000134
paul718e3742002-12-13 20:15:29 +0000135/* Help information display. */
136static void
137usage (int status)
138{
139 if (status != 0)
140 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
141 else
hasso95e735b2004-08-26 13:08:30 +0000142 printf ("Usage : %s [OPTION...]\n\n" \
143 "Integrated shell for Quagga routing software suite. \n\n"\
144 "-b, --boot Execute boot startup configuration\n" \
145 "-c, --command Execute argument as command\n "\
hasso67e29ab2004-08-26 22:21:31 +0000146 "-f, --config_file Set configuration file name\n"\
hasso95e735b2004-08-26 13:08:30 +0000147 "-h, --help Display this help and exit\n\n" \
148 "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
149
paul718e3742002-12-13 20:15:29 +0000150 exit (status);
151}
152
153/* VTY shell options, we use GNU getopt library. */
154struct option longopts[] =
155{
hassob094d262004-08-25 12:22:00 +0000156 { "boot", no_argument, NULL, 'b'},
hasso4991f6c2004-04-06 11:36:17 +0000157 /* For compatibility with older zebra/quagga versions */
paul718e3742002-12-13 20:15:29 +0000158 { "eval", required_argument, NULL, 'e'},
hasso4991f6c2004-04-06 11:36:17 +0000159 { "command", required_argument, NULL, 'c'},
paul718e3742002-12-13 20:15:29 +0000160 { "help", no_argument, NULL, 'h'},
hasso67e29ab2004-08-26 22:21:31 +0000161 { "config_file", required_argument, NULL, 'f'},
paul718e3742002-12-13 20:15:29 +0000162 { 0 }
163};
hassob094d262004-08-25 12:22:00 +0000164
paul718e3742002-12-13 20:15:29 +0000165/* Read a string, and return a pointer to it. Returns NULL on EOF. */
166char *
167vtysh_rl_gets ()
168{
hasso4991f6c2004-04-06 11:36:17 +0000169 HIST_ENTRY *last;
paul718e3742002-12-13 20:15:29 +0000170 /* If the buffer has already been allocated, return the memory
hasso95e735b2004-08-26 13:08:30 +0000171 * to the free pool. */
paul718e3742002-12-13 20:15:29 +0000172 if (line_read)
173 {
174 free (line_read);
175 line_read = NULL;
176 }
177
178 /* Get a line from the user. Change prompt according to node. XXX. */
179 line_read = readline (vtysh_prompt ());
180
hasso4991f6c2004-04-06 11:36:17 +0000181 /* If the line has any text in it, save it on the history. But only if
hasso95e735b2004-08-26 13:08:30 +0000182 * last command in history isn't the same one. */
paul718e3742002-12-13 20:15:29 +0000183 if (line_read && *line_read)
hasso4991f6c2004-04-06 11:36:17 +0000184 {
185 using_history();
186 last = previous_history();
187 if (!last || strcmp (last->line, line_read) != 0)
188 add_history (line_read);
189 }
paul718e3742002-12-13 20:15:29 +0000190
191 return (line_read);
192}
hassob094d262004-08-25 12:22:00 +0000193
paul718e3742002-12-13 20:15:29 +0000194/* VTY shell main routine. */
195int
196main (int argc, char **argv, char **env)
197{
198 char *p;
199 int opt;
200 int eval_flag = 0;
201 int boot_flag = 0;
202 char *eval_line = NULL;
203 char *integrated_file = NULL;
204
205 /* Preserve name of myself. */
206 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
207
208 /* Option handling. */
209 while (1)
210 {
hasso67e29ab2004-08-26 22:21:31 +0000211 opt = getopt_long (argc, argv, "be:c:hf:", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000212
213 if (opt == EOF)
214 break;
215
216 switch (opt)
217 {
218 case 0:
219 break;
220 case 'b':
221 boot_flag = 1;
222 break;
223 case 'e':
hasso4991f6c2004-04-06 11:36:17 +0000224 case 'c':
paul718e3742002-12-13 20:15:29 +0000225 eval_flag = 1;
226 eval_line = optarg;
227 break;
228 case 'h':
229 usage (0);
230 break;
hasso67e29ab2004-08-26 22:21:31 +0000231 /* XXX It isn't used in any way. */
paul718e3742002-12-13 20:15:29 +0000232 case 'i':
233 integrated_file = strdup (optarg);
hasso67e29ab2004-08-26 22:21:31 +0000234 case 'f':
235 config_file = optarg;
236 break;
paul718e3742002-12-13 20:15:29 +0000237 default:
238 usage (1);
239 break;
240 }
241 }
242
243 /* Initialize user input buffer. */
244 line_read = NULL;
245
246 /* Signal and others. */
hassoe42f5a32004-08-28 17:04:33 +0000247 vtysh_signal_init ();
paul718e3742002-12-13 20:15:29 +0000248
249 /* Make vty structure and register commands. */
250 vtysh_init_vty ();
251 vtysh_init_cmd ();
252 vtysh_user_init ();
253 vtysh_config_init ();
254
255 vty_init_vtysh ();
256
257 sort_node ();
258
259 vtysh_connect_all ();
260
261 /* Read vtysh configuration file. */
hasso67e29ab2004-08-26 22:21:31 +0000262 vtysh_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000263
hasso95e735b2004-08-26 13:08:30 +0000264 /* If eval mode. */
paul718e3742002-12-13 20:15:29 +0000265 if (eval_flag)
266 {
267 vtysh_execute_no_pager (eval_line);
268 exit (0);
269 }
270
271 /* Boot startup configuration file. */
272 if (boot_flag)
273 {
hasso67e29ab2004-08-26 22:21:31 +0000274 vtysh_read_config (integrate_file, integrate_default);
paul718e3742002-12-13 20:15:29 +0000275 exit (0);
276 }
277
278 vtysh_pager_init ();
279
280 vtysh_readline_init ();
281
282 vty_hello (vty);
283
284 vtysh_auth ();
285
286 /* Preparation for longjmp() in sigtstp(). */
287 sigsetjmp (jmpbuf, 1);
288 jmpflag = 1;
289
290 /* Main command loop. */
291 while (vtysh_rl_gets ())
292 vtysh_execute (line_read);
293
294 printf ("\n");
295
296 /* Rest in peace. */
297 exit (0);
298}