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 | * Print-related requests for DISCUSS. |
---|
10 | * |
---|
11 | * $Id: print.c,v 1.29 2006-03-13 21:39:56 amb Exp $ |
---|
12 | * |
---|
13 | */ |
---|
14 | |
---|
15 | |
---|
16 | #ifndef lint |
---|
17 | static char rcsid_discuss_c[] = |
---|
18 | "$Id: print.c,v 1.29 2006-03-13 21:39:56 amb Exp $"; |
---|
19 | #endif /* lint */ |
---|
20 | |
---|
21 | #include <stdio.h> |
---|
22 | #include <errno.h> |
---|
23 | #include <sys/file.h> |
---|
24 | #include <signal.h> |
---|
25 | #include <string.h> |
---|
26 | #if HAVE_SYS_WAIT_H |
---|
27 | #include <sys/wait.h> |
---|
28 | #endif |
---|
29 | #if HAVE_FCNTL_H |
---|
30 | #include <fcntl.h> |
---|
31 | #endif |
---|
32 | #include <discuss/discuss.h> |
---|
33 | #include <ss/ss.h> |
---|
34 | #include "config.h" |
---|
35 | #include "globals.h" |
---|
36 | |
---|
37 | #ifdef lint |
---|
38 | #define USE(var) var=var; |
---|
39 | #else /* lint */ |
---|
40 | #define USE(var) ; |
---|
41 | #endif /* lint */ |
---|
42 | |
---|
43 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
---|
44 | |
---|
45 | extern tfile unix_tfile(); |
---|
46 | static trn_nums performed; |
---|
47 | static char * request_name; |
---|
48 | static trn_info t_info; |
---|
49 | static tfile tf; |
---|
50 | |
---|
51 | extern void sl_free(); |
---|
52 | |
---|
53 | static int |
---|
54 | display_trans(t_infop, codep) |
---|
55 | trn_info3 *t_infop; |
---|
56 | int *codep; |
---|
57 | { |
---|
58 | output_trans(t_infop, tf, codep); |
---|
59 | if (*codep == 0) { |
---|
60 | dsc_public.highest_seen = max(dsc_public.highest_seen,t_infop->current); |
---|
61 | dsc_public.current = t_infop->current; |
---|
62 | performed = TRUE; |
---|
63 | } else if (*codep == EPIPE) { |
---|
64 | dsc_public.current = t_infop->current; |
---|
65 | performed = TRUE; |
---|
66 | return; /* silently quit */ |
---|
67 | } |
---|
68 | else if (*codep != DELETED_TRN) { |
---|
69 | fprintf(stderr, "Error printing transaction: %s\n", |
---|
70 | error_message(*codep)); |
---|
71 | return; |
---|
72 | } |
---|
73 | |
---|
74 | *codep = 0; |
---|
75 | return; |
---|
76 | } |
---|
77 | |
---|
78 | prt_trans(argc, argv) |
---|
79 | int argc; |
---|
80 | char **argv; |
---|
81 | { |
---|
82 | int fd; |
---|
83 | #if !HAVE_SIGACTION |
---|
84 | int (*old_sig)(); |
---|
85 | #endif |
---|
86 | int code; |
---|
87 | selection_list *trn_list; |
---|
88 | #if HAVE_SIGACTION |
---|
89 | struct sigaction act, oact; |
---|
90 | #endif |
---|
91 | |
---|
92 | request_name = ss_name(sci_idx); |
---|
93 | |
---|
94 | if (argc != 1) { |
---|
95 | if (strcmp(argv[0], "print") && |
---|
96 | strcmp(argv[0], "show") && |
---|
97 | strcmp(argv[0], "pr") && |
---|
98 | strcmp(argv[0], "p")) { |
---|
99 | fprintf(stderr, "Usage: %s\n", argv[0]); |
---|
100 | return; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | if (!dsc_public.attending) { |
---|
105 | ss_perror(sci_idx, DISC_NO_MTG, ""); |
---|
106 | return; |
---|
107 | } |
---|
108 | dsc_destroy_mtg_info(&dsc_public.m_info); |
---|
109 | dsc_get_mtg_info(&dsc_public.nb, |
---|
110 | &dsc_public.m_info, &code); |
---|
111 | if (code != 0) { |
---|
112 | (void) ss_perror(sci_idx, code, "Can't get meeting info"); |
---|
113 | return; |
---|
114 | } |
---|
115 | |
---|
116 | dsc_get_trn_info(&dsc_public.nb, dsc_public.current, |
---|
117 | &t_info, &code); |
---|
118 | if (code == DELETED_TRN) { |
---|
119 | t_info.current = dsc_public.current; |
---|
120 | t_info.next = dsc_public.current+1; |
---|
121 | t_info.prev = dsc_public.current-1; |
---|
122 | } else if (code) |
---|
123 | t_info.current = 0; |
---|
124 | |
---|
125 | dsc_destroy_trn_info(&t_info); |
---|
126 | |
---|
127 | if (argc == 1) { |
---|
128 | char *ref; |
---|
129 | if (!strcmp(argv[0], "print") || |
---|
130 | !strcmp(argv[0], "show") || |
---|
131 | !strcmp(argv[0], "pr") || |
---|
132 | !strcmp(argv[0], "p")) { |
---|
133 | ref = "current"; |
---|
134 | } else ref = argv[0]; |
---|
135 | |
---|
136 | trn_list = trn_select(&t_info, ref, |
---|
137 | (selection_list *)NULL, &code); |
---|
138 | if (code) { |
---|
139 | ss_perror(sci_idx, code, ""); |
---|
140 | sl_free(trn_list); |
---|
141 | return; |
---|
142 | } |
---|
143 | } |
---|
144 | else if (argc == 2) { |
---|
145 | trn_list = trn_select(&t_info, argv[1], |
---|
146 | (selection_list *)NULL, &code); |
---|
147 | if (code) { |
---|
148 | ss_perror(sci_idx, code, ""); |
---|
149 | sl_free(trn_list); |
---|
150 | return; |
---|
151 | } |
---|
152 | } |
---|
153 | else { |
---|
154 | trn_list = (selection_list *)NULL; |
---|
155 | while (argv++, argc-- > 1) { |
---|
156 | trn_list = trn_select(&t_info, *argv, |
---|
157 | trn_list, &code); |
---|
158 | if (code) { |
---|
159 | ss_perror(sci_idx, code, *argv); |
---|
160 | sl_free(trn_list); |
---|
161 | return; |
---|
162 | } |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | performed = FALSE; |
---|
167 | /* |
---|
168 | * Ignore SIGPIPE from the pager |
---|
169 | */ |
---|
170 | #if HAVE_SIGACTION |
---|
171 | sigemptyset(&act.sa_mask); |
---|
172 | act.sa_flags = 0; |
---|
173 | act.sa_handler= (void (*)()) SIG_IGN; |
---|
174 | (void) sigaction(SIGPIPE, &act, &oact); |
---|
175 | #else |
---|
176 | old_sig = signal(SIGPIPE, SIG_IGN); |
---|
177 | #endif |
---|
178 | fd = ss_pager_create(); |
---|
179 | if (fd < 0) { |
---|
180 | ss_perror(sci_idx, errno, "Can't start pager"); |
---|
181 | return; |
---|
182 | } |
---|
183 | tf = unix_tfile(fd); |
---|
184 | (void) sl_map(display_trans, trn_list,FALSE); |
---|
185 | sl_free(trn_list); |
---|
186 | tclose(tf, &code); |
---|
187 | (void) close(fd); |
---|
188 | (void) tdestroy(tf); |
---|
189 | (void) wait((int *)0); |
---|
190 | #if HAVE_SIGACTION |
---|
191 | (void) sigaction (SIGPIPE, &oact, NULL); |
---|
192 | #else |
---|
193 | (void) signal(SIGPIPE, old_sig); |
---|
194 | #endif |
---|
195 | if (!performed) |
---|
196 | ss_perror(sci_idx, DISC_NO_TRN, ""); |
---|
197 | } |
---|
198 | |
---|
199 | write_trans(argc, argv) |
---|
200 | int argc; |
---|
201 | char **argv; |
---|
202 | { |
---|
203 | selection_list *trn_list; |
---|
204 | int fd; |
---|
205 | int code; |
---|
206 | char *arg, *filename; |
---|
207 | |
---|
208 | if (dsc_public.host == (char *)NULL) { |
---|
209 | ss_perror(sci_idx, DISC_NO_MTG, ""); |
---|
210 | return; |
---|
211 | } |
---|
212 | dsc_destroy_mtg_info(&dsc_public.m_info); |
---|
213 | dsc_get_mtg_info(&dsc_public.nb, |
---|
214 | &dsc_public.m_info, &code); |
---|
215 | if (code != 0) { |
---|
216 | (void) ss_perror(sci_idx, code, "Can't get meeting info"); |
---|
217 | return; |
---|
218 | } |
---|
219 | if (argc == 3) { |
---|
220 | arg = argv[1]; |
---|
221 | filename = argv[2]; |
---|
222 | } |
---|
223 | else if (argc == 2) { |
---|
224 | arg = "current"; |
---|
225 | filename = argv[1]; |
---|
226 | } |
---|
227 | else { |
---|
228 | (void) fprintf(stderr, |
---|
229 | "Usage: %s transaction_list filename\n", |
---|
230 | argv[0]); |
---|
231 | return; |
---|
232 | } |
---|
233 | trn_list = trn_select(&t_info, arg, (selection_list *)NULL, &code); |
---|
234 | if (code) { |
---|
235 | ss_perror(sci_idx, code, arg); |
---|
236 | sl_free(trn_list); |
---|
237 | return; |
---|
238 | } |
---|
239 | performed = FALSE; |
---|
240 | |
---|
241 | fd = open(filename, O_CREAT|O_APPEND|O_WRONLY, 0666); |
---|
242 | if (fd < 0) { |
---|
243 | ss_perror(sci_idx, errno, "Can't open output file"); |
---|
244 | return; |
---|
245 | } |
---|
246 | tf = unix_tfile(fd); |
---|
247 | (void) sl_map(display_trans, trn_list, FALSE); |
---|
248 | sl_free(trn_list); |
---|
249 | tclose(tf, &code); |
---|
250 | (void) close(fd); |
---|
251 | (void) tdestroy(tf); |
---|
252 | if (!performed) |
---|
253 | ss_perror(sci_idx, DISC_NO_TRN, ""); |
---|
254 | return; |
---|
255 | } |
---|