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.

210 lines
5.3KB

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