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