blob: c13df51de8180d5c06643e75b047f7c04daa3f30 [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
32static struct msg_entry *alloc_entry(struct msg_entries *entries,
33 const char *mcc, const char *mnc,
34 const char *option, const char *text)
35{
36 struct msg_entry *entry = talloc_zero(entries, struct msg_entry);
37 if (!entry)
38 return NULL;
39
40 entry->mcc = talloc_strdup(entry, mcc);
41 entry->mnc = talloc_strdup(entry, mnc);
42 entry->option = talloc_strdup(entry, option);
43 entry->text = talloc_strdup(entry, text);
44
45 llist_add_tail(&entry->list, &entries->entry);
46 return entry;
47}
48
49static struct msg_entries *alloc_entries(void *ctx)
50{
51 struct msg_entries *entries;
52
53 entries = talloc_zero(ctx, struct msg_entries);
54 if (!entries)
55 return NULL;
56
57 INIT_LLIST_HEAD(&entries->entry);
58 return entries;
59}
60
61/*
62 * split a line like 'foo:Text'.
63 */
64static void handle_line(struct msg_entries *entries, char *line)
65{
66 int i;
67 const int len = strlen(line);
68
69 char *items[3];
70 int last_item = 0;
71
Holger Hans Peter Freyther131bc802010-10-26 09:31:16 +020072 /* Skip comments from the file */
73 if (line[0] == '#')
74 return;
75
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080076 for (i = 0; i < len; ++i) {
77 if (line[i] == '\n' || line[i] == '\r')
78 line[i] = '\0';
79 else if (line[i] == ':' && last_item < 3) {
80 line[i] = '\0';
81
82 items[last_item++] = &line[i + 1];
83 }
84 }
85
86 if (last_item == 3) {
87 alloc_entry(entries, &line[0] , items[0], items[1], items[2]);
88 return;
89 }
90
91 /* nothing found */
92}
93
94struct msg_entries *msg_entry_parse(void *ctx, const char *filename)
95{
96 struct msg_entries *entries;
97 size_t n;
98 char *line;
99 FILE *file;
100
101 file = fopen(filename, "r");
102 if (!file)
103 return NULL;
104
105 entries = alloc_entries(ctx);
106 if (!entries) {
107 fclose(file);
108 return NULL;
109 }
110
111 n = 2342;
112 line = NULL;
113 while (getline(&line, &n, file) != -1) {
114 handle_line(entries, line);
115 free(line);
116 line = NULL;
117 }
118
119 fclose(file);
120 return entries;
121}