source: owl.c @ 488ebf6

owl
Last change on this file since 488ebf6 was 19c0c55, checked in by James M. Kretchmar <kretch@mit.edu>, 15 years ago
remove unnecessary putenv and unnecessary sprintf
  • Property mode set to 100644
File size: 23.7 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 branched BarnOwl project, Copyright (c)
22 * 2006-2009 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;
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
69  argcsave=argc;
70  argvsave=argv;
71  configfile=NULL;
72  tty=NULL;
73  debug=0;
74  initialsubs=1;
75  if (argc>0) {
76    argv++;
77    argc--;
78  }
79  while (argc>0) {
80    if (!strcmp(argv[0], "-n")) {
81      initialsubs=0;
82      argv++;
83      argc--;
84    } else if (!strcmp(argv[0], "-c")) {
85      if (argc<2) {
86        fprintf(stderr, "Too few arguments to -c\n");
87        usage();
88        exit(1);
89      }
90      configfile=argv[1];
91      argv+=2;
92      argc-=2;
93    } else if (!strcmp(argv[0], "-t")) {
94      if (argc<2) {
95        fprintf(stderr, "Too few arguments to -t\n");
96        usage();
97        exit(1);
98      }
99      tty=argv[1];
100      argv+=2;
101      argc-=2;
102    } else if (!strcmp(argv[0], "-d")) {
103      debug=1;
104      argv++;
105      argc--;
106    } else if (!strcmp(argv[0], "-D")) {
107      debug=1;
108      unlink(OWL_DEBUG_FILE);
109      argv++;
110      argc--;
111    } else if (!strcmp(argv[0], "-v")) {
112      printf("This is owl version %s\n", OWL_VERSION_STRING);
113      exit(0);
114    } else {
115      fprintf(stderr, "Uknown argument\n");
116      usage();       
117      exit(1);
118    }
119  }
120
121  owl_function_debugmsg("startup: Finished parsing arguments");
122
123#ifdef HAVE_LIBZEPHYR
124  /* zephyr init */
125  ret=owl_zephyr_initialize();
126  if (ret) {
127    exit(1);
128  }
129#endif
130 
131  /* signal handler */
132  /*sigact.sa_handler=sig_handler;*/
133  sigact.sa_sigaction=sig_handler;
134  sigemptyset(&sigact.sa_mask);
135  sigact.sa_flags=SA_SIGINFO;
136  sigaction(SIGWINCH, &sigact, NULL);
137  sigaction(SIGALRM, &sigact, NULL);
138  sigaction(SIGPIPE, &sigact, NULL);
139  sigaction(SIGTERM, &sigact, NULL);
140  sigaction(SIGHUP, &sigact, NULL);
141
142  /* save initial terminal settings */
143  tcgetattr(0, owl_global_get_startup_tio(&g));
144
145  /* turn ISTRIP off */
146  tcgetattr(0, &tio);
147  tio.c_iflag &= ~ISTRIP;
148  tcsetattr(0, TCSAFLUSH, &tio);
149
150  /* screen init */
151  if (!getenv("TERMINFO")) {
152    owl_function_debugmsg("startup: found TERMINFO unset in environment");
153  } else {
154    owl_function_debugmsg("startup: found TERMINFO is %s from environment", getenv("TERMINFO"));
155  }
156  initscr();
157  start_color();
158#ifdef HAVE_USE_DEFAULT_COLORS
159  use_default_colors();
160#endif
161  raw();
162  noecho();
163
164  /* define simple color pairs */
165  if (has_colors() && COLOR_PAIRS>=8) {
166    init_pair(OWL_COLOR_BLACK,   COLOR_BLACK,   -1);
167    init_pair(OWL_COLOR_RED,     COLOR_RED,     -1);
168    init_pair(OWL_COLOR_GREEN,   COLOR_GREEN,   -1);
169    init_pair(OWL_COLOR_YELLOW,  COLOR_YELLOW,  -1);
170    init_pair(OWL_COLOR_BLUE,    COLOR_BLUE,    -1);
171    init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, -1);
172    init_pair(OWL_COLOR_CYAN,    COLOR_CYAN,    -1);
173    init_pair(OWL_COLOR_WHITE,   COLOR_WHITE,   -1);
174  }
175
176  /* owl global init */
177  owl_global_init(&g);
178  if (debug) owl_global_set_debug_on(&g);
179  owl_function_debugmsg("startup: first available debugging message");
180  owl_global_set_startupargs(&g, argcsave, argvsave);
181  owl_global_set_haveaim(&g);
182
183  /* prepare stdin dispatch */
184  {
185    owl_dispatch *d = owl_malloc(sizeof(owl_dispatch));
186    owl_function_debugmsg("startup: creating input selector");
187    d->fd = STDIN;
188    d->cfunc = &owl_process_input;
189    d->destroy = NULL;
190    owl_select_add_dispatch(d);
191  }
192
193#ifdef HAVE_LIBZEPHYR
194  owl_global_set_havezephyr(&g);
195  if (!ret) {
196    owl_dispatch *d = owl_malloc(sizeof(owl_dispatch));
197    owl_function_debugmsg("startup: creating zephyr selector");
198    d->fd = ZGetFD();
199    d->cfunc = &owl_zephyr_process_events;
200    d->destroy = NULL;
201    owl_select_add_dispatch(d);
202    owl_global_set_havezephyr(&g);
203  }
204#endif
205
206#if OWL_STDERR_REDIR
207  /* Do this only after we've started curses up... */
208  {
209    owl_dispatch *d = owl_malloc(sizeof(owl_dispatch));
210    owl_function_debugmsg("startup: doing stderr redirection");
211    d->fd = stderr_replace();
212    d->cfunc = stderr_redirect_handler;
213    d->destroy = NULL;
214    owl_function_debugmsg("testing: 1");
215    owl_select_add_dispatch(d);
216    owl_function_debugmsg("testing: 2");
217  }
218
219#endif   
220
221  /* create the owl directory, in case it does not exist */
222  owl_function_debugmsg("startup: creating owl directory, if not present");
223  dir=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_CONFIG_DIR);
224  mkdir(dir, S_IRWXU);
225  owl_free(dir);
226
227  /* set the tty, either from the command line, or by figuring it out */
228  owl_function_debugmsg("startup: setting tty name");
229  if (tty) {
230    owl_global_set_tty(&g, tty);
231  } else {
232    owl_global_set_tty(&g, owl_util_get_default_tty());
233  }
234
235  /* setup the built-in styles */
236  owl_function_debugmsg("startup: creating built-in styles");
237  s=owl_malloc(sizeof(owl_style));
238  owl_style_create_internal(s, "default", &owl_stylefunc_default, "Default message formatting");
239  owl_global_add_style(&g, s);
240
241  s=owl_malloc(sizeof(owl_style));
242  owl_style_create_internal(s, "basic", &owl_stylefunc_basic, "Basic message formatting.");
243  owl_global_add_style(&g, s);
244#if 0
245  s=owl_malloc(sizeof(owl_style));
246  owl_style_create_internal(s, "vt", &owl_stylefunc_vt, "VT message formatting.");
247  owl_global_add_style(&g, s);
248#endif
249  s=owl_malloc(sizeof(owl_style));
250  owl_style_create_internal(s, "oneline", &owl_stylefunc_oneline, "Formats for one-line-per-message");
251  owl_global_add_style(&g, s);
252
253  /* setup the default filters */
254  /* the personal filter will need to change again when AIM chat's are
255   *  included.  Also, there should be an %aimme% */
256  owl_function_debugmsg("startup: creating default filters");
257  f=owl_malloc(sizeof(owl_filter));
258  owl_filter_init_fromstring(f, "personal", "( type ^zephyr$ "
259                             "and class ^message$ and instance ^personal$ "
260                             "and ( recipient ^%me%$ or sender ^%me%$ ) ) "
261                             "or ( type ^aim$ and login ^none$ )");
262  owl_list_append_element(owl_global_get_filterlist(&g), f);
263
264  f=owl_malloc(sizeof(owl_filter));
265  owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )");
266  owl_list_append_element(owl_global_get_filterlist(&g), f);
267
268  f=owl_malloc(sizeof(owl_filter));
269  owl_filter_init_fromstring(f, "ping", "opcode ^ping$");
270  owl_list_append_element(owl_global_get_filterlist(&g), f);
271
272  f=owl_malloc(sizeof(owl_filter));
273  owl_filter_init_fromstring(f, "auto", "opcode ^auto$");
274  owl_list_append_element(owl_global_get_filterlist(&g), f);
275
276  f=owl_malloc(sizeof(owl_filter));
277  owl_filter_init_fromstring(f, "login", "not login ^none$");
278  owl_list_append_element(owl_global_get_filterlist(&g), f);
279
280  f=owl_malloc(sizeof(owl_filter));
281  owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$");
282  owl_list_append_element(owl_global_get_filterlist(&g), f);
283
284  f=owl_malloc(sizeof(owl_filter));
285  owl_filter_init_fromstring(f, "out", "direction ^out$");
286  owl_list_append_element(owl_global_get_filterlist(&g), f);
287
288  f=owl_malloc(sizeof(owl_filter));
289  owl_filter_init_fromstring(f, "aim", "type ^aim$");
290  owl_list_append_element(owl_global_get_filterlist(&g), f);
291
292  f=owl_malloc(sizeof(owl_filter));
293  owl_filter_init_fromstring(f, "zephyr", "type ^zephyr$");
294  owl_list_append_element(owl_global_get_filterlist(&g), f);
295
296  f=owl_malloc(sizeof(owl_filter));
297  owl_filter_init_fromstring(f, "none", "false");
298  owl_list_append_element(owl_global_get_filterlist(&g), f);
299
300  f=owl_malloc(sizeof(owl_filter));
301  owl_filter_init_fromstring(f, "all", "true");
302  owl_list_append_element(owl_global_get_filterlist(&g), f);
303
304  /* set the current view */
305  owl_function_debugmsg("startup: setting the current view");
306  owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default"));
307
308  /* AIM init */
309  owl_function_debugmsg("startup: doing AIM initialization");
310  owl_aim_init();
311
312  /* process the startup file */
313  owl_function_debugmsg("startup: processing startup file");
314  owl_function_source(NULL);
315
316  /* read the config file */
317  owl_function_debugmsg("startup: processing config file");
318  owl_context_set_readconfig(owl_global_get_context(&g));
319  perlerr=owl_perlconfig_readconfig(configfile);
320  if (perlerr) {
321    endwin();
322    owl_function_error("Error parsing configfile: %s\n", perlerr);
323    fprintf(stderr, "\nError parsing configfile: %s\n", perlerr);
324    fflush(stderr);
325    printf("\nError parsing configfile: %s\n", perlerr);
326    fflush(stdout);
327    exit(1);
328  }
329
330  /* if the config defines a formatting function, add 'perl' as a style */
331  if (owl_global_is_config_format(&g)) {
332    owl_function_debugmsg("Found perl formatting");
333    s=owl_malloc(sizeof(owl_style));
334    owl_style_create_perl(s, "perl", "owl::_format_msg_legacy_wrap",
335                          "User-defined perl style that calls owl::format_msg"
336                          "with legacy global variable support");
337    owl_global_add_style(&g, s);
338    owl_global_set_default_style(&g, "perl");
339  }
340
341  /* execute the startup function in the configfile */
342  owl_function_debugmsg("startup: executing perl startup, if applicable");
343  perlout = owl_perlconfig_execute("owl::startup();");
344  if (perlout) owl_free(perlout);
345 
346  /* hold on to the window names for convenience */
347  msgwin=owl_global_get_curs_msgwin(&g);
348  recwin=owl_global_get_curs_recwin(&g);
349  sepwin=owl_global_get_curs_sepwin(&g);
350  typwin=owl_global_get_curs_typwin(&g);
351  tw=owl_global_get_typwin(&g);
352
353  /* welcome message */
354  owl_function_debugmsg("startup: creating splash message");
355  owl_function_adminmsg("",
356                        "-----------------------------------------------------------------------\n"
357                        "Welcome to Owl version " OWL_VERSION_STRING ".  Press 'h' for on-line help. \n"
358                        "                                                                       \n"
359                        "If you would like to receive release announcements about Owl you can   \n"
360                        "join the owl-users mailing list at http://www.ktools.org/              \n"
361                        "                                                                 ^ ^   \n"
362                        "                                                                 OvO   \n"
363                        "Please report any bugs or suggestions to bug-owl@ktools.org     (   )  \n"
364                        "-----------------------------------------------------------------m-m---\n"
365                        );
366  sepbar(NULL);
367
368  wrefresh(sepwin);
369
370  /* load zephyr subs */
371  if (initialsubs) {
372    int ret2;
373    owl_function_debugmsg("startup: loading initial zephyr subs");
374
375    /* load default subscriptions */
376    ret=owl_zephyr_loaddefaultsubs();
377   
378    /* load subscriptions from subs file */
379    ret2=owl_zephyr_loadsubs(NULL, 0);
380
381    if (ret || ret2) {
382      owl_function_adminmsg("", "Error loading zephyr subscriptions");
383    } else if (ret2!=-1) {
384      owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
385    }
386
387    /* load login subscriptions */
388    if (owl_global_is_loginsubs(&g)) {
389      owl_function_debugmsg("startup: loading login subs");
390      owl_function_loadloginsubs(NULL);
391    }
392  }
393
394  /* First buddy check to sync the list without notifications */
395  owl_function_debugmsg("startup: doing initial zephyr buddy check");
396  /* owl_function_zephyr_buddy_check(0); */
397
398  /* set the startup and default style, based on userclue and presence of a
399   * formatting function */
400  owl_function_debugmsg("startup: setting startup and default style");
401  if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) {
402    /* the style was set by the user: leave it alone */
403  } else if (owl_global_is_config_format(&g)) {
404    owl_global_set_default_style(&g, "perl");
405  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
406    owl_global_set_default_style(&g, "default");
407  } else {
408    owl_global_set_default_style(&g, "basic");
409  }
410
411  /* zlog in if we need to */
412  if (owl_global_is_startuplogin(&g)) {
413    owl_function_debugmsg("startup: doing zlog in");
414    owl_zephyr_zlog_in();
415  }
416
417  owl_function_debugmsg("startup: set style for the view");
418  owl_view_set_style(owl_global_get_current_view(&g), 
419                     owl_global_get_style_by_name(&g, owl_global_get_default_style(&g)));   
420
421  owl_function_debugmsg("startup: setting context interactive");
422  owl_context_set_interactive(owl_global_get_context(&g));
423
424  nexttimediff=10;
425  nexttime=time(NULL);
426
427  owl_zephyr_process_events(NULL);
428  owl_function_debugmsg("startup: entering main loop");
429  /* main loop */
430  while (1) {
431
432    /* if a resize has been scheduled, deal with it */
433    owl_global_resize(&g, 0, 0);
434
435    /* these are here in case a resize changes the windows */
436    msgwin=owl_global_get_curs_msgwin(&g);
437    recwin=owl_global_get_curs_recwin(&g);
438    sepwin=owl_global_get_curs_sepwin(&g);
439    typwin=owl_global_get_curs_typwin(&g);
440
441    followlast=owl_global_should_followlast(&g);
442   
443    /* Do AIM stuff */
444    if (owl_global_is_doaimevents(&g)) {
445      /* owl_aim_process_events(); */
446
447      if (owl_global_is_aimloggedin(&g)) {
448        if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
449          /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
450          owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
451        }
452      }
453    }
454
455    /* little hack */
456    now=time(NULL);
457    today=localtime(&now);
458    if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) {
459      if (time(NULL)>nexttime) {
460        if (nexttimediff==1) {
461          nexttimediff=10;
462        } else {
463          nexttimediff=1;
464        }
465        nexttime+=nexttimediff;
466        owl_hack_animate();
467      }
468    }
469
470    /* Grab incoming messages. */
471    newmsgs=0;
472    while(owl_global_messagequeue_pending(&g)) {
473      owl_message *m=NULL;
474      owl_filter *f;
475
476      m=owl_global_messageuque_popmsg(&g);
477     
478      /* if this message it on the puntlist, nuke it and continue */
479      if (owl_global_message_is_puntable(&g, m)) {
480        owl_message_free(m);
481        continue;
482      }
483
484      /*  login or logout that should be ignored? */
485      if (owl_global_is_ignorelogins(&g) && owl_message_is_loginout(m)) {
486        owl_message_free(m);
487        continue;
488      }
489
490      /* otherwise add it to the global list */
491      owl_messagelist_append_element(owl_global_get_msglist(&g), m);
492      newmsgs=1;
493
494      /* let the config know the new message has been received */
495      owl_perlconfig_getmsg(m, 0, NULL);
496
497      /* add it to any necessary views; right now there's only the current view */
498      owl_view_consider_message(owl_global_get_current_view(&g), m);
499
500      /* do we need to autoreply? */
501      if (owl_global_is_zaway(&g) && !owl_message_get_attribute_value(m, "isauto")) {
502        if (owl_message_is_type_zephyr(m)) {
503          owl_zephyr_zaway(m);
504        } else if (owl_message_is_type_aim(m)) {
505          if (owl_message_is_private(m)) {
506            owl_function_send_aimawymsg(owl_message_get_sender(m), owl_global_get_zaway_msg(&g));
507          }
508        }
509      }
510
511      /* ring the bell if it's a personal */
512      if (!strcmp(owl_global_get_personalbell(&g), "on")) {
513          if (!owl_message_is_loginout(m) &&
514              !owl_message_is_mail(m) &&
515              owl_message_is_private(m)) {
516            owl_function_beep();
517          }
518      } else if (!strcmp(owl_global_get_personalbell(&g), "off")) {
519        /* do nothing */
520      } else {
521        f=owl_global_get_filter(&g, owl_global_get_personalbell(&g));
522        if (f && owl_filter_message_match(f, m)) {
523          owl_function_beep();
524        }
525      }
526
527
528      /* if it matches the alert filter, do the alert action */
529      f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g));
530      if (f && owl_filter_message_match(f, m)) {
531        owl_function_command(owl_global_get_alert_action(&g));
532      }
533
534      /* if it's a zephyr login or logout, update the zbuddylist */
535      if (owl_message_is_type_zephyr(m) && owl_message_is_loginout(m)) {
536        if (owl_message_is_login(m)) {
537          owl_zbuddylist_adduser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
538        } else if (owl_message_is_logout(m)) {
539          owl_zbuddylist_deluser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
540        } else {
541          owl_function_error("Internal error: received login notice that is neither login nor logout");
542        }
543      }
544
545      /* check for burning ears message */
546      /* this is an unsupported feature */
547      if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) {
548        char *buff;
549        buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m));
550        owl_function_adminmsg(buff, "");
551        owl_free(buff);
552        owl_function_beep();
553      }
554
555      /* log the message if we need to */
556      owl_log_message(m);
557    }
558
559    /* is it time to check zbuddies? */
560    if (owl_global_is_pseudologins(&g)) {
561      if (owl_timer_is_expired(owl_global_get_zephyr_buddycheck_timer(&g))) {
562        owl_function_debugmsg("Doing zephyr buddy check");
563        owl_function_zephyr_buddy_check(1);
564        owl_timer_reset(owl_global_get_zephyr_buddycheck_timer(&g));
565      }
566    }
567
568    /* follow the last message if we're supposed to */
569    if (newmsgs && followlast) {
570      owl_function_lastmsg_noredisplay();
571    }
572
573    /* do the newmsgproc thing */
574    if (newmsgs) {
575      owl_function_do_newmsgproc();
576    }
577   
578    /* redisplay if necessary */
579    /* this should be optimized to not run if the new messages won't be displayed */
580    if (newmsgs) {
581      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
582      sepbar(NULL);
583      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
584        owl_popwin_refresh(owl_global_get_popwin(&g));
585        /* TODO: this is a broken kludge */
586        if (owl_global_get_viewwin(&g)) {
587          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
588        }
589      }
590      owl_global_set_needrefresh(&g);
591    }
592
593    /* if a popwin just came up, refresh it */
594    pw=owl_global_get_popwin(&g);
595    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
596      owl_popwin_refresh(pw);
597      owl_popwin_no_needs_first_refresh(pw);
598      owl_global_set_needrefresh(&g);
599      /* TODO: this is a broken kludge */
600      if (owl_global_get_viewwin(&g)) {
601        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
602      }
603    }
604
605    /* update the terminal if we need to */
606    if (owl_global_is_needrefresh(&g)) {
607      /* leave the cursor in the appropriate window */
608      if (owl_global_is_typwin_active(&g)) {
609        owl_function_set_cursor(typwin);
610      } else {
611        owl_function_set_cursor(sepwin);
612      }
613      doupdate();
614      owl_global_set_noneedrefresh(&g);
615    }
616
617    /* Some calls into libzephyr call Z_WaitForNotice(), which has its
618     * own select loop and may leave zephyrs on the queue. Check for
619     * them now, and process any we find. */
620    owl_zephyr_process_events(NULL);
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.