blob: abb4e7cfbf6f8705d9b5fba5ed71e67e0758fbe8 [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 *
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080020 */
21
Holger Hans Peter Freyther6cce3d72015-03-18 21:35:56 +010022#define _WITH_GETLINE
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>
Sylvain Munaut31659f72012-12-12 00:00:43 +010031#include <stdio.h>
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080032
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020033static struct osmo_config_entry *
34alloc_entry(struct osmo_config_list *entries,
35 const char *mcc, const char *mnc,
36 const char *option, const char *text)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080037{
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020038 struct osmo_config_entry *entry =
39 talloc_zero(entries, struct osmo_config_entry);
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080040 if (!entry)
41 return NULL;
42
43 entry->mcc = talloc_strdup(entry, mcc);
44 entry->mnc = talloc_strdup(entry, mnc);
45 entry->option = talloc_strdup(entry, option);
46 entry->text = talloc_strdup(entry, text);
47
48 llist_add_tail(&entry->list, &entries->entry);
49 return entry;
50}
51
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020052static struct osmo_config_list *alloc_entries(void *ctx)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080053{
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020054 struct osmo_config_list *entries;
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080055
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020056 entries = talloc_zero(ctx, struct osmo_config_list);
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080057 if (!entries)
58 return NULL;
59
60 INIT_LLIST_HEAD(&entries->entry);
61 return entries;
62}
63
64/*
65 * split a line like 'foo:Text'.
66 */
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020067static void handle_line(struct osmo_config_list *entries, char *line)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080068{
69 int i;
70 const int len = strlen(line);
71
72 char *items[3];
73 int last_item = 0;
74
Holger Hans Peter Freyther131bc802010-10-26 09:31:16 +020075 /* Skip comments from the file */
76 if (line[0] == '#')
77 return;
78
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080079 for (i = 0; i < len; ++i) {
80 if (line[i] == '\n' || line[i] == '\r')
81 line[i] = '\0';
82 else if (line[i] == ':' && last_item < 3) {
83 line[i] = '\0';
84
85 items[last_item++] = &line[i + 1];
86 }
87 }
88
89 if (last_item == 3) {
90 alloc_entry(entries, &line[0] , items[0], items[1], items[2]);
91 return;
92 }
93
94 /* nothing found */
95}
96
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020097struct osmo_config_list *osmo_config_list_parse(void *ctx, const char *filename)
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +080098{
Pablo Neira Ayuso1b4a42c2011-05-07 13:01:41 +020099 struct osmo_config_list *entries;
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +0800100 size_t n;
101 char *line;
102 FILE *file;
103
104 file = fopen(filename, "r");
105 if (!file)
106 return NULL;
107
108 entries = alloc_entries(ctx);
109 if (!entries) {
110 fclose(file);
111 return NULL;
112 }
113
114 n = 2342;
115 line = NULL;
116 while (getline(&line, &n, file) != -1) {
117 handle_line(entries, line);
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +0800118 }
Neels Hofmeyr00393e12017-11-16 16:24:04 +0100119 /* The returned getline() buffer needs to be freed even if it failed. It can simply re-use the
120 * buffer that was allocated on the first call. */
121 free(line);
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +0800122
123 fclose(file);
124 return entries;
125}