source: roaraudio/libroar/vio_dstr.c @ 1323:ad8d25a78446

Last change on this file since 1323:ad8d25a78446 was 1323:ad8d25a78446, checked in by phi, 15 years ago

added dstr types, added some meta funcs, wrote basic dstr parser

File size: 6.9 KB
Line 
1//vio_dstr.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37struct {
38 int    id;
39 char * name;
40 int    pdeftype[16];
41} _roar_vio_dstr_objs[] = {
42/*
43grep '^#define ROAR_VIO_DSTR_OBJT_' vio_dstr.h | cut -d' ' -f2 | while read objt; do name=`cut -d_ -f5,6,7,8,9,10 <<<$objt | tr A-Z a-z`; echo -e " {$objt,\t \"$name\","; echo "      {ROAR_VIO_DEF_TYPE_EOL}},"; done;
44*/
45 {ROAR_VIO_DSTR_OBJT_FILE,       "file",
46      {ROAR_VIO_DEF_TYPE_EOL}},
47 {ROAR_VIO_DSTR_OBJT_FH,         "fh",
48      {ROAR_VIO_DEF_TYPE_EOL}},
49 {ROAR_VIO_DSTR_OBJT_SOCKETFH,   "socketfh",
50      {ROAR_VIO_DEF_TYPE_EOL}},
51 {ROAR_VIO_DSTR_OBJT_PASS,       "pass",
52      {ROAR_VIO_DEF_TYPE_EOL}},
53 {ROAR_VIO_DSTR_OBJT_RE,         "re",
54      {ROAR_VIO_DEF_TYPE_EOL}},
55 {ROAR_VIO_DSTR_OBJT_EXEC,       "exec",
56      {ROAR_VIO_DEF_TYPE_EOL}},
57
58 {ROAR_VIO_DSTR_OBJT_SOCKET,     "socket",
59      {ROAR_VIO_DEF_TYPE_EOL}},
60 {ROAR_VIO_DSTR_OBJT_UNIX,       "unix",
61      {ROAR_VIO_DEF_TYPE_EOL}},
62 {ROAR_VIO_DSTR_OBJT_DECNET,     "decnet",
63      {ROAR_VIO_DEF_TYPE_EOL}},
64 {ROAR_VIO_DSTR_OBJT_TCP,        "tcp",
65      {ROAR_VIO_DEF_TYPE_EOL}},
66 {ROAR_VIO_DSTR_OBJT_UDP,        "udp",
67      {ROAR_VIO_DEF_TYPE_EOL}},
68 {ROAR_VIO_DSTR_OBJT_TCP6,       "tcp6",
69      {ROAR_VIO_DEF_TYPE_EOL}},
70 {ROAR_VIO_DSTR_OBJT_UDP6,       "udp6",
71      {ROAR_VIO_DEF_TYPE_EOL}},
72
73 {ROAR_VIO_DSTR_OBJT_SOCKS,      "socks",
74      {ROAR_VIO_DEF_TYPE_EOL}},
75 {ROAR_VIO_DSTR_OBJT_SOCKS4,     "socks4",
76      {ROAR_VIO_DEF_TYPE_EOL}},
77 {ROAR_VIO_DSTR_OBJT_SOCKS4A,    "socks4a",
78      {ROAR_VIO_DEF_TYPE_EOL}},
79 {ROAR_VIO_DSTR_OBJT_SOCKS4D,    "socks4d",
80      {ROAR_VIO_DEF_TYPE_EOL}},
81 {ROAR_VIO_DSTR_OBJT_SOCKS5,     "socks5",
82      {ROAR_VIO_DEF_TYPE_EOL}},
83 {ROAR_VIO_DSTR_OBJT_SSH,        "ssh",
84      {ROAR_VIO_DEF_TYPE_EOL}},
85
86 {ROAR_VIO_DSTR_OBJT_HTTP09,     "http09",
87      {ROAR_VIO_DEF_TYPE_EOL}},
88 {ROAR_VIO_DSTR_OBJT_HTTP10,     "http10",
89      {ROAR_VIO_DEF_TYPE_EOL}},
90 {ROAR_VIO_DSTR_OBJT_HTTP11,     "http11",
91      {ROAR_VIO_DEF_TYPE_EOL}},
92 {ROAR_VIO_DSTR_OBJT_HTTP,       "http",
93      {ROAR_VIO_DEF_TYPE_EOL}},
94 {ROAR_VIO_DSTR_OBJT_GOPHER,     "gopher",
95      {ROAR_VIO_DEF_TYPE_EOL}},
96
97 {ROAR_VIO_DSTR_OBJT_GZIP,       "gzip",
98      {ROAR_VIO_DEF_TYPE_EOL}},
99 {ROAR_VIO_DSTR_OBJT_BZIP2,      "bzip2",
100      {ROAR_VIO_DEF_TYPE_EOL}},
101
102 {ROAR_VIO_DSTR_OBJT_PGP,        "pgp",
103      {ROAR_VIO_DEF_TYPE_EOL}},
104 {ROAR_VIO_DSTR_OBJT_PGP_ENC,    "pgp_enc",
105      {ROAR_VIO_DEF_TYPE_EOL}},
106 {ROAR_VIO_DSTR_OBJT_PGP_STORE,  "pgp_store",
107      {ROAR_VIO_DEF_TYPE_EOL}},
108 {ROAR_VIO_DSTR_OBJT_SSL1,       "ssl1",
109      {ROAR_VIO_DEF_TYPE_EOL}},
110 {ROAR_VIO_DSTR_OBJT_SSL2,       "ssl2",
111      {ROAR_VIO_DEF_TYPE_EOL}},
112 {ROAR_VIO_DSTR_OBJT_SSL3,       "ssl3",
113      {ROAR_VIO_DEF_TYPE_EOL}},
114 {ROAR_VIO_DSTR_OBJT_TLS,        "tls",
115      {ROAR_VIO_DEF_TYPE_EOL}},
116 {ROAR_VIO_DSTR_OBJT_SSLTLS,     "ssltls",
117      {ROAR_VIO_DEF_TYPE_EOL}},
118
119 {ROAR_VIO_DSTR_OBJT_MAGIC,      "magic",
120      {ROAR_VIO_DEF_TYPE_EOL}},
121
122 {ROAR_VIO_DSTR_OBJT_INTERNAL, "INTERNAL",
123      {ROAR_VIO_DEF_TYPE_FILE, ROAR_VIO_DEF_TYPE_SOCKET, ROAR_VIO_DEF_TYPE_FH, ROAR_VIO_DEF_TYPE_SOCKETFH,
124       ROAR_VIO_DEF_TYPE_EOL}},
125 {ROAR_VIO_DSTR_OBJT_EOL, NULL, {ROAR_VIO_DEF_TYPE_EOL}}
126};
127
128int     roar_vio_dstr_get_type(char * str) {
129 int i;
130
131 for (i = 0; _roar_vio_dstr_objs[i].id != ROAR_VIO_DSTR_OBJT_EOL; i++) {
132  if ( strcasecmp(_roar_vio_dstr_objs[i].name, str) == 0 )
133   return _roar_vio_dstr_objs[i].id;
134 }
135
136 return -1;
137}
138
139char *  roar_vio_dstr_get_name(int type) {
140 int i;
141
142 for (i = 0; _roar_vio_dstr_objs[i].id != ROAR_VIO_DSTR_OBJT_EOL; i++) {
143  if ( _roar_vio_dstr_objs[i].id == type )
144   return _roar_vio_dstr_objs[i].name;
145 }
146
147 return NULL;
148}
149
150int     roar_vio_dstr_init_defaults (struct roar_vio_defaults * def, int type, int o_flags, mode_t o_mode) {
151 if ( def == NULL )
152  return -1;
153
154 memset(def, 0, sizeof(struct roar_vio_defaults));
155
156 def->type    = type;
157 def->o_flags = o_flags;
158 def->o_mode  = o_mode;
159
160 return 0;
161}
162
163int     roar_vio_open_dstr    (struct roar_vio_calls * calls, char * dstr, struct roar_vio_defaults * def, int dnum) {
164 return roar_vio_open_dstr_vio(calls, dstr, def, dnum, NULL);
165}
166
167#define _ret(x) free(dstr); return (x)
168
169int     roar_vio_open_dstr_vio(struct roar_vio_calls * calls,
170                               char * dstr, struct roar_vio_defaults * def, int dnum,
171                               struct roar_vio_calls * vio) {
172 char * next;
173 char * this;
174 char * name;
175 char * opts;
176 char * dst;
177 char * c;
178 int    inopts;
179 int    type;
180
181 if ( calls == NULL || dstr == NULL )
182  return -1;
183
184 if ( dnum != 0 && def == NULL )
185  return -1;
186
187 if ( dnum > 1 )
188  return -1;
189
190 if ( (dstr = strdup(dstr)) == NULL )
191  return -1;
192
193 next = dstr;
194
195 while (next != NULL) {
196  this = next;
197  next = strstr(next, "##");
198
199  if (next != NULL) {
200   *next = 0;
201   next += 2;
202  }
203
204  // we have the current token in 'this'.
205
206  opts   = NULL;
207  dst    = NULL;
208
209  if ( strstr(this, ":") != NULL ) {
210   name   = this;
211   inopts = 0;
212   for (c = this; *c != 0; c++) {
213    if ( *c == '[' ) {
214     *c     = 0;
215     opts   = c + 1;
216     inopts = 1;
217    } else if ( *c == ']' &&  inopts ) {
218     *c     = 0;
219     inopts = 0;
220    } else if ( *c == ':' && !inopts ) {
221     *c     = 0;
222     dst    = *(c+1) == 0 ? NULL : c + 1;
223     break;
224    }
225   }
226  } else {
227   // we need to guess that this is here...
228   // currently we guess this is a file in all cases
229   name = "file";
230   dst  = this;
231  }
232
233  ROAR_WARN("roar_vio_open_dstr_vio(*): name='%s', opts='%s', dst='%s'", name, opts, dst);
234
235  if ( (type = roar_vio_dstr_get_type(name)) == -1 ) {
236   _ret(-1);
237  }
238
239  ROAR_WARN("roar_vio_open_dstr_vio(*): type=0x%.4x(%s)", type, roar_vio_dstr_get_name(type));
240
241 }
242
243 _ret(-1);
244}
245
246//ll
Note: See TracBrowser for help on using the repository browser.