Debian Codegraph @

rptp/rptp.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
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("open Connect to a server.\n");
536 printf("pause Pause sounds that are playing.\n");
537 printf("play Play sounds\n");
538 printf("put Send a sound.\n");
539 printf("quit Terminate the rptp session.\n");
540 printf("reset Tell the server to reset itself.\n");
541 printf("set Change server settings.\n");
542 printf("skip Skip sounds in a sound list.\n");
543 printf("status Display server statistics.\n");
544 printf("stop Stop sounds that are playing.\n");
545 printf("version Display the version of the server.\n");
546 printf("volume Get and set the volume of the audio device.\n");
547 printf("wait Wait for a spool id, volume change, or command execution.\n");
548 }
549 }
550
551 #ifdef __STDC__
552 void
553 command_list(int argc, char **argv)
554 #else
555 void
556 command_list(argc, argv)
557 int argc;
558 char **argv;
559 #endif
560 {
561 int n;
562
563 if (!connected())
564 {
565 return;
566 }
567
568 argv_to_command(argv);
569
570 switch (rptp_command(rptp_fd, command, response, sizeof(response)))
571 {
572 case -1:
573 rptp_perror(argv[0]);
574 command_close(argc, argv);
575 return;
576
577 case 1:
578 do_error(response);
579 return;
580
581 case 0:
582 if (raw)
583 {
584 printf("%s\n", response);
585 }
586 break;
587 }
588
589 for (;;)
590 {
591 n = rptp_getline(rptp_fd, rptp_buf, sizeof(rptp_buf));
592 if (n < 0)
593 {
594 rptp_perror("list");
595 command_close(argc, argv);
596 break;
597 }
598 if (strcmp(rptp_buf, ".") == 0)
599 {
600 if (raw)
601 {
602 printf("%s\n", rptp_buf);
603 }
604 break;
605 }
606 printf("%s\n", rptp_buf);
607 }
608 }
609
610 #ifdef __STDC__
611 void
612 command_quit(int argc, char **argv)
613 #else
614 void
615 command_quit(argc, argv)
616 int argc;
617 char **argv;
618 #endif
619 {
620 if (rptp_fd != -1)
621 {
622 rptp_close(rptp_fd);
623 printf("Connection closed.\n");
624 }
625 done(0);
626 }
627
628 #ifdef __STDC__
629 void
630 command_put(int argc, char **argv)
631 #else
632 void
633 command_put(argc, argv)
634 int argc;
635 char **argv;
636 #endif
637 {
638 FILE *fp;
639 int size, n, nwritten;
640 struct stat st;
641 char line[RPTP_MAX_LINE];
642 int total_size;
643
644 if (!connected())
645 {
646 return;
647 }
648
649 if (stat(argv[1], &st) < 0)
650 {
651 perror(argv[1]);
652 return;
653 }
654 fp = fopen(argv[1], "r");
655 if (fp == NULL)
656 {
657 perror(argv[1]);
658 return;
659 }
660 size = st.st_size;
661
662 sprintf(line, "put sound=%s size=%d", argv[1], size);
663
664 switch (rptp_command(rptp_fd, line, response, sizeof(response)))
665 {
666 case -1:
667 rptp_perror(argv[0]);
668 command_close(argc, argv);
669 return;
670
671 case 1:
672 do_error(response);
673 return;
674
675 case 0:
676 break;
677 }
678
679 if (raw)
680 {
681 printf("%s\n", response);
682 }
683
684 total_size = size;
685 while (size > 0)
686 {
687 n = fread(rptp_buf, 1, sizeof(rptp_buf), fp);
688 nwritten = rptp_write(rptp_fd, rptp_buf, n);
689 if (nwritten != n)
690 {
691 rptp_perror("put");
692 command_close(argc, argv);
693 break;
694 }
695 size -= nwritten;
696
697 sprintf(line, "\r%s %d/%d %d%%", argv[1],
698 total_size - size, total_size,
699 (int)(((float)(total_size-size)/total_size)*100));
700 write(2, line, strlen(line));
701 }
702 write(2, "\n", 1);
703 fclose(fp);
704 }
705
706 #ifdef __STDC__
707 void
708 command_get(int argc, char **argv)
709 #else
710 void
711 command_get(argc, argv)
712 int argc;
713 char **argv;
714 #endif
715 {
716 FILE *fp;
717 char *filename;
718 char *p;
719 int size, n, nread, total_size;
720 char line[RPTP_MAX_LINE];
721
722 if (!connected())
723 {
724 return;
725 }
726
727 if (argc == 3)
728 {
729 filename = argv[2];
730 argv[2] = NULL;
731 }
732 else
733 {
734 filename = argv[1];
735 }
736
737 sprintf(line, "get sound=%s", argv[1]);
738
739 switch (rptp_command(rptp_fd, line, response, sizeof(response)))
740 {
741 case -1:
742 rptp_perror(argv[0]);
743 command_close(argc, argv);
744 return;
745
746 case 1:
747 do_error(response);
748 return;
749
750 case 0:
751 break;
752 }
753
754 if (raw)
755 printf("%s\n", response);
756
757 fp = fopen(filename, "w");
758 if (fp == NULL)
759 {
760 perror(filename);
761 return;
762 }
763
764 if (strchr(response, '='))
765 {
766 size = atoi(rptp_parse(response, "size"));
767 }
768 else
769 {
770 p = strtok(response + 1, " ");
771 size = atoi(strtok(NULL, "\r\n"));
772 }
773
774 total_size = size;
775 while (size > 0)
776 {
777 n = MIN(sizeof(rptp_buf), size);
778 nread = rptp_read(rptp_fd, rptp_buf, n);
779 if (nread != n)
780 {
781 rptp_perror("get");
782 break;
783 }
784 fwrite(rptp_buf, 1, n, fp);
785 size -= n;
786
787 sprintf(line, "\r%s %d/%d %d%%", filename,
788 total_size - size, total_size,
789 (int)(((float)(total_size-size)/total_size)*100));
790 write(2, line, strlen(line));
791 }
792 write(2, "\n", 1);
793 fclose(fp);
794 }
795
796 #ifdef __STDC__
797 void
798 command_unknown(int argc, char **argv)
799 #else
800 void
801 command_unknown(argc, argv)
802 int argc;
803 char **argv;
804 #endif
805 {
806 printf("unknown command `%s'.\n", argv[0]);
807 }
808
809 #ifdef __STDC__
810 void
811 command_status(int argc, char **argv)
812 #else
813 void
814 command_status(argc, argv)
815 int argc;
816 char **argv;
817 #endif
818 {
819 argv_to_command(argv);
820
821 if (!connected())
822 {
823 return;
824 }
825
826 switch (rptp_command(rptp_fd, command, response, sizeof(response)))
827 {
828 case -1:
829 rptp_perror(argv[0]);
830 command_close(argc, argv);
831 return;
832
833 case 1:
834 do_error(response);
835 return;
836
837 case 0:
838 break;
839 }
840
841 if (raw)
842 {
843 printf("%s\n", response);
844 }
845 else
846 {
847 int first = 1;
848 char *name, *value;
849
850 while (name = rptp_parse(first ? response : 0, 0))
851 {
852 first = 0;
853 value = rptp_parse(0, name);
854 printf("%s=%s\n", name, value);
855 }
856 }
857 }
858
859 #ifdef __STDC__
860 void
861 command_volume(int argc, char **argv)
862 #else
863 void
864 command_volume(argc, argv)
865 int argc;
866 char **argv;
867 #endif
868 {
869 char *volume;
870
871 if (!connected())
872 {
873 return;
874 }
875
876 if (argc == 2)
877 {
878 sprintf(command, "set volume=%s", argv[1]);
879 }
880 else
881 {
882 sprintf(command, "set volume");
883 }
884
885 switch (rptp_command(rptp_fd, command, response, sizeof(response)))
886 {
887 case -1:
888 rptp_perror(argv[0]);
889 command_close(argc, argv);
890 return;
891
892 case 1:
893 do_error(response);
894 return;
895
896 case 0:
897 break;
898 }
899
900 volume = rptp_parse(response, "volume");
901 if (volume && *volume)
902 {
903 printf("volume=%s\n", volume);
904 }
905 else
906 {
907 printf("unknown response `%s'\n", response);
908 }
909 }
910
911 #ifdef __STDC__
912 void
913 command_skip(int argc, char **argv)
914 #else
915 void
916 command_skip(argc, argv)
917 int argc;
918 char **argv;
919 #endif
920 {
921 char *value;
922
923 if (!connected())
924 {
925 return;
926 }
927
928 if (strchr(command, '='))
929 {
930 /* Leave command alone. */
931 }
932 else if (argc == 3)
933 {
934 sprintf(command, "skip id=%s count=%s", argv[1], argv[2]);
935 }
936 else if (argc == 2)
937 {
938 sprintf(command, "skip id=%s count=1", argv[1]);
939 }
940 else
941 {
942 sprintf(command, "skip id=#0 count=1");
943 }
944
945 switch (rptp_command(rptp_fd, command, response, sizeof(response)))
946 {
947 case -1:
948 rptp_perror(argv[0]);
949 command_close(argc, argv);
950 return;
951
952 case 1:
953 do_error(response);
954 return;
955
956 case 0:
957 break;
958 }
959
960 value = rptp_parse(response, "message");
961 if (value && *value)
962 {
963 printf("%s\n", value);
964 }
965 else
966 {
967 printf("unknown response `%s'\n", response);
968 }
969 }
970
971 #ifdef __STDC__
972 void
973 command_set(int argc, char **argv)
974 #else
975 void
976 command_set(argc, argv)
977 int argc;
978 char **argv;
979 #endif
980 {
981 char *value;
982
983 if (!connected())
984 {
985 return;
986 }
987
988 argv_to_command(argv);
989
990 switch (rptp_command(rptp_fd, command, response, sizeof(response)))
991 {
992 case -1:
993 rptp_perror(argv[0]);
994 command_close(argc, argv);
995 return;
996
997 case 1:
998 do_error(response);
999 return;
1000
1001 case 0:
1002 break;
1003 }
1004 }
1005
1006 #ifdef __STDC__
1007 void
1008 command_monitor(int argc, char **argv)
1009 #else
1010 void
1011 command_monitor(argc, argv)
1012 int argc;
1013 char **argv;
1014 #endif
1015 {
1016 char *value, *p;
1017 char buf[8192];
1018 char line[80];
1019 int n, size;
1020 int sample_rate, precision, channels, sample_size;
1021
1022 if (!connected())
1023 {
1024 return;
1025 }
1026
1027 sprintf(command, "monitor");
1028
1029 switch (rptp_command(rptp_fd, command, response, sizeof(response)))
1030 {
1031 case -1:
1032 rptp_perror(argv[0]);
1033 command_close(argc, argv);
1034 return;
1035
1036 case 1:
1037 do_error(response);
1038 return;
1039
1040 case 0:
1041 break;
1042 }
1043
1044 p = strtok(rptp_parse(response, "audio-info"), ",");
1045 // input_format
1046
1047 p = strtok(NULL, ",");
1048 if (p) sample_rate = atoi(p);
1049
1050 p = strtok(NULL, ",");
1051 if (p) precision = atoi(p);
1052
1053 p = strtok(NULL, ",");
1054 if (p) channels = atoi(p);
1055
1056 sample_size = precision/8 * channels;
1057
1058 fprintf(stderr, "%dhz %dbit %s\n", sample_rate, precision,
1059 channels == 2 ? "stereo" : "mono");
1060
1061 while ((n = read(rptp_fd, buf, sizeof(buf))) > 0)
1062 {
1063 write(1, buf, n);
1064 size += n;
1065 }
1066 close(rptp_fd);
1067 }
1068
1069 void
1070 do_application()
1071 {
1072 sprintf(command, "set application=\"rptp %s\"", RPLAY_VERSION);
1073
1074 switch (rptp_command(rptp_fd, command, response, sizeof(response)))
1075 {
1076 case -1:
1077 rptp_perror("application");
1078 rptp_close(rptp_fd);
1079 rptp_fd = -1;
1080 return;
1081
1082 case 1:
1083 do_error(response);
1084 return;
1085
1086 case 0:
1087 break;
1088 }
1089 }
1090
1091 #ifdef __STDC__
1092 void
1093 do_error(char *response)
1094 #else
1095 void
1096 do_error(response)
1097 char *response;
1098 #endif
1099 {
1100 if (raw)
1101 {
1102 printf("%s\n", response);
1103 }
1104 else
1105 {
1106 char *error;
1107
1108 error = rptp_parse(response, "error");
1109 if (!error || !*error)
1110 {
1111 error = response;
1112 }
1113 printf("%s\n", error);
1114 }
1115 }
1116
1117 void
1118 usage()
1119 {
1120 printf("\nrplay %s\n\n", RPLAY_VERSION);
1121 printf("usage: rptp [options] [command]\n");
1122 printf("--help\n");
1123 printf("\tDisplay helpful information.\n");
1124 printf("\n");
1125
1126 printf("-h HOST, --host=HOST\n");
1127 printf("\tSpecify the RPTP host, default = %s.\n",
1128 rplay_default_host());
1129 printf("\n");
1130
1131 printf("-p PORT, --port=PORT\n");
1132 printf("\tUse PORT instead of the default RPTP port, default = %d.\n", RPTP_PORT);
1133 printf("\n");
1134
1135 printf("-r, --raw\n");
1136 printf("\tEnable raw RPTP mode.\n");
1137 printf("\n");
1138
1139 printf("-v, --version\n");
1140 printf("\tDisplay rplay version information.\n");
1141 }
1142
1143 #ifdef __STDC__
1144 void
1145 done(int exit_value)
1146 #else
1147 void
1148 done(exit_value)
1149 int exit_value;
1150 #endif
1151 {
1152 exit(exit_value);
1153 }