source: owl.c @ 9cd7e6a

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