blob: 047e21e9c807d5643d9d6694fc017b789e68b450 [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"
38
39/* VTY shell program name. */
40char *progname;
41
42/* Configuration file name. Usually this is configurable, but vtysh
43 has static configuration file only. */
44char *config_file = NULL;
45
46/* Configuration file and directory. */
47char *config_current = NULL;
48char config_default[] = SYSCONFDIR VTYSH_DEFAULT_CONFIG;
49
50/* Integrated configuration file. */
51char *integrate_file = NULL;
52char *integrate_current = NULL;
53#if 0
54char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
55#endif
56
57/* Flag for indicate executing child command. */
58int execute_flag = 0;
59
60/* For sigsetjmp() & siglongjmp(). */
61static sigjmp_buf jmpbuf;
62
63/* Flag for avoid recursive siglongjmp() call. */
64static int jmpflag = 0;
65
66/* A static variable for holding the line. */
67static char *line_read;
68
69/* Master of threads. */
70struct thread_master *master;
71
72/* SIGTSTP handler. This function care user's ^Z input. */
73void
74sigtstp (int sig)
75{
76 /* Execute "end" command. */
77 vtysh_execute ("end");
78
79 /* Initialize readline. */
80 rl_initialize ();
81 printf ("\n");
82
83 /* Check jmpflag for duplicate siglongjmp(). */
84 if (! jmpflag)
85 return;
86
87 jmpflag = 0;
88
89 /* Back to main command loop. */
90 siglongjmp (jmpbuf, 1);
91}
92
93/* SIGINT handler. This function care user's ^Z input. */
94void
95sigint (int sig)
96{
97 /* Check this process is not child process. */
98 if (! execute_flag)
99 {
100 rl_initialize ();
101 printf ("\n");
102 rl_forced_update_display ();
103 }
104}
105
106/* Signale wrapper. */
107RETSIGTYPE *
108signal_set (int signo, void (*func)(int))
109{
110 int ret;
111 struct sigaction sig;
112 struct sigaction osig;
113
114 sig.sa_handler = func;
115 sigemptyset (&sig.sa_mask);
116 sig.sa_flags = 0;
117#ifdef SA_RESTART
118 sig.sa_flags |= SA_RESTART;
119#endif /* SA_RESTART */
120
121 ret = sigaction (signo, &sig, &osig);
122
123 if (ret < 0)
124 return (SIG_ERR);
125 else
126 return (osig.sa_handler);
127}
128
129/* Initialization of signal handles. */
130void
131signal_init ()
132{
133 signal_set (SIGINT, sigint);
134 signal_set (SIGTSTP, sigtstp);
135 signal_set (SIGPIPE, SIG_IGN);
136}
137
138/* Help information display. */
139static void
140usage (int status)
141{
142 if (status != 0)
143 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
144 else
145 {
146 printf ("Usage : %s [OPTION...]\n\n\
hasso4991f6c2004-04-06 11:36:17 +0000147Integrated shell for Quagga routing software suite. \n\n\
paul718e3742002-12-13 20:15:29 +0000148-b, --boot Execute boot startup configuration\n\
hasso4991f6c2004-04-06 11:36:17 +0000149-c, --command Execute argument as command\n\
paul718e3742002-12-13 20:15:29 +0000150-h, --help Display this help and exit\n\
151\n\
152Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
153 }
154 exit (status);
155}
156
157/* VTY shell options, we use GNU getopt library. */
158struct option longopts[] =
159{
160 { "boot", no_argument, NULL, 'b'},
hasso4991f6c2004-04-06 11:36:17 +0000161 /* For compatibility with older zebra/quagga versions */
paul718e3742002-12-13 20:15:29 +0000162 { "eval", required_argument, NULL, 'e'},
hasso4991f6c2004-04-06 11:36:17 +0000163 { "command", required_argument, NULL, 'c'},
paul718e3742002-12-13 20:15:29 +0000164 { "help", no_argument, NULL, 'h'},
165 { 0 }
166};
167
168/* Read a string, and return a pointer to it. Returns NULL on EOF. */
169char *
170vtysh_rl_gets ()
171{
hasso4991f6c2004-04-06 11:36:17 +0000172 HIST_ENTRY *last;
paul718e3742002-12-13 20:15:29 +0000173 /* If the buffer has already been allocated, return the memory
174 to the free pool. */
175 if (line_read)
176 {
177 free (line_read);
178 line_read = NULL;
179 }
180
181 /* Get a line from the user. Change prompt according to node. XXX. */
182 line_read = readline (vtysh_prompt ());
183
hasso4991f6c2004-04-06 11:36:17 +0000184 /* If the line has any text in it, save it on the history. But only if
185 * last command in history isn't the same one.
186 */
paul718e3742002-12-13 20:15:29 +0000187 if (line_read && *line_read)
hasso4991f6c2004-04-06 11:36:17 +0000188 {
189 using_history();
190 last = previous_history();
191 if (!last || strcmp (last->line, line_read) != 0)
192 add_history (line_read);
193 }
paul718e3742002-12-13 20:15:29 +0000194
195 return (line_read);
196}
197
198/* VTY shell main routine. */
199int
200main (int argc, char **argv, char **env)
201{
202 char *p;
203 int opt;
204 int eval_flag = 0;
205 int boot_flag = 0;
206 char *eval_line = NULL;
207 char *integrated_file = NULL;
208
209 /* Preserve name of myself. */
210 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
211
212 /* Option handling. */
213 while (1)
214 {
hasso4991f6c2004-04-06 11:36:17 +0000215 opt = getopt_long (argc, argv, "be:c:h", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000216
217 if (opt == EOF)
218 break;
219
220 switch (opt)
221 {
222 case 0:
223 break;
224 case 'b':
225 boot_flag = 1;
226 break;
227 case 'e':
hasso4991f6c2004-04-06 11:36:17 +0000228 case 'c':
paul718e3742002-12-13 20:15:29 +0000229 eval_flag = 1;
230 eval_line = optarg;
231 break;
232 case 'h':
233 usage (0);
234 break;
235 case 'i':
236 integrated_file = strdup (optarg);
237 default:
238 usage (1);
239 break;
240 }
241 }
242
243 /* Initialize user input buffer. */
244 line_read = NULL;
245
246 /* Signal and others. */
247 signal_init ();
248
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. */
262 vtysh_read_config (config_file, config_current, config_default);
263
264 /* If eval mode */
265 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 {
274 vtysh_read_config (integrate_file, integrate_current, integrate_default);
275 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}