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