ospfd: Tighten up the connected check for redistribution
[jleu-quagga.git] / vtysh / vtysh_config.c
blobfb8a12690ab3acd863d5e18e3d7d9a2c763e4165
1 /* Configuration generator.
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
9 later version.
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
19 02111-1307, USA. */
21 #include <zebra.h>
23 #include "command.h"
24 #include "linklist.h"
25 #include "memory.h"
27 #include "vtysh/vtysh.h"
29 vector configvec;
31 extern int vtysh_writeconfig_integrated;
33 struct config
35 /* Configuration node name. */
36 char *name;
38 /* Configuration string line. */
39 struct list *line;
41 /* Configuration can be nest. */
42 struct config *config;
44 /* Index of this config. */
45 u_int32_t index;
48 struct list *config_top;
50 int
51 line_cmp (char *c1, char *c2)
53 return strcmp (c1, c2);
56 void
57 line_del (char *line)
59 XFREE (MTYPE_VTYSH_CONFIG_LINE, line);
62 struct config *
63 config_new ()
65 struct config *config;
66 config = XCALLOC (MTYPE_VTYSH_CONFIG, sizeof (struct config));
67 return config;
70 int
71 config_cmp (struct config *c1, struct config *c2)
73 return strcmp (c1->name, c2->name);
76 void
77 config_del (struct config* config)
79 list_delete (config->line);
80 if (config->name)
81 XFREE (MTYPE_VTYSH_CONFIG_LINE, config->name);
82 XFREE (MTYPE_VTYSH_CONFIG, config);
85 struct config *
86 config_get (int index, const char *line)
88 struct config *config;
89 struct config *config_loop;
90 struct list *master;
91 struct listnode *node, *nnode;
93 config = config_loop = NULL;
95 master = vector_lookup_ensure (configvec, index);
97 if (! master)
99 master = list_new ();
100 master->del = (void (*) (void *))config_del;
101 master->cmp = (int (*)(void *, void *)) config_cmp;
102 vector_set_index (configvec, index, master);
105 for (ALL_LIST_ELEMENTS (master, node, nnode, config_loop))
107 if (strcmp (config_loop->name, line) == 0)
108 config = config_loop;
111 if (! config)
113 config = config_new ();
114 config->line = list_new ();
115 config->line->del = (void (*) (void *))line_del;
116 config->line->cmp = (int (*)(void *, void *)) line_cmp;
117 config->name = XSTRDUP (MTYPE_VTYSH_CONFIG_LINE, line);
118 config->index = index;
119 listnode_add (master, config);
121 return config;
124 void
125 config_add_line (struct list *config, const char *line)
127 listnode_add (config, XSTRDUP (MTYPE_VTYSH_CONFIG_LINE, line));
130 void
131 config_add_line_uniq (struct list *config, const char *line)
133 struct listnode *node, *nnode;
134 char *pnt;
136 for (ALL_LIST_ELEMENTS (config, node, nnode, pnt))
138 if (strcmp (pnt, line) == 0)
139 return;
141 listnode_add_sort (config, XSTRDUP (MTYPE_VTYSH_CONFIG_LINE, line));
144 void
145 vtysh_config_parse_line (const char *line)
147 char c;
148 static struct config *config = NULL;
150 if (! line)
151 return;
153 c = line[0];
155 if (c == '\0')
156 return;
158 /* printf ("[%s]\n", line); */
160 switch (c)
162 case '!':
163 case '#':
164 break;
165 case ' ':
166 /* Store line to current configuration. */
167 if (config)
169 if (strncmp (line, " address-family vpnv4",
170 strlen (" address-family vpnv4")) == 0)
171 config = config_get (BGP_VPNV4_NODE, line);
172 else if (strncmp (line, " address-family ipv4 multicast",
173 strlen (" address-family ipv4 multicast")) == 0)
174 config = config_get (BGP_IPV4M_NODE, line);
175 else if (strncmp (line, " address-family ipv6",
176 strlen (" address-family ipv6")) == 0)
177 config = config_get (BGP_IPV6_NODE, line);
178 else if (config->index == RMAP_NODE ||
179 config->index == INTERFACE_NODE ||
180 config->index == VTY_NODE)
181 config_add_line_uniq (config->line, line);
182 else
183 config_add_line (config->line, line);
185 else
186 config_add_line (config_top, line);
187 break;
188 default:
189 if (strncmp (line, "interface", strlen ("interface")) == 0)
190 config = config_get (INTERFACE_NODE, line);
191 else if (strncmp (line, "router-id", strlen ("router-id")) == 0)
192 config = config_get (ZEBRA_NODE, line);
193 else if (strncmp (line, "router rip", strlen ("router rip")) == 0)
194 config = config_get (RIP_NODE, line);
195 else if (strncmp (line, "router ripng", strlen ("router ripng")) == 0)
196 config = config_get (RIPNG_NODE, line);
197 else if (strncmp (line, "router ospf", strlen ("router ospf")) == 0)
198 config = config_get (OSPF_NODE, line);
199 else if (strncmp (line, "router ospf6", strlen ("router ospf6")) == 0)
200 config = config_get (OSPF6_NODE, line);
201 else if (strncmp (line, "router bgp", strlen ("router bgp")) == 0)
202 config = config_get (BGP_NODE, line);
203 else if (strncmp (line, "router isis", strlen ("router isis")) == 0)
204 config = config_get (ISIS_NODE, line);
205 else if (strncmp (line, "router bgp", strlen ("router bgp")) == 0)
206 config = config_get (BGP_NODE, line);
207 else if (strncmp (line, "route-map", strlen ("route-map")) == 0)
208 config = config_get (RMAP_NODE, line);
209 else if (strncmp (line, "access-list", strlen ("access-list")) == 0)
210 config = config_get (ACCESS_NODE, line);
211 else if (strncmp (line, "ipv6 access-list",
212 strlen ("ipv6 access-list")) == 0)
213 config = config_get (ACCESS_IPV6_NODE, line);
214 else if (strncmp (line, "ip prefix-list",
215 strlen ("ip prefix-list")) == 0)
216 config = config_get (PREFIX_NODE, line);
217 else if (strncmp (line, "ipv6 prefix-list",
218 strlen ("ipv6 prefix-list")) == 0)
219 config = config_get (PREFIX_IPV6_NODE, line);
220 else if (strncmp (line, "ip as-path access-list",
221 strlen ("ip as-path access-list")) == 0)
222 config = config_get (AS_LIST_NODE, line);
223 else if (strncmp (line, "ip community-list",
224 strlen ("ip community-list")) == 0)
225 config = config_get (COMMUNITY_LIST_NODE, line);
226 else if (strncmp (line, "ip route", strlen ("ip route")) == 0)
227 config = config_get (IP_NODE, line);
228 else if (strncmp (line, "ipv6 route", strlen ("ipv6 route")) == 0)
229 config = config_get (IP_NODE, line);
230 else if (strncmp (line, "key", strlen ("key")) == 0)
231 config = config_get (KEYCHAIN_NODE, line);
232 else if (strncmp (line, "line", strlen ("line")) == 0)
233 config = config_get (VTY_NODE, line);
234 else if ( (strncmp (line, "ipv6 forwarding",
235 strlen ("ipv6 forwarding")) == 0)
236 || (strncmp (line, "ip forwarding",
237 strlen ("ip forwarding")) == 0) )
238 config = config_get (FORWARDING_NODE, line);
239 else if (strncmp (line, "service", strlen ("service")) == 0)
240 config = config_get (SERVICE_NODE, line);
241 else if (strncmp (line, "debug", strlen ("debug")) == 0)
242 config = config_get (DEBUG_NODE, line);
243 else if (strncmp (line, "password", strlen ("password")) == 0
244 || strncmp (line, "enable password",
245 strlen ("enable password")) == 0)
246 config = config_get (AAA_NODE, line);
247 else if (strncmp (line, "ip protocol", strlen ("ip protocol")) == 0)
248 config = config_get (PROTOCOL_NODE, line);
249 else
251 if (strncmp (line, "log", strlen ("log")) == 0
252 || strncmp (line, "hostname", strlen ("hostname")) == 0
254 config_add_line_uniq (config_top, line);
255 else
256 config_add_line (config_top, line);
257 config = NULL;
259 break;
263 void
264 vtysh_config_parse (char *line)
266 char *begin;
267 char *pnt;
269 begin = pnt = line;
271 while (*pnt != '\0')
273 if (*pnt == '\n')
275 *pnt++ = '\0';
276 vtysh_config_parse_line (begin);
277 begin = pnt;
279 else
281 pnt++;
286 /* Macro to check delimiter is needed between each configuration line
287 * or not. */
288 #define NO_DELIMITER(I) \
289 ((I) == ACCESS_NODE || (I) == PREFIX_NODE || (I) == IP_NODE \
290 || (I) == AS_LIST_NODE || (I) == COMMUNITY_LIST_NODE || \
291 (I) == ACCESS_IPV6_NODE || (I) == PREFIX_IPV6_NODE \
292 || (I) == SERVICE_NODE || (I) == FORWARDING_NODE || (I) == DEBUG_NODE \
293 || (I) == AAA_NODE)
295 /* Display configuration to file pointer. */
296 void
297 vtysh_config_dump (FILE *fp)
299 struct listnode *node, *nnode;
300 struct listnode *mnode, *mnnode;
301 struct config *config;
302 struct list *master;
303 char *line;
304 unsigned int i;
306 for (ALL_LIST_ELEMENTS (config_top, node, nnode, line))
308 fprintf (fp, "%s\n", line);
309 fflush (fp);
311 fprintf (fp, "!\n");
312 fflush (fp);
314 for (i = 0; i < vector_active (configvec); i++)
315 if ((master = vector_slot (configvec, i)) != NULL)
317 for (ALL_LIST_ELEMENTS (master, node, nnode, config))
319 fprintf (fp, "%s\n", config->name);
320 fflush (fp);
322 for (ALL_LIST_ELEMENTS (config->line, mnode, mnnode, line))
324 fprintf (fp, "%s\n", line);
325 fflush (fp);
327 if (! NO_DELIMITER (i))
329 fprintf (fp, "!\n");
330 fflush (fp);
333 if (NO_DELIMITER (i))
335 fprintf (fp, "!\n");
336 fflush (fp);
340 for (i = 0; i < vector_active (configvec); i++)
341 if ((master = vector_slot (configvec, i)) != NULL)
343 list_delete (master);
344 vector_slot (configvec, i) = NULL;
346 list_delete_all_node (config_top);
349 /* Read up configuration file from file_name. */
350 static void
351 vtysh_read_file (FILE *confp)
353 int ret;
354 struct vty *vty;
356 vty = vty_new ();
357 vty->fd = 0; /* stdout */
358 vty->type = VTY_TERM;
359 vty->node = CONFIG_NODE;
361 vtysh_execute_no_pager ("enable");
362 vtysh_execute_no_pager ("configure terminal");
364 /* Execute configuration file. */
365 ret = vtysh_config_from_file (vty, confp);
367 vtysh_execute_no_pager ("end");
368 vtysh_execute_no_pager ("disable");
370 vty_close (vty);
372 if (ret != CMD_SUCCESS)
374 switch (ret)
376 case CMD_ERR_AMBIGUOUS:
377 fprintf (stderr, "Ambiguous command.\n");
378 break;
379 case CMD_ERR_NO_MATCH:
380 fprintf (stderr, "There is no such command.\n");
381 break;
383 fprintf (stderr, "Error occured during reading below line.\n%s\n",
384 vty->buf);
385 exit (1);
389 /* Read up configuration file from config_default_dir. */
391 vtysh_read_config (char *config_default_dir)
393 FILE *confp = NULL;
395 confp = fopen (config_default_dir, "r");
396 if (confp == NULL)
397 return (1);
399 vtysh_read_file (confp);
400 fclose (confp);
401 host_config_set (config_default_dir);
403 return (0);
406 /* We don't write vtysh specific into file from vtysh. vtysh.conf should
407 * be edited by hand. So, we handle only "write terminal" case here and
408 * integrate vtysh specific conf with conf from daemons.
410 void
411 vtysh_config_write ()
413 char line[81];
414 extern struct host host;
416 if (host.name)
418 sprintf (line, "hostname %s", host.name);
419 vtysh_config_parse_line(line);
421 if (vtysh_writeconfig_integrated)
422 vtysh_config_parse_line ("service integrated-vtysh-config");
425 void
426 vtysh_config_init ()
428 config_top = list_new ();
429 config_top->del = (void (*) (void *))line_del;
430 configvec = vector_init (1);