You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * 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.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Form parameters
  27. *
  28. */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <ipxe/params.h>
  32. /** List of all parameter lists */
  33. static LIST_HEAD ( parameters );
  34. /**
  35. * Free form parameter list
  36. *
  37. * @v refcnt Reference count
  38. */
  39. static void free_parameters ( struct refcnt *refcnt ) {
  40. struct parameters *params =
  41. container_of ( refcnt, struct parameters, refcnt );
  42. struct parameter *param;
  43. struct parameter *tmp;
  44. DBGC ( params, "PARAMS \"%s\" destroyed\n", params->name );
  45. /* Free all parameters */
  46. list_for_each_entry_safe ( param, tmp, &params->entries, list ) {
  47. list_del ( &param->list );
  48. free ( param );
  49. }
  50. /* Free parameter list */
  51. free ( params );
  52. }
  53. /**
  54. * Find form parameter list by name
  55. *
  56. * @v name Parameter list name (may be NULL)
  57. * @ret params Parameter list, or NULL if not found
  58. */
  59. struct parameters * find_parameters ( const char *name ) {
  60. struct parameters *params;
  61. list_for_each_entry ( params, &parameters, list ) {
  62. if ( ( params->name == name ) ||
  63. ( strcmp ( params->name, name ) == 0 ) ) {
  64. return params;
  65. }
  66. }
  67. return NULL;
  68. }
  69. /**
  70. * Create form parameter list
  71. *
  72. * @v name Parameter list name (may be NULL)
  73. * @ret params Parameter list, or NULL on failure
  74. */
  75. struct parameters * create_parameters ( const char *name ) {
  76. struct parameters *params;
  77. size_t name_len;
  78. char *name_copy;
  79. /* Destroy any existing parameter list of this name */
  80. params = find_parameters ( name );
  81. if ( params ) {
  82. claim_parameters ( params );
  83. params_put ( params );
  84. }
  85. /* Allocate parameter list */
  86. name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
  87. params = zalloc ( sizeof ( *params ) + name_len );
  88. if ( ! params )
  89. return NULL;
  90. ref_init ( &params->refcnt, free_parameters );
  91. name_copy = ( ( void * ) ( params + 1 ) );
  92. /* Populate parameter list */
  93. if ( name ) {
  94. strcpy ( name_copy, name );
  95. params->name = name_copy;
  96. }
  97. INIT_LIST_HEAD ( &params->entries );
  98. /* Add to list of parameter lists */
  99. list_add_tail ( &params->list, &parameters );
  100. DBGC ( params, "PARAMS \"%s\" created\n", params->name );
  101. return params;
  102. }
  103. /**
  104. * Add form parameter
  105. *
  106. * @v params Parameter list
  107. * @v key Parameter key
  108. * @v value Parameter value
  109. * @ret param Parameter, or NULL on failure
  110. */
  111. struct parameter * add_parameter ( struct parameters *params,
  112. const char *key, const char *value ) {
  113. struct parameter *param;
  114. size_t key_len;
  115. size_t value_len;
  116. char *key_copy;
  117. char *value_copy;
  118. /* Allocate parameter */
  119. key_len = ( strlen ( key ) + 1 /* NUL */ );
  120. value_len = ( strlen ( value ) + 1 /* NUL */ );
  121. param = zalloc ( sizeof ( *param ) + key_len + value_len );
  122. if ( ! param )
  123. return NULL;
  124. key_copy = ( ( void * ) ( param + 1 ) );
  125. value_copy = ( key_copy + key_len );
  126. /* Populate parameter */
  127. strcpy ( key_copy, key );
  128. param->key = key_copy;
  129. strcpy ( value_copy, value );
  130. param->value = value_copy;
  131. /* Add to list of parameters */
  132. list_add_tail ( &param->list, &params->entries );
  133. DBGC ( params, "PARAMS \"%s\" added \"%s\"=\"%s\"\n",
  134. params->name, param->key, param->value );
  135. return param;
  136. }