source: roaraudio/roarclients/roarvio.c @ 4886:e9434774f456

Last change on this file since 4886:e9434774f456 was 4886:e9434774f456, checked in by phi, 13 years ago

avoid error message on no-error

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