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
6 * Fixed and further modified on 5-Sep-1995 by Jukka Ukkonen <jau@funet.fi>.
8 * 11-Sep-1995: Jukka A. Ukkonen <jau@funet.fi>
9 * A couple of further fixes to my own earlier "fixes".
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.
16 * 11-Oct-1995: Serge V.Vakulenko <vak@cronyx.ru>
17 * New eject algorithm.
18 * Some code style reformatting.
20 * $FreeBSD: src/usr.sbin/cdcontrol/cdcontrol.c,v 1.24.2.11 2002/11/20 00:26:19 njl Exp $
21 * $DragonFly: src/usr.sbin/cdcontrol/cdcontrol.c,v 1.11 2006/09/26 17:32:09 dillon Exp $
25 #include <sys/cdrio.h>
27 #include <sys/ioctl.h>
28 #include <sys/param.h>
43 #define ASTS_INVALID 0x00 /* Audio status byte not valid */
44 #define ASTS_PLAYING 0x11 /* Audio play operation in progress */
45 #define ASTS_PAUSED 0x12 /* Audio play operation paused */
46 #define ASTS_COMPLETED 0x13 /* Audio play operation successfully completed */
47 #define ASTS_ERROR 0x14 /* Audio play operation stopped due to error */
48 #define ASTS_VOID 0x15 /* No current audio status to return */
50 #ifndef DEFAULT_CD_DRIVE
51 # define DEFAULT_CD_DRIVE "/dev/cd0c"
54 #ifndef DEFAULT_CD_PARTITION
55 # define DEFAULT_CD_PARTITION "c"
74 #define CMD_PREVIOUS 17
76 #define STATUS_AUDIO 0x1
77 #define STATUS_MEDIA 0x2
78 #define STATUS_VOLUME 0x4
86 { CMD_CLOSE
, "close", 1, "" },
87 { CMD_DEBUG
, "debug", 1, "on | off" },
88 { CMD_EJECT
, "eject", 1, "" },
89 { CMD_HELP
, "?", 1, 0 },
90 { CMD_HELP
, "help", 1, "" },
91 { CMD_INFO
, "info", 1, "" },
92 { CMD_NEXT
, "next", 1, "" },
93 { CMD_PAUSE
, "pause", 2, "" },
94 { CMD_PLAY
, "play", 1, "min1:sec1[.fram1] [min2:sec2[.fram2]]" },
95 { CMD_PLAY
, "play", 1, "track1[.index1] [track2[.index2]]" },
96 { CMD_PLAY
, "play", 1, "tr1 m1:s1[.f1] [[tr2] [m2:s2[.f2]]]" },
97 { CMD_PLAY
, "play", 1, "[#block [len]]" },
98 { CMD_PREVIOUS
, "previous", 2, "" },
99 { CMD_QUIT
, "quit", 1, "" },
100 { CMD_QUIT
, "exit", 1, "" },
101 { CMD_RESET
, "reset", 4, "" },
102 { CMD_RESUME
, "resume", 1, "" },
103 { CMD_SET
, "set", 2, "msf | lba" },
104 { CMD_STATUS
, "status", 1, "[audio | media | volume]" },
105 { CMD_STOP
, "stop", 3, "" },
106 { CMD_VOLUME
, "volume", 1,
107 "<l> <r> | left | right | mute | mono | stereo" },
108 { CMD_CDID
, "cdid", 2, "" },
109 { CMD_SPEED
, "speed", 2, "speed" },
113 struct cd_toc_entry toc_buffer
[100];
120 int setvol(int, int);
121 int read_toc_entrys(int);
122 int play_msf(int, int, int, int, int, int);
123 int play_track(int, int, int, int);
124 int get_vol(int *, int *);
125 int status(int *, int *, int *, int *);
127 int next_prev(char *arg
, int);
131 int pstatus(char *arg
);
133 void prtrack(struct cd_toc_entry
*e
, int lastflag
);
134 void lba2msf(unsigned long lba
,
135 u_char
*m
, u_char
*s
, u_char
*f
);
136 unsigned int msf2lba(u_char m
, u_char s
, u_char f
);
137 int play_blocks(int blk
, int len
);
138 int run(int cmd
, char *arg
);
139 char *parse(char *buf
, int *cmd
);
142 char *use_cdrom_instead(const char *);
143 const char *strstatus(int);
144 static u_int
dbprog_discid(void);
145 const char *cdcontrol_prompt(void);
155 for (c
=cmdtab
; c
->name
; ++c
) {
159 for (i
= c
->min
, s
= c
->name
; *s
; s
++, i
--) {
167 printf (" %s", c
->args
);
170 printf ("\n\tThe word \"play\" is not required for the play commands.\n");
171 printf ("\tThe plain target address is taken as a synonym for play.\n");
177 fprintf (stderr
, "usage: cdcontrol [-sv] [-f device] [command ...]\n");
182 use_cdrom_instead(const char *old_envvar
)
186 device
= getenv(old_envvar
);
188 warnx("%s environment variable deprecated, "
189 "please use CDROM in the future.", old_envvar
);
195 main(int argc
, char **argv
)
201 switch (getopt (argc
, argv
, "svhf:")) {
222 if (argc
> 0 && ! strcasecmp (*argv
, "help"))
226 cdname
= getenv("CDROM");
230 cdname
= use_cdrom_instead("MUSIC_CD");
232 cdname
= use_cdrom_instead("CD_DRIVE");
234 cdname
= use_cdrom_instead("DISC");
236 cdname
= use_cdrom_instead("CDPLAY");
239 cdname
= DEFAULT_CD_DRIVE
;
240 warnx("no CD device name specified, defaulting to %s", cdname
);
247 for (p
=buf
; argc
-->0; ++argv
) {
248 len
= strlen (*argv
);
250 if (p
+ len
>= buf
+ sizeof (buf
) - 1)
260 arg
= parse (buf
, &cmd
);
261 return (run (cmd
, arg
));
265 verbose
= isatty (0);
268 printf ("Compact Disc Control utility, version %s\n", VERSION
);
269 printf ("Type `?' for command list\n\n");
274 if (run (cmd
, arg
) < 0) {
285 run(int cmd
, char *arg
)
296 if (fd
< 0 && ! open_cd ())
302 if (fd
< 0 && ! open_cd ())
308 if (fd
< 0 && ! open_cd ())
311 return pstatus (arg
);
315 if (fd
< 0 && ! open_cd ())
318 while (isspace (*arg
))
321 return next_prev (arg
, cmd
);
324 if (fd
< 0 && ! open_cd ())
327 return ioctl (fd
, CDIOCPAUSE
);
330 if (fd
< 0 && ! open_cd ())
333 return ioctl (fd
, CDIOCRESUME
);
336 if (fd
< 0 && ! open_cd ())
339 rc
= ioctl (fd
, CDIOCSTOP
);
341 ioctl (fd
, CDIOCALLOW
);
346 if (fd
< 0 && ! open_cd ())
349 rc
= ioctl (fd
, CDIOCRESET
);
357 if (fd
< 0 && ! open_cd ())
360 if (! strcasecmp (arg
, "on"))
361 return ioctl (fd
, CDIOCSETDEBUG
);
363 if (! strcasecmp (arg
, "off"))
364 return ioctl (fd
, CDIOCCLRDEBUG
);
366 warnx("invalid command arguments");
371 if (fd
< 0 && ! open_cd ())
374 ioctl (fd
, CDIOCALLOW
);
375 rc
= ioctl (fd
, CDIOCEJECT
);
381 if (fd
< 0 && ! open_cd ())
384 ioctl (fd
, CDIOCALLOW
);
385 rc
= ioctl (fd
, CDIOCCLOSE
);
393 if (fd
< 0 && ! open_cd ())
396 while (isspace (*arg
))
402 if (! strcasecmp (arg
, "msf"))
404 else if (! strcasecmp (arg
, "lba"))
407 warnx("invalid command arguments");
411 if (fd
< 0 && !open_cd ())
414 if (! strncasecmp (arg
, "left", strlen(arg
)))
415 return ioctl (fd
, CDIOCSETLEFT
);
417 if (! strncasecmp (arg
, "right", strlen(arg
)))
418 return ioctl (fd
, CDIOCSETRIGHT
);
420 if (! strncasecmp (arg
, "mono", strlen(arg
)))
421 return ioctl (fd
, CDIOCSETMONO
);
423 if (! strncasecmp (arg
, "stereo", strlen(arg
)))
424 return ioctl (fd
, CDIOCSETSTERIO
);
426 if (! strncasecmp (arg
, "mute", strlen(arg
)))
427 return ioctl (fd
, CDIOCSETMUTE
);
429 if (2 != sscanf (arg
, "%d %d", &l
, &r
)) {
430 warnx("invalid command arguments");
434 return setvol (l
, r
);
437 if (fd
< 0 && ! open_cd ())
441 if (strcasecmp("max", arg
) == 0)
442 speed
= CDR_MAX_SPEED
;
444 speed
= strtol(arg
, NULL
, 10) * 177;
445 if (speed
<= 0 || speed
> INT_MAX
) {
446 warnx("invalid command arguments %s", arg
);
449 return ioctl(fd
, CDRIOCREADSPEED
, &speed
);
462 struct ioc_toc_header h
;
464 int rc
, start
, end
= 0, istart
= 1, iend
= 1;
466 rc
= ioctl (fd
, CDIOREADTOCHEADER
, &h
);
471 n
= h
.ending_track
- h
.starting_track
+ 1;
472 rc
= read_toc_entrys ((n
+ 1) * sizeof (struct cd_toc_entry
));
477 if (! arg
|| ! *arg
) {
478 /* Play the whole disc */
480 return play_blocks (0, msf2lba (toc_buffer
[n
].addr
.msf
.minute
,
481 toc_buffer
[n
].addr
.msf
.second
,
482 toc_buffer
[n
].addr
.msf
.frame
));
484 return play_blocks (0, ntohl(toc_buffer
[n
].addr
.lba
));
487 if (strchr (arg
, '#')) {
488 /* Play block #blk [ len ] */
491 if (2 != sscanf (arg
, "#%d%d", &blk
, &len
) &&
492 1 != sscanf (arg
, "#%d", &blk
))
497 len
= msf2lba (toc_buffer
[n
].addr
.msf
.minute
,
498 toc_buffer
[n
].addr
.msf
.second
,
499 toc_buffer
[n
].addr
.msf
.frame
) - blk
;
501 len
= ntohl(toc_buffer
[n
].addr
.lba
) - blk
;
503 return play_blocks (blk
, len
);
506 if (strchr (arg
, ':')) {
508 * Play MSF m1:s1 [ .f1 ] [ m2:s2 [ .f2 ] ]
510 * Will now also undestand timed addresses relative
511 * to the beginning of a track in the form...
513 * tr1 m1:s1[.f1] [[tr2] [m2:s2[.f2]]]
516 unsigned m1
, m2
, s1
, s2
, f1
, f2
;
517 unsigned char tm
, ts
, tf
;
519 tr2
= m2
= s2
= f2
= f1
= 0;
520 if (8 == sscanf (arg
, "%d %d:%d.%d %d %d:%d.%d",
521 &tr1
, &m1
, &s1
, &f1
, &tr2
, &m2
, &s2
, &f2
))
522 goto Play_Relative_Addresses
;
524 tr2
= m2
= s2
= f2
= f1
= 0;
525 if (7 == sscanf (arg
, "%d %d:%d %d %d:%d.%d",
526 &tr1
, &m1
, &s1
, &tr2
, &m2
, &s2
, &f2
))
527 goto Play_Relative_Addresses
;
529 tr2
= m2
= s2
= f2
= f1
= 0;
530 if (7 == sscanf (arg
, "%d %d:%d.%d %d %d:%d",
531 &tr1
, &m1
, &s1
, &f1
, &tr2
, &m2
, &s2
))
532 goto Play_Relative_Addresses
;
534 tr2
= m2
= s2
= f2
= f1
= 0;
535 if (7 == sscanf (arg
, "%d %d:%d.%d %d:%d.%d",
536 &tr1
, &m1
, &s1
, &f1
, &m2
, &s2
, &f2
))
537 goto Play_Relative_Addresses
;
539 tr2
= m2
= s2
= f2
= f1
= 0;
540 if (6 == sscanf (arg
, "%d %d:%d.%d %d:%d",
541 &tr1
, &m1
, &s1
, &f1
, &m2
, &s2
))
542 goto Play_Relative_Addresses
;
544 tr2
= m2
= s2
= f2
= f1
= 0;
545 if (6 == sscanf (arg
, "%d %d:%d %d:%d.%d",
546 &tr1
, &m1
, &s1
, &m2
, &s2
, &f2
))
547 goto Play_Relative_Addresses
;
549 tr2
= m2
= s2
= f2
= f1
= 0;
550 if (6 == sscanf (arg
, "%d %d:%d.%d %d %d",
551 &tr1
, &m1
, &s1
, &f1
, &tr2
, &m2
))
552 goto Play_Relative_Addresses
;
554 tr2
= m2
= s2
= f2
= f1
= 0;
555 if (5 == sscanf (arg
, "%d %d:%d %d:%d", &tr1
, &m1
, &s1
, &m2
, &s2
))
556 goto Play_Relative_Addresses
;
558 tr2
= m2
= s2
= f2
= f1
= 0;
559 if (5 == sscanf (arg
, "%d %d:%d %d %d",
560 &tr1
, &m1
, &s1
, &tr2
, &m2
))
561 goto Play_Relative_Addresses
;
563 tr2
= m2
= s2
= f2
= f1
= 0;
564 if (5 == sscanf (arg
, "%d %d:%d.%d %d",
565 &tr1
, &m1
, &s1
, &f1
, &tr2
))
566 goto Play_Relative_Addresses
;
568 tr2
= m2
= s2
= f2
= f1
= 0;
569 if (4 == sscanf (arg
, "%d %d:%d %d", &tr1
, &m1
, &s1
, &tr2
))
570 goto Play_Relative_Addresses
;
572 tr2
= m2
= s2
= f2
= f1
= 0;
573 if (4 == sscanf (arg
, "%d %d:%d.%d", &tr1
, &m1
, &s1
, &f1
))
574 goto Play_Relative_Addresses
;
576 tr2
= m2
= s2
= f2
= f1
= 0;
577 if (3 == sscanf (arg
, "%d %d:%d", &tr1
, &m1
, &s1
))
578 goto Play_Relative_Addresses
;
580 tr2
= m2
= s2
= f2
= f1
= 0;
581 goto Try_Absolute_Timed_Addresses
;
583 Play_Relative_Addresses
:
592 tm
= toc_buffer
[tr1
].addr
.msf
.minute
;
593 ts
= toc_buffer
[tr1
].addr
.msf
.second
;
594 tf
= toc_buffer
[tr1
].addr
.msf
.frame
;
596 lba2msf(ntohl(toc_buffer
[tr1
].addr
.lba
),
603 printf ("Track %d is not that long.\n", tr1
+ 1);
622 if (m2
|| s2
|| f2
) {
640 m2
= toc_buffer
[n
].addr
.msf
.minute
;
641 s2
= toc_buffer
[n
].addr
.msf
.second
;
642 f2
= toc_buffer
[n
].addr
.msf
.frame
;
644 lba2msf(ntohl(toc_buffer
[n
].addr
.lba
),
651 } else if (tr2
> n
) {
658 tm
= toc_buffer
[tr2
].addr
.msf
.minute
;
659 ts
= toc_buffer
[tr2
].addr
.msf
.second
;
660 tf
= toc_buffer
[tr2
].addr
.msf
.frame
;
662 lba2msf(ntohl(toc_buffer
[tr2
].addr
.lba
),
680 tm
= toc_buffer
[n
].addr
.msf
.minute
;
681 ts
= toc_buffer
[n
].addr
.msf
.second
;
682 tf
= toc_buffer
[n
].addr
.msf
.frame
;
684 lba2msf(ntohl(toc_buffer
[n
].addr
.lba
),
692 printf ("The playing time of the disc is not that long.\n");
695 return (play_msf (m1
, s1
, f1
, m2
, s2
, f2
));
697 Try_Absolute_Timed_Addresses
:
698 if (6 != sscanf (arg
, "%d:%d.%d%d:%d.%d",
699 &m1
, &s1
, &f1
, &m2
, &s2
, &f2
) &&
700 5 != sscanf (arg
, "%d:%d.%d%d:%d", &m1
, &s1
, &f1
, &m2
, &s2
) &&
701 5 != sscanf (arg
, "%d:%d%d:%d.%d", &m1
, &s1
, &m2
, &s2
, &f2
) &&
702 3 != sscanf (arg
, "%d:%d.%d", &m1
, &s1
, &f1
) &&
703 4 != sscanf (arg
, "%d:%d%d:%d", &m1
, &s1
, &m2
, &s2
) &&
704 2 != sscanf (arg
, "%d:%d", &m1
, &s1
))
709 m2
= toc_buffer
[n
].addr
.msf
.minute
;
710 s2
= toc_buffer
[n
].addr
.msf
.second
;
711 f2
= toc_buffer
[n
].addr
.msf
.frame
;
713 lba2msf(ntohl(toc_buffer
[n
].addr
.lba
),
720 return play_msf (m1
, s1
, f1
, m2
, s2
, f2
);
724 * Play track trk1 [ .idx1 ] [ trk2 [ .idx2 ] ]
726 if (4 != sscanf (arg
, "%d.%d%d.%d", &start
, &istart
, &end
, &iend
) &&
727 3 != sscanf (arg
, "%d.%d%d", &start
, &istart
, &end
) &&
728 3 != sscanf (arg
, "%d%d.%d", &start
, &end
, &iend
) &&
729 2 != sscanf (arg
, "%d.%d", &start
, &istart
) &&
730 2 != sscanf (arg
, "%d%d", &start
, &end
) &&
731 1 != sscanf (arg
, "%d", &start
))
736 return (play_track (start
, istart
, end
, iend
));
739 warnx("invalid command arguments");
744 next_prev(char *arg
, int cmd
)
746 struct ioc_toc_header h
;
747 int dir
, junk
, n
, off
, rc
, trk
;
749 dir
= (cmd
== CMD_NEXT
) ? 1 : -1;
750 rc
= ioctl (fd
, CDIOREADTOCHEADER
, &h
);
754 n
= h
.ending_track
- h
.starting_track
+ 1;
755 rc
= status (&trk
, &junk
, &junk
, &junk
);
760 if (sscanf (arg
, "%u", &off
) != 1) {
761 warnx("invalid command argument");
768 if (trk
> h
.ending_track
)
771 return (play_track (trk
, 1, n
, 1));
778 case ASTS_INVALID
: return ("invalid");
779 case ASTS_PLAYING
: return ("playing");
780 case ASTS_PAUSED
: return ("paused");
781 case ASTS_COMPLETED
: return ("completed");
782 case ASTS_ERROR
: return ("error");
783 case ASTS_VOID
: return ("void");
784 default: return ("??");
792 struct ioc_read_subchannel ss
;
793 struct cd_sub_channel_info data
;
794 int rc
, trk
, m
, s
, f
;
796 char *p
, vmcn
[(4 * 15) + 1];
798 while ((p
= strtok(arg
, " \t"))) {
800 if (!strncasecmp(p
, "audio", strlen(p
)))
801 what
|= STATUS_AUDIO
;
802 else if (!strncasecmp(p
, "media", strlen(p
)))
803 what
|= STATUS_MEDIA
;
804 else if (!strncasecmp(p
, "volume", strlen(p
)))
805 what
|= STATUS_VOLUME
;
807 warnx("invalid command arguments");
812 what
= STATUS_AUDIO
|STATUS_MEDIA
|STATUS_VOLUME
;
813 if (what
& STATUS_AUDIO
) {
814 rc
= status (&trk
, &m
, &s
, &f
);
817 printf ("Audio status = %d<%s>, current track = %d, current position = %d:%02d.%02d\n",
818 rc
, strstatus (rc
), trk
, m
, s
, f
);
820 printf ("%d %d %d:%02d.%02d\n", rc
, trk
, m
, s
, f
);
822 printf ("No current status info available\n");
824 if (what
& STATUS_MEDIA
) {
825 bzero (&ss
, sizeof (ss
));
827 ss
.data_len
= sizeof (data
);
828 ss
.address_format
= msf
? CD_MSF_FORMAT
: CD_LBA_FORMAT
;
829 ss
.data_format
= CD_MEDIA_CATALOG
;
830 rc
= ioctl (fd
, CDIOCREADSUBCHANNEL
, (char *) &ss
);
832 printf("Media catalog is %sactive",
833 ss
.data
->what
.media_catalog
.mc_valid
? "": "in");
834 if (ss
.data
->what
.media_catalog
.mc_valid
&&
835 ss
.data
->what
.media_catalog
.mc_number
[0])
837 strvisx (vmcn
, ss
.data
->what
.media_catalog
.mc_number
,
838 (sizeof (vmcn
) - 1) / 4, VIS_OCTAL
| VIS_NL
);
839 printf(", number \"%.*s\"", (int)sizeof (vmcn
), vmcn
);
843 printf("No media catalog info available\n");
845 if (what
& STATUS_VOLUME
) {
846 rc
= ioctl (fd
, CDIOCGETVOL
, &v
);
849 printf ("Left volume = %d, right volume = %d\n",
852 printf ("%d %d\n", v
.vol
[0], v
.vol
[1]);
854 printf ("No volume level info available\n");
861 * Convert an integer to its text string representation, and
862 * compute its checksum. Used by dbprog_discid to derive the
866 * n - The integer value.
869 * The integer checksum.
878 /* For backward compatibility this algorithm must not change */
879 sprintf(buf
, "%u", n
);
880 for (p
= buf
; *p
!= '\0'; p
++)
889 * Compute a magic disc ID based on the number of tracks,
890 * the length of each track, and a checksum of the string
891 * that represents the offset of each track.
894 * s - Pointer to the curstat_t structure.
897 * The integer disc ID.
902 struct ioc_toc_header h
;
908 rc
= ioctl (fd
, CDIOREADTOCHEADER
, &h
);
911 ntr
= h
.ending_track
- h
.starting_track
+ 1;
914 rc
= read_toc_entrys ((ntr
+ 1) * sizeof (struct cd_toc_entry
));
918 /* For backward compatibility this algorithm must not change */
919 for (i
= 0; i
< ntr
; i
++) {
920 #define TC_MM(a) toc_buffer[a].addr.msf.minute
921 #define TC_SS(a) toc_buffer[a].addr.msf.second
922 n
+= dbprog_sum((TC_MM(i
) * 60) + TC_SS(i
));
924 t
+= ((TC_MM(i
+1) * 60) + TC_SS(i
+1)) -
925 ((TC_MM(i
) * 60) + TC_SS(i
));
928 return((n
% 0xff) << 24 | t
<< 8 | ntr
);
936 id
= dbprog_discid();
941 printf ("%08x\n",id
);
947 info(char *arg __unused
)
949 struct ioc_toc_header h
;
952 rc
= ioctl (fd
, CDIOREADTOCHEADER
, &h
);
955 printf ("Starting track = %d, ending track = %d, TOC size = %d bytes\n",
956 h
.starting_track
, h
.ending_track
, h
.len
);
958 printf ("%d %d %d\n", h
.starting_track
,
959 h
.ending_track
, h
.len
);
961 warn("getting toc header");
965 n
= h
.ending_track
- h
.starting_track
+ 1;
966 rc
= read_toc_entrys ((n
+ 1) * sizeof (struct cd_toc_entry
));
971 printf ("track start duration block length type\n");
972 printf ("-------------------------------------------------\n");
975 for (i
= 0; i
< n
; i
++) {
976 printf ("%5d ", toc_buffer
[i
].track
);
977 prtrack (toc_buffer
+ i
, 0);
979 printf ("%5d ", toc_buffer
[n
].track
);
980 prtrack (toc_buffer
+ n
, 1);
985 lba2msf(unsigned long lba
, u_char
*m
, u_char
*s
, u_char
*f
)
987 lba
+= 150; /* block start offset */
988 lba
&= 0xffffff; /* negative lbas use only 24 bits */
989 *m
= lba
/ (60 * 75);
996 msf2lba(u_char m
, u_char s
, u_char f
)
998 return (((m
* 60) + s
) * 75 + f
) - 150;
1002 prtrack(struct cd_toc_entry
*e
, int lastflag
)
1004 int block
, next
, len
;
1008 /* Print track start */
1009 printf ("%2d:%02d.%02d ", e
->addr
.msf
.minute
,
1010 e
->addr
.msf
.second
, e
->addr
.msf
.frame
);
1012 block
= msf2lba (e
->addr
.msf
.minute
, e
->addr
.msf
.second
,
1015 block
= ntohl(e
->addr
.lba
);
1016 lba2msf(block
, &m
, &s
, &f
);
1017 /* Print track start */
1018 printf ("%2d:%02d.%02d ", m
, s
, f
);
1021 /* Last track -- print block */
1022 printf (" - %6d - -\n", block
);
1027 next
= msf2lba (e
[1].addr
.msf
.minute
, e
[1].addr
.msf
.second
,
1028 e
[1].addr
.msf
.frame
);
1030 next
= ntohl(e
[1].addr
.lba
);
1032 /* Take into account a start offset time. */
1033 lba2msf (len
- 150, &m
, &s
, &f
);
1035 /* Print duration, block, length, type */
1036 printf ("%2d:%02d.%02d %6d %6d %5s\n", m
, s
, f
, block
, len
,
1037 (e
->control
& 4) ? "data" : "audio");
1041 play_track(int tstart
, int istart
, int tend
, int iend
)
1043 struct ioc_play_track t
;
1045 t
.start_track
= tstart
;
1046 t
.start_index
= istart
;
1050 return ioctl (fd
, CDIOCPLAYTRACKS
, &t
);
1054 play_blocks(int blk
, int len
)
1056 struct ioc_play_blocks t
;
1061 return ioctl (fd
, CDIOCPLAYBLOCKS
, &t
);
1065 setvol(int left
, int right
)
1074 return ioctl (fd
, CDIOCSETVOL
, &v
);
1078 read_toc_entrys(int len
)
1080 struct ioc_read_toc_entry t
;
1082 t
.address_format
= msf
? CD_MSF_FORMAT
: CD_LBA_FORMAT
;
1083 t
.starting_track
= 0;
1085 t
.data
= toc_buffer
;
1087 return (ioctl (fd
, CDIOREADTOCENTRYS
, (char *) &t
));
1091 play_msf(int start_m
, int start_s
, int start_f
,
1092 int end_m
, int end_s
, int end_f
)
1094 struct ioc_play_msf a
;
1096 a
.start_m
= start_m
;
1097 a
.start_s
= start_s
;
1098 a
.start_f
= start_f
;
1103 return ioctl (fd
, CDIOCPLAYMSF
, (char *) &a
);
1107 status(int *trk
, int *min
, int *sec
, int *frame
)
1109 struct ioc_read_subchannel s
;
1110 struct cd_sub_channel_info data
;
1113 bzero (&s
, sizeof (s
));
1115 s
.data_len
= sizeof (data
);
1116 s
.address_format
= msf
? CD_MSF_FORMAT
: CD_LBA_FORMAT
;
1117 s
.data_format
= CD_CURRENT_POSITION
;
1119 if (ioctl (fd
, CDIOCREADSUBCHANNEL
, (char *) &s
) < 0)
1122 *trk
= s
.data
->what
.position
.track_number
;
1124 *min
= s
.data
->what
.position
.reladdr
.msf
.minute
;
1125 *sec
= s
.data
->what
.position
.reladdr
.msf
.second
;
1126 *frame
= s
.data
->what
.position
.reladdr
.msf
.frame
;
1128 lba2msf(ntohl(s
.data
->what
.position
.reladdr
.lba
),
1135 return s
.data
->header
.audio_status
;
1139 cdcontrol_prompt(void)
1141 return ("cdcontrol> ");
1148 static EditLine
*el
= NULL
;
1149 static History
*hist
= NULL
;
1150 static HistEvent he
;
1151 static char buf
[MAXLINE
];
1154 const char *bp
= NULL
;
1160 el
= el_init("cdcontrol", stdin
, stdout
, stderr
);
1161 hist
= history_init();
1162 history(hist
, &he
, H_SETSIZE
, 100);
1163 el_set(el
, EL_HIST
, history
, hist
);
1164 el_set(el
, EL_EDITOR
, "emacs");
1165 el_set(el
, EL_PROMPT
, cdcontrol_prompt
);
1166 el_set(el
, EL_SIGNAL
, 1);
1167 el_source(el
, NULL
);
1169 if ((bp
= el_gets(el
, &num
)) == NULL
|| num
== 0) {
1171 fprintf (stderr
, "\r\n");
1175 len
= (num
> MAXLINE
) ? MAXLINE
: num
;
1176 memcpy(buf
, bp
, len
);
1178 history(hist
, &he
, H_ENTER
, bp
);
1182 if (! fgets (buf
, sizeof (buf
), stdin
)) {
1184 fprintf (stderr
, "\r\n");
1188 p
= parse (buf
, cmd
);
1194 parse(char *buf
, int *cmd
)
1200 for (p
=buf
; isspace (*p
); p
++)
1203 if (isdigit (*p
) || (p
[0] == '#' && isdigit (p
[1]))) {
1206 } else if (*p
== '+') {
1209 } else if (*p
== '-') {
1210 *cmd
= CMD_PREVIOUS
;
1214 for (buf
= p
; *p
&& ! isspace (*p
); p
++)
1221 if (*p
) { /* It must be a spacing character! */
1225 for (q
=p
; *q
&& *q
!= '\n' && *q
!= '\r'; q
++)
1231 for (c
=cmdtab
; c
->name
; ++c
) {
1232 /* Is it an exact match? */
1233 if (! strcasecmp (buf
, c
->name
)) {
1238 /* Try short hand forms then... */
1239 if (len
>= c
->min
&& ! strncasecmp (buf
, c
->name
, len
)) {
1240 if (*cmd
!= -1 && *cmd
!= c
->command
) {
1241 warnx("ambiguous command");
1249 warnx("invalid command, enter ``help'' for commands");
1253 while (isspace (*p
))
1261 char devbuf
[MAXPATHLEN
];
1266 if (*cdname
== '/') {
1267 snprintf (devbuf
, MAXPATHLEN
, "%s", cdname
);
1269 snprintf (devbuf
, MAXPATHLEN
, "%s%s", _PATH_DEV
, cdname
);
1272 fd
= open (devbuf
, O_RDONLY
);
1274 if (fd
< 0 && errno
== ENOENT
) {
1275 strcat (devbuf
, DEFAULT_CD_PARTITION
);
1276 fd
= open (devbuf
, O_RDONLY
);
1280 if (errno
== ENXIO
) {
1281 /* ENXIO has an overloaded meaning here.
1282 * The original "Device not configured" should
1283 * be interpreted as "No disc in drive %s". */
1284 warnx("no disc in drive %s", devbuf
);
1287 err(1, "%s", devbuf
);