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.

param_cmd.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * Form parameter commands
  23. *
  24. */
  25. #include <stdlib.h>
  26. #include <errno.h>
  27. #include <getopt.h>
  28. #include <ipxe/params.h>
  29. #include <ipxe/parseopt.h>
  30. #include <ipxe/command.h>
  31. /** "params" options */
  32. struct params_options {
  33. /** Name */
  34. char *name;
  35. /** Delete */
  36. int delete;
  37. };
  38. /** "params" option list */
  39. static struct option_descriptor params_opts[] = {
  40. OPTION_DESC ( "name", 'n', required_argument,
  41. struct params_options, name, parse_string ),
  42. OPTION_DESC ( "delete", 'd', no_argument,
  43. struct params_options, delete, parse_flag ),
  44. };
  45. /** "params" command descriptor */
  46. static struct command_descriptor params_cmd =
  47. COMMAND_DESC ( struct params_options, params_opts, 0, 0, NULL );
  48. /**
  49. * The "params" command
  50. *
  51. * @v argc Argument count
  52. * @v argv Argument list
  53. * @ret rc Return status code
  54. */
  55. static int params_exec ( int argc, char **argv ) {
  56. struct params_options opts;
  57. struct parameters *params;
  58. int rc;
  59. /* Parse options */
  60. if ( ( rc = parse_options ( argc, argv, &params_cmd, &opts ) ) != 0)
  61. return rc;
  62. /* Create parameter list */
  63. params = create_parameters ( opts.name );
  64. if ( ! params )
  65. return -ENOMEM;
  66. /* Destroy parameter list, if applicable */
  67. if ( opts.delete ) {
  68. claim_parameters ( params );
  69. params_put ( params );
  70. }
  71. return 0;
  72. }
  73. /** "param" options */
  74. struct param_options {
  75. /** Parameter list name */
  76. char *params;
  77. };
  78. /** "param" option list */
  79. static struct option_descriptor param_opts[] = {
  80. OPTION_DESC ( "params", 'p', required_argument,
  81. struct param_options, params, parse_string ),
  82. };
  83. /** "param" command descriptor */
  84. static struct command_descriptor param_cmd =
  85. COMMAND_DESC ( struct param_options, param_opts, 1, MAX_ARGUMENTS,
  86. "<key> [<value>]" );
  87. /**
  88. * The "param" command
  89. *
  90. * @v argc Argument count
  91. * @v argv Argument list
  92. * @ret rc Return status code
  93. */
  94. static int param_exec ( int argc, char **argv ) {
  95. struct param_options opts;
  96. char *key;
  97. char *value;
  98. struct parameters *params;
  99. struct parameter *param;
  100. int rc;
  101. /* Parse options */
  102. if ( ( rc = parse_options ( argc, argv, &param_cmd, &opts ) ) != 0 )
  103. goto err_parse_options;
  104. /* Parse key */
  105. key = argv[optind];
  106. /* Parse value */
  107. value = concat_args ( &argv[ optind + 1 ] );
  108. if ( ! value ) {
  109. rc = -ENOMEM;
  110. goto err_parse_value;
  111. }
  112. /* Identify parameter list */
  113. if ( ( rc = parse_parameters ( opts.params, &params ) ) != 0 )
  114. goto err_parse_parameters;
  115. /* Add parameter */
  116. param = add_parameter ( params, key, value );
  117. if ( ! param ) {
  118. rc = -ENOMEM;
  119. goto err_add_parameter;
  120. }
  121. /* Success */
  122. rc = 0;
  123. err_add_parameter:
  124. err_parse_parameters:
  125. free ( value );
  126. err_parse_value:
  127. err_parse_options:
  128. return rc;
  129. }
  130. /** Form parameter commands */
  131. struct command param_commands[] __command = {
  132. {
  133. .name = "params",
  134. .exec = params_exec,
  135. },
  136. {
  137. .name = "param",
  138. .exec = param_exec,
  139. },
  140. };