paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
gdt | 5e4fa16 | 2004-03-16 14:38:36 +0000 | [diff] [blame] | 32 | #include <lib/version.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | #include "getopt.h" |
| 34 | #include "command.h" |
| 35 | |
| 36 | #include "vtysh/vtysh.h" |
| 37 | #include "vtysh/vtysh_user.h" |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 38 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 39 | /* VTY shell program name. */ |
| 40 | char *progname; |
| 41 | |
hasso | 67e29ab | 2004-08-26 22:21:31 +0000 | [diff] [blame] | 42 | /* Configuration file name and directory. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 43 | char *config_file = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 44 | char config_default[] = SYSCONFDIR VTYSH_DEFAULT_CONFIG; |
| 45 | |
| 46 | /* Integrated configuration file. */ |
| 47 | char *integrate_file = NULL; |
| 48 | char *integrate_current = NULL; |
| 49 | #if 0 |
| 50 | char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG; |
| 51 | #endif |
| 52 | |
| 53 | /* Flag for indicate executing child command. */ |
| 54 | int execute_flag = 0; |
| 55 | |
| 56 | /* For sigsetjmp() & siglongjmp(). */ |
| 57 | static sigjmp_buf jmpbuf; |
| 58 | |
| 59 | /* Flag for avoid recursive siglongjmp() call. */ |
| 60 | static int jmpflag = 0; |
| 61 | |
| 62 | /* A static variable for holding the line. */ |
| 63 | static char *line_read; |
| 64 | |
| 65 | /* Master of threads. */ |
| 66 | struct thread_master *master; |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 67 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 68 | /* SIGTSTP handler. This function care user's ^Z input. */ |
| 69 | void |
| 70 | sigtstp (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. */ |
| 90 | void |
| 91 | sigint (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 | |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame^] | 102 | /* Signale wrapper for vtysh. We don't use sigevent because |
| 103 | * vtysh doesn't use threads. TODO */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 104 | RETSIGTYPE * |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame^] | 105 | vtysh_signal_set (int signo, void (*func)(int)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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. */ |
| 127 | void |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame^] | 128 | vtysh_signal_init () |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 129 | { |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame^] | 130 | vtysh_signal_set (SIGINT, sigint); |
| 131 | vtysh_signal_set (SIGTSTP, sigtstp); |
| 132 | vtysh_signal_set (SIGPIPE, SIG_IGN); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 133 | } |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 134 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 135 | /* Help information display. */ |
| 136 | static void |
| 137 | usage (int status) |
| 138 | { |
| 139 | if (status != 0) |
| 140 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 141 | else |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 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 "\ |
hasso | 67e29ab | 2004-08-26 22:21:31 +0000 | [diff] [blame] | 146 | "-f, --config_file Set configuration file name\n"\ |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 147 | "-h, --help Display this help and exit\n\n" \ |
| 148 | "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 149 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 150 | exit (status); |
| 151 | } |
| 152 | |
| 153 | /* VTY shell options, we use GNU getopt library. */ |
| 154 | struct option longopts[] = |
| 155 | { |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 156 | { "boot", no_argument, NULL, 'b'}, |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 157 | /* For compatibility with older zebra/quagga versions */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 158 | { "eval", required_argument, NULL, 'e'}, |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 159 | { "command", required_argument, NULL, 'c'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 160 | { "help", no_argument, NULL, 'h'}, |
hasso | 67e29ab | 2004-08-26 22:21:31 +0000 | [diff] [blame] | 161 | { "config_file", required_argument, NULL, 'f'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 162 | { 0 } |
| 163 | }; |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 164 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 165 | /* Read a string, and return a pointer to it. Returns NULL on EOF. */ |
| 166 | char * |
| 167 | vtysh_rl_gets () |
| 168 | { |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 169 | HIST_ENTRY *last; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 170 | /* If the buffer has already been allocated, return the memory |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 171 | * to the free pool. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 181 | /* If the line has any text in it, save it on the history. But only if |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 182 | * last command in history isn't the same one. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 183 | if (line_read && *line_read) |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 184 | { |
| 185 | using_history(); |
| 186 | last = previous_history(); |
| 187 | if (!last || strcmp (last->line, line_read) != 0) |
| 188 | add_history (line_read); |
| 189 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 190 | |
| 191 | return (line_read); |
| 192 | } |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 193 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 194 | /* VTY shell main routine. */ |
| 195 | int |
| 196 | main (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 | { |
hasso | 67e29ab | 2004-08-26 22:21:31 +0000 | [diff] [blame] | 211 | opt = getopt_long (argc, argv, "be:c:hf:", longopts, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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': |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 224 | case 'c': |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 225 | eval_flag = 1; |
| 226 | eval_line = optarg; |
| 227 | break; |
| 228 | case 'h': |
| 229 | usage (0); |
| 230 | break; |
hasso | 67e29ab | 2004-08-26 22:21:31 +0000 | [diff] [blame] | 231 | /* XXX It isn't used in any way. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 232 | case 'i': |
| 233 | integrated_file = strdup (optarg); |
hasso | 67e29ab | 2004-08-26 22:21:31 +0000 | [diff] [blame] | 234 | case 'f': |
| 235 | config_file = optarg; |
| 236 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 237 | default: |
| 238 | usage (1); |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /* Initialize user input buffer. */ |
| 244 | line_read = NULL; |
| 245 | |
| 246 | /* Signal and others. */ |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame^] | 247 | vtysh_signal_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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. */ |
hasso | 67e29ab | 2004-08-26 22:21:31 +0000 | [diff] [blame] | 262 | vtysh_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 263 | |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 264 | /* If eval mode. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | { |
hasso | 67e29ab | 2004-08-26 22:21:31 +0000 | [diff] [blame] | 274 | vtysh_read_config (integrate_file, integrate_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | } |