configure.ac: add libusb/libusbx support (for xpp)
[dahdi/tools.git] / xpp / parse_span_specs.c
1 /*
2  * Written by Oron Peled <oron@actcom.co.il>
3  * Copyright (C) 2014, Xorcom
4  *
5  * All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <stdio.h>
24 #include <assert.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <limits.h>
29 #include <regex.h>
30 #include <fnmatch.h>
31 #include <sys/time.h>
32 #include "parse_span_specs.h"
33
34 void free_span_specifications(struct span_specs *span_specs)
35 {
36         if (span_specs) {
37                 if (span_specs->buf)
38                         free(span_specs->buf);
39                 free(span_specs);
40         }
41 }
42
43 static enum tdm_codec is_alaw_span_type(const char *span_type)
44 {
45         assert(span_type);
46         if (strcmp(span_type, "E1") == 0)
47                 return TDM_CODEC_ALAW;
48         else if (strcmp(span_type, "T1") == 0)
49                 return TDM_CODEC_ULAW;
50         return TDM_CODEC_UNKNOWN;
51 }
52
53 struct span_specs *parse_span_specifications(const char *spec_string, int default_is_alaw)
54 {
55         struct span_specs *span_specs;
56         char *p;
57         int spanno;
58         int i;
59
60         if (!spec_string)
61                 return NULL;
62         /* Allocate  and Initialize */
63         span_specs = calloc(sizeof(char *), MAX_SPANNO);
64         if (!span_specs)
65                 goto err;
66         for (spanno = 0; spanno < MAX_SPANNO; spanno++)
67                 span_specs->span_is_alaw[spanno] = TDM_CODEC_UNKNOWN;
68         span_specs->buf = strdup(spec_string);
69         if (!span_specs->buf)
70                 goto err;
71         for (i = 0;; i++) {
72                 char *curr_item;
73                 char *tokenize_key;
74                 char *key;
75                 char *value;
76                 enum tdm_codec is_alaw;
77                 int matched;
78
79                 /* Split to items */
80                 p = (i == 0) ? span_specs->buf : NULL;
81                 p = strtok_r(p, " \t,", &curr_item);
82                 if (!p)
83                         break;
84
85                 /* Split to <span>:<type> */
86                 key = strtok_r(p, ":", &tokenize_key);
87                 if (!key) {
88                         fprintf(stderr,
89                                 "Missing ':' (item #%d inside '%s')\n",
90                                 i+1, spec_string);
91                         goto err;
92                 }
93                 value = strtok_r(NULL, ":", &tokenize_key);
94                 if (!value) {
95                         fprintf(stderr,
96                                 "Missing value after ':' (item #%d inside '%s')\n",
97                                 i+1, spec_string);
98                         goto err;
99                 }
100
101                 /* Match span specification and set alaw/ulaw */
102                 is_alaw = is_alaw_span_type(value);
103                 if (is_alaw == TDM_CODEC_UNKNOWN) {
104                         fprintf(stderr,
105                                 "Illegal span type '%s' (item #%d inside '%s')\n",
106                                 value, i+1, spec_string);
107                         goto err;
108                 }
109                 matched = 0;
110                 for (spanno = 0; spanno < MAX_SPANNO; spanno++) {
111                         char tmpbuf[BUFSIZ];
112
113                         snprintf(tmpbuf, sizeof(tmpbuf), "%d", spanno + 1);
114                         if (fnmatch(p, tmpbuf, 0) == 0) {
115                                 matched++;
116                                 span_specs->span_is_alaw[spanno] = is_alaw;
117                         }
118                 }
119                 if (!matched) {
120                         fprintf(stderr,
121                                 "Span specification '%s' does not match any span (item #%d inside '%s')\n",
122                                 key, i+1, spec_string);
123                         goto err;
124                 }
125         }
126
127         /* Set defaults */
128         for (spanno = 0; spanno < MAX_SPANNO; spanno++) {
129                 if (span_specs->span_is_alaw[spanno] == TDM_CODEC_UNKNOWN) {
130                         span_specs->span_is_alaw[spanno] = default_is_alaw;
131                 }
132         }
133         return span_specs;
134 err:
135         free_span_specifications(span_specs);
136         return NULL;
137 }
138
139 void print_span_specifications(struct span_specs *span_specs, FILE *output)
140 {
141         int spanno;
142
143         if (!span_specs)
144                 return;
145         for (spanno = 0; spanno < MAX_SPANNO; spanno++) {
146                 enum tdm_codec is_alaw;
147
148                 is_alaw = span_specs->span_is_alaw[spanno];
149                 fprintf(output, "%d %s\n",
150                         spanno+1, (is_alaw == TDM_CODEC_ALAW) ? "alaw" : "ulaw");
151         }
152 }