1 | /* |
---|
2 | * |
---|
3 | * Copyright (C) 1989 by the Massachusetts Institute of Technology |
---|
4 | * Developed by the MIT Student Information Processing Board (SIPB). |
---|
5 | * For copying information, see the file mit-copyright.h in this release. |
---|
6 | * |
---|
7 | */ |
---|
8 | /* |
---|
9 | * |
---|
10 | * $Id: trn_select.c,v 1.17 1999-02-08 14:46:55 danw Exp $ |
---|
11 | * |
---|
12 | */ |
---|
13 | |
---|
14 | #ifndef lint |
---|
15 | static char rcsid_discuss_c[] = |
---|
16 | "$Id: trn_select.c,v 1.17 1999-02-08 14:46:55 danw Exp $"; |
---|
17 | #endif /* lint */ |
---|
18 | |
---|
19 | #define MIN(a,b) ((a)<(b)?(a):(b)) |
---|
20 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
---|
21 | #include <stdio.h> |
---|
22 | #include <discuss/discuss.h> |
---|
23 | #include "globals.h" |
---|
24 | |
---|
25 | extern char *malloc(); |
---|
26 | |
---|
27 | selection_list * |
---|
28 | sl_insert_range(low, high, flags, old_list, code_ptr) |
---|
29 | register int low, high; |
---|
30 | int flags; |
---|
31 | register selection_list *old_list; |
---|
32 | int *code_ptr; |
---|
33 | { |
---|
34 | register selection_list *p, *new; |
---|
35 | p = old_list; |
---|
36 | |
---|
37 | new = (selection_list *)malloc(sizeof(selection_list)); |
---|
38 | if (!new) { |
---|
39 | *code_ptr = errno; |
---|
40 | return((selection_list *)NULL); |
---|
41 | } |
---|
42 | new->low = low; |
---|
43 | new->high = high; |
---|
44 | new->flags = flags; |
---|
45 | new->next = (selection_list *)NULL; |
---|
46 | |
---|
47 | if (!p) |
---|
48 | return(new); /* first one, so just return it */ |
---|
49 | |
---|
50 | /* we just add it on to the end, and let the user lose if he |
---|
51 | wants...(he might want them printed/listed out of order. |
---|
52 | Ken, try to document... */ |
---|
53 | |
---|
54 | while (p->next != NULL) { |
---|
55 | p = p->next; |
---|
56 | } |
---|
57 | |
---|
58 | p->next = new; |
---|
59 | return(old_list); |
---|
60 | } |
---|
61 | |
---|
62 | selection_list * |
---|
63 | sl_insert_num(num, flags, old_list, code_ptr) |
---|
64 | int num; |
---|
65 | int flags; |
---|
66 | selection_list *old_list; |
---|
67 | int *code_ptr; |
---|
68 | { |
---|
69 | return(sl_insert_range(num, num, flags, old_list, code_ptr)); |
---|
70 | } |
---|
71 | |
---|
72 | void |
---|
73 | sl_free(list) |
---|
74 | selection_list *list; |
---|
75 | { |
---|
76 | register selection_list *p, *q; |
---|
77 | p = list; |
---|
78 | while (p) { |
---|
79 | q = p->next; |
---|
80 | free(p); |
---|
81 | p = q; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | int filter_call_func (func, t, r, flags) |
---|
86 | int (*func)(); |
---|
87 | trn_info3 *t; |
---|
88 | int *r; |
---|
89 | int flags; |
---|
90 | { |
---|
91 | if (flags & filter_ONLY_DELETED) if (!(t->flags & TRN_FDELETED)) return (0); |
---|
92 | if (flags & filter_ONLY_INITIAL) if (t->pref) return (0); |
---|
93 | if (flags & filter_ONLY_TERMINAL) if (t->nref) return (0); |
---|
94 | /* note, FLAG_SET and FLAG_RESET are not really opposites. |
---|
95 | The default is to map transaction with either flag set or reset */ |
---|
96 | if (flags & filter_FLAG_SET) if (!(t->flags & TRN_FLAG1)) return (0); |
---|
97 | if (flags & filter_FLAG_RESET) if (t->flags & TRN_FLAG1) return (0); |
---|
98 | return (func (t, r)); |
---|
99 | } |
---|
100 | |
---|
101 | int |
---|
102 | sl_map(func, list, filter_flags) |
---|
103 | int (*func)(); /* return 0 -> continue */ |
---|
104 | selection_list *list; |
---|
105 | int filter_flags; |
---|
106 | { |
---|
107 | register selection_list *p; |
---|
108 | register int i; |
---|
109 | int result,trn_no; |
---|
110 | trn_info3 t_info; |
---|
111 | |
---|
112 | flag_interrupts(); |
---|
113 | for (p = list; p; p = p->next) { |
---|
114 | if ((p->flags & flag_AREF)) { /* Handle aref */ |
---|
115 | dsc_get_trn_info3 (&dsc_public.nb, p->low, &t_info, &result); |
---|
116 | t_info.current = p->low; |
---|
117 | if (result != 0) { |
---|
118 | filter_call_func(func, &t_info, &result, filter_flags); |
---|
119 | dsc_destroy_trn_info3(&t_info); |
---|
120 | if (result != 0) { |
---|
121 | return(result); |
---|
122 | } |
---|
123 | continue; |
---|
124 | } |
---|
125 | if (t_info.fref != t_info.current) { /* Get fref */ |
---|
126 | dsc_destroy_trn_info3(&t_info); |
---|
127 | trn_no = t_info.fref; |
---|
128 | dsc_get_trn_info3 (&dsc_public.nb, trn_no, &t_info, &result); |
---|
129 | t_info.current = trn_no; |
---|
130 | } |
---|
131 | |
---|
132 | do { |
---|
133 | filter_call_func(func, &t_info, &result, filter_flags); |
---|
134 | dsc_destroy_trn_info3(&t_info); |
---|
135 | if (result != 0) |
---|
136 | return(result); |
---|
137 | |
---|
138 | if (interrupt) |
---|
139 | goto exit; |
---|
140 | |
---|
141 | trn_no = t_info.nref; |
---|
142 | if (trn_no != 0) { |
---|
143 | dsc_get_trn_info3 (&dsc_public.nb, trn_no, &t_info, &result); |
---|
144 | t_info.current = trn_no; |
---|
145 | } |
---|
146 | } while (trn_no != 0); |
---|
147 | } else if (filter_flags & |
---|
148 | (filter_INCLUDE_DELETED | filter_ONLY_DELETED)) { |
---|
149 | |
---|
150 | /* Range */ |
---|
151 | for (i = p->low; i <= p->high; i++) { |
---|
152 | if (i == 0) |
---|
153 | continue; |
---|
154 | |
---|
155 | dsc_get_trn_info3 (&dsc_public.nb, i, &t_info, &result); |
---|
156 | if ((result != DELETED_TRN) || (t_info.current != 0)) { |
---|
157 | t_info.current = i; |
---|
158 | filter_call_func(func, &t_info, &result, filter_flags); |
---|
159 | } |
---|
160 | dsc_destroy_trn_info3(&t_info); |
---|
161 | if (result != 0) |
---|
162 | return(result); |
---|
163 | |
---|
164 | if (interrupt) |
---|
165 | goto exit; |
---|
166 | } |
---|
167 | } else { /* Walk chains where possible */ |
---|
168 | trn_no = p->low; |
---|
169 | if (trn_no < dsc_public.m_info.first) |
---|
170 | trn_no = dsc_public.m_info.first; |
---|
171 | |
---|
172 | while (trn_no != 0 && trn_no <= p->high) { |
---|
173 | if (interrupt) |
---|
174 | goto exit; |
---|
175 | |
---|
176 | dsc_get_trn_info3(&dsc_public.nb, trn_no, &t_info, &result); |
---|
177 | t_info.current = trn_no; |
---|
178 | |
---|
179 | if (result == DELETED_TRN) { |
---|
180 | trn_no++; |
---|
181 | dsc_destroy_trn_info3(&t_info); |
---|
182 | continue; |
---|
183 | } |
---|
184 | |
---|
185 | filter_call_func(func, &t_info, &result, filter_flags); |
---|
186 | dsc_destroy_trn_info3(&t_info); |
---|
187 | if (result != 0) |
---|
188 | return(result); |
---|
189 | |
---|
190 | trn_no = t_info.next; |
---|
191 | } |
---|
192 | } |
---|
193 | } |
---|
194 | exit: |
---|
195 | dont_flag_interrupts(); |
---|
196 | return(0); |
---|
197 | } |
---|
198 | |
---|
199 | selection_list * |
---|
200 | trn_select(t_info, string, old_sl_ptr, code_ptr) |
---|
201 | trn_info *t_info; |
---|
202 | char *string; |
---|
203 | selection_list *old_sl_ptr; |
---|
204 | register int *code_ptr; |
---|
205 | { |
---|
206 | int low, high, flags; |
---|
207 | |
---|
208 | *code_ptr = trnexpr_parse(&dsc_public.m_info, t_info, string, |
---|
209 | &low, &high, &flags); |
---|
210 | if (*code_ptr != 0) |
---|
211 | return((selection_list *)NULL); |
---|
212 | old_sl_ptr = sl_insert_range(low, high, flags, old_sl_ptr, code_ptr); |
---|
213 | return(old_sl_ptr); |
---|
214 | } |
---|