paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Configuration generator. |
| 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 | #include <zebra.h> |
| 22 | |
| 23 | #include "command.h" |
| 24 | #include "linklist.h" |
| 25 | #include "memory.h" |
| 26 | |
| 27 | #include "vtysh/vtysh.h" |
| 28 | |
| 29 | vector configvec; |
| 30 | |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 31 | extern int vtysh_writeconfig_integrated; |
| 32 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | struct config |
| 34 | { |
| 35 | /* Configuration node name. */ |
| 36 | char *name; |
| 37 | |
| 38 | /* Configuration string line. */ |
| 39 | struct list *line; |
| 40 | |
| 41 | /* Configuration can be nest. */ |
| 42 | struct config *config; |
| 43 | |
| 44 | /* Index of this config. */ |
| 45 | u_int32_t index; |
| 46 | }; |
| 47 | |
| 48 | struct list *config_top; |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 49 | |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 50 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 51 | line_cmp (char *c1, char *c2) |
| 52 | { |
| 53 | return strcmp (c1, c2); |
| 54 | } |
| 55 | |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 56 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 57 | line_del (char *line) |
| 58 | { |
| 59 | XFREE (MTYPE_VTYSH_CONFIG_LINE, line); |
| 60 | } |
| 61 | |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 62 | static struct config * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 63 | config_new () |
| 64 | { |
| 65 | struct config *config; |
| 66 | config = XCALLOC (MTYPE_VTYSH_CONFIG, sizeof (struct config)); |
| 67 | return config; |
| 68 | } |
| 69 | |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 70 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 71 | config_cmp (struct config *c1, struct config *c2) |
| 72 | { |
| 73 | return strcmp (c1->name, c2->name); |
| 74 | } |
| 75 | |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 76 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | config_del (struct config* config) |
| 78 | { |
| 79 | list_delete (config->line); |
| 80 | if (config->name) |
| 81 | XFREE (MTYPE_VTYSH_CONFIG_LINE, config->name); |
| 82 | XFREE (MTYPE_VTYSH_CONFIG, config); |
| 83 | } |
| 84 | |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 85 | static struct config * |
hasso | dda0952 | 2004-10-07 21:40:25 +0000 | [diff] [blame] | 86 | config_get (int index, const char *line) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 87 | { |
| 88 | struct config *config; |
| 89 | struct config *config_loop; |
| 90 | struct list *master; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 91 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 92 | |
| 93 | config = config_loop = NULL; |
| 94 | |
| 95 | master = vector_lookup_ensure (configvec, index); |
| 96 | |
| 97 | if (! master) |
| 98 | { |
| 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); |
| 103 | } |
| 104 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 105 | for (ALL_LIST_ELEMENTS (master, node, nnode, config_loop)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 106 | { |
| 107 | if (strcmp (config_loop->name, line) == 0) |
| 108 | config = config_loop; |
| 109 | } |
| 110 | |
| 111 | if (! config) |
| 112 | { |
| 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); |
| 120 | } |
| 121 | return config; |
| 122 | } |
| 123 | |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 124 | static void |
hasso | dda0952 | 2004-10-07 21:40:25 +0000 | [diff] [blame] | 125 | config_add_line (struct list *config, const char *line) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 126 | { |
| 127 | listnode_add (config, XSTRDUP (MTYPE_VTYSH_CONFIG_LINE, line)); |
| 128 | } |
| 129 | |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 130 | static void |
hasso | dda0952 | 2004-10-07 21:40:25 +0000 | [diff] [blame] | 131 | config_add_line_uniq (struct list *config, const char *line) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 132 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 133 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 134 | char *pnt; |
| 135 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 136 | for (ALL_LIST_ELEMENTS (config, node, nnode, pnt)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 137 | { |
| 138 | if (strcmp (pnt, line) == 0) |
| 139 | return; |
| 140 | } |
| 141 | listnode_add_sort (config, XSTRDUP (MTYPE_VTYSH_CONFIG_LINE, line)); |
| 142 | } |
| 143 | |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 144 | static void |
hasso | dda0952 | 2004-10-07 21:40:25 +0000 | [diff] [blame] | 145 | vtysh_config_parse_line (const char *line) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 146 | { |
| 147 | char c; |
| 148 | static struct config *config = NULL; |
| 149 | |
| 150 | if (! line) |
| 151 | return; |
| 152 | |
| 153 | c = line[0]; |
| 154 | |
| 155 | if (c == '\0') |
| 156 | return; |
| 157 | |
| 158 | /* printf ("[%s]\n", line); */ |
| 159 | |
| 160 | switch (c) |
| 161 | { |
| 162 | case '!': |
| 163 | case '#': |
| 164 | break; |
| 165 | case ' ': |
| 166 | /* Store line to current configuration. */ |
| 167 | if (config) |
| 168 | { |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 169 | if (strncmp (line, " address-family vpnv4", |
| 170 | strlen (" address-family vpnv4")) == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 171 | config = config_get (BGP_VPNV4_NODE, line); |
Lou Berger | 13c378d | 2016-01-12 13:41:56 -0500 | [diff] [blame] | 172 | else if (strncmp (line, " address-family vpn6", |
| 173 | strlen (" address-family vpn6")) == 0) |
| 174 | config = config_get (BGP_VPNV6_NODE, line); |
Lou Berger | a3fda88 | 2016-01-12 13:42:04 -0500 | [diff] [blame^] | 175 | else if (strncmp (line, " address-family encapv6", |
| 176 | strlen (" address-family encapv6")) == 0) |
| 177 | config = config_get (BGP_ENCAPV6_NODE, line); |
| 178 | else if (strncmp (line, " address-family encap", |
| 179 | strlen (" address-family encap")) == 0) |
| 180 | config = config_get (BGP_ENCAP_NODE, line); |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 181 | else if (strncmp (line, " address-family ipv4 multicast", |
| 182 | strlen (" address-family ipv4 multicast")) == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 183 | config = config_get (BGP_IPV4M_NODE, line); |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 184 | else if (strncmp (line, " address-family ipv6", |
| 185 | strlen (" address-family ipv6")) == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 186 | config = config_get (BGP_IPV6_NODE, line); |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 187 | else if (config->index == RMAP_NODE || |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 188 | config->index == INTERFACE_NODE || |
| 189 | config->index == VTY_NODE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 190 | config_add_line_uniq (config->line, line); |
| 191 | else |
| 192 | config_add_line (config->line, line); |
| 193 | } |
| 194 | else |
| 195 | config_add_line (config_top, line); |
| 196 | break; |
| 197 | default: |
| 198 | if (strncmp (line, "interface", strlen ("interface")) == 0) |
| 199 | config = config_get (INTERFACE_NODE, line); |
hasso | dfbb912 | 2004-12-22 11:53:09 +0000 | [diff] [blame] | 200 | else if (strncmp (line, "router-id", strlen ("router-id")) == 0) |
| 201 | config = config_get (ZEBRA_NODE, line); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 202 | else if (strncmp (line, "router rip", strlen ("router rip")) == 0) |
| 203 | config = config_get (RIP_NODE, line); |
paul | 97e1c4d | 2003-03-28 02:25:23 +0000 | [diff] [blame] | 204 | else if (strncmp (line, "router ripng", strlen ("router ripng")) == 0) |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 205 | config = config_get (RIPNG_NODE, line); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 206 | else if (strncmp (line, "router ospf", strlen ("router ospf")) == 0) |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 207 | config = config_get (OSPF_NODE, line); |
paul | 97e1c4d | 2003-03-28 02:25:23 +0000 | [diff] [blame] | 208 | else if (strncmp (line, "router ospf6", strlen ("router ospf6")) == 0) |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 209 | config = config_get (OSPF6_NODE, line); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 210 | else if (strncmp (line, "router bgp", strlen ("router bgp")) == 0) |
| 211 | config = config_get (BGP_NODE, line); |
paul | 13b8baa | 2004-01-15 01:00:49 +0000 | [diff] [blame] | 212 | else if (strncmp (line, "router isis", strlen ("router isis")) == 0) |
hasso | c25e458 | 2003-12-23 10:39:08 +0000 | [diff] [blame] | 213 | config = config_get (ISIS_NODE, line); |
hasso | dfbb912 | 2004-12-22 11:53:09 +0000 | [diff] [blame] | 214 | else if (strncmp (line, "router bgp", strlen ("router bgp")) == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 215 | config = config_get (BGP_NODE, line); |
| 216 | else if (strncmp (line, "route-map", strlen ("route-map")) == 0) |
| 217 | config = config_get (RMAP_NODE, line); |
| 218 | else if (strncmp (line, "access-list", strlen ("access-list")) == 0) |
| 219 | config = config_get (ACCESS_NODE, line); |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 220 | else if (strncmp (line, "ipv6 access-list", |
| 221 | strlen ("ipv6 access-list")) == 0) |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 222 | config = config_get (ACCESS_IPV6_NODE, line); |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 223 | else if (strncmp (line, "ip prefix-list", |
| 224 | strlen ("ip prefix-list")) == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 225 | config = config_get (PREFIX_NODE, line); |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 226 | else if (strncmp (line, "ipv6 prefix-list", |
| 227 | strlen ("ipv6 prefix-list")) == 0) |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 228 | config = config_get (PREFIX_IPV6_NODE, line); |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 229 | else if (strncmp (line, "ip as-path access-list", |
| 230 | strlen ("ip as-path access-list")) == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 231 | config = config_get (AS_LIST_NODE, line); |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 232 | else if (strncmp (line, "ip community-list", |
| 233 | strlen ("ip community-list")) == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 234 | config = config_get (COMMUNITY_LIST_NODE, line); |
| 235 | else if (strncmp (line, "ip route", strlen ("ip route")) == 0) |
| 236 | config = config_get (IP_NODE, line); |
paul | 97e1c4d | 2003-03-28 02:25:23 +0000 | [diff] [blame] | 237 | else if (strncmp (line, "ipv6 route", strlen ("ipv6 route")) == 0) |
| 238 | config = config_get (IP_NODE, line); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 239 | else if (strncmp (line, "key", strlen ("key")) == 0) |
| 240 | config = config_get (KEYCHAIN_NODE, line); |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 241 | else if (strncmp (line, "line", strlen ("line")) == 0) |
| 242 | config = config_get (VTY_NODE, line); |
| 243 | else if ( (strncmp (line, "ipv6 forwarding", |
| 244 | strlen ("ipv6 forwarding")) == 0) |
| 245 | || (strncmp (line, "ip forwarding", |
| 246 | strlen ("ip forwarding")) == 0) ) |
| 247 | config = config_get (FORWARDING_NODE, line); |
| 248 | else if (strncmp (line, "service", strlen ("service")) == 0) |
| 249 | config = config_get (SERVICE_NODE, line); |
hasso | dfbb912 | 2004-12-22 11:53:09 +0000 | [diff] [blame] | 250 | else if (strncmp (line, "debug", strlen ("debug")) == 0) |
| 251 | config = config_get (DEBUG_NODE, line); |
hasso | 060d438 | 2005-03-09 12:41:14 +0000 | [diff] [blame] | 252 | else if (strncmp (line, "password", strlen ("password")) == 0 |
| 253 | || strncmp (line, "enable password", |
| 254 | strlen ("enable password")) == 0) |
| 255 | config = config_get (AAA_NODE, line); |
Chris Caputo | 6e79f8b | 2009-06-23 05:55:57 +0000 | [diff] [blame] | 256 | else if (strncmp (line, "ip protocol", strlen ("ip protocol")) == 0) |
| 257 | config = config_get (PROTOCOL_NODE, line); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 258 | else |
| 259 | { |
| 260 | if (strncmp (line, "log", strlen ("log")) == 0 |
| 261 | || strncmp (line, "hostname", strlen ("hostname")) == 0 |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 262 | ) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 263 | config_add_line_uniq (config_top, line); |
| 264 | else |
| 265 | config_add_line (config_top, line); |
| 266 | config = NULL; |
| 267 | } |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void |
| 273 | vtysh_config_parse (char *line) |
| 274 | { |
| 275 | char *begin; |
| 276 | char *pnt; |
| 277 | |
| 278 | begin = pnt = line; |
| 279 | |
| 280 | while (*pnt != '\0') |
| 281 | { |
| 282 | if (*pnt == '\n') |
| 283 | { |
| 284 | *pnt++ = '\0'; |
| 285 | vtysh_config_parse_line (begin); |
| 286 | begin = pnt; |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | pnt++; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /* Macro to check delimiter is needed between each configuration line |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 296 | * or not. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 297 | #define NO_DELIMITER(I) \ |
| 298 | ((I) == ACCESS_NODE || (I) == PREFIX_NODE || (I) == IP_NODE \ |
paul | 97e1c4d | 2003-03-28 02:25:23 +0000 | [diff] [blame] | 299 | || (I) == AS_LIST_NODE || (I) == COMMUNITY_LIST_NODE || \ |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 300 | (I) == ACCESS_IPV6_NODE || (I) == PREFIX_IPV6_NODE \ |
hasso | 060d438 | 2005-03-09 12:41:14 +0000 | [diff] [blame] | 301 | || (I) == SERVICE_NODE || (I) == FORWARDING_NODE || (I) == DEBUG_NODE \ |
| 302 | || (I) == AAA_NODE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 303 | |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 304 | /* Display configuration to file pointer. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 305 | void |
| 306 | vtysh_config_dump (FILE *fp) |
| 307 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 308 | struct listnode *node, *nnode; |
| 309 | struct listnode *mnode, *mnnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 310 | struct config *config; |
| 311 | struct list *master; |
| 312 | char *line; |
hasso | dda0952 | 2004-10-07 21:40:25 +0000 | [diff] [blame] | 313 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 314 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 315 | for (ALL_LIST_ELEMENTS (config_top, node, nnode, line)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 316 | { |
| 317 | fprintf (fp, "%s\n", line); |
| 318 | fflush (fp); |
| 319 | } |
| 320 | fprintf (fp, "!\n"); |
| 321 | fflush (fp); |
| 322 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 323 | for (i = 0; i < vector_active (configvec); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 324 | if ((master = vector_slot (configvec, i)) != NULL) |
| 325 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 326 | for (ALL_LIST_ELEMENTS (master, node, nnode, config)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 327 | { |
| 328 | fprintf (fp, "%s\n", config->name); |
hasso | b094d26 | 2004-08-25 12:22:00 +0000 | [diff] [blame] | 329 | fflush (fp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 330 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 331 | for (ALL_LIST_ELEMENTS (config->line, mnode, mnnode, line)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 332 | { |
| 333 | fprintf (fp, "%s\n", line); |
| 334 | fflush (fp); |
| 335 | } |
| 336 | if (! NO_DELIMITER (i)) |
| 337 | { |
| 338 | fprintf (fp, "!\n"); |
| 339 | fflush (fp); |
| 340 | } |
| 341 | } |
| 342 | if (NO_DELIMITER (i)) |
| 343 | { |
| 344 | fprintf (fp, "!\n"); |
| 345 | fflush (fp); |
| 346 | } |
| 347 | } |
| 348 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 349 | for (i = 0; i < vector_active (configvec); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 350 | if ((master = vector_slot (configvec, i)) != NULL) |
| 351 | { |
| 352 | list_delete (master); |
| 353 | vector_slot (configvec, i) = NULL; |
| 354 | } |
| 355 | list_delete_all_node (config_top); |
| 356 | } |
| 357 | |
| 358 | /* Read up configuration file from file_name. */ |
| 359 | static void |
| 360 | vtysh_read_file (FILE *confp) |
| 361 | { |
| 362 | int ret; |
| 363 | struct vty *vty; |
| 364 | |
| 365 | vty = vty_new (); |
| 366 | vty->fd = 0; /* stdout */ |
| 367 | vty->type = VTY_TERM; |
| 368 | vty->node = CONFIG_NODE; |
| 369 | |
| 370 | vtysh_execute_no_pager ("enable"); |
| 371 | vtysh_execute_no_pager ("configure terminal"); |
| 372 | |
hasso | 95e735b | 2004-08-26 13:08:30 +0000 | [diff] [blame] | 373 | /* Execute configuration file. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 374 | ret = vtysh_config_from_file (vty, confp); |
| 375 | |
| 376 | vtysh_execute_no_pager ("end"); |
| 377 | vtysh_execute_no_pager ("disable"); |
| 378 | |
| 379 | vty_close (vty); |
| 380 | |
| 381 | if (ret != CMD_SUCCESS) |
| 382 | { |
| 383 | switch (ret) |
| 384 | { |
| 385 | case CMD_ERR_AMBIGUOUS: |
| 386 | fprintf (stderr, "Ambiguous command.\n"); |
| 387 | break; |
| 388 | case CMD_ERR_NO_MATCH: |
| 389 | fprintf (stderr, "There is no such command.\n"); |
| 390 | break; |
| 391 | } |
| 392 | fprintf (stderr, "Error occured during reading below line.\n%s\n", |
| 393 | vty->buf); |
| 394 | exit (1); |
| 395 | } |
| 396 | } |
| 397 | |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 398 | /* Read up configuration file from config_default_dir. */ |
| 399 | int |
| 400 | vtysh_read_config (char *config_default_dir) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 401 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 402 | FILE *confp = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 403 | |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 404 | confp = fopen (config_default_dir, "r"); |
| 405 | if (confp == NULL) |
| 406 | return (1); |
hasso | 67e29ab | 2004-08-26 22:21:31 +0000 | [diff] [blame] | 407 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 408 | vtysh_read_file (confp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 409 | fclose (confp); |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 410 | host_config_set (config_default_dir); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 411 | |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 412 | return (0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 413 | } |
| 414 | |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 415 | /* We don't write vtysh specific into file from vtysh. vtysh.conf should |
| 416 | * be edited by hand. So, we handle only "write terminal" case here and |
| 417 | * integrate vtysh specific conf with conf from daemons. |
| 418 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 419 | void |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 420 | vtysh_config_write () |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 421 | { |
hasso | dda0952 | 2004-10-07 21:40:25 +0000 | [diff] [blame] | 422 | char line[81]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 423 | extern struct host host; |
| 424 | |
| 425 | if (host.name) |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 426 | { |
| 427 | sprintf (line, "hostname %s", host.name); |
| 428 | vtysh_config_parse_line(line); |
| 429 | } |
| 430 | if (vtysh_writeconfig_integrated) |
| 431 | vtysh_config_parse_line ("service integrated-vtysh-config"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void |
| 435 | vtysh_config_init () |
| 436 | { |
| 437 | config_top = list_new (); |
| 438 | config_top->del = (void (*) (void *))line_del; |
| 439 | configvec = vector_init (1); |
| 440 | } |