blob: d2b180d78bbef6ccfffdb693d1f398eb67c9b0be [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>
31
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020032static struct osmo_config_entry *
33alloc_entry(struct osmo_config_list *entries,
34 const char *mcc, const char *mnc,
35 const char *option, const char *text)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080036{
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020037 struct osmo_config_entry *entry =
38 talloc_zero(entries, struct osmo_config_entry);
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080039 if (!entry)
40 return NULL;
41
42 entry->mcc = talloc_strdup(entry, mcc);
43 entry->mnc = talloc_strdup(entry, mnc);
44 entry->option = talloc_strdup(entry, option);
45 entry->text = talloc_strdup(entry, text);
46
47 llist_add_tail(&entry->list, &entries->entry);
48 return entry;
49}
50
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020051static struct osmo_config_list *alloc_entries(void *ctx)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080052{
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020053 struct osmo_config_list *entries;
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080054
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020055 entries = talloc_zero(ctx, struct osmo_config_list);
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080056 if (!entries)
57 return NULL;
58
59 INIT_LLIST_HEAD(&entries->entry);
60 return entries;
61}
62
63/*
64 * split a line like 'foo:Text'.
65 */
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020066static void handle_line(struct osmo_config_list *entries, char *line)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080067{
68 int i;
69 const int len = strlen(line);
70
71 char *items[3];
72 int last_item = 0;
73
Holger Hans Peter Freyther131bc802010-10-26 09:31:16 +020074 /* Skip comments from the file */
75 if (line[0] == '#')
76 return;
77
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080078 for (i = 0; i < len; ++i) {
79 if (line[i] == '\n' || line[i] == '\r')
80 line[i] = '\0';
81 else if (line[i] == ':' && last_item < 3) {
82 line[i] = '\0';
83
84 items[last_item++] = &line[i + 1];
85 }
86 }
87
88 if (last_item == 3) {
89 alloc_entry(entries, &line[0] , items[0], items[1], items[2]);
90 return;
91 }
92
93 /* nothing found */
94}
95
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020096struct osmo_config_list *osmo_config_list_parse(void *ctx, const char *filename)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080097{
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020098 struct osmo_config_list *entries;
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080099 size_t n;
100 char *line;
101 FILE *file;
102
103 file = fopen(filename, "r");
104 if (!file)
105 return NULL;
106
107 entries = alloc_entries(ctx);
108 if (!entries) {
109 fclose(file);
110 return NULL;
111 }
112
113 n = 2342;
114 line = NULL;
115 while (getline(&line, &n, file) != -1) {
116 handle_line(entries, line);
117 free(line);
118 line = NULL;
119 }
120
121 fclose(file);
122 return entries;
123}