blob: bf36bf36c7f6eb72788ff7b007bd2440c1073636 [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
Holger Hans Peter Freyther6cce3d72015-03-18 21:35:56 +010024#define _WITH_GETLINE
25
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010026#include <osmocom/core/msgfile.h>
27#include <osmocom/core/talloc.h>
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080028
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080029#include <sys/stat.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <string.h>
Sylvain Munaut31659f72012-12-12 00:00:43 +010033#include <stdio.h>
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080034
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020035static struct osmo_config_entry *
36alloc_entry(struct osmo_config_list *entries,
37 const char *mcc, const char *mnc,
38 const char *option, const char *text)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080039{
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020040 struct osmo_config_entry *entry =
41 talloc_zero(entries, struct osmo_config_entry);
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080042 if (!entry)
43 return NULL;
44
45 entry->mcc = talloc_strdup(entry, mcc);
46 entry->mnc = talloc_strdup(entry, mnc);
47 entry->option = talloc_strdup(entry, option);
48 entry->text = talloc_strdup(entry, text);
49
50 llist_add_tail(&entry->list, &entries->entry);
51 return entry;
52}
53
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020054static struct osmo_config_list *alloc_entries(void *ctx)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080055{
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020056 struct osmo_config_list *entries;
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080057
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020058 entries = talloc_zero(ctx, struct osmo_config_list);
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080059 if (!entries)
60 return NULL;
61
62 INIT_LLIST_HEAD(&entries->entry);
63 return entries;
64}
65
66/*
67 * split a line like 'foo:Text'.
68 */
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020069static void handle_line(struct osmo_config_list *entries, char *line)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080070{
71 int i;
72 const int len = strlen(line);
73
74 char *items[3];
75 int last_item = 0;
76
Holger Hans Peter Freyther131bc802010-10-26 09:31:16 +020077 /* Skip comments from the file */
78 if (line[0] == '#')
79 return;
80
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080081 for (i = 0; i < len; ++i) {
82 if (line[i] == '\n' || line[i] == '\r')
83 line[i] = '\0';
84 else if (line[i] == ':' && last_item < 3) {
85 line[i] = '\0';
86
87 items[last_item++] = &line[i + 1];
88 }
89 }
90
91 if (last_item == 3) {
92 alloc_entry(entries, &line[0] , items[0], items[1], items[2]);
93 return;
94 }
95
96 /* nothing found */
97}
98
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020099struct osmo_config_list *osmo_config_list_parse(void *ctx, const char *filename)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +0800100{
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +0200101 struct osmo_config_list *entries;
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +0800102 size_t n;
103 char *line;
104 FILE *file;
105
106 file = fopen(filename, "r");
107 if (!file)
108 return NULL;
109
110 entries = alloc_entries(ctx);
111 if (!entries) {
112 fclose(file);
113 return NULL;
114 }
115
116 n = 2342;
117 line = NULL;
118 while (getline(&line, &n, file) != -1) {
119 handle_line(entries, line);
120 free(line);
121 line = NULL;
122 }
123
124 fclose(file);
125 return entries;
126}