source: owl.c @ debb15d

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