[comp.sys.dec] redirection of stdio, et al.

minow@decvax.UUCP (Martin Minow) (04/27/87)

In article <224@unc.cs.unc.edu> mcguffey@unc.cs.unc.edu (Michael McGuffey)
requests a method to allow command line redirection under VMS.  What I
use follows the signature.

Martin Minow
decvax!minow

main(argc, argv)
int		argc;
char		*argv[];
{
#ifdef vms
	argc = getredirection(argc, argv);
#endif
	...
}
--------
/*
 * getredirection() is intended to aid in porting C programs
 * to VMS (Vax-11 C) which does not support '>' and '<'
 * I/O redirection.  With suitable modification, it may
 * useful for other portability problems as well.
 */

static int
getredirection(argc, argv)
int		argc;
char		**argv;
/*
 * Process vms redirection arg's.  Exit if any error is seen.
 * If getredirection() processes an argument, it is erased
 * from the vector.  getredirection() returns a new argc value.
 *
 * Warning: do not try to simplify the code for vms.  The code
 * presupposes that getredirection() is called before any data is
 * read from stdin or written to stdout.
 *
 * Normal usage is as follows:
 *
 *	main(argc, argv)
 *	int		argc;
 *	char		*argv[];
 *	{
 *		argc = getredirection(argc, argv);
 *	}
 */
{
#ifdef	vms
	register char		*ap;	/* Argument pointer	*/
	int			i;	/* argv[] index		*/
	int			j;	/* Output index		*/
	int			file;	/* File_descriptor 	*/
	extern int		errno;	/* Last vms i/o error 	*/

	for (j = i = 1; i < argc; i++) {   /* Do all arguments	*/
	    switch (*(ap = argv[i])) {
	    case '<':			/* <file		*/
		if (freopen(++ap, "r", stdin) == NULL) {
		    perror(ap);		/* Can't find file	*/
		    exit(errno);	/* Is a fatal error	*/
		}

	    case '>':			/* >file or >>file	*/
		if (*++ap == '>') {	/* >>file		*/
		    /*
		     * If the file exists, and is writable by us,
		     * call freopen to append to the file (using the
		     * file's current attributes).  Otherwise, create
		     * a new file with "vanilla" attributes as if
		     * the argument was given as ">filename".
		     * access(name, 2) is TRUE if we can write on
		     * the specified file.
		     */
		    if (access(++ap, 2) == 0) {
			if (freopen(ap, "a", stdout) != NULL)
			    break;	/* Exit case statement	*/
			perror(ap);	/* Error, can't append	*/
			exit(errno);	/* After access test	*/
		    }			/* If file accessable	*/
		}
		/*
		 * On vms, we want to create the file using "standard"
		 * record attributes.  create(...) creates the file
		 * using the caller's default protection mask and
		 * "variable length, implied carriage return"
		 * attributes. dup2() associates the file with stdout.
		 */
		if ((file = creat(ap, 0, "rat=cr", "rfm=var")) == -1
		 || dup2(file, fileno(stdout)) == -1) {
		    perror(ap);		/* Can't create file	*/
		    exit(errno);	/* is a fatal error	*/
		}			/* If '>' creation	*/
		break;			/* Exit case test	*/

	    default:
		argv[j++] = ap;		/* Not a redirector	*/
		break;			/* Exit case test	*/
	    }
	}				/* For all arguments	*/
	return (j);
#else
	/*
	 * Note: argv[] is referenced to fool the Decus C
	 * syntax analyser, supressing an unneeded warning
	 * message.
	 */
	return (argv[0], argc);		/* Just return as seen	*/
#endif
}

gdw@ssl-macc.co.uk (Grenville Whelan) (04/30/87)

In article <224@unc.cs.unc.edu> mcguffey@unc.cs.unc.edu (Michael McGuffey) writes:
>
>Does anyone know of any tools that allow command line redirection of
>stdio under VMS. Something similar to the way unix and msdos does it is
>preferable to some of the methods that have previously been proposed.
>
>-mike mcguffey


Simply redirect the logical SYS$OUTPUT to a file rather than the terminal,
ie.
     $ ASSIGN <FILE> SYS$OUTPUT
     $ <commands>
     $ DEASSIGN SYS$OUTPUT
-- 
# Grenville Whelan  -  Software Sciences Ltd, Park Street, Macclesfield, UK  #
# (EMAIL gdw@uk.co.ssl-macc) (UUCP !mcvax!ukc!mcvax!gdw) (TEL +44 625 29241) #

      "Are you the Police?"  "No ma'am, we're musicians!" -- Elwood.