|
1 |
| -/* houserelays - A simple home web server for world domination through relays |
2 |
| - * |
3 |
| - * Copyright 2020, Pascal Martin |
4 |
| - * |
5 |
| - * This program is free software; you can redistribute it and/or |
6 |
| - * modify it under the terms of the GNU General Public License |
7 |
| - * as published by the Free Software Foundation; either version 2 |
8 |
| - * of the License, or (at your option) any later version. |
9 |
| - * |
10 |
| - * This program is distributed in the hope that it will be useful, |
11 |
| - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
| - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
| - * GNU General Public License for more details. |
14 |
| - * |
15 |
| - * You should have received a copy of the GNU General Public License |
16 |
| - * along with this program; if not, write to the Free Software |
17 |
| - * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 |
| - * Boston, MA 02110-1301, USA. |
19 |
| - * |
20 |
| - * |
21 |
| - * houserelays_config.c - Access the relays config. |
22 |
| - * |
23 |
| - * SYNOPSYS: |
24 |
| - * |
25 |
| - * const char *houserelays_config_load (int argc, const char **argv); |
26 |
| - * |
27 |
| - * Load the configuration from the specified config option, or else |
28 |
| - * from the default config file. |
29 |
| - * |
30 |
| - * int houserelays_config_file (void); |
31 |
| - * int houserelays_config_size (void); |
32 |
| - * |
33 |
| - * Return a file descriptor (and the size) of the configuration file |
34 |
| - * being used. |
35 |
| - * |
36 |
| - * const char *houserelays_config_update (const char *text); |
37 |
| - * |
38 |
| - * Update both the live configuration and the configuration file with |
39 |
| - * the provided text. |
40 |
| - * |
41 |
| - * const char *houserelays_config_string (int parent, const char *path); |
42 |
| - * int houserelays_config_integer (int parent, const char *path); |
43 |
| - * double houserelays_config_boolean (int parent, const char *path); |
44 |
| - * |
45 |
| - * Access individual items starting from the specified parent |
46 |
| - * (the config root is index 0). |
47 |
| - * |
48 |
| - * int houserelays_config_array (int parent, const char *path); |
49 |
| - * int houserelays_config_array_length (int array); |
50 |
| - * |
51 |
| - * Retrieve an array. |
52 |
| - * |
53 |
| - * int houserelays_config_object (int parent, const char *path); |
54 |
| - * |
55 |
| - * Retrieve an object. |
56 |
| - * |
57 |
| - */ |
58 |
| - |
59 |
| -#include <string.h> |
60 |
| -#include <unistd.h> |
61 |
| -#include <stdlib.h> |
62 |
| -#include <sys/types.h> |
63 |
| -#include <sys/stat.h> |
64 |
| -#include <fcntl.h> |
65 |
| - |
66 |
| -#include <echttp_json.h> |
67 |
| - |
68 |
| -#include "houselog.h" |
69 |
| -#include "houserelays.h" |
70 |
| -#include "houserelays_config.h" |
71 |
| - |
72 |
| -#define CONFIGMAXSIZE 1024 |
73 |
| - |
74 |
| -static ParserToken ConfigParsed[CONFIGMAXSIZE]; |
75 |
| -static int ConfigTokenCount = 0; |
76 |
| -static char *ConfigText; |
77 |
| -static int ConfigTextSize = 0; |
78 |
| -static int ConfigTextLength = 0; |
79 |
| - |
80 |
| -static const char *ConfigFile = "/etc/house/relays.json"; |
81 |
| - |
82 |
| -static const char *houserelays_config_refresh (const char *file) { |
83 |
| - |
84 |
| - if (ConfigText) echttp_parser_free (ConfigText); |
85 |
| - ConfigText = echttp_parser_load (file); |
86 |
| - ConfigTextLength = strlen(ConfigText); |
87 |
| - |
88 |
| - ConfigTokenCount = CONFIGMAXSIZE; |
89 |
| - return echttp_json_parse (ConfigText, ConfigParsed, &ConfigTokenCount); |
90 |
| -} |
91 |
| - |
92 |
| -const char *houserelays_config_load (int argc, const char **argv) { |
93 |
| - |
94 |
| - int i; |
95 |
| - |
96 |
| - for (i = 1; i < argc; ++i) { |
97 |
| - if (strncmp ("--config=", argv[i], 9) == 0) { |
98 |
| - ConfigFile = strdup(argv[i] + 9); |
99 |
| - } |
100 |
| - } |
101 |
| - houselog_event ("SYSTEM", "CONFIG", "LOAD", "FILE %s", ConfigFile); |
102 |
| - return houserelays_config_refresh (ConfigFile); |
103 |
| -} |
104 |
| - |
105 |
| -const char *houserelays_config_update (const char *text) { |
106 |
| - |
107 |
| - int fd; |
108 |
| - |
109 |
| - fd = open (ConfigFile, O_WRONLY|O_TRUNC|O_CREAT, 0777); |
110 |
| - if (fd >= 0) { |
111 |
| - write (fd, text, strlen(text)); |
112 |
| - close (fd); |
113 |
| - houselog_event ("SYSTEM", "CONFIG", "UPDATED", "FILE %s", ConfigFile); |
114 |
| - } |
115 |
| - return houserelays_config_refresh (ConfigFile); |
116 |
| -} |
117 |
| - |
118 |
| -int houserelays_config_file (void) { |
119 |
| - return open(ConfigFile, O_RDONLY); |
120 |
| -} |
121 |
| - |
122 |
| -int houserelays_config_size (void) { |
123 |
| - return ConfigTextLength; |
124 |
| -} |
125 |
| - |
126 |
| -int houserelays_config_find (int parent, const char *path, int type) { |
127 |
| - int i; |
128 |
| - if (parent < 0 || parent >= ConfigTokenCount) return -1; |
129 |
| - i = echttp_json_search(ConfigParsed+parent, path); |
130 |
| - if (i >= 0 && ConfigParsed[parent+i].type == type) return parent+i; |
131 |
| - return -1; |
132 |
| -} |
133 |
| - |
134 |
| -const char *houserelays_config_string (int parent, const char *path) { |
135 |
| - int i = houserelays_config_find(parent, path, PARSER_STRING); |
136 |
| - return (i >= 0) ? ConfigParsed[i].value.string : 0; |
137 |
| -} |
138 |
| - |
139 |
| -int houserelays_config_integer (int parent, const char *path) { |
140 |
| - int i = houserelays_config_find(parent, path, PARSER_INTEGER); |
141 |
| - return (i >= 0) ? ConfigParsed[i].value.integer : 0; |
142 |
| -} |
143 |
| - |
144 |
| -int houserelays_config_boolean (int parent, const char *path) { |
145 |
| - int i = houserelays_config_find(parent, path, PARSER_BOOL); |
146 |
| - return (i >= 0) ? ConfigParsed[i].value.bool : 0; |
147 |
| -} |
148 |
| - |
149 |
| -int houserelays_config_array (int parent, const char *path) { |
150 |
| - return houserelays_config_find(parent, path, PARSER_ARRAY); |
151 |
| -} |
152 |
| - |
153 |
| -int houserelays_config_array_length (int array) { |
154 |
| - if (array < 0 |
155 |
| - || array >= ConfigTokenCount |
156 |
| - || ConfigParsed[array].type != PARSER_ARRAY) return 0; |
157 |
| - return ConfigParsed[array].length; |
158 |
| -} |
159 |
| - |
160 |
| -int houserelays_config_object (int parent, const char *path) { |
161 |
| - return houserelays_config_find(parent, path, PARSER_OBJECT); |
162 |
| -} |
163 |
| - |
| 1 | +THIS FILE IS NOW OBSOLETE: see houseportal/houseconfig.c |
0 commit comments