source: owl.c @ 3bc7358

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