Преглед на файлове

[params] Add support for the general concept of a form parameter list

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown преди 11 години
родител
ревизия
564419c7e1
променени са 4 файла, в които са добавени 237 реда и са изтрити 0 реда
  1. 165
    0
      src/core/params.c
  2. 24
    0
      src/core/parseopt.c
  3. 46
    0
      src/include/ipxe/params.h
  4. 2
    0
      src/include/ipxe/parseopt.h

+ 165
- 0
src/core/params.c Целия файл

@@ -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, &parameters, 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 ( &params->entries );
82
+
83
+	/* Add to list of parameter lists */
84
+	list_add_tail ( &params->list, &parameters );
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 ( &param->list, &params->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, &params->entries, list ) {
142
+		list_del ( &param->list );
143
+		free ( param );
144
+	}
145
+
146
+	/* Free parameter list */
147
+	list_del ( &params->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 ( &params->list );
162
+
163
+	/* Reinitialise list to allow for subsequent destroy_parameters() */
164
+	INIT_LIST_HEAD ( &params->list );
165
+}

+ 24
- 0
src/core/parseopt.c Целия файл

@@ -29,6 +29,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
29 29
 #include <ipxe/netdevice.h>
30 30
 #include <ipxe/menu.h>
31 31
 #include <ipxe/settings.h>
32
+#include <ipxe/params.h>
32 33
 #include <ipxe/parseopt.h>
33 34
 
34 35
 /** @file
@@ -250,6 +251,29 @@ int parse_autovivified_setting ( char *text, struct named_setting *setting ) {
250 251
 	return parse_setting ( text, setting, autovivify_child_settings );
251 252
 }
252 253
 
254
+/**
255
+ * Parse form parameter list name
256
+ *
257
+ * @v text		Text
258
+ * @ret params		Parameter list
259
+ * @ret rc		Return status code
260
+ */
261
+int parse_parameters ( char *text, struct parameters **params ) {
262
+
263
+	/* Find parameter list */
264
+	*params = find_parameters ( text );
265
+	if ( ! *params ) {
266
+		if ( text ) {
267
+			printf ( "\"%s\": no such parameter list\n", text );
268
+		} else {
269
+			printf ( "No default parameter list\n" );
270
+		}
271
+		return -ENOENT;
272
+	}
273
+
274
+	return 0;
275
+}
276
+
253 277
 /**
254 278
  * Print command usage message
255 279
  *

+ 46
- 0
src/include/ipxe/params.h Целия файл

@@ -0,0 +1,46 @@
1
+#ifndef _IPXE_PARAMS_H
2
+#define _IPXE_PARAMS_H
3
+
4
+/** @file
5
+ *
6
+ * Form parameters
7
+ *
8
+ */
9
+
10
+FILE_LICENCE ( GPL2_OR_LATER );
11
+
12
+#include <ipxe/list.h>
13
+#include <ipxe/refcnt.h>
14
+
15
+/** A form parameter list */
16
+struct parameters {
17
+	/** List of all parameter lists */
18
+	struct list_head list;
19
+	/** Name */
20
+	const char *name;
21
+	/** Parameters */
22
+	struct list_head entries;
23
+};
24
+
25
+/** A form parameter */
26
+struct parameter {
27
+	/** List of form parameters */
28
+	struct list_head list;
29
+	/** Key */
30
+	const char *key;
31
+	/** Value */
32
+	const char *value;
33
+};
34
+
35
+/** Iterate over all form parameters in a list */
36
+#define for_each_param( param, params )				\
37
+	list_for_each_entry ( (param), &(params)->entries, list )
38
+
39
+extern struct parameters * find_parameters ( const char *name );
40
+extern struct parameters * create_parameters ( const char *name );
41
+extern struct parameter * add_parameter ( struct parameters *params,
42
+					  const char *key, const char *value );
43
+extern void destroy_parameters ( struct parameters *params );
44
+extern void claim_parameters ( struct parameters *params );
45
+
46
+#endif /* _IPXE_PARAMS_H */

+ 2
- 0
src/include/ipxe/parseopt.h Целия файл

@@ -15,6 +15,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
15 15
 
16 16
 struct net_device;
17 17
 struct menu;
18
+struct parameters;
18 19
 
19 20
 /** A command-line option descriptor */
20 21
 struct option_descriptor {
@@ -135,6 +136,7 @@ extern int parse_setting ( char *text, struct named_setting *setting,
135 136
 extern int parse_existing_setting ( char *text, struct named_setting *setting );
136 137
 extern int parse_autovivified_setting ( char *text,
137 138
 					struct named_setting *setting );
139
+extern int parse_parameters ( char *text, struct parameters **params );
138 140
 extern void print_usage ( struct command_descriptor *cmd, char **argv );
139 141
 extern int reparse_options ( int argc, char **argv,
140 142
 			     struct command_descriptor *cmd, void *opts );

Loading…
Отказ
Запис