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.

nvo_cmd.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (C) 2010 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <getopt.h>
  24. #include <ipxe/settings.h>
  25. #include <ipxe/command.h>
  26. #include <ipxe/parseopt.h>
  27. #include <readline/readline.h>
  28. FILE_LICENCE ( GPL2_OR_LATER );
  29. /** @file
  30. *
  31. * Non-volatile option commands
  32. *
  33. */
  34. /** "show" options */
  35. struct show_options {};
  36. /** "show" option list */
  37. static struct option_descriptor show_opts[] = {};
  38. /** "show" command descriptor */
  39. static struct command_descriptor show_cmd =
  40. COMMAND_DESC ( struct show_options, show_opts, 1, 1, "<setting>" );
  41. /**
  42. * "show" command
  43. *
  44. * @v argc Argument count
  45. * @v argv Argument list
  46. * @ret rc Return status code
  47. */
  48. static int show_exec ( int argc, char **argv ) {
  49. struct show_options opts;
  50. const char *name;
  51. char name_buf[32];
  52. char value_buf[256];
  53. int rc;
  54. /* Parse options */
  55. if ( ( rc = parse_options ( argc, argv, &show_cmd, &opts ) ) != 0 )
  56. return rc;
  57. /* Parse setting name */
  58. name = argv[optind];
  59. /* Fetch setting */
  60. if ( ( rc = fetchf_named_setting ( name, name_buf, sizeof ( name_buf ),
  61. value_buf,
  62. sizeof ( value_buf ) ) ) < 0 ) {
  63. printf ( "Could not find \"%s\": %s\n",
  64. name, strerror ( rc ) );
  65. return rc;
  66. }
  67. /* Print setting value */
  68. printf ( "%s = %s\n", name_buf, value_buf );
  69. return 0;
  70. }
  71. /** "set", "clear", and "read" options */
  72. struct set_core_options {};
  73. /** "set", "clear", and "read" option list */
  74. static struct option_descriptor set_core_opts[] = {};
  75. /** "set" command descriptor */
  76. static struct command_descriptor set_cmd =
  77. COMMAND_DESC ( struct set_core_options, set_core_opts, 1, MAX_ARGUMENTS,
  78. "<setting> <value>" );
  79. /** "clear" and "read" command descriptor */
  80. static struct command_descriptor clear_read_cmd =
  81. COMMAND_DESC ( struct set_core_options, set_core_opts, 1, 1,
  82. "<setting>" );
  83. /**
  84. * "set", "clear", and "read" command
  85. *
  86. * @v argc Argument count
  87. * @v argv Argument list
  88. * @v cmd Command descriptor
  89. * @v get_value Method to obtain setting value
  90. * @ret rc Return status code
  91. */
  92. static int set_core_exec ( int argc, char **argv,
  93. struct command_descriptor *cmd,
  94. int ( * get_value ) ( char **args, char **value ) ) {
  95. struct set_core_options opts;
  96. const char *name;
  97. char *value;
  98. int rc;
  99. /* Parse options */
  100. if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
  101. goto err_parse_options;
  102. /* Parse setting name */
  103. name = argv[optind];
  104. /* Parse setting value */
  105. if ( ( rc = get_value ( &argv[ optind + 1 ], &value ) ) != 0 )
  106. goto err_get_value;
  107. /* Determine total length of command line */
  108. if ( ( rc = storef_named_setting ( name, value ) ) != 0 ) {
  109. printf ( "Could not %s \"%s\": %s\n",
  110. argv[0], name, strerror ( rc ) );
  111. goto err_store;
  112. }
  113. free ( value );
  114. return 0;
  115. err_store:
  116. free ( value );
  117. err_get_value:
  118. err_parse_options:
  119. return rc;
  120. }
  121. /**
  122. * Get setting value for "set" command
  123. *
  124. * @v args Remaining arguments
  125. * @ret value Setting value
  126. * @ret rc Return status code
  127. */
  128. static int set_value ( char **args, char **value ) {
  129. *value = concat_args ( args );
  130. if ( ! *value )
  131. return -ENOMEM;
  132. return 0;
  133. }
  134. /**
  135. * "set" command
  136. *
  137. * @v argc Argument count
  138. * @v argv Argument list
  139. * @ret rc Return status code
  140. */
  141. static int set_exec ( int argc, char **argv ) {
  142. return set_core_exec ( argc, argv, &set_cmd, set_value );
  143. }
  144. /**
  145. * Get setting value for "clear" command
  146. *
  147. * @v args Remaining arguments
  148. * @ret value Setting value
  149. * @ret rc Return status code
  150. */
  151. static int clear_value ( char **args __unused, char **value ) {
  152. *value = NULL;
  153. return 0;
  154. }
  155. /**
  156. * "clear" command
  157. *
  158. * @v argc Argument count
  159. * @v argv Argument list
  160. * @ret rc Return status code
  161. */
  162. static int clear_exec ( int argc, char **argv ) {
  163. return set_core_exec ( argc, argv, &clear_read_cmd, clear_value );
  164. }
  165. /**
  166. * Get setting value for "read" command
  167. *
  168. * @ret value Setting value
  169. * @ret rc Return status code
  170. */
  171. static int read_value ( char **args __unused, char **value ) {
  172. *value = readline ( NULL );
  173. if ( ! *value )
  174. return -ENOMEM;
  175. return 0;
  176. }
  177. /**
  178. * "read" command
  179. *
  180. * @v argc Argument count
  181. * @v argv Argument list
  182. * @ret rc Return status code
  183. */
  184. static int read_exec ( int argc, char **argv ) {
  185. return set_core_exec ( argc, argv, &clear_read_cmd, read_value );
  186. }
  187. /** Non-volatile option commands */
  188. struct command nvo_commands[] __command = {
  189. {
  190. .name = "show",
  191. .exec = show_exec,
  192. },
  193. {
  194. .name = "set",
  195. .exec = set_exec,
  196. },
  197. {
  198. .name = "clear",
  199. .exec = clear_exec,
  200. },
  201. {
  202. .name = "read",
  203. .exec = read_exec,
  204. },
  205. };