1 /*
2 * ini.c
3 *
4 * Copyright (c) 2013-2018 Pacman Development Team <pacman-dev@archlinux.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <errno.h>
21 #include <limits.h>
22 #include <string.h> /* strdup */
23
24 #include <alpm.h>
25
26 #include "ini.h"
27 #include "util.h"
28
29 /**
30 * @brief Parse a pacman-style INI config file.
31 *
32 * @param file path to the config file
33 * @param cb callback for key/value pairs
34 * @param data caller defined data to be passed to the callback
35 *
36 * @return the callback return value
37 *
38 * @note The callback will be called at the beginning of each section with an
39 * empty key and value and for each key/value pair.
40 *
41 * @note If the parser encounters an error the callback will be called with
42 * section, key, and value set to NULL and errno set by fopen, fgets, or
43 * strdup.
44 *
45 * @note The @a key and @a value passed to @ cb will be overwritten between
46 * calls. The section name will remain valid until after @a cb is called to
47 * begin a new section.
48 *
49 * @note Parsing will immediately stop if the callback returns non-zero.
50 */
parse_ini(const char * file,ini_parser_fn cb,void * data)51 int parse_ini(const char *file, ini_parser_fn cb, void *data)
52 {
53 char line[PATH_MAX], *section_name = NULL;
54 FILE *fp = NULL;
55 int linenum = 0;
56 int ret = 0;
57
58 fp = fopen(file, "r");
59 if(fp == NULL) {
60 return cb(file, 0, NULL, NULL, NULL, data);
61 }
62
63 while(safe_fgets(line, PATH_MAX, fp)) {
64 char *key, *value;
65 size_t line_len;
66
67 linenum++;
68
69 line_len = strtrim(line);
70
71 if(line_len == 0 || line[0] == '#') {
72 continue;
73 }
74
75 if(line[0] == '[' && line[line_len - 1] == ']') {
76 char *name;
77 /* new config section, skip the '[' */
78 name = strdup(line + 1);
79 name[line_len - 2] = '\0';
80
81 ret = cb(file, linenum, name, NULL, NULL, data);
82 free(section_name);
83 section_name = name;
84
85 /* we're at a new section; perform any post-actions for the prior */
86 if(ret) {
87 goto cleanup;
88 }
89 continue;
90 }
91
92 /* directive */
93 /* strsep modifies the 'line' string: 'key \0 value' */
94 key = line;
95 value = line;
96 strsep(&value, "=");
97 strtrim(key);
98 strtrim(value);
99
100 if((ret = cb(file, linenum, section_name, key, value, data)) != 0) {
101 goto cleanup;
102 }
103 }
104
105 cleanup:
106 fclose(fp);
107 free(section_name);
108 return ret;
109 }
110