| 1 |
/* $Id: rptp.c,v 1.6 1999/03/21 00:45:08 boyns Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright (C) 1993-99 Mark R. Boyns <[email protected]> |
| 5 |
* |
| 6 |
* This file is part of rplay. |
| 7 |
* |
| 8 |
* rplay is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* rplay is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with rplay; see the file COPYING. If not, write to the |
| 20 |
* Free Software Foundation, Inc., |
| 21 |
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
#include "config.h" |
| 26 |
#endif |
| 27 |
#include "version.h" |
| 28 |
#include <sys/types.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
#include <stdio.h> |
| 31 |
#ifdef HAVE_STRING_H |
| 32 |
#include <string.h> |
| 33 |
#endif |
| 34 |
#ifdef HAVE_LIBREADLINE |
| 35 |
#include <readline/readline.h> |
| 36 |
#include <readline/history.h> |
| 37 |
#endif /* HAVE_LIBREADLINE */ |
| 38 |
#include "rplay.h" |
| 39 |
#include "getopt.h" |
| 40 |
|
| 41 |
typedef struct |
| 42 |
{ |
| 43 |
char *name; |
| 44 |
int min_args; |
| 45 |
int max_args; |
| 46 |
char *usage; |
| 47 |
#ifdef __STDC__ |
| 48 |
void (*func) (int argc, char **argv); |
| 49 |
#else |
| 50 |
void (*func) (); |
| 51 |
#endif |
| 52 |
} |
| 53 |
COMMAND; |
| 54 |
|
| 55 |
#ifdef __STDC__ |
| 56 |
void command_open(int argc, char **argv); |
| 57 |
void command_close(int argc, char **argv); |
| 58 |
void command_play(int argc, char **argv); |
| 59 |
void command_help(int argc, char **argv); |
| 60 |
void command_list(int argc, char **argv); |
| 61 |
void command_quit(int argc, char **argv); |
| 62 |
void command_put(int argc, char **argv); |
| 63 |
void command_get(int argc, char **argv); |
| 64 |
void command_unknown(int argc, char **argv); |
| 65 |
void command_set(int argc, char **argv); |
| 66 |
void command_status(int argc, char **argv); |
| 67 |
void command_generic(int argc, char **argv); |
| 68 |
void command_volume(int argc, char **argv); |
| 69 |
void command_skip(int argc, char **argv); |
| 70 |
void command_set(int argc, char **argv); |
| 71 |
void command_monitor(int argc, char **argv); |
| 72 |
void argv_to_command(char **argv); |
| 73 |
int connected(); |
| 74 |
void done(int exit_value); |
| 75 |
void usage(); |
| 76 |
void do_application(); |
| 77 |
void do_error(char *repsonse); |
| 78 |
#else |
| 79 |
void command_open( /* int argc, char **argv */ ); |
| 80 |
void command_close( /* int argc, char **argv */ ); |
| 81 |
void command_play( /* int argc, char **argv */ ); |
| 82 |
void command_help( /* int argc, char **argv */ ); |
| 83 |
void command_list( /* int argc, char **argv */ ); |
| 84 |
void command_quit( /* int argc, char **argv */ ); |
| 85 |
void command_put( /* int argc, char **argv */ ); |
| 86 |
void command_get( /* int argc, char **argv */ ); |
| 87 |
void command_unknown( /* int argc, char **argv */ ); |
| 88 |
void command_status( /* int argc, char **argv */ ); |
| 89 |
void command_generic( /* int argc, char **argv */ ); |
| 90 |
void command_volume( /* int argc, char **argv */ ); |
| 91 |
void command_skip( /* int argc, char **argv */ ); |
| 92 |
void command_set( /* int argc, char **argv */ ); |
| 93 |
void command_monitor( /* int argc, char **argv */ ); |
| 94 |
void argv_to_command( /* char **argv */ ); |
| 95 |
int connected(); |
| 96 |
void done( /* int exit_value */ ); |
| 97 |
void usage(); |
| 98 |
void do_application(); |
| 99 |
void do_error( /* char *repsonse */ ); |
| 100 |
#endif |
| 101 |
|
| 102 |
COMMAND commands[] = |
| 103 |
{ |
| 104 |
"access", 0, 0, "", command_generic, |
| 105 |
"close", 0, 0, "", command_close, |
| 106 |
"continue", 1, -1, "#id|sound ...", command_play, |
| 107 |
"find", 1, 1, "sound", command_generic, |
| 108 |
"get", 1, 2, "sound [filename]", command_get, |
| 109 |
"help", 0, 1, "[command]", command_help, |
| 110 |
"info", 1, 1, "sound", command_generic, |
| 111 |
"list", 0, 1, "[connections|hosts|servers|spool|sounds]", command_list, |
| 112 |
"monitor", 0, 0, "", command_monitor, |
| 113 |
"open", 1, 2, "hostname [port]", command_open, |
| 114 |
"pause", 1, -1, "#id|sound ...", command_play, |
| 115 |
"play", 1, -1, "[options] sound ...", command_play, |
| 116 |
"put", 1, 1, "sound", command_put, |
| 117 |
"quit", 0, 0, "", command_quit, |
| 118 |
"reset", 0, 0, "", command_generic, |
| 119 |
"set", 1, -1, "name=value", command_set, |
| 120 |
"skip", 0, 2, "[#id] [[+|-]count]", command_skip, |
| 121 |
"status", 0, 0, "", command_status, |
| 122 |
"stop", 1, -1, "#id|sound ...", command_play, |
| 123 |
"version", 0, 0, "", command_generic, |
| 124 |
"volume", 0, 1, "[[+|-]volume]", command_volume, |
| 125 |
"wait", 1, -1, "#id|command|event-list", command_generic, |
| 126 |
}; |
| 127 |
|
| 128 |
#define NCOMMANDS (sizeof(commands)/sizeof(COMMAND)) |
| 129 |
|
| 130 |
static struct option longopts[] = |
| 131 |
{ |
| 132 |
{"help", no_argument, NULL, 1}, |
| 133 |
{"host", required_argument, NULL, 'h'}, |
| 134 |
{"port", required_argument, NULL, 'p'}, |
| 135 |
{"prompt", required_argument, NULL, 2}, |
| 136 |
{"raw", no_argument, NULL, 'r'}, |
| 137 |
{"version", no_argument, NULL, 'v'}, |
| 138 |
{NULL, 0, NULL, 0} |
| 139 |
}; |
| 140 |
|
| 141 |
int rptp_fd = -1; |
| 142 |
char rptp_buf[4096]; |
| 143 |
char command[RPTP_MAX_LINE]; |
| 144 |
char response[RPTP_MAX_LINE]; |
| 145 |
char *prompt = "rptp> "; |
| 146 |
int interactive = 1; |
| 147 |
int raw = 0; |
| 148 |
|
| 149 |
extern int optind; |
| 150 |
extern char *optarg; |
| 151 |
|
| 152 |
#ifdef __STDC__ |
| 153 |
main(int argc, char **argv) |
| 154 |
#else |
| 155 |
main(argc, argv) |
| 156 |
int argc; |
| 157 |
char **argv; |
| 158 |
#endif |
| 159 |
{ |
| 160 |
char buf[256]; |
| 161 |
int i; |
| 162 |
int port = RPTP_PORT; |
| 163 |
int c; |
| 164 |
char *av[RPTP_MAX_ARGS], *p, *host = NULL; |
| 165 |
int ac, first; |
| 166 |
char numeric_string[128]; |
| 167 |
|
| 168 |
while ((c = getopt_long(argc, argv, "+h:p:rv", longopts, 0)) != -1) |
| 169 |
{ |
| 170 |
switch (c) |
| 171 |
{ |
| 172 |
case 0: |
| 173 |
/* getopt has processed a long-named option -- do nothing */ |
| 174 |
break; |
| 175 |
|
| 176 |
case 1: /* --help */ |
| 177 |
usage(); |
| 178 |
done(0); |
| 179 |
|
| 180 |
case 2: /* --prompt */ |
| 181 |
prompt = optarg; |
| 182 |
break; |
| 183 |
|
| 184 |
case 'h': |
| 185 |
host = optarg; |
| 186 |
break; |
| 187 |
|
| 188 |
case 'p': |
| 189 |
port = atoi(optarg); |
| 190 |
break; |
| 191 |
|
| 192 |
case 'r': |
| 193 |
raw++; |
| 194 |
break; |
| 195 |
|
| 196 |
case 'v': |
| 197 |
printf("rplay %s\n", RPLAY_VERSION); |
| 198 |
done(0); |
| 199 |
|
| 200 |
default: |
| 201 |
fprintf(stderr, "Try `rptp --help' for more information.\n"); |
| 202 |
exit(1); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
if (optind != argc) |
| 207 |
{ |
| 208 |
interactive = 0; |
| 209 |
} |
| 210 |
|
| 211 |
if (host == NULL) |
| 212 |
{ |
| 213 |
host = rplay_default_host(); |
| 214 |
} |
| 215 |
|
| 216 |
rptp_fd = rptp_open(host, port, response, sizeof(response)); |
| 217 |
if (rptp_fd < 0) |
| 218 |
{ |
| 219 |
rptp_perror("open"); |
| 220 |
} |
| 221 |
else if (interactive) |
| 222 |
{ |
| 223 |
if (raw) |
| 224 |
{ |
| 225 |
printf("%s\n", response); |
| 226 |
} |
| 227 |
else |
| 228 |
{ |
| 229 |
if (strchr(response, '=')) |
| 230 |
{ |
| 231 |
rptp_parse(response, 0); |
| 232 |
printf("%s rplayd %s connected\n", |
| 233 |
rptp_parse(0, "host"), |
| 234 |
rptp_parse(0, "version")); |
| 235 |
} |
| 236 |
else |
| 237 |
{ |
| 238 |
printf("Connected to %s port %d.\n", host, port); |
| 239 |
printf("%s\n", response + 1); |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
do_application(); |
| 245 |
|
| 246 |
do |
| 247 |
{ |
| 248 |
if (interactive) |
| 249 |
{ |
| 250 |
if (!raw) |
| 251 |
{ |
| 252 |
#ifdef HAVE_LIBREADLINE |
| 253 |
p = readline(prompt); |
| 254 |
if (!p) |
| 255 |
{ |
| 256 |
done(0); |
| 257 |
} |
| 258 |
add_history(p); |
| 259 |
strcpy(buf, p); |
| 260 |
#else |
| 261 |
printf(prompt); |
| 262 |
fflush(stdout); |
| 263 |
#endif |
| 264 |
} |
| 265 |
|
| 266 |
#ifndef HAVE_LIBREADLINE |
| 267 |
if (fgets(buf, sizeof(buf), stdin) == NULL) |
| 268 |
{ |
| 269 |
done(0); |
| 270 |
} |
| 271 |
#endif |
| 272 |
first = 1; |
| 273 |
ac = 0; |
| 274 |
while ((p = strtok(first ? buf : NULL, " \t\r\n"))) |
| 275 |
{ |
| 276 |
av[ac++] = p; |
| 277 |
first = 0; |
| 278 |
} |
| 279 |
av[ac] = NULL; |
| 280 |
} |
| 281 |
else |
| 282 |
{ |
| 283 |
int i; |
| 284 |
|
| 285 |
ac = 0; |
| 286 |
for (i = optind; i < argc; i++) |
| 287 |
{ |
| 288 |
av[ac++] = argv[i]; |
| 289 |
} |
| 290 |
av[ac] = NULL; |
| 291 |
} |
| 292 |
|
| 293 |
if (av[0] == NULL) |
| 294 |
{ |
| 295 |
continue; |
| 296 |
} |
| 297 |
|
| 298 |
for (i = 0; i < NCOMMANDS; i++) |
| 299 |
{ |
| 300 |
if (strcasecmp(commands[i].name, av[0]) == 0) |
| 301 |
{ |
| 302 |
if ((commands[i].min_args >= 0 && ac - 1 < commands[i].min_args) |
| 303 |
|| (commands[i].max_args >= 0 && ac - 1 > commands[i].max_args)) |
| 304 |
{ |
| 305 |
printf("Usage: %s %s\n", commands[i].name, commands[i].usage); |
| 306 |
} |
| 307 |
else |
| 308 |
{ |
| 309 |
(*commands[i].func) (ac, av); |
| 310 |
} |
| 311 |
break; |
| 312 |
} |
| 313 |
} |
| 314 |
if (i == NCOMMANDS) |
| 315 |
{ |
| 316 |
command_unknown(ac, av); |
| 317 |
} |
| 318 |
} |
| 319 |
while (interactive); |
| 320 |
|
| 321 |
done(0); |
| 322 |
} |
| 323 |
|
| 324 |
#ifdef __STDC__ |
| 325 |
void |
| 326 |
argv_to_command(char **argv) |
| 327 |
#else |
| 328 |
void |
| 329 |
argv_to_command(argv) |
| 330 |
char **argv; |
| 331 |
#endif |
| 332 |
{ |
| 333 |
command[0] = '\0'; |
| 334 |
for (; *argv; argv++) |
| 335 |
{ |
| 336 |
strcat(command, *argv); |
| 337 |
if (*(argv + 1)) |
| 338 |
{ |
| 339 |
strcat(command, " "); |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
int |
| 345 |
connected() |
| 346 |
{ |
| 347 |
if (rptp_fd != -1) |
| 348 |
{ |
| 349 |
return 1; |
| 350 |
} |
| 351 |
else |
| 352 |
{ |
| 353 |
printf("You're not connected, use `open' first.\n"); |
| 354 |
return 0; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
#ifdef __STDC__ |
| 359 |
void |
| 360 |
command_open(int argc, char **argv) |
| 361 |
#else |
| 362 |
void |
| 363 |
command_open(argc, argv) |
| 364 |
int argc; |
| 365 |
char **argv; |
| 366 |
#endif |
| 367 |
{ |
| 368 |
int port; |
| 369 |
|
| 370 |
if (rptp_fd != -1) |
| 371 |
{ |
| 372 |
printf("You're already connected, use `close' first.\n"); |
| 373 |
} |
| 374 |
else |
| 375 |
{ |
| 376 |
port = argc == 3 ? atoi(argv[2]) : RPTP_PORT; |
| 377 |
rptp_fd = rptp_open(argv[1], port, response, sizeof(response)); |
| 378 |
if (rptp_fd < 0) |
| 379 |
{ |
| 380 |
rptp_perror("open"); |
| 381 |
} |
| 382 |
else |
| 383 |
{ |
| 384 |
if (raw) |
| 385 |
{ |
| 386 |
printf("%s\n", response); |
| 387 |
} |
| 388 |
else |
| 389 |
{ |
| 390 |
if (strchr(response, '=')) |
| 391 |
{ |
| 392 |
printf("%s rplayd %s connected\n", |
| 393 |
rptp_parse(response, "host"), |
| 394 |
rptp_parse(0, "version")); |
| 395 |
} |
| 396 |
else |
| 397 |
{ |
| 398 |
printf("Connected to %s port %d.\n", argv[1], port); |
| 399 |
printf("%s\n", response + 1); |
| 400 |
} |
| 401 |
} |
| 402 |
do_application(); |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
#ifdef __STDC__ |
| 408 |
void |
| 409 |
command_close(int argc, char **argv) |
| 410 |
#else |
| 411 |
void |
| 412 |
command_close(argc, argv) |
| 413 |
int argc; |
| 414 |
char **argv; |
| 415 |
#endif |
| 416 |
{ |
| 417 |
if (connected()) |
| 418 |
{ |
| 419 |
rptp_close(rptp_fd); |
| 420 |
rptp_fd = -1; |
| 421 |
if (interactive) |
| 422 |
{ |
| 423 |
printf("Connection closed.\n"); |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
#ifdef __STDC__ |
| 429 |
void |
| 430 |
command_play(int argc, char **argv) |
| 431 |
#else |
| 432 |
void |
| 433 |
command_play(argc, argv) |
| 434 |
int argc; |
| 435 |
char **argv; |
| 436 |
#endif |
| 437 |
{ |
| 438 |
argv_to_command(argv); |
| 439 |
|
| 440 |
if (!connected()) |
| 441 |
{ |
| 442 |
return; |
| 443 |
} |
| 444 |
|
| 445 |
switch (rptp_command(rptp_fd, command, response, sizeof(response))) |
| 446 |
{ |
| 447 |
case -1: |
| 448 |
rptp_perror(argv[0]); |
| 449 |
command_close(argc, argv); |
| 450 |
return; |
| 451 |
|
| 452 |
case 1: |
| 453 |
do_error(response); |
| 454 |
return; |
| 455 |
|
| 456 |
case 0: |
| 457 |
break; |
| 458 |
} |
| 459 |
|
| 460 |
if (raw) |
| 461 |
{ |
| 462 |
printf("%s\n", response); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
#ifdef __STDC__ |
| 467 |
void |
| 468 |
command_generic(int argc, char **argv) |
| 469 |
#else |
| 470 |
void |
| 471 |
command_generic(argc, argv) |
| 472 |
int argc; |
| 473 |
char **argv; |
| 474 |
#endif |
| 475 |
{ |
| 476 |
argv_to_command(argv); |
| 477 |
|
| 478 |
if (!connected()) |
| 479 |
{ |
| 480 |
return; |
| 481 |
} |
| 482 |
|
| 483 |
switch (rptp_command(rptp_fd, command, response, sizeof(response))) |
| 484 |
{ |
| 485 |
case -1: |
| 486 |
rptp_perror(argv[0]); |
| 487 |
command_close(argc, argv); |
| 488 |
return; |
| 489 |
|
| 490 |
case 1: |
| 491 |
do_error(response); |
| 492 |
return; |
| 493 |
|
| 494 |
case 0: |
| 495 |
break; |
| 496 |
} |
| 497 |
|
| 498 |
printf("%s\n", raw ? response : response + 1); |
| 499 |
} |
| 500 |
|
| 501 |
#ifdef __STDC__ |
| 502 |
void |
| 503 |
command_help(int argc, char **argv) |
| 504 |
#else |
| 505 |
void |
| 506 |
command_help(argc, argv) |
| 507 |
int argc; |
| 508 |
char **argv; |
| 509 |
#endif |
| 510 |
{ |
| 511 |
int i; |
| 512 |
|
| 513 |
if (argc == 2) |
| 514 |
{ |
| 515 |
for (i = 0; i < NCOMMANDS; i++) |
| 516 |
{ |
| 517 |
if (strcasecmp(commands[i].name, argv[1]) == 0) |
| 518 |
{ |
| 519 |
printf("Usage: %s %s\n", commands[i].name, commands[i].usage); |
| 520 |
return; |
| 521 |
} |
| 522 |
} |
| 523 |
command_unknown(argc, argv); |
| 524 |
} |
| 525 |
else |
| 526 |
{ |
| 527 |
printf("access Display remote access permissions.\n"); |
| 528 |
printf("close Close the current server connection.\n"); |
| 529 |
printf("continue Continue paused sounds.\n"); |
| 530 |
printf("find Search for a sound.\n"); |
| 531 |
printf("get Retrieve a sound.\n"); |
| 532 |
printf("help Display help information.\n"); |
| 533 |
printf("info Display sound information.\n"); |
| 534 |
printf("list Display various server information.\n"); |
| 535 |
printf("monitor Monitor the audio output of rplayd.\n"); |
| 536 |
printf(" Requires \"m\" permission in rplay.hosts\n"); |
| 537 |
printf("open Connect to a server.\n"); |
| 538 |
printf("pause Pause sounds that are playing.\n"); |
| 539 |
printf("play Play sounds\n"); |
| 540 |
printf("put Send a sound.\n"); |
| 541 |
printf("quit Terminate the rptp session.\n"); |
| 542 |
printf("reset Tell the server to reset itself.\n"); |
| 543 |
printf("set Change server settings.\n"); |
| 544 |
printf("skip Skip sounds in a sound list.\n"); |
| 545 |
printf("status Display server statistics.\n"); |
| 546 |
printf("stop Stop sounds that are playing.\n"); |
| 547 |
printf("version Display the version of the server.\n"); |
| 548 |
printf("volume Get and set the volume of the audio device.\n"); |
| 549 |
printf("wait Wait for a spool id, volume change, or command execution.\n"); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
#ifdef __STDC__ |
| 554 |
void |
| 555 |
command_list(int argc, char **argv) |
| 556 |
#else |
| 557 |
void |
| 558 |
command_list(argc, argv) |
| 559 |
int argc; |
| 560 |
char **argv; |
| 561 |
#endif |
| 562 |
{ |
| 563 |
int n; |
| 564 |
|
| 565 |
if (!connected()) |
| 566 |
{ |
| 567 |
return; |
| 568 |
} |
| 569 |
|
| 570 |
argv_to_command(argv); |
| 571 |
|
| 572 |
switch (rptp_command(rptp_fd, command, response, sizeof(response))) |
| 573 |
{ |
| 574 |
case -1: |
| 575 |
rptp_perror(argv[0]); |
| 576 |
command_close(argc, argv); |
| 577 |
return; |
| 578 |
|
| 579 |
case 1: |
| 580 |
do_error(response); |
| 581 |
return; |
| 582 |
|
| 583 |
case 0: |
| 584 |
if (raw) |
| 585 |
{ |
| 586 |
printf("%s\n", response); |
| 587 |
} |
| 588 |
break; |
| 589 |
} |
| 590 |
|
| 591 |
for (;;) |
| 592 |
{ |
| 593 |
n = rptp_getline(rptp_fd, rptp_buf, sizeof(rptp_buf)); |
| 594 |
if (n < 0) |
| 595 |
{ |
| 596 |
rptp_perror("list"); |
| 597 |
command_close(argc, argv); |
| 598 |
break; |
| 599 |
} |
| 600 |
if (strcmp(rptp_buf, ".") == 0) |
| 601 |
{ |
| 602 |
if (raw) |
| 603 |
{ |
| 604 |
printf("%s\n", rptp_buf); |
| 605 |
} |
| 606 |
break; |
| 607 |
} |
| 608 |
printf("%s\n", rptp_buf); |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
#ifdef __STDC__ |
| 613 |
void |
| 614 |
command_quit(int argc, char **argv) |
| 615 |
#else |
| 616 |
void |
| 617 |
command_quit(argc, argv) |
| 618 |
int argc; |
| 619 |
char **argv; |
| 620 |
#endif |
| 621 |
{ |
| 622 |
if (rptp_fd != -1) |
| 623 |
{ |
| 624 |
rptp_close(rptp_fd); |
| 625 |
printf("Connection closed.\n"); |
| 626 |
} |
| 627 |
done(0); |
| 628 |
} |
| 629 |
|
| 630 |
#ifdef __STDC__ |
| 631 |
void |
| 632 |
command_put(int argc, char **argv) |
| 633 |
#else |
| 634 |
void |
| 635 |
command_put(argc, argv) |
| 636 |
int argc; |
| 637 |
char **argv; |
| 638 |
#endif |
| 639 |
{ |
| 640 |
FILE *fp; |
| 641 |
int size, n, nwritten; |
| 642 |
struct stat st; |
| 643 |
char line[RPTP_MAX_LINE]; |
| 644 |
int total_size; |
| 645 |
|
| 646 |
if (!connected()) |
| 647 |
{ |
| 648 |
return; |
| 649 |
} |
| 650 |
|
| 651 |
if (stat(argv[1], &st) < 0) |
| 652 |
{ |
| 653 |
perror(argv[1]); |
| 654 |
return; |
| 655 |
} |
| 656 |
fp = fopen(argv[1], "r"); |
| 657 |
if (fp == NULL) |
| 658 |
{ |
| 659 |
perror(argv[1]); |
| 660 |
return; |
| 661 |
} |
| 662 |
size = st.st_size; |
| 663 |
|
| 664 |
sprintf(line, "put sound=%s size=%d", argv[1], size); |
| 665 |
|
| 666 |
switch (rptp_command(rptp_fd, line, response, sizeof(response))) |
| 667 |
{ |
| 668 |
case -1: |
| 669 |
rptp_perror(argv[0]); |
| 670 |
command_close(argc, argv); |
| 671 |
return; |
| 672 |
|
| 673 |
case 1: |
| 674 |
do_error(response); |
| 675 |
return; |
| 676 |
|
| 677 |
case 0: |
| 678 |
break; |
| 679 |
} |
| 680 |
|
| 681 |
if (raw) |
| 682 |
{ |
| 683 |
printf("%s\n", response); |
| 684 |
} |
| 685 |
|
| 686 |
total_size = size; |
| 687 |
while (size > 0) |
| 688 |
{ |
| 689 |
n = fread(rptp_buf, 1, sizeof(rptp_buf), fp); |
| 690 |
nwritten = rptp_write(rptp_fd, rptp_buf, n); |
| 691 |
if (nwritten != n) |
| 692 |
{ |
| 693 |
rptp_perror("put"); |
| 694 |
command_close(argc, argv); |
| 695 |
break; |
| 696 |
} |
| 697 |
size -= nwritten; |
| 698 |
|
| 699 |
sprintf(line, "\r%s %d/%d %d%%", argv[1], |
| 700 |
total_size - size, total_size, |
| 701 |
(int)(((float)(total_size-size)/total_size)*100)); |
| 702 |
write(2, line, strlen(line)); |
| 703 |
} |
| 704 |
write(2, "\n", 1); |
| 705 |
fclose(fp); |
| 706 |
} |
| 707 |
|
| 708 |
#ifdef __STDC__ |
| 709 |
void |
| 710 |
command_get(int argc, char **argv) |
| 711 |
#else |
| 712 |
void |
| 713 |
command_get(argc, argv) |
| 714 |
int argc; |
| 715 |
char **argv; |
| 716 |
#endif |
| 717 |
{ |
| 718 |
FILE *fp; |
| 719 |
char *filename; |
| 720 |
char *p; |
| 721 |
int size, n, nread, total_size; |
| 722 |
char line[RPTP_MAX_LINE]; |
| 723 |
|
| 724 |
if (!connected()) |
| 725 |
{ |
| 726 |
return; |
| 727 |
} |
| 728 |
|
| 729 |
if (argc == 3) |
| 730 |
{ |
| 731 |
filename = argv[2]; |
| 732 |
argv[2] = NULL; |
| 733 |
} |
| 734 |
else |
| 735 |
{ |
| 736 |
filename = argv[1]; |
| 737 |
} |
| 738 |
|
| 739 |
sprintf(line, "get sound=%s", argv[1]); |
| 740 |
|
| 741 |
switch (rptp_command(rptp_fd, line, response, sizeof(response))) |
| 742 |
{ |
| 743 |
case -1: |
| 744 |
rptp_perror(argv[0]); |
| 745 |
command_close(argc, argv); |
| 746 |
return; |
| 747 |
|
| 748 |
case 1: |
| 749 |
do_error(response); |
| 750 |
return; |
| 751 |
|
| 752 |
case 0: |
| 753 |
break; |
| 754 |
} |
| 755 |
|
| 756 |
if (raw) |
| 757 |
printf("%s\n", response); |
| 758 |
|
| 759 |
fp = fopen(filename, "w"); |
| 760 |
if (fp == NULL) |
| 761 |
{ |
| 762 |
perror(filename); |
| 763 |
return; |
| 764 |
} |
| 765 |
|
| 766 |
if (strchr(response, '=')) |
| 767 |
{ |
| 768 |
size = atoi(rptp_parse(response, "size")); |
| 769 |
} |
| 770 |
else |
| 771 |
{ |
| 772 |
p = strtok(response + 1, " "); |
| 773 |
size = atoi(strtok(NULL, "\r\n")); |
| 774 |
} |
| 775 |
|
| 776 |
total_size = size; |
| 777 |
while (size > 0) |
| 778 |
{ |
| 779 |
n = MIN(sizeof(rptp_buf), size); |
| 780 |
nread = rptp_read(rptp_fd, rptp_buf, n); |
| 781 |
if (nread != n) |
| 782 |
{ |
| 783 |
rptp_perror("get"); |
| 784 |
break; |
| 785 |
} |
| 786 |
fwrite(rptp_buf, 1, n, fp); |
| 787 |
size -= n; |
| 788 |
|
| 789 |
sprintf(line, "\r%s %d/%d %d%%", filename, |
| 790 |
total_size - size, total_size, |
| 791 |
(int)(((float)(total_size-size)/total_size)*100)); |
| 792 |
write(2, line, strlen(line)); |
| 793 |
} |
| 794 |
write(2, "\n", 1); |
| 795 |
fclose(fp); |
| 796 |
} |
| 797 |
|
| 798 |
#ifdef __STDC__ |
| 799 |
void |
| 800 |
command_unknown(int argc, char **argv) |
| 801 |
#else |
| 802 |
void |
| 803 |
command_unknown(argc, argv) |
| 804 |
int argc; |
| 805 |
char **argv; |
| 806 |
#endif |
| 807 |
{ |
| 808 |
printf("unknown command `%s'.\n", argv[0]); |
| 809 |
} |
| 810 |
|
| 811 |
#ifdef __STDC__ |
| 812 |
void |
| 813 |
command_status(int argc, char **argv) |
| 814 |
#else |
| 815 |
void |
| 816 |
command_status(argc, argv) |
| 817 |
int argc; |
| 818 |
char **argv; |
| 819 |
#endif |
| 820 |
{ |
| 821 |
argv_to_command(argv); |
| 822 |
|
| 823 |
if (!connected()) |
| 824 |
{ |
| 825 |
return; |
| 826 |
} |
| 827 |
|
| 828 |
switch (rptp_command(rptp_fd, command, response, sizeof(response))) |
| 829 |
{ |
| 830 |
case -1: |
| 831 |
rptp_perror(argv[0]); |
| 832 |
command_close(argc, argv); |
| 833 |
return; |
| 834 |
|
| 835 |
case 1: |
| 836 |
do_error(response); |
| 837 |
return; |
| 838 |
|
| 839 |
case 0: |
| 840 |
break; |
| 841 |
} |
| 842 |
|
| 843 |
if (raw) |
| 844 |
{ |
| 845 |
printf("%s\n", response); |
| 846 |
} |
| 847 |
else |
| 848 |
{ |
| 849 |
int first = 1; |
| 850 |
char *name, *value; |
| 851 |
|
| 852 |
while (name = rptp_parse(first ? response : 0, 0)) |
| 853 |
{ |
| 854 |
first = 0; |
| 855 |
value = rptp_parse(0, name); |
| 856 |
printf("%s=%s\n", name, value); |
| 857 |
} |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
#ifdef __STDC__ |
| 862 |
void |
| 863 |
command_volume(int argc, char **argv) |
| 864 |
#else |
| 865 |
void |
| 866 |
command_volume(argc, argv) |
| 867 |
int argc; |
| 868 |
char **argv; |
| 869 |
#endif |
| 870 |
{ |
| 871 |
char *volume; |
| 872 |
|
| 873 |
if (!connected()) |
| 874 |
{ |
| 875 |
return; |
| 876 |
} |
| 877 |
|
| 878 |
if (argc == 2) |
| 879 |
{ |
| 880 |
sprintf(command, "set volume=%s", argv[1]); |
| 881 |
} |
| 882 |
else |
| 883 |
{ |
| 884 |
sprintf(command, "set volume"); |
| 885 |
} |
| 886 |
|
| 887 |
switch (rptp_command(rptp_fd, command, response, sizeof(response))) |
| 888 |
{ |
| 889 |
case -1: |
| 890 |
rptp_perror(argv[0]); |
| 891 |
command_close(argc, argv); |
| 892 |
return; |
| 893 |
|
| 894 |
case 1: |
| 895 |
do_error(response); |
| 896 |
return; |
| 897 |
|
| 898 |
case 0: |
| 899 |
break; |
| 900 |
} |
| 901 |
|
| 902 |
volume = rptp_parse(response, "volume"); |
| 903 |
if (volume && *volume) |
| 904 |
{ |
| 905 |
printf("volume=%s\n", volume); |
| 906 |
} |
| 907 |
else |
| 908 |
{ |
| 909 |
printf("unknown response `%s'\n", response); |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
#ifdef __STDC__ |
| 914 |
void |
| 915 |
command_skip(int argc, char **argv) |
| 916 |
#else |
| 917 |
void |
| 918 |
command_skip(argc, argv) |
| 919 |
int argc; |
| 920 |
char **argv; |
| 921 |
#endif |
| 922 |
{ |
| 923 |
char *value; |
| 924 |
|
| 925 |
if (!connected()) |
| 926 |
{ |
| 927 |
return; |
| 928 |
} |
| 929 |
|
| 930 |
if (strchr(command, '=')) |
| 931 |
{ |
| 932 |
/* Leave command alone. */ |
| 933 |
} |
| 934 |
else if (argc == 3) |
| 935 |
{ |
| 936 |
sprintf(command, "skip id=%s count=%s", argv[1], argv[2]); |
| 937 |
} |
| 938 |
else if (argc == 2) |
| 939 |
{ |
| 940 |
sprintf(command, "skip id=%s count=1", argv[1]); |
| 941 |
} |
| 942 |
else |
| 943 |
{ |
| 944 |
sprintf(command, "skip id=#0 count=1"); |
| 945 |
} |
| 946 |
|
| 947 |
switch (rptp_command(rptp_fd, command, response, sizeof(response))) |
| 948 |
{ |
| 949 |
case -1: |
| 950 |
rptp_perror(argv[0]); |
| 951 |
command_close(argc, argv); |
| 952 |
return; |
| 953 |
|
| 954 |
case 1: |
| 955 |
do_error(response); |
| 956 |
return; |
| 957 |
|
| 958 |
case 0: |
| 959 |
break; |
| 960 |
} |
| 961 |
|
| 962 |
value = rptp_parse(response, "message"); |
| 963 |
if (value && *value) |
| 964 |
{ |
| 965 |
printf("%s\n", value); |
| 966 |
} |
| 967 |
else |
| 968 |
{ |
| 969 |
printf("unknown response `%s'\n", response); |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
#ifdef __STDC__ |
| 974 |
void |
| 975 |
command_set(int argc, char **argv) |
| 976 |
#else |
| 977 |
void |
| 978 |
command_set(argc, argv) |
| 979 |
int argc; |
| 980 |
char **argv; |
| 981 |
#endif |
| 982 |
{ |
| 983 |
char *value; |
| 984 |
|
| 985 |
if (!connected()) |
| 986 |
{ |
| 987 |
return; |
| 988 |
} |
| 989 |
|
| 990 |
argv_to_command(argv); |
| 991 |
|
| 992 |
switch (rptp_command(rptp_fd, command, response, sizeof(response))) |
| 993 |
{ |
| 994 |
case -1: |
| 995 |
rptp_perror(argv[0]); |
| 996 |
command_close(argc, argv); |
| 997 |
return; |
| 998 |
|
| 999 |
case 1: |
| 1000 |
do_error(response); |
| 1001 |
return; |
| 1002 |
|
| 1003 |
case 0: |
| 1004 |
break; |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
#ifdef __STDC__ |
| 1009 |
void |
| 1010 |
command_monitor(int argc, char **argv) |
| 1011 |
#else |
| 1012 |
void |
| 1013 |
command_monitor(argc, argv) |
| 1014 |
int argc; |
| 1015 |
char **argv; |
| 1016 |
#endif |
| 1017 |
{ |
| 1018 |
char *value, *p; |
| 1019 |
char buf[8192]; |
| 1020 |
char line[80]; |
| 1021 |
int n, size; |
| 1022 |
int sample_rate, precision, channels, sample_size; |
| 1023 |
|
| 1024 |
if (!connected()) |
| 1025 |
{ |
| 1026 |
return; |
| 1027 |
} |
| 1028 |
|
| 1029 |
sprintf(command, "monitor"); |
| 1030 |
|
| 1031 |
switch (rptp_command(rptp_fd, command, response, sizeof(response))) |
| 1032 |
{ |
| 1033 |
case -1: |
| 1034 |
rptp_perror(argv[0]); |
| 1035 |
command_close(argc, argv); |
| 1036 |
return; |
| 1037 |
|
| 1038 |
case 1: |
| 1039 |
do_error(response); |
| 1040 |
return; |
| 1041 |
|
| 1042 |
case 0: |
| 1043 |
break; |
| 1044 |
} |
| 1045 |
|
| 1046 |
p = strtok(rptp_parse(response, "audio-info"), ","); |
| 1047 |
// input_format |
| 1048 |
|
| 1049 |
p = strtok(NULL, ","); |
| 1050 |
if (p) sample_rate = atoi(p); |
| 1051 |
|
| 1052 |
p = strtok(NULL, ","); |
| 1053 |
if (p) precision = atoi(p); |
| 1054 |
|
| 1055 |
p = strtok(NULL, ","); |
| 1056 |
if (p) channels = atoi(p); |
| 1057 |
|
| 1058 |
sample_size = precision/8 * channels; |
| 1059 |
|
| 1060 |
fprintf(stderr, "%dhz %dbit %s\n", sample_rate, precision, |
| 1061 |
channels == 2 ? "stereo" : "mono"); |
| 1062 |
|
| 1063 |
while ((n = read(rptp_fd, buf, sizeof(buf))) > 0) |
| 1064 |
{ |
| 1065 |
write(1, buf, n); |
| 1066 |
size += n; |
| 1067 |
} |
| 1068 |
close(rptp_fd); |
| 1069 |
} |
| 1070 |
|
| 1071 |
void |
| 1072 |
do_application() |
| 1073 |
{ |
| 1074 |
sprintf(command, "set application=\"rptp %s\"", RPLAY_VERSION); |
| 1075 |
|
| 1076 |
switch (rptp_command(rptp_fd, command, response, sizeof(response))) |
| 1077 |
{ |
| 1078 |
case -1: |
| 1079 |
rptp_perror("application"); |
| 1080 |
rptp_close(rptp_fd); |
| 1081 |
rptp_fd = -1; |
| 1082 |
return; |
| 1083 |
|
| 1084 |
case 1: |
| 1085 |
do_error(response); |
| 1086 |
return; |
| 1087 |
|
| 1088 |
case 0: |
| 1089 |
break; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
#ifdef __STDC__ |
| 1094 |
void |
| 1095 |
do_error(char *response) |
| 1096 |
#else |
| 1097 |
void |
| 1098 |
do_error(response) |
| 1099 |
char *response; |
| 1100 |
#endif |
| 1101 |
{ |
| 1102 |
if (raw) |
| 1103 |
{ |
| 1104 |
printf("%s\n", response); |
| 1105 |
} |
| 1106 |
else |
| 1107 |
{ |
| 1108 |
char *error; |
| 1109 |
|
| 1110 |
error = rptp_parse(response, "error"); |
| 1111 |
if (!error || !*error) |
| 1112 |
{ |
| 1113 |
error = response; |
| 1114 |
} |
| 1115 |
printf("%s\n", error); |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
void |
| 1120 |
usage() |
| 1121 |
{ |
| 1122 |
printf("\nrplay %s\n\n", RPLAY_VERSION); |
| 1123 |
printf("usage: rptp [options] [command]\n"); |
| 1124 |
printf("--help\n"); |
| 1125 |
printf("\tDisplay helpful information.\n"); |
| 1126 |
printf("\n"); |
| 1127 |
|
| 1128 |
printf("-h HOST, --host=HOST\n"); |
| 1129 |
printf("\tSpecify the RPTP host, default = %s.\n", |
| 1130 |
rplay_default_host()); |
| 1131 |
printf("\n"); |
| 1132 |
|
| 1133 |
printf("-p PORT, --port=PORT\n"); |
| 1134 |
printf("\tUse PORT instead of the default RPTP port, default = %d.\n", RPTP_PORT); |
| 1135 |
printf("\n"); |
| 1136 |
|
| 1137 |
printf("-r, --raw\n"); |
| 1138 |
printf("\tEnable raw RPTP mode.\n"); |
| 1139 |
printf("\n"); |
| 1140 |
|
| 1141 |
printf("-v, --version\n"); |
| 1142 |
printf("\tDisplay rplay version information.\n"); |
| 1143 |
} |
| 1144 |
|
| 1145 |
#ifdef __STDC__ |
| 1146 |
void |
| 1147 |
done(int exit_value) |
| 1148 |
#else |
| 1149 |
void |
| 1150 |
done(exit_value) |
| 1151 |
int exit_value; |
| 1152 |
#endif |
| 1153 |
{ |
| 1154 |
exit(exit_value); |
| 1155 |
} |