mirror of
https://github.com/xcat2/xNBA.git
synced 2024-11-24 18:40:12 +00:00
[params] Add support for the general concept of a form parameter list
Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
parent
c692a690da
commit
564419c7e1
165
src/core/params.c
Normal file
165
src/core/params.c
Normal file
@ -0,0 +1,165 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
FILE_LICENCE ( GPL2_OR_LATER );
|
||||
|
||||
/** @file
|
||||
*
|
||||
* Form parameters
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ipxe/params.h>
|
||||
|
||||
/** List of all parameter lists */
|
||||
static LIST_HEAD ( parameters );
|
||||
|
||||
/**
|
||||
* Find form parameter list by name
|
||||
*
|
||||
* @v name Parameter list name (may be NULL)
|
||||
* @ret params Parameter list, or NULL if not found
|
||||
*/
|
||||
struct parameters * find_parameters ( const char *name ) {
|
||||
struct parameters *params;
|
||||
|
||||
list_for_each_entry ( params, ¶meters, list ) {
|
||||
if ( ( params->name == name ) ||
|
||||
( strcmp ( params->name, name ) == 0 ) ) {
|
||||
return params;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create form parameter list
|
||||
*
|
||||
* @v name Parameter list name (may be NULL)
|
||||
* @ret params Parameter list, or NULL on failure
|
||||
*/
|
||||
struct parameters * create_parameters ( const char *name ) {
|
||||
struct parameters *params;
|
||||
size_t name_len;
|
||||
char *name_copy;
|
||||
|
||||
/* Destroy any existing parameter list of this name */
|
||||
params = find_parameters ( name );
|
||||
if ( params )
|
||||
destroy_parameters ( params );
|
||||
|
||||
/* Allocate parameter list */
|
||||
name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
|
||||
params = zalloc ( sizeof ( *params ) + name_len );
|
||||
if ( ! params )
|
||||
return NULL;
|
||||
name_copy = ( ( void * ) ( params + 1 ) );
|
||||
|
||||
/* Populate parameter list */
|
||||
if ( name ) {
|
||||
strcpy ( name_copy, name );
|
||||
params->name = name_copy;
|
||||
}
|
||||
INIT_LIST_HEAD ( ¶ms->entries );
|
||||
|
||||
/* Add to list of parameter lists */
|
||||
list_add_tail ( ¶ms->list, ¶meters );
|
||||
|
||||
DBGC ( params, "PARAMS \"%s\" created\n", params->name );
|
||||
return params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add form parameter
|
||||
*
|
||||
* @v params Parameter list
|
||||
* @v key Parameter key
|
||||
* @v value Parameter value
|
||||
* @ret param Parameter, or NULL on failure
|
||||
*/
|
||||
struct parameter * add_parameter ( struct parameters *params,
|
||||
const char *key, const char *value ) {
|
||||
struct parameter *param;
|
||||
size_t key_len;
|
||||
size_t value_len;
|
||||
char *key_copy;
|
||||
char *value_copy;
|
||||
|
||||
/* Allocate parameter */
|
||||
key_len = ( strlen ( key ) + 1 /* NUL */ );
|
||||
value_len = ( strlen ( value ) + 1 /* NUL */ );
|
||||
param = zalloc ( sizeof ( *param ) + key_len + value_len );
|
||||
if ( ! param )
|
||||
return NULL;
|
||||
key_copy = ( ( void * ) ( param + 1 ) );
|
||||
value_copy = ( key_copy + key_len );
|
||||
|
||||
/* Populate parameter */
|
||||
strcpy ( key_copy, key );
|
||||
param->key = key_copy;
|
||||
strcpy ( value_copy, value );
|
||||
param->value = value_copy;
|
||||
|
||||
/* Add to list of parameters */
|
||||
list_add_tail ( ¶m->list, ¶ms->entries );
|
||||
|
||||
DBGC ( params, "PARAMS \"%s\" added \"%s\"=\"%s\"\n",
|
||||
params->name, param->key, param->value );
|
||||
return param;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy form parameter list
|
||||
*
|
||||
* @v params Parameter list
|
||||
*/
|
||||
void destroy_parameters ( struct parameters *params ) {
|
||||
struct parameter *param;
|
||||
struct parameter *tmp;
|
||||
|
||||
DBGC ( params, "PARAMS \"%s\" destroyed\n", params->name );
|
||||
|
||||
/* Free all parameters */
|
||||
list_for_each_entry_safe ( param, tmp, ¶ms->entries, list ) {
|
||||
list_del ( ¶m->list );
|
||||
free ( param );
|
||||
}
|
||||
|
||||
/* Free parameter list */
|
||||
list_del ( ¶ms->list );
|
||||
free ( params );
|
||||
}
|
||||
|
||||
/**
|
||||
* Claim ownership of form parameter list
|
||||
*
|
||||
* @v params Parameter list
|
||||
*/
|
||||
void claim_parameters ( struct parameters *params ) {
|
||||
|
||||
DBGC ( params, "PARAMS \"%s\" claimed\n", params->name );
|
||||
|
||||
/* Remove from list of parameter lists */
|
||||
list_del ( ¶ms->list );
|
||||
|
||||
/* Reinitialise list to allow for subsequent destroy_parameters() */
|
||||
INIT_LIST_HEAD ( ¶ms->list );
|
||||
}
|
@ -29,6 +29,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||
#include <ipxe/netdevice.h>
|
||||
#include <ipxe/menu.h>
|
||||
#include <ipxe/settings.h>
|
||||
#include <ipxe/params.h>
|
||||
#include <ipxe/parseopt.h>
|
||||
|
||||
/** @file
|
||||
@ -250,6 +251,29 @@ int parse_autovivified_setting ( char *text, struct named_setting *setting ) {
|
||||
return parse_setting ( text, setting, autovivify_child_settings );
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse form parameter list name
|
||||
*
|
||||
* @v text Text
|
||||
* @ret params Parameter list
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
int parse_parameters ( char *text, struct parameters **params ) {
|
||||
|
||||
/* Find parameter list */
|
||||
*params = find_parameters ( text );
|
||||
if ( ! *params ) {
|
||||
if ( text ) {
|
||||
printf ( "\"%s\": no such parameter list\n", text );
|
||||
} else {
|
||||
printf ( "No default parameter list\n" );
|
||||
}
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print command usage message
|
||||
*
|
||||
|
46
src/include/ipxe/params.h
Normal file
46
src/include/ipxe/params.h
Normal file
@ -0,0 +1,46 @@
|
||||
#ifndef _IPXE_PARAMS_H
|
||||
#define _IPXE_PARAMS_H
|
||||
|
||||
/** @file
|
||||
*
|
||||
* Form parameters
|
||||
*
|
||||
*/
|
||||
|
||||
FILE_LICENCE ( GPL2_OR_LATER );
|
||||
|
||||
#include <ipxe/list.h>
|
||||
#include <ipxe/refcnt.h>
|
||||
|
||||
/** A form parameter list */
|
||||
struct parameters {
|
||||
/** List of all parameter lists */
|
||||
struct list_head list;
|
||||
/** Name */
|
||||
const char *name;
|
||||
/** Parameters */
|
||||
struct list_head entries;
|
||||
};
|
||||
|
||||
/** A form parameter */
|
||||
struct parameter {
|
||||
/** List of form parameters */
|
||||
struct list_head list;
|
||||
/** Key */
|
||||
const char *key;
|
||||
/** Value */
|
||||
const char *value;
|
||||
};
|
||||
|
||||
/** Iterate over all form parameters in a list */
|
||||
#define for_each_param( param, params ) \
|
||||
list_for_each_entry ( (param), &(params)->entries, list )
|
||||
|
||||
extern struct parameters * find_parameters ( const char *name );
|
||||
extern struct parameters * create_parameters ( const char *name );
|
||||
extern struct parameter * add_parameter ( struct parameters *params,
|
||||
const char *key, const char *value );
|
||||
extern void destroy_parameters ( struct parameters *params );
|
||||
extern void claim_parameters ( struct parameters *params );
|
||||
|
||||
#endif /* _IPXE_PARAMS_H */
|
@ -15,6 +15,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||
|
||||
struct net_device;
|
||||
struct menu;
|
||||
struct parameters;
|
||||
|
||||
/** A command-line option descriptor */
|
||||
struct option_descriptor {
|
||||
@ -135,6 +136,7 @@ extern int parse_setting ( char *text, struct named_setting *setting,
|
||||
extern int parse_existing_setting ( char *text, struct named_setting *setting );
|
||||
extern int parse_autovivified_setting ( char *text,
|
||||
struct named_setting *setting );
|
||||
extern int parse_parameters ( char *text, struct parameters **params );
|
||||
extern void print_usage ( struct command_descriptor *cmd, char **argv );
|
||||
extern int reparse_options ( int argc, char **argv,
|
||||
struct command_descriptor *cmd, void *opts );
|
||||
|
Loading…
Reference in New Issue
Block a user