1 | /* |
---|
2 | * Copyright 2001 Massachusetts Institute of Technology |
---|
3 | * |
---|
4 | * Permission to use, copy, modify, and distribute this software and |
---|
5 | * its documentation for any purpose and without fee is hereby |
---|
6 | * granted, provided that the above copyright notice and this |
---|
7 | * permission notice appear in all copies and in supporting |
---|
8 | * documentation. No representation is made about the suitability of |
---|
9 | * this software for any purpose. It is provided "as is" without |
---|
10 | * express or implied warranty. |
---|
11 | */ |
---|
12 | |
---|
13 | #include <stdio.h> |
---|
14 | #include <unistd.h> |
---|
15 | #include <stdlib.h> |
---|
16 | #include <string.h> |
---|
17 | #include <signal.h> |
---|
18 | #include <time.h> |
---|
19 | #include <sys/param.h> |
---|
20 | #include <sys/types.h> |
---|
21 | #include <sys/stat.h> |
---|
22 | #include "owl.h" |
---|
23 | |
---|
24 | #if OWL_STDERR_REDIR |
---|
25 | #ifdef HAVE_SYS_IOCTL_H |
---|
26 | #include <sys/ioctl.h> |
---|
27 | #endif |
---|
28 | #ifdef HAVE_SYS_FILIO_H |
---|
29 | #include <sys/filio.h> |
---|
30 | #endif |
---|
31 | int stderr_replace(void); |
---|
32 | void stderr_redirect(int rfd); |
---|
33 | #endif |
---|
34 | |
---|
35 | static const char fileIdent[] = "$Id$"; |
---|
36 | |
---|
37 | owl_global g; |
---|
38 | |
---|
39 | int main(int argc, char **argv, char **env) { |
---|
40 | WINDOW *recwin, *sepwin, *typwin, *msgwin; |
---|
41 | owl_editwin *tw; |
---|
42 | owl_popwin *pw; |
---|
43 | int j, ret, initialsubs, debug, argcsave, followlast; |
---|
44 | int newmsgs, zpendcount, nexttimediff; |
---|
45 | struct sigaction sigact; |
---|
46 | char *configfile, *tty, *perlout, *perlerr, **argvsave, buff[LINE], startupmsg[LINE]; |
---|
47 | owl_filter *f; |
---|
48 | owl_style *s; |
---|
49 | time_t nexttime, now; |
---|
50 | struct tm *today; |
---|
51 | char *dir; |
---|
52 | #ifdef HAVE_LIBZEPHYR |
---|
53 | ZNotice_t notice; |
---|
54 | #endif |
---|
55 | #if OWL_STDERR_REDIR |
---|
56 | int newstderr; |
---|
57 | #endif |
---|
58 | |
---|
59 | argcsave=argc; |
---|
60 | argvsave=argv; |
---|
61 | configfile=NULL; |
---|
62 | tty=NULL; |
---|
63 | debug=0; |
---|
64 | initialsubs=1; |
---|
65 | if (argc>0) { |
---|
66 | argv++; |
---|
67 | argc--; |
---|
68 | } |
---|
69 | while (argc>0) { |
---|
70 | if (!strcmp(argv[0], "-n")) { |
---|
71 | initialsubs=0; |
---|
72 | argv++; |
---|
73 | argc--; |
---|
74 | } else if (!strcmp(argv[0], "-c")) { |
---|
75 | if (argc<2) { |
---|
76 | fprintf(stderr, "Too few arguments to -c\n"); |
---|
77 | usage(); |
---|
78 | exit(1); |
---|
79 | } |
---|
80 | configfile=argv[1]; |
---|
81 | argv+=2; |
---|
82 | argc-=2; |
---|
83 | } else if (!strcmp(argv[0], "-t")) { |
---|
84 | if (argc<2) { |
---|
85 | fprintf(stderr, "Too few arguments to -t\n"); |
---|
86 | usage(); |
---|
87 | exit(1); |
---|
88 | } |
---|
89 | tty=argv[1]; |
---|
90 | argv+=2; |
---|
91 | argc-=2; |
---|
92 | } else if (!strcmp(argv[0], "-d")) { |
---|
93 | debug=1; |
---|
94 | argv++; |
---|
95 | argc--; |
---|
96 | } else if (!strcmp(argv[0], "-v")) { |
---|
97 | printf("This is owl version %s\n", OWL_VERSION_STRING); |
---|
98 | exit(0); |
---|
99 | } else { |
---|
100 | fprintf(stderr, "Uknown argument\n"); |
---|
101 | usage(); |
---|
102 | exit(1); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | #ifdef HAVE_LIBZEPHYR |
---|
107 | /* zephyr init */ |
---|
108 | ret=owl_zephyr_initialize(); |
---|
109 | if (ret) { |
---|
110 | exit(1); |
---|
111 | } |
---|
112 | #endif |
---|
113 | |
---|
114 | /* signal handler */ |
---|
115 | sigact.sa_handler=sig_handler; |
---|
116 | sigemptyset(&sigact.sa_mask); |
---|
117 | sigact.sa_flags=0; |
---|
118 | sigaction(SIGWINCH, &sigact, NULL); |
---|
119 | sigaction(SIGALRM, &sigact, NULL); |
---|
120 | |
---|
121 | /* screen init */ |
---|
122 | sprintf(buff, "TERMINFO=%s", TERMINFO); |
---|
123 | putenv(buff); |
---|
124 | initscr(); |
---|
125 | start_color(); |
---|
126 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
127 | use_default_colors(); |
---|
128 | #endif |
---|
129 | raw(); |
---|
130 | noecho(); |
---|
131 | |
---|
132 | /* define simple color pairs */ |
---|
133 | if (has_colors() && COLOR_PAIRS>=8) { |
---|
134 | init_pair(OWL_COLOR_BLACK, COLOR_BLACK, -1); |
---|
135 | init_pair(OWL_COLOR_RED, COLOR_RED, -1); |
---|
136 | init_pair(OWL_COLOR_GREEN, COLOR_GREEN, -1); |
---|
137 | init_pair(OWL_COLOR_YELLOW, COLOR_YELLOW, -1); |
---|
138 | init_pair(OWL_COLOR_BLUE, COLOR_BLUE, -1); |
---|
139 | init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, -1); |
---|
140 | init_pair(OWL_COLOR_CYAN, COLOR_CYAN, -1); |
---|
141 | init_pair(OWL_COLOR_WHITE, COLOR_WHITE, -1); |
---|
142 | } |
---|
143 | |
---|
144 | /* owl global init */ |
---|
145 | owl_global_init(&g); |
---|
146 | if (debug) owl_global_set_debug_on(&g); |
---|
147 | owl_global_set_startupargs(&g, argcsave, argvsave); |
---|
148 | #ifdef HAVE_LIBZEPHYR |
---|
149 | owl_global_set_havezephyr(&g); |
---|
150 | #endif |
---|
151 | owl_global_set_haveaim(&g); |
---|
152 | |
---|
153 | #if OWL_STDERR_REDIR |
---|
154 | /* Do this only after we've started curses up... */ |
---|
155 | newstderr = stderr_replace(); |
---|
156 | #endif |
---|
157 | |
---|
158 | /* create the owl directory, in case it does not exist */ |
---|
159 | dir=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_CONFIG_DIR); |
---|
160 | mkdir(dir, S_IRWXU); |
---|
161 | owl_free(dir); |
---|
162 | |
---|
163 | /* set the tty, either from the command line, or by figuring it out */ |
---|
164 | if (tty) { |
---|
165 | owl_global_set_tty(&g, tty); |
---|
166 | } else { |
---|
167 | owl_global_set_tty(&g, owl_util_get_default_tty()); |
---|
168 | } |
---|
169 | |
---|
170 | /* setup the built-in styles */ |
---|
171 | s=owl_malloc(sizeof(owl_style)); |
---|
172 | owl_style_create_internal(s, "default", &owl_stylefunc_default, |
---|
173 | "Default message formatting"); |
---|
174 | owl_global_add_style(&g, s); |
---|
175 | |
---|
176 | s=owl_malloc(sizeof(owl_style)); |
---|
177 | owl_style_create_internal(s, "basic", &owl_stylefunc_basic, |
---|
178 | "Basic message formatting."); |
---|
179 | owl_global_add_style(&g, s); |
---|
180 | #if 0 |
---|
181 | s=owl_malloc(sizeof(owl_style)); |
---|
182 | owl_style_create_internal(s, "vt", &owl_stylefunc_vt, |
---|
183 | "VT message formatting."); |
---|
184 | owl_global_add_style(&g, s); |
---|
185 | #endif |
---|
186 | s=owl_malloc(sizeof(owl_style)); |
---|
187 | owl_style_create_internal(s, "oneline", &owl_stylefunc_oneline, |
---|
188 | "Formats for one-line-per-message"); |
---|
189 | owl_global_add_style(&g, s); |
---|
190 | |
---|
191 | /* setup the default filters */ |
---|
192 | /* the personal filter will need to change again when AIM chat's are |
---|
193 | * included. Also, there should be an %aimme% */ |
---|
194 | f=owl_malloc(sizeof(owl_filter)); |
---|
195 | owl_filter_init_fromstring(f, "personal", "( type ^zephyr$ " |
---|
196 | "and class ^message$ and instance ^personal$ " |
---|
197 | "and ( recipient ^%me%$ or sender ^%me%$ ) ) " |
---|
198 | "or ( type ^aim$ and login ^none$ )"); |
---|
199 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
200 | |
---|
201 | f=owl_malloc(sizeof(owl_filter)); |
---|
202 | owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )"); |
---|
203 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
204 | |
---|
205 | f=owl_malloc(sizeof(owl_filter)); |
---|
206 | owl_filter_init_fromstring(f, "ping", "opcode ^ping$"); |
---|
207 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
208 | |
---|
209 | f=owl_malloc(sizeof(owl_filter)); |
---|
210 | owl_filter_init_fromstring(f, "auto", "opcode ^auto$"); |
---|
211 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
212 | |
---|
213 | f=owl_malloc(sizeof(owl_filter)); |
---|
214 | owl_filter_init_fromstring(f, "login", "not login ^none$"); |
---|
215 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
216 | |
---|
217 | f=owl_malloc(sizeof(owl_filter)); |
---|
218 | owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$"); |
---|
219 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
220 | |
---|
221 | f=owl_malloc(sizeof(owl_filter)); |
---|
222 | owl_filter_init_fromstring(f, "out", "direction ^out$"); |
---|
223 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
224 | |
---|
225 | f=owl_malloc(sizeof(owl_filter)); |
---|
226 | owl_filter_init_fromstring(f, "aim", "type ^aim$"); |
---|
227 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
228 | |
---|
229 | f=owl_malloc(sizeof(owl_filter)); |
---|
230 | owl_filter_init_fromstring(f, "zephyr", "type ^zephyr$"); |
---|
231 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
232 | |
---|
233 | f=owl_malloc(sizeof(owl_filter)); |
---|
234 | owl_filter_init_fromstring(f, "none", "false"); |
---|
235 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
236 | |
---|
237 | f=owl_malloc(sizeof(owl_filter)); |
---|
238 | owl_filter_init_fromstring(f, "all", "true"); |
---|
239 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
240 | |
---|
241 | /* set the current view */ |
---|
242 | owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default")); |
---|
243 | |
---|
244 | /* AIM init */ |
---|
245 | owl_aim_init(); |
---|
246 | |
---|
247 | /* process the startup file */ |
---|
248 | owl_function_source(NULL); |
---|
249 | |
---|
250 | /* read the config file */ |
---|
251 | owl_context_set_readconfig(owl_global_get_context(&g)); |
---|
252 | perlerr=owl_perlconfig_readconfig(configfile); |
---|
253 | if (perlerr) { |
---|
254 | endwin(); |
---|
255 | fprintf(stderr, "\nError parsing configfile: %s\n", perlerr); |
---|
256 | exit(1); |
---|
257 | } |
---|
258 | |
---|
259 | /* if the config defines a formatting function, add 'perl' as a style */ |
---|
260 | if (owl_global_is_config_format(&g)) { |
---|
261 | owl_function_debugmsg("Found perl formatting"); |
---|
262 | s=owl_malloc(sizeof(owl_style)); |
---|
263 | owl_style_create_perl(s, "perl", "owl::_format_msg_legacy_wrap", |
---|
264 | "User-defined perl style that calls owl::format_msg" |
---|
265 | "with legacy global variable support"); |
---|
266 | owl_global_add_style(&g, s); |
---|
267 | owl_global_set_default_style(&g, "perl"); |
---|
268 | } |
---|
269 | |
---|
270 | /* execute the startup function in the configfile */ |
---|
271 | perlout = owl_perlconfig_execute("owl::startup();"); |
---|
272 | if (perlout) owl_free(perlout); |
---|
273 | owl_function_debugmsg("-- Owl Startup --"); |
---|
274 | |
---|
275 | /* hold on to the window names for convenience */ |
---|
276 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
277 | recwin=owl_global_get_curs_recwin(&g); |
---|
278 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
279 | typwin=owl_global_get_curs_typwin(&g); |
---|
280 | tw=owl_global_get_typwin(&g); |
---|
281 | |
---|
282 | /* welcome message */ |
---|
283 | strcpy(startupmsg, "-----------------------------------------------------------------------\n"); |
---|
284 | sprintf(buff, "Welcome to Owl version %s. Press 'h' for on-line help. \n", OWL_VERSION_STRING); |
---|
285 | strcat(startupmsg, buff); |
---|
286 | strcat(startupmsg, " \n"); |
---|
287 | strcat(startupmsg, "If you would like to receive release announcements about Owl you can \n"); |
---|
288 | strcat(startupmsg, "join the owl-users@mit.edu mailing at http://web.mit.edu/ktools/ \n"); |
---|
289 | strcat(startupmsg, " ^ ^ \n"); |
---|
290 | strcat(startupmsg, " OvO \n"); |
---|
291 | strcat(startupmsg, "Please report any bugs or suggestions to bug-owl@mit.edu ( ) \n"); |
---|
292 | strcat(startupmsg, "-----------------------------------------------------------------m-m---\n"); |
---|
293 | owl_function_adminmsg("", startupmsg); |
---|
294 | sepbar(NULL); |
---|
295 | |
---|
296 | wrefresh(sepwin); |
---|
297 | |
---|
298 | /* load zephyr subs */ |
---|
299 | if (initialsubs) { |
---|
300 | /* load normal subscriptions */ |
---|
301 | ret=owl_zephyr_loadsubs(NULL); |
---|
302 | if (ret==-1) { |
---|
303 | owl_function_adminmsg("", "Error loading zephyr subscriptions, file inaccessable"); |
---|
304 | } else if (ret!=0) { |
---|
305 | owl_function_adminmsg("", "Error loading zephyr subscriptions"); |
---|
306 | } |
---|
307 | |
---|
308 | if (ret!=-1) { |
---|
309 | owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES); |
---|
310 | } |
---|
311 | |
---|
312 | /* load login subscriptions */ |
---|
313 | if (owl_global_is_loginsubs(&g)) { |
---|
314 | owl_function_loadloginsubs(NULL); |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | /* set the startup and default style, based on userclue and presence of a |
---|
319 | * formatting function */ |
---|
320 | if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) { |
---|
321 | /* the style was set by the user: leave it alone */ |
---|
322 | } else if (owl_global_is_config_format(&g)) { |
---|
323 | owl_global_set_default_style(&g, "perl"); |
---|
324 | } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) { |
---|
325 | owl_global_set_default_style(&g, "default"); |
---|
326 | } else { |
---|
327 | owl_global_set_default_style(&g, "basic"); |
---|
328 | } |
---|
329 | |
---|
330 | /* zlog in if we need to */ |
---|
331 | if (owl_global_is_startuplogin(&g)) { |
---|
332 | owl_zephyr_zlog_in(); |
---|
333 | } |
---|
334 | |
---|
335 | owl_view_set_style(owl_global_get_current_view(&g), |
---|
336 | owl_global_get_style_by_name(&g, owl_global_get_default_style(&g))); |
---|
337 | |
---|
338 | owl_context_set_interactive(owl_global_get_context(&g)); |
---|
339 | |
---|
340 | nexttimediff=10; |
---|
341 | nexttime=time(NULL); |
---|
342 | |
---|
343 | /* main loop */ |
---|
344 | while (1) { |
---|
345 | |
---|
346 | /* if a resize has been scheduled, deal with it */ |
---|
347 | owl_global_resize(&g, 0, 0); |
---|
348 | |
---|
349 | /* these are here in case a resize changes the windows */ |
---|
350 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
351 | recwin=owl_global_get_curs_recwin(&g); |
---|
352 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
353 | typwin=owl_global_get_curs_typwin(&g); |
---|
354 | |
---|
355 | followlast=owl_global_should_followlast(&g); |
---|
356 | |
---|
357 | /* Do AIM stuff */ |
---|
358 | if (owl_global_is_doaimevents(&g)) { |
---|
359 | owl_aim_process_events(); |
---|
360 | |
---|
361 | if (owl_global_is_aimloggedin(&g)) { |
---|
362 | if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) { |
---|
363 | /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */ |
---|
364 | owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g)); |
---|
365 | } |
---|
366 | } |
---|
367 | } |
---|
368 | |
---|
369 | /* little hack */ |
---|
370 | now=time(NULL); |
---|
371 | today=localtime(&now); |
---|
372 | if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) { |
---|
373 | if (time(NULL)>nexttime) { |
---|
374 | if (nexttimediff==1) { |
---|
375 | nexttimediff=10; |
---|
376 | } else { |
---|
377 | nexttimediff=1; |
---|
378 | } |
---|
379 | nexttime+=nexttimediff; |
---|
380 | owl_hack_animate(); |
---|
381 | } |
---|
382 | } |
---|
383 | |
---|
384 | /* Grab incoming messages. */ |
---|
385 | newmsgs=0; |
---|
386 | zpendcount=0; |
---|
387 | while(owl_zephyr_zpending() || owl_global_messagequeue_pending(&g)) { |
---|
388 | #ifdef HAVE_LIBZEPHYR |
---|
389 | struct sockaddr_in from; |
---|
390 | #endif |
---|
391 | owl_message *m=NULL; |
---|
392 | owl_filter *f; |
---|
393 | |
---|
394 | /* grab the new message, stick it in 'm' */ |
---|
395 | if (owl_zephyr_zpending()) { |
---|
396 | #ifdef HAVE_LIBZEPHYR |
---|
397 | /* grab a zephyr notice, but if we've done 20 without stopping, |
---|
398 | take a break to process keystrokes etc. */ |
---|
399 | if (zpendcount>20) break; |
---|
400 | ZReceiveNotice(¬ice, &from); |
---|
401 | zpendcount++; |
---|
402 | |
---|
403 | /* is this an ack from a zephyr we sent? */ |
---|
404 | if (owl_zephyr_notice_is_ack(¬ice)) { |
---|
405 | owl_zephyr_handle_ack(¬ice); |
---|
406 | continue; |
---|
407 | } |
---|
408 | |
---|
409 | /* if it's a ping and we're not viewing pings then skip it */ |
---|
410 | if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) { |
---|
411 | continue; |
---|
412 | } |
---|
413 | |
---|
414 | /* create the new message */ |
---|
415 | m=owl_malloc(sizeof(owl_message)); |
---|
416 | owl_message_create_from_znotice(m, ¬ice); |
---|
417 | #endif |
---|
418 | } else if (owl_global_messagequeue_pending(&g)) { |
---|
419 | m=owl_global_messageuque_popmsg(&g); |
---|
420 | } |
---|
421 | |
---|
422 | /* if this message it on the puntlist, nuke it and continue */ |
---|
423 | if (owl_global_message_is_puntable(&g, m)) { |
---|
424 | owl_message_free(m); |
---|
425 | continue; |
---|
426 | } |
---|
427 | |
---|
428 | /* otherwise add it to the global list */ |
---|
429 | owl_messagelist_append_element(owl_global_get_msglist(&g), m); |
---|
430 | newmsgs=1; |
---|
431 | |
---|
432 | /* let the config know the new message has been received */ |
---|
433 | owl_perlconfig_getmsg(m, 0, NULL); |
---|
434 | |
---|
435 | /* add it to any necessary views; right now there's only the current view */ |
---|
436 | owl_view_consider_message(owl_global_get_current_view(&g), m); |
---|
437 | |
---|
438 | /* do we need to autoreply? */ |
---|
439 | if (owl_global_is_zaway(&g)) { |
---|
440 | if (owl_message_is_type_zephyr(m)) { |
---|
441 | owl_zephyr_zaway(m); |
---|
442 | } else if (owl_message_is_type_aim(m)) { |
---|
443 | /* can't do this, there's no mechanism not to start a loop */ |
---|
444 | /* owl_aim_send_im(owl_message_get_sender(m), owl_global_get_zaway_msg(&g)); */ |
---|
445 | } |
---|
446 | } |
---|
447 | |
---|
448 | /* ring the bell if it's a personal */ |
---|
449 | if (owl_global_is_personalbell(&g) && |
---|
450 | !owl_message_is_loginout(m) && |
---|
451 | !owl_message_is_mail(m) && |
---|
452 | owl_message_is_private(m)) { |
---|
453 | owl_function_beep(); |
---|
454 | } |
---|
455 | |
---|
456 | /* if it matches the alert filter, do the alert action */ |
---|
457 | f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g)); |
---|
458 | if (f && owl_filter_message_match(f, m)) { |
---|
459 | owl_function_command(owl_global_get_alert_action(&g)); |
---|
460 | } |
---|
461 | |
---|
462 | /* check for burning ears message */ |
---|
463 | /* this is an unsupported feature */ |
---|
464 | if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) { |
---|
465 | char *buff; |
---|
466 | buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m)); |
---|
467 | owl_function_adminmsg(buff, ""); |
---|
468 | owl_free(buff); |
---|
469 | owl_function_beep(); |
---|
470 | } |
---|
471 | |
---|
472 | /* log the message if we need to */ |
---|
473 | if (owl_global_is_logging(&g) || owl_global_is_classlogging(&g)) { |
---|
474 | owl_log_incoming(m); |
---|
475 | } |
---|
476 | } |
---|
477 | |
---|
478 | /* follow the last message if we're supposed to */ |
---|
479 | if (newmsgs && followlast) { |
---|
480 | owl_function_lastmsg_noredisplay(); |
---|
481 | } |
---|
482 | |
---|
483 | /* do the newmsgproc thing */ |
---|
484 | if (newmsgs) { |
---|
485 | owl_function_do_newmsgproc(); |
---|
486 | } |
---|
487 | |
---|
488 | /* redisplay if necessary */ |
---|
489 | /* this should be optimized to not run if the new messages won't be displayed */ |
---|
490 | if (newmsgs) { |
---|
491 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
492 | sepbar(NULL); |
---|
493 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
494 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
495 | /* TODO: this is a broken kludge */ |
---|
496 | if (owl_global_get_viewwin(&g)) { |
---|
497 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
498 | } |
---|
499 | } |
---|
500 | owl_global_set_needrefresh(&g); |
---|
501 | } |
---|
502 | |
---|
503 | /* if a popwin just came up, refresh it */ |
---|
504 | pw=owl_global_get_popwin(&g); |
---|
505 | if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) { |
---|
506 | owl_popwin_refresh(pw); |
---|
507 | owl_popwin_no_needs_first_refresh(pw); |
---|
508 | owl_global_set_needrefresh(&g); |
---|
509 | /* TODO: this is a broken kludge */ |
---|
510 | if (owl_global_get_viewwin(&g)) { |
---|
511 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
512 | } |
---|
513 | } |
---|
514 | |
---|
515 | /* update the terminal if we need to */ |
---|
516 | if (owl_global_is_needrefresh(&g)) { |
---|
517 | /* leave the cursor in the appropriate window */ |
---|
518 | if (owl_global_is_typwin_active(&g)) { |
---|
519 | owl_function_set_cursor(typwin); |
---|
520 | } else { |
---|
521 | owl_function_set_cursor(sepwin); |
---|
522 | } |
---|
523 | doupdate(); |
---|
524 | owl_global_set_noneedrefresh(&g); |
---|
525 | } |
---|
526 | |
---|
527 | /* Handle all keypresses. If no key has been pressed, sleep for a |
---|
528 | * little bit, but otherwise do not. This lets input be grabbed |
---|
529 | * as quickly as possbile */ |
---|
530 | j=wgetch(typwin); |
---|
531 | if (j==ERR) { |
---|
532 | usleep(10); |
---|
533 | continue; |
---|
534 | } |
---|
535 | /* find and activate the current keymap. |
---|
536 | * TODO: this should really get fixed by activating |
---|
537 | * keymaps as we switch between windows... |
---|
538 | */ |
---|
539 | if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) { |
---|
540 | owl_context_set_popless(owl_global_get_context(&g), |
---|
541 | owl_global_get_viewwin(&g)); |
---|
542 | owl_function_activate_keymap("popless"); |
---|
543 | } else if (owl_global_is_typwin_active(&g) |
---|
544 | && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) { |
---|
545 | /* |
---|
546 | owl_context_set_editline(owl_global_get_context(&g), tw); |
---|
547 | owl_function_activate_keymap("editline"); |
---|
548 | */ |
---|
549 | } else if (owl_global_is_typwin_active(&g) |
---|
550 | && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) { |
---|
551 | owl_context_set_editmulti(owl_global_get_context(&g), tw); |
---|
552 | owl_function_activate_keymap("editmulti"); |
---|
553 | } else { |
---|
554 | owl_context_set_recv(owl_global_get_context(&g)); |
---|
555 | owl_function_activate_keymap("recv"); |
---|
556 | } |
---|
557 | /* now actually handle the keypress */ |
---|
558 | ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j); |
---|
559 | if (ret!=0 && ret!=1) { |
---|
560 | owl_function_makemsg("Unable to handle keypress"); |
---|
561 | } |
---|
562 | |
---|
563 | #if OWL_STDERR_REDIR |
---|
564 | stderr_redirect(newstderr); |
---|
565 | #endif |
---|
566 | |
---|
567 | } |
---|
568 | } |
---|
569 | |
---|
570 | void sig_handler(int sig) { |
---|
571 | if (sig==SIGWINCH) { |
---|
572 | /* we can't inturrupt a malloc here, so it just sets a flag |
---|
573 | * schedulding a resize for later |
---|
574 | */ |
---|
575 | owl_function_resize(); |
---|
576 | } |
---|
577 | } |
---|
578 | |
---|
579 | void usage() { |
---|
580 | fprintf(stderr, "Owl version %s\n", OWL_VERSION_STRING); |
---|
581 | fprintf(stderr, "Usage: owl [-n] [-d] [-v] [-h] [-c <configfile>] [-t <ttyname>]\n"); |
---|
582 | fprintf(stderr, " -n don't load zephyr subscriptions\n"); |
---|
583 | fprintf(stderr, " -d enable debugging\n"); |
---|
584 | fprintf(stderr, " -v print the Owl version number and exit\n"); |
---|
585 | fprintf(stderr, " -h print this help message\n"); |
---|
586 | fprintf(stderr, " -c specify an alternate config file\n"); |
---|
587 | fprintf(stderr, " -t set the tty name\n"); |
---|
588 | } |
---|
589 | |
---|
590 | |
---|
591 | |
---|
592 | #if OWL_STDERR_REDIR |
---|
593 | |
---|
594 | /* Replaces stderr with a pipe so that we can read from it. |
---|
595 | * Returns the fd of the pipe from which stderr can be read. */ |
---|
596 | int stderr_replace(void) { |
---|
597 | int pipefds[2]; |
---|
598 | if (0 != pipe(pipefds)) { |
---|
599 | perror("pipe"); |
---|
600 | owl_function_debugmsg("stderr_replace: pipe FAILED\n"); |
---|
601 | return -1; |
---|
602 | } |
---|
603 | owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]); |
---|
604 | if (-1 == dup2(pipefds[1], 2 /*stderr*/)) { |
---|
605 | owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno)); |
---|
606 | perror("dup2"); |
---|
607 | return -1; |
---|
608 | } |
---|
609 | return pipefds[0]; |
---|
610 | } |
---|
611 | |
---|
612 | /* Sends stderr (read from rfd) messages to a file */ |
---|
613 | void stderr_redirect(int rfd) { |
---|
614 | int navail, bread; |
---|
615 | char *buf; |
---|
616 | /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/ |
---|
617 | if (rfd<0) return; |
---|
618 | if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) { |
---|
619 | return; |
---|
620 | } |
---|
621 | /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/ |
---|
622 | if (navail<=0) return; |
---|
623 | if (navail>256) { navail = 256; } |
---|
624 | buf = owl_malloc(navail+1); |
---|
625 | bread = read(rfd, buf, navail); |
---|
626 | if (buf[navail-1] != '\0') { |
---|
627 | buf[navail] = '\0'; |
---|
628 | } |
---|
629 | owl_function_error("Err: %s", buf); |
---|
630 | owl_free(buf); |
---|
631 | } |
---|
632 | |
---|
633 | #endif /* OWL_STDERR_REDIR */ |
---|