1 /* -*- mode: c; c-file-style: "linux"; -*- */
3 Copyright (C) 2003 Bob Ham <rah@bash.sh
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #ifndef __jack_driver_parse_h__
22 #define __jack_driver_parse_h__
24 #include <jack/jslist.h>
25 #include <jack/driver_interface.h>
28 jack_print_driver_options (jack_driver_desc_t
* desc
, FILE *file
)
31 char arg_default
[JACK_DRIVER_PARAM_STRING_MAX
+ 1];
33 for (i
= 0; i
< desc
->nparams
; i
++) {
34 switch (desc
->params
[i
].type
) {
35 case JackDriverParamInt
:
36 sprintf (arg_default
, "%" PRId32
, desc
->params
[i
].value
.i
);
38 case JackDriverParamUInt
:
39 sprintf (arg_default
, "%" PRIu32
, desc
->params
[i
].value
.ui
);
41 case JackDriverParamChar
:
42 sprintf (arg_default
, "%c", desc
->params
[i
].value
.c
);
44 case JackDriverParamString
:
45 if (desc
->params
[i
].value
.str
&&
46 strcmp (desc
->params
[i
].value
.str
, "") != 0)
47 sprintf (arg_default
, "%s", desc
->params
[i
].value
.str
);
49 sprintf (arg_default
, "none");
51 case JackDriverParamBool
:
52 sprintf (arg_default
, "%s", desc
->params
[i
].value
.i
? "true" : "false");
56 fprintf (file
, "\t-%c, --%s \t%s (default: %s)\n",
57 desc
->params
[i
].character
,
59 desc
->params
[i
].short_desc
,
65 jack_print_driver_param_usage (jack_driver_desc_t
* desc
, unsigned long param
, FILE *file
)
67 fprintf (file
, "Usage information for the '%s' parameter for driver '%s':\n",
68 desc
->params
[param
].name
, desc
->name
);
70 fprintf (file
, "%s\n", desc
->params
[param
].long_desc
);
75 jack_parse_driver_params (jack_driver_desc_t
* desc
, int argc
, char **argv
, JSList
** param_ptr
)
77 struct option
* long_options
;
78 char * options
, * options_ptr
;
81 JSList
* params
= NULL
;
82 jack_driver_param_t
* driver_param
;
90 if (strcmp (argv
[1], "-h") == 0 || strcmp (argv
[1], "--help") == 0) {
92 for (i
= 0; i
< desc
->nparams
; i
++) {
93 if (strcmp (desc
->params
[i
].name
, argv
[2]) == 0) {
94 jack_print_driver_param_usage (desc
, i
, stdout
);
99 fprintf (stderr
, "jackd: unknown option '%s' "
100 "for driver '%s'\n", argv
[2],
104 printf ("Parameters for driver '%s' (all parameters are optional):\n", desc
->name
);
105 jack_print_driver_options (desc
, stdout
);
110 /* set up the stuff for getopt */
111 options
= calloc (desc
->nparams
*3 + 1, sizeof (char));
112 long_options
= calloc (desc
->nparams
+ 1, sizeof (struct option
));
114 options_ptr
= options
;
115 for (i
= 0; i
< desc
->nparams
; i
++) {
116 sprintf (options_ptr
, "%c::", desc
->params
[i
].character
);
119 long_options
[i
].name
= desc
->params
[i
].name
;
120 long_options
[i
].flag
= NULL
;
121 long_options
[i
].val
= desc
->params
[i
].character
;
122 long_options
[i
].has_arg
= optional_argument
;
125 /* create the params */
128 while ((opt
= getopt_long(argc
, argv
, options
, long_options
, NULL
)) != -1) {
130 if (opt
== ':' || opt
== '?') {
132 fprintf (stderr
, "Missing option to argument '%c'\n", optopt
);
134 fprintf (stderr
, "Unknownage with option '%c'\n", optopt
);
137 fprintf (stderr
, "Options for driver '%s':\n", desc
->name
);
138 jack_print_driver_options (desc
, stderr
);
142 for (param_index
= 0; param_index
< desc
->nparams
; param_index
++) {
143 if (opt
== desc
->params
[param_index
].character
) {
148 driver_param
= calloc (1, sizeof (jack_driver_param_t
));
150 driver_param
->character
= desc
->params
[param_index
].character
;
152 if (!optarg
&& optind
< argc
&&
153 strlen(argv
[optind
]) &&
154 argv
[optind
][0] != '-') {
155 optarg
= argv
[optind
];
159 switch (desc
->params
[param_index
].type
) {
160 case JackDriverParamInt
:
161 driver_param
->value
.i
= atoi (optarg
);
163 case JackDriverParamUInt
:
164 driver_param
->value
.ui
= strtoul (optarg
, NULL
, 10);
166 case JackDriverParamChar
:
167 driver_param
->value
.c
= optarg
[0];
169 case JackDriverParamString
:
170 strncpy (driver_param
->value
.str
, optarg
, JACK_DRIVER_PARAM_STRING_MAX
);
172 case JackDriverParamBool
:
174 if (strcasecmp ("false", optarg
) == 0 ||
175 strcasecmp ("off", optarg
) == 0 ||
176 strcasecmp ("no", optarg
) == 0 ||
177 strcasecmp ("0", optarg
) == 0 ||
178 strcasecmp ("(null)", optarg
) == 0 ) {
180 driver_param
->value
.i
= FALSE
;
184 driver_param
->value
.i
= TRUE
;
190 if (desc
->params
[param_index
].type
== JackDriverParamBool
) {
191 driver_param
->value
.i
= TRUE
;
193 driver_param
->value
= desc
->params
[param_index
].value
;
197 params
= jack_slist_append (params
, driver_param
);
210 #endif /* __jack_driver_parse_h__ */