source: owl.c @ 799f36c

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