sms: SMS where cropped (from VTY), concatenation of SMS where not possible
[osmocom-bb.git] / src / msgfile.c
1 /*
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
24 #include <osmocom/core/msgfile.h>
25 #include <osmocom/core/talloc.h>
26
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <string.h>
31
32 static struct osmo_config_entry *
33 alloc_entry(struct osmo_config_list *entries,
34             const char *mcc, const char *mnc,
35             const char *option, const char *text)
36 {
37         struct osmo_config_entry *entry =
38                 talloc_zero(entries, struct osmo_config_entry);
39         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
51 static struct osmo_config_list *alloc_entries(void *ctx)
52 {
53         struct osmo_config_list *entries;
54
55         entries = talloc_zero(ctx, struct osmo_config_list);
56         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  */
66 static void handle_line(struct osmo_config_list *entries, char *line)
67 {
68         int i;
69         const int len = strlen(line);
70
71         char *items[3];
72         int last_item = 0;
73
74         /* Skip comments from the file */
75         if (line[0] == '#')
76                 return;
77
78         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
96 struct osmo_config_list *osmo_config_list_parse(void *ctx, const char *filename)
97 {
98         struct osmo_config_list *entries;
99         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 }