source: owl.c @ 5fcf137

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