| 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 - 2019 MonetDB B.V. |
| 7 | */ |
| 8 | |
| 9 | #include "monetdb_config.h" |
| 10 | #include "dotmonetdb.h" |
| 11 | #include <string.h> |
| 12 | |
| 13 | void |
| 14 | parse_dotmonetdb(char **user, char **passwd, char **dbname, char **language, int *save_history, char **output, int *pagewidth) |
| 15 | { |
| 16 | char *cfile; |
| 17 | FILE *config = NULL; |
| 18 | char buf[FILENAME_MAX]; |
| 19 | |
| 20 | if ((cfile = getenv("DOTMONETDBFILE" )) == NULL) { |
| 21 | /* no environment variable: use a default */ |
| 22 | if ((config = fopen(".monetdb" , "r" )) == NULL) { |
| 23 | if ((cfile = getenv("HOME" )) != NULL) { |
| 24 | int len = snprintf(buf, sizeof(buf), "%s%c.monetdb" , cfile, DIR_SEP); |
| 25 | if (len == -1 || len >= FILENAME_MAX) { |
| 26 | cfile = NULL; |
| 27 | } else { |
| 28 | config = fopen(buf, "r" ); |
| 29 | if (config) |
| 30 | cfile = strdup(buf); |
| 31 | else |
| 32 | cfile = NULL; |
| 33 | } |
| 34 | } |
| 35 | } else { |
| 36 | cfile = strdup(".monetdb" ); |
| 37 | } |
| 38 | } else if (*cfile == 0) { |
| 39 | /* empty environment variable: skip the file */ |
| 40 | cfile = NULL; |
| 41 | } else if ((config = fopen(cfile, "r" )) == NULL) { |
| 42 | fprintf(stderr, "failed to open file '%s': %s\n" , |
| 43 | cfile, strerror(errno)); |
| 44 | cfile = NULL; |
| 45 | } else { |
| 46 | cfile = strdup(cfile); |
| 47 | } |
| 48 | |
| 49 | if (user) |
| 50 | *user = NULL; |
| 51 | if (passwd) |
| 52 | *passwd = NULL; |
| 53 | if (dbname) |
| 54 | *dbname = NULL; |
| 55 | if (language) |
| 56 | *language = NULL; |
| 57 | if (output) |
| 58 | *output = NULL; |
| 59 | if (save_history) |
| 60 | *save_history = 0; |
| 61 | if (pagewidth) |
| 62 | *pagewidth = 0; |
| 63 | |
| 64 | if (config) { |
| 65 | int line = 0; |
| 66 | char *q; |
| 67 | while (fgets(buf, sizeof(buf), config) != NULL) { |
| 68 | line++; |
| 69 | q = strchr(buf, '\n'); |
| 70 | if (q) |
| 71 | *q = 0; |
| 72 | if (buf[0] == '\0' || buf[0] == '#') |
| 73 | continue; |
| 74 | if ((q = strchr(buf, '=')) == NULL) { |
| 75 | fprintf(stderr, "%s:%d: syntax error: %s\n" , |
| 76 | cfile, line, buf); |
| 77 | continue; |
| 78 | } |
| 79 | *q++ = '\0'; |
| 80 | /* this basically sucks big time, as I can't easily set |
| 81 | * a default, hence I only do things I think are useful |
| 82 | * for now, needs a better solution */ |
| 83 | if (strcmp(buf, "user" ) == 0) { |
| 84 | if (user) |
| 85 | *user = strdup(q); |
| 86 | q = NULL; |
| 87 | } else if (strcmp(buf, "password" ) == 0) { |
| 88 | if (passwd) |
| 89 | *passwd = strdup(q); |
| 90 | q = NULL; |
| 91 | } else if (strcmp(buf, "database" ) == 0) { |
| 92 | if (dbname) |
| 93 | *dbname = strdup(q); |
| 94 | q = NULL; |
| 95 | } else if (strcmp(buf, "language" ) == 0) { |
| 96 | /* make sure we don't set garbage */ |
| 97 | if (strcmp(q, "sql" ) != 0 && |
| 98 | strcmp(q, "mal" ) != 0) { |
| 99 | fprintf(stderr, "%s:%d: unsupported " |
| 100 | "language: %s\n" , |
| 101 | cfile, line, q); |
| 102 | } else if (language) |
| 103 | *language = strdup(q); |
| 104 | q = NULL; |
| 105 | } else if (strcmp(buf, "save_history" ) == 0) { |
| 106 | if (strcmp(q, "true" ) == 0 || |
| 107 | strcmp(q, "on" ) == 0) { |
| 108 | if (save_history) |
| 109 | *save_history = 1; |
| 110 | q = NULL; |
| 111 | } else if (strcmp(q, "false" ) == 0 || |
| 112 | strcmp(q, "off" ) == 0) { |
| 113 | if (save_history) |
| 114 | *save_history = 0; |
| 115 | q = NULL; |
| 116 | } |
| 117 | } else if (strcmp(buf, "format" ) == 0) { |
| 118 | if (output) |
| 119 | *output = strdup(q); |
| 120 | q = NULL; |
| 121 | } else if (strcmp(buf, "width" ) == 0) { |
| 122 | if (pagewidth) |
| 123 | *pagewidth = atoi(q); |
| 124 | q = NULL; |
| 125 | } |
| 126 | if (q != NULL) |
| 127 | fprintf(stderr, "%s:%d: unknown property: %s\n" , |
| 128 | cfile, line, buf); |
| 129 | } |
| 130 | } |
| 131 | if (cfile) |
| 132 | free(cfile); |
| 133 | if (config) |
| 134 | fclose(config); |
| 135 | } |
| 136 | |