1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: handle.c 769 2007-10-24 00:15:40Z hubert@u.washington.edu $";
3 #endif
4 
5 /*
6  * ========================================================================
7  * Copyright 2013-2021 Eduardo Chappa
8  * Copyright 2006-2007 University of Washington
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *     http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * ========================================================================
17  */
18 
19 #include "../pith/headers.h"
20 #include "../pith/handle.h"
21 #include "../pith/mailview.h"
22 
23 
24 HANDLE_S *
get_handle(HANDLE_S * handles,int key)25 get_handle(HANDLE_S *handles, int key)
26 {
27     HANDLE_S *h;
28 
29     if((h = handles) != NULL){
30 	for( ; h ; h = h->next)
31 	  if(h->key == key)
32 	    return(h);
33 
34 	for(h = handles->prev ; h ; h = h->prev)
35 	  if(h->key == key)
36 	    return(h);
37     }
38 
39     return(NULL);
40 }
41 
42 
43 void
init_handles(HANDLE_S ** handlesp)44 init_handles(HANDLE_S **handlesp)
45 {
46     if(handlesp)
47       *handlesp = NULL;
48 
49     (void) url_external_specific_handler(NULL, 0);
50 }
51 
52 
53 
54 HANDLE_S *
new_handle(HANDLE_S ** handlesp)55 new_handle(HANDLE_S **handlesp)
56 {
57     HANDLE_S *hp, *h = NULL;
58 
59     if(handlesp){
60 	h = (HANDLE_S *) fs_get(sizeof(HANDLE_S));
61 	memset(h, 0, sizeof(HANDLE_S));
62 
63 	/* Put it in the list */
64 	if((hp = *handlesp) != NULL){
65 	    while(hp->next)
66 	      hp = hp->next;
67 
68 	    h->key = hp->key + 1;
69 	    hp->next = h;
70 	    h->prev = hp;
71 	}
72 	else{
73 	    /* Assumption #2,340: There are NO ZERO KEY HANDLES */
74 	    h->key = 1;
75 	    *handlesp = h;
76 	}
77     }
78 
79     return(h);
80 }
81 
82 
83 /*
84  * Normally we ignore the is_used bit in HANDLE_S. However, if we are
85  * using the delete_quotes filter, we pay attention to it. All of the is_used
86  * bits are off by default, and the delete_quotes filter turns them on
87  * if it is including lines with those handles.
88  *
89  * This is a bit of a crock, since it depends heavily on the order of the
90  * filters. Notice that the charset_editorial filter, which comes after
91  * delete_quotes and adds a handle, has to explicitly set the is_used bit!
92  */
93 void
delete_unused_handles(HANDLE_S ** handlesp)94 delete_unused_handles(HANDLE_S **handlesp)
95 {
96     HANDLE_S *h, *nexth;
97 
98     if(handlesp && *handlesp && (*handlesp)->using_is_used){
99 	for(h = *handlesp; h && h->prev; h = h->prev)
100 	  ;
101 
102 	for(; h; h = nexth){
103 	    nexth = h->next;
104 	    if(h->is_used == 0){
105 		if(h == *handlesp)
106 		  *handlesp = nexth;
107 
108 		free_handle(&h);
109 	    }
110 	}
111     }
112 }
113 
114 
115 void
free_handle(HANDLE_S ** h)116 free_handle(HANDLE_S **h)
117 {
118     if(h){
119 	if((*h)->next)				/* clip from list */
120 	  (*h)->next->prev = (*h)->prev;
121 
122 	if((*h)->prev)
123 	  (*h)->prev->next = (*h)->next;
124 
125 	if((*h)->type == URL){			/* destroy malloc'd data */
126 	    if((*h)->h.url.path)
127 	      fs_give((void **) &(*h)->h.url.path);
128 
129 	   if((*h)->h.url.tool)
130 	     fs_give((void **) &(*h)->h.url.tool);
131 
132 	    if((*h)->h.url.name)
133 	      fs_give((void **) &(*h)->h.url.name);
134 	}
135 
136 	free_handle_locations(&(*h)->loc);
137 
138 	fs_give((void **) h);
139     }
140 }
141 
142 
143 void
free_handles(HANDLE_S ** handlesp)144 free_handles(HANDLE_S **handlesp)
145 {
146     HANDLE_S *h;
147 
148     if(handlesp && *handlesp){
149 	while((h = (*handlesp)->next) != NULL)
150 	  free_handle(&h);
151 
152 	while((h = (*handlesp)->prev) != NULL)
153 	  free_handle(&h);
154 
155 	free_handle(handlesp);
156     }
157 }
158 
159 
160 void
free_handle_locations(POSLIST_S ** l)161 free_handle_locations(POSLIST_S **l)
162 {
163     if(*l){
164 	free_handle_locations(&(*l)->next);
165 	fs_give((void **) l);
166     }
167 }
168 
169 
170