Changeset: aaf30448d53a for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=aaf30448d53a
Added Files:
        clients/mapiclient/dotmonetdb.c
        clients/mapiclient/dotmonetdb.h
Modified Files:
        clients/mapiclient/Makefile.ag
        clients/mapiclient/mclient.c
        clients/mapiclient/stethoscope.c
        clients/mapiclient/tomograph.c
Branch: default
Log Message:

Move parsing of .monetdb file to separate function: code reuse.


diffs (truncated from 558 to 300 lines):

diff --git a/clients/mapiclient/Makefile.ag b/clients/mapiclient/Makefile.ag
--- a/clients/mapiclient/Makefile.ag
+++ b/clients/mapiclient/Makefile.ag
@@ -20,7 +20,7 @@ INCLUDES = ../mapilib ../../common/optio
 
 lib_mcutil = {
        NOINST
-       SOURCES = dump.c prompt.c
+       SOURCES = dump.c prompt.c dotmonetdb.c
 }
 
 bin_mclient = {
@@ -61,6 +61,6 @@ bin_mnc = {
 
 man_MANS = mclient.1 msqldump.1
 
-EXTRA_DIST = msqldump.h mprompt.h $(man_MANS)
+EXTRA_DIST = msqldump.h mprompt.h dotmonetdb.h $(man_MANS)
 
 EXTRA_DIST_DIR = Tests
diff --git a/clients/mapiclient/dotmonetdb.c b/clients/mapiclient/dotmonetdb.c
new file mode 100644
--- /dev/null
+++ b/clients/mapiclient/dotmonetdb.c
@@ -0,0 +1,139 @@
+/*
+ * The contents of this file are subject to the MonetDB Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.monetdb.org/Legal/MonetDBLicense
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * The Original Code is the MonetDB Database System.
+ *
+ * The Initial Developer of the Original Code is CWI.
+ * Portions created by CWI are Copyright (C) 1997-July 2008 CWI.
+ * Copyright August 2008-2012 MonetDB B.V.
+ * All Rights Reserved.
+ */
+
+#include "monetdb_config.h"
+#include "dotmonetdb.h"
+#include <stdio.h>
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_STRINGS_H
+#include <strings.h>
+#endif
+
+void
+parse_dotmonetdb(char **user, char **passwd, char **language, int 
*save_history, char **output, int *pagewidth)
+{
+       char *cfile;
+       FILE *config;
+       char buf[1024];
+
+       if ((cfile = getenv("DOTMONETDBFILE")) == NULL) {
+               if ((config = fopen(".monetdb", "r")) == NULL) {
+                       if ((cfile = getenv("HOME")) != NULL) {
+                               snprintf(buf, sizeof(buf), "%s%c.monetdb", 
cfile, DIR_SEP);
+                               config = fopen(buf, "r");
+                               if (config)
+                                       cfile = strdup(buf);
+                               else
+                                       cfile = NULL;
+                       }
+               } else {
+                       cfile = strdup(".monetdb");
+               }
+       } else if (*cfile != 0 && (config = fopen(cfile, "r")) == NULL) {
+               cfile = NULL;
+               fprintf(stderr, "failed to open file '%s': %s\n",
+                       cfile, strerror(errno));
+       } else {
+               cfile = strdup(cfile);
+       }
+
+       if (user)
+               *user = NULL;
+       if (passwd)
+               *passwd = NULL;
+       if (language)
+               *language = NULL;
+       if (output)
+               *output = NULL;
+       if (save_history)
+               *save_history = 0;
+       if (pagewidth)
+               *pagewidth = -1;
+
+       if (config) {
+               int line = 0;
+               char *q;
+               while (fgets(buf, sizeof(buf), config) != NULL) {
+                       line++;
+                       q = strchr(buf, '\n');
+                       if (q)
+                               *q = 0;
+                       if (buf[0] == '\0' || buf[0] == '#')
+                               continue;
+                       if ((q = strchr(buf, '=')) == NULL) {
+                               fprintf(stderr, "%s:%d: syntax error: %s\n",
+                                       cfile, line, buf);
+                               continue;
+                       }
+                       *q++ = '\0';
+                       /* this basically sucks big time, as I can't easily set
+                        * a default, hence I only do things I think are useful
+                        * for now, needs a better solution */
+                       if (strcmp(buf, "user") == 0) {
+                               if (user)
+                                       *user = strdup(q);
+                               q = NULL;
+                       } else if (strcmp(buf, "password") == 0 || strcmp(buf, 
"passwd") == 0) {
+                               if (passwd)
+                                       *passwd = strdup(q);
+                               q = NULL;
+                       } else if (strcmp(buf, "language") == 0) {
+                               /* make sure we don't set garbage */
+                               if (strcmp(q, "sql") != 0 &&
+                                   strcmp(q, "mal") != 0 &&
+                                   strcmp(q, "jaql") != 0) {
+                                       fprintf(stderr, "%s:%d: unsupported "
+                                               "language: %s\n",
+                                               cfile, line, q);
+                               } else if (language)
+                                       *language = strdup(q);
+                               q = NULL;
+                       } else if (strcmp(buf, "save_history") == 0) {
+                               if (strcmp(q, "true") == 0 ||
+                                   strcmp(q, "on") == 0) {
+                                       if (save_history)
+                                               *save_history = 1;
+                                       q = NULL;
+                               } else if (strcmp(q, "false") == 0 ||
+                                          strcmp(q, "off") == 0) {
+                                       if (save_history)
+                                               *save_history = 0;
+                                       q = NULL;
+                               }
+                       } else if (strcmp(buf, "format") == 0) {
+                               if (output)
+                                       *output = strdup(q);
+                               q = NULL;
+                       } else if (strcmp(buf, "width") == 0) {
+                               if (pagewidth)
+                                       *pagewidth = atoi(q);
+                               q = NULL;
+                       }
+                       if (q != NULL)
+                               fprintf(stderr, "%s:%d: unknown property: %s\n",
+                                       cfile, line, buf);
+               }
+       }
+       if (cfile)
+               free(cfile);
+       if (config)
+               fclose(config);
+}
diff --git a/clients/mapiclient/dotmonetdb.h b/clients/mapiclient/dotmonetdb.h
new file mode 100644
--- /dev/null
+++ b/clients/mapiclient/dotmonetdb.h
@@ -0,0 +1,21 @@
+/*
+ * The contents of this file are subject to the MonetDB Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.monetdb.org/Legal/MonetDBLicense
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * The Original Code is the MonetDB Database System.
+ *
+ * The Initial Developer of the Original Code is CWI.
+ * Portions created by CWI are Copyright (C) 1997-July 2008 CWI.
+ * Copyright August 2008-2012 MonetDB B.V.
+ * All Rights Reserved.
+ */
+
+extern void parse_dotmonetdb(char **user, char **passwd, char **language,
+                            int *save_history, char **output, int *pagewidth);
diff --git a/clients/mapiclient/mclient.c b/clients/mapiclient/mclient.c
--- a/clients/mapiclient/mclient.c
+++ b/clients/mapiclient/mclient.c
@@ -56,6 +56,7 @@
 #include "stream.h"
 #include "msqldump.h"
 #include "mprompt.h"
+#include "dotmonetdb.h"
 #ifdef HAVE_LOCALE_H
 #include <locale.h>
 #endif
@@ -2649,7 +2650,6 @@ main(int argc, char **argv)
        int settz = 1;
        int autocommit = 1;     /* autocommit mode default on */
        struct stat statb;
-       stream *config = NULL;
        char user_set_as_flag = 0;
        static struct option long_options[] = {
                {"autocommit", 0, 0, 'a'},
@@ -2704,99 +2704,16 @@ main(int argc, char **argv)
 #endif
 
        /* parse config file first, command line options override */
-       if (getenv("DOTMONETDBFILE") == NULL) {
-               if (stat(".monetdb", &statb) == 0) {
-                       config = open_rastream(".monetdb");
-               } else if (getenv("HOME") != NULL) {
-                       char buf[1024];
-                       snprintf(buf, sizeof(buf), "%s/.monetdb", 
getenv("HOME"));
-                       if (stat(buf, &statb) == 0) {
-                               config = open_rastream(buf);
-                       }
+       parse_dotmonetdb(&user, &passwd, &language, &save_history, &output, 
&pagewidth);
+       pagewidthset = pagewidth != 0;
+       if (language) {
+               if (strcmp(language, "sql") == 0) {
+                       mode = SQL;
+               } else if (strcmp(language, "mal") == 0) {
+                       mode = MAL;
+               } else if (strcmp(language, "jaql") == 0) {
+                       mode = JAQL;
                }
-       } else {
-               char *cfile = getenv("DOTMONETDBFILE");
-               if (strcmp(cfile, "") != 0) {
-                       if (stat(cfile, &statb) == 0) {
-                               config = open_rastream(cfile);
-                       } else {
-                               mnstr_printf(stderr_stream,
-                                             "failed to open file '%s': %s\n",
-                                             cfile, strerror(errno));
-                       }
-               }
-       }
-
-       if (config != NULL) {
-               char buf[1024];
-               char *q;
-               ssize_t len;
-               int line = 0;
-               while ((len = mnstr_readline(config, buf, sizeof(buf) - 1)) > 
0) {
-                       line++;
-                       buf[len - 1] = '\0';    /* drop newline */
-                       if (buf[0] == '#' || buf[0] == '\0')
-                               continue;
-                       if ((q = strchr(buf, '=')) == NULL) {
-                               mnstr_printf(stderr_stream, "%s:%d: syntax 
error: %s\n", mnstr_name(config), line, buf);
-                               continue;
-                       }
-                       *q++ = '\0';
-                       /* this basically sucks big time, as I can't easily set
-                        * a default, hence I only do things I think are useful
-                        * for now, needs a better solution */
-                       if (strcmp(buf, "user") == 0) {
-                               user = strdup(q);       /* leak */
-                               q = NULL;
-                       } else if (strcmp(buf, "password") == 0 || strcmp(buf, 
"passwd") == 0) {
-                               passwd = strdup(q);     /* leak */
-                               q = NULL;
-                       } else if (strcmp(buf, "language") == 0) {
-                               language = strdup(q);   /* leak */
-                               if (strcmp(language, "sql") == 0) {
-                                       mode = SQL;
-                                       q = NULL;
-                               } else if (strcmp(language, "mal") == 0) {
-                                       mode = MAL;
-                                       q = NULL;
-                               } else if (strcmp(language, "jaql") == 0) {
-                                       mode = JAQL;
-                                       q = NULL;
-                               } else {
-                                       /* make sure we don't set garbage */
-                                       mnstr_printf(stderr_stream,
-                                                     "%s:%d: unsupported "
-                                                     "language: %s\n",
-                                                     mnstr_name(config),
-                                                     line, q);
-                                       free(language);
-                                       language = NULL;
-                                       q = NULL;
-                               }
-                       } else if (strcmp(buf, "save_history") == 0) {
-                               if (strcmp(q, "true") == 0 ||
-                                   strcmp(q, "on") == 0)
-                               {
-                                       save_history = 1;
-                                       q = NULL;
-                               } else if (strcmp(q, "false") == 0 ||
-                                          strcmp(q, "off") == 0)
-                               {
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to