]> git.proxmox.com Git - mirror_frr.git/blame - vtysh/vtysh_main.c
New way to handle secondary addresses from Gilad Arnold.
[mirror_frr.git] / vtysh / vtysh_main.c
CommitLineData
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. */
40char *progname;
41
67e29abc 42/* Configuration file name and directory. */
718e3744 43char *config_file = NULL;
718e3744 44char 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;
b094d260 67
718e3744 68/* 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
e42f5a37 102/* Signale wrapper for vtysh. We don't use sigevent because
103 * vtysh doesn't use threads. TODO */
718e3744 104RETSIGTYPE *
e42f5a37 105vtysh_signal_set (int signo, void (*func)(int))
718e3744 106{
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
e42f5a37 128vtysh_signal_init ()
718e3744 129{
e42f5a37 130 vtysh_signal_set (SIGINT, sigint);
131 vtysh_signal_set (SIGTSTP, sigtstp);
132 vtysh_signal_set (SIGPIPE, SIG_IGN);
718e3744 133}
b094d260 134
718e3744 135/* 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
95e735b5 142 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 "\
67e29abc 146 "-f, --config_file Set configuration file name\n"\
95e735b5 147 "-h, --help Display this help and exit\n\n" \
148 "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
149
718e3744 150 exit (status);
151}
152
153/* VTY shell options, we use GNU getopt library. */
154struct option longopts[] =
155{
b094d260 156 { "boot", no_argument, NULL, 'b'},
4991f6ca 157 /* For compatibility with older zebra/quagga versions */
718e3744 158 { "eval", required_argument, NULL, 'e'},
4991f6ca 159 { "command", required_argument, NULL, 'c'},
718e3744 160 { "help", no_argument, NULL, 'h'},
67e29abc 161 { "config_file", required_argument, NULL, 'f'},
718e3744 162 { 0 }
163};
b094d260 164
718e3744 165/* Read a string, and return a pointer to it. Returns NULL on EOF. */
166char *
167vtysh_rl_gets ()
168{
4991f6ca 169 HIST_ENTRY *last;
718e3744 170 /* If the buffer has already been allocated, return the memory
95e735b5 171 * to the free pool. */
718e3744 172 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
4991f6ca 181 /* If the line has any text in it, save it on the history. But only if
95e735b5 182 * last command in history isn't the same one. */
718e3744 183 if (line_read && *line_read)
4991f6ca 184 {
185 using_history();
186 last = previous_history();
187 if (!last || strcmp (last->line, line_read) != 0)
188 add_history (line_read);
189 }
718e3744 190
191 return (line_read);
192}
b094d260 193
718e3744 194/* 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 {
67e29abc 211 opt = getopt_long (argc, argv, "be:c:hf:", longopts, 0);
718e3744 212
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':
4991f6ca 224 case 'c':
718e3744 225 eval_flag = 1;
226 eval_line = optarg;
227 break;
228 case 'h':
229 usage (0);
230 break;
67e29abc 231 /* XXX It isn't used in any way. */
718e3744 232 case 'i':
233 integrated_file = strdup (optarg);
67e29abc 234 case 'f':
235 config_file = optarg;
236 break;
718e3744 237 default:
238 usage (1);
239 break;
240 }
241 }
242
243 /* Initialize user input buffer. */
244 line_read = NULL;
245
246 /* Signal and others. */
e42f5a37 247 vtysh_signal_init ();
718e3744 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. */
67e29abc 262 vtysh_read_config (config_file, config_default);
718e3744 263
95e735b5 264 /* If eval mode. */
718e3744 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 {
67e29abc 274 vtysh_read_config (integrate_file, integrate_default);
718e3744 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}