source: owl.c @ 5df4b9a

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