1 /* $OpenBSD: cdio.c,v 1.86 2023/03/08 04:43:10 guenther Exp $ */
2
3 /* Copyright (c) 1995 Serge V. Vakulenko
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Serge V. Vakulenko.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Compact Disc Control Utility by Serge V. Vakulenko <vak@cronyx.ru>.
35 * Based on the non-X based CD player by Jean-Marc Zucconi and
36 * Andrey A. Chernov.
37 *
38 * Fixed and further modified on 5-Sep-1995 by Jukka Ukkonen <jau@funet.fi>.
39 *
40 * 11-Sep-1995: Jukka A. Ukkonen <jau@funet.fi>
41 * A couple of further fixes to my own earlier "fixes".
42 *
43 * 18-Sep-1995: Jukka A. Ukkonen <jau@funet.fi>
44 * Added an ability to specify addresses relative to the
45 * beginning of a track. This is in fact a variation of
46 * doing the simple play_msf() call.
47 *
48 * 11-Oct-1995: Serge V.Vakulenko <vak@cronyx.ru>
49 * New eject algorithm.
50 * Some code style reformatting.
51 *
52 * $FreeBSD: cdcontrol.c,v 1.13 1996/06/25 21:01:27 ache Exp $
53 */
54
55 #include <sys/types.h>
56 #include <sys/cdio.h>
57 #include <sys/ioctl.h>
58 #include <sys/queue.h>
59 #include <sys/scsiio.h>
60 #include <sys/stat.h>
61
62 #include <ctype.h>
63 #include <err.h>
64 #include <errno.h>
65 #include <fcntl.h>
66 #include <signal.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <unistd.h>
71 #include <limits.h>
72 #include <histedit.h>
73 #include <util.h>
74 #include <vis.h>
75
76 #include "extern.h"
77
78 #define ASTS_INVALID 0x00 /* Audio status byte not valid */
79 #define ASTS_PLAYING 0x11 /* Audio play operation in progress */
80 #define ASTS_PAUSED 0x12 /* Audio play operation paused */
81 #define ASTS_COMPLETED 0x13 /* Audio play operation successfully completed */
82 #define ASTS_ERROR 0x14 /* Audio play operation stopped due to error */
83 #define ASTS_VOID 0x15 /* No current audio status to return */
84
85 #ifndef DEFAULT_CD_DRIVE
86 # define DEFAULT_CD_DRIVE "cd0"
87 #endif
88
89 #define CMD_DEBUG 1
90 #define CMD_DEVICE 2
91 #define CMD_EJECT 3
92 #define CMD_HELP 4
93 #define CMD_INFO 5
94 #define CMD_PAUSE 6
95 #define CMD_PLAY 7
96 #define CMD_QUIT 8
97 #define CMD_RESUME 9
98 #define CMD_STOP 10
99 #define CMD_VOLUME 11
100 #define CMD_CLOSE 12
101 #define CMD_RESET 13
102 #define CMD_SET 14
103 #define CMD_STATUS 15
104 #define CMD_NEXT 16
105 #define CMD_PREV 17
106 #define CMD_REPLAY 18
107 #define CMD_CDDB 19
108 #define CMD_CDID 20
109 #define CMD_BLANK 21
110 #define CMD_CDRIP 22
111 #define CMD_CDPLAY 23
112
113 struct cmdtab {
114 int command;
115 char *name;
116 int min;
117 char *args;
118 } cmdtab[] = {
119 { CMD_BLANK, "blank", 1, "" },
120 { CMD_CDDB, "cddbinfo", 2, "[n]" },
121 { CMD_CDID, "cdid", 3, "" },
122 { CMD_CDPLAY, "cdplay", 3, "[track1-trackN ...]" },
123 { CMD_CDRIP, "cdrip", 3, "[track1-trackN ...]" },
124 { CMD_CLOSE, "close", 1, "" },
125 { CMD_DEBUG, "debug", 3, "on | off" },
126 { CMD_DEVICE, "device", 1, "devname" },
127 { CMD_EJECT, "eject", 1, "" },
128 { CMD_QUIT, "exit", 2, "" },
129 { CMD_HELP, "?", 1, 0 },
130 { CMD_HELP, "help", 1, "" },
131 { CMD_INFO, "info", 1, "" },
132 { CMD_NEXT, "next", 1, "" },
133 { CMD_PAUSE, "pause", 2, "" },
134 { CMD_PLAY, "play", 1, "[track1[.index1] [track2[.index2]]]" },
135 { CMD_PLAY, "play", 1, "[[tr1] m1:s1[.f1] [tr2] [m2:s2[.f2]]]" },
136 { CMD_PLAY, "play", 1, "[#block [len]]" },
137 { CMD_PREV, "previous", 2, "" },
138 { CMD_QUIT, "quit", 1, "" },
139 { CMD_REPLAY, "replay", 3, "" },
140 { CMD_RESET, "reset", 4, "" },
141 { CMD_RESUME, "resume", 1, "" },
142 { CMD_SET, "set", 2, "lba | msf" },
143 { CMD_STATUS, "status", 1, "" },
144 { CMD_STOP, "stop", 3, "" },
145 { CMD_VOLUME, "volume", 1, "left_channel right_channel" },
146 { CMD_VOLUME, "volume", 1, "left | right | mono | stereo | mute" },
147 { 0, 0, 0, 0}
148 };
149
150 struct cd_toc_entry *toc_buffer;
151
152 struct track_head tracks;
153
154 char *cdname;
155 int fd = -1;
156 int writeperm = 0;
157 u_int8_t mediacap[MMC_FEATURE_MAX / NBBY];
158 int verbose = 1;
159 int msf = 1;
160 const char *cddb_host;
161 char **track_names;
162 volatile sig_atomic_t signo;
163
164 EditLine *el = NULL; /* line-editing structure */
165 History *hist = NULL; /* line-editing history */
166 void switch_el(void);
167
168 extern char *__progname;
169
170 int setvol(int, int);
171 int read_toc_entrys(int);
172 int play_msf(int, int, int, int, int, int);
173 int play_track(int, int, int, int);
174 int status(int *, int *, int *, int *);
175 int is_wave(int);
176 __dead void tao(int argc, char **argv);
177 int play(char *arg);
178 int info(char *arg);
179 int cddbinfo(char *arg);
180 int pstatus(char *arg);
181 int play_next(char *arg);
182 int play_prev(char *arg);
183 int play_same(char *arg);
184 void sigint_handler(int);
185 char *input(int *);
186 char *prompt(void);
187 void prtrack(struct cd_toc_entry *e, int lastflag, char *name);
188 void lba2msf(unsigned long lba, u_char *m, u_char *s, u_char *f);
189 unsigned int msf2lba(u_char m, u_char s, u_char f);
190 int play_blocks(int blk, int len);
191 int run(int cmd, char *arg);
192 char *parse(char *buf, int *cmd);
193 void help(void);
194 void usage(void);
195 char *strstatus(int);
196 int cdid(void);
197 void addmsf(u_int *, u_int *, u_int *, u_char, u_char, u_char);
198 int cmpmsf(u_char, u_char, u_char, u_char, u_char, u_char);
199 void toc2msf(u_int, u_char *, u_char *, u_char *);
200
201 void
help(void)202 help(void)
203 {
204 struct cmdtab *c;
205 char *s, n;
206 int i;
207
208 for (c = cmdtab; c->name; ++c) {
209 if (!c->args)
210 continue;
211 printf("\t");
212 for (i = c->min, s = c->name; *s; s++, i--) {
213 if (i > 0)
214 n = toupper((unsigned char)*s);
215 else
216 n = *s;
217 putchar(n);
218 }
219 if (*c->args)
220 printf(" %s", c->args);
221 printf("\n");
222 }
223 printf("\n\tThe word \"play\" is not required for the play commands.\n");
224 printf("\tThe plain target address is taken as a synonym for play.\n");
225 }
226
227 void
usage(void)228 usage(void)
229 {
230 fprintf(stderr, "usage: %s [-sv] [-d host:port] [-f device] "
231 "[command [arg ...]]\n",
232 __progname);
233 exit(1);
234 }
235
236 int
main(int argc,char ** argv)237 main(int argc, char **argv)
238 {
239 int ch, cmd;
240 char *arg;
241
242 cdname = getenv("DISC");
243 if (!cdname)
244 cdname = getenv("CDROM");
245
246 cddb_host = getenv("CDDB");
247 if (!cddb_host)
248 cddb_host = "gnudb.gnudb.org";
249
250 while ((ch = getopt(argc, argv, "svd:f:")) != -1)
251 switch (ch) {
252 case 's':
253 verbose = 0;
254 break;
255 case 'v':
256 verbose++;
257 break;
258 case 'f':
259 cdname = optarg;
260 break;
261 case 'd':
262 cddb_host = optarg;
263 break;
264 default:
265 usage();
266 }
267
268 argc -= optind;
269 argv += optind;
270
271 if (argc > 0 && ! strcasecmp(*argv, "help"))
272 usage();
273
274 if (!cdname) {
275 cdname = DEFAULT_CD_DRIVE;
276 if (verbose > 1)
277 fprintf(stderr,
278 "No CD device name specified. Defaulting to %s.\n",
279 cdname);
280 }
281
282 if (argc > 0 && !strcasecmp(*argv, "tao")) {
283 tao(argc, argv);
284 /* NOTREACHED */
285 }
286 if (argc > 0) {
287 char buf[80], *p;
288 int len;
289
290 for (p=buf; argc-->0; ++argv) {
291 len = snprintf(p, buf + sizeof buf - p,
292 "%s%s", (p > buf) ? " " : "", *argv);
293
294 if (len < 0 || len >= buf + sizeof buf - p)
295 errx(1, "argument list too long.");
296
297 p += len;
298 }
299 arg = parse(buf, &cmd);
300 return (run(cmd, arg));
301 }
302
303 if (verbose == 1)
304 verbose = isatty(0);
305
306 if (verbose) {
307 printf("Compact Disc Control utility, version %s\n", VERSION);
308 printf("Type `?' for command list\n\n");
309 }
310
311 switch_el();
312
313 for (;;) {
314 arg = input(&cmd);
315 if (run(cmd, arg) < 0) {
316 if (verbose)
317 warn(NULL);
318 close(fd);
319 fd = -1;
320 }
321 fflush(stdout);
322 }
323 }
324
325 int
run(int cmd,char * arg)326 run(int cmd, char *arg)
327 {
328 int l, r, rc;
329 static char newcdname[PATH_MAX];
330
331 switch (cmd) {
332
333 case CMD_QUIT:
334 switch_el();
335 exit(0);
336
337 case CMD_INFO:
338 if (!open_cd(cdname, 0))
339 return (0);
340
341 return info(arg);
342
343 case CMD_CDDB:
344 if (!open_cd(cdname, 0))
345 return (0);
346
347 return cddbinfo(arg);
348
349 case CMD_CDID:
350 if (!open_cd(cdname, 0))
351 return (0);
352 return cdid();
353
354 case CMD_STATUS:
355 if (!open_cd(cdname, 0))
356 return (0);
357
358 return pstatus(arg);
359
360 case CMD_PAUSE:
361 if (!open_cd(cdname, 0))
362 return (0);
363
364 return ioctl(fd, CDIOCPAUSE);
365
366 case CMD_RESUME:
367 if (!open_cd(cdname, 0))
368 return (0);
369
370 return ioctl(fd, CDIOCRESUME);
371
372 case CMD_STOP:
373 if (!open_cd(cdname, 0))
374 return (0);
375
376 rc = ioctl(fd, CDIOCSTOP);
377
378 (void) ioctl(fd, CDIOCALLOW);
379
380 return (rc);
381
382 case CMD_RESET:
383 if (!open_cd(cdname, 0))
384 return (0);
385
386 rc = ioctl(fd, CDIOCRESET);
387 if (rc == -1)
388 return rc;
389 close(fd);
390 fd = -1;
391 return (0);
392
393 case CMD_DEBUG:
394 if (!open_cd(cdname, 0))
395 return (0);
396
397 if (!strcasecmp(arg, "on"))
398 return ioctl(fd, CDIOCSETDEBUG);
399
400 if (!strcasecmp(arg, "off"))
401 return ioctl(fd, CDIOCCLRDEBUG);
402
403 printf("%s: Invalid command arguments\n", __progname);
404
405 return (0);
406
407 case CMD_DEVICE:
408 /* close old device */
409 if (fd > -1) {
410 (void) ioctl(fd, CDIOCALLOW);
411 close(fd);
412 fd = -1;
413 }
414
415 if (strlen(arg) == 0) {
416 printf("%s: Invalid parameter\n", __progname);
417 return (0);
418 }
419
420 /* open new device */
421 if (!open_cd(arg, 0))
422 return (0);
423 (void) strlcpy(newcdname, arg, sizeof(newcdname));
424 cdname = newcdname;
425 return (1);
426
427 case CMD_EJECT:
428 if (!open_cd(cdname, 0))
429 return (0);
430
431 (void) ioctl(fd, CDIOCALLOW);
432 rc = ioctl(fd, CDIOCEJECT);
433 if (rc == -1)
434 return (rc);
435 close(fd);
436 fd = -1;
437 if (track_names)
438 free_names(track_names);
439 track_names = NULL;
440 return (0);
441
442 case CMD_CLOSE:
443 if (!open_cd(cdname, 0))
444 return (0);
445
446 (void) ioctl(fd, CDIOCALLOW);
447 rc = ioctl(fd, CDIOCCLOSE);
448 if (rc == -1)
449 return (rc);
450 close(fd);
451 fd = -1;
452 return (0);
453
454 case CMD_PLAY:
455 if (!open_cd(cdname, 0))
456 return (0);
457
458 while (isspace((unsigned char)*arg))
459 arg++;
460
461 return play(arg);
462
463 case CMD_SET:
464 if (!strcasecmp(arg, "msf"))
465 msf = 1;
466 else if (!strcasecmp(arg, "lba"))
467 msf = 0;
468 else
469 printf("%s: Invalid command arguments\n", __progname);
470 return (0);
471
472 case CMD_VOLUME:
473 if (!open_cd(cdname, 0))
474 return (0);
475
476 if (!strncasecmp(arg, "left", strlen(arg)))
477 return ioctl(fd, CDIOCSETLEFT);
478
479 if (!strncasecmp(arg, "right", strlen(arg)))
480 return ioctl(fd, CDIOCSETRIGHT);
481
482 if (!strncasecmp(arg, "mono", strlen(arg)))
483 return ioctl(fd, CDIOCSETMONO);
484
485 if (!strncasecmp(arg, "stereo", strlen(arg)))
486 return ioctl(fd, CDIOCSETSTEREO);
487
488 if (!strncasecmp(arg, "mute", strlen(arg)))
489 return ioctl(fd, CDIOCSETMUTE);
490
491 if (2 != sscanf(arg, "%d%d", &l, &r)) {
492 printf("%s: Invalid command arguments\n", __progname);
493 return (0);
494 }
495
496 return setvol(l, r);
497
498 case CMD_NEXT:
499 if (!open_cd(cdname, 0))
500 return (0);
501
502 return play_next(arg);
503
504 case CMD_PREV:
505 if (!open_cd(cdname, 0))
506 return (0);
507
508 return play_prev(arg);
509
510 case CMD_REPLAY:
511 if (!open_cd(cdname, 0))
512 return 0;
513
514 return play_same(arg);
515 case CMD_BLANK:
516 if (!open_cd(cdname, 1))
517 return 0;
518
519 if (get_media_capabilities(mediacap, 1) == -1) {
520 warnx("Can't determine media type");
521 return (0);
522 }
523 if (cdio_isset(mediacap, MMC_FEATURE_CDRW_WRITE) == 0 &&
524 get_media_type() != MEDIATYPE_CDRW) {
525 warnx("The media doesn't support blanking");
526 return (0);
527 }
528
529 return blank();
530 case CMD_CDRIP:
531 if (!open_cd(cdname, 0))
532 return (0);
533
534 while (isspace((unsigned char)*arg))
535 arg++;
536
537 return cdrip(arg);
538 case CMD_CDPLAY:
539 if (!open_cd(cdname, 0))
540 return (0);
541
542 while (isspace((unsigned char)*arg))
543 arg++;
544
545 return cdplay(arg);
546 default:
547 case CMD_HELP:
548 help();
549 return (0);
550
551 }
552 }
553
554 /*
555 * Check if audio file has RIFF WAVE format. If not, we assume it's just PCM.
556 */
557 int
is_wave(int fd)558 is_wave(int fd)
559 {
560 char buf[WAVHDRLEN];
561 int rv;
562
563 rv = 0;
564 if (read(fd, buf, sizeof(buf)) == sizeof(buf)) {
565 if (memcmp(buf, "RIFF", 4) == 0 &&
566 memcmp(buf + 8, "WAVE", 4) == 0)
567 rv = 1;
568 }
569
570 return (rv);
571 }
572
573 __dead void
tao(int argc,char ** argv)574 tao(int argc, char **argv)
575 {
576 struct stat sb;
577 struct track_info *cur_track;
578 struct track_info *tr;
579 off_t availblk, needblk = 0;
580 u_int blklen;
581 u_int ntracks = 0;
582 char type;
583 int ch, speed;
584 const char *errstr;
585
586 if (argc == 1)
587 usage();
588
589 SLIST_INIT(&tracks);
590 type = 'd';
591 speed = DRIVE_SPEED_OPTIMAL;
592 blklen = 2048;
593 while (argc > 1) {
594 tr = malloc(sizeof(struct track_info));
595 if (tr == NULL)
596 err(1, "tao");
597
598 optreset = 1;
599 optind = 1;
600 while ((ch = getopt(argc, argv, "ads:")) != -1) {
601 switch (ch) {
602 case 'a':
603 type = 'a';
604 blklen = 2352;
605 break;
606 case 'd':
607 type = 'd';
608 blklen = 2048;
609 break;
610 case 's':
611 if (strcmp(optarg, "auto") == 0) {
612 speed = DRIVE_SPEED_OPTIMAL;
613 } else if (strcmp(optarg, "max") == 0) {
614 speed = DRIVE_SPEED_MAX;
615 } else {
616 speed = (int)strtonum(optarg, 1,
617 CD_MAX_SPEED, &errstr);
618 if (errstr != NULL) {
619 errx(1,
620 "incorrect speed value");
621 }
622 }
623 break;
624 default:
625 usage();
626 /* NOTREACHED */
627 }
628 }
629
630 if (speed != DRIVE_SPEED_OPTIMAL && speed != DRIVE_SPEED_MAX)
631 tr->speed = CD_SPEED_TO_KBPS(speed, blklen);
632 else
633 tr->speed = speed;
634
635 tr->type = type;
636 tr->blklen = blklen;
637 argc -= optind;
638 argv += optind;
639 if (argv[0] == NULL)
640 usage();
641 tr->file = argv[0];
642 tr->fd = open(tr->file, O_RDONLY);
643 if (tr->fd == -1)
644 err(1, "cannot open file %s", tr->file);
645 if (fstat(tr->fd, &sb) == -1)
646 err(1, "cannot stat file %s", tr->file);
647 tr->sz = sb.st_size;
648 tr->off = 0;
649 if (tr->type == 'a') {
650 if (is_wave(tr->fd)) {
651 tr->sz -= WAVHDRLEN;
652 tr->off = WAVHDRLEN;
653 }
654 }
655 if (SLIST_EMPTY(&tracks))
656 SLIST_INSERT_HEAD(&tracks, tr, track_list);
657 else
658 SLIST_INSERT_AFTER(cur_track, tr, track_list);
659 cur_track = tr;
660 }
661
662 if (!open_cd(cdname, 1))
663 exit(1);
664 if (get_media_capabilities(mediacap, 1) == -1)
665 errx(1, "Can't determine media type");
666 if (cdio_isset(mediacap, MMC_FEATURE_CD_TAO) == 0)
667 errx(1, "The media can't be written in TAO mode");
668
669 get_disc_size(&availblk);
670 SLIST_FOREACH(tr, &tracks, track_list) {
671 needblk += tr->sz/tr->blklen;
672 ntracks++;
673 }
674 needblk += (ntracks - 1) * 150; /* transition area between tracks */
675 if (needblk > availblk)
676 errx(1, "Only %llu of the required %llu blocks available",
677 availblk, needblk);
678 if (writetao(&tracks) != 0)
679 exit(1);
680 else
681 exit(0);
682 }
683
684 int
play(char * arg)685 play(char *arg)
686 {
687 struct ioc_toc_header h;
688 unsigned char tm, ts, tf;
689 unsigned int tr1, tr2, m1, m2, s1, s2, f1, f2, i1, i2;
690 unsigned int blk, len, n;
691 char c;
692 int rc;
693
694 rc = ioctl(fd, CDIOREADTOCHEADER, &h);
695
696 if (rc == -1)
697 return (rc);
698
699 if (h.starting_track > h.ending_track) {
700 printf("TOC starting_track > TOC ending_track\n");
701 return (0);
702 }
703
704 n = h.ending_track - h.starting_track + 1;
705 rc = read_toc_entrys((n + 1) * sizeof (struct cd_toc_entry));
706
707 if (rc < 0)
708 return (rc);
709
710 /*
711 * Truncate trailing white space. Then by adding %c to the end of the
712 * sscanf() formats we catch any errant trailing characters.
713 */
714 rc = strlen(arg) - 1;
715 while (rc >= 0 && isspace((unsigned char)arg[rc])) {
716 arg[rc] = '\0';
717 rc--;
718 }
719
720 if (!arg || ! *arg) {
721 /* Play the whole disc */
722 return (play_track(h.starting_track, 1, h.ending_track, 1));
723 }
724
725 if (strchr(arg, '#')) {
726 /* Play block #blk [ len ] */
727 if (2 != sscanf(arg, "#%u%u%c", &blk, &len, &c) &&
728 1 != sscanf(arg, "#%u%c", &blk, &c)) {
729 printf("%s: Invalid command arguments\n", __progname);
730 return (0);
731 }
732
733 if (len == 0) {
734 if (msf)
735 len = msf2lba(toc_buffer[n].addr.msf.minute,
736 toc_buffer[n].addr.msf.second,
737 toc_buffer[n].addr.msf.frame) - blk;
738 else
739 len = toc_buffer[n].addr.lba - blk;
740 }
741 return play_blocks(blk, len);
742 }
743
744 if (strchr(arg, ':') == NULL) {
745 /*
746 * Play track tr1[.i1] [tr2[.i2]]
747 */
748 if (4 == sscanf(arg, "%u.%u%u.%u%c", &tr1, &i1, &tr2, &i2, &c))
749 goto play_track;
750
751 i2 = 1;
752 if (3 == sscanf(arg, "%u.%u%u%c", &tr1, &i1, &tr2, &c))
753 goto play_track;
754
755 i1 = 1;
756 if (3 == sscanf(arg, "%u%u.%u%c", &tr1, &tr2, &i2, &c))
757 goto play_track;
758
759 tr2 = 0;
760 i2 = 1;
761 if (2 == sscanf(arg, "%u.%u%c", &tr1, &i1, &c))
762 goto play_track;
763
764 i1 = i2 = 1;
765 if (2 == sscanf(arg, "%u%u%c", &tr1, &tr2, &c))
766 goto play_track;
767
768 i1 = i2 = 1;
769 tr2 = 0;
770 if (1 == sscanf(arg, "%u%c", &tr1, &c))
771 goto play_track;
772
773 printf("%s: Invalid command arguments\n", __progname);
774 return (0);
775
776 play_track:
777 if (tr1 > n || tr2 > n) {
778 printf("Track number must be between 0 and %u\n", n);
779 return (0);
780 } else if (tr2 == 0)
781 tr2 = h.ending_track;
782
783 if (tr1 > tr2) {
784 printf("starting_track > ending_track\n");
785 return (0);
786 }
787
788 return (play_track(tr1, i1, tr2, i2));
789 }
790
791 /*
792 * Play MSF [tr1] m1:s1[.f1] [tr2] [m2:s2[.f2]]
793 *
794 * Start Time End Time
795 * ---------- --------
796 * tr1 m1:s1.f1 tr2 m2:s2.f2
797 * tr1 m1:s1 tr2 m2:s2.f2
798 * tr1 m1:s1.f1 tr2 m2:s2
799 * tr1 m1:s1 tr2 m2:s2
800 * m1:s1.f1 tr2 m2:s2.f2
801 * m1:s1 tr2 m2:s2.f2
802 * m1:s1.f1 tr2 m2:s2
803 * m1:s1 tr2 m2:s2
804 * tr1 m1:s1.f1 m2:s2.f2
805 * tr1 m1:s1 m2:s2.f2
806 * tr1 m1:s1.f1 m2:s2
807 * tr1 m1:s1 m2:s2
808 * m1:s1.f1 m2:s2.f2
809 * m1:s1 m2:s2.f2
810 * m1:s1.f1 m2:s2
811 * m1:s1 m2:s2
812 * tr1 m1:s1.f1 tr2
813 * tr1 m1:s1 tr2
814 * m1:s1.f1 tr2
815 * m1:s1 tr2
816 * tr1 m1:s1.f1 <end of disc>
817 * tr1 m1:s1 <end of disc>
818 * m1:s1.f1 <end of disc>
819 * m1:s1 <end of disc>
820 */
821
822 /* tr1 m1:s1.f1 tr2 m2:s2.f2 */
823 if (8 == sscanf(arg, "%u%u:%u.%u%u%u:%u.%u%c",
824 &tr1, &m1, &s1, &f1, &tr2, &m2, &s2, &f2, &c))
825 goto play_msf;
826
827 /* tr1 m1:s1 tr2 m2:s2.f2 */
828 f1 = 0;
829 if (7 == sscanf(arg, "%u%u:%u%u%u:%u.%u%c",
830 &tr1, &m1, &s1, &tr2, &m2, &s2, &f2, &c))
831 goto play_msf;
832
833 /* tr1 m1:s1.f1 tr2 m2:s2 */
834 f2 =0;
835 if (7 == sscanf(arg, "%u%u:%u.%u%u%u:%u%c",
836 &tr1, &m1, &s1, &f1, &tr2, &m2, &s2, &c))
837 goto play_msf;
838
839 /* m1:s1.f1 tr2 m2:s2.f2 */
840 tr1 = 0;
841 if (7 == sscanf(arg, "%u:%u.%u%u%u:%u.%u%c",
842 &m1, &s1, &f1, &tr2, &m2, &s2, &f2, &c))
843 goto play_msf;
844
845 /* tr1 m1:s1.f1 m2:s2.f2 */
846 tr2 = 0;
847 if (7 == sscanf(arg, "%u%u:%u.%u%u:%u.%u%c",
848 &tr1, &m1, &s1, &f1, &m2, &s2, &f2, &c))
849 goto play_msf;
850
851 /* m1:s1 tr2 m2:s2.f2 */
852 tr1 = f1 = 0;
853 if (6 == sscanf(arg, "%u:%u%u%u:%u.%u%c",
854 &m1, &s1, &tr2, &m2, &s2, &f2, &c))
855 goto play_msf;
856
857 /* m1:s1.f1 tr2 m2:s2 */
858 tr1 = f2 = 0;
859 if (6 == sscanf(arg, "%u:%u.%u%u%u:%u%c",
860 &m1, &s1, &f1, &tr2, &m2, &s2, &c))
861 goto play_msf;
862
863 /* m1:s1.f1 m2:s2.f2 */
864 tr1 = tr2 = 0;
865 if (6 == sscanf(arg, "%u:%u.%u%u:%u.%u%c",
866 &m1, &s1, &f1, &m2, &s2, &f2, &c))
867 goto play_msf;
868
869 /* tr1 m1:s1.f1 m2:s2 */
870 tr2 = f2 = 0;
871 if (6 == sscanf(arg, "%u%u:%u.%u%u:%u%c",
872 &tr1, &m1, &s1, &f1, &m2, &s2, &c))
873 goto play_msf;
874
875 /* tr1 m1:s1 m2:s2.f2 */
876 tr2 = f1 = 0;
877 if (6 == sscanf(arg, "%u%u:%u%u:%u.%u%c",
878 &tr1, &m1, &s1, &m2, &s2, &f2, &c))
879 goto play_msf;
880
881 /* tr1 m1:s1 tr2 m2:s2 */
882 f1 = f2 = 0;
883 if (6 == sscanf(arg, "%u%u:%u%u%u:%u%c",
884 &tr1, &m1, &s1, &tr2, &m2, &s2, &c))
885 goto play_msf;
886
887 /* m1:s1 tr2 m2:s2 */
888 tr1 = f1 = f2 = 0;
889 if (5 == sscanf(arg, "%u:%u%u%u:%u%c", &m1, &s1, &tr2, &m2, &s2, &c))
890 goto play_msf;
891
892 /* tr1 m1:s1 m2:s2 */
893 f1 = tr2 = f2 = 0;
894 if (5 == sscanf(arg, "%u%u:%u%u:%u%c", &tr1, &m1, &s1, &m2, &s2, &c))
895 goto play_msf;
896
897 /* m1:s1 m2:s2.f2 */
898 tr1 = f1 = tr2 = 0;
899 if (5 == sscanf(arg, "%u:%u%u:%u.%u%c", &m1, &s1, &m2, &s2, &f2, &c))
900 goto play_msf;
901
902 /* m1:s1.f1 m2:s2 */
903 tr1 = tr2 = f2 = 0;
904 if (5 == sscanf(arg, "%u:%u.%u%u:%u%c", &m1, &s1, &f1, &m2, &s2, &c))
905 goto play_msf;
906
907 /* tr1 m1:s1.f1 tr2 */
908 m2 = s2 = f2 = 0;
909 if (5 == sscanf(arg, "%u%u:%u.%u%u%c", &tr1, &m1, &s1, &f1, &tr2, &c))
910 goto play_msf;
911
912 /* m1:s1 m2:s2 */
913 tr1 = f1 = tr2 = f2 = 0;
914 if (4 == sscanf(arg, "%u:%u%u:%u%c", &m1, &s1, &m2, &s2, &c))
915 goto play_msf;
916
917 /* tr1 m1:s1.f1 <end of disc> */
918 tr2 = m2 = s2 = f2 = 0;
919 if (4 == sscanf(arg, "%u%u:%u.%u%c", &tr1, &m1, &s1, &f1, &c))
920 goto play_msf;
921
922 /* tr1 m1:s1 tr2 */
923 f1 = m2 = s2 = f2 = 0;
924 if (4 == sscanf(arg, "%u%u:%u%u%c", &tr1, &m1, &s1, &tr2, &c))
925 goto play_msf;
926
927 /* m1:s1.f1 tr2 */
928 tr1 = m2 = s2 = f2 = 0;
929 if (4 == sscanf(arg, "%u%u:%u%u%c", &m1, &s1, &f1, &tr2, &c))
930 goto play_msf;
931
932 /* m1:s1.f1 <end of disc> */
933 tr1 = tr2 = m2 = s2 = f2 = 0;
934 if (3 == sscanf(arg, "%u:%u.%u%c", &m1, &s1, &f1, &c))
935 goto play_msf;
936
937 /* tr1 m1:s1 <end of disc> */
938 f1 = tr2 = m2 = s2 = f2 = 0;
939 if (3 == sscanf(arg, "%u%u:%u%c", &tr1, &m1, &s1, &c))
940 goto play_msf;
941
942 /* m1:s1 tr2 */
943 tr1 = f1 = m2 = s2 = f2 = 0;
944 if (3 == sscanf(arg, "%u:%u%u%c", &m1, &s1, &tr2, &c))
945 goto play_msf;
946
947 /* m1:s1 <end of disc> */
948 tr1 = f1 = tr2 = m2 = s2 = f2 = 0;
949 if (2 == sscanf(arg, "%u:%u%c", &m1, &s1, &c))
950 goto play_msf;
951
952 printf("%s: Invalid command arguments\n", __progname);
953 return (0);
954
955 play_msf:
956 if (tr1 > n || tr2 > n) {
957 printf("Track number must be between 0 and %u\n", n);
958 return (0);
959 } else if (m1 > 99 || m2 > 99) {
960 printf("Minutes must be between 0 and 99\n");
961 return (0);
962 } else if (s1 > 59 || s2 > 59) {
963 printf("Seconds must be between 0 and 59\n");
964 return (0);
965 } else if (f1 > 74 || f2 > 74) {
966 printf("Frames number must be between 0 and 74\n");
967 return (0);
968 }
969
970 if (tr1 > 0) {
971 /*
972 * Start time is relative to tr1, Add start time of tr1
973 * to (m1,s1,f1) to yield absolute start time.
974 */
975 toc2msf(tr1, &tm, &ts, &tf);
976 addmsf(&m1, &s1, &f1, tm, ts, tf);
977
978 /* Compare (m1,s1,f1) to start time of next track. */
979 toc2msf(tr1+1, &tm, &ts, &tf);
980 if (cmpmsf(m1, s1, f1, tm, ts, tf) == 1) {
981 printf("Track %u is not that long.\n", tr1);
982 return (0);
983 }
984 }
985
986 toc2msf(n+1, &tm, &ts, &tf);
987 if (cmpmsf(m1, s1, f1, tm, ts, tf) == 1) {
988 printf("Start time is after end of disc.\n");
989 return (0);
990 }
991
992 if (tr2 > 0) {
993 /*
994 * End time is relative to tr2, Add start time of tr2
995 * to (m2,s2,f2) to yield absolute end time.
996 */
997 toc2msf(tr2, &tm, &ts, &tf);
998 addmsf(&m2, &s2, &f2, tm, ts, tf);
999
1000 /* Compare (m2,s2,f2) to start time of next track. */
1001 toc2msf(tr2+1, &tm, &ts, &tf);
1002 if (cmpmsf(m2, s2, f2, tm, ts, tf) == 1) {
1003 printf("Track %u is not that long.\n", tr2);
1004 return (0);
1005 }
1006 }
1007
1008 toc2msf(n+1, &tm, &ts, &tf);
1009
1010 if (!(tr2 || m2 || s2 || f2)) {
1011 /* Play to end of disc. */
1012 m2 = tm;
1013 s2 = ts;
1014 f2 = tf;
1015 } else if (cmpmsf(m2, s2, f2, tm, ts, tf) == 1) {
1016 printf("End time is after end of disc.\n");
1017 return (0);
1018 }
1019
1020 if (cmpmsf(m1, s1, f1, m2, s2, f2) == 1) {
1021 printf("Start time is after end time.\n");
1022 return (0);
1023 }
1024
1025 return play_msf(m1, s1, f1, m2, s2, f2);
1026 }
1027
1028 int
play_prev(char * arg)1029 play_prev(char *arg)
1030 {
1031 int trk, min, sec, frm, rc;
1032 struct ioc_toc_header h;
1033
1034 if (status(&trk, &min, &sec, &frm) >= 0) {
1035 trk--;
1036
1037 rc = ioctl(fd, CDIOREADTOCHEADER, &h);
1038 if (rc == -1) {
1039 warn("getting toc header");
1040 return (rc);
1041 }
1042
1043 if (trk < h.starting_track)
1044 return play_track(h.starting_track, 1,
1045 h.ending_track + 1, 1);
1046 return play_track(trk, 1, h.ending_track, 1);
1047 }
1048
1049 return (0);
1050 }
1051
1052 int
play_same(char * arg)1053 play_same(char *arg)
1054 {
1055 int trk, min, sec, frm, rc;
1056 struct ioc_toc_header h;
1057
1058 if (status (&trk, &min, &sec, &frm) >= 0) {
1059 rc = ioctl(fd, CDIOREADTOCHEADER, &h);
1060 if (rc == -1) {
1061 warn("getting toc header");
1062 return (rc);
1063 }
1064
1065 return play_track(trk, 1, h.ending_track, 1);
1066 }
1067
1068 return (0);
1069 }
1070
1071 int
play_next(char * arg)1072 play_next(char *arg)
1073 {
1074 int trk, min, sec, frm, rc;
1075 struct ioc_toc_header h;
1076
1077 if (status(&trk, &min, &sec, &frm) >= 0) {
1078 trk++;
1079 rc = ioctl(fd, CDIOREADTOCHEADER, &h);
1080 if (rc == -1) {
1081 warn("getting toc header");
1082 return (rc);
1083 }
1084
1085 if (trk > h.ending_track) {
1086 printf("%s: end of CD\n", __progname);
1087
1088 rc = ioctl(fd, CDIOCSTOP);
1089
1090 (void) ioctl(fd, CDIOCALLOW);
1091
1092 return (rc);
1093 }
1094
1095 return play_track(trk, 1, h.ending_track, 1);
1096 }
1097
1098 return (0);
1099 }
1100
1101 char *
strstatus(int sts)1102 strstatus(int sts)
1103 {
1104 switch (sts) {
1105 case ASTS_INVALID:
1106 return ("invalid");
1107 case ASTS_PLAYING:
1108 return ("playing");
1109 case ASTS_PAUSED:
1110 return ("paused");
1111 case ASTS_COMPLETED:
1112 return ("completed");
1113 case ASTS_ERROR:
1114 return ("error");
1115 case ASTS_VOID:
1116 return ("void");
1117 default:
1118 return ("??");
1119 }
1120 }
1121
1122 int
pstatus(char * arg)1123 pstatus(char *arg)
1124 {
1125 struct ioc_vol v;
1126 struct ioc_read_subchannel ss;
1127 struct cd_sub_channel_info data;
1128 int rc, trk, m, s, f;
1129 char vis_catalog[1 + 4 * 15];
1130
1131 rc = status(&trk, &m, &s, &f);
1132 if (rc >= 0) {
1133 if (verbose) {
1134 if (track_names)
1135 printf("Audio status = %d<%s>, "
1136 "current track = %d (%s)\n"
1137 "\tcurrent position = %d:%02d.%02d\n",
1138 rc, strstatus(rc), trk,
1139 trk ? track_names[trk-1] : "", m, s, f);
1140 else
1141 printf("Audio status = %d<%s>, "
1142 "current track = %d, "
1143 "current position = %d:%02d.%02d\n",
1144 rc, strstatus(rc), trk, m, s, f);
1145 } else
1146 printf("%d %d %d:%02d.%02d\n", rc, trk, m, s, f);
1147 } else
1148 printf("No current status info available\n");
1149
1150 bzero(&ss, sizeof (ss));
1151 ss.data = &data;
1152 ss.data_len = sizeof (data);
1153 ss.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
1154 ss.data_format = CD_MEDIA_CATALOG;
1155 rc = ioctl(fd, CDIOCREADSUBCHANNEL, (char *) &ss);
1156 if (rc >= 0) {
1157 printf("Media catalog is %sactive",
1158 ss.data->what.media_catalog.mc_valid ? "": "in");
1159 if (ss.data->what.media_catalog.mc_valid &&
1160 ss.data->what.media_catalog.mc_number[0]) {
1161 strvisx(vis_catalog,
1162 (char *)ss.data->what.media_catalog.mc_number,
1163 15, VIS_SAFE);
1164 printf(", number \"%.15s\"", vis_catalog);
1165 }
1166 putchar('\n');
1167 } else
1168 printf("No media catalog info available\n");
1169
1170 rc = ioctl(fd, CDIOCGETVOL, &v);
1171 if (rc >= 0) {
1172 if (verbose)
1173 printf("Left volume = %d, right volume = %d\n",
1174 v.vol[0], v.vol[1]);
1175 else
1176 printf("%d %d\n", v.vol[0], v.vol[1]);
1177 } else
1178 printf("No volume level info available\n");
1179 return(0);
1180 }
1181
1182 int
cdid(void)1183 cdid(void)
1184 {
1185 unsigned long id;
1186 struct ioc_toc_header h;
1187 int rc, n;
1188
1189 rc = ioctl(fd, CDIOREADTOCHEADER, &h);
1190 if (rc == -1) {
1191 warn("getting toc header");
1192 return (rc);
1193 }
1194
1195 n = h.ending_track - h.starting_track + 1;
1196 rc = read_toc_entrys((n + 1) * sizeof (struct cd_toc_entry));
1197 if (rc < 0)
1198 return (rc);
1199
1200 id = cddb_discid(n, toc_buffer);
1201 if (id) {
1202 if (verbose)
1203 printf("CDID=");
1204 printf("%08lx\n", id);
1205 }
1206 return id ? 0 : 1;
1207 }
1208
1209 int
info(char * arg)1210 info(char *arg)
1211 {
1212 struct ioc_toc_header h;
1213 int rc, i, n;
1214
1215 if (get_media_capabilities(mediacap, 1) == -1)
1216 errx(1, "Can't determine media type");
1217
1218 rc = ioctl(fd, CDIOREADTOCHEADER, &h);
1219 if (rc >= 0) {
1220 if (verbose)
1221 printf("Starting track = %d, ending track = %d, TOC size = %d bytes\n",
1222 h.starting_track, h.ending_track, h.len);
1223 else
1224 printf("%d %d %d\n", h.starting_track,
1225 h.ending_track, h.len);
1226 } else {
1227 warn("getting toc header");
1228 return (rc);
1229 }
1230
1231 n = h.ending_track - h.starting_track + 1;
1232 rc = read_toc_entrys((n + 1) * sizeof (struct cd_toc_entry));
1233 if (rc < 0)
1234 return (rc);
1235
1236 if (verbose) {
1237 printf("track start duration block length type\n");
1238 printf("-------------------------------------------------\n");
1239 }
1240
1241 for (i = 0; i < n; i++) {
1242 printf("%5d ", toc_buffer[i].track);
1243 prtrack(toc_buffer + i, 0, NULL);
1244 }
1245 printf("%5d ", toc_buffer[n].track);
1246 prtrack(toc_buffer + n, 1, NULL);
1247 return (0);
1248 }
1249
1250 int
cddbinfo(char * arg)1251 cddbinfo(char *arg)
1252 {
1253 struct ioc_toc_header h;
1254 int rc, i, n;
1255
1256 rc = ioctl(fd, CDIOREADTOCHEADER, &h);
1257 if (rc == -1) {
1258 warn("getting toc header");
1259 return (rc);
1260 }
1261
1262 n = h.ending_track - h.starting_track + 1;
1263 rc = read_toc_entrys((n + 1) * sizeof (struct cd_toc_entry));
1264 if (rc < 0)
1265 return (rc);
1266
1267 if (track_names)
1268 free_names(track_names);
1269 track_names = NULL;
1270
1271 track_names = cddb(cddb_host, n, toc_buffer, arg);
1272 if (!track_names)
1273 return(0);
1274
1275 printf("-------------------------------------------------\n");
1276
1277 for (i = 0; i < n; i++) {
1278 printf("%5d ", toc_buffer[i].track);
1279 prtrack(toc_buffer + i, 0, track_names[i]);
1280 }
1281 printf("%5d ", toc_buffer[n].track);
1282 prtrack(toc_buffer + n, 1, "");
1283 return (0);
1284 }
1285
1286 void
lba2msf(unsigned long lba,u_char * m,u_char * s,u_char * f)1287 lba2msf(unsigned long lba, u_char *m, u_char *s, u_char *f)
1288 {
1289 lba += 150; /* block start offset */
1290 lba &= 0xffffff; /* negative lbas use only 24 bits */
1291 *m = lba / (60 * 75);
1292 lba %= (60 * 75);
1293 *s = lba / 75;
1294 *f = lba % 75;
1295 }
1296
1297 unsigned int
msf2lba(u_char m,u_char s,u_char f)1298 msf2lba(u_char m, u_char s, u_char f)
1299 {
1300 return (((m * 60) + s) * 75 + f) - 150;
1301 }
1302
1303 unsigned long
entry2time(struct cd_toc_entry * e)1304 entry2time(struct cd_toc_entry *e)
1305 {
1306 int block;
1307 u_char m, s, f;
1308
1309 if (msf) {
1310 return (e->addr.msf.minute * 60 + e->addr.msf.second);
1311 } else {
1312 block = e->addr.lba;
1313 lba2msf(block, &m, &s, &f);
1314 return (m*60+s);
1315 }
1316 }
1317
1318 unsigned long
entry2frames(struct cd_toc_entry * e)1319 entry2frames(struct cd_toc_entry *e)
1320 {
1321 int block;
1322 unsigned char m, s, f;
1323
1324 if (msf) {
1325 return e->addr.msf.frame + e->addr.msf.second * 75 +
1326 e->addr.msf.minute * 60 * 75;
1327 } else {
1328 block = e->addr.lba;
1329 lba2msf(block, &m, &s, &f);
1330 return f + s * 75 + m * 60 * 75;
1331 }
1332 }
1333
1334 void
prtrack(struct cd_toc_entry * e,int lastflag,char * name)1335 prtrack(struct cd_toc_entry *e, int lastflag, char *name)
1336 {
1337 int block, next, len;
1338 u_char m, s, f;
1339
1340 if (msf) {
1341 if (!name || lastflag)
1342 /* Print track start */
1343 printf("%2d:%02d.%02d ", e->addr.msf.minute,
1344 e->addr.msf.second, e->addr.msf.frame);
1345
1346 block = msf2lba(e->addr.msf.minute, e->addr.msf.second,
1347 e->addr.msf.frame);
1348 } else {
1349 block = e->addr.lba;
1350 if (!name || lastflag) {
1351 lba2msf(block, &m, &s, &f);
1352 /* Print track start */
1353 printf("%2d:%02d.%02d ", m, s, f);
1354 }
1355 }
1356 if (lastflag) {
1357 if (!name)
1358 /* Last track -- print block */
1359 printf(" - %6d - -\n", block);
1360 else
1361 printf("\n");
1362 return;
1363 }
1364
1365 if (msf)
1366 next = msf2lba(e[1].addr.msf.minute, e[1].addr.msf.second,
1367 e[1].addr.msf.frame);
1368 else
1369 next = e[1].addr.lba;
1370 len = next - block;
1371 lba2msf(len - 150, &m, &s, &f);
1372
1373 if (name)
1374 printf("%2d:%02d.%02d %s\n", m, s, f, name);
1375 /* Print duration, block, length, type */
1376 else
1377 printf("%2d:%02d.%02d %6d %6d %5s\n", m, s, f, block, len,
1378 (e->control & 4) ? "data" : "audio");
1379 }
1380
1381 int
play_track(int tstart,int istart,int tend,int iend)1382 play_track(int tstart, int istart, int tend, int iend)
1383 {
1384 struct ioc_play_track t;
1385
1386 t.start_track = tstart;
1387 t.start_index = istart;
1388 t.end_track = tend;
1389 t.end_index = iend;
1390
1391 return ioctl(fd, CDIOCPLAYTRACKS, &t);
1392 }
1393
1394 int
play_blocks(int blk,int len)1395 play_blocks(int blk, int len)
1396 {
1397 struct ioc_play_blocks t;
1398
1399 t.blk = blk;
1400 t.len = len;
1401
1402 return ioctl(fd, CDIOCPLAYBLOCKS, &t);
1403 }
1404
1405 int
setvol(int left,int right)1406 setvol(int left, int right)
1407 {
1408 struct ioc_vol v;
1409
1410 v.vol[0] = left;
1411 v.vol[1] = right;
1412 v.vol[2] = 0;
1413 v.vol[3] = 0;
1414
1415 return ioctl(fd, CDIOCSETVOL, &v);
1416 }
1417
1418 int
read_toc_entrys(int len)1419 read_toc_entrys(int len)
1420 {
1421 struct ioc_read_toc_entry t;
1422
1423 if (toc_buffer) {
1424 free(toc_buffer);
1425 toc_buffer = 0;
1426 }
1427
1428 toc_buffer = malloc(len);
1429
1430 if (!toc_buffer) {
1431 errno = ENOMEM;
1432 return (-1);
1433 }
1434
1435 t.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
1436 t.starting_track = 0;
1437 t.data_len = len;
1438 t.data = toc_buffer;
1439
1440 return (ioctl(fd, CDIOREADTOCENTRYS, (char *) &t));
1441 }
1442
1443 int
play_msf(int start_m,int start_s,int start_f,int end_m,int end_s,int end_f)1444 play_msf(int start_m, int start_s, int start_f, int end_m, int end_s, int end_f)
1445 {
1446 struct ioc_play_msf a;
1447
1448 a.start_m = start_m;
1449 a.start_s = start_s;
1450 a.start_f = start_f;
1451 a.end_m = end_m;
1452 a.end_s = end_s;
1453 a.end_f = end_f;
1454
1455 return ioctl(fd, CDIOCPLAYMSF, (char *) &a);
1456 }
1457
1458 int
status(int * trk,int * min,int * sec,int * frame)1459 status(int *trk, int *min, int *sec, int *frame)
1460 {
1461 struct ioc_read_subchannel s;
1462 struct cd_sub_channel_info data;
1463 u_char mm, ss, ff;
1464
1465 bzero(&s, sizeof (s));
1466 s.data = &data;
1467 s.data_len = sizeof (data);
1468 s.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
1469 s.data_format = CD_CURRENT_POSITION;
1470
1471 if (ioctl(fd, CDIOCREADSUBCHANNEL, (char *) &s) == -1)
1472 return -1;
1473
1474 *trk = s.data->what.position.track_number;
1475 if (msf) {
1476 *min = s.data->what.position.reladdr.msf.minute;
1477 *sec = s.data->what.position.reladdr.msf.second;
1478 *frame = s.data->what.position.reladdr.msf.frame;
1479 } else {
1480 /*
1481 * NOTE: CDIOCREADSUBCHANNEL does not put the lba info into
1482 * host order like CDIOREADTOCENTRYS does.
1483 */
1484 lba2msf(betoh32(s.data->what.position.reladdr.lba), &mm, &ss,
1485 &ff);
1486 *min = mm;
1487 *sec = ss;
1488 *frame = ff;
1489 }
1490
1491 return s.data->header.audio_status;
1492 }
1493
1494 void
sigint_handler(int signo_arg)1495 sigint_handler(int signo_arg)
1496 {
1497 signo = signo_arg;
1498 }
1499
1500 char *
input(int * cmd)1501 input(int *cmd)
1502 {
1503 struct sigaction sa;
1504 char *buf;
1505 int siz = 0;
1506 char *p;
1507 HistEvent hev;
1508
1509 memset(&sa, 0, sizeof(sa));
1510 do {
1511 signo = 0;
1512 sa.sa_handler = sigint_handler;
1513 if (sigaction(SIGINT, &sa, NULL) == -1)
1514 err(1, "sigaction");
1515 buf = (char *)el_gets(el, &siz);
1516 sa.sa_handler = SIG_DFL;
1517 if (sigaction(SIGINT, &sa, NULL) == -1)
1518 err(1, "sigaction");
1519 if (buf == NULL || siz <= 0) {
1520 fprintf(stderr, "\r\n");
1521 if (siz < 0 && errno == EINTR && signo == SIGINT)
1522 continue;
1523 *cmd = CMD_QUIT;
1524 return (0);
1525 }
1526 if (strlen(buf) > 1)
1527 history(hist, &hev, H_ENTER, buf);
1528 p = parse(buf, cmd);
1529 } while (!p);
1530 return (p);
1531 }
1532
1533 char *
parse(char * buf,int * cmd)1534 parse(char *buf, int *cmd)
1535 {
1536 struct cmdtab *c;
1537 char *p;
1538 size_t len;
1539
1540 for (p=buf; isspace((unsigned char)*p); p++)
1541 continue;
1542
1543 if (isdigit((unsigned char)*p) ||
1544 (p[0] == '#' && isdigit((unsigned char)p[1]))) {
1545 *cmd = CMD_PLAY;
1546 return (p);
1547 }
1548
1549 for (buf = p; *p && ! isspace((unsigned char)*p); p++)
1550 continue;
1551
1552 len = p - buf;
1553 if (!len)
1554 return (0);
1555
1556 if (*p) { /* It must be a spacing character! */
1557 char *q;
1558
1559 *p++ = 0;
1560 for (q=p; *q && *q != '\n' && *q != '\r'; q++)
1561 continue;
1562 *q = 0;
1563 }
1564
1565 *cmd = -1;
1566 for (c=cmdtab; c->name; ++c) {
1567 /* Is it an exact match? */
1568 if (!strcasecmp(buf, c->name)) {
1569 *cmd = c->command;
1570 break;
1571 }
1572
1573 /* Try short hand forms then... */
1574 if (len >= c->min && ! strncasecmp(buf, c->name, len)) {
1575 if (*cmd != -1 && *cmd != c->command) {
1576 fprintf(stderr, "Ambiguous command\n");
1577 return (0);
1578 }
1579 *cmd = c->command;
1580 }
1581 }
1582
1583 if (*cmd == -1) {
1584 fprintf(stderr, "%s: Invalid command, enter ``help'' for commands.\n",
1585 __progname);
1586 return (0);
1587 }
1588
1589 while (isspace((unsigned char)*p))
1590 p++;
1591 return p;
1592 }
1593
1594 int
open_cd(char * dev,int needwrite)1595 open_cd(char *dev, int needwrite)
1596 {
1597 char *realdev;
1598 int tries;
1599
1600 if (fd > -1) {
1601 if (needwrite && !writeperm) {
1602 close(fd);
1603 fd = -1;
1604 } else
1605 return (1);
1606 }
1607
1608 for (tries = 0; fd < 0 && tries < 10; tries++) {
1609 if (needwrite)
1610 fd = opendev(dev, O_RDWR, OPENDEV_PART, &realdev);
1611 else
1612 fd = opendev(dev, O_RDONLY, OPENDEV_PART, &realdev);
1613 if (fd == -1) {
1614 if (errno == ENXIO) {
1615 /* ENXIO has an overloaded meaning here.
1616 * The original "Device not configured" should
1617 * be interpreted as "No disc in drive %s". */
1618 warnx("No disc in drive %s.", realdev);
1619 return (0);
1620 } else if (errno != EIO) {
1621 /* EIO may simply mean the device is not ready
1622 * yet which is common with CD changers. */
1623 warn("Can't open %s", realdev);
1624 return (0);
1625 }
1626 }
1627 sleep(1);
1628 }
1629 if (fd == -1) {
1630 warn("Can't open %s", realdev);
1631 return (0);
1632 }
1633 writeperm = needwrite;
1634 return (1);
1635 }
1636
1637 char *
prompt(void)1638 prompt(void)
1639 {
1640 return (verbose ? "cdio> " : "");
1641 }
1642
1643 void
switch_el(void)1644 switch_el(void)
1645 {
1646 HistEvent hev;
1647
1648 if (el == NULL && hist == NULL) {
1649 el = el_init(__progname, stdin, stdout, stderr);
1650 hist = history_init();
1651 history(hist, &hev, H_SETSIZE, 100);
1652 el_set(el, EL_HIST, history, hist);
1653 el_set(el, EL_EDITOR, "emacs");
1654 el_set(el, EL_PROMPT, prompt);
1655 el_set(el, EL_SIGNAL, 1);
1656 el_source(el, NULL);
1657
1658 } else {
1659 if (hist != NULL) {
1660 history_end(hist);
1661 hist = NULL;
1662 }
1663 if (el != NULL) {
1664 el_end(el);
1665 el = NULL;
1666 }
1667 }
1668 }
1669
1670 void
addmsf(u_int * m,u_int * s,u_int * f,u_char m_inc,u_char s_inc,u_char f_inc)1671 addmsf(u_int *m, u_int *s, u_int *f, u_char m_inc, u_char s_inc, u_char f_inc)
1672 {
1673 *f += f_inc;
1674 if (*f > 75) {
1675 *s += *f / 75;
1676 *f %= 75;
1677 }
1678
1679 *s += s_inc;
1680 if (*s > 60) {
1681 *m += *s / 60;
1682 *s %= 60;
1683 }
1684
1685 *m += m_inc;
1686 }
1687
1688 int
cmpmsf(u_char m1,u_char s1,u_char f1,u_char m2,u_char s2,u_char f2)1689 cmpmsf(u_char m1, u_char s1, u_char f1, u_char m2, u_char s2, u_char f2)
1690 {
1691 if (m1 > m2)
1692 return (1);
1693 else if (m1 < m2)
1694 return (-1);
1695
1696 if (s1 > s2)
1697 return (1);
1698 else if (s1 < s2)
1699 return (-1);
1700
1701 if (f1 > f2)
1702 return (1);
1703 else if (f1 < f2)
1704 return (-1);
1705
1706 return (0);
1707 }
1708
1709 void
toc2msf(u_int track,u_char * m,u_char * s,u_char * f)1710 toc2msf(u_int track, u_char *m, u_char *s, u_char *f)
1711 {
1712 struct cd_toc_entry *ctep;
1713
1714 ctep = &toc_buffer[track - 1];
1715
1716 if (msf) {
1717 *m = ctep->addr.msf.minute;
1718 *s = ctep->addr.msf.second;
1719 *f = ctep->addr.msf.frame;
1720 } else
1721 lba2msf(ctep->addr.lba, m, s, f);
1722 }
1723