1 /* Virtual terminal interface shell.
2 * Copyright (C) 2000 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
29 #include <readline/readline.h>
30 #include <readline/history.h>
32 #include <lib/version.h>
36 #include "vtysh/vtysh.h"
37 #include "vtysh/vtysh_user.h"
39 /* VTY shell program name. */
42 /* Configuration file name and directory. */
43 char config_default
[] = SYSCONFDIR VTYSH_DEFAULT_CONFIG
;
45 /* Flag for indicate executing child command. */
48 /* For sigsetjmp() & siglongjmp(). */
49 static sigjmp_buf jmpbuf
;
51 /* Flag for avoid recursive siglongjmp() call. */
52 static int jmpflag
= 0;
54 /* A static variable for holding the line. */
55 static char *line_read
;
57 /* Master of threads. */
58 struct thread_master
*master
;
60 /* SIGTSTP handler. This function care user's ^Z input. */
64 /* Execute "end" command. */
65 vtysh_execute ("end");
67 /* Initialize readline. */
71 /* Check jmpflag for duplicate siglongjmp(). */
77 /* Back to main command loop. */
78 siglongjmp (jmpbuf
, 1);
81 /* SIGINT handler. This function care user's ^Z input. */
85 /* Check this process is not child process. */
90 rl_forced_update_display ();
94 /* Signale wrapper for vtysh. We don't use sigevent because
95 * vtysh doesn't use threads. TODO */
97 vtysh_signal_set (int signo
, void (*func
)(int))
100 struct sigaction sig
;
101 struct sigaction osig
;
103 sig
.sa_handler
= func
;
104 sigemptyset (&sig
.sa_mask
);
107 sig
.sa_flags
|= SA_RESTART
;
108 #endif /* SA_RESTART */
110 ret
= sigaction (signo
, &sig
, &osig
);
115 return (osig
.sa_handler
);
118 /* Initialization of signal handles. */
122 vtysh_signal_set (SIGINT
, sigint
);
123 vtysh_signal_set (SIGTSTP
, sigtstp
);
124 vtysh_signal_set (SIGPIPE
, SIG_IGN
);
127 /* Help information display. */
132 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
134 printf ("Usage : %s [OPTION...]\n\n" \
135 "Integrated shell for Quagga routing software suite. \n\n"\
136 "-b, --boot Execute boot startup configuration\n" \
137 "-c, --command Execute argument as command\n "\
138 "-h, --help Display this help and exit\n\n" \
139 "Report bugs to %s\n", progname
, ZEBRA_BUG_ADDRESS
);
144 /* VTY shell options, we use GNU getopt library. */
145 struct option longopts
[] =
147 { "boot", no_argument
, NULL
, 'b'},
148 /* For compatibility with older zebra/quagga versions */
149 { "eval", required_argument
, NULL
, 'e'},
150 { "command", required_argument
, NULL
, 'c'},
151 { "help", no_argument
, NULL
, 'h'},
155 /* Read a string, and return a pointer to it. Returns NULL on EOF. */
160 /* If the buffer has already been allocated, return the memory
161 * to the free pool. */
168 /* Get a line from the user. Change prompt according to node. XXX. */
169 line_read
= readline (vtysh_prompt ());
171 /* If the line has any text in it, save it on the history. But only if
172 * last command in history isn't the same one. */
173 if (line_read
&& *line_read
)
176 last
= previous_history();
177 if (!last
|| strcmp (last
->line
, line_read
) != 0)
178 add_history (line_read
);
184 /* VTY shell main routine. */
186 main (int argc
, char **argv
, char **env
)
192 char *eval_line
= NULL
;
194 /* Preserve name of myself. */
195 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
197 /* Option handling. */
200 opt
= getopt_long (argc
, argv
, "be:c:h", longopts
, 0);
226 /* Initialize user input buffer. */
229 /* Signal and others. */
230 vtysh_signal_init ();
232 /* Make vty structure and register commands. */
236 vtysh_config_init ();
242 vtysh_connect_all ();
244 /* Read vtysh configuration file. */
245 vtysh_read_config (config_default
);
250 vtysh_execute_no_pager (eval_line
);
254 /* Boot startup configuration file. */
257 if (vtysh_read_config (integrate_default
))
259 fprintf (stderr
, "Can't open configuration file [%s]\n",
269 vtysh_readline_init ();
275 /* Enter into enable node. */
276 vtysh_execute ("enable");
278 /* Preparation for longjmp() in sigtstp(). */
279 sigsetjmp (jmpbuf
, 1);
282 /* Main command loop. */
283 while (vtysh_rl_gets ())
284 vtysh_execute (line_read
);