Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file msgfile.c |
| 2 | * Parse a simple file with messages, e.g used for USSD messages. */ |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 3 | /* |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 4 | * (C) 2010 by Holger Hans Peter Freyther |
| 5 | * (C) 2010 by On-Waves |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 10 | * 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 Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 20 | */ |
| 21 | |
Holger Hans Peter Freyther | 6cce3d7 | 2015-03-18 21:35:56 +0100 | [diff] [blame] | 22 | #define _WITH_GETLINE |
| 23 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 24 | #include <osmocom/core/msgfile.h> |
| 25 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 26 | |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <unistd.h> |
| 30 | #include <string.h> |
Sylvain Munaut | 31659f7 | 2012-12-12 00:00:43 +0100 | [diff] [blame] | 31 | #include <stdio.h> |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 32 | |
Pablo Neira Ayuso | 1b4a42c | 2011-05-07 13:01:41 +0200 | [diff] [blame] | 33 | static struct osmo_config_entry * |
| 34 | alloc_entry(struct osmo_config_list *entries, |
| 35 | const char *mcc, const char *mnc, |
| 36 | const char *option, const char *text) |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 37 | { |
Pablo Neira Ayuso | 1b4a42c | 2011-05-07 13:01:41 +0200 | [diff] [blame] | 38 | struct osmo_config_entry *entry = |
| 39 | talloc_zero(entries, struct osmo_config_entry); |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 40 | 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 Ayuso | 1b4a42c | 2011-05-07 13:01:41 +0200 | [diff] [blame] | 52 | static struct osmo_config_list *alloc_entries(void *ctx) |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 53 | { |
Pablo Neira Ayuso | 1b4a42c | 2011-05-07 13:01:41 +0200 | [diff] [blame] | 54 | struct osmo_config_list *entries; |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 55 | |
Pablo Neira Ayuso | 1b4a42c | 2011-05-07 13:01:41 +0200 | [diff] [blame] | 56 | entries = talloc_zero(ctx, struct osmo_config_list); |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 57 | 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 Ayuso | 1b4a42c | 2011-05-07 13:01:41 +0200 | [diff] [blame] | 67 | static void handle_line(struct osmo_config_list *entries, char *line) |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 68 | { |
| 69 | int i; |
| 70 | const int len = strlen(line); |
| 71 | |
| 72 | char *items[3]; |
| 73 | int last_item = 0; |
| 74 | |
Holger Hans Peter Freyther | 131bc80 | 2010-10-26 09:31:16 +0200 | [diff] [blame] | 75 | /* Skip comments from the file */ |
| 76 | if (line[0] == '#') |
| 77 | return; |
| 78 | |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 79 | 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 Ayuso | 1b4a42c | 2011-05-07 13:01:41 +0200 | [diff] [blame] | 97 | struct osmo_config_list *osmo_config_list_parse(void *ctx, const char *filename) |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 98 | { |
Pablo Neira Ayuso | 1b4a42c | 2011-05-07 13:01:41 +0200 | [diff] [blame] | 99 | struct osmo_config_list *entries; |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 100 | 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 Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 118 | } |
Neels Hofmeyr | 00393e1 | 2017-11-16 16:24:04 +0100 | [diff] [blame] | 119 | /* 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 Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 122 | |
| 123 | fclose(file); |
| 124 | return entries; |
| 125 | } |