/* * Copyright (C) 2009-2016 Olof Hagsand and Benny Holmgren This file is part of CLIXON. CLIXON is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. CLIXON is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with CLIXON; see the file LICENSE. If not, see . */ #ifdef HAVE_CONFIG_H #include "clixon_config.h" /* generated by config & autoconf */ #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* cligen */ #include /* clicon */ #include /* Command line options to be passed to getopt(3) */ #define DBCTRL_OPTS "hDd:pbn:r:m:Zi" /* * remove_entry */ static int remove_entry(char *dbname, char *key) { #ifdef NOTYET /* This assumes direct access to database */ return db_del(dbname, key); #else return 0; #endif } /* * usage */ static void usage(char *argv0) { fprintf(stderr, "usage:%s\n" "where options are\n" "\t-h\t\tHelp\n" "\t-D\t\tDebug\n" "\t-d \tDatabase name (default: running_db)\n" "\t-p\t\tDump database on stdout\n" "\t-b\t\tBrief output, just print keys. Combine with -p or -m\n" "\t-n \" \" Add database entry\n" "\t-r \tRemove database entry\n" "\t-m \tMatch regexp key in database\n" "\t-Z\t\tDelete database\n" "\t-i\t\tInit database\n", argv0 ); exit(0); } int main(int argc, char **argv) { char c; int zapdb; int initdb; int dumpdb; int addent; int rment; char *matchkey = NULL; char *addstr; char rmkey[MAXPATHLEN]; int brief; char dbname[MAXPATHLEN] = {0,}; int use_syslog; yang_spec *yspec; clicon_handle h; /* In the startup, logs to stderr & debug flag set later */ clicon_log_init(__PROGRAM__, LOG_INFO, CLICON_LOG_STDERR); /* Defaults */ zapdb = 0; initdb = 0; dumpdb = 0; addent = 0; rment = 0; brief = 0; use_syslog = 0; addstr = NULL; memset(rmkey, '\0', sizeof(rmkey)); if ((h = clicon_handle_init()) == NULL) goto done; /* getopt in two steps, first find config-file before over-riding options. */ while ((c = getopt(argc, argv, DBCTRL_OPTS)) != -1) switch (c) { case '?' : case 'h' : /* help */ usage(argv[0]); break; case 'D' : /* debug */ debug = 1; break; case 'S': /* Log on syslog */ use_syslog = 1; break; } /* * Logs, error and debug to stderr or syslog, set debug level */ clicon_log_init(__PROGRAM__, debug?LOG_DEBUG:LOG_INFO, use_syslog?CLICON_LOG_SYSLOG:CLICON_LOG_STDERR); clicon_debug_init(debug, NULL); /* Now rest of options */ optind = 1; while ((c = getopt(argc, argv, DBCTRL_OPTS)) != -1) switch (c) { case 'Z': /* Zap database */ zapdb++; break; case 'i': /* Init database */ initdb++; break; case 'p': /* Dump/print database */ dumpdb++; break; case 'b': /* Dump/print/match database brief (combone w -p or -m) */ brief++; break; case 'd': /* dbname */ if (!optarg || sscanf(optarg, "%s", dbname) != 1) usage(argv[0]); break; case 'n': /* add database entry */ if (!optarg || !strlen(optarg) || (addstr = strdup(optarg)) == NULL) usage(argv[0]); /* XXX addign both key and value, for now only key */ addent++; break; case 'r': if (!optarg || sscanf(optarg, "%s", rmkey) != 1) usage(argv[0]); rment++; break; case 'm': if (!optarg || !strlen(optarg) || (matchkey = strdup(optarg)) == NULL) usage(argv[0]); dumpdb++; break; case 'D': /* Processed earlier, ignore now. */ case 'S': break; default: usage(argv[0]); break; } argc -= optind; argv += optind; if (*dbname == '\0'){ clicon_err(OE_FATAL, 0, "database not specified (with -d ): %s"); goto done; } yspec = clicon_dbspec_yang(h); if (dumpdb){ if (xmldb_dump(stdout, dbname, matchkey)) { fprintf(stderr, "Match error\n"); goto done; } } if (addent) /* add entry */ if (xmldb_put_xkey(dbname, addstr, NULL, yspec, OP_REPLACE) < 0) goto done; if (rment) if (remove_entry(dbname, rmkey) < 0) goto done; if (zapdb) /* remove databases */ /* XXX This assumes direct access to database */ if (unlink(dbname) < 0){ clicon_err(OE_FATAL, errno, "unlink %s", dbname); goto done; } if (initdb) if (xmldb_init(dbname) < 0) goto done; done: return 0; }