1 const char list_rcs[] = "$Id: list.c,v 1.14 2002/03/24 13:25:43 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.14 2002/03/24 13:25:43 swa
38 * name change related issues
40 * Revision 1.13 2002/03/07 03:46:17 oes
41 * Fixed compiler warnings
43 * Revision 1.12 2001/10/25 03:40:48 david__schmidt
44 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
45 * threads to call select() simultaneously. So, it's time to do a real, live,
46 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
47 * (native). Both versions will work, but using __OS2__ offers multi-threading.
49 * Revision 1.11 2001/10/23 21:21:03 jongfoster
50 * New error handling - error codes are now jb_errs, not ints.
51 * Changed the way map() handles out-of-memory, to dramatically
52 * reduce the amount of error-checking clutter needed.
54 * Revision 1.10 2001/09/16 17:30:24 jongfoster
55 * Fixing a compiler warning.
57 * Revision 1.9 2001/09/16 13:20:29 jongfoster
58 * Rewrite of list library. Now has seperate header and list_entry
59 * structures. Also added a large sprinking of assert()s to the list
62 * Revision 1.8 2001/08/07 14:00:20 oes
65 * Revision 1.7 2001/08/05 16:06:20 jongfoster
66 * Modifiying "struct map" so that there are now separate header and
67 * "map_entry" structures. This means that functions which modify a
68 * map no longer need to return a pointer to the modified map.
69 * Also, it no longer reverses the order of the entries (which may be
70 * important with some advanced template substitutions).
72 * Revision 1.6 2001/07/31 14:44:51 oes
73 * list_to_text() now appends empty line at end
75 * Revision 1.5 2001/06/29 21:45:41 oes
76 * Indentation, CRLF->LF, Tab-> Space
78 * Revision 1.4 2001/06/29 13:30:22 oes
79 * - Added Convenience function enlist_unique_header(),
80 * which takes the Header name and value as separate
81 * arguments and thus saves the pain of sprintf()ing
82 * and determining the Header name length to enlist_unique
84 * - Removed logentry from cancelled commit
86 * Revision 1.3 2001/06/03 19:12:24 oes
87 * functions for new struct map, extended enlist_unique
89 * Revision 1.2 2001/06/01 18:49:17 jongfoster
90 * Replaced "list_share" with "list" - the tiny memory gain was not
91 * worth the extra complexity.
93 * Revision 1.1 2001/05/31 21:11:53 jongfoster
94 * - Moved linked list support to new "list.c" file.
95 * Structure definitions are still in project.h,
96 * function prototypes are now in "list.h".
97 * - Added support for "struct list_share", which is identical
98 * to "struct list" except it saves memory by not duplicating
99 * the strings. Obviously, this only works if there is some
100 * other way of managing the memory used by the strings.
101 * (These list_share lists are used for lists which last
102 * for only 1 request, and where all the list entries are
103 * just coming directly from entries in the actionsfile.)
104 * Note that you still need to destroy list_share lists
105 * properly to free the nodes - it's only the strings
109 *********************************************************************/
115 /* FIXME: The following headers are not needed for Win32. Are they
116 * needed on other platforms?
119 #include <sys/types.h>
125 #if !defined(_WIN32) && !defined(__OS2__)
133 #include "miscutil.h"
135 const char list_h_rcs[] = LIST_H_VERSION;
138 static int list_is_valid (const struct list *the_list);
141 /*********************************************************************
143 * Function : list_init
145 * Description : Create a new, empty list in user-allocated memory.
146 * Caller should allocate a "struct list" variable,
147 * then pass it to this function.
148 * (Implementation note: Rather than calling this
149 * function, you can also just memset the memory to
150 * zero, e.g. if you have a larger structure you
151 * want to initialize quickly. However, that isn't
152 * really good design.)
155 * 1 : the_list = pointer to list
159 *********************************************************************/
160 void init_list(struct list *the_list)
162 memset(the_list, '\0', sizeof(*the_list));
166 /*********************************************************************
168 * Function : destroy_list
170 * Description : Destroy a string list (opposite of list_init).
171 * On return, the memory used by the list entries has
172 * been freed, but not the memory used by the_list
173 * itself. You should not re-use the_list without
174 * calling list_init().
176 * (Implementation note: You *can* reuse the_list
177 * without calling list_init(), but please don't.
178 * If you want to remove all entries from a list
179 * and still have a usable list, then use
180 * list_remove_all().)
183 * 1 : the_list = pointer to list
187 *********************************************************************/
188 void destroy_list (struct list *the_list)
190 struct list_entry *cur_entry, *next_entry;
194 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
196 next_entry = cur_entry->next;
197 freez(cur_entry->str);
201 the_list->first = NULL;
202 the_list->last = NULL;
206 /*********************************************************************
208 * Function : list_is_valid
210 * Description : Check that a string list is valid. The intended
211 * usage is "assert(list_is_valid(the_list))".
212 * Currently this checks that "the_list->last"
213 * is correct, and that the list dosn't contain
214 * circular references. It is likely to crash if
215 * it's passed complete garbage.
218 * 1 : the_list = pointer to list. Must be non-null.
220 * Returns : 1 if list is valid, 0 otherwise.
222 *********************************************************************/
223 static int list_is_valid (const struct list *the_list)
226 * If you don't want this check, just change the line below
227 * from "#if 1" to "#if 0".
230 const struct list_entry *cur_entry;
231 const struct list_entry *last_entry = NULL;
236 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
238 last_entry = cur_entry;
243 * Just check that this string can be accessed - i.e. it's a valid
246 strlen(cur_entry->str);
250 * Check for looping back to first
252 if ((length != 0) && (cur_entry == the_list->first))
258 * Arbitrarily limit length to prevent infinite loops.
266 * Check this isn't marked as the last entry, unless of course it's
267 * *really* the last entry.
269 if ((the_list->last == cur_entry) && (cur_entry->next != NULL))
271 /* This is the last entry, but there's data after it !!?? */
276 return (the_list->last == last_entry);
282 /*********************************************************************
286 * Description : Append a string into a specified string list.
289 * 1 : the_list = pointer to list
290 * 2 : str = string to add to the list (maybe NULL)
292 * Returns : JB_ERR_OK on success
293 * JB_ERR_MEMORY on out-of-memory error.
294 * On error, the_list will be unchanged.
296 *********************************************************************/
297 jb_err enlist(struct list *the_list, const char *str)
299 struct list_entry *cur;
302 assert(list_is_valid(the_list));
304 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
306 return JB_ERR_MEMORY;
311 if (NULL == (cur->str = strdup(str)))
314 return JB_ERR_MEMORY;
317 /* else { cur->str = NULL; } - implied by zalloc */
319 /* cur->next = NULL; - implied by zalloc */
323 the_list->last->next = cur;
324 the_list->last = cur;
328 the_list->first = cur;
329 the_list->last = cur;
332 assert(list_is_valid(the_list));
337 /*********************************************************************
339 * Function : enlist_first
341 * Description : Append a string as first element into a specified
345 * 1 : the_list = pointer to list
346 * 2 : str = string to add to the list (maybe NULL)
348 * Returns : JB_ERR_OK on success
349 * JB_ERR_MEMORY on out-of-memory error.
350 * On error, the_list will be unchanged.
352 *********************************************************************/
353 jb_err enlist_first(struct list *the_list, const char *str)
355 struct list_entry *cur;
358 assert(list_is_valid(the_list));
360 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
362 return JB_ERR_MEMORY;
367 if (NULL == (cur->str = strdup(str)))
370 return JB_ERR_MEMORY;
373 /* else { cur->str = NULL; } - implied by zalloc */
375 cur->next = the_list->first;
377 the_list->first = cur;
378 if (the_list->last == NULL)
380 the_list->last = cur;
383 assert(list_is_valid(the_list));
388 /*********************************************************************
390 * Function : enlist_unique
392 * Description : Append a string into a specified string list,
393 * if & only if it's not there already.
394 * If the num_significant_chars argument is nonzero,
395 * only compare up to the nth character.
398 * 1 : the_list = pointer to list
399 * 2 : str = string to add to the list
400 * 3 : num_significant_chars = number of chars to use
401 * for uniqueness test, or 0 to require an exact match.
403 * Returns : JB_ERR_OK on success
404 * JB_ERR_MEMORY on out-of-memory error.
405 * On error, the_list will be unchanged.
406 * "Success" does not indicate whether or not the
407 * item was already in the list.
409 *********************************************************************/
410 jb_err enlist_unique(struct list *the_list, const char *str,
411 size_t num_significant_chars)
413 struct list_entry *cur_entry;
416 assert(list_is_valid(the_list));
418 assert(num_significant_chars >= 0);
419 assert(num_significant_chars <= strlen(str));
421 if (num_significant_chars > 0)
423 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
425 if ( (cur_entry->str != NULL)
426 && (0 == strncmp(str, cur_entry->str, num_significant_chars)))
435 /* Test whole string */
436 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
438 if ( (cur_entry->str != NULL) && (0 == strcmp(str, cur_entry->str)))
446 return enlist(the_list, str);
450 /*********************************************************************
452 * Function : enlist_unique_header
454 * Description : Make a HTTP header from the two strings name and value,
455 * and append the result into a specified string list,
456 * if & only if there isn't already a header with that name.
459 * 1 : the_list = pointer to list
460 * 2 : name = HTTP header name (e.g. "Content-type")
461 * 3 : value = HTTP header value (e.g. "text/html")
463 * Returns : JB_ERR_OK on success
464 * JB_ERR_MEMORY on out-of-memory error.
465 * On error, the_list will be unchanged.
466 * "Success" does not indicate whether or not the
467 * header was already in the list.
469 *********************************************************************/
470 jb_err enlist_unique_header(struct list *the_list, const char *name,
478 assert(list_is_valid(the_list));
482 length = strlen(name) + 2;
483 if (NULL == (str = (char *)malloc(length + strlen(value) + 1)))
485 return JB_ERR_MEMORY;
488 str[length - 2] = ':';
489 str[length - 1] = ' ';
490 strcpy(str + length, value);
492 result = enlist_unique(the_list, str, length);
496 assert(list_is_valid(the_list));
503 /*********************************************************************
505 * Function : list_remove_all
507 * Description : Remove all entries from a list. On return, the_list
508 * is a valid, empty list. Note that this is similar
509 * to destroy_list(), but the difference is that this
510 * function guarantees that the list structure is still
511 * valid after the call.
514 * 1 : the_list = pointer to list
518 *********************************************************************/
519 void list_remove_all(struct list *the_list)
521 struct list_entry *cur_entry;
522 struct list_entry *next_entry;
525 assert(list_is_valid(the_list));
527 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
529 next_entry = cur_entry->next;
530 freez(cur_entry->str);
534 the_list->first = the_list->last = NULL;
536 assert(list_is_valid(the_list));
540 /*********************************************************************
542 * Function : list_to_text
544 * Description : "Flatten" a string list into 1 long \r\n delimited string,
545 * adding an empty line at the end. NULL entries are ignored.
546 * This function does not change the_list.
549 * 1 : the_list = pointer to list
551 * Returns : NULL on malloc error, else new long string.
552 * Caller must free() it.
554 *********************************************************************/
555 char *list_to_text(const struct list *the_list)
557 struct list_entry *cur_entry;
563 assert(list_is_valid(the_list));
565 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
569 size += strlen(cur_entry->str) + 2;
573 if ((ret = (char *)malloc(size + 1)) == NULL)
582 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
586 strcpy(s, cur_entry->str);
588 *s++ = '\r'; *s++ = '\n';
591 *s++ = '\r'; *s++ = '\n';
597 /*********************************************************************
599 * Function : list_remove_item
601 * Description : Remove a string from a specified string list.
604 * 1 : the_list = pointer to list
605 * 2 : str = string to remove from the list - non-NULL
607 * Returns : Number of times it was removed.
609 *********************************************************************/
610 int list_remove_item(struct list *the_list, const char *str)
612 struct list_entry *prev = NULL;
613 struct list_entry *cur;
614 struct list_entry *next;
618 assert(list_is_valid(the_list));
621 cur = the_list->first;
627 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
637 the_list->first = next;
639 free((char *)cur->str);
649 the_list->last = prev;
651 assert(list_is_valid(the_list));
657 /*********************************************************************
659 * Function : list_remove_list
661 * Description : Remove all strings in one list from another list.
662 * This is currently a brute-force algorithm
663 * (it compares every pair of strings).
666 * 1 : dest = list to change
667 * 2 : src = list of strings to remove
669 * Returns : Total number of strings removed.
671 *********************************************************************/
672 int list_remove_list(struct list *dest, const struct list *src)
674 struct list_entry *cur;
679 assert(list_is_valid(src));
680 assert(list_is_valid(dest));
682 for (cur = src->first; cur != NULL; cur = cur->next)
684 if (cur->str != NULL)
686 count += list_remove_item(dest, cur->str);
690 assert(list_is_valid(src));
691 assert(list_is_valid(dest));
697 /*********************************************************************
699 * Function : list_duplicate
701 * Description : Copy a string list
704 * 1 : dest = Destination list. Must be a valid list.
705 * All existing entries will be removed.
706 * 1 : src = pointer to source list for copy.
708 * Returns : JB_ERR_OK on success
709 * JB_ERR_MEMORY on out-of-memory error.
710 * On error, dest will be empty.
712 *********************************************************************/
713 jb_err list_duplicate(struct list *dest, const struct list *src)
715 struct list_entry * cur_src;
716 struct list_entry * cur_dest;
720 assert(list_is_valid(src));
721 assert(list_is_valid(dest));
723 list_remove_all(dest);
725 /* Need to process first entry specially so we can set dest->first */
726 cur_src = src->first;
729 cur_dest = dest->first = (struct list_entry *)zalloc(sizeof(*cur_dest));
730 if (cur_dest == NULL)
734 assert(list_is_valid(src));
735 assert(list_is_valid(dest));
737 return JB_ERR_MEMORY;
742 cur_dest->str = strdup(cur_src->str);
743 if (cur_dest->str == NULL)
747 assert(list_is_valid(src));
748 assert(list_is_valid(dest));
750 return JB_ERR_MEMORY;
753 /* else { cur_dest->str = NULL; } - implied by zalloc */
755 /* Now process the rest */
756 for (cur_src = cur_src->next; cur_src; cur_src = cur_src->next)
758 cur_dest = cur_dest->next = (struct list_entry *)zalloc(sizeof(*cur_dest));
759 if (cur_dest == NULL)
763 assert(list_is_valid(src));
764 assert(list_is_valid(dest));
766 return JB_ERR_MEMORY;
770 cur_dest->str = strdup(cur_src->str);
771 if (cur_dest->str == NULL)
775 assert(list_is_valid(src));
776 assert(list_is_valid(dest));
778 return JB_ERR_MEMORY;
781 /* else { cur_dest->str = NULL; } - implied by zalloc */
784 dest->last = cur_dest;
787 assert(list_is_valid(src));
788 assert(list_is_valid(dest));
794 /*********************************************************************
796 * Function : list_append_list_unique
798 * Description : Append a string list to another list.
799 * Duplicate items are not added.
802 * 1 : dest = pointer to destination list for merge.
803 * 2 : src = pointer to source for merge.
805 * Returns : JB_ERR_OK on success
806 * JB_ERR_MEMORY on out-of-memory error.
807 * On error, some (but not all) of src might have
808 * been copied into dest.
810 *********************************************************************/
811 jb_err list_append_list_unique(struct list *dest, const struct list *src)
813 struct list_entry * cur;
817 assert(list_is_valid(src));
818 assert(list_is_valid(dest));
820 for (cur = src->first; cur; cur = cur->next)
824 if (enlist_unique(dest, cur->str, 0))
826 assert(list_is_valid(src));
827 assert(list_is_valid(dest));
829 return JB_ERR_MEMORY;
834 assert(list_is_valid(src));
835 assert(list_is_valid(dest));
841 /*********************************************************************
843 * Function : list_is_empty
845 * Description : Test whether a list is empty. Does not change the list.
848 * 1 : the_list = pointer to list to test.
850 * Returns : Nonzero iff the list contains no entries.
852 *********************************************************************/
853 int list_is_empty(const struct list *the_list)
856 assert(list_is_valid(the_list));
858 return (the_list->first == NULL);
862 /*********************************************************************
866 * Description : Create a new, empty map.
870 * Returns : A new, empty map, or NULL if out of memory.
872 *********************************************************************/
873 struct map *new_map(void)
875 return (struct map *) zalloc(sizeof(struct map));
879 /*********************************************************************
881 * Function : free_map
883 * Description : Free the memory occupied by a map and its
887 * 1 : the_map = map to be freed. May be NULL.
891 *********************************************************************/
892 void free_map(struct map *the_map)
894 struct map_entry *cur_entry;
895 struct map_entry *next_entry;
902 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = next_entry)
904 freez(cur_entry->name);
905 freez(cur_entry->value);
907 next_entry = cur_entry->next;
911 the_map->first = the_map->last = NULL;
917 /*********************************************************************
921 * Description : Add a mapping from given name to given value to a
924 * Note: Since all strings will be free()d in free_map()
925 * later, set the copy flags for constants or
926 * strings that will be independantly free()d.
928 * Note2: This function allows NULL parameters - it
929 * returns JB_ERR_MEMORY in that case.
931 * Note3: If this function returns JB_ERR_MEMORY,
932 * it will free(name) unless you specify
933 * name_needs_copying, and similarly it will
934 * free(value) unless you specify
935 * value_needs_copying.
937 * Due to Note2 and Note3 above, the following code
938 * is legal, and will never crash or leak memory even
939 * if the system runs out of memory:
941 * err = map(mymap, "xyz", 1, html_encode(somestring), 0);
943 * err will be set to JB_ERR_MEMORY if either call runs
944 * out-of-memory. Without these features, you would
945 * need to check the return value of html_encode in the
946 * above example for NULL, which (at least) doubles the
947 * amount of error-checking code needed.
950 * 1 : the_map = map to add to
951 * 2 : name = name to add
952 * 3 : name_needs_copying = flag set if a copy of name should be used
953 * 4 : value = value to add
954 * 5 : value_needs_copying = flag set if a copy of value should be used
956 * Returns : JB_ERR_OK on success
957 * JB_ERR_MEMORY on out-of-memory error.
959 *********************************************************************/
960 jb_err map(struct map *the_map,
961 const char *name, int name_needs_copying,
962 const char *value, int value_needs_copying)
964 struct map_entry *new_entry;
970 || (NULL == (new_entry = zalloc(sizeof(*new_entry)))) )
972 if ((name != NULL) && (!name_needs_copying))
976 if ((value != NULL) && (!value_needs_copying))
980 return JB_ERR_MEMORY;
983 if (name_needs_copying)
985 if (NULL == (name = strdup(name)))
988 if (!value_needs_copying)
992 return JB_ERR_MEMORY;
996 if (value_needs_copying)
998 if (NULL == (value = strdup(value)))
1002 return JB_ERR_MEMORY;
1006 new_entry->name = name;
1007 new_entry->value = value;
1008 /* new_entry->next = NULL; - implied by zalloc */
1012 the_map->last->next = new_entry;
1013 the_map->last = new_entry;
1017 the_map->first = new_entry;
1018 the_map->last = new_entry;
1025 /*********************************************************************
1029 * Description : Look up an item with a given name in a map, and
1033 * 1 : the_map = map to look in
1034 * 2 : name = name parameter to look for
1036 * Returns : the value if found, else the empty string.
1037 * Return value is alloced as part of the map, so
1038 * it is freed when the map is destroyed. Caller
1039 * must not free or modify it.
1041 *********************************************************************/
1042 const char *lookup(const struct map *the_map, const char *name)
1044 const struct map_entry *cur_entry;
1049 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = cur_entry->next)
1051 if (!strcmp(name, cur_entry->name))
1053 return cur_entry->value;