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.

209 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' for driver '%s'\n",
  93. argv[2], desc->name);
  94. }
  95. printf ("Options for driver '%s':\n", desc->name);
  96. jack_print_driver_options (desc, stdout);
  97. return 1;
  98. }
  99. } else {
  100. /* save some processing */
  101. *param_ptr = NULL;
  102. return 0;
  103. }
  104. /* set up the stuff for getopt */
  105. options = calloc (desc->nparams*3 + 1, sizeof (char));
  106. options_ptr = options;
  107. long_options = calloc (desc->nparams + 1, sizeof (struct option));
  108. for (i = 0; i < desc->nparams; i++) {
  109. *options_ptr = desc->params[i].character;
  110. options_ptr++;
  111. if (desc->params[i].has_arg > 0) {
  112. *options_ptr = ':';
  113. options_ptr++;
  114. if (desc->params[i].has_arg == optional_argument) {
  115. *options_ptr = ':';
  116. options_ptr++;
  117. }
  118. }
  119. long_options[i].name = desc->params[i].name;
  120. long_options[i].has_arg = desc->params[i].has_arg;
  121. long_options[i].flag = NULL;
  122. long_options[i].val = desc->params[i].character;
  123. }
  124. /* create the params */
  125. optind = 0;
  126. opterr = 0;
  127. while ((opt = getopt_long(argc, argv, options, long_options, NULL)) != -1) {
  128. if (opt == ':' || opt == '?') {
  129. if (opt == ':') {
  130. fprintf (stderr, "Missing option to argument '%c'\n", optopt);
  131. } else {
  132. fprintf (stderr, "Unknown option '%c'\n", optopt);
  133. }
  134. fprintf (stderr, "Options for driver '%s':\n", desc->name);
  135. jack_print_driver_options (desc, stderr);
  136. exit (1);
  137. }
  138. for (param_index = 0; desc->nparams; param_index++) {
  139. if (opt == desc->params[param_index].character)
  140. break;
  141. }
  142. driver_param = calloc (1, sizeof (jack_driver_param_t));
  143. driver_param->character = desc->params[param_index].character;
  144. if (optarg) {
  145. switch (desc->params[param_index].type) {
  146. case JackDriverParamInt:
  147. driver_param->value.i = atoi (optarg);
  148. break;
  149. case JackDriverParamUInt:
  150. driver_param->value.ui = strtoul (optarg, NULL, 10);
  151. break;
  152. case JackDriverParamChar:
  153. driver_param->value.c = optarg[0];
  154. break;
  155. case JackDriverParamString:
  156. strncpy (driver_param->value.str, optarg, JACK_DRIVER_PARAM_STRING_MAX);
  157. break;
  158. case JackDriverParamBool:
  159. driver_param->value.i = 1;
  160. break;
  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. return 0;
  170. }
  171. #endif /* __jack_driver_parse_h__ */