1 /* 2 * Compact Disc Control Utility by Serge V. Vakulenko <vak@cronyx.ru>. 3 * Based on the non-X based CD player by Jean-Marc Zucconi and 4 * Andrey A. Chernov. 5 * 6 * Fixed and further modified on 5-Sep-1995 by Jukka Ukkonen <jau@funet.fi>. 7 * 8 * 11-Sep-1995: Jukka A. Ukkonen <jau@funet.fi> 9 * A couple of further fixes to my own earlier "fixes". 10 * 11 * 18-Sep-1995: Jukka A. Ukkonen <jau@funet.fi> 12 * Added an ability to specify addresses relative to the 13 * beginning of a track. This is in fact a variation of 14 * doing the simple play_msf() call. 15 * 16 * 11-Oct-1995: Serge V.Vakulenko <vak@cronyx.ru> 17 * New eject algorithm. 18 * Some code style reformatting. 19 */ 20 21 #ifndef lint 22 static const char rcsid[] = 23 "$FreeBSD$"; 24 #endif /* not lint */ 25 26 #include <ctype.h> 27 #include <err.h> 28 #include <errno.h> 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include <unistd.h> 33 #include <sys/file.h> 34 #include <sys/cdio.h> 35 #include <sys/ioctl.h> 36 #include <histedit.h> 37 38 #define VERSION "2.0" 39 40 #define ASTS_INVALID 0x00 /* Audio status byte not valid */ 41 #define ASTS_PLAYING 0x11 /* Audio play operation in progress */ 42 #define ASTS_PAUSED 0x12 /* Audio play operation paused */ 43 #define ASTS_COMPLETED 0x13 /* Audio play operation successfully completed */ 44 #define ASTS_ERROR 0x14 /* Audio play operation stopped due to error */ 45 #define ASTS_VOID 0x15 /* No current audio status to return */ 46 47 #ifndef DEFAULT_CD_DRIVE 48 # define DEFAULT_CD_DRIVE "/dev/cd0c" 49 #endif 50 51 #ifndef DEFAULT_CD_PARTITION 52 # define DEFAULT_CD_PARTITION "c" 53 #endif 54 55 #define CMD_DEBUG 1 56 #define CMD_EJECT 2 57 #define CMD_HELP 3 58 #define CMD_INFO 4 59 #define CMD_PAUSE 5 60 #define CMD_PLAY 6 61 #define CMD_QUIT 7 62 #define CMD_RESUME 8 63 #define CMD_STOP 9 64 #define CMD_VOLUME 10 65 #define CMD_CLOSE 11 66 #define CMD_RESET 12 67 #define CMD_SET 13 68 #define CMD_STATUS 14 69 #define STATUS_AUDIO 0x1 70 #define STATUS_MEDIA 0x2 71 #define STATUS_VOLUME 0x4 72 73 struct cmdtab { 74 int command; 75 char *name; 76 unsigned min; 77 char *args; 78 } cmdtab[] = { 79 { CMD_CLOSE, "close", 1, "" }, 80 { CMD_DEBUG, "debug", 1, "on | off" }, 81 { CMD_EJECT, "eject", 1, "" }, 82 { CMD_HELP, "?", 1, 0 }, 83 { CMD_HELP, "help", 1, "" }, 84 { CMD_INFO, "info", 1, "" }, 85 { CMD_PAUSE, "pause", 2, "" }, 86 { CMD_PLAY, "play", 1, "min1:sec1[.fram1] [min2:sec2[.fram2]]" }, 87 { CMD_PLAY, "play", 1, "track1[.index1] [track2[.index2]]" }, 88 { CMD_PLAY, "play", 1, "tr1 m1:s1[.f1] [[tr2] [m2:s2[.f2]]]" }, 89 { CMD_PLAY, "play", 1, "[#block [len]]" }, 90 { CMD_QUIT, "quit", 1, "" }, 91 { CMD_RESET, "reset", 4, "" }, 92 { CMD_RESUME, "resume", 1, "" }, 93 { CMD_SET, "set", 2, "msf | lba" }, 94 { CMD_STATUS, "status", 1, "[audio | media | volume]" }, 95 { CMD_STOP, "stop", 3, "" }, 96 { CMD_VOLUME, "volume", 1, "<l> <r> | left | right | mute | mono | stereo" }, 97 { 0, } 98 }; 99 100 struct cd_toc_entry toc_buffer[100]; 101 102 const char *cdname; 103 int fd = -1; 104 int verbose = 1; 105 int msf = 1; 106 107 int setvol __P((int, int)); 108 int read_toc_entrys __P((int)); 109 int play_msf __P((int, int, int, int, int, int)); 110 int play_track __P((int, int, int, int)); 111 int get_vol __P((int *, int *)); 112 int status __P((int *, int *, int *, int *)); 113 int open_cd __P((void)); 114 int play __P((char *arg)); 115 int info __P((char *arg)); 116 int pstatus __P((char *arg)); 117 char *input __P((int *)); 118 void prtrack __P((struct cd_toc_entry *e, int lastflag)); 119 void lba2msf __P((unsigned long lba, 120 u_char *m, u_char *s, u_char *f)); 121 unsigned int msf2lba __P((u_char m, u_char s, u_char f)); 122 int play_blocks __P((int blk, int len)); 123 int run __P((int cmd, char *arg)); 124 char *parse __P((char *buf, int *cmd)); 125 126 void help () 127 { 128 struct cmdtab *c; 129 char *s, n; 130 int i; 131 132 for (c=cmdtab; c->name; ++c) { 133 if (! c->args) 134 continue; 135 printf("\t"); 136 for (i = c->min, s = c->name; *s; s++, i--) { 137 if (i > 0) 138 n = toupper(*s); 139 else 140 n = *s; 141 putchar(n); 142 } 143 if (*c->args) 144 printf (" %s", c->args); 145 printf ("\n"); 146 } 147 printf ("\n\tThe word \"play\" is not required for the play commands.\n"); 148 printf ("\tThe plain target address is taken as a synonym for play.\n"); 149 } 150 151 void usage () 152 { 153 fprintf (stderr, "usage: cdcontrol [-sv] [-f device] [command ...]\n"); 154 exit (1); 155 } 156 157 int main (int argc, char **argv) 158 { 159 int cmd; 160 char *arg; 161 162 cdname = getenv ("MUSIC_CD"); 163 if (! cdname) 164 cdname = getenv ("CD_DRIVE"); 165 if (! cdname) 166 cdname = getenv ("DISC"); 167 if (! cdname) 168 cdname = getenv ("CDPLAY"); 169 170 for (;;) { 171 switch (getopt (argc, argv, "svhf:")) { 172 case EOF: 173 break; 174 case 's': 175 verbose = 0; 176 continue; 177 case 'v': 178 verbose = 2; 179 continue; 180 case 'f': 181 cdname = optarg; 182 continue; 183 case 'h': 184 default: 185 usage (); 186 } 187 break; 188 } 189 argc -= optind; 190 argv += optind; 191 192 if (argc > 0 && ! strcasecmp (*argv, "help")) 193 usage (); 194 195 if (! cdname) { 196 cdname = DEFAULT_CD_DRIVE; 197 warnx("no CD device name specified, defaulting to %s", cdname); 198 } 199 200 if (argc > 0) { 201 char buf[80], *p; 202 int len; 203 204 for (p=buf; argc-->0; ++argv) { 205 len = strlen (*argv); 206 207 if (p + len >= buf + sizeof (buf) - 1) 208 usage (); 209 210 if (p > buf) 211 *p++ = ' '; 212 213 strcpy (p, *argv); 214 p += len; 215 } 216 *p = 0; 217 arg = parse (buf, &cmd); 218 return (run (cmd, arg)); 219 } 220 221 if (verbose == 1) 222 verbose = isatty (0); 223 224 if (verbose) { 225 printf ("Compact Disc Control utility, version %s\n", VERSION); 226 printf ("Type `?' for command list\n\n"); 227 } 228 229 for (;;) { 230 arg = input (&cmd); 231 if (run (cmd, arg) < 0) { 232 if (verbose) 233 warn(NULL); 234 close (fd); 235 fd = -1; 236 } 237 fflush (stdout); 238 } 239 } 240 241 int run (int cmd, char *arg) 242 { 243 int l, r, rc; 244 245 switch (cmd) { 246 247 case CMD_QUIT: 248 exit (0); 249 250 case CMD_INFO: 251 if (fd < 0 && ! open_cd ()) 252 return (0); 253 254 return info (arg); 255 256 case CMD_STATUS: 257 if (fd < 0 && ! open_cd ()) 258 return (0); 259 260 return pstatus (arg); 261 262 case CMD_PAUSE: 263 if (fd < 0 && ! open_cd ()) 264 return (0); 265 266 return ioctl (fd, CDIOCPAUSE); 267 268 case CMD_RESUME: 269 if (fd < 0 && ! open_cd ()) 270 return (0); 271 272 return ioctl (fd, CDIOCRESUME); 273 274 case CMD_STOP: 275 if (fd < 0 && ! open_cd ()) 276 return (0); 277 278 rc = ioctl (fd, CDIOCSTOP); 279 280 (void) ioctl (fd, CDIOCALLOW); 281 282 return (rc); 283 284 case CMD_RESET: 285 if (fd < 0 && ! open_cd ()) 286 return (0); 287 288 rc = ioctl (fd, CDIOCRESET); 289 if (rc < 0) 290 return rc; 291 close(fd); 292 fd = -1; 293 return (0); 294 295 case CMD_DEBUG: 296 if (fd < 0 && ! open_cd ()) 297 return (0); 298 299 if (! strcasecmp (arg, "on")) 300 return ioctl (fd, CDIOCSETDEBUG); 301 302 if (! strcasecmp (arg, "off")) 303 return ioctl (fd, CDIOCCLRDEBUG); 304 305 warnx("invalid command arguments"); 306 307 return (0); 308 309 case CMD_EJECT: 310 if (fd < 0 && ! open_cd ()) 311 return (0); 312 313 (void) ioctl (fd, CDIOCALLOW); 314 rc = ioctl (fd, CDIOCEJECT); 315 if (rc < 0) 316 return (rc); 317 return (0); 318 319 case CMD_CLOSE: 320 if (fd < 0 && ! open_cd ()) 321 return (0); 322 323 (void) ioctl (fd, CDIOCALLOW); 324 rc = ioctl (fd, CDIOCCLOSE); 325 if (rc < 0) 326 return (rc); 327 close(fd); 328 fd = -1; 329 return (0); 330 331 case CMD_PLAY: 332 if (fd < 0 && ! open_cd ()) 333 return (0); 334 335 while (isspace (*arg)) 336 arg++; 337 338 return play (arg); 339 340 case CMD_SET: 341 if (! strcasecmp (arg, "msf")) 342 msf = 1; 343 else if (! strcasecmp (arg, "lba")) 344 msf = 0; 345 else 346 warnx("invalid command arguments"); 347 return (0); 348 349 case CMD_VOLUME: 350 if (fd < 0 && !open_cd ()) 351 return (0); 352 353 if (! strncasecmp (arg, "left", strlen(arg))) 354 return ioctl (fd, CDIOCSETLEFT); 355 356 if (! strncasecmp (arg, "right", strlen(arg))) 357 return ioctl (fd, CDIOCSETRIGHT); 358 359 if (! strncasecmp (arg, "mono", strlen(arg))) 360 return ioctl (fd, CDIOCSETMONO); 361 362 if (! strncasecmp (arg, "stereo", strlen(arg))) 363 return ioctl (fd, CDIOCSETSTERIO); 364 365 if (! strncasecmp (arg, "mute", strlen(arg))) 366 return ioctl (fd, CDIOCSETMUTE); 367 368 if (2 != sscanf (arg, "%d %d", &l, &r)) { 369 warnx("invalid command arguments"); 370 return (0); 371 } 372 373 return setvol (l, r); 374 375 default: 376 case CMD_HELP: 377 help (); 378 return (0); 379 380 } 381 } 382 383 int play (char *arg) 384 { 385 struct ioc_toc_header h; 386 int rc, n, start, end = 0, istart = 1, iend = 1; 387 388 rc = ioctl (fd, CDIOREADTOCHEADER, &h); 389 390 if (rc < 0) 391 return (rc); 392 393 n = h.ending_track - h.starting_track + 1; 394 rc = read_toc_entrys ((n + 1) * sizeof (struct cd_toc_entry)); 395 396 if (rc < 0) 397 return (rc); 398 399 if (! arg || ! *arg) { 400 /* Play the whole disc */ 401 if (msf) 402 return play_blocks (0, msf2lba (toc_buffer[n].addr.msf.minute, 403 toc_buffer[n].addr.msf.second, 404 toc_buffer[n].addr.msf.frame)); 405 else 406 return play_blocks (0, ntohl(toc_buffer[n].addr.lba)); 407 } 408 409 if (strchr (arg, '#')) { 410 /* Play block #blk [ len ] */ 411 int blk, len = 0; 412 413 if (2 != sscanf (arg, "#%d%d", &blk, &len) && 414 1 != sscanf (arg, "#%d", &blk)) 415 goto Clean_up; 416 417 if (len == 0) { 418 if (msf) 419 len = msf2lba (toc_buffer[n].addr.msf.minute, 420 toc_buffer[n].addr.msf.second, 421 toc_buffer[n].addr.msf.frame) - blk; 422 else 423 len = ntohl(toc_buffer[n].addr.lba) - blk; 424 } 425 return play_blocks (blk, len); 426 } 427 428 if (strchr (arg, ':')) { 429 /* 430 * Play MSF m1:s1 [ .f1 ] [ m2:s2 [ .f2 ] ] 431 * 432 * Will now also undestand timed addresses relative 433 * to the beginning of a track in the form... 434 * 435 * tr1 m1:s1[.f1] [[tr2] [m2:s2[.f2]]] 436 */ 437 unsigned tr1, tr2; 438 unsigned m1, m2, s1, s2, f1, f2; 439 unsigned char tm, ts, tf; 440 441 tr2 = m2 = s2 = f2 = f1 = 0; 442 if (8 == sscanf (arg, "%d %d:%d.%d %d %d:%d.%d", 443 &tr1, &m1, &s1, &f1, &tr2, &m2, &s2, &f2)) 444 goto Play_Relative_Addresses; 445 446 tr2 = m2 = s2 = f2 = f1 = 0; 447 if (7 == sscanf (arg, "%d %d:%d %d %d:%d.%d", 448 &tr1, &m1, &s1, &tr2, &m2, &s2, &f2)) 449 goto Play_Relative_Addresses; 450 451 tr2 = m2 = s2 = f2 = f1 = 0; 452 if (7 == sscanf (arg, "%d %d:%d.%d %d %d:%d", 453 &tr1, &m1, &s1, &f1, &tr2, &m2, &s2)) 454 goto Play_Relative_Addresses; 455 456 tr2 = m2 = s2 = f2 = f1 = 0; 457 if (7 == sscanf (arg, "%d %d:%d.%d %d:%d.%d", 458 &tr1, &m1, &s1, &f1, &m2, &s2, &f2)) 459 goto Play_Relative_Addresses; 460 461 tr2 = m2 = s2 = f2 = f1 = 0; 462 if (6 == sscanf (arg, "%d %d:%d.%d %d:%d", 463 &tr1, &m1, &s1, &f1, &m2, &s2)) 464 goto Play_Relative_Addresses; 465 466 tr2 = m2 = s2 = f2 = f1 = 0; 467 if (6 == sscanf (arg, "%d %d:%d %d:%d.%d", 468 &tr1, &m1, &s1, &m2, &s2, &f2)) 469 goto Play_Relative_Addresses; 470 471 tr2 = m2 = s2 = f2 = f1 = 0; 472 if (6 == sscanf (arg, "%d %d:%d.%d %d %d", 473 &tr1, &m1, &s1, &f1, &tr2, &m2)) 474 goto Play_Relative_Addresses; 475 476 tr2 = m2 = s2 = f2 = f1 = 0; 477 if (5 == sscanf (arg, "%d %d:%d %d:%d", &tr1, &m1, &s1, &m2, &s2)) 478 goto Play_Relative_Addresses; 479 480 tr2 = m2 = s2 = f2 = f1 = 0; 481 if (5 == sscanf (arg, "%d %d:%d %d %d", 482 &tr1, &m1, &s1, &tr2, &m2)) 483 goto Play_Relative_Addresses; 484 485 tr2 = m2 = s2 = f2 = f1 = 0; 486 if (5 == sscanf (arg, "%d %d:%d.%d %d", 487 &tr1, &m1, &s1, &f1, &tr2)) 488 goto Play_Relative_Addresses; 489 490 tr2 = m2 = s2 = f2 = f1 = 0; 491 if (4 == sscanf (arg, "%d %d:%d %d", &tr1, &m1, &s1, &tr2)) 492 goto Play_Relative_Addresses; 493 494 tr2 = m2 = s2 = f2 = f1 = 0; 495 if (4 == sscanf (arg, "%d %d:%d.%d", &tr1, &m1, &s1, &f1)) 496 goto Play_Relative_Addresses; 497 498 tr2 = m2 = s2 = f2 = f1 = 0; 499 if (3 == sscanf (arg, "%d %d:%d", &tr1, &m1, &s1)) 500 goto Play_Relative_Addresses; 501 502 tr2 = m2 = s2 = f2 = f1 = 0; 503 goto Try_Absolute_Timed_Addresses; 504 505 Play_Relative_Addresses: 506 if (! tr1) 507 tr1 = 1; 508 else if (tr1 > n) 509 tr1 = n; 510 511 if (msf) { 512 tm = toc_buffer[tr1].addr.msf.minute; 513 ts = toc_buffer[tr1].addr.msf.second; 514 tf = toc_buffer[tr1].addr.msf.frame; 515 } else 516 lba2msf(ntohl(toc_buffer[tr1].addr.lba), 517 &tm, &ts, &tf); 518 if ((m1 > tm) 519 || ((m1 == tm) 520 && ((s1 > ts) 521 || ((s1 == ts) 522 && (f1 > tf))))) { 523 printf ("Track %d is not that long.\n", tr1); 524 return (0); 525 } 526 527 tr1--; 528 529 f1 += tf; 530 if (f1 >= 75) { 531 s1 += f1 / 75; 532 f1 %= 75; 533 } 534 535 s1 += ts; 536 if (s1 >= 60) { 537 m1 += s1 / 60; 538 s1 %= 60; 539 } 540 541 m1 += tm; 542 543 if (! tr2) { 544 if (m2 || s2 || f2) { 545 tr2 = tr1; 546 f2 += f1; 547 if (f2 >= 75) { 548 s2 += f2 / 75; 549 f2 %= 75; 550 } 551 552 s2 += s1; 553 if (s2 > 60) { 554 m2 += s2 / 60; 555 s2 %= 60; 556 } 557 558 m2 += m1; 559 } else { 560 tr2 = n; 561 if (msf) { 562 m2 = toc_buffer[n].addr.msf.minute; 563 s2 = toc_buffer[n].addr.msf.second; 564 f2 = toc_buffer[n].addr.msf.frame; 565 } else { 566 lba2msf(ntohl(toc_buffer[n].addr.lba), 567 &tm, &ts, &tf); 568 m2 = tm; 569 s2 = ts; 570 f2 = tf; 571 } 572 } 573 } else if (tr2 > n) { 574 tr2 = n; 575 m2 = s2 = f2 = 0; 576 } else { 577 if (m2 || s2 || f2) 578 tr2--; 579 if (msf) { 580 tm = toc_buffer[tr2].addr.msf.minute; 581 ts = toc_buffer[tr2].addr.msf.second; 582 tf = toc_buffer[tr2].addr.msf.frame; 583 } else 584 lba2msf(ntohl(toc_buffer[tr2].addr.lba), 585 &tm, &ts, &tf); 586 f2 += tf; 587 if (f2 >= 75) { 588 s2 += f2 / 75; 589 f2 %= 75; 590 } 591 592 s2 += ts; 593 if (s2 > 60) { 594 m2 += s2 / 60; 595 s2 %= 60; 596 } 597 598 m2 += tm; 599 } 600 601 if (msf) { 602 tm = toc_buffer[n].addr.msf.minute; 603 ts = toc_buffer[n].addr.msf.second; 604 tf = toc_buffer[n].addr.msf.frame; 605 } else 606 lba2msf(ntohl(toc_buffer[n].addr.lba), 607 &tm, &ts, &tf); 608 if ((tr2 < n) 609 && ((m2 > tm) 610 || ((m2 == tm) 611 && ((s2 > ts) 612 || ((s2 == ts) 613 && (f2 > tf)))))) { 614 printf ("The playing time of the disc is not that long.\n"); 615 return (0); 616 } 617 return (play_msf (m1, s1, f1, m2, s2, f2)); 618 619 Try_Absolute_Timed_Addresses: 620 if (6 != sscanf (arg, "%d:%d.%d%d:%d.%d", 621 &m1, &s1, &f1, &m2, &s2, &f2) && 622 5 != sscanf (arg, "%d:%d.%d%d:%d", &m1, &s1, &f1, &m2, &s2) && 623 5 != sscanf (arg, "%d:%d%d:%d.%d", &m1, &s1, &m2, &s2, &f2) && 624 3 != sscanf (arg, "%d:%d.%d", &m1, &s1, &f1) && 625 4 != sscanf (arg, "%d:%d%d:%d", &m1, &s1, &m2, &s2) && 626 2 != sscanf (arg, "%d:%d", &m1, &s1)) 627 goto Clean_up; 628 629 if (m2 == 0) { 630 if (msf) { 631 m2 = toc_buffer[n].addr.msf.minute; 632 s2 = toc_buffer[n].addr.msf.second; 633 f2 = toc_buffer[n].addr.msf.frame; 634 } else { 635 lba2msf(ntohl(toc_buffer[n].addr.lba), 636 &tm, &ts, &tf); 637 m2 = tm; 638 s2 = ts; 639 f2 = tf; 640 } 641 } 642 return play_msf (m1, s1, f1, m2, s2, f2); 643 } 644 645 /* 646 * Play track trk1 [ .idx1 ] [ trk2 [ .idx2 ] ] 647 */ 648 if (4 != sscanf (arg, "%d.%d%d.%d", &start, &istart, &end, &iend) && 649 3 != sscanf (arg, "%d.%d%d", &start, &istart, &end) && 650 3 != sscanf (arg, "%d%d.%d", &start, &end, &iend) && 651 2 != sscanf (arg, "%d.%d", &start, &istart) && 652 2 != sscanf (arg, "%d%d", &start, &end) && 653 1 != sscanf (arg, "%d", &start)) 654 goto Clean_up; 655 656 if (end == 0) 657 end = n; 658 return (play_track (start, istart, end, iend)); 659 660 Clean_up: 661 warnx("invalid command arguments"); 662 return (0); 663 } 664 665 char *strstatus (int sts) 666 { 667 switch (sts) { 668 case ASTS_INVALID: return ("invalid"); 669 case ASTS_PLAYING: return ("playing"); 670 case ASTS_PAUSED: return ("paused"); 671 case ASTS_COMPLETED: return ("completed"); 672 case ASTS_ERROR: return ("error"); 673 case ASTS_VOID: return ("void"); 674 default: return ("??"); 675 } 676 } 677 678 int pstatus (char *arg) 679 { 680 struct ioc_vol v; 681 struct ioc_read_subchannel ss; 682 struct cd_sub_channel_info data; 683 int rc, trk, m, s, f; 684 int what = 0; 685 char *p; 686 687 while ((p = strtok(arg, " \t"))) { 688 arg = 0; 689 if (!strncasecmp(p, "audio", strlen(p))) 690 what |= STATUS_AUDIO; 691 else if (!strncasecmp(p, "media", strlen(p))) 692 what |= STATUS_MEDIA; 693 else if (!strncasecmp(p, "volume", strlen(p))) 694 what |= STATUS_VOLUME; 695 else { 696 warnx("invalid command arguments"); 697 return 0; 698 } 699 } 700 if (!what) 701 what = STATUS_AUDIO|STATUS_MEDIA|STATUS_VOLUME; 702 if (what & STATUS_AUDIO) { 703 rc = status (&trk, &m, &s, &f); 704 if (rc >= 0) 705 if (verbose) 706 printf ("Audio status = %d<%s>, current track = %d, current position = %d:%02d.%02d\n", 707 rc, strstatus (rc), trk, m, s, f); 708 else 709 printf ("%d %d %d:%02d.%02d\n", rc, trk, m, s, f); 710 else 711 printf ("No current status info available\n"); 712 } 713 if (what & STATUS_MEDIA) { 714 bzero (&ss, sizeof (ss)); 715 ss.data = &data; 716 ss.data_len = sizeof (data); 717 ss.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT; 718 ss.data_format = CD_MEDIA_CATALOG; 719 rc = ioctl (fd, CDIOCREADSUBCHANNEL, (char *) &ss); 720 if (rc >= 0) { 721 printf("Media catalog is %sactive", 722 ss.data->what.media_catalog.mc_valid ? "": "in"); 723 if (ss.data->what.media_catalog.mc_valid && 724 ss.data->what.media_catalog.mc_number[0]) 725 printf(", number \"%.15s\"", 726 ss.data->what.media_catalog.mc_number); 727 putchar('\n'); 728 } else 729 printf("No media catalog info available\n"); 730 } 731 if (what & STATUS_VOLUME) { 732 rc = ioctl (fd, CDIOCGETVOL, &v); 733 if (rc >= 0) 734 if (verbose) 735 printf ("Left volume = %d, right volume = %d\n", 736 v.vol[0], v.vol[1]); 737 else 738 printf ("%d %d\n", v.vol[0], v.vol[1]); 739 else 740 printf ("No volume level info available\n"); 741 } 742 return(0); 743 } 744 745 int info (char *arg) 746 { 747 struct ioc_toc_header h; 748 int rc, i, n; 749 750 rc = ioctl (fd, CDIOREADTOCHEADER, &h); 751 if (rc >= 0) { 752 if (verbose) 753 printf ("Starting track = %d, ending track = %d, TOC size = %d bytes\n", 754 h.starting_track, h.ending_track, h.len); 755 else 756 printf ("%d %d %d\n", h.starting_track, 757 h.ending_track, h.len); 758 } else { 759 warn("getting toc header"); 760 return (rc); 761 } 762 763 n = h.ending_track - h.starting_track + 1; 764 rc = read_toc_entrys ((n + 1) * sizeof (struct cd_toc_entry)); 765 if (rc < 0) 766 return (rc); 767 768 if (verbose) { 769 printf ("track start duration block length type\n"); 770 printf ("-------------------------------------------------\n"); 771 } 772 773 for (i = 0; i < n; i++) { 774 printf ("%5d ", toc_buffer[i].track); 775 prtrack (toc_buffer + i, 0); 776 } 777 printf ("%5d ", toc_buffer[n].track); 778 prtrack (toc_buffer + n, 1); 779 return (0); 780 } 781 782 void lba2msf (unsigned long lba, u_char *m, u_char *s, u_char *f) 783 { 784 lba += 150; /* block start offset */ 785 lba &= 0xffffff; /* negative lbas use only 24 bits */ 786 *m = lba / (60 * 75); 787 lba %= (60 * 75); 788 *s = lba / 75; 789 *f = lba % 75; 790 } 791 792 unsigned int msf2lba (u_char m, u_char s, u_char f) 793 { 794 return (((m * 60) + s) * 75 + f) - 150; 795 } 796 797 void prtrack (struct cd_toc_entry *e, int lastflag) 798 { 799 int block, next, len; 800 u_char m, s, f; 801 802 if (msf) { 803 /* Print track start */ 804 printf ("%2d:%02d.%02d ", e->addr.msf.minute, 805 e->addr.msf.second, e->addr.msf.frame); 806 807 block = msf2lba (e->addr.msf.minute, e->addr.msf.second, 808 e->addr.msf.frame); 809 } else { 810 block = ntohl(e->addr.lba); 811 lba2msf(block, &m, &s, &f); 812 /* Print track start */ 813 printf ("%2d:%02d.%02d ", m, s, f); 814 } 815 if (lastflag) { 816 /* Last track -- print block */ 817 printf (" - %6d - -\n", block); 818 return; 819 } 820 821 if (msf) 822 next = msf2lba (e[1].addr.msf.minute, e[1].addr.msf.second, 823 e[1].addr.msf.frame); 824 else 825 next = ntohl(e[1].addr.lba); 826 len = next - block; 827 lba2msf (len, &m, &s, &f); 828 829 /* Print duration, block, length, type */ 830 printf ("%2d:%02d.%02d %6d %6d %5s\n", m, s, f, block, len, 831 (e->control & 4) ? "data" : "audio"); 832 } 833 834 int play_track (int tstart, int istart, int tend, int iend) 835 { 836 struct ioc_play_track t; 837 838 t.start_track = tstart; 839 t.start_index = istart; 840 t.end_track = tend; 841 t.end_index = iend; 842 843 return ioctl (fd, CDIOCPLAYTRACKS, &t); 844 } 845 846 int play_blocks (int blk, int len) 847 { 848 struct ioc_play_blocks t; 849 850 t.blk = blk; 851 t.len = len; 852 853 return ioctl (fd, CDIOCPLAYBLOCKS, &t); 854 } 855 856 int setvol (int left, int right) 857 { 858 struct ioc_vol v; 859 860 v.vol[0] = left; 861 v.vol[1] = right; 862 v.vol[2] = 0; 863 v.vol[3] = 0; 864 865 return ioctl (fd, CDIOCSETVOL, &v); 866 } 867 868 int read_toc_entrys (int len) 869 { 870 struct ioc_read_toc_entry t; 871 872 t.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT; 873 t.starting_track = 0; 874 t.data_len = len; 875 t.data = toc_buffer; 876 877 return (ioctl (fd, CDIOREADTOCENTRYS, (char *) &t)); 878 } 879 880 int play_msf (int start_m, int start_s, int start_f, 881 int end_m, int end_s, int end_f) 882 { 883 struct ioc_play_msf a; 884 885 a.start_m = start_m; 886 a.start_s = start_s; 887 a.start_f = start_f; 888 a.end_m = end_m; 889 a.end_s = end_s; 890 a.end_f = end_f; 891 892 return ioctl (fd, CDIOCPLAYMSF, (char *) &a); 893 } 894 895 int status (int *trk, int *min, int *sec, int *frame) 896 { 897 struct ioc_read_subchannel s; 898 struct cd_sub_channel_info data; 899 u_char mm, ss, ff; 900 901 bzero (&s, sizeof (s)); 902 s.data = &data; 903 s.data_len = sizeof (data); 904 s.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT; 905 s.data_format = CD_CURRENT_POSITION; 906 907 if (ioctl (fd, CDIOCREADSUBCHANNEL, (char *) &s) < 0) 908 return -1; 909 910 *trk = s.data->what.position.track_number; 911 if (msf) { 912 *min = s.data->what.position.reladdr.msf.minute; 913 *sec = s.data->what.position.reladdr.msf.second; 914 *frame = s.data->what.position.reladdr.msf.frame; 915 } else { 916 lba2msf(ntohl(s.data->what.position.reladdr.lba), 917 &mm, &ss, &ff); 918 *min = mm; 919 *sec = ss; 920 *frame = ff; 921 } 922 923 return s.data->header.audio_status; 924 } 925 926 const char * 927 cdcontrol_prompt() 928 { 929 return ("cdcontrol> "); 930 } 931 932 char * 933 input (int *cmd) 934 { 935 #define MAXLINE 80 936 static EditLine *el = NULL; 937 static History *hist = NULL; 938 static char buf[MAXLINE]; 939 int num = 0; 940 int len; 941 const char *bp = NULL; 942 char *p; 943 944 do { 945 if (verbose) { 946 if (!el) { 947 el = el_init("cdcontrol", stdin, stdout); 948 hist = history_init(); 949 history(hist, H_EVENT, 100); 950 el_set(el, EL_HIST, history, hist); 951 el_set(el, EL_EDITOR, "emacs"); 952 el_set(el, EL_PROMPT, cdcontrol_prompt); 953 el_set(el, EL_SIGNAL, 1); 954 el_source(el, NULL); 955 } 956 if ((bp = el_gets(el, &num)) == NULL || num == 0) 957 return (0); 958 959 len = (num > MAXLINE) ? MAXLINE : num; 960 memcpy(buf, bp, len); 961 buf[len] = 0; 962 history(hist, H_ENTER, bp); 963 #undef MAXLINE 964 965 } else { 966 if (! fgets (buf, sizeof (buf), stdin)) { 967 *cmd = CMD_QUIT; 968 fprintf (stderr, "\r\n"); 969 return (0); 970 } 971 } 972 p = parse (buf, cmd); 973 } while (! p); 974 return (p); 975 } 976 977 char *parse (char *buf, int *cmd) 978 { 979 struct cmdtab *c; 980 char *p; 981 int len; 982 983 for (p=buf; isspace (*p); p++) 984 continue; 985 986 if (isdigit (*p) || (p[0] == '#' && isdigit (p[1]))) { 987 *cmd = CMD_PLAY; 988 return (p); 989 } 990 991 for (buf = p; *p && ! isspace (*p); p++) 992 continue; 993 994 len = p - buf; 995 if (! len) 996 return (0); 997 998 if (*p) { /* It must be a spacing character! */ 999 char *q; 1000 1001 *p++ = 0; 1002 for (q=p; *q && *q != '\n' && *q != '\r'; q++) 1003 continue; 1004 *q = 0; 1005 } 1006 1007 *cmd = -1; 1008 for (c=cmdtab; c->name; ++c) { 1009 /* Is it an exact match? */ 1010 if (! strcasecmp (buf, c->name)) { 1011 *cmd = c->command; 1012 break; 1013 } 1014 1015 /* Try short hand forms then... */ 1016 if (len >= c->min && ! strncasecmp (buf, c->name, len)) { 1017 if (*cmd != -1 && *cmd != c->command) { 1018 warnx("ambiguous command"); 1019 return (0); 1020 } 1021 *cmd = c->command; 1022 } 1023 } 1024 1025 if (*cmd == -1) { 1026 warnx("invalid command, enter ``help'' for commands"); 1027 return (0); 1028 } 1029 1030 while (isspace (*p)) 1031 p++; 1032 return p; 1033 } 1034 1035 int open_cd () 1036 { 1037 char devbuf[80]; 1038 1039 if (fd > -1) 1040 return (1); 1041 1042 if (*cdname == '/') 1043 strcpy (devbuf, cdname); 1044 else if (*cdname == 'r') 1045 sprintf (devbuf, "/dev/%s", cdname); 1046 else 1047 sprintf (devbuf, "/dev/r%s", cdname); 1048 1049 fd = open (devbuf, O_RDONLY); 1050 1051 if (fd < 0 && errno == ENOENT) { 1052 strcat (devbuf, DEFAULT_CD_PARTITION); 1053 fd = open (devbuf, O_RDONLY); 1054 } 1055 1056 if (fd < 0) { 1057 if (errno == ENXIO) { 1058 /* ENXIO has an overloaded meaning here. 1059 * The original "Device not configured" should 1060 * be interpreted as "No disc in drive %s". */ 1061 warnx("no disc in drive %s", devbuf); 1062 return (0); 1063 } 1064 err(1, "%s", devbuf); 1065 } 1066 return (1); 1067 } 1068