stop & detach driver when exiting due to -T/--temporary flag
[jack.git] / jack / driver_parse.h
blobd6d63b79fc24cffabb3965e2a755813ff6527744
1 /* -*- mode: c; c-file-style: "linux"; -*- */
2 /*
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>
27 static void
28 jack_print_driver_options (jack_driver_desc_t * desc, FILE *file)
30 unsigned long i;
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);
37 break;
38 case JackDriverParamUInt:
39 sprintf (arg_default, "%" PRIu32, desc->params[i].value.ui);
40 break;
41 case JackDriverParamChar:
42 sprintf (arg_default, "%c", desc->params[i].value.c);
43 break;
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);
48 else
49 sprintf (arg_default, "none");
50 break;
51 case JackDriverParamBool:
52 sprintf (arg_default, "%s", desc->params[i].value.i ? "true" : "false");
53 break;
56 fprintf (file, "\t-%c, --%s \t%s (default: %s)\n",
57 desc->params[i].character,
58 desc->params[i].name,
59 desc->params[i].short_desc,
60 arg_default);
64 static void
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);
74 static int
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;
79 unsigned long i;
80 int opt, param_index;
81 JSList * params = NULL;
82 jack_driver_param_t * driver_param;
84 if (argc <= 1) {
85 *param_ptr = NULL;
86 return 0;
89 /* check for help */
90 if (strcmp (argv[1], "-h") == 0 || strcmp (argv[1], "--help") == 0) {
91 if (argc > 2) {
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);
95 return 1;
99 fprintf (stderr, "jackd: unknown option '%s' "
100 "for driver '%s'\n", argv[2],
101 desc->name);
104 printf ("Parameters for driver '%s' (all parameters are optional):\n", desc->name);
105 jack_print_driver_options (desc, stdout);
106 return 1;
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);
117 options_ptr += 3;
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 */
126 optind = 0;
127 opterr = 0;
128 while ((opt = getopt_long(argc, argv, options, long_options, NULL)) != -1) {
130 if (opt == ':' || opt == '?') {
131 if (opt == ':') {
132 fprintf (stderr, "Missing option to argument '%c'\n", optopt);
133 } else {
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);
139 exit (1);
142 for (param_index = 0; param_index < desc->nparams; param_index++) {
143 if (opt == desc->params[param_index].character) {
144 break;
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];
158 if (optarg) {
159 switch (desc->params[param_index].type) {
160 case JackDriverParamInt:
161 driver_param->value.i = atoi (optarg);
162 break;
163 case JackDriverParamUInt:
164 driver_param->value.ui = strtoul (optarg, NULL, 10);
165 break;
166 case JackDriverParamChar:
167 driver_param->value.c = optarg[0];
168 break;
169 case JackDriverParamString:
170 strncpy (driver_param->value.str, optarg, JACK_DRIVER_PARAM_STRING_MAX);
171 break;
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;
182 } else {
184 driver_param->value.i = TRUE;
187 break;
189 } else {
190 if (desc->params[param_index].type == JackDriverParamBool) {
191 driver_param->value.i = TRUE;
192 } else {
193 driver_param->value = desc->params[param_index].value;
197 params = jack_slist_append (params, driver_param);
200 free (options);
201 free (long_options);
203 if (param_ptr)
204 *param_ptr = params;
206 return 0;
210 #endif /* __jack_driver_parse_h__ */