Changeset 2a2bb60 for owl.c


Ignore:
Timestamp:
Oct 17, 2003, 9:51:17 PM (21 years ago)
Author:
Erik Nygren <nygren@mit.edu>
Branches:
master, barnowl_perlaim, debian, owl, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
e8b95f8
Parents:
27c3a93
Message:
2.0.9-pre-3
	Added testing feature for capturing stderr messages
	        from commands and displaying them in the errors buffer.
		This feature is disabled by default.
		Enable in owl.h with:  OWL_STDERR_REDIR 1
File:
1 edited

Legend:

Unmodified
Added
Removed
  • owl.c

    r27c3a93 r2a2bb60  
    2121#include <sys/stat.h>
    2222#include "owl.h"
     23
     24#if OWL_STDERR_REDIR
     25#include <sys/ioctl.h>
     26int stderr_replace(void);
     27void stderr_redirect(int rfd);
     28#endif
    2329
    2430static const char fileIdent[] = "$Id$";
     
    4248  ZNotice_t notice;
    4349#endif
     50#if OWL_STDERR_REDIR
     51  int newstderr;
     52#endif
    4453
    4554  argcsave=argc;
     
    136145#endif
    137146  owl_global_set_haveaim(&g);
     147
     148#if OWL_STDERR_REDIR
     149  /* Do this only after we've started curses up... */ 
     150  newstderr = stderr_replace();
     151#endif   
     152
    138153 
    139154  /* create the owl directory, in case it does not exist */
     
    530545      owl_function_makemsg("Unable to handle keypress");
    531546    }
     547
     548#if OWL_STDERR_REDIR
     549    stderr_redirect(newstderr);
     550#endif   
     551
    532552  }
    533553}
     
    552572  fprintf(stderr, "  -t      set the tty name\n");
    553573}
     574
     575
     576
     577#if OWL_STDERR_REDIR
     578
     579/* Replaces stderr with a pipe so that we can read from it.
     580 * Returns the fd of the pipe from which stderr can be read. */
     581int stderr_replace(void) {
     582  int pipefds[2];
     583  if (0 != pipe(pipefds)) {
     584    perror("pipe");
     585    owl_function_debugmsg("stderr_replace: pipe FAILED\n");
     586    return -1;
     587  }
     588    owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]);
     589  if (-1 == dup2(pipefds[1], 2 /*stderr*/)) {
     590    owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno));
     591    perror("dup2");
     592    return -1;
     593  }
     594  return pipefds[0];
     595}
     596
     597/* Sends stderr (read from rfd) messages to a file */
     598void stderr_redirect(int rfd) {
     599  int navail, bread;
     600  char *buf;
     601  /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/
     602  if (rfd<0) return;
     603  if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) {
     604    return;
     605  }
     606  /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/
     607  if (navail<=0) return;
     608  if (navail>256) { navail = 256; }
     609  buf = owl_malloc(navail+1);
     610  bread = read(rfd, buf, navail);
     611  if (buf[navail-1] != '\0') {
     612    buf[navail] = '\0';
     613  }
     614  owl_function_error("Err: %s", buf);
     615  owl_free(buf);
     616}
     617
     618#endif /* OWL_STDERR_REDIR */
Note: See TracChangeset for help on using the changeset viewer.