source: owl.c @ a0a5179

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since a0a5179 was a0a5179, checked in by James M. Kretchmar <kretch@mit.edu>, 20 years ago
blist works for AIM even if the zephyr .anyone can't be read AIM Buddy debugging
  • Property mode set to 100644
File size: 20.7 KB
Line 
1/*
2 * Copyright 2001 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that the above copyright notice and this
7 * permission notice appear in all copies and in supporting
8 * documentation.  No representation is made about the suitability of
9 * this software for any purpose.  It is provided "as is" without
10 * express or implied warranty.
11 */
12
13#include <stdio.h>
14#include <unistd.h>
15#include <stdlib.h>
16#include <string.h>
17#include <signal.h>
18#include <time.h>
19#include <sys/param.h>
20#include <sys/types.h>
21#include <sys/stat.h>
22#include "owl.h"
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);
32void stderr_redirect(int rfd);
33#endif
34
35static const char fileIdent[] = "$Id$";
36
37owl_global g;
38
39int main(int argc, char **argv, char **env) {
40  WINDOW *recwin, *sepwin, *typwin, *msgwin;
41  owl_editwin *tw;
42  owl_popwin *pw;
43  int j, ret, initialsubs, debug, argcsave, followlast;
44  int newmsgs, zpendcount, nexttimediff;
45  struct sigaction sigact;
46  char *configfile, *tty, *perlout, *perlerr, **argvsave, buff[LINE], startupmsg[LINE];
47  owl_filter *f;
48  owl_style *s;
49  time_t nexttime, now;
50  struct tm *today;
51  char *dir;
52#ifdef HAVE_LIBZEPHYR
53  ZNotice_t notice;
54#endif
55#if OWL_STDERR_REDIR
56  int newstderr;
57#endif
58
59  owl_function_debugmsg("startup: it's the very best place to start");
60
61  owl_function_debugmsg("startup: processing command line arguments");
62  argcsave=argc;
63  argvsave=argv;
64  configfile=NULL;
65  tty=NULL;
66  debug=0;
67  initialsubs=1;
68  if (argc>0) {
69    argv++;
70    argc--;
71  }
72  while (argc>0) {
73    if (!strcmp(argv[0], "-n")) {
74      initialsubs=0;
75      argv++;
76      argc--;
77    } else if (!strcmp(argv[0], "-c")) {
78      if (argc<2) {
79        fprintf(stderr, "Too few arguments to -c\n");
80        usage();
81        exit(1);
82      }
83      configfile=argv[1];
84      argv+=2;
85      argc-=2;
86    } else if (!strcmp(argv[0], "-t")) {
87      if (argc<2) {
88        fprintf(stderr, "Too few arguments to -t\n");
89        usage();
90        exit(1);
91      }
92      tty=argv[1];
93      argv+=2;
94      argc-=2;
95    } else if (!strcmp(argv[0], "-d")) {
96      debug=1;
97      argv++;
98      argc--;
99    } else if (!strcmp(argv[0], "-v")) {
100      printf("This is owl version %s\n", OWL_VERSION_STRING);
101      exit(0);
102    } else {
103      fprintf(stderr, "Uknown argument\n");
104      usage();       
105      exit(1);
106    }
107  }
108
109#ifdef HAVE_LIBZEPHYR
110  /* zephyr init */
111  owl_function_debugmsg("startup: initializing zephyr");
112  ret=owl_zephyr_initialize();
113  if (ret) {
114    exit(1);
115  }
116#endif
117 
118  /* signal handler */
119  owl_function_debugmsg("startup: setting up signal handler");
120  sigact.sa_handler=sig_handler;
121  sigemptyset(&sigact.sa_mask);
122  sigact.sa_flags=0;
123  sigaction(SIGWINCH, &sigact, NULL);
124  sigaction(SIGALRM, &sigact, NULL);
125
126  /* screen init */
127  owl_function_debugmsg("startup: initializing screen");
128  sprintf(buff, "TERMINFO=%s", TERMINFO);
129  putenv(buff);
130  initscr();
131  start_color();
132#ifdef HAVE_USE_DEFAULT_COLORS
133  use_default_colors();
134#endif
135  raw();
136  noecho();
137
138  /* define simple color pairs */
139  if (has_colors() && COLOR_PAIRS>=8) {
140    init_pair(OWL_COLOR_BLACK,   COLOR_BLACK,   -1);
141    init_pair(OWL_COLOR_RED,     COLOR_RED,     -1);
142    init_pair(OWL_COLOR_GREEN,   COLOR_GREEN,   -1);
143    init_pair(OWL_COLOR_YELLOW,  COLOR_YELLOW,  -1);
144    init_pair(OWL_COLOR_BLUE,    COLOR_BLUE,    -1);
145    init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, -1);
146    init_pair(OWL_COLOR_CYAN,    COLOR_CYAN,    -1);
147    init_pair(OWL_COLOR_WHITE,   COLOR_WHITE,   -1);
148  }
149
150  /* owl global init */
151  owl_function_debugmsg("startup: doing owl global initialization");
152  owl_global_init(&g);
153  if (debug) owl_global_set_debug_on(&g);
154  owl_global_set_startupargs(&g, argcsave, argvsave);
155#ifdef HAVE_LIBZEPHYR
156  owl_global_set_havezephyr(&g);
157#endif
158  owl_global_set_haveaim(&g);
159
160#if OWL_STDERR_REDIR
161  /* Do this only after we've started curses up... */
162  owl_function_debugmsg("startup: doing stderr redirection");
163  newstderr = stderr_replace();
164#endif   
165
166  /* create the owl directory, in case it does not exist */
167  owl_function_debugmsg("startup: creating owl directory, if not present");
168  dir=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_CONFIG_DIR);
169  mkdir(dir, S_IRWXU);
170  owl_free(dir);
171
172  /* set the tty, either from the command line, or by figuring it out */
173  owl_function_debugmsg("startup: setting tty name");
174  if (tty) {
175    owl_global_set_tty(&g, tty);
176  } else {
177    owl_global_set_tty(&g, owl_util_get_default_tty());
178  }
179
180  /* setup the built-in styles */
181  owl_function_debugmsg("startup: creating built-in styles");
182  s=owl_malloc(sizeof(owl_style));
183  owl_style_create_internal(s, "default", &owl_stylefunc_default,
184                            "Default message formatting");
185  owl_global_add_style(&g, s);
186
187  s=owl_malloc(sizeof(owl_style));
188  owl_style_create_internal(s, "basic", &owl_stylefunc_basic,
189                            "Basic message formatting.");
190  owl_global_add_style(&g, s);
191#if 0
192  s=owl_malloc(sizeof(owl_style));
193  owl_style_create_internal(s, "vt", &owl_stylefunc_vt,
194                            "VT message formatting.");
195  owl_global_add_style(&g, s);
196#endif
197  s=owl_malloc(sizeof(owl_style));
198  owl_style_create_internal(s, "oneline", &owl_stylefunc_oneline,
199                            "Formats for one-line-per-message");
200  owl_global_add_style(&g, s);
201
202  /* setup the default filters */
203  /* the personal filter will need to change again when AIM chat's are
204   *  included.  Also, there should be an %aimme% */
205  owl_function_debugmsg("startup: creating default filters");
206  f=owl_malloc(sizeof(owl_filter));
207  owl_filter_init_fromstring(f, "personal", "( type ^zephyr$ "
208                             "and class ^message$ and instance ^personal$ "
209                             "and ( recipient ^%me%$ or sender ^%me%$ ) ) "
210                             "or ( type ^aim$ and login ^none$ )");
211  owl_list_append_element(owl_global_get_filterlist(&g), f);
212
213  f=owl_malloc(sizeof(owl_filter));
214  owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )");
215  owl_list_append_element(owl_global_get_filterlist(&g), f);
216
217  f=owl_malloc(sizeof(owl_filter));
218  owl_filter_init_fromstring(f, "ping", "opcode ^ping$");
219  owl_list_append_element(owl_global_get_filterlist(&g), f);
220
221  f=owl_malloc(sizeof(owl_filter));
222  owl_filter_init_fromstring(f, "auto", "opcode ^auto$");
223  owl_list_append_element(owl_global_get_filterlist(&g), f);
224
225  f=owl_malloc(sizeof(owl_filter));
226  owl_filter_init_fromstring(f, "login", "not login ^none$");
227  owl_list_append_element(owl_global_get_filterlist(&g), f);
228
229  f=owl_malloc(sizeof(owl_filter));
230  owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$");
231  owl_list_append_element(owl_global_get_filterlist(&g), f);
232
233  f=owl_malloc(sizeof(owl_filter));
234  owl_filter_init_fromstring(f, "out", "direction ^out$");
235  owl_list_append_element(owl_global_get_filterlist(&g), f);
236
237  f=owl_malloc(sizeof(owl_filter));
238  owl_filter_init_fromstring(f, "aim", "type ^aim$");
239  owl_list_append_element(owl_global_get_filterlist(&g), f);
240
241  f=owl_malloc(sizeof(owl_filter));
242  owl_filter_init_fromstring(f, "zephyr", "type ^zephyr$");
243  owl_list_append_element(owl_global_get_filterlist(&g), f);
244
245  f=owl_malloc(sizeof(owl_filter));
246  owl_filter_init_fromstring(f, "none", "false");
247  owl_list_append_element(owl_global_get_filterlist(&g), f);
248
249  f=owl_malloc(sizeof(owl_filter));
250  owl_filter_init_fromstring(f, "all", "true");
251  owl_list_append_element(owl_global_get_filterlist(&g), f);
252
253  /* set the current view */
254  owl_function_debugmsg("startup: setting the current view");
255  owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default"));
256
257  /* AIM init */
258  owl_function_debugmsg("startup: doing AIM initialization");
259  owl_aim_init();
260
261  /* process the startup file */
262  owl_function_debugmsg("startup: processing startup file");
263  owl_function_source(NULL);
264
265  /* read the config file */
266  owl_function_debugmsg("startup: processing config file");
267  owl_context_set_readconfig(owl_global_get_context(&g));
268  perlerr=owl_perlconfig_readconfig(configfile);
269  if (perlerr) {
270    endwin();
271    fprintf(stderr, "\nError parsing configfile: %s\n", perlerr);
272    exit(1);
273  }
274
275  /* if the config defines a formatting function, add 'perl' as a style */
276  if (owl_global_is_config_format(&g)) {
277    owl_function_debugmsg("Found perl formatting");
278    s=owl_malloc(sizeof(owl_style));
279    owl_style_create_perl(s, "perl", "owl::_format_msg_legacy_wrap",
280                          "User-defined perl style that calls owl::format_msg"
281                          "with legacy global variable support");
282    owl_global_add_style(&g, s);
283    owl_global_set_default_style(&g, "perl");
284  }
285
286  /* execute the startup function in the configfile */
287  owl_function_debugmsg("startup: executing perl startup, if applicable");
288  perlout = owl_perlconfig_execute("owl::startup();");
289  if (perlout) owl_free(perlout);
290 
291  /* hold on to the window names for convenience */
292  msgwin=owl_global_get_curs_msgwin(&g);
293  recwin=owl_global_get_curs_recwin(&g);
294  sepwin=owl_global_get_curs_sepwin(&g);
295  typwin=owl_global_get_curs_typwin(&g);
296  tw=owl_global_get_typwin(&g);
297
298  /* welcome message */
299  owl_function_debugmsg("startup: creating splash message");
300  strcpy(startupmsg, "-----------------------------------------------------------------------\n");
301  sprintf(buff,      "Welcome to Owl version %s.  Press 'h' for on-line help. \n", OWL_VERSION_STRING);
302  strcat(startupmsg, buff);
303  strcat(startupmsg, "                                                                       \n");
304  strcat(startupmsg, "If you would like to receive release announcements about Owl you can   \n");
305  strcat(startupmsg, "join the owl-users@mit.edu mailing at http://web.mit.edu/ktools/       \n");
306  strcat(startupmsg, "                                                                 ^ ^   \n");
307  strcat(startupmsg, "                                                                 OvO   \n");
308  strcat(startupmsg, "Please report any bugs or suggestions to bug-owl@mit.edu        (   )  \n");
309  strcat(startupmsg, "-----------------------------------------------------------------m-m---\n");
310  owl_function_adminmsg("", startupmsg);
311  sepbar(NULL);
312
313  wrefresh(sepwin);
314
315  /* load zephyr subs */
316  if (initialsubs) {
317    owl_function_debugmsg("startup: loading initial zephyr subs");
318    /* load normal subscriptions */
319    ret=owl_zephyr_loadsubs(NULL);
320    if (ret==-1) {
321      owl_function_adminmsg("", "Error loading zephyr subscriptions, file inaccessable");
322    } else if (ret!=0) {
323      owl_function_adminmsg("", "Error loading zephyr subscriptions");
324    }
325   
326    if (ret!=-1) {
327      owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
328    }
329
330    /* load login subscriptions */
331    if (owl_global_is_loginsubs(&g)) {
332      owl_function_debugmsg("startup: loading login subs");
333      owl_function_loadloginsubs(NULL);
334    }
335  }
336
337  /* set the startup and default style, based on userclue and presence of a
338   * formatting function */
339  owl_function_debugmsg("startup: setting startup and default style");
340  if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) {
341    /* the style was set by the user: leave it alone */
342  } else if (owl_global_is_config_format(&g)) {
343    owl_global_set_default_style(&g, "perl");
344  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
345    owl_global_set_default_style(&g, "default");
346  } else {
347    owl_global_set_default_style(&g, "basic");
348  }
349
350  /* zlog in if we need to */
351  if (owl_global_is_startuplogin(&g)) {
352    owl_function_debugmsg("startup: doing zlog in");
353    owl_zephyr_zlog_in();
354  }
355
356  owl_function_debugmsg("startup: set style for the view");
357  owl_view_set_style(owl_global_get_current_view(&g), 
358                     owl_global_get_style_by_name(&g, owl_global_get_default_style(&g)));   
359
360  owl_function_debugmsg("startup: setting context interactive");
361  owl_context_set_interactive(owl_global_get_context(&g));
362
363  nexttimediff=10;
364  nexttime=time(NULL);
365
366  owl_function_debugmsg("startup: entering main loop");
367  /* main loop */
368  while (1) {
369
370    /* if a resize has been scheduled, deal with it */
371    owl_global_resize(&g, 0, 0);
372
373    /* these are here in case a resize changes the windows */
374    msgwin=owl_global_get_curs_msgwin(&g);
375    recwin=owl_global_get_curs_recwin(&g);
376    sepwin=owl_global_get_curs_sepwin(&g);
377    typwin=owl_global_get_curs_typwin(&g);
378
379    followlast=owl_global_should_followlast(&g);
380   
381    /* Do AIM stuff */
382    if (owl_global_is_doaimevents(&g)) {
383      owl_aim_process_events();
384
385      if (owl_global_is_aimloggedin(&g)) {
386        if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
387          /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
388          owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
389        }
390      }
391    }
392
393    /* little hack */
394    now=time(NULL);
395    today=localtime(&now);
396    if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) {
397      if (time(NULL)>nexttime) {
398        if (nexttimediff==1) {
399          nexttimediff=10;
400        } else {
401          nexttimediff=1;
402        }
403        nexttime+=nexttimediff;
404        owl_hack_animate();
405      }
406    }
407
408    /* Grab incoming messages. */
409    newmsgs=0;
410    zpendcount=0;
411    while(owl_zephyr_zpending() || owl_global_messagequeue_pending(&g)) {
412#ifdef HAVE_LIBZEPHYR
413      struct sockaddr_in from;
414#endif
415      owl_message *m=NULL;
416      owl_filter *f;
417
418      /* grab the new message, stick it in 'm' */
419      if (owl_zephyr_zpending()) {
420#ifdef HAVE_LIBZEPHYR
421        /* grab a zephyr notice, but if we've done 20 without stopping,
422           take a break to process keystrokes etc. */
423        if (zpendcount>20) break;
424        ZReceiveNotice(&notice, &from);
425        zpendcount++;
426       
427        /* is this an ack from a zephyr we sent? */
428        if (owl_zephyr_notice_is_ack(&notice)) {
429          owl_zephyr_handle_ack(&notice);
430          continue;
431        }
432       
433        /* if it's a ping and we're not viewing pings then skip it */
434        if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
435          continue;
436        }
437
438        /* create the new message */
439        m=owl_malloc(sizeof(owl_message));
440        owl_message_create_from_znotice(m, &notice);
441#endif
442      } else if (owl_global_messagequeue_pending(&g)) {
443        m=owl_global_messageuque_popmsg(&g);
444      }
445     
446      /* if this message it on the puntlist, nuke it and continue */
447      if (owl_global_message_is_puntable(&g, m)) {
448        owl_message_free(m);
449        continue;
450      }
451
452      /* otherwise add it to the global list */
453      owl_messagelist_append_element(owl_global_get_msglist(&g), m);
454      newmsgs=1;
455
456      /* let the config know the new message has been received */
457      owl_perlconfig_getmsg(m, 0, NULL);
458
459      /* add it to any necessary views; right now there's only the current view */
460      owl_view_consider_message(owl_global_get_current_view(&g), m);
461
462      /* do we need to autoreply? */
463      if (owl_global_is_zaway(&g)) {
464        if (owl_message_is_type_zephyr(m)) {
465          owl_zephyr_zaway(m);
466        } else if (owl_message_is_type_aim(m)) {
467          /* can't do this, there's no mechanism not to start a loop */
468          /* owl_aim_send_im(owl_message_get_sender(m), owl_global_get_zaway_msg(&g)); */
469        }
470      }
471
472      /* ring the bell if it's a personal */
473      if (owl_global_is_personalbell(&g) &&
474          !owl_message_is_loginout(m) &&
475          !owl_message_is_mail(m) &&
476          owl_message_is_private(m)) {
477        owl_function_beep();
478      }
479
480      /* if it matches the alert filter, do the alert action */
481      f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g));
482      if (f && owl_filter_message_match(f, m)) {
483        owl_function_command(owl_global_get_alert_action(&g));
484      }
485
486      /* check for burning ears message */
487      /* this is an unsupported feature */
488      if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) {
489        char *buff;
490        buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m));
491        owl_function_adminmsg(buff, "");
492        owl_free(buff);
493        owl_function_beep();
494      }
495
496      /* log the message if we need to */
497      if (owl_global_is_logging(&g) || owl_global_is_classlogging(&g)) {
498        owl_log_incoming(m);
499      }
500    }
501
502    /* follow the last message if we're supposed to */
503    if (newmsgs && followlast) {
504      owl_function_lastmsg_noredisplay();
505    }
506
507    /* do the newmsgproc thing */
508    if (newmsgs) {
509      owl_function_do_newmsgproc();
510    }
511   
512    /* redisplay if necessary */
513    /* this should be optimized to not run if the new messages won't be displayed */
514    if (newmsgs) {
515      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
516      sepbar(NULL);
517      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
518        owl_popwin_refresh(owl_global_get_popwin(&g));
519        /* TODO: this is a broken kludge */
520        if (owl_global_get_viewwin(&g)) {
521          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
522        }
523      }
524      owl_global_set_needrefresh(&g);
525    }
526
527    /* if a popwin just came up, refresh it */
528    pw=owl_global_get_popwin(&g);
529    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
530      owl_popwin_refresh(pw);
531      owl_popwin_no_needs_first_refresh(pw);
532      owl_global_set_needrefresh(&g);
533      /* TODO: this is a broken kludge */
534      if (owl_global_get_viewwin(&g)) {
535        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
536      }
537    }
538
539    /* update the terminal if we need to */
540    if (owl_global_is_needrefresh(&g)) {
541      /* leave the cursor in the appropriate window */
542      if (owl_global_is_typwin_active(&g)) {
543        owl_function_set_cursor(typwin);
544      } else {
545        owl_function_set_cursor(sepwin);
546      }
547      doupdate();
548      owl_global_set_noneedrefresh(&g);
549    }
550
551    /* Handle all keypresses.  If no key has been pressed, sleep for a
552     * little bit, but otherwise do not.  This lets input be grabbed
553     * as quickly as possbile */
554    j=wgetch(typwin);
555    if (j==ERR) {
556      usleep(10);
557      continue;
558    }
559    /* find and activate the current keymap.
560     * TODO: this should really get fixed by activating
561     * keymaps as we switch between windows...
562     */
563    if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
564      owl_context_set_popless(owl_global_get_context(&g), 
565                              owl_global_get_viewwin(&g));
566      owl_function_activate_keymap("popless");
567    } else if (owl_global_is_typwin_active(&g) 
568               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
569      /*
570      owl_context_set_editline(owl_global_get_context(&g), tw);
571      owl_function_activate_keymap("editline");
572      */
573    } else if (owl_global_is_typwin_active(&g) 
574               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
575      owl_context_set_editmulti(owl_global_get_context(&g), tw);
576      owl_function_activate_keymap("editmulti");
577    } else {
578      owl_context_set_recv(owl_global_get_context(&g));
579      owl_function_activate_keymap("recv");
580    }
581    /* now actually handle the keypress */
582    ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
583    if (ret!=0 && ret!=1) {
584      owl_function_makemsg("Unable to handle keypress");
585    }
586
587#if OWL_STDERR_REDIR
588    stderr_redirect(newstderr);
589#endif   
590
591  }
592}
593
594void sig_handler(int sig) {
595  if (sig==SIGWINCH) {
596    /* we can't inturrupt a malloc here, so it just sets a flag
597     * schedulding a resize for later
598     */
599    owl_function_resize();
600  }
601}
602
603void usage() {
604  fprintf(stderr, "Owl version %s\n", OWL_VERSION_STRING);
605  fprintf(stderr, "Usage: owl [-n] [-d] [-v] [-h] [-c <configfile>] [-t <ttyname>]\n");
606  fprintf(stderr, "  -n      don't load zephyr subscriptions\n");
607  fprintf(stderr, "  -d      enable debugging\n");
608  fprintf(stderr, "  -v      print the Owl version number and exit\n");
609  fprintf(stderr, "  -h      print this help message\n");
610  fprintf(stderr, "  -c      specify an alternate config file\n");
611  fprintf(stderr, "  -t      set the tty name\n");
612}
613
614
615
616#if OWL_STDERR_REDIR
617
618/* Replaces stderr with a pipe so that we can read from it.
619 * Returns the fd of the pipe from which stderr can be read. */
620int stderr_replace(void) {
621  int pipefds[2];
622  if (0 != pipe(pipefds)) {
623    perror("pipe");
624    owl_function_debugmsg("stderr_replace: pipe FAILED\n");
625    return -1;
626  }
627    owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]);
628  if (-1 == dup2(pipefds[1], 2 /*stderr*/)) {
629    owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno));
630    perror("dup2");
631    return -1;
632  }
633  return pipefds[0];
634}
635
636/* Sends stderr (read from rfd) messages to a file */
637void stderr_redirect(int rfd) {
638  int navail, bread;
639  char *buf;
640  /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/
641  if (rfd<0) return;
642  if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) {
643    return;
644  }
645  /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/
646  if (navail<=0) return;
647  if (navail>256) { navail = 256; }
648  buf = owl_malloc(navail+1);
649  bread = read(rfd, buf, navail);
650  if (buf[navail-1] != '\0') {
651    buf[navail] = '\0';
652  }
653  owl_function_error("Err: %s", buf);
654  owl_free(buf);
655}
656
657#endif /* OWL_STDERR_REDIR */
Note: See TracBrowser for help on using the repository browser.