edit_config.c

Go to the documentation of this file.
00001 /* edit_config.c  --  eurephiadm command:
00002  *                    Sets, deletes or prints a config setting in the database
00003  *
00004  *  GPLv2 only - Copyright (C) 2008 - 2010
00005  *               David Sommerseth <dazo@users.sourceforge.net>
00006  *
00007  *  This program is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU General Public License
00009  *  as published by the Free Software Foundation; version 2
00010  *  of the License.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020  *
00021  */
00022 
00033 #include <stdio.h>
00034 #include <string.h>
00035 #include <assert.h>
00036 
00037 #include <libxml/tree.h>
00038 
00039 #include <eurephia_nullsafe.h>
00040 #include <eurephia_context.h>
00041 #include <eurephia_log.h>
00042 #include <eurephia_xml.h>
00043 #include <eurephia_values_struct.h>
00044 #include <eurephia_values.h>
00045 #include <eurephiadb_session_struct.h>
00046 #include <eurephiadb_driver.h>
00047 
00048 #define MODULE "eurephiadm::config" 
00049 #include "../argparser.h"
00050 
00055 void help_EditConfig() {
00056         printf("%s - Edit eurephia configuration\n\n", MODULE);
00057         printf("  The config command let you add, change or delete\n"
00058                "  configuration parameters in the eurephia database\n"
00059                "  in an easy way.  To be allowed to do so, you must\n"
00060                "  have been granted 'config' access.\n\n");
00061 
00062         printf("  The following arguments are accepted:\n"
00063                "        -l | --list                   List all parameters in db\n"
00064                "        -s | --set <key> <value>      Add/change a parameter.\n"
00065                "        -D | --delete <key>           Remove the parameter.\n"
00066                "\n"
00067                "  If no arguments is given, you can give a key name, and the\n"
00068                "  associated value will be printed.\n\n"
00069                "        eurephiadm config <key>\n\n");
00070 
00071         printf("  The command will exit with exit code 0 on success.\n\n");
00072 }
00073 
00077 int cmd_ShowCfg(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv);
00078 
00090 int cmd_EditConfig(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
00091         int rc = 0, i = 0;
00092         xmlDoc *cfgxml = NULL, *resxml = NULL;
00093         xmlNode *cfg_n = NULL;
00094 
00095         e_options editargs[] = {
00096                 {"--set", "-s", 2},
00097                 {"--delete", "-D", 1},
00098                 {"--list", "-l", 0},
00099                 {"--help", "-h", 0},
00100                 {NULL, NULL, 0}
00101         };
00102 
00103         assert((ctx != NULL) && (ctx->dbc != NULL) && (ctx->dbc->config != NULL));
00104 
00105         for( i = 1; i < argc; i++ ) {
00106                 if( *argv[i] != '-' ) {
00107                         break;
00108                 }
00109                 switch( eurephia_getopt(&i, argc, argv, editargs) ) {
00110                 case 's':
00111                         eurephiaXML_CreateDoc(ctx, 1, "configuration", &cfgxml, &cfg_n);
00112                         cfg_n = xmlNewChild(cfg_n, NULL, (xmlChar *) "set", (xmlChar *) optargs[1]);
00113                         xmlNewProp(cfg_n, (xmlChar *) "key", (xmlChar *) optargs[0]);
00114                         resxml = eDBadminConfiguration(ctx, cfgxml);
00115                         xmlFreeDoc(cfgxml);
00116                         break;
00117 
00118                 case 'D':
00119                         eurephiaXML_CreateDoc(ctx, 1, "configuration", &cfgxml, &cfg_n);
00120                         cfg_n = xmlNewChild(cfg_n, NULL, (xmlChar *) "delete", NULL);
00121                         xmlNewProp(cfg_n, (xmlChar *) "key", (xmlChar *) optargs[0]);
00122                         resxml = eDBadminConfiguration(ctx, cfgxml);
00123                         xmlFreeDoc(cfgxml);
00124                         break;
00125 
00126                 case 'l':
00127                         cmd_ShowCfg(ctx, NULL, NULL, 0, NULL);
00128                         return 0;
00129 
00130                 case 'h':
00131                         help_EditConfig();
00132                         return 0;
00133 
00134                 default:
00135                         return 1;
00136                 }
00137         }
00138 
00139         if( argc == 2 ) {
00140                 char *val = eGet_value(ctx->dbc->config, argv[1]);
00141                 fprintf(stdout, "%s\n", (val != NULL ? val : ""));
00142                 rc = 1;
00143         } else if( argc == 1 ){
00144                 fprintf(stderr, "No config action performed\n");
00145                 rc = 1;
00146         } else if( resxml != NULL ) {
00147                 eurephiaRESULT *res = NULL;
00148 
00149                 res = eurephiaXML_ParseResultMsg(ctx, resxml);
00150                 if( res == NULL ) {
00151                         fprintf(stderr, "%s: Error updating the configuration. No results returned.\n",
00152                                 MODULE);
00153                         return rc = 1;
00154                 }
00155 
00156                 if( res->resultType == exmlERROR ) {
00157                         fprintf(stderr, "%s: %s\n", MODULE, res->message);
00158                         rc = 1;
00159                 } else {
00160                         fprintf(stdout, "%s: %s\n", MODULE, res->message);
00161                         rc = 0;
00162                 }
00163                 xmlFreeDoc(resxml);
00164                 free_nullsafe(ctx, res);
00165         }  else {
00166                 fprintf(stderr, "Unexpected error while performing configuration changes\n");
00167                 rc = 1;
00168         }
00169         return (rc != 1);
00170 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines