blob: 134bcf73ca4df451b5a6abd53f67270dd199302a [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"
Andrew J. Schorrf366ad32006-07-27 18:01:41 +000035#include "memory.h"
paul718e3742002-12-13 20:15:29 +000036
37#include "vtysh/vtysh.h"
38#include "vtysh/vtysh_user.h"
hassob094d262004-08-25 12:22:00 +000039
paul718e3742002-12-13 20:15:29 +000040/* VTY shell program name. */
41char *progname;
42
hasso67e29ab2004-08-26 22:21:31 +000043/* Configuration file name and directory. */
paul718e3742002-12-13 20:15:29 +000044char config_default[] = SYSCONFDIR VTYSH_DEFAULT_CONFIG;
45
paul718e3742002-12-13 20:15:29 +000046/* Flag for indicate executing child command. */
47int execute_flag = 0;
48
49/* For sigsetjmp() & siglongjmp(). */
50static sigjmp_buf jmpbuf;
51
52/* Flag for avoid recursive siglongjmp() call. */
53static int jmpflag = 0;
54
55/* A static variable for holding the line. */
56static char *line_read;
57
58/* Master of threads. */
59struct thread_master *master;
hassob094d262004-08-25 12:22:00 +000060
paul718e3742002-12-13 20:15:29 +000061/* SIGTSTP handler. This function care user's ^Z input. */
62void
63sigtstp (int sig)
64{
65 /* Execute "end" command. */
66 vtysh_execute ("end");
67
68 /* Initialize readline. */
69 rl_initialize ();
70 printf ("\n");
71
72 /* Check jmpflag for duplicate siglongjmp(). */
73 if (! jmpflag)
74 return;
75
76 jmpflag = 0;
77
78 /* Back to main command loop. */
79 siglongjmp (jmpbuf, 1);
80}
81
82/* SIGINT handler. This function care user's ^Z input. */
83void
84sigint (int sig)
85{
86 /* Check this process is not child process. */
87 if (! execute_flag)
88 {
89 rl_initialize ();
90 printf ("\n");
91 rl_forced_update_display ();
92 }
93}
94
hassoe42f5a32004-08-28 17:04:33 +000095/* Signale wrapper for vtysh. We don't use sigevent because
96 * vtysh doesn't use threads. TODO */
paul718e3742002-12-13 20:15:29 +000097RETSIGTYPE *
hassoe42f5a32004-08-28 17:04:33 +000098vtysh_signal_set (int signo, void (*func)(int))
paul718e3742002-12-13 20:15:29 +000099{
100 int ret;
101 struct sigaction sig;
102 struct sigaction osig;
103
104 sig.sa_handler = func;
105 sigemptyset (&sig.sa_mask);
106 sig.sa_flags = 0;
107#ifdef SA_RESTART
108 sig.sa_flags |= SA_RESTART;
109#endif /* SA_RESTART */
110
111 ret = sigaction (signo, &sig, &osig);
112
113 if (ret < 0)
114 return (SIG_ERR);
115 else
116 return (osig.sa_handler);
117}
118
119/* Initialization of signal handles. */
120void
hassoe42f5a32004-08-28 17:04:33 +0000121vtysh_signal_init ()
paul718e3742002-12-13 20:15:29 +0000122{
hassoe42f5a32004-08-28 17:04:33 +0000123 vtysh_signal_set (SIGINT, sigint);
124 vtysh_signal_set (SIGTSTP, sigtstp);
125 vtysh_signal_set (SIGPIPE, SIG_IGN);
paul718e3742002-12-13 20:15:29 +0000126}
hassob094d262004-08-25 12:22:00 +0000127
paul718e3742002-12-13 20:15:29 +0000128/* Help information display. */
129static void
130usage (int status)
131{
132 if (status != 0)
133 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
134 else
hasso95e735b2004-08-26 13:08:30 +0000135 printf ("Usage : %s [OPTION...]\n\n" \
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000136 "Integrated shell for Quagga routing software suite. \n\n" \
hasso95e735b2004-08-26 13:08:30 +0000137 "-b, --boot Execute boot startup configuration\n" \
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000138 "-c, --command Execute argument as command\n" \
139 "-d, --daemon Connect only to the specified daemon\n" \
140 "-E, --echo Echo prompt and command in -c mode\n" \
hasso95e735b2004-08-26 13:08:30 +0000141 "-h, --help Display this help and exit\n\n" \
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000142 "Note that multiple commands may be executed from the command\n" \
143 "line by passing multiple -c args, or by embedding linefeed\n" \
144 "characters in one or more of the commands.\n\n" \
hasso95e735b2004-08-26 13:08:30 +0000145 "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
146
paul718e3742002-12-13 20:15:29 +0000147 exit (status);
148}
149
150/* VTY shell options, we use GNU getopt library. */
151struct option longopts[] =
152{
hassob094d262004-08-25 12:22:00 +0000153 { "boot", no_argument, NULL, 'b'},
hasso4991f6c2004-04-06 11:36:17 +0000154 /* For compatibility with older zebra/quagga versions */
paul718e3742002-12-13 20:15:29 +0000155 { "eval", required_argument, NULL, 'e'},
hasso4991f6c2004-04-06 11:36:17 +0000156 { "command", required_argument, NULL, 'c'},
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000157 { "daemon", required_argument, NULL, 'd'},
158 { "echo", no_argument, NULL, 'E'},
paul718e3742002-12-13 20:15:29 +0000159 { "help", no_argument, NULL, 'h'},
160 { 0 }
161};
hassob094d262004-08-25 12:22:00 +0000162
paul718e3742002-12-13 20:15:29 +0000163/* Read a string, and return a pointer to it. Returns NULL on EOF. */
164char *
165vtysh_rl_gets ()
166{
hasso4991f6c2004-04-06 11:36:17 +0000167 HIST_ENTRY *last;
paul718e3742002-12-13 20:15:29 +0000168 /* If the buffer has already been allocated, return the memory
hasso95e735b2004-08-26 13:08:30 +0000169 * to the free pool. */
paul718e3742002-12-13 20:15:29 +0000170 if (line_read)
171 {
172 free (line_read);
173 line_read = NULL;
174 }
175
176 /* Get a line from the user. Change prompt according to node. XXX. */
177 line_read = readline (vtysh_prompt ());
178
hasso4991f6c2004-04-06 11:36:17 +0000179 /* If the line has any text in it, save it on the history. But only if
hasso95e735b2004-08-26 13:08:30 +0000180 * last command in history isn't the same one. */
paul718e3742002-12-13 20:15:29 +0000181 if (line_read && *line_read)
hasso4991f6c2004-04-06 11:36:17 +0000182 {
183 using_history();
184 last = previous_history();
185 if (!last || strcmp (last->line, line_read) != 0)
186 add_history (line_read);
187 }
paul718e3742002-12-13 20:15:29 +0000188
189 return (line_read);
190}
hassob094d262004-08-25 12:22:00 +0000191
paul718e3742002-12-13 20:15:29 +0000192/* VTY shell main routine. */
193int
194main (int argc, char **argv, char **env)
195{
196 char *p;
197 int opt;
paul718e3742002-12-13 20:15:29 +0000198 int boot_flag = 0;
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000199 const char *daemon_name = NULL;
200 struct cmd_rec {
201 const char *line;
202 struct cmd_rec *next;
203 } *cmd = NULL;
204 struct cmd_rec *tail = NULL;
205 int echo_command = 0;
paul718e3742002-12-13 20:15:29 +0000206
207 /* Preserve name of myself. */
208 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
209
210 /* Option handling. */
211 while (1)
212 {
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000213 opt = getopt_long (argc, argv, "be:c:d:Eh", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000214
215 if (opt == EOF)
216 break;
217
218 switch (opt)
219 {
220 case 0:
221 break;
222 case 'b':
223 boot_flag = 1;
224 break;
225 case 'e':
hasso4991f6c2004-04-06 11:36:17 +0000226 case 'c':
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000227 {
228 struct cmd_rec *cr;
229 cr = XMALLOC(0, sizeof(*cr));
230 cr->line = optarg;
231 cr->next = NULL;
232 if (tail)
233 tail->next = cr;
234 else
235 cmd = cr;
236 tail = cr;
237 }
238 break;
239 case 'd':
240 daemon_name = optarg;
241 break;
242 case 'E':
243 echo_command = 1;
paul718e3742002-12-13 20:15:29 +0000244 break;
245 case 'h':
246 usage (0);
247 break;
paul718e3742002-12-13 20:15:29 +0000248 default:
249 usage (1);
250 break;
251 }
252 }
253
254 /* Initialize user input buffer. */
255 line_read = NULL;
256
257 /* Signal and others. */
hassoe42f5a32004-08-28 17:04:33 +0000258 vtysh_signal_init ();
paul718e3742002-12-13 20:15:29 +0000259
260 /* Make vty structure and register commands. */
261 vtysh_init_vty ();
262 vtysh_init_cmd ();
263 vtysh_user_init ();
264 vtysh_config_init ();
265
266 vty_init_vtysh ();
267
268 sort_node ();
269
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000270 /* Read vtysh configuration file before connecting to daemons. */
hassoe7168df2004-10-03 20:11:32 +0000271 vtysh_read_config (config_default);
paul718e3742002-12-13 20:15:29 +0000272
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000273 /* Make sure we pass authentication before proceeding. */
274 vtysh_auth ();
275
276 /* Do not connect until we have passed authentication. */
277 if (vtysh_connect_all (daemon_name) <= 0)
paul718e3742002-12-13 20:15:29 +0000278 {
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000279 fprintf(stderr, "Exiting: failed to connect to any daemons.\n");
280 exit(1);
281 }
282
283 /* If eval mode. */
284 if (cmd)
285 {
286 /* Enter into enable node. */
287 vtysh_execute ("enable");
288
289 while (cmd != NULL)
290 {
291 char *eol;
292
293 while ((eol = strchr(cmd->line, '\n')) != NULL)
294 {
295 *eol = '\0';
296 if (echo_command)
297 printf("%s%s\n", vtysh_prompt(), cmd->line);
298 vtysh_execute_no_pager(cmd->line);
299 cmd->line = eol+1;
300 }
301 if (echo_command)
302 printf("%s%s\n", vtysh_prompt(), cmd->line);
303 vtysh_execute_no_pager (cmd->line);
304
305 {
306 struct cmd_rec *cr;
307 cr = cmd;
308 cmd = cmd->next;
309 XFREE(0, cr);
310 }
311 }
paul718e3742002-12-13 20:15:29 +0000312 exit (0);
313 }
314
315 /* Boot startup configuration file. */
316 if (boot_flag)
317 {
hassoe7168df2004-10-03 20:11:32 +0000318 if (vtysh_read_config (integrate_default))
319 {
320 fprintf (stderr, "Can't open configuration file [%s]\n",
321 integrate_default);
322 exit (1);
323 }
324 else
325 exit (0);
paul718e3742002-12-13 20:15:29 +0000326 }
327
328 vtysh_pager_init ();
329
330 vtysh_readline_init ();
331
332 vty_hello (vty);
333
hassoe7168df2004-10-03 20:11:32 +0000334 /* Enter into enable node. */
335 vtysh_execute ("enable");
336
paul718e3742002-12-13 20:15:29 +0000337 /* Preparation for longjmp() in sigtstp(). */
338 sigsetjmp (jmpbuf, 1);
339 jmpflag = 1;
340
341 /* Main command loop. */
342 while (vtysh_rl_gets ())
343 vtysh_execute (line_read);
344
345 printf ("\n");
346
347 /* Rest in peace. */
348 exit (0);
349}