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 | * List request for DISCUSS |
---|
11 | * |
---|
12 | * $Id: list.c,v 1.36 2006-03-10 07:11:31 ghudson Exp $ |
---|
13 | * |
---|
14 | */ |
---|
15 | #ifndef lint |
---|
16 | static char rcsid_discuss_c[] = |
---|
17 | "$Id: list.c,v 1.36 2006-03-10 07:11:31 ghudson Exp $"; |
---|
18 | #endif /* lint */ |
---|
19 | |
---|
20 | #include <stdio.h> |
---|
21 | #include <stdlib.h> |
---|
22 | #include <string.h> |
---|
23 | #include <sys/param.h> /* for MIN() */ |
---|
24 | #include <ss/ss.h> |
---|
25 | #include <sys/ioctl.h> |
---|
26 | #include <termios.h> |
---|
27 | #include "config.h" |
---|
28 | #include <discuss/discuss.h> |
---|
29 | #include "globals.h" |
---|
30 | |
---|
31 | char *local_realm(), *short_time(); |
---|
32 | static trn_info3 t_info; |
---|
33 | static list_it(),delete_it(),retrieve_it(); |
---|
34 | static int performed; /* true if trn was acted upon */ |
---|
35 | static int barred; /* true if access was denied sometime */ |
---|
36 | static int long_subjects; |
---|
37 | static int screen_width = 80; |
---|
38 | static int setting; /* Whether we are turning flag on or off */ |
---|
39 | |
---|
40 | void map_trns(); |
---|
41 | #ifndef MIN |
---|
42 | #define MIN(a, b) ((a) > (b) ? (b) : (a)) |
---|
43 | #endif |
---|
44 | static list_it(t_infop, codep) |
---|
45 | trn_info3 *t_infop; |
---|
46 | int *codep; |
---|
47 | { |
---|
48 | char newtime[26], nlines[10]; |
---|
49 | char *cp,*author; |
---|
50 | int len; |
---|
51 | |
---|
52 | if (*codep == NO_ACCESS) { |
---|
53 | *codep = 0; |
---|
54 | barred = TRUE; |
---|
55 | goto punt; |
---|
56 | } |
---|
57 | else if ((*codep != 0) && (*codep != DELETED_TRN)) { |
---|
58 | ss_perror(sci_idx, *codep, |
---|
59 | "Can't read transaction info"); |
---|
60 | goto punt; |
---|
61 | } |
---|
62 | |
---|
63 | *codep = 0; |
---|
64 | |
---|
65 | if (!performed) { |
---|
66 | performed = TRUE; |
---|
67 | dsc_public.current = t_infop->current; /* current = first */ |
---|
68 | } |
---|
69 | |
---|
70 | strcpy(newtime, short_time(&t_infop->date_entered)); |
---|
71 | if (t_infop-> signature != NULL && *(t_infop->signature) != '\0') |
---|
72 | author = t_infop -> signature; |
---|
73 | else |
---|
74 | author = t_infop -> author; |
---|
75 | |
---|
76 | /* |
---|
77 | * If author/signature ends with current realm, punt the realm. |
---|
78 | */ |
---|
79 | if ((cp=strchr(author, '@')) != NULL) |
---|
80 | if (!strcmp(cp+1, local_realm())) |
---|
81 | *cp = '\0'; |
---|
82 | |
---|
83 | (void) sprintf (nlines, "(%d)", t_infop->num_lines); |
---|
84 | (void) sprintf (buffer, " [%04d]%s%c", |
---|
85 | t_infop->current, |
---|
86 | ((t_infop->flags & TRN_FLAG1) != 0) ? "F" : "", |
---|
87 | (t_infop->current == dsc_public.current) ? '*' : ' '); |
---|
88 | if ((len = MIN(5, 13-strlen (buffer)) - strlen (nlines)) > 0) |
---|
89 | (void) strncat (buffer, " ", len); |
---|
90 | |
---|
91 | if (strlen(author) > 15) |
---|
92 | (void) strcpy(&author[12], "..."); |
---|
93 | |
---|
94 | (void) sprintf (buffer + strlen (buffer), "%s %s %-15s ", |
---|
95 | nlines, newtime, author); |
---|
96 | len = screen_width - 1 - strlen (buffer); |
---|
97 | |
---|
98 | if (!long_subjects && strlen (t_infop->subject) > len && len >= 3) |
---|
99 | (void) strcpy (&t_infop->subject [len - 3], "..."); |
---|
100 | |
---|
101 | (void) printf ("%s%s\n", buffer, t_infop->subject); |
---|
102 | |
---|
103 | punt: |
---|
104 | return; |
---|
105 | } |
---|
106 | |
---|
107 | list (argc, argv, sci_idx) |
---|
108 | int argc, sci_idx; |
---|
109 | char **argv; |
---|
110 | { |
---|
111 | char **ap, **ap2, **nargv; |
---|
112 | int ac; |
---|
113 | #ifdef TIOCGWINSZ |
---|
114 | struct winsize wsz; |
---|
115 | |
---|
116 | /* Determine screen width. */ |
---|
117 | if (ioctl(0, TIOCGWINSZ, &wsz) == 0 && wsz.ws_col > 0) |
---|
118 | screen_width = wsz.ws_col; |
---|
119 | #endif |
---|
120 | |
---|
121 | long_subjects = 0; |
---|
122 | for (ap = argv; *ap; ap++) |
---|
123 | ; |
---|
124 | nargv = (char **) calloc (ap - argv + 1, sizeof (char *)); |
---|
125 | ac = 0; |
---|
126 | for (ap = argv, ap2 = nargv; *ap; ap++) { |
---|
127 | if (!strcmp (*ap, "-long_subjects") || !strcmp (*ap, "-lsj")) |
---|
128 | long_subjects = 1; |
---|
129 | else |
---|
130 | *ap2++ = *ap, ac++; |
---|
131 | } |
---|
132 | *ap2 = (char *) NULL; |
---|
133 | map_trns(ac, nargv, "all", list_it, FALSE); |
---|
134 | free (nargv); |
---|
135 | return; |
---|
136 | } |
---|
137 | |
---|
138 | static delete_it (t_infop, codep) |
---|
139 | trn_info3 *t_infop; |
---|
140 | int *codep; |
---|
141 | { |
---|
142 | if (*codep == DELETED_TRN) { /* Already deleted, done */ |
---|
143 | *codep = 0; |
---|
144 | return; |
---|
145 | } |
---|
146 | |
---|
147 | dsc_delete_trn(&dsc_public.nb, t_infop->current, codep); |
---|
148 | if (*codep == NO_ACCESS) { |
---|
149 | barred = TRUE; |
---|
150 | } else if (*codep == 0) { |
---|
151 | performed = TRUE; |
---|
152 | } else { |
---|
153 | (void) fprintf(stderr, "Error deleting transaction %d: %s\n", |
---|
154 | t_infop->current, error_message(*codep)); |
---|
155 | if (*codep != EXPUNGED_TRN) |
---|
156 | return; /* stop now */ |
---|
157 | } |
---|
158 | |
---|
159 | *codep = 0; |
---|
160 | return; |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | del_trans(argc, argv) |
---|
165 | int argc; |
---|
166 | char **argv; |
---|
167 | { |
---|
168 | map_trns(argc, argv, "current", delete_it, FALSE); |
---|
169 | dsc_public.current = 0; |
---|
170 | return; |
---|
171 | } |
---|
172 | |
---|
173 | static retrieve_it (t_infop, codep) |
---|
174 | trn_info3 *t_infop; |
---|
175 | int *codep; |
---|
176 | { |
---|
177 | if (*codep == 0) { /* Transaction exists */ |
---|
178 | return; |
---|
179 | } |
---|
180 | |
---|
181 | dsc_retrieve_trn(&dsc_public.nb, t_infop->current, codep); |
---|
182 | if (*codep == NO_ACCESS) { |
---|
183 | barred = TRUE; |
---|
184 | } else if (*codep == 0) { |
---|
185 | performed = TRUE; |
---|
186 | dsc_public.current = t_infop->current; |
---|
187 | } else { |
---|
188 | (void) fprintf(stderr, "Error retrieving transaction %d: %s\n", |
---|
189 | t_infop->current, error_message(*codep)); |
---|
190 | return; |
---|
191 | } |
---|
192 | *codep = 0; |
---|
193 | return; |
---|
194 | } |
---|
195 | |
---|
196 | ret_trans(argc, argv) |
---|
197 | int argc; |
---|
198 | char **argv; |
---|
199 | { |
---|
200 | map_trns(argc, argv, "current", retrieve_it, TRUE); |
---|
201 | return; |
---|
202 | } |
---|
203 | |
---|
204 | void map_trns(argc, argv, defalt, proc, filter_flags) |
---|
205 | int argc; |
---|
206 | char **argv; |
---|
207 | char *defalt; |
---|
208 | int (*proc)(); |
---|
209 | int filter_flags; |
---|
210 | { |
---|
211 | int i, code; |
---|
212 | selection_list *trn_list; |
---|
213 | |
---|
214 | if (!dsc_public.attending) { |
---|
215 | ss_perror(sci_idx, 0, "No current meeting.\n"); |
---|
216 | return; |
---|
217 | } |
---|
218 | dsc_destroy_mtg_info(&dsc_public.m_info); |
---|
219 | dsc_get_mtg_info(&dsc_public.nb, |
---|
220 | &dsc_public.m_info, &code); |
---|
221 | if (code != 0) { |
---|
222 | (void) ss_perror(sci_idx, code, "Can't get meeting info"); |
---|
223 | return; |
---|
224 | } |
---|
225 | |
---|
226 | dsc_get_trn_info (&dsc_public.nb, dsc_public.current, |
---|
227 | &t_info, &code); |
---|
228 | if (code != 0) |
---|
229 | t_info.current = 0; |
---|
230 | |
---|
231 | dsc_destroy_trn_info(&t_info); /* Get rid of dynamic stuff */ |
---|
232 | |
---|
233 | trn_list = (selection_list *)NULL; |
---|
234 | for (i = 1; i < argc; i++) { |
---|
235 | if (!strcmp(argv[i], "-initial")) |
---|
236 | filter_flags |= filter_ONLY_INITIAL; |
---|
237 | else if (!strcmp (argv[i], "-terminal")) |
---|
238 | filter_flags |= filter_ONLY_TERMINAL; |
---|
239 | else if ((!strcmp (argv[i], "-include_deleted")) || |
---|
240 | (!strcmp (argv[i], "-idl"))) |
---|
241 | filter_flags |= filter_INCLUDE_DELETED; |
---|
242 | else if ((!strcmp (argv[i], "-only_deleted")) || |
---|
243 | (!strcmp (argv[i], "-odl"))) |
---|
244 | filter_flags |= filter_ONLY_DELETED; |
---|
245 | else if ((!strcmp (argv[i], "-flag_set")) || |
---|
246 | (!strcmp (argv[i], "-flag"))) |
---|
247 | filter_flags |= filter_FLAG_SET; |
---|
248 | else if (!strcmp (argv[i], "-flag_reset")) |
---|
249 | filter_flags |= filter_FLAG_RESET; |
---|
250 | /* someday we'll have abbrevs */ |
---|
251 | else if (!strcmp (argv[i], "-no_terminal")) |
---|
252 | filter_flags &= ~filter_ONLY_TERMINAL; |
---|
253 | else if ((!strcmp (argv[i], "-no_include_deleted")) || |
---|
254 | (!strcmp (argv[i], "-nidl"))) |
---|
255 | filter_flags &= ~filter_INCLUDE_DELETED; |
---|
256 | else if ((!strcmp (argv[i], "-no_only_deleted")) || |
---|
257 | (!strcmp (argv[i], "-nodl"))) |
---|
258 | filter_flags &= ~filter_ONLY_DELETED; |
---|
259 | else if ((!strcmp (argv[i], "-no_flag_set")) || |
---|
260 | (!strcmp (argv[i], "-no_flag"))) |
---|
261 | filter_flags &= ~filter_FLAG_SET; |
---|
262 | else if (!strcmp (argv[i], "-no_flag_reset")) |
---|
263 | filter_flags &= ~filter_FLAG_RESET; |
---|
264 | else { |
---|
265 | trn_list = trn_select(&t_info, argv[i], |
---|
266 | trn_list, &code); |
---|
267 | if (code) { |
---|
268 | sprintf (buffer, "``%s''", argv[i]); |
---|
269 | ss_perror(sci_idx, code, buffer); |
---|
270 | sl_free(trn_list); |
---|
271 | return; |
---|
272 | } |
---|
273 | } |
---|
274 | } |
---|
275 | if (trn_list == (selection_list *)NULL) { |
---|
276 | trn_list = trn_select(&t_info, defalt, |
---|
277 | (selection_list *)NULL, &code); |
---|
278 | if (code) { |
---|
279 | ss_perror(sci_idx, code, defalt); |
---|
280 | sl_free(trn_list); |
---|
281 | return; |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | performed = FALSE; |
---|
286 | barred = FALSE; |
---|
287 | |
---|
288 | (void) sl_map(proc, trn_list, filter_flags); |
---|
289 | sl_free(trn_list); |
---|
290 | if (!performed) |
---|
291 | ss_perror(sci_idx, barred ? NO_ACCESS : DISC_NO_TRN, ""); |
---|
292 | } |
---|
293 | |
---|
294 | static flag_it(t_infop, codep) |
---|
295 | trn_info3 *t_infop; |
---|
296 | int *codep; |
---|
297 | { |
---|
298 | if (*codep == DELETED_TRN) { |
---|
299 | *codep = 0; |
---|
300 | return; |
---|
301 | } else if (*codep == NO_ACCESS) { |
---|
302 | *codep = 0; |
---|
303 | barred = TRUE; |
---|
304 | return; |
---|
305 | } else if (*codep != 0) { |
---|
306 | ss_perror(sci_idx, *codep, |
---|
307 | "Can't read transaction info"); |
---|
308 | return; |
---|
309 | } |
---|
310 | |
---|
311 | if (setting) |
---|
312 | t_infop->flags |= TRN_FLAG1; |
---|
313 | else |
---|
314 | t_infop->flags &= ~TRN_FLAG1; |
---|
315 | |
---|
316 | dsc_set_trn_flags(&dsc_public.nb, t_infop->current, t_infop->flags, codep); |
---|
317 | if (*codep == NO_ACCESS) { |
---|
318 | barred = TRUE; |
---|
319 | } else if (*codep == 0) { |
---|
320 | performed = TRUE; |
---|
321 | } else { |
---|
322 | (void) fprintf(stderr, "Error setting flags for transaction %d: %s\n", |
---|
323 | t_infop->current, error_message(*codep)); |
---|
324 | return; |
---|
325 | } |
---|
326 | *codep = 0; |
---|
327 | |
---|
328 | return; |
---|
329 | } |
---|
330 | |
---|
331 | int switch_cmd(argc, argv) |
---|
332 | int argc; |
---|
333 | char **argv; |
---|
334 | { |
---|
335 | |
---|
336 | if (argc < 2) { |
---|
337 | goto usage; |
---|
338 | } |
---|
339 | |
---|
340 | if (!strcmp(argv[0], "switch_on") || !strcmp(argv[0], "swn")) |
---|
341 | setting = TRUE; |
---|
342 | else |
---|
343 | setting = FALSE; |
---|
344 | |
---|
345 | if (strcmp(argv[1], "flag")) |
---|
346 | goto usage; |
---|
347 | argc--,argv++; |
---|
348 | map_trns(argc, argv, "current", flag_it, FALSE); |
---|
349 | return; |
---|
350 | |
---|
351 | usage: |
---|
352 | (void) fprintf(stderr, "Usage: %s flag [trn_specs]\n",argv[0]); |
---|
353 | return; |
---|
354 | } |
---|