17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (c) 1993-2001 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <ctype.h>
317c478bd9Sstevel@tonic-gate #include <math.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <Audio.h>
347c478bd9Sstevel@tonic-gate #include <AudioHdr.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <parse.h>
377c478bd9Sstevel@tonic-gate #include <convert.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static struct keyword_table Keywords[] = {
407c478bd9Sstevel@tonic-gate 	(char *)"encoding",		K_ENCODING,
417c478bd9Sstevel@tonic-gate 	(char *)"rate",			K_RATE,
427c478bd9Sstevel@tonic-gate 	(char *)"channels",		K_CHANNELS,
437c478bd9Sstevel@tonic-gate 	(char *)"offset",		K_OFFSET,
447c478bd9Sstevel@tonic-gate 	(char *)"format",		K_FORMAT,
457c478bd9Sstevel@tonic-gate 	NULL,				K_NULL,
467c478bd9Sstevel@tonic-gate };
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate // Lookup the string in a keyword table. return the token associated with it.
497c478bd9Sstevel@tonic-gate keyword_type
do_lookup(char * s,struct keyword_table * kp)507c478bd9Sstevel@tonic-gate do_lookup(
517c478bd9Sstevel@tonic-gate 	char			*s,
527c478bd9Sstevel@tonic-gate 	struct keyword_table	*kp)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	struct keyword_table	*tkp = NULL;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	for (; kp && kp->name; kp++) {
577c478bd9Sstevel@tonic-gate 		if (strncmp(s, kp->name, strlen(s)) == 0) {
587c478bd9Sstevel@tonic-gate 			// check if exact match
597c478bd9Sstevel@tonic-gate 			if (strlen(s) == strlen(kp->name)) {
607c478bd9Sstevel@tonic-gate 				return (kp->type);
617c478bd9Sstevel@tonic-gate 			} else {
627c478bd9Sstevel@tonic-gate 				// already have another partial match, so
637c478bd9Sstevel@tonic-gate 				// it's ambiguous
647c478bd9Sstevel@tonic-gate 				if (tkp) {
657c478bd9Sstevel@tonic-gate 					return (K_AMBIG);
667c478bd9Sstevel@tonic-gate 				} else {
677c478bd9Sstevel@tonic-gate 					tkp = kp;
687c478bd9Sstevel@tonic-gate 				}
697c478bd9Sstevel@tonic-gate 			}
707c478bd9Sstevel@tonic-gate 		}
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	// at end of list. if there was a partial match, return it, if
747c478bd9Sstevel@tonic-gate 	// not, there's no match....
757c478bd9Sstevel@tonic-gate 	if (tkp) {
767c478bd9Sstevel@tonic-gate 		return (tkp->type);
777c478bd9Sstevel@tonic-gate 	} else {
787c478bd9Sstevel@tonic-gate 		return (K_NULL);
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate // Parse a file format specification
837c478bd9Sstevel@tonic-gate int
fileformat_parse(char * val,format_type & format)847c478bd9Sstevel@tonic-gate fileformat_parse(
857c478bd9Sstevel@tonic-gate 	char		*val,
867c478bd9Sstevel@tonic-gate 	format_type&	format)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	// XXX - other formats later ...
897c478bd9Sstevel@tonic-gate 	if (strcasecmp(val, "sun") == 0) {
907c478bd9Sstevel@tonic-gate 		format = F_SUN;
917c478bd9Sstevel@tonic-gate 	} else if (strcasecmp(val, "raw") == 0) {
927c478bd9Sstevel@tonic-gate 		format = F_RAW;
937c478bd9Sstevel@tonic-gate 	} else if (strcasecmp(val, "aiff") == 0) {
947c478bd9Sstevel@tonic-gate 		Err(MGET("AIFF not yet supported\n"));
957c478bd9Sstevel@tonic-gate 		return (-1);
967c478bd9Sstevel@tonic-gate 	} else {
977c478bd9Sstevel@tonic-gate 		return (-1);
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 	return (0);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate // Parse an audio format keyword
1037c478bd9Sstevel@tonic-gate int
audioformat_parse(char * val,AudioHdr & hdr)1047c478bd9Sstevel@tonic-gate audioformat_parse(
1057c478bd9Sstevel@tonic-gate 	char		*val,
1067c478bd9Sstevel@tonic-gate 	AudioHdr&	hdr)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	// check if it's "cd" or "dat" or "voice".
1097c478bd9Sstevel@tonic-gate 	// these set the precision and encoding, etc.
1107c478bd9Sstevel@tonic-gate 	if (strcasecmp(val, "dat") == 0) {
1117c478bd9Sstevel@tonic-gate 		hdr.sample_rate = 48000;
1127c478bd9Sstevel@tonic-gate 		hdr.channels = 2;
1137c478bd9Sstevel@tonic-gate 		hdr.encoding = LINEAR;
1147c478bd9Sstevel@tonic-gate 		hdr.samples_per_unit = 1;
1157c478bd9Sstevel@tonic-gate 		hdr.bytes_per_unit = 2;
1167c478bd9Sstevel@tonic-gate 	} else if (strcasecmp(val, "cd") == 0) {
1177c478bd9Sstevel@tonic-gate 		hdr.sample_rate = 44100;
1187c478bd9Sstevel@tonic-gate 		hdr.channels = 2;
1197c478bd9Sstevel@tonic-gate 		hdr.encoding = LINEAR;
1207c478bd9Sstevel@tonic-gate 		hdr.samples_per_unit = 1;
1217c478bd9Sstevel@tonic-gate 		hdr.bytes_per_unit = 2;
1227c478bd9Sstevel@tonic-gate 	} else if (strcasecmp(val, "voice") == 0) {
1237c478bd9Sstevel@tonic-gate 		hdr.sample_rate = 8000;
1247c478bd9Sstevel@tonic-gate 		hdr.channels = 1;
1257c478bd9Sstevel@tonic-gate 		hdr.encoding = ULAW;
1267c478bd9Sstevel@tonic-gate 		hdr.samples_per_unit = 1;
1277c478bd9Sstevel@tonic-gate 		hdr.bytes_per_unit = 1;
1287c478bd9Sstevel@tonic-gate 	} else {
1297c478bd9Sstevel@tonic-gate 		return (-1);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 	return (0);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate // Parse a format spec and return an audio header that describes it.
1357c478bd9Sstevel@tonic-gate // Format is in the form of: [keyword=]value[,[keyword=]value ...].
1367c478bd9Sstevel@tonic-gate int
parse_format(char * s,AudioHdr & hdr,format_type & format,off_t & offset)1377c478bd9Sstevel@tonic-gate parse_format(
1387c478bd9Sstevel@tonic-gate 	char		*s,
1397c478bd9Sstevel@tonic-gate 	AudioHdr&	hdr,
1407c478bd9Sstevel@tonic-gate 	format_type&	format,
1417c478bd9Sstevel@tonic-gate 	off_t&		offset)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	char		*cp;
1447c478bd9Sstevel@tonic-gate 	char		*buf;
1457c478bd9Sstevel@tonic-gate 	char		*key;
1467c478bd9Sstevel@tonic-gate 	char		*val;
1477c478bd9Sstevel@tonic-gate 	char		*cp2;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	offset = 0;
1507c478bd9Sstevel@tonic-gate 	format = F_SUN;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	// if no string provided, just return ...
1537c478bd9Sstevel@tonic-gate 	if (!(s && *s))
1547c478bd9Sstevel@tonic-gate 		return (0);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	// First off, try to parse it as a full format string
1577c478bd9Sstevel@tonic-gate 	// (it would have to have been quoted).
1587c478bd9Sstevel@tonic-gate 	// If this works, we're done.
1597c478bd9Sstevel@tonic-gate 	if (hdr.FormatParse(s) == AUDIO_SUCCESS) {
1607c478bd9Sstevel@tonic-gate 		return (0);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	buf = strdup(s);	// save a copy of the string
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	// XXX - bug alert: if someone has info="xxx,yyy", strtok will
1667c478bd9Sstevel@tonic-gate 	// break unless we snarf properly snarf the info. punt for now,
1677c478bd9Sstevel@tonic-gate 	// fix later (since no info supported yet)....
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	for (cp = strtok(buf, ","); cp; cp = strtok(NULL, ",")) {
1707c478bd9Sstevel@tonic-gate 		// Check if there's a '='
1717c478bd9Sstevel@tonic-gate 		// If so, left side is keyword, right side is value.
1727c478bd9Sstevel@tonic-gate 		// If not, entire string is value.
1737c478bd9Sstevel@tonic-gate 		if (cp2 = strchr(cp, '=')) {
174*2bee374fSToomas Soome 			*cp2++ = '\0';
1757c478bd9Sstevel@tonic-gate 			key = cp;
1767c478bd9Sstevel@tonic-gate 			val = cp2;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 			// Look for the keyword
1797c478bd9Sstevel@tonic-gate 			switch (do_lookup(key, Keywords)) {
1807c478bd9Sstevel@tonic-gate 			case K_ENCODING:
1817c478bd9Sstevel@tonic-gate 				if (hdr.EncodingParse(val)) {
1827c478bd9Sstevel@tonic-gate 					Err(MGET(
1837c478bd9Sstevel@tonic-gate 					    "invalid encoding option: %s\n"),
1847c478bd9Sstevel@tonic-gate 					    val);
1857c478bd9Sstevel@tonic-gate 					goto parse_error;
1867c478bd9Sstevel@tonic-gate 				}
1877c478bd9Sstevel@tonic-gate 				break;
1887c478bd9Sstevel@tonic-gate 			case K_RATE:
1897c478bd9Sstevel@tonic-gate 				if (hdr.RateParse(val)) {
1907c478bd9Sstevel@tonic-gate 					Err(MGET("invalid sample rate: %s\n"),
1917c478bd9Sstevel@tonic-gate 					    val);
1927c478bd9Sstevel@tonic-gate 					goto parse_error;
1937c478bd9Sstevel@tonic-gate 				}
1947c478bd9Sstevel@tonic-gate 				break;
1957c478bd9Sstevel@tonic-gate 			case K_CHANNELS:
1967c478bd9Sstevel@tonic-gate 				if (hdr.ChannelParse(val)) {
1977c478bd9Sstevel@tonic-gate 					Err(MGET(
1987c478bd9Sstevel@tonic-gate 					    "invalid channels option: %s\n"),
1997c478bd9Sstevel@tonic-gate 					    val);
2007c478bd9Sstevel@tonic-gate 					goto parse_error;
2017c478bd9Sstevel@tonic-gate 				}
2027c478bd9Sstevel@tonic-gate 				break;
2037c478bd9Sstevel@tonic-gate 			case K_FORMAT:
2047c478bd9Sstevel@tonic-gate 				if (fileformat_parse(val, format) < 0) {
2057c478bd9Sstevel@tonic-gate 					Err(MGET("unknown format: %s\n"), val);
2067c478bd9Sstevel@tonic-gate 					goto parse_error;
2077c478bd9Sstevel@tonic-gate 				}
2087c478bd9Sstevel@tonic-gate 				break;
2097c478bd9Sstevel@tonic-gate 			case K_OFFSET:
2107c478bd9Sstevel@tonic-gate 				offset = (off_t)atoi(val);
2117c478bd9Sstevel@tonic-gate 				break;
2127c478bd9Sstevel@tonic-gate 			case K_AMBIG:
2137c478bd9Sstevel@tonic-gate 				Err(MGET("ambiguous keyword: %s\n"), key);
2147c478bd9Sstevel@tonic-gate 				goto parse_error;
2157c478bd9Sstevel@tonic-gate 			case K_NULL:
2167c478bd9Sstevel@tonic-gate 				Err(MGET("null keyword: =%s\n"), val);
2177c478bd9Sstevel@tonic-gate 				goto parse_error;
2187c478bd9Sstevel@tonic-gate 			default:
2197c478bd9Sstevel@tonic-gate 				Err(MGET("invalid keyword: %s\n"), key);
2207c478bd9Sstevel@tonic-gate 				goto parse_error;
2217c478bd9Sstevel@tonic-gate 			}
2227c478bd9Sstevel@tonic-gate 		} else {
2237c478bd9Sstevel@tonic-gate 			// No keyword, so try to intuit the value
2247c478bd9Sstevel@tonic-gate 			// First try encoding, audio, and file format.
2257c478bd9Sstevel@tonic-gate 			// If they fail, try sample rate and channels.
2267c478bd9Sstevel@tonic-gate 			val = cp;
2277c478bd9Sstevel@tonic-gate 			if (hdr.EncodingParse(val) &&
2287c478bd9Sstevel@tonic-gate 			    (audioformat_parse(val, hdr) < 0) &&
2297c478bd9Sstevel@tonic-gate 			    (fileformat_parse(val, format) < 0)) {
2307c478bd9Sstevel@tonic-gate 				// If this looks like sample rate, make sure
2317c478bd9Sstevel@tonic-gate 				// it is not ambiguous with channels
2327c478bd9Sstevel@tonic-gate 				if (!hdr.RateParse(val)) {
2337c478bd9Sstevel@tonic-gate 					if (hdr.sample_rate < 1000) {
2347c478bd9Sstevel@tonic-gate 						int	x;
2357c478bd9Sstevel@tonic-gate 						char	y[10];
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 						if (sscanf(val, " %lf %9s",
2387c478bd9Sstevel@tonic-gate 						    &x, y) != 1) {
2397c478bd9Sstevel@tonic-gate 							Err(
2407c478bd9Sstevel@tonic-gate 					MGET("ambiguous numeric option: %s\n"),
2417c478bd9Sstevel@tonic-gate 							    val);
2427c478bd9Sstevel@tonic-gate 							goto parse_error;
2437c478bd9Sstevel@tonic-gate 						}
2447c478bd9Sstevel@tonic-gate 					}
2457c478bd9Sstevel@tonic-gate 				} else if (hdr.ChannelParse(val)) {
2467c478bd9Sstevel@tonic-gate 					Err(MGET("invalid option value: %s\n"),
2477c478bd9Sstevel@tonic-gate 					    val);
2487c478bd9Sstevel@tonic-gate 					goto parse_error;
2497c478bd9Sstevel@tonic-gate 				}
2507c478bd9Sstevel@tonic-gate 			}
2517c478bd9Sstevel@tonic-gate 		}
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 	free(buf);
2547c478bd9Sstevel@tonic-gate 	return (0);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate parse_error:
2577c478bd9Sstevel@tonic-gate 	free(buf);
2587c478bd9Sstevel@tonic-gate 	return (-1);
2597c478bd9Sstevel@tonic-gate }
260