[4834] | 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 | |
---|
[4836] | 26 | int g_verbose = 0; |
---|
| 27 | #define ROAR_DBG_INFOVAR g_verbose |
---|
| 28 | |
---|
[4834] | 29 | #include <roaraudio.h> |
---|
[4835] | 30 | #include <errno.h> |
---|
| 31 | #include <string.h> |
---|
[4837] | 32 | #include <stdio.h> |
---|
[4835] | 33 | |
---|
| 34 | enum action { |
---|
| 35 | READ, |
---|
[4836] | 36 | WRITE, |
---|
| 37 | EXPLAIN |
---|
[4835] | 38 | }; |
---|
| 39 | |
---|
[4836] | 40 | |
---|
[4835] | 41 | void usage (const char * progname) { |
---|
[4837] | 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"); |
---|
[4835] | 51 | } |
---|
[4834] | 52 | |
---|
[4836] | 53 | int 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 | return 0; |
---|
| 154 | } |
---|
| 155 | |
---|
[4834] | 156 | int main (int argc, char * argv[]) { |
---|
[4835] | 157 | struct roar_vio_defaults def; |
---|
| 158 | struct roar_vio_calls vio; |
---|
| 159 | enum action action = READ; |
---|
| 160 | int i; |
---|
| 161 | char * k; |
---|
| 162 | char * file = NULL; |
---|
| 163 | int o_flags = -1; |
---|
[4836] | 164 | int ret = 0; |
---|
[4835] | 165 | |
---|
| 166 | for (i = 1; i < argc; i++) { |
---|
| 167 | k = argv[i]; |
---|
| 168 | |
---|
| 169 | if ( !strcmp(k, "-h") || !strcmp(k, "--help") ) { |
---|
| 170 | usage(argv[0]); |
---|
| 171 | return 0; |
---|
| 172 | } else if ( !strcmp(k, "--read") ) { |
---|
| 173 | action = READ; |
---|
| 174 | } else if ( !strcmp(k, "--write") ) { |
---|
| 175 | action = WRITE; |
---|
[4836] | 176 | } else if ( !strcmp(k, "--explain") ) { |
---|
| 177 | action = EXPLAIN; |
---|
| 178 | } else if ( !strcmp(k, "--verbose") ) { |
---|
| 179 | g_verbose++; |
---|
[4835] | 180 | } else if ( file == NULL ) { |
---|
| 181 | file = k; |
---|
| 182 | } else { |
---|
| 183 | ROAR_ERR("Too many parameters or unknown parameter: %s", k); |
---|
| 184 | usage(argv[0]); |
---|
| 185 | return 1; |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | if ( file == NULL ) { |
---|
| 190 | usage(argv[0]); |
---|
| 191 | return 1; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | switch (action) { |
---|
| 195 | case READ: |
---|
[4836] | 196 | case EXPLAIN: |
---|
[4835] | 197 | o_flags = O_RDONLY; |
---|
| 198 | break; |
---|
| 199 | case WRITE: |
---|
| 200 | o_flags = O_WRONLY|O_CREAT|O_TRUNC; |
---|
| 201 | break; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | if ( o_flags == -1 ) { |
---|
| 205 | ROAR_ERR("o_flags unset, very bad. This should never happen."); |
---|
| 206 | return 1; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, o_flags, 0644) == -1 ) { |
---|
| 210 | ROAR_ERR("Can not init DSTR defaults. Bad."); |
---|
| 211 | return 1; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | if ( roar_vio_open_dstr(&vio, file, &def, 1) == -1 ) { |
---|
| 215 | ROAR_ERR("Can not open file: %s: %s", file, strerror(errno)); |
---|
| 216 | return 1; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | switch (action) { |
---|
| 220 | case READ: |
---|
| 221 | roar_vio_copy_data(roar_stdout, &vio); |
---|
| 222 | break; |
---|
| 223 | case WRITE: |
---|
| 224 | roar_vio_copy_data(&vio, roar_stdin); |
---|
| 225 | break; |
---|
[4836] | 226 | case EXPLAIN: |
---|
| 227 | if ( do_explain(&vio) == -1 ) |
---|
| 228 | ret = 4; |
---|
| 229 | break; |
---|
[4835] | 230 | } |
---|
| 231 | |
---|
| 232 | roar_vio_close(&vio); |
---|
| 233 | |
---|
[4836] | 234 | return ret; |
---|
[4834] | 235 | } |
---|
| 236 | |
---|
| 237 | //ll |
---|