blob: 1f11aa6068ffaf3404278ddc69ab2129adbcdb72 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file msgfile.c
2 * Parse a simple file with messages, e.g used for USSD messages. */
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +08003/*
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +08004 * (C) 2010 by Holger Hans Peter Freyther
5 * (C) 2010 by On-Waves
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
Holger Hans Peter Freyther6cce3d72015-03-18 21:35:56 +010026#define _WITH_GETLINE
27
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010028#include <osmocom/core/msgfile.h>
29#include <osmocom/core/talloc.h>
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080030
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080031#include <sys/stat.h>
32#include <fcntl.h>
33#include <unistd.h>
34#include <string.h>
Sylvain Munaut31659f72012-12-12 00:00:43 +010035#include <stdio.h>
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080036
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020037static struct osmo_config_entry *
38alloc_entry(struct osmo_config_list *entries,
39 const char *mcc, const char *mnc,
40 const char *option, const char *text)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080041{
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020042 struct osmo_config_entry *entry =
43 talloc_zero(entries, struct osmo_config_entry);
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080044 if (!entry)
45 return NULL;
46
47 entry->mcc = talloc_strdup(entry, mcc);
48 entry->mnc = talloc_strdup(entry, mnc);
49 entry->option = talloc_strdup(entry, option);
50 entry->text = talloc_strdup(entry, text);
51
52 llist_add_tail(&entry->list, &entries->entry);
53 return entry;
54}
55
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020056static struct osmo_config_list *alloc_entries(void *ctx)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080057{
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020058 struct osmo_config_list *entries;
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080059
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020060 entries = talloc_zero(ctx, struct osmo_config_list);
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080061 if (!entries)
62 return NULL;
63
64 INIT_LLIST_HEAD(&entries->entry);
65 return entries;
66}
67
68/*
69 * split a line like 'foo:Text'.
70 */
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020071static void handle_line(struct osmo_config_list *entries, char *line)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080072{
73 int i;
74 const int len = strlen(line);
75
76 char *items[3];
77 int last_item = 0;
78
Holger Hans Peter Freyther131bc802010-10-26 09:31:16 +020079 /* Skip comments from the file */
80 if (line[0] == '#')
81 return;
82
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080083 for (i = 0; i < len; ++i) {
84 if (line[i] == '\n' || line[i] == '\r')
85 line[i] = '\0';
86 else if (line[i] == ':' && last_item < 3) {
87 line[i] = '\0';
88
89 items[last_item++] = &line[i + 1];
90 }
91 }
92
93 if (last_item == 3) {
94 alloc_entry(entries, &line[0] , items[0], items[1], items[2]);
95 return;
96 }
97
98 /* nothing found */
99}
100
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +0200101struct osmo_config_list *osmo_config_list_parse(void *ctx, const char *filename)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +0800102{
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +0200103 struct osmo_config_list *entries;
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +0800104 size_t n;
105 char *line;
106 FILE *file;
107
108 file = fopen(filename, "r");
109 if (!file)
110 return NULL;
111
112 entries = alloc_entries(ctx);
113 if (!entries) {
114 fclose(file);
115 return NULL;
116 }
117
118 n = 2342;
119 line = NULL;
120 while (getline(&line, &n, file) != -1) {
121 handle_line(entries, line);
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +0800122 }
Neels Hofmeyr00393e12017-11-16 16:24:04 +0100123 /* The returned getline() buffer needs to be freed even if it failed. It can simply re-use the
124 * buffer that was allocated on the first call. */
125 free(line);
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +0800126
127 fclose(file);
128 return entries;
129}