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.28 2006-03-10 07:11:31 ghudson Exp $ |
---|
12 | * |
---|
13 | */ |
---|
14 | |
---|
15 | |
---|
16 | #ifndef lint |
---|
17 | static char rcsid_discuss_c[] = |
---|
18 | "$Id: print.c,v 1.28 2006-03-10 07:11:31 ghudson 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], "pr") && |
---|
97 | strcmp(argv[0], "p")) { |
---|
98 | fprintf(stderr, "Usage: %s\n", argv[0]); |
---|
99 | return; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | if (!dsc_public.attending) { |
---|
104 | ss_perror(sci_idx, DISC_NO_MTG, ""); |
---|
105 | return; |
---|
106 | } |
---|
107 | dsc_destroy_mtg_info(&dsc_public.m_info); |
---|
108 | dsc_get_mtg_info(&dsc_public.nb, |
---|
109 | &dsc_public.m_info, &code); |
---|
110 | if (code != 0) { |
---|
111 | (void) ss_perror(sci_idx, code, "Can't get meeting info"); |
---|
112 | return; |
---|
113 | } |
---|
114 | |
---|
115 | dsc_get_trn_info(&dsc_public.nb, dsc_public.current, |
---|
116 | &t_info, &code); |
---|
117 | if (code == DELETED_TRN) { |
---|
118 | t_info.current = dsc_public.current; |
---|
119 | t_info.next = dsc_public.current+1; |
---|
120 | t_info.prev = dsc_public.current-1; |
---|
121 | } else if (code) |
---|
122 | t_info.current = 0; |
---|
123 | |
---|
124 | dsc_destroy_trn_info(&t_info); |
---|
125 | |
---|
126 | if (argc == 1) { |
---|
127 | char *ref; |
---|
128 | if (!strcmp(argv[0], "print") || |
---|
129 | !strcmp(argv[0], "pr") || |
---|
130 | !strcmp(argv[0], "p")) { |
---|
131 | ref = "current"; |
---|
132 | } else ref = argv[0]; |
---|
133 | |
---|
134 | trn_list = trn_select(&t_info, ref, |
---|
135 | (selection_list *)NULL, &code); |
---|
136 | if (code) { |
---|
137 | ss_perror(sci_idx, code, ""); |
---|
138 | sl_free(trn_list); |
---|
139 | return; |
---|
140 | } |
---|
141 | } |
---|
142 | else if (argc == 2) { |
---|
143 | trn_list = trn_select(&t_info, argv[1], |
---|
144 | (selection_list *)NULL, &code); |
---|
145 | if (code) { |
---|
146 | ss_perror(sci_idx, code, ""); |
---|
147 | sl_free(trn_list); |
---|
148 | return; |
---|
149 | } |
---|
150 | } |
---|
151 | else { |
---|
152 | trn_list = (selection_list *)NULL; |
---|
153 | while (argv++, argc-- > 1) { |
---|
154 | trn_list = trn_select(&t_info, *argv, |
---|
155 | trn_list, &code); |
---|
156 | if (code) { |
---|
157 | ss_perror(sci_idx, code, *argv); |
---|
158 | sl_free(trn_list); |
---|
159 | return; |
---|
160 | } |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | performed = FALSE; |
---|
165 | /* |
---|
166 | * Ignore SIGPIPE from the pager |
---|
167 | */ |
---|
168 | #if HAVE_SIGACTION |
---|
169 | sigemptyset(&act.sa_mask); |
---|
170 | act.sa_flags = 0; |
---|
171 | act.sa_handler= (void (*)()) SIG_IGN; |
---|
172 | (void) sigaction(SIGPIPE, &act, &oact); |
---|
173 | #else |
---|
174 | old_sig = signal(SIGPIPE, SIG_IGN); |
---|
175 | #endif |
---|
176 | fd = ss_pager_create(); |
---|
177 | if (fd < 0) { |
---|
178 | ss_perror(sci_idx, errno, "Can't start pager"); |
---|
179 | return; |
---|
180 | } |
---|
181 | tf = unix_tfile(fd); |
---|
182 | (void) sl_map(display_trans, trn_list,FALSE); |
---|
183 | sl_free(trn_list); |
---|
184 | tclose(tf, &code); |
---|
185 | (void) close(fd); |
---|
186 | (void) tdestroy(tf); |
---|
187 | (void) wait((int *)0); |
---|
188 | #if HAVE_SIGACTION |
---|
189 | (void) sigaction (SIGPIPE, &oact, NULL); |
---|
190 | #else |
---|
191 | (void) signal(SIGPIPE, old_sig); |
---|
192 | #endif |
---|
193 | if (!performed) |
---|
194 | ss_perror(sci_idx, DISC_NO_TRN, ""); |
---|
195 | } |
---|
196 | |
---|
197 | write_trans(argc, argv) |
---|
198 | int argc; |
---|
199 | char **argv; |
---|
200 | { |
---|
201 | selection_list *trn_list; |
---|
202 | int fd; |
---|
203 | int code; |
---|
204 | char *arg, *filename; |
---|
205 | |
---|
206 | if (dsc_public.host == (char *)NULL) { |
---|
207 | ss_perror(sci_idx, DISC_NO_MTG, ""); |
---|
208 | return; |
---|
209 | } |
---|
210 | dsc_destroy_mtg_info(&dsc_public.m_info); |
---|
211 | dsc_get_mtg_info(&dsc_public.nb, |
---|
212 | &dsc_public.m_info, &code); |
---|
213 | if (code != 0) { |
---|
214 | (void) ss_perror(sci_idx, code, "Can't get meeting info"); |
---|
215 | return; |
---|
216 | } |
---|
217 | if (argc == 3) { |
---|
218 | arg = argv[1]; |
---|
219 | filename = argv[2]; |
---|
220 | } |
---|
221 | else if (argc == 2) { |
---|
222 | arg = "current"; |
---|
223 | filename = argv[1]; |
---|
224 | } |
---|
225 | else { |
---|
226 | (void) fprintf(stderr, |
---|
227 | "Usage: %s transaction_list filename\n", |
---|
228 | argv[0]); |
---|
229 | return; |
---|
230 | } |
---|
231 | trn_list = trn_select(&t_info, arg, (selection_list *)NULL, &code); |
---|
232 | if (code) { |
---|
233 | ss_perror(sci_idx, code, arg); |
---|
234 | sl_free(trn_list); |
---|
235 | return; |
---|
236 | } |
---|
237 | performed = FALSE; |
---|
238 | |
---|
239 | fd = open(filename, O_CREAT|O_APPEND|O_WRONLY, 0666); |
---|
240 | if (fd < 0) { |
---|
241 | ss_perror(sci_idx, errno, "Can't open output file"); |
---|
242 | return; |
---|
243 | } |
---|
244 | tf = unix_tfile(fd); |
---|
245 | (void) sl_map(display_trans, trn_list, FALSE); |
---|
246 | sl_free(trn_list); |
---|
247 | tclose(tf, &code); |
---|
248 | (void) close(fd); |
---|
249 | (void) tdestroy(tf); |
---|
250 | if (!performed) |
---|
251 | ss_perror(sci_idx, DISC_NO_TRN, ""); |
---|
252 | return; |
---|
253 | } |
---|