source: owl.c @ b20de4f

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