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.8KB

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