Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

param_cmd.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Form parameter commands
  27. *
  28. */
  29. #include <stdlib.h>
  30. #include <errno.h>
  31. #include <getopt.h>
  32. #include <ipxe/params.h>
  33. #include <ipxe/parseopt.h>
  34. #include <ipxe/command.h>
  35. /** "params" options */
  36. struct params_options {
  37. /** Name */
  38. char *name;
  39. /** Delete */
  40. int delete;
  41. };
  42. /** "params" option list */
  43. static struct option_descriptor params_opts[] = {
  44. OPTION_DESC ( "name", 'n', required_argument,
  45. struct params_options, name, parse_string ),
  46. OPTION_DESC ( "delete", 'd', no_argument,
  47. struct params_options, delete, parse_flag ),
  48. };
  49. /** "params" command descriptor */
  50. static struct command_descriptor params_cmd =
  51. COMMAND_DESC ( struct params_options, params_opts, 0, 0, NULL );
  52. /**
  53. * The "params" command
  54. *
  55. * @v argc Argument count
  56. * @v argv Argument list
  57. * @ret rc Return status code
  58. */
  59. static int params_exec ( int argc, char **argv ) {
  60. struct params_options opts;
  61. struct parameters *params;
  62. int rc;
  63. /* Parse options */
  64. if ( ( rc = parse_options ( argc, argv, &params_cmd, &opts ) ) != 0)
  65. return rc;
  66. /* Create parameter list */
  67. params = create_parameters ( opts.name );
  68. if ( ! params )
  69. return -ENOMEM;
  70. /* Destroy parameter list, if applicable */
  71. if ( opts.delete ) {
  72. claim_parameters ( params );
  73. params_put ( params );
  74. }
  75. return 0;
  76. }
  77. /** "param" options */
  78. struct param_options {
  79. /** Parameter list name */
  80. char *params;
  81. };
  82. /** "param" option list */
  83. static struct option_descriptor param_opts[] = {
  84. OPTION_DESC ( "params", 'p', required_argument,
  85. struct param_options, params, parse_string ),
  86. };
  87. /** "param" command descriptor */
  88. static struct command_descriptor param_cmd =
  89. COMMAND_DESC ( struct param_options, param_opts, 1, MAX_ARGUMENTS,
  90. "<key> [<value>]" );
  91. /**
  92. * The "param" command
  93. *
  94. * @v argc Argument count
  95. * @v argv Argument list
  96. * @ret rc Return status code
  97. */
  98. static int param_exec ( int argc, char **argv ) {
  99. struct param_options opts;
  100. char *key;
  101. char *value;
  102. struct parameters *params;
  103. struct parameter *param;
  104. int rc;
  105. /* Parse options */
  106. if ( ( rc = parse_options ( argc, argv, &param_cmd, &opts ) ) != 0 )
  107. goto err_parse_options;
  108. /* Parse key */
  109. key = argv[optind];
  110. /* Parse value */
  111. value = concat_args ( &argv[ optind + 1 ] );
  112. if ( ! value ) {
  113. rc = -ENOMEM;
  114. goto err_parse_value;
  115. }
  116. /* Identify parameter list */
  117. if ( ( rc = parse_parameters ( opts.params, &params ) ) != 0 )
  118. goto err_parse_parameters;
  119. /* Add parameter */
  120. param = add_parameter ( params, key, value );
  121. if ( ! param ) {
  122. rc = -ENOMEM;
  123. goto err_add_parameter;
  124. }
  125. /* Success */
  126. rc = 0;
  127. err_add_parameter:
  128. err_parse_parameters:
  129. free ( value );
  130. err_parse_value:
  131. err_parse_options:
  132. return rc;
  133. }
  134. /** Form parameter commands */
  135. struct command param_commands[] __command = {
  136. {
  137. .name = "params",
  138. .exec = params_exec,
  139. },
  140. {
  141. .name = "param",
  142. .exec = param_exec,
  143. },
  144. };