iPXE
params.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * Form parameters
00029  *
00030  */
00031 
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <ipxe/params.h>
00035 
00036 /** List of all parameter lists */
00037 static LIST_HEAD ( parameters );
00038 
00039 /**
00040  * Free form parameter list
00041  *
00042  * @v refcnt            Reference count
00043  */
00044 static void free_parameters ( struct refcnt *refcnt ) {
00045         struct parameters *params =
00046                 container_of ( refcnt, struct parameters, refcnt );
00047         struct parameter *param;
00048         struct parameter *tmp;
00049 
00050         DBGC ( params, "PARAMS \"%s\" destroyed\n", params->name );
00051 
00052         /* Free all parameters */
00053         list_for_each_entry_safe ( param, tmp, &params->entries, list ) {
00054                 list_del ( &param->list );
00055                 free ( param );
00056         }
00057 
00058         /* Free parameter list */
00059         free ( params );
00060 }
00061 
00062 /**
00063  * Find form parameter list by name
00064  *
00065  * @v name              Parameter list name (may be NULL)
00066  * @ret params          Parameter list, or NULL if not found
00067  */
00068 struct parameters * find_parameters ( const char *name ) {
00069         struct parameters *params;
00070 
00071         list_for_each_entry ( params, &parameters, list ) {
00072                 if ( ( params->name == name ) ||
00073                      ( strcmp ( params->name, name ) == 0 ) ) {
00074                         return params;
00075                 }
00076         }
00077         return NULL;
00078 }
00079 
00080 /**
00081  * Create form parameter list
00082  *
00083  * @v name              Parameter list name (may be NULL)
00084  * @ret params          Parameter list, or NULL on failure
00085  */
00086 struct parameters * create_parameters ( const char *name ) {
00087         struct parameters *params;
00088         size_t name_len;
00089         char *name_copy;
00090 
00091         /* Destroy any existing parameter list of this name */
00092         params = find_parameters ( name );
00093         if ( params ) {
00094                 claim_parameters ( params );
00095                 params_put ( params );
00096         }
00097 
00098         /* Allocate parameter list */
00099         name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
00100         params = zalloc ( sizeof ( *params ) + name_len );
00101         if ( ! params )
00102                 return NULL;
00103         ref_init ( &params->refcnt, free_parameters );
00104         name_copy = ( ( void * ) ( params + 1 ) );
00105 
00106         /* Populate parameter list */
00107         if ( name ) {
00108                 strcpy ( name_copy, name );
00109                 params->name = name_copy;
00110         }
00111         INIT_LIST_HEAD ( &params->entries );
00112 
00113         /* Add to list of parameter lists */
00114         list_add_tail ( &params->list, &parameters );
00115 
00116         DBGC ( params, "PARAMS \"%s\" created\n", params->name );
00117         return params;
00118 }
00119 
00120 /**
00121  * Add form parameter
00122  *
00123  * @v params            Parameter list
00124  * @v key               Parameter key
00125  * @v value             Parameter value
00126  * @ret param           Parameter, or NULL on failure
00127  */
00128 struct parameter * add_parameter ( struct parameters *params,
00129                                    const char *key, const char *value ) {
00130         struct parameter *param;
00131         size_t key_len;
00132         size_t value_len;
00133         char *key_copy;
00134         char *value_copy;
00135 
00136         /* Allocate parameter */
00137         key_len = ( strlen ( key ) + 1 /* NUL */ );
00138         value_len = ( strlen ( value ) + 1 /* NUL */ );
00139         param = zalloc ( sizeof ( *param ) + key_len + value_len );
00140         if ( ! param )
00141                 return NULL;
00142         key_copy = ( ( void * ) ( param + 1 ) );
00143         value_copy = ( key_copy + key_len );
00144 
00145         /* Populate parameter */
00146         strcpy ( key_copy, key );
00147         param->key = key_copy;
00148         strcpy ( value_copy, value );
00149         param->value = value_copy;
00150 
00151         /* Add to list of parameters */
00152         list_add_tail ( &param->list, &params->entries );
00153 
00154         DBGC ( params, "PARAMS \"%s\" added \"%s\"=\"%s\"\n",
00155                params->name, param->key, param->value );
00156         return param;
00157 }