András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 1 | /* Code to restore the iptables state, from file by ip6tables-save. |
| 2 | * Author: Andras Kis-Szabo <kisza@sch.bme.hu> |
| 3 | * |
| 4 | * based on iptables-restore |
| 5 | * Authors: |
| 6 | * Harald Welte <laforge@gnumonks.org> |
| 7 | * Rusty Russell <rusty@linuxcare.com.au> |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 8 | * This code is distributed under the terms of GNU GPL v2 |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 9 | * |
Martin Josefsson | 4e5e29a | 2004-02-02 19:59:17 +0000 | [diff] [blame^] | 10 | * $Id: ip6tables-restore.c,v 1.18 2004/02/02 19:58:36 gandalf Exp $ |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <getopt.h> |
| 14 | #include <sys/errno.h> |
| 15 | #include <string.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include "ip6tables.h" |
| 19 | #include "libiptc/libip6tc.h" |
| 20 | |
| 21 | #ifdef DEBUG |
| 22 | #define DEBUGP(x, args...) fprintf(stderr, x, ## args) |
| 23 | #else |
| 24 | #define DEBUGP(x, args...) |
| 25 | #endif |
| 26 | |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 27 | static int binary = 0, counters = 0, verbose = 0, noflush = 0; |
| 28 | |
| 29 | /* Keeping track of external matches and targets. */ |
| 30 | static struct option options[] = { |
| 31 | { "binary", 0, 0, 'b' }, |
| 32 | { "counters", 0, 0, 'c' }, |
Martin Josefsson | ec78971 | 2004-01-31 19:28:13 +0000 | [diff] [blame] | 33 | { "verbose", 0, 0, 'v' }, |
Martin Josefsson | 4e5e29a | 2004-02-02 19:59:17 +0000 | [diff] [blame^] | 34 | { "test", 0, 0, 't' }, |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 35 | { "help", 0, 0, 'h' }, |
| 36 | { "noflush", 0, 0, 'n'}, |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 37 | { "modprobe", 1, 0, 'M'}, |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 38 | { 0 } |
| 39 | }; |
| 40 | |
| 41 | static void print_usage(const char *name, const char *version) __attribute__((noreturn)); |
| 42 | |
| 43 | static void print_usage(const char *name, const char *version) |
| 44 | { |
Martin Josefsson | 4e5e29a | 2004-02-02 19:59:17 +0000 | [diff] [blame^] | 45 | fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-v] [-h]\n" |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 46 | " [ --binary ]\n" |
| 47 | " [ --counters ]\n" |
| 48 | " [ --verbose ]\n" |
Martin Josefsson | 4e5e29a | 2004-02-02 19:59:17 +0000 | [diff] [blame^] | 49 | " [ --test ]\n" |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 50 | " [ --help ]\n" |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 51 | " [ --noflush ]\n" |
| 52 | " [ --modprobe=<command>]\n", name); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 53 | |
| 54 | exit(1); |
| 55 | } |
| 56 | |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 57 | ip6tc_handle_t create_handle(const char *tablename, const char* modprobe) |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 58 | { |
| 59 | ip6tc_handle_t handle; |
| 60 | |
| 61 | handle = ip6tc_init(tablename); |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 62 | |
| 63 | if (!handle) { |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 64 | /* try to insmod the module if iptc_init failed */ |
| 65 | ip6tables_insmod("ip6_tables", modprobe); |
| 66 | handle = ip6tc_init(tablename); |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 67 | } |
| 68 | |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 69 | if (!handle) { |
| 70 | exit_error(PARAMETER_PROBLEM, "%s: unable to initialize" |
| 71 | "table '%s'\n", program_name, tablename); |
| 72 | exit(1); |
| 73 | } |
| 74 | return handle; |
| 75 | } |
| 76 | |
| 77 | int parse_counters(char *string, struct ip6t_counters *ctr) |
| 78 | { |
| 79 | return (sscanf(string, "[%llu:%llu]", &ctr->pcnt, &ctr->bcnt) == 2); |
| 80 | } |
| 81 | |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 82 | /* global new argv and argc */ |
| 83 | static char *newargv[255]; |
| 84 | static int newargc; |
| 85 | |
| 86 | /* function adding one argument to newargv, updating newargc |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 87 | * returns true if argument added, false otherwise */ |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 88 | static int add_argv(char *what) { |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 89 | DEBUGP("add_argv: %s\n", what); |
| 90 | if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) { |
| 91 | newargv[newargc] = strdup(what); |
| 92 | newargc++; |
| 93 | return 1; |
| 94 | } else |
| 95 | return 0; |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static void free_argv(void) { |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 99 | int i; |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 100 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 101 | for (i = 0; i < newargc; i++) |
| 102 | free(newargv[i]); |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 103 | } |
| 104 | |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 105 | int main(int argc, char *argv[]) |
| 106 | { |
Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 107 | ip6tc_handle_t handle = NULL; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 108 | char buffer[10240]; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 109 | int c; |
| 110 | char curtable[IP6T_TABLE_MAXNAMELEN + 1]; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 111 | FILE *in; |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 112 | const char *modprobe = 0; |
Martin Josefsson | 4e5e29a | 2004-02-02 19:59:17 +0000 | [diff] [blame^] | 113 | int in_table = 0, testing = 0; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 114 | |
| 115 | program_name = "ip6tables-restore"; |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 116 | program_version = IPTABLES_VERSION; |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 117 | line = 0; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 118 | |
Harald Welte | 3efb6ea | 2001-08-06 18:50:21 +0000 | [diff] [blame] | 119 | #ifdef NO_SHARED_LIBS |
| 120 | init_extensions(); |
| 121 | #endif |
| 122 | |
Martin Josefsson | 4e5e29a | 2004-02-02 19:59:17 +0000 | [diff] [blame^] | 123 | while ((c = getopt_long(argc, argv, "bcvthnM:", options, NULL)) != -1) { |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 124 | switch (c) { |
| 125 | case 'b': |
| 126 | binary = 1; |
| 127 | break; |
| 128 | case 'c': |
| 129 | counters = 1; |
| 130 | break; |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 131 | case 'v': |
| 132 | verbose = 1; |
| 133 | break; |
Martin Josefsson | 4e5e29a | 2004-02-02 19:59:17 +0000 | [diff] [blame^] | 134 | case 't': |
| 135 | testing = 1; |
| 136 | break; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 137 | case 'h': |
| 138 | print_usage("ip6tables-restore", |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 139 | IPTABLES_VERSION); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 140 | break; |
| 141 | case 'n': |
| 142 | noflush = 1; |
| 143 | break; |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 144 | case 'M': |
| 145 | modprobe = optarg; |
| 146 | break; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | if (optind == argc - 1) { |
| 151 | in = fopen(argv[optind], "r"); |
| 152 | if (!in) { |
| 153 | fprintf(stderr, "Can't open %s: %s", argv[optind], |
| 154 | strerror(errno)); |
| 155 | exit(1); |
| 156 | } |
| 157 | } |
| 158 | else if (optind < argc) { |
| 159 | fprintf(stderr, "Unknown arguments found on commandline"); |
| 160 | exit(1); |
| 161 | } |
| 162 | else in = stdin; |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 163 | |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 164 | /* Grab standard input. */ |
| 165 | while (fgets(buffer, sizeof(buffer), in)) { |
| 166 | int ret; |
| 167 | |
| 168 | line++; |
Martin Josefsson | 5065afa | 2004-01-31 19:33:47 +0000 | [diff] [blame] | 169 | if (buffer[0] == '\n') |
| 170 | continue; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 171 | else if (buffer[0] == '#') { |
Martin Josefsson | 3673dcb | 2004-01-31 19:41:49 +0000 | [diff] [blame] | 172 | if (verbose) |
| 173 | fputs(buffer, stdout); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 174 | continue; |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 175 | } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) { |
Martin Josefsson | 4e5e29a | 2004-02-02 19:59:17 +0000 | [diff] [blame^] | 176 | if (!testing) { |
| 177 | DEBUGP("Calling commit\n"); |
| 178 | ret = ip6tc_commit(&handle); |
| 179 | } else { |
| 180 | DEBUGP("Not calling commit, testing\n"); |
| 181 | ret = 1; |
| 182 | } |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 183 | in_table = 0; |
| 184 | } else if ((buffer[0] == '*') && (!in_table)) { |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 185 | /* New table */ |
| 186 | char *table; |
| 187 | |
| 188 | table = strtok(buffer+1, " \t\n"); |
| 189 | DEBUGP("line %u, table '%s'\n", line, table); |
| 190 | if (!table) { |
| 191 | exit_error(PARAMETER_PROBLEM, |
| 192 | "%s: line %u table name invalid\n", |
| 193 | program_name, line); |
| 194 | exit(1); |
| 195 | } |
| 196 | strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN); |
Karsten Desler | 073df8f | 2004-01-31 15:33:55 +0000 | [diff] [blame] | 197 | curtable[IP6T_TABLE_MAXNAMELEN] = '\0'; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 198 | |
Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 199 | if (handle) |
| 200 | ip6tc_free(&handle); |
| 201 | |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 202 | handle = create_handle(table, modprobe); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 203 | if (noflush == 0) { |
| 204 | DEBUGP("Cleaning all chains of table '%s'\n", |
| 205 | table); |
| 206 | for_each_chain(flush_entries, verbose, 1, |
| 207 | &handle); |
| 208 | |
| 209 | DEBUGP("Deleting all user-defined chains " |
| 210 | "of table '%s'\n", table); |
| 211 | for_each_chain(delete_chain, verbose, 0, |
| 212 | &handle) ; |
| 213 | } |
| 214 | |
| 215 | ret = 1; |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 216 | in_table = 1; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 217 | |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 218 | } else if ((buffer[0] == ':') && (in_table)) { |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 219 | /* New chain. */ |
| 220 | char *policy, *chain; |
| 221 | |
| 222 | chain = strtok(buffer+1, " \t\n"); |
| 223 | DEBUGP("line %u, chain '%s'\n", line, chain); |
| 224 | if (!chain) { |
| 225 | exit_error(PARAMETER_PROBLEM, |
| 226 | "%s: line %u chain name invalid\n", |
| 227 | program_name, line); |
| 228 | exit(1); |
| 229 | } |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 230 | |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 231 | if (!ip6tc_builtin(chain, handle)) { |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 232 | DEBUGP("Creating new chain '%s'\n", chain); |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 233 | if (!ip6tc_create_chain(chain, &handle)) |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 234 | exit_error(PARAMETER_PROBLEM, |
| 235 | "error creating chain " |
| 236 | "'%s':%s\n", chain, |
| 237 | strerror(errno)); |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 238 | } |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 239 | |
| 240 | policy = strtok(NULL, " \t\n"); |
| 241 | DEBUGP("line %u, policy '%s'\n", line, policy); |
| 242 | if (!policy) { |
| 243 | exit_error(PARAMETER_PROBLEM, |
| 244 | "%s: line %u policy invalid\n", |
| 245 | program_name, line); |
| 246 | exit(1); |
| 247 | } |
| 248 | |
| 249 | if (strcmp(policy, "-") != 0) { |
| 250 | struct ip6t_counters count; |
| 251 | |
| 252 | if (counters) { |
| 253 | char *ctrs; |
| 254 | ctrs = strtok(NULL, " \t\n"); |
| 255 | |
| 256 | parse_counters(ctrs, &count); |
| 257 | |
| 258 | } else { |
| 259 | memset(&count, 0, |
| 260 | sizeof(struct ip6t_counters)); |
| 261 | } |
| 262 | |
| 263 | DEBUGP("Setting policy of chain %s to %s\n", |
| 264 | chain, policy); |
| 265 | |
| 266 | if (!ip6tc_set_policy(chain, policy, &count, |
| 267 | &handle)) |
| 268 | exit_error(OTHER_PROBLEM, |
| 269 | "Can't set policy `%s'" |
| 270 | " on `%s' line %u: %s\n", |
| 271 | chain, policy, line, |
| 272 | ip6tc_strerror(errno)); |
| 273 | } |
| 274 | |
| 275 | ret = 1; |
| 276 | |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 277 | } else if (in_table) { |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 278 | int a; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 279 | char *ptr = buffer; |
| 280 | char *pcnt = NULL; |
| 281 | char *bcnt = NULL; |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 282 | char *parsestart; |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 283 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 284 | /* the parser */ |
| 285 | char *param_start, *curchar; |
| 286 | int quote_open; |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 287 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 288 | /* reset the newargv */ |
| 289 | newargc = 0; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 290 | |
| 291 | if (buffer[0] == '[') { |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 292 | /* we have counters in our input */ |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 293 | ptr = strchr(buffer, ']'); |
| 294 | if (!ptr) |
| 295 | exit_error(PARAMETER_PROBLEM, |
| 296 | "Bad line %u: need ]\n", |
| 297 | line); |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 298 | |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 299 | pcnt = strtok(buffer+1, ":"); |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 300 | if (!pcnt) |
| 301 | exit_error(PARAMETER_PROBLEM, |
| 302 | "Bad line %u: need :\n", |
| 303 | line); |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 304 | |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 305 | bcnt = strtok(NULL, "]"); |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 306 | if (!bcnt) |
| 307 | exit_error(PARAMETER_PROBLEM, |
| 308 | "Bad line %u: need ]\n", |
| 309 | line); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 310 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 311 | /* start command parsing after counter */ |
| 312 | parsestart = ptr + 1; |
| 313 | } else { |
| 314 | /* start command parsing at start of line */ |
| 315 | parsestart = buffer; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 316 | } |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 317 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 318 | add_argv(argv[0]); |
| 319 | add_argv("-t"); |
| 320 | add_argv((char *) &curtable); |
| 321 | |
| 322 | if (counters && pcnt && bcnt) { |
| 323 | add_argv("--set-counters"); |
| 324 | add_argv((char *) pcnt); |
| 325 | add_argv((char *) bcnt); |
| 326 | } |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 327 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 328 | /* After fighting with strtok enough, here's now |
| 329 | * a 'real' parser. According to Rusty I'm now no |
| 330 | * longer a real hacker, but I can live with that */ |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 331 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 332 | quote_open = 0; |
| 333 | param_start = parsestart; |
| 334 | |
| 335 | for (curchar = parsestart; *curchar; curchar++) { |
| 336 | if (*curchar == '"') { |
Michael Rash | 29cdbd0 | 2004-01-05 09:41:50 +0000 | [diff] [blame] | 337 | /* quote_open cannot be true if there |
| 338 | * was no previous character. Thus, |
| 339 | * curchar-1 has to be within bounds */ |
| 340 | if (quote_open && |
| 341 | *(curchar-1) != '\\') { |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 342 | quote_open = 0; |
| 343 | *curchar = ' '; |
| 344 | } else { |
| 345 | quote_open = 1; |
| 346 | param_start++; |
| 347 | } |
| 348 | } |
| 349 | if (*curchar == ' ' |
| 350 | || *curchar == '\t' |
| 351 | || * curchar == '\n') { |
| 352 | char param_buffer[1024]; |
| 353 | int param_len = curchar-param_start; |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 354 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 355 | if (quote_open) |
| 356 | continue; |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 357 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 358 | if (!param_len) { |
| 359 | /* two spaces? */ |
| 360 | param_start++; |
| 361 | continue; |
| 362 | } |
| 363 | |
| 364 | /* end of one parameter */ |
| 365 | strncpy(param_buffer, param_start, |
| 366 | param_len); |
| 367 | *(param_buffer+param_len) = '\0'; |
Harald Welte | bb41f88 | 2001-10-21 14:11:54 +0000 | [diff] [blame] | 368 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 369 | /* check if table name specified */ |
Harald Welte | 48a6f90 | 2001-10-22 15:16:21 +0000 | [diff] [blame] | 370 | if (!strncmp(param_buffer, "-t", 3) |
| 371 | || !strncmp(param_buffer, "--table", 8)) { |
Harald Welte | bb41f88 | 2001-10-21 14:11:54 +0000 | [diff] [blame] | 372 | exit_error(PARAMETER_PROBLEM, |
| 373 | "Line %u seems to have a " |
| 374 | "-t table option.\n", line); |
| 375 | exit(1); |
| 376 | } |
| 377 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 378 | add_argv(param_buffer); |
| 379 | param_start += param_len + 1; |
| 380 | } else { |
| 381 | /* regular character, skip */ |
| 382 | } |
| 383 | } |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 384 | |
| 385 | DEBUGP("calling do_command6(%u, argv, &%s, handle):\n", |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 386 | newargc, curtable); |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 387 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 388 | for (a = 0; a < newargc; a++) |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 389 | DEBUGP("argv[%u]: %s\n", a, newargv[a]); |
| 390 | |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 391 | ret = do_command6(newargc, newargv, |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 392 | &newargv[2], &handle); |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 393 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 394 | free_argv(); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 395 | } |
| 396 | if (!ret) { |
| 397 | fprintf(stderr, "%s: line %u failed\n", |
| 398 | program_name, line); |
| 399 | exit(1); |
| 400 | } |
| 401 | } |
Martin Josefsson | e0dc581 | 2004-02-02 19:58:36 +0000 | [diff] [blame] | 402 | if (in_table) { |
| 403 | fprintf(stderr, "%s: COMMIT expected at line %u\n", |
| 404 | program_name, line + 1); |
| 405 | exit(1); |
| 406 | } |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 407 | |
| 408 | return 0; |
| 409 | } |