source: owl.c @ 280ddc6

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