1 const char list_rcs[] = "$Id: list.c,v 1.15 2002/03/26 22:29:55 swa Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/list.c,v $
6 * Purpose : Declares functions to handle lists.
7 * Functions declared include:
8 * `destroy_list', `enlist' and `list_to_text'
10 * Copyright : Written by and Copyright (C) 2001 the SourceForge
11 * Privoxy team. http://www.privoxy.org/
13 * Based on the Internet Junkbuster originally written
14 * by and Copyright (C) 1997 Anonymous Coders and
15 * Junkbusters Corporation. http://www.junkbusters.com
17 * This program is free software; you can redistribute it
18 * and/or modify it under the terms of the GNU General
19 * Public License as published by the Free Software
20 * Foundation; either version 2 of the License, or (at
21 * your option) any later version.
23 * This program is distributed in the hope that it will
24 * be useful, but WITHOUT ANY WARRANTY; without even the
25 * implied warranty of MERCHANTABILITY or FITNESS FOR A
26 * PARTICULAR PURPOSE. See the GNU General Public
27 * License for more details.
29 * The GNU General Public License should be included with
30 * this file. If not, you can view it at
31 * http://www.gnu.org/copyleft/gpl.html
32 * or write to the Free Software Foundation, Inc., 59
33 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 * Revision 1.15 2002/03/26 22:29:55 swa
38 * we have a new homepage!
40 * Revision 1.14 2002/03/24 13:25:43 swa
41 * name change related issues
43 * Revision 1.13 2002/03/07 03:46:17 oes
44 * Fixed compiler warnings
46 * Revision 1.12 2001/10/25 03:40:48 david__schmidt
47 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
48 * threads to call select() simultaneously. So, it's time to do a real, live,
49 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
50 * (native). Both versions will work, but using __OS2__ offers multi-threading.
52 * Revision 1.11 2001/10/23 21:21:03 jongfoster
53 * New error handling - error codes are now jb_errs, not ints.
54 * Changed the way map() handles out-of-memory, to dramatically
55 * reduce the amount of error-checking clutter needed.
57 * Revision 1.10 2001/09/16 17:30:24 jongfoster
58 * Fixing a compiler warning.
60 * Revision 1.9 2001/09/16 13:20:29 jongfoster
61 * Rewrite of list library. Now has seperate header and list_entry
62 * structures. Also added a large sprinking of assert()s to the list
65 * Revision 1.8 2001/08/07 14:00:20 oes
68 * Revision 1.7 2001/08/05 16:06:20 jongfoster
69 * Modifiying "struct map" so that there are now separate header and
70 * "map_entry" structures. This means that functions which modify a
71 * map no longer need to return a pointer to the modified map.
72 * Also, it no longer reverses the order of the entries (which may be
73 * important with some advanced template substitutions).
75 * Revision 1.6 2001/07/31 14:44:51 oes
76 * list_to_text() now appends empty line at end
78 * Revision 1.5 2001/06/29 21:45:41 oes
79 * Indentation, CRLF->LF, Tab-> Space
81 * Revision 1.4 2001/06/29 13:30:22 oes
82 * - Added Convenience function enlist_unique_header(),
83 * which takes the Header name and value as separate
84 * arguments and thus saves the pain of sprintf()ing
85 * and determining the Header name length to enlist_unique
87 * - Removed logentry from cancelled commit
89 * Revision 1.3 2001/06/03 19:12:24 oes
90 * functions for new struct map, extended enlist_unique
92 * Revision 1.2 2001/06/01 18:49:17 jongfoster
93 * Replaced "list_share" with "list" - the tiny memory gain was not
94 * worth the extra complexity.
96 * Revision 1.1 2001/05/31 21:11:53 jongfoster
97 * - Moved linked list support to new "list.c" file.
98 * Structure definitions are still in project.h,
99 * function prototypes are now in "list.h".
100 * - Added support for "struct list_share", which is identical
101 * to "struct list" except it saves memory by not duplicating
102 * the strings. Obviously, this only works if there is some
103 * other way of managing the memory used by the strings.
104 * (These list_share lists are used for lists which last
105 * for only 1 request, and where all the list entries are
106 * just coming directly from entries in the actionsfile.)
107 * Note that you still need to destroy list_share lists
108 * properly to free the nodes - it's only the strings
112 *********************************************************************/
118 /* FIXME: The following headers are not needed for Win32. Are they
119 * needed on other platforms?
122 #include <sys/types.h>
128 #if !defined(_WIN32) && !defined(__OS2__)
136 #include "miscutil.h"
138 const char list_h_rcs[] = LIST_H_VERSION;
141 static int list_is_valid (const struct list *the_list);
144 /*********************************************************************
146 * Function : list_init
148 * Description : Create a new, empty list in user-allocated memory.
149 * Caller should allocate a "struct list" variable,
150 * then pass it to this function.
151 * (Implementation note: Rather than calling this
152 * function, you can also just memset the memory to
153 * zero, e.g. if you have a larger structure you
154 * want to initialize quickly. However, that isn't
155 * really good design.)
158 * 1 : the_list = pointer to list
162 *********************************************************************/
163 void init_list(struct list *the_list)
165 memset(the_list, '\0', sizeof(*the_list));
169 /*********************************************************************
171 * Function : destroy_list
173 * Description : Destroy a string list (opposite of list_init).
174 * On return, the memory used by the list entries has
175 * been freed, but not the memory used by the_list
176 * itself. You should not re-use the_list without
177 * calling list_init().
179 * (Implementation note: You *can* reuse the_list
180 * without calling list_init(), but please don't.
181 * If you want to remove all entries from a list
182 * and still have a usable list, then use
183 * list_remove_all().)
186 * 1 : the_list = pointer to list
190 *********************************************************************/
191 void destroy_list (struct list *the_list)
193 struct list_entry *cur_entry, *next_entry;
197 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
199 next_entry = cur_entry->next;
200 freez(cur_entry->str);
204 the_list->first = NULL;
205 the_list->last = NULL;
209 /*********************************************************************
211 * Function : list_is_valid
213 * Description : Check that a string list is valid. The intended
214 * usage is "assert(list_is_valid(the_list))".
215 * Currently this checks that "the_list->last"
216 * is correct, and that the list dosn't contain
217 * circular references. It is likely to crash if
218 * it's passed complete garbage.
221 * 1 : the_list = pointer to list. Must be non-null.
223 * Returns : 1 if list is valid, 0 otherwise.
225 *********************************************************************/
226 static int list_is_valid (const struct list *the_list)
229 * If you don't want this check, just change the line below
230 * from "#if 1" to "#if 0".
233 const struct list_entry *cur_entry;
234 const struct list_entry *last_entry = NULL;
239 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
241 last_entry = cur_entry;
246 * Just check that this string can be accessed - i.e. it's a valid
249 strlen(cur_entry->str);
253 * Check for looping back to first
255 if ((length != 0) && (cur_entry == the_list->first))
261 * Arbitrarily limit length to prevent infinite loops.
269 * Check this isn't marked as the last entry, unless of course it's
270 * *really* the last entry.
272 if ((the_list->last == cur_entry) && (cur_entry->next != NULL))
274 /* This is the last entry, but there's data after it !!?? */
279 return (the_list->last == last_entry);
285 /*********************************************************************
289 * Description : Append a string into a specified string list.
292 * 1 : the_list = pointer to list
293 * 2 : str = string to add to the list (maybe NULL)
295 * Returns : JB_ERR_OK on success
296 * JB_ERR_MEMORY on out-of-memory error.
297 * On error, the_list will be unchanged.
299 *********************************************************************/
300 jb_err enlist(struct list *the_list, const char *str)
302 struct list_entry *cur;
305 assert(list_is_valid(the_list));
307 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
309 return JB_ERR_MEMORY;
314 if (NULL == (cur->str = strdup(str)))
317 return JB_ERR_MEMORY;
320 /* else { cur->str = NULL; } - implied by zalloc */
322 /* cur->next = NULL; - implied by zalloc */
326 the_list->last->next = cur;
327 the_list->last = cur;
331 the_list->first = cur;
332 the_list->last = cur;
335 assert(list_is_valid(the_list));
340 /*********************************************************************
342 * Function : enlist_first
344 * Description : Append a string as first element into a specified
348 * 1 : the_list = pointer to list
349 * 2 : str = string to add to the list (maybe NULL)
351 * Returns : JB_ERR_OK on success
352 * JB_ERR_MEMORY on out-of-memory error.
353 * On error, the_list will be unchanged.
355 *********************************************************************/
356 jb_err enlist_first(struct list *the_list, const char *str)
358 struct list_entry *cur;
361 assert(list_is_valid(the_list));
363 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
365 return JB_ERR_MEMORY;
370 if (NULL == (cur->str = strdup(str)))
373 return JB_ERR_MEMORY;
376 /* else { cur->str = NULL; } - implied by zalloc */
378 cur->next = the_list->first;
380 the_list->first = cur;
381 if (the_list->last == NULL)
383 the_list->last = cur;
386 assert(list_is_valid(the_list));
391 /*********************************************************************
393 * Function : enlist_unique
395 * Description : Append a string into a specified string list,
396 * if & only if it's not there already.
397 * If the num_significant_chars argument is nonzero,
398 * only compare up to the nth character.
401 * 1 : the_list = pointer to list
402 * 2 : str = string to add to the list
403 * 3 : num_significant_chars = number of chars to use
404 * for uniqueness test, or 0 to require an exact match.
406 * Returns : JB_ERR_OK on success
407 * JB_ERR_MEMORY on out-of-memory error.
408 * On error, the_list will be unchanged.
409 * "Success" does not indicate whether or not the
410 * item was already in the list.
412 *********************************************************************/
413 jb_err enlist_unique(struct list *the_list, const char *str,
414 size_t num_significant_chars)
416 struct list_entry *cur_entry;
419 assert(list_is_valid(the_list));
421 assert(num_significant_chars >= 0);
422 assert(num_significant_chars <= strlen(str));
424 if (num_significant_chars > 0)
426 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
428 if ( (cur_entry->str != NULL)
429 && (0 == strncmp(str, cur_entry->str, num_significant_chars)))
438 /* Test whole string */
439 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
441 if ( (cur_entry->str != NULL) && (0 == strcmp(str, cur_entry->str)))
449 return enlist(the_list, str);
453 /*********************************************************************
455 * Function : enlist_unique_header
457 * Description : Make a HTTP header from the two strings name and value,
458 * and append the result into a specified string list,
459 * if & only if there isn't already a header with that name.
462 * 1 : the_list = pointer to list
463 * 2 : name = HTTP header name (e.g. "Content-type")
464 * 3 : value = HTTP header value (e.g. "text/html")
466 * Returns : JB_ERR_OK on success
467 * JB_ERR_MEMORY on out-of-memory error.
468 * On error, the_list will be unchanged.
469 * "Success" does not indicate whether or not the
470 * header was already in the list.
472 *********************************************************************/
473 jb_err enlist_unique_header(struct list *the_list, const char *name,
481 assert(list_is_valid(the_list));
485 length = strlen(name) + 2;
486 if (NULL == (str = (char *)malloc(length + strlen(value) + 1)))
488 return JB_ERR_MEMORY;
491 str[length - 2] = ':';
492 str[length - 1] = ' ';
493 strcpy(str + length, value);
495 result = enlist_unique(the_list, str, length);
499 assert(list_is_valid(the_list));
506 /*********************************************************************
508 * Function : list_remove_all
510 * Description : Remove all entries from a list. On return, the_list
511 * is a valid, empty list. Note that this is similar
512 * to destroy_list(), but the difference is that this
513 * function guarantees that the list structure is still
514 * valid after the call.
517 * 1 : the_list = pointer to list
521 *********************************************************************/
522 void list_remove_all(struct list *the_list)
524 struct list_entry *cur_entry;
525 struct list_entry *next_entry;
528 assert(list_is_valid(the_list));
530 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
532 next_entry = cur_entry->next;
533 freez(cur_entry->str);
537 the_list->first = the_list->last = NULL;
539 assert(list_is_valid(the_list));
543 /*********************************************************************
545 * Function : list_to_text
547 * Description : "Flatten" a string list into 1 long \r\n delimited string,
548 * adding an empty line at the end. NULL entries are ignored.
549 * This function does not change the_list.
552 * 1 : the_list = pointer to list
554 * Returns : NULL on malloc error, else new long string.
555 * Caller must free() it.
557 *********************************************************************/
558 char *list_to_text(const struct list *the_list)
560 struct list_entry *cur_entry;
566 assert(list_is_valid(the_list));
568 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
572 size += strlen(cur_entry->str) + 2;
576 if ((ret = (char *)malloc(size + 1)) == NULL)
585 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
589 strcpy(s, cur_entry->str);
591 *s++ = '\r'; *s++ = '\n';
594 *s++ = '\r'; *s++ = '\n';
600 /*********************************************************************
602 * Function : list_remove_item
604 * Description : Remove a string from a specified string list.
607 * 1 : the_list = pointer to list
608 * 2 : str = string to remove from the list - non-NULL
610 * Returns : Number of times it was removed.
612 *********************************************************************/
613 int list_remove_item(struct list *the_list, const char *str)
615 struct list_entry *prev = NULL;
616 struct list_entry *cur;
617 struct list_entry *next;
621 assert(list_is_valid(the_list));
624 cur = the_list->first;
630 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
640 the_list->first = next;
642 free((char *)cur->str);
652 the_list->last = prev;
654 assert(list_is_valid(the_list));
660 /*********************************************************************
662 * Function : list_remove_list
664 * Description : Remove all strings in one list from another list.
665 * This is currently a brute-force algorithm
666 * (it compares every pair of strings).
669 * 1 : dest = list to change
670 * 2 : src = list of strings to remove
672 * Returns : Total number of strings removed.
674 *********************************************************************/
675 int list_remove_list(struct list *dest, const struct list *src)
677 struct list_entry *cur;
682 assert(list_is_valid(src));
683 assert(list_is_valid(dest));
685 for (cur = src->first; cur != NULL; cur = cur->next)
687 if (cur->str != NULL)
689 count += list_remove_item(dest, cur->str);
693 assert(list_is_valid(src));
694 assert(list_is_valid(dest));
700 /*********************************************************************
702 * Function : list_duplicate
704 * Description : Copy a string list
707 * 1 : dest = Destination list. Must be a valid list.
708 * All existing entries will be removed.
709 * 1 : src = pointer to source list for copy.
711 * Returns : JB_ERR_OK on success
712 * JB_ERR_MEMORY on out-of-memory error.
713 * On error, dest will be empty.
715 *********************************************************************/
716 jb_err list_duplicate(struct list *dest, const struct list *src)
718 struct list_entry * cur_src;
719 struct list_entry * cur_dest;
723 assert(list_is_valid(src));
724 assert(list_is_valid(dest));
726 list_remove_all(dest);
728 /* Need to process first entry specially so we can set dest->first */
729 cur_src = src->first;
732 cur_dest = dest->first = (struct list_entry *)zalloc(sizeof(*cur_dest));
733 if (cur_dest == NULL)
737 assert(list_is_valid(src));
738 assert(list_is_valid(dest));
740 return JB_ERR_MEMORY;
745 cur_dest->str = strdup(cur_src->str);
746 if (cur_dest->str == NULL)
750 assert(list_is_valid(src));
751 assert(list_is_valid(dest));
753 return JB_ERR_MEMORY;
756 /* else { cur_dest->str = NULL; } - implied by zalloc */
758 /* Now process the rest */
759 for (cur_src = cur_src->next; cur_src; cur_src = cur_src->next)
761 cur_dest = cur_dest->next = (struct list_entry *)zalloc(sizeof(*cur_dest));
762 if (cur_dest == NULL)
766 assert(list_is_valid(src));
767 assert(list_is_valid(dest));
769 return JB_ERR_MEMORY;
773 cur_dest->str = strdup(cur_src->str);
774 if (cur_dest->str == NULL)
778 assert(list_is_valid(src));
779 assert(list_is_valid(dest));
781 return JB_ERR_MEMORY;
784 /* else { cur_dest->str = NULL; } - implied by zalloc */
787 dest->last = cur_dest;
790 assert(list_is_valid(src));
791 assert(list_is_valid(dest));
797 /*********************************************************************
799 * Function : list_append_list_unique
801 * Description : Append a string list to another list.
802 * Duplicate items are not added.
805 * 1 : dest = pointer to destination list for merge.
806 * 2 : src = pointer to source for merge.
808 * Returns : JB_ERR_OK on success
809 * JB_ERR_MEMORY on out-of-memory error.
810 * On error, some (but not all) of src might have
811 * been copied into dest.
813 *********************************************************************/
814 jb_err list_append_list_unique(struct list *dest, const struct list *src)
816 struct list_entry * cur;
820 assert(list_is_valid(src));
821 assert(list_is_valid(dest));
823 for (cur = src->first; cur; cur = cur->next)
827 if (enlist_unique(dest, cur->str, 0))
829 assert(list_is_valid(src));
830 assert(list_is_valid(dest));
832 return JB_ERR_MEMORY;
837 assert(list_is_valid(src));
838 assert(list_is_valid(dest));
844 /*********************************************************************
846 * Function : list_is_empty
848 * Description : Test whether a list is empty. Does not change the list.
851 * 1 : the_list = pointer to list to test.
853 * Returns : Nonzero iff the list contains no entries.
855 *********************************************************************/
856 int list_is_empty(const struct list *the_list)
859 assert(list_is_valid(the_list));
861 return (the_list->first == NULL);
865 /*********************************************************************
869 * Description : Create a new, empty map.
873 * Returns : A new, empty map, or NULL if out of memory.
875 *********************************************************************/
876 struct map *new_map(void)
878 return (struct map *) zalloc(sizeof(struct map));
882 /*********************************************************************
884 * Function : free_map
886 * Description : Free the memory occupied by a map and its
890 * 1 : the_map = map to be freed. May be NULL.
894 *********************************************************************/
895 void free_map(struct map *the_map)
897 struct map_entry *cur_entry;
898 struct map_entry *next_entry;
905 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = next_entry)
907 freez(cur_entry->name);
908 freez(cur_entry->value);
910 next_entry = cur_entry->next;
914 the_map->first = the_map->last = NULL;
920 /*********************************************************************
924 * Description : Add a mapping from given name to given value to a
927 * Note: Since all strings will be free()d in free_map()
928 * later, set the copy flags for constants or
929 * strings that will be independantly free()d.
931 * Note2: This function allows NULL parameters - it
932 * returns JB_ERR_MEMORY in that case.
934 * Note3: If this function returns JB_ERR_MEMORY,
935 * it will free(name) unless you specify
936 * name_needs_copying, and similarly it will
937 * free(value) unless you specify
938 * value_needs_copying.
940 * Due to Note2 and Note3 above, the following code
941 * is legal, and will never crash or leak memory even
942 * if the system runs out of memory:
944 * err = map(mymap, "xyz", 1, html_encode(somestring), 0);
946 * err will be set to JB_ERR_MEMORY if either call runs
947 * out-of-memory. Without these features, you would
948 * need to check the return value of html_encode in the
949 * above example for NULL, which (at least) doubles the
950 * amount of error-checking code needed.
953 * 1 : the_map = map to add to
954 * 2 : name = name to add
955 * 3 : name_needs_copying = flag set if a copy of name should be used
956 * 4 : value = value to add
957 * 5 : value_needs_copying = flag set if a copy of value should be used
959 * Returns : JB_ERR_OK on success
960 * JB_ERR_MEMORY on out-of-memory error.
962 *********************************************************************/
963 jb_err map(struct map *the_map,
964 const char *name, int name_needs_copying,
965 const char *value, int value_needs_copying)
967 struct map_entry *new_entry;
973 || (NULL == (new_entry = zalloc(sizeof(*new_entry)))) )
975 if ((name != NULL) && (!name_needs_copying))
979 if ((value != NULL) && (!value_needs_copying))
983 return JB_ERR_MEMORY;
986 if (name_needs_copying)
988 if (NULL == (name = strdup(name)))
991 if (!value_needs_copying)
995 return JB_ERR_MEMORY;
999 if (value_needs_copying)
1001 if (NULL == (value = strdup(value)))
1005 return JB_ERR_MEMORY;
1009 new_entry->name = name;
1010 new_entry->value = value;
1011 /* new_entry->next = NULL; - implied by zalloc */
1015 the_map->last->next = new_entry;
1016 the_map->last = new_entry;
1020 the_map->first = new_entry;
1021 the_map->last = new_entry;
1028 /*********************************************************************
1032 * Description : Look up an item with a given name in a map, and
1036 * 1 : the_map = map to look in
1037 * 2 : name = name parameter to look for
1039 * Returns : the value if found, else the empty string.
1040 * Return value is alloced as part of the map, so
1041 * it is freed when the map is destroyed. Caller
1042 * must not free or modify it.
1044 *********************************************************************/
1045 const char *lookup(const struct map *the_map, const char *name)
1047 const struct map_entry *cur_entry;
1052 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = cur_entry->next)
1054 if (!strcmp(name, cur_entry->name))
1056 return cur_entry->value;