source: owl.c @ f350fc3

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