2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
34 #include <ipxe/params.h>
36 /** List of all parameter lists */
37 static LIST_HEAD ( parameters );
40 * Free form parameter list
42 * @v refcnt Reference count
44 static void free_parameters ( struct refcnt *refcnt ) {
45 struct parameters *params =
46 container_of ( refcnt, struct parameters, refcnt );
47 struct parameter *param;
48 struct parameter *tmp;
50 DBGC ( params, "PARAMS \"%s\" destroyed\n", params->name );
52 /* Free all parameters */
53 list_for_each_entry_safe ( param, tmp, ¶ms->entries, list ) {
54 list_del ( ¶m->list );
58 /* Free parameter list */
63 * Find form parameter list by name
65 * @v name Parameter list name (may be NULL)
66 * @ret params Parameter list, or NULL if not found
68 struct parameters * find_parameters ( const char *name ) {
69 struct parameters *params;
71 list_for_each_entry ( params, ¶meters, list ) {
72 if ( ( params->name == name ) ||
73 ( strcmp ( params->name, name ) == 0 ) ) {
81 * Create form parameter list
83 * @v name Parameter list name (may be NULL)
84 * @ret params Parameter list, or NULL on failure
86 struct parameters * create_parameters ( const char *name ) {
87 struct parameters *params;
91 /* Destroy any existing parameter list of this name */
92 params = find_parameters ( name );
94 claim_parameters ( params );
95 params_put ( params );
98 /* Allocate parameter list */
99 name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
100 params = zalloc ( sizeof ( *params ) + name_len );
103 ref_init ( ¶ms->refcnt, free_parameters );
104 name_copy = ( ( void * ) ( params + 1 ) );
106 /* Populate parameter list */
108 strcpy ( name_copy, name );
109 params->name = name_copy;
111 INIT_LIST_HEAD ( ¶ms->entries );
113 /* Add to list of parameter lists */
114 list_add_tail ( ¶ms->list, ¶meters );
116 DBGC ( params, "PARAMS \"%s\" created\n", params->name );
123 * @v params Parameter list
124 * @v key Parameter key
125 * @v value Parameter value
126 * @ret param Parameter, or NULL on failure
128 struct parameter * add_parameter ( struct parameters *params,
129 const char *key, const char *value ) {
130 struct parameter *param;
136 /* Allocate parameter */
137 key_len = ( strlen ( key ) + 1 /* NUL */ );
138 value_len = ( strlen ( value ) + 1 /* NUL */ );
139 param = zalloc ( sizeof ( *param ) + key_len + value_len );
142 key_copy = ( ( void * ) ( param + 1 ) );
143 value_copy = ( key_copy + key_len );
145 /* Populate parameter */
146 strcpy ( key_copy, key );
147 param->key = key_copy;
148 strcpy ( value_copy, value );
149 param->value = value_copy;
151 /* Add to list of parameters */
152 list_add_tail ( ¶m->list, ¶ms->entries );
154 DBGC ( params, "PARAMS \"%s\" added \"%s\"=\"%s\"\n",
155 params->name, param->key, param->value );