xref: /onnv-gate/usr/src/cmd/audio/audioconvert/parse.cc (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1993-2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdlib.h>
30*0Sstevel@tonic-gate #include <stdio.h>
31*0Sstevel@tonic-gate #include <string.h>
32*0Sstevel@tonic-gate #include <ctype.h>
33*0Sstevel@tonic-gate #include <math.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <Audio.h>
36*0Sstevel@tonic-gate #include <AudioHdr.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include <parse.h>
39*0Sstevel@tonic-gate #include <convert.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate static struct keyword_table Keywords[] = {
42*0Sstevel@tonic-gate 	(char *)"encoding",		K_ENCODING,
43*0Sstevel@tonic-gate 	(char *)"rate",			K_RATE,
44*0Sstevel@tonic-gate 	(char *)"channels",		K_CHANNELS,
45*0Sstevel@tonic-gate 	(char *)"offset",		K_OFFSET,
46*0Sstevel@tonic-gate 	(char *)"format",		K_FORMAT,
47*0Sstevel@tonic-gate 	NULL,				K_NULL,
48*0Sstevel@tonic-gate };
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate // Lookup the string in a keyword table. return the token associated with it.
51*0Sstevel@tonic-gate keyword_type
do_lookup(char * s,struct keyword_table * kp)52*0Sstevel@tonic-gate do_lookup(
53*0Sstevel@tonic-gate 	char			*s,
54*0Sstevel@tonic-gate 	struct keyword_table	*kp)
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate 	struct keyword_table	*tkp = NULL;
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	for (; kp && kp->name; kp++) {
59*0Sstevel@tonic-gate 		if (strncmp(s, kp->name, strlen(s)) == 0) {
60*0Sstevel@tonic-gate 			// check if exact match
61*0Sstevel@tonic-gate 			if (strlen(s) == strlen(kp->name)) {
62*0Sstevel@tonic-gate 				return (kp->type);
63*0Sstevel@tonic-gate 			} else {
64*0Sstevel@tonic-gate 				// already have another partial match, so
65*0Sstevel@tonic-gate 				// it's ambiguous
66*0Sstevel@tonic-gate 				if (tkp) {
67*0Sstevel@tonic-gate 					return (K_AMBIG);
68*0Sstevel@tonic-gate 				} else {
69*0Sstevel@tonic-gate 					tkp = kp;
70*0Sstevel@tonic-gate 				}
71*0Sstevel@tonic-gate 			}
72*0Sstevel@tonic-gate 		}
73*0Sstevel@tonic-gate 	}
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	// at end of list. if there was a partial match, return it, if
76*0Sstevel@tonic-gate 	// not, there's no match....
77*0Sstevel@tonic-gate 	if (tkp) {
78*0Sstevel@tonic-gate 		return (tkp->type);
79*0Sstevel@tonic-gate 	} else {
80*0Sstevel@tonic-gate 		return (K_NULL);
81*0Sstevel@tonic-gate 	}
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate // Parse a file format specification
85*0Sstevel@tonic-gate int
fileformat_parse(char * val,format_type & format)86*0Sstevel@tonic-gate fileformat_parse(
87*0Sstevel@tonic-gate 	char		*val,
88*0Sstevel@tonic-gate 	format_type&	format)
89*0Sstevel@tonic-gate {
90*0Sstevel@tonic-gate 	// XXX - other formats later ...
91*0Sstevel@tonic-gate 	if (strcasecmp(val, "sun") == 0) {
92*0Sstevel@tonic-gate 		format = F_SUN;
93*0Sstevel@tonic-gate 	} else if (strcasecmp(val, "raw") == 0) {
94*0Sstevel@tonic-gate 		format = F_RAW;
95*0Sstevel@tonic-gate 	} else if (strcasecmp(val, "aiff") == 0) {
96*0Sstevel@tonic-gate 		Err(MGET("AIFF not yet supported\n"));
97*0Sstevel@tonic-gate 		return (-1);
98*0Sstevel@tonic-gate 	} else {
99*0Sstevel@tonic-gate 		return (-1);
100*0Sstevel@tonic-gate 	}
101*0Sstevel@tonic-gate 	return (0);
102*0Sstevel@tonic-gate }
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate // Parse an audio format keyword
105*0Sstevel@tonic-gate int
audioformat_parse(char * val,AudioHdr & hdr)106*0Sstevel@tonic-gate audioformat_parse(
107*0Sstevel@tonic-gate 	char		*val,
108*0Sstevel@tonic-gate 	AudioHdr&	hdr)
109*0Sstevel@tonic-gate {
110*0Sstevel@tonic-gate 	// check if it's "cd" or "dat" or "voice".
111*0Sstevel@tonic-gate 	// these set the precision and encoding, etc.
112*0Sstevel@tonic-gate 	if (strcasecmp(val, "dat") == 0) {
113*0Sstevel@tonic-gate 		hdr.sample_rate = 48000;
114*0Sstevel@tonic-gate 		hdr.channels = 2;
115*0Sstevel@tonic-gate 		hdr.encoding = LINEAR;
116*0Sstevel@tonic-gate 		hdr.samples_per_unit = 1;
117*0Sstevel@tonic-gate 		hdr.bytes_per_unit = 2;
118*0Sstevel@tonic-gate 	} else if (strcasecmp(val, "cd") == 0) {
119*0Sstevel@tonic-gate 		hdr.sample_rate = 44100;
120*0Sstevel@tonic-gate 		hdr.channels = 2;
121*0Sstevel@tonic-gate 		hdr.encoding = LINEAR;
122*0Sstevel@tonic-gate 		hdr.samples_per_unit = 1;
123*0Sstevel@tonic-gate 		hdr.bytes_per_unit = 2;
124*0Sstevel@tonic-gate 	} else if (strcasecmp(val, "voice") == 0) {
125*0Sstevel@tonic-gate 		hdr.sample_rate = 8000;
126*0Sstevel@tonic-gate 		hdr.channels = 1;
127*0Sstevel@tonic-gate 		hdr.encoding = ULAW;
128*0Sstevel@tonic-gate 		hdr.samples_per_unit = 1;
129*0Sstevel@tonic-gate 		hdr.bytes_per_unit = 1;
130*0Sstevel@tonic-gate 	} else {
131*0Sstevel@tonic-gate 		return (-1);
132*0Sstevel@tonic-gate 	}
133*0Sstevel@tonic-gate 	return (0);
134*0Sstevel@tonic-gate }
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate // Parse a format spec and return an audio header that describes it.
137*0Sstevel@tonic-gate // Format is in the form of: [keyword=]value[,[keyword=]value ...].
138*0Sstevel@tonic-gate int
parse_format(char * s,AudioHdr & hdr,format_type & format,off_t & offset)139*0Sstevel@tonic-gate parse_format(
140*0Sstevel@tonic-gate 	char		*s,
141*0Sstevel@tonic-gate 	AudioHdr&	hdr,
142*0Sstevel@tonic-gate 	format_type&	format,
143*0Sstevel@tonic-gate 	off_t&		offset)
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate 	char		*cp;
146*0Sstevel@tonic-gate 	char		*buf;
147*0Sstevel@tonic-gate 	char		*key;
148*0Sstevel@tonic-gate 	char		*val;
149*0Sstevel@tonic-gate 	char		*cp2;
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	offset = 0;
152*0Sstevel@tonic-gate 	format = F_SUN;
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	// if no string provided, just return ...
155*0Sstevel@tonic-gate 	if (!(s && *s))
156*0Sstevel@tonic-gate 		return (0);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	// First off, try to parse it as a full format string
159*0Sstevel@tonic-gate 	// (it would have to have been quoted).
160*0Sstevel@tonic-gate 	// If this works, we're done.
161*0Sstevel@tonic-gate 	if (hdr.FormatParse(s) == AUDIO_SUCCESS) {
162*0Sstevel@tonic-gate 		return (0);
163*0Sstevel@tonic-gate 	}
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	buf = strdup(s);	// save a copy of the string
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	// XXX - bug alert: if someone has info="xxx,yyy", strtok will
168*0Sstevel@tonic-gate 	// break unless we snarf properly snarf the info. punt for now,
169*0Sstevel@tonic-gate 	// fix later (since no info supported yet)....
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	for (cp = strtok(buf, ","); cp; cp = strtok(NULL, ",")) {
172*0Sstevel@tonic-gate 		// Check if there's a '='
173*0Sstevel@tonic-gate 		// If so, left side is keyword, right side is value.
174*0Sstevel@tonic-gate 		// If not, entire string is value.
175*0Sstevel@tonic-gate 		if (cp2 = strchr(cp, '=')) {
176*0Sstevel@tonic-gate 			*cp2++ = NULL;
177*0Sstevel@tonic-gate 			key = cp;
178*0Sstevel@tonic-gate 			val = cp2;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 			// Look for the keyword
181*0Sstevel@tonic-gate 			switch (do_lookup(key, Keywords)) {
182*0Sstevel@tonic-gate 			case K_ENCODING:
183*0Sstevel@tonic-gate 				if (hdr.EncodingParse(val)) {
184*0Sstevel@tonic-gate 					Err(MGET(
185*0Sstevel@tonic-gate 					    "invalid encoding option: %s\n"),
186*0Sstevel@tonic-gate 					    val);
187*0Sstevel@tonic-gate 					goto parse_error;
188*0Sstevel@tonic-gate 				}
189*0Sstevel@tonic-gate 				break;
190*0Sstevel@tonic-gate 			case K_RATE:
191*0Sstevel@tonic-gate 				if (hdr.RateParse(val)) {
192*0Sstevel@tonic-gate 					Err(MGET("invalid sample rate: %s\n"),
193*0Sstevel@tonic-gate 					    val);
194*0Sstevel@tonic-gate 					goto parse_error;
195*0Sstevel@tonic-gate 				}
196*0Sstevel@tonic-gate 				break;
197*0Sstevel@tonic-gate 			case K_CHANNELS:
198*0Sstevel@tonic-gate 				if (hdr.ChannelParse(val)) {
199*0Sstevel@tonic-gate 					Err(MGET(
200*0Sstevel@tonic-gate 					    "invalid channels option: %s\n"),
201*0Sstevel@tonic-gate 					    val);
202*0Sstevel@tonic-gate 					goto parse_error;
203*0Sstevel@tonic-gate 				}
204*0Sstevel@tonic-gate 				break;
205*0Sstevel@tonic-gate 			case K_FORMAT:
206*0Sstevel@tonic-gate 				if (fileformat_parse(val, format) < 0) {
207*0Sstevel@tonic-gate 					Err(MGET("unknown format: %s\n"), val);
208*0Sstevel@tonic-gate 					goto parse_error;
209*0Sstevel@tonic-gate 				}
210*0Sstevel@tonic-gate 				break;
211*0Sstevel@tonic-gate 			case K_OFFSET:
212*0Sstevel@tonic-gate 				offset = (off_t)atoi(val);
213*0Sstevel@tonic-gate 				break;
214*0Sstevel@tonic-gate 			case K_AMBIG:
215*0Sstevel@tonic-gate 				Err(MGET("ambiguous keyword: %s\n"), key);
216*0Sstevel@tonic-gate 				goto parse_error;
217*0Sstevel@tonic-gate 			case K_NULL:
218*0Sstevel@tonic-gate 				Err(MGET("null keyword: =%s\n"), val);
219*0Sstevel@tonic-gate 				goto parse_error;
220*0Sstevel@tonic-gate 			default:
221*0Sstevel@tonic-gate 				Err(MGET("invalid keyword: %s\n"), key);
222*0Sstevel@tonic-gate 				goto parse_error;
223*0Sstevel@tonic-gate 			}
224*0Sstevel@tonic-gate 		} else {
225*0Sstevel@tonic-gate 			// No keyword, so try to intuit the value
226*0Sstevel@tonic-gate 			// First try encoding, audio, and file format.
227*0Sstevel@tonic-gate 			// If they fail, try sample rate and channels.
228*0Sstevel@tonic-gate 			val = cp;
229*0Sstevel@tonic-gate 			if (hdr.EncodingParse(val) &&
230*0Sstevel@tonic-gate 			    (audioformat_parse(val, hdr) < 0) &&
231*0Sstevel@tonic-gate 			    (fileformat_parse(val, format) < 0)) {
232*0Sstevel@tonic-gate 				// If this looks like sample rate, make sure
233*0Sstevel@tonic-gate 				// it is not ambiguous with channels
234*0Sstevel@tonic-gate 				if (!hdr.RateParse(val)) {
235*0Sstevel@tonic-gate 					if (hdr.sample_rate < 1000) {
236*0Sstevel@tonic-gate 						int	x;
237*0Sstevel@tonic-gate 						char	y[10];
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 						if (sscanf(val, " %lf %9s",
240*0Sstevel@tonic-gate 						    &x, y) != 1) {
241*0Sstevel@tonic-gate 							Err(
242*0Sstevel@tonic-gate 					MGET("ambiguous numeric option: %s\n"),
243*0Sstevel@tonic-gate 							    val);
244*0Sstevel@tonic-gate 							goto parse_error;
245*0Sstevel@tonic-gate 						}
246*0Sstevel@tonic-gate 					}
247*0Sstevel@tonic-gate 				} else if (hdr.ChannelParse(val)) {
248*0Sstevel@tonic-gate 					Err(MGET("invalid option value: %s\n"),
249*0Sstevel@tonic-gate 					    val);
250*0Sstevel@tonic-gate 					goto parse_error;
251*0Sstevel@tonic-gate 				}
252*0Sstevel@tonic-gate 			}
253*0Sstevel@tonic-gate 		}
254*0Sstevel@tonic-gate 	}
255*0Sstevel@tonic-gate 	free(buf);
256*0Sstevel@tonic-gate 	return (0);
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate parse_error:
259*0Sstevel@tonic-gate 	free(buf);
260*0Sstevel@tonic-gate 	return (-1);
261*0Sstevel@tonic-gate }
262