Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 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 | |
Holger Hans Peter Freyther | 6cce3d7 | 2015-03-18 21:35:56 +0100 | [diff] [blame] | 24 | #define _WITH_GETLINE |
| 25 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 26 | #include <osmocom/core/msgfile.h> |
| 27 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 28 | |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <unistd.h> |
| 32 | #include <string.h> |
Sylvain Munaut | 31659f7 | 2012-12-12 00:00:43 +0100 | [diff] [blame] | 33 | #include <stdio.h> |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 34 | |
Pablo Neira Ayuso | 1b4a42c | 2011-05-07 13:01:41 +0200 | [diff] [blame] | 35 | static struct osmo_config_entry * |
| 36 | alloc_entry(struct osmo_config_list *entries, |
| 37 | const char *mcc, const char *mnc, |
| 38 | const char *option, const char *text) |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 39 | { |
Pablo Neira Ayuso | 1b4a42c | 2011-05-07 13:01:41 +0200 | [diff] [blame] | 40 | struct osmo_config_entry *entry = |
| 41 | talloc_zero(entries, struct osmo_config_entry); |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 42 | 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 Ayuso | 1b4a42c | 2011-05-07 13:01:41 +0200 | [diff] [blame] | 54 | static struct osmo_config_list *alloc_entries(void *ctx) |
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 | struct osmo_config_list *entries; |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 57 | |
Pablo Neira Ayuso | 1b4a42c | 2011-05-07 13:01:41 +0200 | [diff] [blame] | 58 | entries = talloc_zero(ctx, struct osmo_config_list); |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 59 | 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 Ayuso | 1b4a42c | 2011-05-07 13:01:41 +0200 | [diff] [blame] | 69 | 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] | 70 | { |
| 71 | int i; |
| 72 | const int len = strlen(line); |
| 73 | |
| 74 | char *items[3]; |
| 75 | int last_item = 0; |
| 76 | |
Holger Hans Peter Freyther | 131bc80 | 2010-10-26 09:31:16 +0200 | [diff] [blame] | 77 | /* Skip comments from the file */ |
| 78 | if (line[0] == '#') |
| 79 | return; |
| 80 | |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 81 | 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 Ayuso | 1b4a42c | 2011-05-07 13:01:41 +0200 | [diff] [blame] | 99 | 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] | 100 | { |
Pablo Neira Ayuso | 1b4a42c | 2011-05-07 13:01:41 +0200 | [diff] [blame] | 101 | struct osmo_config_list *entries; |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 102 | 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 | } |