bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

readpipe patch


From: Florian Krohm
Subject: readpipe patch
Date: Fri, 3 May 2002 11:55:05 -0400 (EDT)

Here is the a patch to make readpipe more portable. Inspiration was
taken from diffutils. I changed the calling convention to avoid va_list.
The TODO file mentions portability to DOS. 
I do not have access to such a system and know close to nothing about
what kind of functions thas OS provides. So, I'm not sure whether my
patch improves portability to DOS. 
The patch includes a new file lib/readpipe.h and a cvs diff.

Florian


--------- snip snip -------------- lib/readpipe.h ----------------------
/*  Copyright (C) 2002 Free Software Foundation, Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#ifndef READPIPE_H
# define READPIPE_H 1

# include <stdio.h>

# ifndef PARAMS
#  if defined PROTOTYPES || (defined __STDC__ && __STDC__)
#   define PARAMS(proto) proto
#  else
#   define PARAMS(proto) ()
#  endif
# endif

FILE *read_pipe PARAMS ((const char **argv));
void  close_pipe PARAMS ((FILE *));

#endif /* !READPIPE_H */

------------------------ snip snip ------------- cvs diff follows ---------

Index: bison/ChangeLog
===================================================================
RCS file: /cvsroot/bison/bison/ChangeLog,v
retrieving revision 1.661
diff -r1.661 ChangeLog
0a1,7
> 2002-05-03  Florian Krohm  <address@hidden>
> 
>       * configure.in (AC_FUNC_FORK): Added
>       * lib/readpipe.c: Rewritten to be more portable
>       * lib/readpipe.h: New
>       * src/output.c (output_skeleton): Adapted for new read_pipe.
> 
Index: bison/configure.in
===================================================================
RCS file: /cvsroot/bison/bison/configure.in,v
retrieving revision 1.63
diff -r1.63 configure.in
96a97
> AC_FUNC_FORK
Index: bison/lib/readpipe.c
===================================================================
RCS file: /cvsroot/bison/bison/lib/readpipe.c,v
retrieving revision 1.1
diff -r1.1 readpipe.c
1,2c1,2
< /* Open a pipe to read from a program without intermediary sh.
<    Copyright (C) 1992, 1997 Free Software Foundation, Inc.
---
> /* Open a pipe to read from a program.
>    Copyright (C) 1992, 1997, 2002 Free Software Foundation, Inc.
18c18
< /* Written by David MacKenzie.  */
---
> /* Based on code written by David MacKenzie.  */
24,31d23
< #include <stdio.h>
< 
< #if __STDC__
< #include <stdarg.h>
< #else
< #include <varargs.h>
< #endif
< 
32a25
> # include <sys/types.h>
36,41c29,33
< /* Open a pipe to read from a program without intermediary sh.  Checks
<    PATH.  Sample use:
< 
<    stream = readpipe ("progname", "arg1", "arg2", (char *) 0);
< 
<    Return 0 on error.  */
---
> #ifndef _POSIX_VERSION
> # ifndef STDOUT_FILENO
> #  define STDOUT_FILENO 1
> # endif
> #endif
43,45c35,36
< #if __STDC__
< FILE *
< readpipe (char *progname, ...)
---
> #if STDC_HEADERS
> # include <string.h>
47,48c38,40
< FILE *
< readpipe (va_alist)
---
> # if HAVE_STRINGS_G
> #  include <strings.h>
> # endif
49a42,52
> 
> #include <errno.h>
> #include "quotearg.h"
> #include "xalloc.h"
> #include "readpipe.h"
> 
> /* Open a pipe to read from a program. ARGV represents the argument list of 
> the
>    invoked program and must be NULL-terminated. On error, the function returns
>    NULL and errno can be queried for the specific cause of failure. */
> FILE *
> read_pipe (const char **argv)
51,53c54,57
< #if ! __STDC__
<   char *progname;
< #endif
---
>   FILE *fp;
> 
> #if HAVE_WORKING_FORK || HAVE_WORKING_VFORK
> 
55,69c59,62
<   va_list ap;
<   char *args[100];
<   int argno = 0;
< 
<   /* Copy arguments into `args'. */
< #if __STDC__
<   va_start (ap, progname);
< #else
<   va_start (ap);
<   progname = va_arg (ap, char *);
< #endif
<   args[argno++] = progname;
<   while ((args[argno++] = va_arg (ap, char *)) != NULL)
<     ;
<   va_end (ap);
---
>   pid_t pid;
> 
>   if (pipe (fds) != 0)
>     return NULL;
71,72c64
<   if (pipe (fds) == -1)
<     return 0;
---
>   pid = vfork ();
74c66,69
<   switch (fork ())
---
>   if (pid == -1)
>     return NULL;
> 
>   if (pid == 0)
76,78c71,73
<     case 0:                   /* Child.  Write to pipe. */
<       close (fds[0]);         /* Not needed. */
<       if (fds[1] != 1)                /* Redirect 1 (stdout) only if needed.  
*/
---
>       /* Child */
>       close (fds[0]);    /* Close read end */
>       if (fds[1] != STDOUT_FILENO)
80,86c75,78
<         close (1);            /* We don't want the old stdout. */
<         if (dup (fds[1]) == 0)/* Maybe stdin was closed. */
<           {
<             dup (fds[1]);     /* Guaranteed to dup to 1 (stdout). */
<             close (0);
<           }
<         close (fds[1]);       /* No longer needed. */
---
>         if (dup2 (fds[1], STDOUT_FILENO) != STDOUT_FILENO)
>           return NULL;
>         
>         close (fds[1]);
88,94c80,117
<       execvp (args[0], args);
<       _exit (2);              /* 2 for `cmp'. */
<     case -1:                  /* Error. */
<       return 0;
<     default:                  /* Parent.  Read from pipe. */
<       close (fds[1]);         /* Not needed. */
<       return fdopen (fds[0], "r");
---
>       
>       /* The cast to (char **) is needed for portability to older
>        hosts with a nonstandard prototype for execvp.  */
>       execvp (argv[0], (char **)argv);
>       
>       _exit (errno == ENOEXEC ? 126 : 127);
>     }
>   
>     /* Parent */
>   close (fds[1]);    /* Close write end */
>   
>   fp = fdopen (fds[0], "r");
> 
> #else 
> 
>   /* Open pipe using popen. Since popen invokes a shell under the covers
>      we need to escape the arguments. */
>   char *command, *s, *quoted;
>   const char **p;
>   unsigned buf_size;
> 
>   buf_size = 0;
>   for (p = argv; *p; ++p) 
>     {
>       buf_size += strlen (quotearg_style (shell_quoting_style, *p)) + 1;
>     }
>     
>   command = s = xmalloc (buf_size);
>   quoted = quotearg_style (shell_quoting_style, argv[0]);
>   strcpy (s, quoted);
>   s += strlen (quoted);
>               
>   for (p = argv + 1; *p; ++p) 
>     {
>       *s++ = ' ';
>       quoted = quotearg_style (shell_quoting_style, *p);
>       strcpy (s, quoted);
>       s += strlen (quoted);
95a119,135
>   *s = '\0';
> 
>   fp = popen (command, "r");
>   free (command);
> #endif
> 
>   return fp;
> }
> 
> void
> close_pipe (FILE *fp)
> {
> #if HAVE_WORKING_FORK || HAVE_WORKING_VFORK
>   fclose (fp);
> #else
>   pclose (fp);
> #endif
Index: bison/src/output.c
===================================================================
RCS file: /cvsroot/bison/bison/src/output.c,v
retrieving revision 1.150
diff -r1.150 output.c
106,108c106
< 
< /* From lib/readpipe.h.  */
< FILE *readpipe PARAMS ((const char *, ...));
---
> #include "readpipe.h"
1006a1005
>     const char *argv[7];
1017,1022c1016,1025
<     skel_in = readpipe (m4,
<                       "-I", bison_pkgdatadir,
<                       "m4sugar/m4sugar.m4",
<                       tempfile,
<                       skeleton,
<                       NULL);
---
>     argv[0] = m4;
>     argv[1] = "-I";
>     argv[2] = bison_pkgdatadir;
>     argv[3] = "m4sugar/m4sugar.m4";
>     argv[4] = tempfile;
>     argv[5] = skeleton;
>     argv[6] = NULL;
> 
>     skel_in = read_pipe (argv);
> 
1025a1029,1030
> 
>     close_pipe (skel_in);



reply via email to

[Prev in Thread] Current Thread [Next in Thread]