1 const char list_rcs[] = "$Id: list.c,v 2.0 2002/06/04 14:34:21 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/src/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 2.0 2002/06/04 14:34:21 jongfoster
38 * Moving source files to src/
40 * Revision 1.15 2002/03/26 22:29:55 swa
41 * we have a new homepage!
43 * Revision 1.14 2002/03/24 13:25:43 swa
44 * name change related issues
46 * Revision 1.13 2002/03/07 03:46:17 oes
47 * Fixed compiler warnings
49 * Revision 1.12 2001/10/25 03:40:48 david__schmidt
50 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
51 * threads to call select() simultaneously. So, it's time to do a real, live,
52 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
53 * (native). Both versions will work, but using __OS2__ offers multi-threading.
55 * Revision 1.11 2001/10/23 21:21:03 jongfoster
56 * New error handling - error codes are now jb_errs, not ints.
57 * Changed the way map() handles out-of-memory, to dramatically
58 * reduce the amount of error-checking clutter needed.
60 * Revision 1.10 2001/09/16 17:30:24 jongfoster
61 * Fixing a compiler warning.
63 * Revision 1.9 2001/09/16 13:20:29 jongfoster
64 * Rewrite of list library. Now has seperate header and list_entry
65 * structures. Also added a large sprinking of assert()s to the list
68 * Revision 1.8 2001/08/07 14:00:20 oes
71 * Revision 1.7 2001/08/05 16:06:20 jongfoster
72 * Modifiying "struct map" so that there are now separate header and
73 * "map_entry" structures. This means that functions which modify a
74 * map no longer need to return a pointer to the modified map.
75 * Also, it no longer reverses the order of the entries (which may be
76 * important with some advanced template substitutions).
78 * Revision 1.6 2001/07/31 14:44:51 oes
79 * list_to_text() now appends empty line at end
81 * Revision 1.5 2001/06/29 21:45:41 oes
82 * Indentation, CRLF->LF, Tab-> Space
84 * Revision 1.4 2001/06/29 13:30:22 oes
85 * - Added Convenience function enlist_unique_header(),
86 * which takes the Header name and value as separate
87 * arguments and thus saves the pain of sprintf()ing
88 * and determining the Header name length to enlist_unique
90 * - Removed logentry from cancelled commit
92 * Revision 1.3 2001/06/03 19:12:24 oes
93 * functions for new struct map, extended enlist_unique
95 * Revision 1.2 2001/06/01 18:49:17 jongfoster
96 * Replaced "list_share" with "list" - the tiny memory gain was not
97 * worth the extra complexity.
99 * Revision 1.1 2001/05/31 21:11:53 jongfoster
100 * - Moved linked list support to new "list.c" file.
101 * Structure definitions are still in project.h,
102 * function prototypes are now in "list.h".
103 * - Added support for "struct list_share", which is identical
104 * to "struct list" except it saves memory by not duplicating
105 * the strings. Obviously, this only works if there is some
106 * other way of managing the memory used by the strings.
107 * (These list_share lists are used for lists which last
108 * for only 1 request, and where all the list entries are
109 * just coming directly from entries in the actionsfile.)
110 * Note that you still need to destroy list_share lists
111 * properly to free the nodes - it's only the strings
115 *********************************************************************/
121 /* FIXME: The following headers are not needed for Win32. Are they
122 * needed on other platforms?
125 #include <sys/types.h>
131 #if !defined(_WIN32) && !defined(__OS2__)
139 #include "miscutil.h"
141 const char list_h_rcs[] = LIST_H_VERSION;
144 static int list_is_valid (const struct list *the_list);
147 /*********************************************************************
149 * Function : list_init
151 * Description : Create a new, empty list in user-allocated memory.
152 * Caller should allocate a "struct list" variable,
153 * then pass it to this function.
154 * (Implementation note: Rather than calling this
155 * function, you can also just memset the memory to
156 * zero, e.g. if you have a larger structure you
157 * want to initialize quickly. However, that isn't
158 * really good design.)
161 * 1 : the_list = pointer to list
165 *********************************************************************/
166 void init_list(struct list *the_list)
168 memset(the_list, '\0', sizeof(*the_list));
172 /*********************************************************************
174 * Function : destroy_list
176 * Description : Destroy a string list (opposite of list_init).
177 * On return, the memory used by the list entries has
178 * been freed, but not the memory used by the_list
179 * itself. You should not re-use the_list without
180 * calling list_init().
182 * (Implementation note: You *can* reuse the_list
183 * without calling list_init(), but please don't.
184 * If you want to remove all entries from a list
185 * and still have a usable list, then use
186 * list_remove_all().)
189 * 1 : the_list = pointer to list
193 *********************************************************************/
194 void destroy_list (struct list *the_list)
196 struct list_entry *cur_entry, *next_entry;
200 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
202 next_entry = cur_entry->next;
203 freez(cur_entry->str);
207 the_list->first = NULL;
208 the_list->last = NULL;
212 /*********************************************************************
214 * Function : list_is_valid
216 * Description : Check that a string list is valid. The intended
217 * usage is "assert(list_is_valid(the_list))".
218 * Currently this checks that "the_list->last"
219 * is correct, and that the list dosn't contain
220 * circular references. It is likely to crash if
221 * it's passed complete garbage.
224 * 1 : the_list = pointer to list. Must be non-null.
226 * Returns : 1 if list is valid, 0 otherwise.
228 *********************************************************************/
229 static int list_is_valid (const struct list *the_list)
232 * If you don't want this check, just change the line below
233 * from "#if 1" to "#if 0".
236 const struct list_entry *cur_entry;
237 const struct list_entry *last_entry = NULL;
242 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
244 last_entry = cur_entry;
249 * Just check that this string can be accessed - i.e. it's a valid
252 strlen(cur_entry->str);
256 * Check for looping back to first
258 if ((length != 0) && (cur_entry == the_list->first))
264 * Arbitrarily limit length to prevent infinite loops.
265 * Note that the 1000 limit was hit by a real user in tracker 911950;
266 * removing it for now. Symptoms of a real circular reference will
267 * include 100% CPU usage, I'd imagine. It'll be obvious, anyway.
277 * Check this isn't marked as the last entry, unless of course it's
278 * *really* the last entry.
280 if ((the_list->last == cur_entry) && (cur_entry->next != NULL))
282 /* This is the last entry, but there's data after it !!?? */
287 return (the_list->last == last_entry);
293 /*********************************************************************
297 * Description : Append a string into a specified string list.
300 * 1 : the_list = pointer to list
301 * 2 : str = string to add to the list (maybe NULL)
303 * Returns : JB_ERR_OK on success
304 * JB_ERR_MEMORY on out-of-memory error.
305 * On error, the_list will be unchanged.
307 *********************************************************************/
308 jb_err enlist(struct list *the_list, const char *str)
310 struct list_entry *cur;
313 assert(list_is_valid(the_list));
315 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
317 return JB_ERR_MEMORY;
322 if (NULL == (cur->str = strdup(str)))
325 return JB_ERR_MEMORY;
328 /* else { cur->str = NULL; } - implied by zalloc */
330 /* cur->next = NULL; - implied by zalloc */
334 the_list->last->next = cur;
335 the_list->last = cur;
339 the_list->first = cur;
340 the_list->last = cur;
343 assert(list_is_valid(the_list));
348 /*********************************************************************
350 * Function : enlist_first
352 * Description : Append a string as first element into a specified
356 * 1 : the_list = pointer to list
357 * 2 : str = string to add to the list (maybe NULL)
359 * Returns : JB_ERR_OK on success
360 * JB_ERR_MEMORY on out-of-memory error.
361 * On error, the_list will be unchanged.
363 *********************************************************************/
364 jb_err enlist_first(struct list *the_list, const char *str)
366 struct list_entry *cur;
369 assert(list_is_valid(the_list));
371 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
373 return JB_ERR_MEMORY;
378 if (NULL == (cur->str = strdup(str)))
381 return JB_ERR_MEMORY;
384 /* else { cur->str = NULL; } - implied by zalloc */
386 cur->next = the_list->first;
388 the_list->first = cur;
389 if (the_list->last == NULL)
391 the_list->last = cur;
394 assert(list_is_valid(the_list));
399 /*********************************************************************
401 * Function : enlist_unique
403 * Description : Append a string into a specified string list,
404 * if & only if it's not there already.
405 * If the num_significant_chars argument is nonzero,
406 * only compare up to the nth character.
409 * 1 : the_list = pointer to list
410 * 2 : str = string to add to the list
411 * 3 : num_significant_chars = number of chars to use
412 * for uniqueness test, or 0 to require an exact match.
414 * Returns : JB_ERR_OK on success
415 * JB_ERR_MEMORY on out-of-memory error.
416 * On error, the_list will be unchanged.
417 * "Success" does not indicate whether or not the
418 * item was already in the list.
420 *********************************************************************/
421 jb_err enlist_unique(struct list *the_list, const char *str,
422 size_t num_significant_chars)
424 struct list_entry *cur_entry;
427 assert(list_is_valid(the_list));
429 assert(num_significant_chars >= 0);
430 assert(num_significant_chars <= strlen(str));
432 if (num_significant_chars > 0)
434 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
436 if ( (cur_entry->str != NULL)
437 && (0 == strncmp(str, cur_entry->str, num_significant_chars)))
446 /* Test whole string */
447 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
449 if ( (cur_entry->str != NULL) && (0 == strcmp(str, cur_entry->str)))
457 return enlist(the_list, str);
461 /*********************************************************************
463 * Function : enlist_unique_header
465 * Description : Make a HTTP header from the two strings name and value,
466 * and append the result into a specified string list,
467 * if & only if there isn't already a header with that name.
470 * 1 : the_list = pointer to list
471 * 2 : name = HTTP header name (e.g. "Content-type")
472 * 3 : value = HTTP header value (e.g. "text/html")
474 * Returns : JB_ERR_OK on success
475 * JB_ERR_MEMORY on out-of-memory error.
476 * On error, the_list will be unchanged.
477 * "Success" does not indicate whether or not the
478 * header was already in the list.
480 *********************************************************************/
481 jb_err enlist_unique_header(struct list *the_list, const char *name,
489 assert(list_is_valid(the_list));
493 length = strlen(name) + 2;
494 if (NULL == (str = (char *)malloc(length + strlen(value) + 1)))
496 return JB_ERR_MEMORY;
499 str[length - 2] = ':';
500 str[length - 1] = ' ';
501 strcpy(str + length, value);
503 result = enlist_unique(the_list, str, length);
507 assert(list_is_valid(the_list));
514 /*********************************************************************
516 * Function : list_remove_all
518 * Description : Remove all entries from a list. On return, the_list
519 * is a valid, empty list. Note that this is similar
520 * to destroy_list(), but the difference is that this
521 * function guarantees that the list structure is still
522 * valid after the call.
525 * 1 : the_list = pointer to list
529 *********************************************************************/
530 void list_remove_all(struct list *the_list)
532 struct list_entry *cur_entry;
533 struct list_entry *next_entry;
536 assert(list_is_valid(the_list));
538 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
540 next_entry = cur_entry->next;
541 freez(cur_entry->str);
545 the_list->first = the_list->last = NULL;
547 assert(list_is_valid(the_list));
551 /*********************************************************************
553 * Function : list_to_text
555 * Description : "Flatten" a string list into 1 long \r\n delimited string,
556 * adding an empty line at the end. NULL entries are ignored.
557 * This function does not change the_list.
560 * 1 : the_list = pointer to list
562 * Returns : NULL on malloc error, else new long string.
563 * Caller must free() it.
565 *********************************************************************/
566 char *list_to_text(const struct list *the_list)
568 struct list_entry *cur_entry;
574 assert(list_is_valid(the_list));
576 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
580 size += strlen(cur_entry->str) + 2;
584 if ((ret = (char *)malloc(size + 1)) == NULL)
593 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
597 strcpy(s, cur_entry->str);
599 *s++ = '\r'; *s++ = '\n';
602 *s++ = '\r'; *s++ = '\n';
608 /*********************************************************************
610 * Function : list_remove_item
612 * Description : Remove a string from a specified string list.
615 * 1 : the_list = pointer to list
616 * 2 : str = string to remove from the list - non-NULL
618 * Returns : Number of times it was removed.
620 *********************************************************************/
621 int list_remove_item(struct list *the_list, const char *str)
623 struct list_entry *prev = NULL;
624 struct list_entry *cur;
625 struct list_entry *next;
629 assert(list_is_valid(the_list));
632 cur = the_list->first;
638 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
648 the_list->first = next;
650 free((char *)cur->str);
660 the_list->last = prev;
662 assert(list_is_valid(the_list));
668 /*********************************************************************
670 * Function : list_remove_list
672 * Description : Remove all strings in one list from another list.
673 * This is currently a brute-force algorithm
674 * (it compares every pair of strings).
677 * 1 : dest = list to change
678 * 2 : src = list of strings to remove
680 * Returns : Total number of strings removed.
682 *********************************************************************/
683 int list_remove_list(struct list *dest, const struct list *src)
685 struct list_entry *cur;
690 assert(list_is_valid(src));
691 assert(list_is_valid(dest));
693 for (cur = src->first; cur != NULL; cur = cur->next)
695 if (cur->str != NULL)
697 count += list_remove_item(dest, cur->str);
701 assert(list_is_valid(src));
702 assert(list_is_valid(dest));
708 /*********************************************************************
710 * Function : list_duplicate
712 * Description : Copy a string list
715 * 1 : dest = Destination list. Must be a valid list.
716 * All existing entries will be removed.
717 * 1 : src = pointer to source list for copy.
719 * Returns : JB_ERR_OK on success
720 * JB_ERR_MEMORY on out-of-memory error.
721 * On error, dest will be empty.
723 *********************************************************************/
724 jb_err list_duplicate(struct list *dest, const struct list *src)
726 struct list_entry * cur_src;
727 struct list_entry * cur_dest;
731 assert(list_is_valid(src));
732 assert(list_is_valid(dest));
734 list_remove_all(dest);
736 /* Need to process first entry specially so we can set dest->first */
737 cur_src = src->first;
740 cur_dest = dest->first = (struct list_entry *)zalloc(sizeof(*cur_dest));
741 if (cur_dest == NULL)
745 assert(list_is_valid(src));
746 assert(list_is_valid(dest));
748 return JB_ERR_MEMORY;
753 cur_dest->str = strdup(cur_src->str);
754 if (cur_dest->str == NULL)
758 assert(list_is_valid(src));
759 assert(list_is_valid(dest));
761 return JB_ERR_MEMORY;
764 /* else { cur_dest->str = NULL; } - implied by zalloc */
766 /* Now process the rest */
767 for (cur_src = cur_src->next; cur_src; cur_src = cur_src->next)
769 cur_dest = cur_dest->next = (struct list_entry *)zalloc(sizeof(*cur_dest));
770 if (cur_dest == NULL)
774 assert(list_is_valid(src));
775 assert(list_is_valid(dest));
777 return JB_ERR_MEMORY;
781 cur_dest->str = strdup(cur_src->str);
782 if (cur_dest->str == NULL)
786 assert(list_is_valid(src));
787 assert(list_is_valid(dest));
789 return JB_ERR_MEMORY;
792 /* else { cur_dest->str = NULL; } - implied by zalloc */
795 dest->last = cur_dest;
798 assert(list_is_valid(src));
799 assert(list_is_valid(dest));
805 /*********************************************************************
807 * Function : list_append_list_unique
809 * Description : Append a string list to another list.
810 * Duplicate items are not added.
813 * 1 : dest = pointer to destination list for merge.
814 * 2 : src = pointer to source for merge.
816 * Returns : JB_ERR_OK on success
817 * JB_ERR_MEMORY on out-of-memory error.
818 * On error, some (but not all) of src might have
819 * been copied into dest.
821 *********************************************************************/
822 jb_err list_append_list_unique(struct list *dest, const struct list *src)
824 struct list_entry * cur;
828 assert(list_is_valid(src));
829 assert(list_is_valid(dest));
831 for (cur = src->first; cur; cur = cur->next)
835 if (enlist_unique(dest, cur->str, 0))
837 assert(list_is_valid(src));
838 assert(list_is_valid(dest));
840 return JB_ERR_MEMORY;
845 assert(list_is_valid(src));
846 assert(list_is_valid(dest));
852 /*********************************************************************
854 * Function : list_is_empty
856 * Description : Test whether a list is empty. Does not change the list.
859 * 1 : the_list = pointer to list to test.
861 * Returns : Nonzero iff the list contains no entries.
863 *********************************************************************/
864 int list_is_empty(const struct list *the_list)
867 assert(list_is_valid(the_list));
869 return (the_list->first == NULL);
873 /*********************************************************************
877 * Description : Create a new, empty map.
881 * Returns : A new, empty map, or NULL if out of memory.
883 *********************************************************************/
884 struct map *new_map(void)
886 return (struct map *) zalloc(sizeof(struct map));
890 /*********************************************************************
892 * Function : free_map
894 * Description : Free the memory occupied by a map and its
898 * 1 : the_map = map to be freed. May be NULL.
902 *********************************************************************/
903 void free_map(struct map *the_map)
905 struct map_entry *cur_entry;
906 struct map_entry *next_entry;
913 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = next_entry)
915 freez(cur_entry->name);
916 freez(cur_entry->value);
918 next_entry = cur_entry->next;
922 the_map->first = the_map->last = NULL;
928 /*********************************************************************
932 * Description : Add a mapping from given name to given value to a
935 * Note: Since all strings will be free()d in free_map()
936 * later, set the copy flags for constants or
937 * strings that will be independantly free()d.
939 * Note2: This function allows NULL parameters - it
940 * returns JB_ERR_MEMORY in that case.
942 * Note3: If this function returns JB_ERR_MEMORY,
943 * it will free(name) unless you specify
944 * name_needs_copying, and similarly it will
945 * free(value) unless you specify
946 * value_needs_copying.
948 * Due to Note2 and Note3 above, the following code
949 * is legal, and will never crash or leak memory even
950 * if the system runs out of memory:
952 * err = map(mymap, "xyz", 1, html_encode(somestring), 0);
954 * err will be set to JB_ERR_MEMORY if either call runs
955 * out-of-memory. Without these features, you would
956 * need to check the return value of html_encode in the
957 * above example for NULL, which (at least) doubles the
958 * amount of error-checking code needed.
961 * 1 : the_map = map to add to
962 * 2 : name = name to add
963 * 3 : name_needs_copying = flag set if a copy of name should be used
964 * 4 : value = value to add
965 * 5 : value_needs_copying = flag set if a copy of value should be used
967 * Returns : JB_ERR_OK on success
968 * JB_ERR_MEMORY on out-of-memory error.
970 *********************************************************************/
971 jb_err map(struct map *the_map,
972 const char *name, int name_needs_copying,
973 const char *value, int value_needs_copying)
975 struct map_entry *new_entry;
981 || (NULL == (new_entry = zalloc(sizeof(*new_entry)))) )
983 if ((name != NULL) && (!name_needs_copying))
987 if ((value != NULL) && (!value_needs_copying))
991 return JB_ERR_MEMORY;
994 if (name_needs_copying)
996 if (NULL == (name = strdup(name)))
999 if (!value_needs_copying)
1001 free((char *)value);
1003 return JB_ERR_MEMORY;
1007 if (value_needs_copying)
1009 if (NULL == (value = strdup(value)))
1013 return JB_ERR_MEMORY;
1017 new_entry->name = name;
1018 new_entry->value = value;
1019 /* new_entry->next = NULL; - implied by zalloc */
1023 the_map->last->next = new_entry;
1024 the_map->last = new_entry;
1028 the_map->first = new_entry;
1029 the_map->last = new_entry;
1036 /*********************************************************************
1040 * Description : Look up an item with a given name in a map, and
1044 * 1 : the_map = map to look in
1045 * 2 : name = name parameter to look for
1047 * Returns : the value if found, else the empty string.
1048 * Return value is alloced as part of the map, so
1049 * it is freed when the map is destroyed. Caller
1050 * must not free or modify it.
1052 *********************************************************************/
1053 const char *lookup(const struct map *the_map, const char *name)
1055 const struct map_entry *cur_entry;
1060 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = cur_entry->next)
1062 if (!strcmp(name, cur_entry->name))
1064 return cur_entry->value;