source: owl.c @ 2a2bb60

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