|
@@ -0,0 +1,165 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (C) 2013 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., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
17
|
+ * 02110-1301, USA.
|
|
18
|
+ */
|
|
19
|
+
|
|
20
|
+FILE_LICENCE ( GPL2_OR_LATER );
|
|
21
|
+
|
|
22
|
+/** @file
|
|
23
|
+ *
|
|
24
|
+ * Form parameters
|
|
25
|
+ *
|
|
26
|
+ */
|
|
27
|
+
|
|
28
|
+#include <stdlib.h>
|
|
29
|
+#include <string.h>
|
|
30
|
+#include <ipxe/params.h>
|
|
31
|
+
|
|
32
|
+/** List of all parameter lists */
|
|
33
|
+static LIST_HEAD ( parameters );
|
|
34
|
+
|
|
35
|
+/**
|
|
36
|
+ * Find form parameter list by name
|
|
37
|
+ *
|
|
38
|
+ * @v name Parameter list name (may be NULL)
|
|
39
|
+ * @ret params Parameter list, or NULL if not found
|
|
40
|
+ */
|
|
41
|
+struct parameters * find_parameters ( const char *name ) {
|
|
42
|
+ struct parameters *params;
|
|
43
|
+
|
|
44
|
+ list_for_each_entry ( params, ¶meters, list ) {
|
|
45
|
+ if ( ( params->name == name ) ||
|
|
46
|
+ ( strcmp ( params->name, name ) == 0 ) ) {
|
|
47
|
+ return params;
|
|
48
|
+ }
|
|
49
|
+ }
|
|
50
|
+ return NULL;
|
|
51
|
+}
|
|
52
|
+
|
|
53
|
+/**
|
|
54
|
+ * Create form parameter list
|
|
55
|
+ *
|
|
56
|
+ * @v name Parameter list name (may be NULL)
|
|
57
|
+ * @ret params Parameter list, or NULL on failure
|
|
58
|
+ */
|
|
59
|
+struct parameters * create_parameters ( const char *name ) {
|
|
60
|
+ struct parameters *params;
|
|
61
|
+ size_t name_len;
|
|
62
|
+ char *name_copy;
|
|
63
|
+
|
|
64
|
+ /* Destroy any existing parameter list of this name */
|
|
65
|
+ params = find_parameters ( name );
|
|
66
|
+ if ( params )
|
|
67
|
+ destroy_parameters ( params );
|
|
68
|
+
|
|
69
|
+ /* Allocate parameter list */
|
|
70
|
+ name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
|
|
71
|
+ params = zalloc ( sizeof ( *params ) + name_len );
|
|
72
|
+ if ( ! params )
|
|
73
|
+ return NULL;
|
|
74
|
+ name_copy = ( ( void * ) ( params + 1 ) );
|
|
75
|
+
|
|
76
|
+ /* Populate parameter list */
|
|
77
|
+ if ( name ) {
|
|
78
|
+ strcpy ( name_copy, name );
|
|
79
|
+ params->name = name_copy;
|
|
80
|
+ }
|
|
81
|
+ INIT_LIST_HEAD ( ¶ms->entries );
|
|
82
|
+
|
|
83
|
+ /* Add to list of parameter lists */
|
|
84
|
+ list_add_tail ( ¶ms->list, ¶meters );
|
|
85
|
+
|
|
86
|
+ DBGC ( params, "PARAMS \"%s\" created\n", params->name );
|
|
87
|
+ return params;
|
|
88
|
+}
|
|
89
|
+
|
|
90
|
+/**
|
|
91
|
+ * Add form parameter
|
|
92
|
+ *
|
|
93
|
+ * @v params Parameter list
|
|
94
|
+ * @v key Parameter key
|
|
95
|
+ * @v value Parameter value
|
|
96
|
+ * @ret param Parameter, or NULL on failure
|
|
97
|
+ */
|
|
98
|
+struct parameter * add_parameter ( struct parameters *params,
|
|
99
|
+ const char *key, const char *value ) {
|
|
100
|
+ struct parameter *param;
|
|
101
|
+ size_t key_len;
|
|
102
|
+ size_t value_len;
|
|
103
|
+ char *key_copy;
|
|
104
|
+ char *value_copy;
|
|
105
|
+
|
|
106
|
+ /* Allocate parameter */
|
|
107
|
+ key_len = ( strlen ( key ) + 1 /* NUL */ );
|
|
108
|
+ value_len = ( strlen ( value ) + 1 /* NUL */ );
|
|
109
|
+ param = zalloc ( sizeof ( *param ) + key_len + value_len );
|
|
110
|
+ if ( ! param )
|
|
111
|
+ return NULL;
|
|
112
|
+ key_copy = ( ( void * ) ( param + 1 ) );
|
|
113
|
+ value_copy = ( key_copy + key_len );
|
|
114
|
+
|
|
115
|
+ /* Populate parameter */
|
|
116
|
+ strcpy ( key_copy, key );
|
|
117
|
+ param->key = key_copy;
|
|
118
|
+ strcpy ( value_copy, value );
|
|
119
|
+ param->value = value_copy;
|
|
120
|
+
|
|
121
|
+ /* Add to list of parameters */
|
|
122
|
+ list_add_tail ( ¶m->list, ¶ms->entries );
|
|
123
|
+
|
|
124
|
+ DBGC ( params, "PARAMS \"%s\" added \"%s\"=\"%s\"\n",
|
|
125
|
+ params->name, param->key, param->value );
|
|
126
|
+ return param;
|
|
127
|
+}
|
|
128
|
+
|
|
129
|
+/**
|
|
130
|
+ * Destroy form parameter list
|
|
131
|
+ *
|
|
132
|
+ * @v params Parameter list
|
|
133
|
+ */
|
|
134
|
+void destroy_parameters ( struct parameters *params ) {
|
|
135
|
+ struct parameter *param;
|
|
136
|
+ struct parameter *tmp;
|
|
137
|
+
|
|
138
|
+ DBGC ( params, "PARAMS \"%s\" destroyed\n", params->name );
|
|
139
|
+
|
|
140
|
+ /* Free all parameters */
|
|
141
|
+ list_for_each_entry_safe ( param, tmp, ¶ms->entries, list ) {
|
|
142
|
+ list_del ( ¶m->list );
|
|
143
|
+ free ( param );
|
|
144
|
+ }
|
|
145
|
+
|
|
146
|
+ /* Free parameter list */
|
|
147
|
+ list_del ( ¶ms->list );
|
|
148
|
+ free ( params );
|
|
149
|
+}
|
|
150
|
+
|
|
151
|
+/**
|
|
152
|
+ * Claim ownership of form parameter list
|
|
153
|
+ *
|
|
154
|
+ * @v params Parameter list
|
|
155
|
+ */
|
|
156
|
+void claim_parameters ( struct parameters *params ) {
|
|
157
|
+
|
|
158
|
+ DBGC ( params, "PARAMS \"%s\" claimed\n", params->name );
|
|
159
|
+
|
|
160
|
+ /* Remove from list of parameter lists */
|
|
161
|
+ list_del ( ¶ms->list );
|
|
162
|
+
|
|
163
|
+ /* Reinitialise list to allow for subsequent destroy_parameters() */
|
|
164
|
+ INIT_LIST_HEAD ( ¶ms->list );
|
|
165
|
+}
|