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_default[] = SYSCONFDIR VTYSH_DEFAULT_CONFIG; |
| 44 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 45 | /* Flag for indicate executing child command. */ |
| 46 | int execute_flag = 0; |
| 47 | |
| 48 | /* For sigsetjmp() & siglongjmp(). */ |
| 49 | static sigjmp_buf jmpbuf; |
| 50 | |
| 51 | /* Flag for avoid recursive siglongjmp() call. */ |
| 52 | static int jmpflag = 0; |
| 53 | |
| 54 | /* A static variable for holding the line. */ |
| 55 | static char *line_read; |
| 56 | |
| 57 | /* Master of threads. */ |
| 58 | struct thread_master *master; |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 59 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 60 | /* SIGTSTP handler. This function care user's ^Z input. */ |
| 61 | void |
| 62 | sigtstp (int sig) |
| 63 | { |
| 64 | /* Execute "end" command. */ |
| 65 | vtysh_execute ("end"); |
| 66 | |
| 67 | /* Initialize readline. */ |
| 68 | rl_initialize (); |
| 69 | printf ("\n"); |
| 70 | |
| 71 | /* Check jmpflag for duplicate siglongjmp(). */ |
| 72 | if (! jmpflag) |
| 73 | return; |
| 74 | |
| 75 | jmpflag = 0; |
| 76 | |
| 77 | /* Back to main command loop. */ |
| 78 | siglongjmp (jmpbuf, 1); |
| 79 | } |
| 80 | |
| 81 | /* SIGINT handler. This function care user's ^Z input. */ |
| 82 | void |
| 83 | sigint (int sig) |
| 84 | { |
| 85 | /* Check this process is not child process. */ |
| 86 | if (! execute_flag) |
| 87 | { |
| 88 | rl_initialize (); |
| 89 | printf ("\n"); |
| 90 | rl_forced_update_display (); |
| 91 | } |
| 92 | } |
| 93 | |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 94 | /* Signale wrapper for vtysh. We don't use sigevent because |
| 95 | * vtysh doesn't use threads. TODO */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 96 | RETSIGTYPE * |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 97 | vtysh_signal_set (int signo, void (*func)(int)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 98 | { |
| 99 | int ret; |
| 100 | struct sigaction sig; |
| 101 | struct sigaction osig; |
| 102 | |
| 103 | sig.sa_handler = func; |
| 104 | sigemptyset (&sig.sa_mask); |
| 105 | sig.sa_flags = 0; |
| 106 | #ifdef SA_RESTART |
| 107 | sig.sa_flags |= SA_RESTART; |
| 108 | #endif /* SA_RESTART */ |
| 109 | |
| 110 | ret = sigaction (signo, &sig, &osig); |
| 111 | |
| 112 | if (ret < 0) |
| 113 | return (SIG_ERR); |
| 114 | else |
| 115 | return (osig.sa_handler); |
| 116 | } |
| 117 | |
| 118 | /* Initialization of signal handles. */ |
| 119 | void |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 120 | vtysh_signal_init () |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 121 | { |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 122 | vtysh_signal_set (SIGINT, sigint); |
| 123 | vtysh_signal_set (SIGTSTP, sigtstp); |
| 124 | vtysh_signal_set (SIGPIPE, SIG_IGN); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 125 | } |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 126 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 127 | /* Help information display. */ |
| 128 | static void |
| 129 | usage (int status) |
| 130 | { |
| 131 | if (status != 0) |
| 132 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 133 | else |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 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); |
| 140 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 141 | exit (status); |
| 142 | } |
| 143 | |
| 144 | /* VTY shell options, we use GNU getopt library. */ |
| 145 | struct option longopts[] = |
| 146 | { |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 147 | { "boot", no_argument, NULL, 'b'}, |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 148 | /* For compatibility with older zebra/quagga versions */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 149 | { "eval", required_argument, NULL, 'e'}, |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 150 | { "command", required_argument, NULL, 'c'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 151 | { "help", no_argument, NULL, 'h'}, |
| 152 | { 0 } |
| 153 | }; |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 154 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 155 | /* Read a string, and return a pointer to it. Returns NULL on EOF. */ |
| 156 | char * |
| 157 | vtysh_rl_gets () |
| 158 | { |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 159 | HIST_ENTRY *last; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 160 | /* If the buffer has already been allocated, return the memory |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 161 | * to the free pool. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 162 | if (line_read) |
| 163 | { |
| 164 | free (line_read); |
| 165 | line_read = NULL; |
| 166 | } |
| 167 | |
| 168 | /* Get a line from the user. Change prompt according to node. XXX. */ |
| 169 | line_read = readline (vtysh_prompt ()); |
| 170 | |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 171 | /* 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] | 172 | * last command in history isn't the same one. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 173 | if (line_read && *line_read) |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 174 | { |
| 175 | using_history(); |
| 176 | last = previous_history(); |
| 177 | if (!last || strcmp (last->line, line_read) != 0) |
| 178 | add_history (line_read); |
| 179 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 180 | |
| 181 | return (line_read); |
| 182 | } |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 183 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 184 | /* VTY shell main routine. */ |
| 185 | int |
| 186 | main (int argc, char **argv, char **env) |
| 187 | { |
| 188 | char *p; |
| 189 | int opt; |
| 190 | int eval_flag = 0; |
| 191 | int boot_flag = 0; |
| 192 | char *eval_line = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 193 | |
| 194 | /* Preserve name of myself. */ |
| 195 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 196 | |
| 197 | /* Option handling. */ |
| 198 | while (1) |
| 199 | { |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 200 | opt = getopt_long (argc, argv, "be:c:h", longopts, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 201 | |
| 202 | if (opt == EOF) |
| 203 | break; |
| 204 | |
| 205 | switch (opt) |
| 206 | { |
| 207 | case 0: |
| 208 | break; |
| 209 | case 'b': |
| 210 | boot_flag = 1; |
| 211 | break; |
| 212 | case 'e': |
hasso | 4991f6c | 2004-04-06 11:36:17 +0000 | [diff] [blame] | 213 | case 'c': |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 214 | eval_flag = 1; |
| 215 | eval_line = optarg; |
| 216 | break; |
| 217 | case 'h': |
| 218 | usage (0); |
| 219 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 220 | default: |
| 221 | usage (1); |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* Initialize user input buffer. */ |
| 227 | line_read = NULL; |
| 228 | |
| 229 | /* Signal and others. */ |
hasso | e42f5a3 | 2004-08-28 17:04:33 +0000 | [diff] [blame] | 230 | vtysh_signal_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 231 | |
| 232 | /* Make vty structure and register commands. */ |
| 233 | vtysh_init_vty (); |
| 234 | vtysh_init_cmd (); |
| 235 | vtysh_user_init (); |
| 236 | vtysh_config_init (); |
| 237 | |
| 238 | vty_init_vtysh (); |
| 239 | |
| 240 | sort_node (); |
| 241 | |
| 242 | vtysh_connect_all (); |
| 243 | |
| 244 | /* Read vtysh configuration file. */ |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 245 | vtysh_read_config (config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 246 | |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 247 | /* If eval mode. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 248 | if (eval_flag) |
| 249 | { |
| 250 | vtysh_execute_no_pager (eval_line); |
| 251 | exit (0); |
| 252 | } |
| 253 | |
| 254 | /* Boot startup configuration file. */ |
| 255 | if (boot_flag) |
| 256 | { |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 257 | if (vtysh_read_config (integrate_default)) |
| 258 | { |
| 259 | fprintf (stderr, "Can't open configuration file [%s]\n", |
| 260 | integrate_default); |
| 261 | exit (1); |
| 262 | } |
| 263 | else |
| 264 | exit (0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | vtysh_pager_init (); |
| 268 | |
| 269 | vtysh_readline_init (); |
| 270 | |
| 271 | vty_hello (vty); |
| 272 | |
| 273 | vtysh_auth (); |
| 274 | |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 275 | /* Enter into enable node. */ |
| 276 | vtysh_execute ("enable"); |
| 277 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 278 | /* Preparation for longjmp() in sigtstp(). */ |
| 279 | sigsetjmp (jmpbuf, 1); |
| 280 | jmpflag = 1; |
| 281 | |
| 282 | /* Main command loop. */ |
| 283 | while (vtysh_rl_gets ()) |
| 284 | vtysh_execute (line_read); |
| 285 | |
| 286 | printf ("\n"); |
| 287 | |
| 288 | /* Rest in peace. */ |
| 289 | exit (0); |
| 290 | } |