source: roaraudio/roarclients/roarvio.c @ 4836:684486ae8f9f

Last change on this file since 4836:684486ae8f9f was 4836:684486ae8f9f, checked in by phi, 13 years ago

add --explain

File size: 5.0 KB
Line 
1//roarvio.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011
5 *
6 *  This file is part of roarclients 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 *  RoarAudio 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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26int g_verbose = 0;
27#define ROAR_DBG_INFOVAR g_verbose
28
29#include <roaraudio.h>
30#include <errno.h>
31#include <string.h>
32
33enum action {
34 READ,
35 WRITE,
36 EXPLAIN
37};
38
39
40void usage (const char * progname) {
41}
42
43int do_explain (struct roar_vio_calls * cur) {
44 struct roar_sockname sockname;
45 int                  have_sockname;
46 int need_space, need_space2;
47 int level = 0;
48 int fh;
49 char * name;
50 const char * codec;
51 char * content_type;
52
53 while (cur != NULL) {
54  if ( roar_vio_ctl(cur, ROAR_VIO_CTL_GET_NAME, &name) == -1 )
55   name = "UNKNOWN";
56
57  if ( g_verbose ) {
58   if ( roar_vio_ctl(cur, ROAR_VIO_CTL_GET_MIMETYPE, &content_type) == -1 )
59    content_type = "UNKNOWN";
60
61   codec = roar_codec2str(roar_mime2codec(content_type));
62
63   if ( roar_vio_ctl(cur, ROAR_VIO_CTL_GET_FH, &fh) == -1 )
64    fh = -1;
65
66   if ( g_verbose > 1 ) {
67    if ( roar_vio_ctl(cur, ROAR_VIO_CTL_GET_PEERNAME, &sockname) == -1 ) {
68     have_sockname = 0;
69    } else {
70     have_sockname = 1;
71    }
72   } else {
73    have_sockname = 0;
74   }
75  } else {
76   content_type  = "UNKNOWN";
77   codec         = "UNKNOWN";
78   fh            = -1;
79   have_sockname = 0;
80  }
81
82  printf("%i: %s", level, name);
83  if ( fh != -1 || !!strcasecmp(codec, "UNKNOWN") || !!strcasecmp(content_type, "UNKNOWN") || have_sockname ) {
84   need_space = 0;
85   printf(" (");
86   if ( fh != -1 ) {
87    printf("fh=%i", fh);
88    need_space = 1;
89   }
90
91   if ( have_sockname ) {
92    need_space2 = 0;
93    printf("%ssocket={", need_space ? ", " : "");
94
95    need_space2 = 1;
96    switch (sockname.type) {
97     case ROAR_SOCKET_TYPE_UNIX:   printf("af=UNIX");   break;
98     case ROAR_SOCKET_TYPE_DECNET: printf("af=DECnet"); break;
99     case ROAR_SOCKET_TYPE_INET:   printf("af=INET");   break;
100     case ROAR_SOCKET_TYPE_INET6:  printf("af=INET6");  break;
101     default:
102       need_space2 = 0;
103      break;
104    }
105
106    if ( sockname.addr != NULL ) {
107     printf("%saddr=\"%s\"", need_space2 ? ", " : "", sockname.addr);
108     need_space2 = 1;
109    }
110
111    if ( sockname.port ) {
112     printf("%sport=%i", need_space2 ? ", " : "", sockname.port);
113     need_space2 = 1;
114    }
115
116    printf("}");
117    need_space = 1;
118   }
119
120   if ( !!strcasecmp(codec, "UNKNOWN") ) {
121    printf("%scodec=%s", need_space ? ", " : "", codec);
122    need_space = 1;
123   }
124
125   if ( !!strcmp(content_type, "UNKNOWN") ) {
126    printf("%scontent-type=\"%s\"", need_space ? ", " : "", content_type);
127    need_space = 1;
128   }
129   printf(")");
130  }
131  printf("\n");
132
133  level++;
134
135  if ( have_sockname )
136   if ( sockname.addr != NULL )
137    roar_mm_free(sockname.addr);
138
139  if ( roar_vio_ctl(cur, ROAR_VIO_CTL_GET_NEXT, &cur) == -1 )
140   cur = NULL;
141 }
142
143 return 0;
144}
145
146int main (int argc, char * argv[]) {
147 struct roar_vio_defaults def;
148 struct roar_vio_calls vio;
149 enum action action = READ;
150 int i;
151 char * k;
152 char * file = NULL;
153 int o_flags = -1;
154 int ret = 0;
155
156 for (i = 1; i < argc; i++) {
157  k = argv[i];
158
159  if ( !strcmp(k, "-h") || !strcmp(k, "--help") ) {
160   usage(argv[0]);
161   return 0;
162  } else if ( !strcmp(k, "--read") ) {
163   action = READ;
164  } else if ( !strcmp(k, "--write") ) {
165   action = WRITE;
166  } else if ( !strcmp(k, "--explain") ) {
167   action = EXPLAIN;
168  } else if ( !strcmp(k, "--verbose") ) {
169   g_verbose++;
170  } else if ( file == NULL ) {
171   file = k;
172  } else {
173   ROAR_ERR("Too many parameters or unknown parameter: %s", k);
174   usage(argv[0]);
175   return 1;
176  }
177 }
178
179 if ( file == NULL ) {
180  usage(argv[0]);
181  return 1;
182 }
183
184 switch (action) {
185  case READ:
186  case EXPLAIN:
187    o_flags = O_RDONLY;
188   break;
189  case WRITE:
190    o_flags = O_WRONLY|O_CREAT|O_TRUNC;
191   break;
192 }
193
194 if ( o_flags == -1 ) {
195  ROAR_ERR("o_flags unset, very bad. This should never happen.");
196  return 1;
197 }
198
199 if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, o_flags, 0644) == -1 ) {
200  ROAR_ERR("Can not init DSTR defaults. Bad.");
201  return 1;
202 }
203
204 if ( roar_vio_open_dstr(&vio, file, &def, 1) == -1 ) {
205  ROAR_ERR("Can not open file: %s: %s", file, strerror(errno));
206  return 1;
207 }
208
209 switch (action) {
210  case READ:
211    roar_vio_copy_data(roar_stdout, &vio);
212   break;
213  case WRITE:
214    roar_vio_copy_data(&vio, roar_stdin);
215   break;
216  case EXPLAIN:
217    if ( do_explain(&vio) == -1 )
218     ret = 4;
219   break;
220 }
221
222 roar_vio_close(&vio);
223
224 return ret;
225}
226
227//ll
Note: See TracBrowser for help on using the repository browser.