JACK API headers
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.

213 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 <jack/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. break;
  43. case JackDriverParamBool:
  44. sprintf (arg_default, "%s", desc->params[i].value.i ? "true" : "false");
  45. break;
  46. }
  47. fprintf (file, "\t-%c, --%s \t%s (default: %s)\n",
  48. desc->params[i].character,
  49. desc->params[i].name,
  50. desc->params[i].short_desc,
  51. arg_default);
  52. }
  53. }
  54. static void
  55. jack_print_driver_param_usage (jack_driver_desc_t * desc, unsigned long param, FILE *file)
  56. {
  57. fprintf (file, "Usage information for the '%s' parameter for driver '%s':\n",
  58. desc->params[param].name, desc->name);
  59. fprintf (file, "%s\n", desc->params[param].long_desc);
  60. }
  61. static int
  62. jack_parse_driver_params (jack_driver_desc_t * desc, int argc, char **argv, JSList ** param_ptr)
  63. {
  64. struct option * long_options;
  65. char * options, * options_ptr;
  66. unsigned long i;
  67. int opt, param_index;
  68. JSList * params = NULL;
  69. jack_driver_param_t * driver_param;
  70. if (argc <= 1) {
  71. *param_ptr = NULL;
  72. return 0;
  73. }
  74. /* check for help */
  75. if (strcmp (argv[1], "-h") == 0 || strcmp (argv[1], "--help") == 0) {
  76. if (argc > 2) {
  77. for (i = 0; i < desc->nparams; i++) {
  78. if (strcmp (desc->params[i].name, argv[2]) == 0) {
  79. jack_print_driver_param_usage (desc, i, stdout);
  80. return 1;
  81. }
  82. }
  83. fprintf (stderr, "jackd: unknown option '%s' "
  84. "for driver '%s'\n", argv[2],
  85. desc->name);
  86. }
  87. printf ("Parameters for driver '%s' (all parameters are optional):\n", desc->name);
  88. jack_print_driver_options (desc, stdout);
  89. return 1;
  90. }
  91. /* set up the stuff for getopt */
  92. options = calloc (desc->nparams*3 + 1, sizeof (char));
  93. long_options = calloc (desc->nparams + 1, sizeof (struct option));
  94. options_ptr = options;
  95. for (i = 0; i < desc->nparams; i++) {
  96. sprintf (options_ptr, "%c::", desc->params[i].character);
  97. options_ptr += 3;
  98. long_options[i].name = desc->params[i].name;
  99. long_options[i].flag = NULL;
  100. long_options[i].val = desc->params[i].character;
  101. long_options[i].has_arg = optional_argument;
  102. }
  103. /* create the params */
  104. optind = 0;
  105. opterr = 0;
  106. while ((opt = getopt_long(argc, argv, options, long_options, NULL)) != -1) {
  107. if (opt == ':' || opt == '?') {
  108. if (opt == ':') {
  109. fprintf (stderr, "Missing option to argument '%c'\n", optopt);
  110. } else {
  111. fprintf (stderr, "Unknownage with option '%c'\n", optopt);
  112. }
  113. fprintf (stderr, "Options for driver '%s':\n", desc->name);
  114. jack_print_driver_options (desc, stderr);
  115. exit (1);
  116. }
  117. for (param_index = 0; param_index < desc->nparams; param_index++) {
  118. if (opt == desc->params[param_index].character) {
  119. break;
  120. }
  121. }
  122. driver_param = calloc (1, sizeof (jack_driver_param_t));
  123. driver_param->character = desc->params[param_index].character;
  124. if (!optarg && optind < argc &&
  125. strlen(argv[optind]) &&
  126. argv[optind][0] != '-') {
  127. optarg = argv[optind];
  128. }
  129. if (optarg) {
  130. switch (desc->params[param_index].type) {
  131. case JackDriverParamInt:
  132. driver_param->value.i = atoi (optarg);
  133. break;
  134. case JackDriverParamUInt:
  135. driver_param->value.ui = strtoul (optarg, NULL, 10);
  136. break;
  137. case JackDriverParamChar:
  138. driver_param->value.c = optarg[0];
  139. break;
  140. case JackDriverParamString:
  141. strncpy (driver_param->value.str, optarg, JACK_DRIVER_PARAM_STRING_MAX);
  142. break;
  143. case JackDriverParamBool:
  144. if (strcasecmp ("false", optarg) == 0 ||
  145. strcasecmp ("off", optarg) == 0 ||
  146. strcasecmp ("no", optarg) == 0 ||
  147. strcasecmp ("0", optarg) == 0 ||
  148. strcasecmp ("(null)", optarg) == 0 ) {
  149. driver_param->value.i = FALSE;
  150. } else {
  151. driver_param->value.i = TRUE;
  152. }
  153. break;
  154. }
  155. } else {
  156. if (desc->params[param_index].type == JackDriverParamBool) {
  157. driver_param->value.i = TRUE;
  158. } else {
  159. driver_param->value = desc->params[param_index].value;
  160. }
  161. }
  162. params = jack_slist_append (params, driver_param);
  163. }
  164. free (options);
  165. free (long_options);
  166. if (param_ptr)
  167. *param_ptr = params;
  168. return 0;
  169. }
  170. #endif /* __jack_driver_parse_h__ */