jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
5.5KB

  1. /* -*- mode: c; c-file-style: "linux"; -*- */
  2. /*
  3. Copyright (C) 2003 Bob Ham <rah@bash.sh
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #ifndef __jack_driver_parse_h__
  17. #define __jack_driver_parse_h__
  18. #include <jack/jslist.h>
  19. #include "driver_interface.h"
  20. static void
  21. jack_print_driver_options (jack_driver_desc_t * desc, FILE *file)
  22. {
  23. unsigned long i;
  24. char arg_default[JACK_DRIVER_PARAM_STRING_MAX + 1];
  25. for (i = 0; i < desc->nparams; i++) {
  26. switch (desc->params[i].type) {
  27. case JackDriverParamInt:
  28. sprintf (arg_default, "%" PRId32, desc->params[i].value.i);
  29. break;
  30. case JackDriverParamUInt:
  31. sprintf (arg_default, "%" PRIu32, desc->params[i].value.ui);
  32. break;
  33. case JackDriverParamChar:
  34. sprintf (arg_default, "%c", desc->params[i].value.c);
  35. break;
  36. case JackDriverParamString:
  37. if (desc->params[i].value.str &&
  38. strcmp (desc->params[i].value.str, "") != 0) {
  39. sprintf (arg_default, "%s", desc->params[i].value.str);
  40. } else {
  41. sprintf (arg_default, "none");
  42. }
  43. break;
  44. case JackDriverParamBool:
  45. sprintf (arg_default, "%s", desc->params[i].value.i ? "true" : "false");
  46. break;
  47. }
  48. fprintf (file, "\t-%c, --%s \t%s (default: %s)\n",
  49. desc->params[i].character,
  50. desc->params[i].name,
  51. desc->params[i].short_desc,
  52. arg_default);
  53. }
  54. }
  55. static void
  56. jack_print_driver_param_usage (jack_driver_desc_t * desc, unsigned long param, FILE *file)
  57. {
  58. fprintf (file, "Usage information for the '%s' parameter for driver '%s':\n",
  59. desc->params[param].name, desc->name);
  60. fprintf (file, "%s\n", desc->params[param].long_desc);
  61. }
  62. static int
  63. jack_parse_driver_params (jack_driver_desc_t * desc, int argc, char **argv, JSList ** param_ptr)
  64. {
  65. struct option * long_options;
  66. char * options, * options_ptr;
  67. unsigned long i;
  68. int opt, param_index;
  69. JSList * params = NULL;
  70. jack_driver_param_t * driver_param;
  71. if (argc <= 1) {
  72. *param_ptr = NULL;
  73. return 0;
  74. }
  75. /* check for help */
  76. if (strcmp (argv[1], "-h") == 0 || strcmp (argv[1], "--help") == 0) {
  77. if (argc > 2) {
  78. for (i = 0; i < desc->nparams; i++) {
  79. if (strcmp (desc->params[i].name, argv[2]) == 0) {
  80. jack_print_driver_param_usage (desc, i, stdout);
  81. return 1;
  82. }
  83. }
  84. fprintf (stderr, "jackd: unknown option '%s' "
  85. "for driver '%s'\n", argv[2],
  86. desc->name);
  87. }
  88. printf ("Parameters for driver '%s' (all parameters are optional):\n", desc->name);
  89. jack_print_driver_options (desc, stdout);
  90. return 1;
  91. }
  92. /* set up the stuff for getopt */
  93. options = calloc (desc->nparams * 3 + 1, sizeof(char));
  94. long_options = calloc (desc->nparams + 1, sizeof(struct option));
  95. options_ptr = options;
  96. for (i = 0; i < desc->nparams; i++) {
  97. sprintf (options_ptr, "%c::", desc->params[i].character);
  98. options_ptr += 3;
  99. long_options[i].name = desc->params[i].name;
  100. long_options[i].flag = NULL;
  101. long_options[i].val = desc->params[i].character;
  102. long_options[i].has_arg = optional_argument;
  103. }
  104. /* create the params */
  105. optind = 0;
  106. opterr = 0;
  107. while ((opt = getopt_long (argc, argv, options, long_options, NULL)) != -1) {
  108. if (opt == ':' || opt == '?') {
  109. if (opt == ':') {
  110. fprintf (stderr, "Missing option to argument '%c'\n", optopt);
  111. } else {
  112. fprintf (stderr, "Unknownage with option '%c'\n", optopt);
  113. }
  114. fprintf (stderr, "Options for driver '%s':\n", desc->name);
  115. jack_print_driver_options (desc, stderr);
  116. exit (1);
  117. }
  118. for (param_index = 0; param_index < desc->nparams; param_index++) {
  119. if (opt == desc->params[param_index].character) {
  120. break;
  121. }
  122. }
  123. driver_param = calloc (1, sizeof(jack_driver_param_t));
  124. driver_param->character = desc->params[param_index].character;
  125. if (!optarg && optind < argc &&
  126. strlen (argv[optind]) &&
  127. argv[optind][0] != '-') {
  128. optarg = argv[optind];
  129. }
  130. if (optarg) {
  131. switch (desc->params[param_index].type) {
  132. case JackDriverParamInt:
  133. driver_param->value.i = atoi (optarg);
  134. break;
  135. case JackDriverParamUInt:
  136. driver_param->value.ui = strtoul (optarg, NULL, 10);
  137. break;
  138. case JackDriverParamChar:
  139. driver_param->value.c = optarg[0];
  140. break;
  141. case JackDriverParamString:
  142. strncpy (driver_param->value.str, optarg, JACK_DRIVER_PARAM_STRING_MAX);
  143. break;
  144. case JackDriverParamBool:
  145. if (strcasecmp ("false", optarg) == 0 ||
  146. strcasecmp ("off", optarg) == 0 ||
  147. strcasecmp ("no", optarg) == 0 ||
  148. strcasecmp ("0", optarg) == 0 ||
  149. strcasecmp ("(null)", optarg) == 0 ) {
  150. driver_param->value.i = FALSE;
  151. } else {
  152. driver_param->value.i = TRUE;
  153. }
  154. break;
  155. }
  156. } else {
  157. if (desc->params[param_index].type == JackDriverParamBool) {
  158. driver_param->value.i = TRUE;
  159. } else {
  160. driver_param->value = desc->params[param_index].value;
  161. }
  162. }
  163. params = jack_slist_append (params, driver_param);
  164. }
  165. free (options);
  166. free (long_options);
  167. if (param_ptr) {
  168. *param_ptr = params;
  169. }
  170. return 0;
  171. }
  172. #endif /* __jack_driver_parse_h__ */