1 /* $OpenBSD: cdio.c,v 1.72 2010/03/01 02:09:44 krw 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/param.h> 56 #include <sys/file.h> 57 #include <sys/cdio.h> 58 #include <sys/ioctl.h> 59 #include <sys/queue.h> 60 #include <sys/scsiio.h> 61 #include <sys/stat.h> 62 63 #include <ctype.h> 64 #include <err.h> 65 #include <errno.h> 66 #include <stdio.h> 67 #include <stdlib.h> 68 #include <string.h> 69 #include <unistd.h> 70 #include <histedit.h> 71 #include <util.h> 72 #include <vis.h> 73 74 #include "extern.h" 75 76 #define ASTS_INVALID 0x00 /* Audio status byte not valid */ 77 #define ASTS_PLAYING 0x11 /* Audio play operation in progress */ 78 #define ASTS_PAUSED 0x12 /* Audio play operation paused */ 79 #define ASTS_COMPLETED 0x13 /* Audio play operation successfully completed */ 80 #define ASTS_ERROR 0x14 /* Audio play operation stopped due to error */ 81 #define ASTS_VOID 0x15 /* No current audio status to return */ 82 83 #ifndef DEFAULT_CD_DRIVE 84 # define DEFAULT_CD_DRIVE "cd0" 85 #endif 86 87 #define CMD_DEBUG 1 88 #define CMD_DEVICE 2 89 #define CMD_EJECT 3 90 #define CMD_HELP 4 91 #define CMD_INFO 5 92 #define CMD_PAUSE 6 93 #define CMD_PLAY 7 94 #define CMD_QUIT 8 95 #define CMD_RESUME 9 96 #define CMD_STOP 10 97 #define CMD_VOLUME 11 98 #define CMD_CLOSE 12 99 #define CMD_RESET 13 100 #define CMD_SET 14 101 #define CMD_STATUS 15 102 #define CMD_NEXT 16 103 #define CMD_PREV 17 104 #define CMD_REPLAY 18 105 #define CMD_CDDB 19 106 #define CMD_CDID 20 107 #define CMD_BLANK 21 108 #define CMD_CDRIP 22 109 #define CMD_CDPLAY 23 110 111 struct cmdtab { 112 int command; 113 char *name; 114 int min; 115 char *args; 116 } cmdtab[] = { 117 { CMD_BLANK, "blank", 1, "" }, 118 { CMD_CDDB, "cddbinfo", 2, "[n]" }, 119 { CMD_CDID, "cdid", 3, "" }, 120 { CMD_CDPLAY, "cdplay", 3, "[track1-trackN ...]" }, 121 { CMD_CDRIP, "cdrip", 3, "[track1-trackN ...]" }, 122 { CMD_CLOSE, "close", 1, "" }, 123 { CMD_DEBUG, "debug", 3, "on | off" }, 124 { CMD_DEVICE, "device", 1, "devname" }, 125 { CMD_EJECT, "eject", 1, "" }, 126 { CMD_QUIT, "exit", 2, "" }, 127 { CMD_HELP, "?", 1, 0 }, 128 { CMD_HELP, "help", 1, "" }, 129 { CMD_INFO, "info", 1, "" }, 130 { CMD_NEXT, "next", 1, "" }, 131 { CMD_PAUSE, "pause", 2, "" }, 132 { CMD_PLAY, "play", 1, "[track1[.index1] [track2[.index2]]]" }, 133 { CMD_PLAY, "play", 1, "[[tr1] m1:s1[.f1] [tr2] [m2:s2[.f2]]]" }, 134 { CMD_PLAY, "play", 1, "[#block [len]]" }, 135 { CMD_PREV, "previous", 2, "" }, 136 { CMD_QUIT, "quit", 1, "" }, 137 { CMD_REPLAY, "replay", 3, "" }, 138 { CMD_RESET, "reset", 4, "" }, 139 { CMD_RESUME, "resume", 1, "" }, 140 { CMD_SET, "set", 2, "lba | msf" }, 141 { CMD_STATUS, "status", 1, "" }, 142 { CMD_STOP, "stop", 3, "" }, 143 { CMD_VOLUME, "volume", 1, "left_channel right_channel" }, 144 { CMD_VOLUME, "volume", 1, "left | right | mono | stereo | mute" }, 145 { 0, 0, 0, 0} 146 }; 147 148 struct cd_toc_entry *toc_buffer; 149 150 char *cdname; 151 int fd = -1; 152 int writeperm = 0; 153 u_int8_t mediacap[MMC_FEATURE_MAX / NBBY]; 154 int verbose = 1; 155 int msf = 1; 156 const char *cddb_host; 157 char **track_names; 158 159 EditLine *el = NULL; /* line-editing structure */ 160 History *hist = NULL; /* line-editing history */ 161 void switch_el(void); 162 163 extern char *__progname; 164 165 int setvol(int, int); 166 int read_toc_entrys(int); 167 int play_msf(int, int, int, int, int, int); 168 int play_track(int, int, int, int); 169 int status(int *, int *, int *, int *); 170 int is_wave(int); 171 __dead void tao(int argc, char **argv); 172 int play(char *arg); 173 int info(char *arg); 174 int cddbinfo(char *arg); 175 int pstatus(char *arg); 176 int play_next(char *arg); 177 int play_prev(char *arg); 178 int play_same(char *arg); 179 char *input(int *); 180 char *prompt(void); 181 void prtrack(struct cd_toc_entry *e, int lastflag, char *name); 182 void lba2msf(unsigned long lba, u_char *m, u_char *s, u_char *f); 183 unsigned int msf2lba(u_char m, u_char s, u_char f); 184 int play_blocks(int blk, int len); 185 int run(int cmd, char *arg); 186 char *parse(char *buf, int *cmd); 187 void help(void); 188 void usage(void); 189 char *strstatus(int); 190 int cdid(void); 191 void addmsf(u_int *, u_int *, u_int *, u_char, u_char, u_char); 192 int cmpmsf(u_char, u_char, u_char, u_char, u_char, u_char); 193 void toc2msf(u_int, u_char *, u_char *, u_char *); 194 195 void 196 help(void) 197 { 198 struct cmdtab *c; 199 char *s, n; 200 int i; 201 202 for (c = cmdtab; c->name; ++c) { 203 if (!c->args) 204 continue; 205 printf("\t"); 206 for (i = c->min, s = c->name; *s; s++, i--) { 207 if (i > 0) 208 n = toupper(*s); 209 else 210 n = *s; 211 putchar(n); 212 } 213 if (*c->args) 214 printf(" %s", c->args); 215 printf("\n"); 216 } 217 printf("\n\tThe word \"play\" is not required for the play commands.\n"); 218 printf("\tThe plain target address is taken as a synonym for play.\n"); 219 } 220 221 void 222 usage(void) 223 { 224 fprintf(stderr, "usage: %s [-sv] [-d host:port] [-f device] [command args ...]\n", 225 __progname); 226 exit(1); 227 } 228 229 int 230 main(int argc, char **argv) 231 { 232 int ch, cmd; 233 char *arg; 234 235 cdname = getenv("DISC"); 236 if (!cdname) 237 cdname = getenv("CDROM"); 238 239 cddb_host = getenv("CDDB"); 240 if (!cddb_host) 241 cddb_host = "freedb.freedb.org"; 242 243 while ((ch = getopt(argc, argv, "svd:f:")) != -1) 244 switch (ch) { 245 case 's': 246 verbose = 0; 247 break; 248 case 'v': 249 verbose++; 250 break; 251 case 'f': 252 cdname = optarg; 253 break; 254 case 'd': 255 cddb_host = optarg; 256 break; 257 default: 258 usage(); 259 } 260 261 argc -= optind; 262 argv += optind; 263 264 if (argc > 0 && ! strcasecmp(*argv, "help")) 265 usage(); 266 267 if (!cdname) { 268 cdname = DEFAULT_CD_DRIVE; 269 if (verbose > 1) 270 fprintf(stderr, 271 "No CD device name specified. Defaulting to %s.\n", 272 cdname); 273 } 274 275 if (argc > 0 && !strcasecmp(*argv, "tao")) { 276 tao(argc, argv); 277 /* NOTREACHED */ 278 } 279 if (argc > 0) { 280 char buf[80], *p; 281 int len; 282 283 for (p=buf; argc-->0; ++argv) { 284 len = snprintf(p, buf + sizeof buf - p, 285 "%s%s", (p > buf) ? " " : "", *argv); 286 287 if (len == -1 || len >= buf + sizeof buf - p) 288 errx(1, "argument list too long."); 289 290 p += len; 291 } 292 arg = parse(buf, &cmd); 293 return (run(cmd, arg)); 294 } 295 296 if (verbose == 1) 297 verbose = isatty(0); 298 299 if (verbose) { 300 printf("Compact Disc Control utility, version %s\n", VERSION); 301 printf("Type `?' for command list\n\n"); 302 } 303 304 switch_el(); 305 306 for (;;) { 307 arg = input(&cmd); 308 if (run(cmd, arg) < 0) { 309 if (verbose) 310 warn(NULL); 311 close(fd); 312 fd = -1; 313 } 314 fflush(stdout); 315 } 316 } 317 318 int 319 run(int cmd, char *arg) 320 { 321 int l, r, rc; 322 static char newcdname[MAXPATHLEN]; 323 324 switch (cmd) { 325 326 case CMD_QUIT: 327 switch_el(); 328 exit(0); 329 330 case CMD_INFO: 331 if (!open_cd(cdname, 0)) 332 return (0); 333 334 return info(arg); 335 336 case CMD_CDDB: 337 if (!open_cd(cdname, 0)) 338 return (0); 339 340 return cddbinfo(arg); 341 342 case CMD_CDID: 343 if (!open_cd(cdname, 0)) 344 return (0); 345 return cdid(); 346 347 case CMD_STATUS: 348 if (!open_cd(cdname, 0)) 349 return (0); 350 351 return pstatus(arg); 352 353 case CMD_PAUSE: 354 if (!open_cd(cdname, 0)) 355 return (0); 356 357 return ioctl(fd, CDIOCPAUSE); 358 359 case CMD_RESUME: 360 if (!open_cd(cdname, 0)) 361 return (0); 362 363 return ioctl(fd, CDIOCRESUME); 364 365 case CMD_STOP: 366 if (!open_cd(cdname, 0)) 367 return (0); 368 369 rc = ioctl(fd, CDIOCSTOP); 370 371 (void) ioctl(fd, CDIOCALLOW); 372 373 return (rc); 374 375 case CMD_RESET: 376 if (!open_cd(cdname, 0)) 377 return (0); 378 379 rc = ioctl(fd, CDIOCRESET); 380 if (rc < 0) 381 return rc; 382 close(fd); 383 fd = -1; 384 return (0); 385 386 case CMD_DEBUG: 387 if (!open_cd(cdname, 0)) 388 return (0); 389 390 if (!strcasecmp(arg, "on")) 391 return ioctl(fd, CDIOCSETDEBUG); 392 393 if (!strcasecmp(arg, "off")) 394 return ioctl(fd, CDIOCCLRDEBUG); 395 396 printf("%s: Invalid command arguments\n", __progname); 397 398 return (0); 399 400 case CMD_DEVICE: 401 /* close old device */ 402 if (fd > -1) { 403 (void) ioctl(fd, CDIOCALLOW); 404 close(fd); 405 fd = -1; 406 } 407 408 if (strlen(arg) == 0) { 409 printf("%s: Invalid parameter\n", __progname); 410 return (0); 411 } 412 413 /* open new device */ 414 if (!open_cd(arg, 0)) 415 return (0); 416 (void) strlcpy(newcdname, arg, sizeof(newcdname)); 417 cdname = newcdname; 418 return (1); 419 420 case CMD_EJECT: 421 if (!open_cd(cdname, 0)) 422 return (0); 423 424 (void) ioctl(fd, CDIOCALLOW); 425 rc = ioctl(fd, CDIOCEJECT); 426 if (rc < 0) 427 return (rc); 428 #if defined(__OpenBSD__) 429 close(fd); 430 fd = -1; 431 #endif 432 if (track_names) 433 free_names(track_names); 434 track_names = NULL; 435 return (0); 436 437 case CMD_CLOSE: 438 #if defined(CDIOCCLOSE) 439 if (!open_cd(cdname, 0)) 440 return (0); 441 442 (void) ioctl(fd, CDIOCALLOW); 443 rc = ioctl(fd, CDIOCCLOSE); 444 if (rc < 0) 445 return (rc); 446 close(fd); 447 fd = -1; 448 return (0); 449 #else 450 printf("%s: Command not yet supported\n", __progname); 451 return (0); 452 #endif 453 454 case CMD_PLAY: 455 if (!open_cd(cdname, 0)) 456 return (0); 457 458 while (isspace(*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 (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(*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(*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 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 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, 0640); 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 (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 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 < 0) 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(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 } 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 /* ARGSUSED */ 1029 int 1030 play_prev(char *arg) 1031 { 1032 int trk, min, sec, frm, rc; 1033 struct ioc_toc_header h; 1034 1035 if (status(&trk, &min, &sec, &frm) >= 0) { 1036 trk--; 1037 1038 rc = ioctl(fd, CDIOREADTOCHEADER, &h); 1039 if (rc < 0) { 1040 warn("getting toc header"); 1041 return (rc); 1042 } 1043 1044 if (trk < h.starting_track) 1045 return play_track(h.starting_track, 1, 1046 h.ending_track + 1, 1); 1047 return play_track(trk, 1, h.ending_track, 1); 1048 } 1049 1050 return (0); 1051 } 1052 1053 /* ARGSUSED */ 1054 int 1055 play_same(char *arg) 1056 { 1057 int trk, min, sec, frm, rc; 1058 struct ioc_toc_header h; 1059 1060 if (status (&trk, &min, &sec, &frm) >= 0) { 1061 rc = ioctl(fd, CDIOREADTOCHEADER, &h); 1062 if (rc < 0) { 1063 warn("getting toc header"); 1064 return (rc); 1065 } 1066 1067 return play_track(trk, 1, h.ending_track, 1); 1068 } 1069 1070 return (0); 1071 } 1072 1073 /* ARGSUSED */ 1074 int 1075 play_next(char *arg) 1076 { 1077 int trk, min, sec, frm, rc; 1078 struct ioc_toc_header h; 1079 1080 if (status(&trk, &min, &sec, &frm) >= 0) { 1081 trk++; 1082 rc = ioctl(fd, CDIOREADTOCHEADER, &h); 1083 if (rc < 0) { 1084 warn("getting toc header"); 1085 return (rc); 1086 } 1087 1088 if (trk > h.ending_track) { 1089 printf("%s: end of CD\n", __progname); 1090 1091 rc = ioctl(fd, CDIOCSTOP); 1092 1093 (void) ioctl(fd, CDIOCALLOW); 1094 1095 return (rc); 1096 } 1097 1098 return play_track(trk, 1, h.ending_track, 1); 1099 } 1100 1101 return (0); 1102 } 1103 1104 char * 1105 strstatus(int sts) 1106 { 1107 switch (sts) { 1108 case ASTS_INVALID: 1109 return ("invalid"); 1110 case ASTS_PLAYING: 1111 return ("playing"); 1112 case ASTS_PAUSED: 1113 return ("paused"); 1114 case ASTS_COMPLETED: 1115 return ("completed"); 1116 case ASTS_ERROR: 1117 return ("error"); 1118 case ASTS_VOID: 1119 return ("void"); 1120 default: 1121 return ("??"); 1122 } 1123 } 1124 1125 /* ARGSUSED */ 1126 int 1127 pstatus(char *arg) 1128 { 1129 struct ioc_vol v; 1130 struct ioc_read_subchannel ss; 1131 struct cd_sub_channel_info data; 1132 int rc, trk, m, s, f; 1133 char vis_catalog[1 + 4 * 15]; 1134 1135 rc = status(&trk, &m, &s, &f); 1136 if (rc >= 0) { 1137 if (verbose) { 1138 if (track_names) 1139 printf("Audio status = %d<%s>, " 1140 "current track = %d (%s)\n" 1141 "\tcurrent position = %d:%02d.%02d\n", 1142 rc, strstatus(rc), trk, 1143 trk ? track_names[trk-1] : "", m, s, f); 1144 else 1145 printf("Audio status = %d<%s>, " 1146 "current track = %d, " 1147 "current position = %d:%02d.%02d\n", 1148 rc, strstatus(rc), trk, m, s, f); 1149 } else 1150 printf("%d %d %d:%02d.%02d\n", rc, trk, m, s, f); 1151 } else 1152 printf("No current status info available\n"); 1153 1154 bzero(&ss, sizeof (ss)); 1155 ss.data = &data; 1156 ss.data_len = sizeof (data); 1157 ss.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT; 1158 ss.data_format = CD_MEDIA_CATALOG; 1159 rc = ioctl(fd, CDIOCREADSUBCHANNEL, (char *) &ss); 1160 if (rc >= 0) { 1161 printf("Media catalog is %sactive", 1162 ss.data->what.media_catalog.mc_valid ? "": "in"); 1163 if (ss.data->what.media_catalog.mc_valid && 1164 ss.data->what.media_catalog.mc_number[0]) { 1165 strvisx(vis_catalog, 1166 (char *)ss.data->what.media_catalog.mc_number, 1167 15, VIS_SAFE); 1168 printf(", number \"%.15s\"", vis_catalog); 1169 } 1170 putchar('\n'); 1171 } else 1172 printf("No media catalog info available\n"); 1173 1174 rc = ioctl(fd, CDIOCGETVOL, &v); 1175 if (rc >= 0) { 1176 if (verbose) 1177 printf("Left volume = %d, right volume = %d\n", 1178 v.vol[0], v.vol[1]); 1179 else 1180 printf("%d %d\n", v.vol[0], v.vol[1]); 1181 } else 1182 printf("No volume level info available\n"); 1183 return(0); 1184 } 1185 1186 int 1187 cdid(void) 1188 { 1189 unsigned long id; 1190 struct ioc_toc_header h; 1191 int rc, n; 1192 1193 rc = ioctl(fd, CDIOREADTOCHEADER, &h); 1194 if (rc == -1) { 1195 warn("getting toc header"); 1196 return (rc); 1197 } 1198 1199 n = h.ending_track - h.starting_track + 1; 1200 rc = read_toc_entrys((n + 1) * sizeof (struct cd_toc_entry)); 1201 if (rc < 0) 1202 return (rc); 1203 1204 id = cddb_discid(n, toc_buffer); 1205 if (id) { 1206 if (verbose) 1207 printf("CDID="); 1208 printf("%08lx\n", id); 1209 } 1210 return id ? 0 : 1; 1211 } 1212 1213 /* ARGSUSED */ 1214 int 1215 info(char *arg) 1216 { 1217 struct ioc_toc_header h; 1218 int rc, i, n; 1219 1220 if (get_media_capabilities(mediacap, 1) == -1) 1221 errx(1, "Can't determine media type"); 1222 1223 rc = ioctl(fd, CDIOREADTOCHEADER, &h); 1224 if (rc >= 0) { 1225 if (verbose) 1226 printf("Starting track = %d, ending track = %d, TOC size = %d bytes\n", 1227 h.starting_track, h.ending_track, h.len); 1228 else 1229 printf("%d %d %d\n", h.starting_track, 1230 h.ending_track, h.len); 1231 } else { 1232 warn("getting toc header"); 1233 return (rc); 1234 } 1235 1236 n = h.ending_track - h.starting_track + 1; 1237 rc = read_toc_entrys((n + 1) * sizeof (struct cd_toc_entry)); 1238 if (rc < 0) 1239 return (rc); 1240 1241 if (verbose) { 1242 printf("track start duration block length type\n"); 1243 printf("-------------------------------------------------\n"); 1244 } 1245 1246 for (i = 0; i < n; i++) { 1247 printf("%5d ", toc_buffer[i].track); 1248 prtrack(toc_buffer + i, 0, NULL); 1249 } 1250 printf("%5d ", toc_buffer[n].track); 1251 prtrack(toc_buffer + n, 1, NULL); 1252 return (0); 1253 } 1254 1255 int 1256 cddbinfo(char *arg) 1257 { 1258 struct ioc_toc_header h; 1259 int rc, i, n; 1260 1261 rc = ioctl(fd, CDIOREADTOCHEADER, &h); 1262 if (rc == -1) { 1263 warn("getting toc header"); 1264 return (rc); 1265 } 1266 1267 n = h.ending_track - h.starting_track + 1; 1268 rc = read_toc_entrys((n + 1) * sizeof (struct cd_toc_entry)); 1269 if (rc < 0) 1270 return (rc); 1271 1272 if (track_names) 1273 free_names(track_names); 1274 track_names = NULL; 1275 1276 track_names = cddb(cddb_host, n, toc_buffer, arg); 1277 if (!track_names) 1278 return(0); 1279 1280 printf("-------------------------------------------------\n"); 1281 1282 for (i = 0; i < n; i++) { 1283 printf("%5d ", toc_buffer[i].track); 1284 prtrack(toc_buffer + i, 0, track_names[i]); 1285 } 1286 printf("%5d ", toc_buffer[n].track); 1287 prtrack(toc_buffer + n, 1, ""); 1288 return (0); 1289 } 1290 1291 void 1292 lba2msf(unsigned long lba, u_char *m, u_char *s, u_char *f) 1293 { 1294 lba += 150; /* block start offset */ 1295 lba &= 0xffffff; /* negative lbas use only 24 bits */ 1296 *m = lba / (60 * 75); 1297 lba %= (60 * 75); 1298 *s = lba / 75; 1299 *f = lba % 75; 1300 } 1301 1302 unsigned int 1303 msf2lba(u_char m, u_char s, u_char f) 1304 { 1305 return (((m * 60) + s) * 75 + f) - 150; 1306 } 1307 1308 unsigned long 1309 entry2time(struct cd_toc_entry *e) 1310 { 1311 int block; 1312 u_char m, s, f; 1313 1314 if (msf) { 1315 return (e->addr.msf.minute * 60 + e->addr.msf.second); 1316 } else { 1317 block = e->addr.lba; 1318 lba2msf(block, &m, &s, &f); 1319 return (m*60+s); 1320 } 1321 } 1322 1323 unsigned long 1324 entry2frames(struct cd_toc_entry *e) 1325 { 1326 int block; 1327 unsigned char m, s, f; 1328 1329 if (msf) { 1330 return e->addr.msf.frame + e->addr.msf.second * 75 + 1331 e->addr.msf.minute * 60 * 75; 1332 } else { 1333 block = e->addr.lba; 1334 lba2msf(block, &m, &s, &f); 1335 return f + s * 75 + m * 60 * 75; 1336 } 1337 } 1338 1339 void 1340 prtrack(struct cd_toc_entry *e, int lastflag, char *name) 1341 { 1342 int block, next, len; 1343 u_char m, s, f; 1344 1345 if (msf) { 1346 if (!name || lastflag) 1347 /* Print track start */ 1348 printf("%2d:%02d.%02d ", e->addr.msf.minute, 1349 e->addr.msf.second, e->addr.msf.frame); 1350 1351 block = msf2lba(e->addr.msf.minute, e->addr.msf.second, 1352 e->addr.msf.frame); 1353 } else { 1354 block = e->addr.lba; 1355 if (!name || lastflag) { 1356 lba2msf(block, &m, &s, &f); 1357 /* Print track start */ 1358 printf("%2d:%02d.%02d ", m, s, f); 1359 } 1360 } 1361 if (lastflag) { 1362 if (!name) 1363 /* Last track -- print block */ 1364 printf(" - %6d - -\n", block); 1365 else 1366 printf("\n"); 1367 return; 1368 } 1369 1370 if (msf) 1371 next = msf2lba(e[1].addr.msf.minute, e[1].addr.msf.second, 1372 e[1].addr.msf.frame); 1373 else 1374 next = e[1].addr.lba; 1375 len = next - block; 1376 lba2msf(len - 150, &m, &s, &f); 1377 1378 if (name) 1379 printf("%2d:%02d.%02d %s\n", m, s, f, name); 1380 /* Print duration, block, length, type */ 1381 else 1382 printf("%2d:%02d.%02d %6d %6d %5s\n", m, s, f, block, len, 1383 (e->control & 4) ? "data" : "audio"); 1384 } 1385 1386 int 1387 play_track(int tstart, int istart, int tend, int iend) 1388 { 1389 struct ioc_play_track t; 1390 1391 t.start_track = tstart; 1392 t.start_index = istart; 1393 t.end_track = tend; 1394 t.end_index = iend; 1395 1396 return ioctl(fd, CDIOCPLAYTRACKS, &t); 1397 } 1398 1399 int 1400 play_blocks(int blk, int len) 1401 { 1402 struct ioc_play_blocks t; 1403 1404 t.blk = blk; 1405 t.len = len; 1406 1407 return ioctl(fd, CDIOCPLAYBLOCKS, &t); 1408 } 1409 1410 int 1411 setvol(int left, int right) 1412 { 1413 struct ioc_vol v; 1414 1415 v.vol[0] = left; 1416 v.vol[1] = right; 1417 v.vol[2] = 0; 1418 v.vol[3] = 0; 1419 1420 return ioctl(fd, CDIOCSETVOL, &v); 1421 } 1422 1423 int 1424 read_toc_entrys(int len) 1425 { 1426 struct ioc_read_toc_entry t; 1427 1428 if (toc_buffer) { 1429 free(toc_buffer); 1430 toc_buffer = 0; 1431 } 1432 1433 toc_buffer = malloc(len); 1434 1435 if (!toc_buffer) { 1436 errno = ENOMEM; 1437 return (-1); 1438 } 1439 1440 t.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT; 1441 t.starting_track = 0; 1442 t.data_len = len; 1443 t.data = toc_buffer; 1444 1445 return (ioctl(fd, CDIOREADTOCENTRYS, (char *) &t)); 1446 } 1447 1448 int 1449 play_msf(int start_m, int start_s, int start_f, int end_m, int end_s, int end_f) 1450 { 1451 struct ioc_play_msf a; 1452 1453 a.start_m = start_m; 1454 a.start_s = start_s; 1455 a.start_f = start_f; 1456 a.end_m = end_m; 1457 a.end_s = end_s; 1458 a.end_f = end_f; 1459 1460 return ioctl(fd, CDIOCPLAYMSF, (char *) &a); 1461 } 1462 1463 int 1464 status(int *trk, int *min, int *sec, int *frame) 1465 { 1466 struct ioc_read_subchannel s; 1467 struct cd_sub_channel_info data; 1468 u_char mm, ss, ff; 1469 1470 bzero(&s, sizeof (s)); 1471 s.data = &data; 1472 s.data_len = sizeof (data); 1473 s.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT; 1474 s.data_format = CD_CURRENT_POSITION; 1475 1476 if (ioctl(fd, CDIOCREADSUBCHANNEL, (char *) &s) < 0) 1477 return -1; 1478 1479 *trk = s.data->what.position.track_number; 1480 if (msf) { 1481 *min = s.data->what.position.reladdr.msf.minute; 1482 *sec = s.data->what.position.reladdr.msf.second; 1483 *frame = s.data->what.position.reladdr.msf.frame; 1484 } else { 1485 /* 1486 * NOTE: CDIOCREADSUBCHANNEL does not put the lba info into 1487 * host order like CDIOREADTOCENTRYS does. 1488 */ 1489 lba2msf(betoh32(s.data->what.position.reladdr.lba), &mm, &ss, 1490 &ff); 1491 *min = mm; 1492 *sec = ss; 1493 *frame = ff; 1494 } 1495 1496 return s.data->header.audio_status; 1497 } 1498 1499 char * 1500 input(int *cmd) 1501 { 1502 char *buf; 1503 int siz = 0; 1504 char *p; 1505 HistEvent hev; 1506 1507 do { 1508 if ((buf = (char *) el_gets(el, &siz)) == NULL || !siz) { 1509 *cmd = CMD_QUIT; 1510 fprintf(stderr, "\r\n"); 1511 return (0); 1512 } 1513 if (strlen(buf) > 1) 1514 history(hist, &hev, H_ENTER, buf); 1515 p = parse(buf, cmd); 1516 } while (!p); 1517 return (p); 1518 } 1519 1520 char * 1521 parse(char *buf, int *cmd) 1522 { 1523 struct cmdtab *c; 1524 char *p; 1525 size_t len; 1526 1527 for (p=buf; isspace(*p); p++) 1528 continue; 1529 1530 if (isdigit(*p) || (p[0] == '#' && isdigit(p[1]))) { 1531 *cmd = CMD_PLAY; 1532 return (p); 1533 } 1534 1535 for (buf = p; *p && ! isspace(*p); p++) 1536 continue; 1537 1538 len = p - buf; 1539 if (!len) 1540 return (0); 1541 1542 if (*p) { /* It must be a spacing character! */ 1543 char *q; 1544 1545 *p++ = 0; 1546 for (q=p; *q && *q != '\n' && *q != '\r'; q++) 1547 continue; 1548 *q = 0; 1549 } 1550 1551 *cmd = -1; 1552 for (c=cmdtab; c->name; ++c) { 1553 /* Is it an exact match? */ 1554 if (!strcasecmp(buf, c->name)) { 1555 *cmd = c->command; 1556 break; 1557 } 1558 1559 /* Try short hand forms then... */ 1560 if (len >= c->min && ! strncasecmp(buf, c->name, len)) { 1561 if (*cmd != -1 && *cmd != c->command) { 1562 fprintf(stderr, "Ambiguous command\n"); 1563 return (0); 1564 } 1565 *cmd = c->command; 1566 } 1567 } 1568 1569 if (*cmd == -1) { 1570 fprintf(stderr, "%s: Invalid command, enter ``help'' for commands.\n", 1571 __progname); 1572 return (0); 1573 } 1574 1575 while (isspace(*p)) 1576 p++; 1577 return p; 1578 } 1579 1580 int 1581 open_cd(char *dev, int needwrite) 1582 { 1583 char *realdev; 1584 int tries; 1585 1586 if (fd > -1) { 1587 if (needwrite && !writeperm) { 1588 close(fd); 1589 fd = -1; 1590 } else 1591 return (1); 1592 } 1593 1594 for (tries = 0; fd < 0 && tries < 10; tries++) { 1595 if (needwrite) 1596 fd = opendev(dev, O_RDWR, OPENDEV_PART, &realdev); 1597 else 1598 fd = opendev(dev, O_RDONLY, OPENDEV_PART, &realdev); 1599 if (fd < 0) { 1600 if (errno == ENXIO) { 1601 /* ENXIO has an overloaded meaning here. 1602 * The original "Device not configured" should 1603 * be interpreted as "No disc in drive %s". */ 1604 warnx("No disc in drive %s.", realdev); 1605 return (0); 1606 } else if (errno != EIO) { 1607 /* EIO may simply mean the device is not ready 1608 * yet which is common with CD changers. */ 1609 warn("Can't open %s", realdev); 1610 return (0); 1611 } 1612 } 1613 sleep(1); 1614 } 1615 if (fd < 0) { 1616 warn("Can't open %s", realdev); 1617 return (0); 1618 } 1619 writeperm = needwrite; 1620 return (1); 1621 } 1622 1623 char * 1624 prompt(void) 1625 { 1626 return (verbose ? "cdio> " : ""); 1627 } 1628 1629 void 1630 switch_el(void) 1631 { 1632 HistEvent hev; 1633 1634 if (el == NULL && hist == NULL) { 1635 el = el_init(__progname, stdin, stdout, stderr); 1636 hist = history_init(); 1637 history(hist, &hev, H_SETSIZE, 100); 1638 el_set(el, EL_HIST, history, hist); 1639 el_set(el, EL_EDITOR, "emacs"); 1640 el_set(el, EL_PROMPT, prompt); 1641 el_set(el, EL_SIGNAL, 1); 1642 el_source(el, NULL); 1643 1644 } else { 1645 if (hist != NULL) { 1646 history_end(hist); 1647 hist = NULL; 1648 } 1649 if (el != NULL) { 1650 el_end(el); 1651 el = NULL; 1652 } 1653 } 1654 } 1655 1656 void 1657 addmsf(u_int *m, u_int *s, u_int *f, u_char m_inc, u_char s_inc, u_char f_inc) 1658 { 1659 *f += f_inc; 1660 if (*f > 75) { 1661 *s += *f / 75; 1662 *f %= 75; 1663 } 1664 1665 *s += s_inc; 1666 if (*s > 60) { 1667 *m += *s / 60; 1668 *s %= 60; 1669 } 1670 1671 *m += m_inc; 1672 } 1673 1674 int 1675 cmpmsf(u_char m1, u_char s1, u_char f1, u_char m2, u_char s2, u_char f2) 1676 { 1677 if (m1 > m2) 1678 return (1); 1679 else if (m1 < m2) 1680 return (-1); 1681 1682 if (s1 > s2) 1683 return (1); 1684 else if (s1 < s2) 1685 return (-1); 1686 1687 if (f1 > f2) 1688 return (1); 1689 else if (f1 < f2) 1690 return (-1); 1691 1692 return (0); 1693 } 1694 1695 void 1696 toc2msf(u_int track, u_char *m, u_char *s, u_char *f) 1697 { 1698 struct cd_toc_entry *ctep; 1699 1700 ctep = &toc_buffer[track - 1]; 1701 1702 if (msf) { 1703 *m = ctep->addr.msf.minute; 1704 *s = ctep->addr.msf.second; 1705 *f = ctep->addr.msf.frame; 1706 } else 1707 lba2msf(ctep->addr.lba, m, s, f); 1708 } 1709