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