|
|
1.1 ! root 1: /* ! 2: * Copyright (C) 2010 Michael Brown <[email protected]>. ! 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: ! 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: ! 30: FILE_LICENCE ( GPL2_OR_LATER ); ! 31: ! 32: /** @file ! 33: * ! 34: * Non-volatile option commands ! 35: * ! 36: */ ! 37: ! 38: /** "show" options */ ! 39: struct show_options {}; ! 40: ! 41: /** "show" option list */ ! 42: static struct option_descriptor show_opts[] = {}; ! 43: ! 44: /** "show" command descriptor */ ! 45: static struct command_descriptor show_cmd = ! 46: COMMAND_DESC ( struct show_options, show_opts, 1, 1, "<setting>" ); ! 47: ! 48: /** ! 49: * "show" command ! 50: * ! 51: * @v argc Argument count ! 52: * @v argv Argument list ! 53: * @ret rc Return status code ! 54: */ ! 55: static int show_exec ( int argc, char **argv ) { ! 56: struct show_options opts; ! 57: const char *name; ! 58: char name_buf[32]; ! 59: char value_buf[256]; ! 60: int rc; ! 61: ! 62: /* Parse options */ ! 63: if ( ( rc = parse_options ( argc, argv, &show_cmd, &opts ) ) != 0 ) ! 64: return rc; ! 65: ! 66: /* Parse setting name */ ! 67: name = argv[optind]; ! 68: ! 69: /* Fetch setting */ ! 70: if ( ( rc = fetchf_named_setting ( name, name_buf, sizeof ( name_buf ), ! 71: value_buf, ! 72: sizeof ( value_buf ) ) ) < 0 ) { ! 73: printf ( "Could not find \"%s\": %s\n", ! 74: name, strerror ( rc ) ); ! 75: return rc; ! 76: } ! 77: ! 78: /* Print setting value */ ! 79: printf ( "%s = %s\n", name_buf, value_buf ); ! 80: ! 81: return 0; ! 82: } ! 83: ! 84: /** "set", "clear", and "read" options */ ! 85: struct set_core_options {}; ! 86: ! 87: /** "set", "clear", and "read" option list */ ! 88: static struct option_descriptor set_core_opts[] = {}; ! 89: ! 90: /** "set" command descriptor */ ! 91: static struct command_descriptor set_cmd = ! 92: COMMAND_DESC ( struct set_core_options, set_core_opts, 1, MAX_ARGUMENTS, ! 93: "<setting> <value>" ); ! 94: ! 95: /** "clear" and "read" command descriptor */ ! 96: static struct command_descriptor clear_read_cmd = ! 97: COMMAND_DESC ( struct set_core_options, set_core_opts, 1, 1, ! 98: "<setting>" ); ! 99: ! 100: /** ! 101: * "set", "clear", and "read" command ! 102: * ! 103: * @v argc Argument count ! 104: * @v argv Argument list ! 105: * @v cmd Command descriptor ! 106: * @v get_value Method to obtain setting value ! 107: * @ret rc Return status code ! 108: */ ! 109: static int set_core_exec ( int argc, char **argv, ! 110: struct command_descriptor *cmd, ! 111: int ( * get_value ) ( char **args, char **value ) ) { ! 112: struct set_core_options opts; ! 113: const char *name; ! 114: char *value; ! 115: int rc; ! 116: ! 117: /* Parse options */ ! 118: if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 ) ! 119: goto err_parse_options; ! 120: ! 121: /* Parse setting name */ ! 122: name = argv[optind]; ! 123: ! 124: /* Parse setting value */ ! 125: if ( ( rc = get_value ( &argv[ optind + 1 ], &value ) ) != 0 ) ! 126: goto err_get_value; ! 127: ! 128: /* Determine total length of command line */ ! 129: if ( ( rc = storef_named_setting ( name, value ) ) != 0 ) { ! 130: printf ( "Could not %s \"%s\": %s\n", ! 131: argv[0], name, strerror ( rc ) ); ! 132: goto err_store; ! 133: } ! 134: ! 135: free ( value ); ! 136: return 0; ! 137: ! 138: err_store: ! 139: free ( value ); ! 140: err_get_value: ! 141: err_parse_options: ! 142: return rc; ! 143: } ! 144: ! 145: /** ! 146: * Get setting value for "set" command ! 147: * ! 148: * @v args Remaining arguments ! 149: * @ret value Setting value ! 150: * @ret rc Return status code ! 151: */ ! 152: static int set_value ( char **args, char **value ) { ! 153: ! 154: *value = concat_args ( args ); ! 155: if ( ! *value ) ! 156: return -ENOMEM; ! 157: ! 158: return 0; ! 159: } ! 160: ! 161: /** ! 162: * "set" command ! 163: * ! 164: * @v argc Argument count ! 165: * @v argv Argument list ! 166: * @ret rc Return status code ! 167: */ ! 168: static int set_exec ( int argc, char **argv ) { ! 169: return set_core_exec ( argc, argv, &set_cmd, set_value ); ! 170: } ! 171: ! 172: /** ! 173: * Get setting value for "clear" command ! 174: * ! 175: * @v args Remaining arguments ! 176: * @ret value Setting value ! 177: * @ret rc Return status code ! 178: */ ! 179: static int clear_value ( char **args __unused, char **value ) { ! 180: ! 181: *value = NULL; ! 182: return 0; ! 183: } ! 184: ! 185: /** ! 186: * "clear" command ! 187: * ! 188: * @v argc Argument count ! 189: * @v argv Argument list ! 190: * @ret rc Return status code ! 191: */ ! 192: static int clear_exec ( int argc, char **argv ) { ! 193: return set_core_exec ( argc, argv, &clear_read_cmd, clear_value ); ! 194: } ! 195: ! 196: /** ! 197: * Get setting value for "read" command ! 198: * ! 199: * @ret value Setting value ! 200: * @ret rc Return status code ! 201: */ ! 202: static int read_value ( char **args __unused, char **value ) { ! 203: ! 204: *value = readline ( NULL ); ! 205: if ( ! *value ) ! 206: return -ENOMEM; ! 207: ! 208: return 0; ! 209: } ! 210: ! 211: /** ! 212: * "read" command ! 213: * ! 214: * @v argc Argument count ! 215: * @v argv Argument list ! 216: * @ret rc Return status code ! 217: */ ! 218: static int read_exec ( int argc, char **argv ) { ! 219: return set_core_exec ( argc, argv, &clear_read_cmd, read_value ); ! 220: } ! 221: ! 222: /** Non-volatile option commands */ ! 223: struct command nvo_commands[] __command = { ! 224: { ! 225: .name = "show", ! 226: .exec = show_exec, ! 227: }, ! 228: { ! 229: .name = "set", ! 230: .exec = set_exec, ! 231: }, ! 232: { ! 233: .name = "clear", ! 234: .exec = clear_exec, ! 235: }, ! 236: { ! 237: .name = "read", ! 238: .exec = read_exec, ! 239: }, ! 240: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.