]>
Commit | Line | Data |
---|---|---|
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. */ |
40 | char *progname; | |
41 | ||
42 | /* Configuration file name. Usually this is configurable, but vtysh | |
95e735b5 | 43 | * has static configuration file only. */ |
718e3744 | 44 | char *config_file = NULL; |
45 | ||
46 | /* Configuration file and directory. */ | |
47 | char *config_current = NULL; | |
48 | char config_default[] = SYSCONFDIR VTYSH_DEFAULT_CONFIG; | |
49 | ||
50 | /* Integrated configuration file. */ | |
51 | char *integrate_file = NULL; | |
52 | char *integrate_current = NULL; | |
53 | #if 0 | |
54 | char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG; | |
55 | #endif | |
56 | ||
57 | /* Flag for indicate executing child command. */ | |
58 | int execute_flag = 0; | |
59 | ||
60 | /* For sigsetjmp() & siglongjmp(). */ | |
61 | static sigjmp_buf jmpbuf; | |
62 | ||
63 | /* Flag for avoid recursive siglongjmp() call. */ | |
64 | static int jmpflag = 0; | |
65 | ||
66 | /* A static variable for holding the line. */ | |
67 | static char *line_read; | |
68 | ||
69 | /* Master of threads. */ | |
70 | struct thread_master *master; | |
b094d260 | 71 | |
718e3744 | 72 | /* SIGTSTP handler. This function care user's ^Z input. */ |
73 | void | |
74 | sigtstp (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. */ | |
94 | void | |
95 | sigint (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. */ | |
107 | RETSIGTYPE * | |
108 | signal_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. */ | |
130 | void | |
131 | signal_init () | |
132 | { | |
133 | signal_set (SIGINT, sigint); | |
134 | signal_set (SIGTSTP, sigtstp); | |
135 | signal_set (SIGPIPE, SIG_IGN); | |
136 | } | |
b094d260 | 137 | |
718e3744 | 138 | /* Help information display. */ |
139 | static void | |
140 | usage (int status) | |
141 | { | |
142 | if (status != 0) | |
143 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); | |
144 | else | |
95e735b5 | 145 | printf ("Usage : %s [OPTION...]\n\n" \ |
146 | "Integrated shell for Quagga routing software suite. \n\n"\ | |
147 | "-b, --boot Execute boot startup configuration\n" \ | |
148 | "-c, --command Execute argument as command\n "\ | |
149 | "-h, --help Display this help and exit\n\n" \ | |
150 | "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); | |
151 | ||
718e3744 | 152 | exit (status); |
153 | } | |
154 | ||
155 | /* VTY shell options, we use GNU getopt library. */ | |
156 | struct option longopts[] = | |
157 | { | |
b094d260 | 158 | { "boot", no_argument, NULL, 'b'}, |
4991f6ca | 159 | /* For compatibility with older zebra/quagga versions */ |
718e3744 | 160 | { "eval", required_argument, NULL, 'e'}, |
4991f6ca | 161 | { "command", required_argument, NULL, 'c'}, |
718e3744 | 162 | { "help", no_argument, NULL, 'h'}, |
163 | { 0 } | |
164 | }; | |
b094d260 | 165 | |
718e3744 | 166 | /* Read a string, and return a pointer to it. Returns NULL on EOF. */ |
167 | char * | |
168 | vtysh_rl_gets () | |
169 | { | |
4991f6ca | 170 | HIST_ENTRY *last; |
718e3744 | 171 | /* If the buffer has already been allocated, return the memory |
95e735b5 | 172 | * to the free pool. */ |
718e3744 | 173 | if (line_read) |
174 | { | |
175 | free (line_read); | |
176 | line_read = NULL; | |
177 | } | |
178 | ||
179 | /* Get a line from the user. Change prompt according to node. XXX. */ | |
180 | line_read = readline (vtysh_prompt ()); | |
181 | ||
4991f6ca | 182 | /* If the line has any text in it, save it on the history. But only if |
95e735b5 | 183 | * last command in history isn't the same one. */ |
718e3744 | 184 | if (line_read && *line_read) |
4991f6ca | 185 | { |
186 | using_history(); | |
187 | last = previous_history(); | |
188 | if (!last || strcmp (last->line, line_read) != 0) | |
189 | add_history (line_read); | |
190 | } | |
718e3744 | 191 | |
192 | return (line_read); | |
193 | } | |
b094d260 | 194 | |
718e3744 | 195 | /* VTY shell main routine. */ |
196 | int | |
197 | main (int argc, char **argv, char **env) | |
198 | { | |
199 | char *p; | |
200 | int opt; | |
201 | int eval_flag = 0; | |
202 | int boot_flag = 0; | |
203 | char *eval_line = NULL; | |
204 | char *integrated_file = NULL; | |
205 | ||
206 | /* Preserve name of myself. */ | |
207 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); | |
208 | ||
209 | /* Option handling. */ | |
210 | while (1) | |
211 | { | |
4991f6ca | 212 | opt = getopt_long (argc, argv, "be:c:h", longopts, 0); |
718e3744 | 213 | |
214 | if (opt == EOF) | |
215 | break; | |
216 | ||
217 | switch (opt) | |
218 | { | |
219 | case 0: | |
220 | break; | |
221 | case 'b': | |
222 | boot_flag = 1; | |
223 | break; | |
224 | case 'e': | |
4991f6ca | 225 | case 'c': |
718e3744 | 226 | eval_flag = 1; |
227 | eval_line = optarg; | |
228 | break; | |
229 | case 'h': | |
230 | usage (0); | |
231 | break; | |
232 | case 'i': | |
233 | integrated_file = strdup (optarg); | |
234 | default: | |
235 | usage (1); | |
236 | break; | |
237 | } | |
238 | } | |
239 | ||
240 | /* Initialize user input buffer. */ | |
241 | line_read = NULL; | |
242 | ||
243 | /* Signal and others. */ | |
244 | signal_init (); | |
245 | ||
246 | /* Make vty structure and register commands. */ | |
247 | vtysh_init_vty (); | |
248 | vtysh_init_cmd (); | |
249 | vtysh_user_init (); | |
250 | vtysh_config_init (); | |
251 | ||
252 | vty_init_vtysh (); | |
253 | ||
254 | sort_node (); | |
255 | ||
256 | vtysh_connect_all (); | |
257 | ||
258 | /* Read vtysh configuration file. */ | |
259 | vtysh_read_config (config_file, config_current, config_default); | |
260 | ||
95e735b5 | 261 | /* If eval mode. */ |
718e3744 | 262 | if (eval_flag) |
263 | { | |
264 | vtysh_execute_no_pager (eval_line); | |
265 | exit (0); | |
266 | } | |
267 | ||
268 | /* Boot startup configuration file. */ | |
269 | if (boot_flag) | |
270 | { | |
271 | vtysh_read_config (integrate_file, integrate_current, integrate_default); | |
272 | exit (0); | |
273 | } | |
274 | ||
275 | vtysh_pager_init (); | |
276 | ||
277 | vtysh_readline_init (); | |
278 | ||
279 | vty_hello (vty); | |
280 | ||
281 | vtysh_auth (); | |
282 | ||
283 | /* Preparation for longjmp() in sigtstp(). */ | |
284 | sigsetjmp (jmpbuf, 1); | |
285 | jmpflag = 1; | |
286 | ||
287 | /* Main command loop. */ | |
288 | while (vtysh_rl_gets ()) | |
289 | vtysh_execute (line_read); | |
290 | ||
291 | printf ("\n"); | |
292 | ||
293 | /* Rest in peace. */ | |
294 | exit (0); | |
295 | } |