LCOV - code coverage report
Current view: top level - clients/mapiclient - dotmonetdb.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 29 59 49.2 %
Date: 2021-10-13 02:24:04 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : #include "monetdb_config.h"
      10             : #include "dotmonetdb.h"
      11             : #define LIBMUTILS 1
      12             : #include "mutils.h"
      13             : #include <string.h>
      14             : 
      15             : void
      16         215 : parse_dotmonetdb(DotMonetdb *dotfile)
      17             : {
      18             :         char *cfile;
      19             :         FILE *config = NULL;
      20             :         char buf[FILENAME_MAX];
      21             : 
      22         215 :         if (dotfile == NULL)
      23           0 :                 return;
      24             :         /* if environment variable DOTMONETDBFILE is set, use it */
      25             :         /* 1. use $DOTMONETDBFILE (if set but empty do not read config file);
      26             :          * 2. use .monetdb;
      27             :          * 3. use ${XDG_CONFIG_HOME-$HOME/.config}/monetdb;
      28             :          * 4. use $HOME/.monetdb
      29             :          * (3 is standard shell syntax: use XDG_CONFIG_HOME if set, else use
      30             :          * $HOME/.config in its place)
      31             :          */
      32         215 :         if ((cfile = getenv("DOTMONETDBFILE")) == NULL) {
      33             :                 /* no environment variable: use a default */
      34             :                 cfile = ".monetdb";
      35           2 :                 if ((config = MT_fopen(cfile, "r")) == NULL) {
      36           2 :                         const char *xdg = getenv("XDG_CONFIG_HOME");
      37           2 :                         const char *home = getenv("HOME");
      38             :                         int len = -1;
      39             :                         cfile = buf;
      40           2 :                         if (xdg != NULL)
      41           0 :                                 len = snprintf(buf, sizeof(buf), "%s%cmonetdb", xdg, DIR_SEP);
      42           2 :                         else if (home != NULL)
      43           2 :                                 len = snprintf(buf, sizeof(buf), "%s%c.config%cmonetdb", home, DIR_SEP, DIR_SEP);
      44           4 :                         if (len == -1 || len >= FILENAME_MAX || (config = MT_fopen(buf, "r")) == NULL) {
      45           2 :                                 if (home) {
      46           2 :                                         len = snprintf(buf, sizeof(buf), "%s%c.monetdb", home, DIR_SEP);
      47           2 :                                         if (len >= 0 && len < FILENAME_MAX)
      48             :                                                 config = MT_fopen(buf, "r");
      49             :                                 }
      50             :                         }
      51             :                 }
      52         426 :         } else if (*cfile != 0 && (config = MT_fopen(cfile, "r")) == NULL) {
      53           0 :                 fprintf(stderr, "failed to open file '%s': %s\n",
      54           0 :                         cfile, strerror(errno));
      55             :         }
      56             : 
      57         215 :         *dotfile = (DotMonetdb) {0};
      58             : 
      59         215 :         if (config) {
      60             :                 int line = 0;
      61             :                 char *q;
      62         639 :                 while (fgets(buf, sizeof(buf), config) != NULL) {
      63         426 :                         line++;
      64         426 :                         q = strchr(buf, '\n');
      65         426 :                         if (q)
      66         426 :                                 *q = 0;
      67         426 :                         if (buf[0] == '\0' || buf[0] == '#')
      68           0 :                                 continue;
      69         426 :                         if ((q = strchr(buf, '=')) == NULL) {
      70           0 :                                 fprintf(stderr, "%s:%d: syntax error: %s\n",
      71             :                                         cfile, line, buf);
      72           0 :                                 continue;
      73             :                         }
      74         426 :                         *q++ = '\0';
      75             :                         /* this basically sucks big time, as I can't easily set
      76             :                          * a default, hence I only do things I think are useful
      77             :                          * for now, needs a better solution */
      78         426 :                         if (strcmp(buf, "user") == 0) {
      79         213 :                                 dotfile->user = strdup(q);
      80             :                                 q = NULL;
      81         213 :                         } else if (strcmp(buf, "password") == 0) {
      82         213 :                                 dotfile->passwd = strdup(q);
      83             :                                 q = NULL;
      84           0 :                         } else if (strcmp(buf, "database") == 0) {
      85           0 :                                 dotfile->dbname = strdup(q);
      86             :                                 q = NULL;
      87           0 :                         } else if (strcmp(buf, "host") == 0) {
      88           0 :                                 dotfile->host = strdup(q);
      89             :                                 q = NULL;
      90           0 :                         } else if (strcmp(buf, "language") == 0) {
      91             :                                 /* make sure we don't set garbage */
      92           0 :                                 if (strcmp(q, "sql") != 0 &&
      93           0 :                                     strcmp(q, "mal") != 0) {
      94           0 :                                         fprintf(stderr, "%s:%d: unsupported "
      95             :                                                 "language: %s\n",
      96             :                                                 cfile, line, q);
      97             :                                 }
      98           0 :                                 dotfile->language = strdup(q);
      99             :                                 q = NULL;
     100           0 :                         } else if (strcmp(buf, "save_history") == 0) {
     101           0 :                                 if (strcmp(q, "true") == 0 ||
     102           0 :                                     strcmp(q, "on") == 0) {
     103           0 :                                         dotfile->save_history = true;
     104             :                                         q = NULL;
     105           0 :                                 } else if (strcmp(q, "false") == 0 ||
     106           0 :                                            strcmp(q, "off") == 0) {
     107           0 :                                         dotfile->save_history = false;
     108             :                                         q = NULL;
     109             :                                 }
     110           0 :                         } else if (strcmp(buf, "format") == 0) {
     111           0 :                                 dotfile->output = strdup(q);
     112             :                                 q = NULL;
     113           0 :                         } else if (strcmp(buf, "width") == 0) {
     114           0 :                                 dotfile->pagewidth = atoi(q);
     115             :                                 q = NULL;
     116           0 :                         } else if (strcmp(buf, "port") == 0) {
     117           0 :                                 dotfile->port = atoi(q);
     118             :                                 q = NULL;
     119             :                         }
     120             :                         if (q != NULL)
     121           0 :                                 fprintf(stderr, "%s:%d: unknown property: %s\n",
     122             :                                         cfile, line, buf);
     123             :                 }
     124         213 :                 fclose(config);
     125             :         }
     126             : }

Generated by: LCOV version 1.14