source: owl.c @ e7dc035

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since e7dc035 was 124aebc, checked in by Nelson Elhage <nelhage@mit.edu>, 18 years ago
Adding owl_obarray for interning strings
  • Property mode set to 100644
File size: 25.4 KB
Line 
1/*  Copyright (c) 2004 James Kretchmar. All rights reserved.
2 *
3 *  Redistribution and use in source and binary forms, with or without
4 *  modification, are permitted provided that the following conditions are
5 *  met:
6 * 
7 *    * Redistributions of source code must retain the above copyright
8 *      notice, this list of conditions and the following disclaimer.
9 * 
10 *    * Redistributions in binary form must reproduce the above copyright
11 *      notice, this list of conditions and the following disclaimer in
12 *      the documentation and/or other materials provided with the
13 *      distribution.
14 * 
15 *    * Redistributions in any form must be accompanied by information on
16 *      how to obtain complete source code for the Owl software and any
17 *      accompanying software that uses the Owl software. The source code
18 *      must either be included in the distribution or be available for no
19 *      more than the cost of distribution plus a nominal fee, and must be
20 *      freely redistributable under reasonable conditions. For an
21 *      executable file, complete source code means the source code for
22 *      all modules it contains. It does not include source code for
23 *      modules or files that typically accompany the major components of
24 *      the operating system on which the executable file runs.
25 * 
26 *
27 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 *  WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
30 *  NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
31 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
34 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
36 *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
37 *  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#include <stdio.h>
41#include <unistd.h>
42#include <stdlib.h>
43#include <string.h>
44#include <signal.h>
45#include <time.h>
46#include <sys/param.h>
47#include <sys/types.h>
48#include <termios.h>
49#include <sys/stat.h>
50#include "owl.h"
51
52#if OWL_STDERR_REDIR
53#ifdef HAVE_SYS_IOCTL_H
54#include <sys/ioctl.h>
55#endif
56#ifdef HAVE_SYS_FILIO_H
57#include <sys/filio.h>
58#endif
59int stderr_replace(void);
60#endif
61
62static const char fileIdent[] = "$Id$";
63
64owl_global g;
65
66int main(int argc, char **argv, char **env)
67{
68  WINDOW *recwin, *sepwin, *typwin, *msgwin;
69  owl_editwin *tw;
70  owl_popwin *pw;
71  int j, ret, initialsubs, debug, argcsave, followlast;
72  int newmsgs, zpendcount, nexttimediff;
73  struct sigaction sigact;
74  char *configfile, *tty, *perlout, *perlerr, **argvsave, buff[LINE], startupmsg[LINE];
75  owl_filter *f;
76  owl_style *s;
77  time_t nexttime, now;
78  struct tm *today;
79  char *dir;
80  struct termios tio;
81#ifdef HAVE_LIBZEPHYR
82  ZNotice_t notice;
83#endif
84#if OWL_STDERR_REDIR
85  int newstderr;
86#endif
87
88  argcsave=argc;
89  argvsave=argv;
90  configfile=NULL;
91  tty=NULL;
92  debug=0;
93  initialsubs=1;
94  if (argc>0) {
95    argv++;
96    argc--;
97  }
98  while (argc>0) {
99    if (!strcmp(argv[0], "-n")) {
100      initialsubs=0;
101      argv++;
102      argc--;
103    } else if (!strcmp(argv[0], "-c")) {
104      if (argc<2) {
105        fprintf(stderr, "Too few arguments to -c\n");
106        usage();
107        exit(1);
108      }
109      configfile=argv[1];
110      argv+=2;
111      argc-=2;
112    } else if (!strcmp(argv[0], "-t")) {
113      if (argc<2) {
114        fprintf(stderr, "Too few arguments to -t\n");
115        usage();
116        exit(1);
117      }
118      tty=argv[1];
119      argv+=2;
120      argc-=2;
121    } else if (!strcmp(argv[0], "-d")) {
122      debug=1;
123      argv++;
124      argc--;
125    } else if (!strcmp(argv[0], "-D")) {
126      debug=1;
127      unlink(OWL_DEBUG_FILE);
128      argv++;
129      argc--;
130    } else if (!strcmp(argv[0], "-v")) {
131      printf("This is owl version %s\n", OWL_VERSION_STRING);
132      exit(0);
133    } else {
134      fprintf(stderr, "Uknown argument\n");
135      usage();       
136      exit(1);
137    }
138  }
139
140  owl_function_debugmsg("startup: Finished parsing arguments");
141
142  /* signal handler */
143  /*sigact.sa_handler=sig_handler;*/
144  sigact.sa_sigaction=sig_handler;
145  sigemptyset(&sigact.sa_mask);
146  sigact.sa_flags=SA_SIGINFO;
147  sigaction(SIGWINCH, &sigact, NULL);
148  sigaction(SIGALRM, &sigact, NULL);
149  sigaction(SIGPIPE, &sigact, NULL);
150  sigaction(SIGTERM, &sigact, NULL);
151  sigaction(SIGHUP, &sigact, NULL);
152
153  /* save initial terminal settings */
154  tcgetattr(0, owl_global_get_startup_tio(&g));
155
156  /* turn ISTRIP off */
157  tcgetattr(0, &tio);
158  tio.c_iflag &= ~ISTRIP;
159  tcsetattr(0, TCSAFLUSH, &tio);
160
161  /* screen init */
162  if (!getenv("TERMINFO")) {
163    sprintf(buff, "TERMINFO=%s", TERMINFO);
164    putenv(buff);
165    owl_function_debugmsg("startup: setting TERMINFO to %s", TERMINFO);
166  } else {
167    owl_function_debugmsg("startup: leaving TERMINFO as %s from envrionment", getenv("TERMINFO"));
168  }
169  initscr();
170  start_color();
171#ifdef HAVE_USE_DEFAULT_COLORS
172  use_default_colors();
173#endif
174  raw();
175  noecho();
176
177  /* define simple color pairs */
178  if (has_colors() && COLOR_PAIRS>=8) {
179    int bg = COLOR_BLACK;
180#ifdef HAVE_USE_DEFAULT_COLORS
181    bg = -1;
182#endif
183    init_pair(OWL_COLOR_BLACK,   COLOR_BLACK,   bg);
184    init_pair(OWL_COLOR_RED,     COLOR_RED,     bg);
185    init_pair(OWL_COLOR_GREEN,   COLOR_GREEN,   bg);
186    init_pair(OWL_COLOR_YELLOW,  COLOR_YELLOW,  bg);
187    init_pair(OWL_COLOR_BLUE,    COLOR_BLUE,    bg);
188    init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, bg);
189    init_pair(OWL_COLOR_CYAN,    COLOR_CYAN,    bg);
190    init_pair(OWL_COLOR_WHITE,   COLOR_WHITE,   bg);
191  }
192
193  /* owl global init */
194  owl_global_init(&g);
195    if (debug) owl_global_set_debug_on(&g);
196  owl_function_debugmsg("startup: first available debugging message");
197  owl_global_set_startupargs(&g, argcsave, argvsave);
198  owl_global_set_haveaim(&g);
199
200#ifdef HAVE_LIBZEPHYR
201  /* zephyr init */
202  ret=owl_zephyr_initialize();
203  if (!ret)
204      owl_global_set_havezephyr(&g);
205#endif
206
207#if OWL_STDERR_REDIR
208  /* Do this only after we've started curses up... */
209  owl_function_debugmsg("startup: doing stderr redirection");
210  newstderr = stderr_replace();
211  owl_muxevents_add(owl_global_get_muxevents(&g), newstderr, OWL_MUX_READ,
212                    stderr_redirect_handler, NULL);
213#endif   
214
215  /* create the owl directory, in case it does not exist */
216  owl_function_debugmsg("startup: creating owl directory, if not present");
217  dir=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_CONFIG_DIR);
218  mkdir(dir, S_IRWXU);
219  owl_free(dir);
220
221  /* set the tty, either from the command line, or by figuring it out */
222  owl_function_debugmsg("startup: setting tty name");
223  if (tty) {
224    owl_global_set_tty(&g, tty);
225  } else {
226    owl_global_set_tty(&g, owl_util_get_default_tty());
227  }
228
229  /* Initialize perl */
230  owl_function_debugmsg("startup: processing config file");
231  owl_context_set_readconfig(owl_global_get_context(&g));
232  perlerr=owl_perlconfig_initperl(configfile);
233  if (perlerr) {
234    endwin();
235    owl_function_error("Internal perl error: %s\n", perlerr);
236    fprintf(stderr, "Internal perl error: %s\n", perlerr);
237    fflush(stderr);
238    printf("Internal perl error: %s\n", perlerr);
239    fflush(stdout);
240    exit(1);
241  }
242
243  /* setup the built-in styles */
244  owl_function_debugmsg("startup: creating built-in styles");
245
246  s=owl_malloc(sizeof(owl_style));
247  owl_style_create_internal(s, "basic", &owl_stylefunc_basic, "Basic message formatting.");
248  owl_global_add_style(&g, s);
249#if 0
250  s=owl_malloc(sizeof(owl_style));
251  owl_style_create_internal(s, "vt", &owl_stylefunc_vt, "VT message formatting.");
252  owl_global_add_style(&g, s);
253#endif
254  s=owl_malloc(sizeof(owl_style));
255  owl_style_create_internal(s, "oneline", &owl_stylefunc_oneline, "Formats for one-line-per-message");
256  owl_global_add_style(&g, s);
257
258  /* setup the default filters */
259  /* the personal filter will need to change again when AIM chat's are
260   *  included.  Also, there should be an %aimme% */
261  owl_function_debugmsg("startup: creating default filters");
262  f=owl_malloc(sizeof(owl_filter));
263  owl_filter_init_fromstring(f, "personal", "isprivate ^true$");
264  owl_list_append_element(owl_global_get_filterlist(&g), f);
265
266  f=owl_malloc(sizeof(owl_filter));
267  owl_filter_init_fromstring(f, "wordwrap", "not ( type ^admin$ or type ^zephyr$ ) ");
268  owl_list_append_element(owl_global_get_filterlist(&g), f);
269
270  f=owl_malloc(sizeof(owl_filter));
271  owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )");
272  owl_list_append_element(owl_global_get_filterlist(&g), f);
273
274  f=owl_malloc(sizeof(owl_filter));
275  owl_filter_init_fromstring(f, "ping", "opcode ^ping$");
276  owl_list_append_element(owl_global_get_filterlist(&g), f);
277
278  f=owl_malloc(sizeof(owl_filter));
279  owl_filter_init_fromstring(f, "auto", "opcode ^auto$");
280  owl_list_append_element(owl_global_get_filterlist(&g), f);
281
282  f=owl_malloc(sizeof(owl_filter));
283  owl_filter_init_fromstring(f, "login", "not login ^none$");
284  owl_list_append_element(owl_global_get_filterlist(&g), f);
285
286  f=owl_malloc(sizeof(owl_filter));
287  owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$");
288  owl_list_append_element(owl_global_get_filterlist(&g), f);
289
290  f=owl_malloc(sizeof(owl_filter));
291  owl_filter_init_fromstring(f, "out", "direction ^out$");
292  owl_list_append_element(owl_global_get_filterlist(&g), f);
293
294  f=owl_malloc(sizeof(owl_filter));
295  owl_filter_init_fromstring(f, "aim", "type ^aim$");
296  owl_list_append_element(owl_global_get_filterlist(&g), f);
297
298  f=owl_malloc(sizeof(owl_filter));
299  owl_filter_init_fromstring(f, "zephyr", "type ^zephyr$");
300  owl_list_append_element(owl_global_get_filterlist(&g), f);
301
302  f=owl_malloc(sizeof(owl_filter));
303  owl_filter_init_fromstring(f, "none", "false");
304  owl_list_append_element(owl_global_get_filterlist(&g), f);
305
306  f=owl_malloc(sizeof(owl_filter));
307  owl_filter_init_fromstring(f, "all", "true");
308  owl_list_append_element(owl_global_get_filterlist(&g), f);
309
310  /* set the current view */
311  owl_function_debugmsg("startup: setting the current view");
312  owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default"));
313
314  /* AIM init */
315  owl_function_debugmsg("startup: doing AIM initialization");
316  owl_aim_init();
317
318  /* if the config defines a formatting function, add 'perl' as a style */
319  if (owl_global_is_config_format(&g)) {
320    owl_function_debugmsg("Found perl formatting");
321    s=owl_malloc(sizeof(owl_style));
322    owl_style_create_perl(s, "perl", "BarnOwl::_format_msg_legacy_wrap",
323                          "User-defined perl style that calls BarnOwl::format_msg"
324                          "with legacy global variable support");
325    owl_global_add_style(&g, s);
326    owl_global_set_default_style(&g, "perl");
327  }
328
329  /* execute the startup function in the configfile */
330  owl_function_debugmsg("startup: executing perl startup, if applicable");
331  perlout = owl_perlconfig_execute("BarnOwl::Hooks::startup();");
332  if (perlout) owl_free(perlout);
333
334  /* hold on to the window names for convenience */
335  msgwin=owl_global_get_curs_msgwin(&g);
336  recwin=owl_global_get_curs_recwin(&g);
337  sepwin=owl_global_get_curs_sepwin(&g);
338  typwin=owl_global_get_curs_typwin(&g);
339  tw=owl_global_get_typwin(&g);
340
341  /* welcome message */
342  owl_function_debugmsg("startup: creating splash message");
343  strcpy(startupmsg, "-----------------------------------------------------------------------\n");
344  sprintf(buff,      "Welcome to barnowl version %s.  Press 'h' for on-line help. \n", OWL_VERSION_STRING);
345  strcat(startupmsg, buff);
346  strcat(startupmsg, "                                                                       \n");
347  strcat(startupmsg, "This is an UNOFFICIAL DEVELOPMENT BUILD of owl. If you are using this  \n");
348  strcat(startupmsg, "build regularly, please add yourself to dirty-owl-hackers@mit.edu      \n");
349  strcat(startupmsg, "                                                                 ^ ^   \n");
350  strcat(startupmsg, "Any bugs should be reported to dirty-owl-hackers@mit.edu         OvO   \n");
351  strcat(startupmsg, "Do not seek help with this build from bug-owl or ktools.        (   )  \n");
352  strcat(startupmsg, "-----------------------------------------------------------------m-m---\n");
353  owl_function_adminmsg("", startupmsg);
354  sepbar(NULL);
355
356  /* process the startup file */
357  owl_function_debugmsg("startup: processing startup file");
358  owl_function_source(NULL);
359
360  wrefresh(sepwin);
361
362  /* load zephyr subs */
363  if (initialsubs) {
364    int ret2;
365    owl_function_debugmsg("startup: loading initial zephyr subs");
366
367    /* load default subscriptions */
368    ret=owl_zephyr_loaddefaultsubs();
369   
370    /* load subscriptions from subs file */
371    ret2=owl_zephyr_loadsubs(NULL, 0);
372
373    if (ret || ret2) {
374      owl_function_adminmsg("", "Error loading zephyr subscriptions");
375    } else if (ret2!=-1) {
376      owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
377    }
378
379    /* load login subscriptions */
380    if (owl_global_is_loginsubs(&g)) {
381      owl_function_debugmsg("startup: loading login subs");
382      owl_function_loadloginsubs(NULL);
383    }
384  }
385
386  /* First buddy check to sync the list without notifications */
387  owl_function_debugmsg("startup: doing initial zephyr buddy check");
388  /* owl_function_zephyr_buddy_check(0); */
389
390  /* set the startup and default style, based on userclue and presence of a
391   * formatting function */
392  owl_function_debugmsg("startup: setting startup and default style");
393  if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) {
394    /* the style was set by the user: leave it alone */
395  } else if (owl_global_is_config_format(&g)) {
396    owl_global_set_default_style(&g, "perl");
397  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
398    owl_global_set_default_style(&g, "default");
399  } else {
400    owl_global_set_default_style(&g, "basic");
401  }
402
403  /* zlog in if we need to */
404  if (owl_global_is_startuplogin(&g)) {
405    owl_function_debugmsg("startup: doing zlog in");
406    owl_zephyr_zlog_in();
407  }
408
409  owl_function_debugmsg("startup: set style for the view");
410  owl_view_set_style(owl_global_get_current_view(&g), 
411                     owl_global_get_style_by_name(&g, owl_global_get_default_style(&g)));   
412
413  owl_function_debugmsg("startup: setting context interactive");
414  owl_context_set_interactive(owl_global_get_context(&g));
415
416  nexttimediff=10;
417  nexttime=time(NULL);
418
419  owl_function_debugmsg("startup: entering main loop");
420  /* main loop */
421  while (1) {
422
423    /* if a resize has been scheduled, deal with it */
424    owl_global_resize(&g, 0, 0);
425
426    /* these are here in case a resize changes the windows */
427    msgwin=owl_global_get_curs_msgwin(&g);
428    recwin=owl_global_get_curs_recwin(&g);
429    sepwin=owl_global_get_curs_sepwin(&g);
430    typwin=owl_global_get_curs_typwin(&g);
431
432    followlast=owl_global_should_followlast(&g);
433   
434    /* Do AIM stuff */
435    if (owl_global_is_doaimevents(&g)) {
436      owl_aim_process_events();
437
438      if (owl_global_is_aimloggedin(&g)) {
439        if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
440          /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
441          owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
442        }
443      }
444    }
445
446    owl_perlconfig_mainloop();
447
448    /* little hack */
449    now=time(NULL);
450    today=localtime(&now);
451    if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) {
452      if (time(NULL)>nexttime) {
453        if (nexttimediff==1) {
454          nexttimediff=10;
455        } else {
456          nexttimediff=1;
457        }
458        nexttime+=nexttimediff;
459        owl_hack_animate();
460      }
461    }
462
463    /* Grab incoming messages. */
464    newmsgs=0;
465    zpendcount=0;
466    while(owl_zephyr_zpending() || owl_global_messagequeue_pending(&g)) {
467#ifdef HAVE_LIBZEPHYR
468      struct sockaddr_in from;
469#endif
470      owl_message *m=NULL;
471      owl_filter *f;
472
473      /* grab the new message, stick it in 'm' */
474      if (owl_zephyr_zpending()) {
475#ifdef HAVE_LIBZEPHYR
476        /* grab a zephyr notice, but if we've done 20 without stopping,
477           take a break to process keystrokes etc. */
478        if (zpendcount>20) break;
479        ZReceiveNotice(&notice, &from);
480        zpendcount++;
481       
482        /* is this an ack from a zephyr we sent? */
483        if (owl_zephyr_notice_is_ack(&notice)) {
484          owl_zephyr_handle_ack(&notice);
485          continue;
486        }
487       
488        /* if it's a ping and we're not viewing pings then skip it */
489        if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
490          continue;
491        }
492
493        /* create the new message */
494        m=owl_malloc(sizeof(owl_message));
495        owl_message_create_from_znotice(m, &notice);
496#endif
497      } else if (owl_global_messagequeue_pending(&g)) {
498        /* pick up the non-zephyr message in the message queue */
499        m=owl_global_messageuque_popmsg(&g);
500      } else {
501        /* Not supposed to happen, but we seem to get here on resizes */
502        owl_function_debugmsg("Bottomed out looking for zephyr");
503      }
504
505      /* If we didn't pick up a message for some reason, don't go on */
506      if (m==NULL) {
507        owl_function_debugmsg("m is null in main loop");
508        continue;
509      }
510     
511      /* if this message it on the puntlist, nuke it and continue */
512      if (owl_global_message_is_puntable(&g, m)) {
513        owl_message_free(m);
514        continue;
515      }
516
517      /*  login or logout that should be ignored? */
518      if (owl_global_is_ignorelogins(&g) && owl_message_is_loginout(m)) {
519        owl_message_free(m);
520        continue;
521      }
522
523      /* otherwise add it to the global list */
524      owl_messagelist_append_element(owl_global_get_msglist(&g), m);
525      newmsgs=1;
526
527      /* let the config know the new message has been received */
528      owl_perlconfig_getmsg(m, 0, NULL);
529
530      /* add it to any necessary views; right now there's only the current view */
531      owl_view_consider_message(owl_global_get_current_view(&g), m);
532
533      /* do we need to autoreply? */
534      if (owl_global_is_zaway(&g) && !owl_message_get_attribute_value(m, "isauto")) {
535        if (owl_message_is_type_zephyr(m)) {
536          owl_zephyr_zaway(m);
537        } else if (owl_message_is_type_aim(m)) {
538          if (owl_message_is_private(m)) {
539            owl_function_send_aimawymsg(owl_message_get_sender(m), owl_global_get_zaway_msg(&g));
540          }
541        }
542      }
543
544      /* ring the bell if it's a personal */
545      if (!strcmp(owl_global_get_personalbell(&g), "on")) {
546          if (!owl_message_is_loginout(m) &&
547              !owl_message_is_mail(m) &&
548              owl_message_is_private(m)) {
549            owl_function_beep();
550          }
551      } else if (!strcmp(owl_global_get_personalbell(&g), "off")) {
552        /* do nothing */
553      } else {
554        f=owl_global_get_filter(&g, owl_global_get_personalbell(&g));
555        if (f && owl_filter_message_match(f, m)) {
556          owl_function_beep();
557        }
558      }
559
560
561      /* if it matches the alert filter, do the alert action */
562      f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g));
563      if (f && owl_filter_message_match(f, m)) {
564        owl_function_command(owl_global_get_alert_action(&g));
565      }
566
567      /* if it's a zephyr login or logout, update the zbuddylist */
568      if (owl_message_is_type_zephyr(m) && owl_message_is_loginout(m)) {
569        if (owl_message_is_login(m)) {
570          owl_zbuddylist_adduser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
571        } else if (owl_message_is_logout(m)) {
572          owl_zbuddylist_deluser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
573        } else {
574          owl_function_error("Internal error: received login notice that is neither login nor logout");
575        }
576      }
577
578      /* check for burning ears message */
579      /* this is an unsupported feature */
580      if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) {
581        char *buff;
582        buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m));
583        owl_function_adminmsg(buff, "");
584        owl_free(buff);
585        owl_function_beep();
586      }
587
588      /* log the message if we need to */
589      owl_log_message(m);
590    }
591
592    /* is it time to check zbuddies? */
593    if (owl_global_is_pseudologins(&g)) {
594      if (owl_timer_is_expired(owl_global_get_zephyr_buddycheck_timer(&g))) {
595        owl_function_debugmsg("Doing zephyr buddy check");
596        owl_function_zephyr_buddy_check(1);
597        owl_timer_reset(owl_global_get_zephyr_buddycheck_timer(&g));
598      }
599    }
600
601    /* dispatch any muxevents */
602    owl_muxevents_dispatch(owl_global_get_muxevents(&g), 0);
603
604    /* follow the last message if we're supposed to */
605    if (newmsgs && followlast) {
606      owl_function_lastmsg_noredisplay();
607    }
608
609    /* do the newmsgproc thing */
610    if (newmsgs) {
611      owl_function_do_newmsgproc();
612    }
613   
614    /* redisplay if necessary */
615    /* this should be optimized to not run if the new messages won't be displayed */
616    if (newmsgs) {
617      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
618      sepbar(NULL);
619      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
620        owl_popwin_refresh(owl_global_get_popwin(&g));
621        /* TODO: this is a broken kludge */
622        if (owl_global_get_viewwin(&g)) {
623          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
624        }
625      }
626      owl_global_set_needrefresh(&g);
627    }
628
629    /* if a popwin just came up, refresh it */
630    pw=owl_global_get_popwin(&g);
631    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
632      owl_popwin_refresh(pw);
633      owl_popwin_no_needs_first_refresh(pw);
634      owl_global_set_needrefresh(&g);
635      /* TODO: this is a broken kludge */
636      if (owl_global_get_viewwin(&g)) {
637        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
638      }
639    }
640
641    /* update the terminal if we need to */
642    if (owl_global_is_needrefresh(&g)) {
643      /* leave the cursor in the appropriate window */
644      if (owl_global_is_typwin_active(&g)) {
645        owl_function_set_cursor(typwin);
646      } else {
647        owl_function_set_cursor(sepwin);
648      }
649      doupdate();
650      owl_global_set_noneedrefresh(&g);
651    }
652
653    /* Handle all keypresses.  If no key has been pressed, sleep for a
654     * little bit, but otherwise do not.  This lets input be grabbed
655     * as quickly as possbile */
656    j=wgetch(typwin);
657    if (j==ERR) {
658      usleep(10);
659    } else {
660      owl_global_update_lastinputtime(&g);
661      /* find and activate the current keymap.
662       * TODO: this should really get fixed by activating
663       * keymaps as we switch between windows...
664       */
665      if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
666        owl_context_set_popless(owl_global_get_context(&g), 
667                                owl_global_get_viewwin(&g));
668        owl_function_activate_keymap("popless");
669      } else if (owl_global_is_typwin_active(&g) 
670                 && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
671        /*
672          owl_context_set_editline(owl_global_get_context(&g), tw);
673          owl_function_activate_keymap("editline");
674        */
675      } else if (owl_global_is_typwin_active(&g) 
676                 && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
677        owl_context_set_editmulti(owl_global_get_context(&g), tw);
678        owl_function_activate_keymap("editmulti");
679      } else {
680        owl_context_set_recv(owl_global_get_context(&g));
681        owl_function_activate_keymap("recv");
682      }
683      /* now actually handle the keypress */
684      ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
685      if (ret!=0 && ret!=1) {
686        owl_function_makemsg("Unable to handle keypress");
687      }
688    }
689
690    /* Log any error signals */
691    {
692      siginfo_t si;
693      int signum;
694      if ((signum = owl_global_get_errsignal_and_clear(&g, &si)) > 0) {
695        owl_function_error("Got unexpected signal: %d %s  (code: %d band: %d  errno: %d)", 
696                           signum, signum==SIGPIPE?"SIGPIPE":"SIG????",
697                           si.si_code, si.si_band, si.si_errno);
698      }
699    }
700
701  }
702}
703
704void sig_handler(int sig, siginfo_t *si, void *data)
705{
706  if (sig==SIGWINCH) {
707    /* we can't inturrupt a malloc here, so it just sets a flag
708     * schedulding a resize for later
709     */
710    owl_function_resize();
711  } else if (sig==SIGPIPE || sig==SIGCHLD) {
712    /* Set a flag and some info that we got the sigpipe
713     * so we can record that we got it and why... */
714    owl_global_set_errsignal(&g, sig, si);
715  } else if (sig==SIGTERM || sig==SIGHUP) {
716    owl_function_quit();
717  }
718
719}
720
721void usage()
722{
723  fprintf(stderr, "Owl version %s\n", OWL_VERSION_STRING);
724  fprintf(stderr, "Usage: owl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-t <ttyname>]\n");
725  fprintf(stderr, "  -n      don't load zephyr subscriptions\n");
726  fprintf(stderr, "  -d      enable debugging\n");
727  fprintf(stderr, "  -D      enable debugging and delete previous debug file\n");
728  fprintf(stderr, "  -v      print the Owl version number and exit\n");
729  fprintf(stderr, "  -h      print this help message\n");
730  fprintf(stderr, "  -c      specify an alternate config file\n");
731  fprintf(stderr, "  -t      set the tty name\n");
732}
733
734#if OWL_STDERR_REDIR
735
736/* Replaces stderr with a pipe so that we can read from it.
737 * Returns the fd of the pipe from which stderr can be read. */
738int stderr_replace(void)
739{
740  int pipefds[2];
741  if (0 != pipe(pipefds)) {
742    perror("pipe");
743    owl_function_debugmsg("stderr_replace: pipe FAILED\n");
744    return -1;
745  }
746    owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]);
747  if (-1 == dup2(pipefds[1], 2 /*stderr*/)) {
748    owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno));
749    perror("dup2");
750    return -1;
751  }
752  return pipefds[0];
753}
754
755/* Sends stderr (read from rfd) messages to the error console */
756void stderr_redirect_handler(int handle, int rfd, int eventmask, void *data) 
757{
758  int navail, bread;
759  char *buf;
760  /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/
761  if (rfd<0) return;
762  if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) {
763    return;
764  }
765  /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/
766  if (navail<=0) return;
767  if (navail>256) { navail = 256; }
768  buf = owl_malloc(navail+1);
769  bread = read(rfd, buf, navail);
770  if (buf[navail-1] != '\0') {
771    buf[navail] = '\0';
772  }
773  owl_function_error("Err: %s", buf);
774  owl_free(buf);
775}
776
777#endif /* OWL_STDERR_REDIR */
Note: See TracBrowser for help on using the repository browser.