source: owl.c @ 405d5e6

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