source: owl.c @ 7967433

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