source: owl.c @ ee0574c

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