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