nvo_cmd.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. FILE_LICENCE ( GPL2_OR_LATER );
  28. /** @file
  29. *
  30. * Non-volatile option commands
  31. *
  32. */
  33. /** "show" options */
  34. struct show_options {};
  35. /** "show" option list */
  36. static struct option_descriptor show_opts[] = {};
  37. /** "show" command descriptor */
  38. static struct command_descriptor show_cmd =
  39. COMMAND_DESC ( struct show_options, show_opts, 1, 1, "<setting>" );
  40. /**
  41. * "show" command
  42. *
  43. * @v argc Argument count
  44. * @v argv Argument list
  45. * @ret rc Return status code
  46. */
  47. static int show_exec ( int argc, char **argv ) {
  48. struct show_options opts;
  49. const char *name;
  50. char buf[256];
  51. int rc;
  52. /* Parse options */
  53. if ( ( rc = parse_options ( argc, argv, &show_cmd, &opts ) ) != 0 )
  54. return rc;
  55. /* Parse setting name */
  56. name = argv[optind];
  57. /* Fetch setting */
  58. if ( ( rc = fetchf_named_setting ( name, buf,
  59. sizeof ( buf ) ) ) < 0 ) {
  60. printf ( "Could not find \"%s\": %s\n",
  61. name, strerror ( rc ) );
  62. return rc;
  63. }
  64. /* Print setting value */
  65. printf ( "%s = %s\n", name, buf );
  66. return 0;
  67. }
  68. /** "set" options */
  69. struct set_options {};
  70. /** "set" option list */
  71. static struct option_descriptor set_opts[] = {};
  72. /** "set" command descriptor */
  73. static struct command_descriptor set_cmd =
  74. COMMAND_DESC ( struct set_options, set_opts, 1, MAX_ARGUMENTS,
  75. "<setting> <value>" );
  76. /**
  77. * "set" command
  78. *
  79. * @v argc Argument count
  80. * @v argv Argument list
  81. * @ret rc Return status code
  82. */
  83. static int set_exec ( int argc, char **argv ) {
  84. struct set_options opts;
  85. const char *name;
  86. char *value;
  87. int rc;
  88. /* Parse options */
  89. if ( ( rc = parse_options ( argc, argv, &set_cmd, &opts ) ) != 0 )
  90. goto err_parse_options;
  91. /* Parse setting name */
  92. name = argv[optind];
  93. /* Parse setting value */
  94. value = concat_args ( &argv[ optind + 1 ] );
  95. if ( ! value ) {
  96. rc = -ENOMEM;
  97. goto err_concat_args;
  98. }
  99. /* Determine total length of command line */
  100. if ( ( rc = storef_named_setting ( name, value ) ) != 0 ) {
  101. printf ( "Could not set \"%s\"=\"%s\": %s\n",
  102. name, value, strerror ( rc ) );
  103. goto err_store;
  104. }
  105. free ( value );
  106. return 0;
  107. err_store:
  108. free ( value );
  109. err_concat_args:
  110. err_parse_options:
  111. return rc;
  112. }
  113. /** "clear" options */
  114. struct clear_options {};
  115. /** "clear" option list */
  116. static struct option_descriptor clear_opts[] = {};
  117. /** "clear" command descriptor */
  118. static struct command_descriptor clear_cmd =
  119. COMMAND_DESC ( struct clear_options, clear_opts, 1, 1, "<setting>" );
  120. /**
  121. * "clear" command
  122. *
  123. * @v argc Argument count
  124. * @v argv Argument list
  125. * @ret rc Return status code
  126. */
  127. static int clear_exec ( int argc, char **argv ) {
  128. struct clear_options opts;
  129. const char *name;
  130. int rc;
  131. /* Parse options */
  132. if ( ( rc = parse_options ( argc, argv, &clear_cmd, &opts ) ) != 0 )
  133. return rc;
  134. /* Parse setting name */
  135. name = argv[optind];
  136. /* Clear setting */
  137. if ( ( rc = delete_named_setting ( name ) ) != 0 ) {
  138. printf ( "Could not clear \"%s\": %s\n",
  139. name, strerror ( rc ) );
  140. return rc;
  141. }
  142. return 0;
  143. }
  144. /** Non-volatile option commands */
  145. struct command nvo_commands[] __command = {
  146. {
  147. .name = "show",
  148. .exec = show_exec,
  149. },
  150. {
  151. .name = "set",
  152. .exec = set_exec,
  153. },
  154. {
  155. .name = "clear",
  156. .exec = clear_exec,
  157. },
  158. };