blob: 1a991841452736225d69c0b9957913837657c4f9 [file] [log] [blame]
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +08001/*
2 * Parse a simple file with messages, e.g used for USSD messages
3 *
4 * (C) 2010 by Holger Hans Peter Freyther
5 * (C) 2010 by On-Waves
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010024#include <osmocom/core/msgfile.h>
25#include <osmocom/core/talloc.h>
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080026
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080027#include <sys/stat.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <string.h>
Sylvain Munaut31659f72012-12-12 00:00:43 +010031#include <stdio.h>
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080032
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020033static struct osmo_config_entry *
34alloc_entry(struct osmo_config_list *entries,
35 const char *mcc, const char *mnc,
36 const char *option, const char *text)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080037{
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020038 struct osmo_config_entry *entry =
39 talloc_zero(entries, struct osmo_config_entry);
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080040 if (!entry)
41 return NULL;
42
43 entry->mcc = talloc_strdup(entry, mcc);
44 entry->mnc = talloc_strdup(entry, mnc);
45 entry->option = talloc_strdup(entry, option);
46 entry->text = talloc_strdup(entry, text);
47
48 llist_add_tail(&entry->list, &entries->entry);
49 return entry;
50}
51
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020052static struct osmo_config_list *alloc_entries(void *ctx)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080053{
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020054 struct osmo_config_list *entries;
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080055
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020056 entries = talloc_zero(ctx, struct osmo_config_list);
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080057 if (!entries)
58 return NULL;
59
60 INIT_LLIST_HEAD(&entries->entry);
61 return entries;
62}
63
64/*
65 * split a line like 'foo:Text'.
66 */
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020067static void handle_line(struct osmo_config_list *entries, char *line)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080068{
69 int i;
70 const int len = strlen(line);
71
72 char *items[3];
73 int last_item = 0;
74
Holger Hans Peter Freyther131bc802010-10-26 09:31:16 +020075 /* Skip comments from the file */
76 if (line[0] == '#')
77 return;
78
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080079 for (i = 0; i < len; ++i) {
80 if (line[i] == '\n' || line[i] == '\r')
81 line[i] = '\0';
82 else if (line[i] == ':' && last_item < 3) {
83 line[i] = '\0';
84
85 items[last_item++] = &line[i + 1];
86 }
87 }
88
89 if (last_item == 3) {
90 alloc_entry(entries, &line[0] , items[0], items[1], items[2]);
91 return;
92 }
93
94 /* nothing found */
95}
96
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020097struct osmo_config_list *osmo_config_list_parse(void *ctx, const char *filename)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080098{
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020099 struct osmo_config_list *entries;
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +0800100 size_t n;
101 char *line;
102 FILE *file;
103
104 file = fopen(filename, "r");
105 if (!file)
106 return NULL;
107
108 entries = alloc_entries(ctx);
109 if (!entries) {
110 fclose(file);
111 return NULL;
112 }
113
114 n = 2342;
115 line = NULL;
116 while (getline(&line, &n, file) != -1) {
117 handle_line(entries, line);
118 free(line);
119 line = NULL;
120 }
121
122 fclose(file);
123 return entries;
124}