home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
213_01
/
dior.c
< prev
next >
Wrap
Text File
|
1979-12-31
|
15KB
|
636 lines
/* DIOR.C VERS:- 01.00 DATE:- 09/26/86 TIME:- 09:36:32 PM */
/*
%CC1 $1.C -X
*/
/*
Description:
IO redirection library from BDS C and Van Nuys Toolkit,
by Eugene H. Mallory.
Minor modification:
avoid CR-CR pair before LF by patch in putchar() of DIOR.C and by
adding declaration of _putc_last in DIO.H;
By J.A. Rupley, Tucson, Arizona
Coded for BDS C compiler, version 1.50a
*/
/*********************************************************************
* DIO *
* Written by Gene Mallory 12/24/83 *
**********************************************************************
* COPYRIGHT 1983 EUGENE H. MALLORY *
*********************************************************************/
/*
Redirection can take the following forms:
> fid Output redirection to file
< fid Input rediredtion from file
+ fid Output redirection to file and screen
~ fid Output redirection to file and printer
>> fid Append to file
+> fid Append to file and output to screen
~> fid Append to file and output to printer
> LST: Output to LST device
>> LST: As above
+ LST: Output to LST device and screen
> LP1: Output to LP1 (IOBYTE = 0x80)
>> LP1: As above
+ LP1: As above and to screen
> LP2: Output to LP2 (IOBYTE = 0xC0)
>> LP2: As above
+ LP2: As above and to screen
> OUT: Output to PUNCH DEVICE
>> OUT: As above
+ OUT: As above and to screen
~ OUT: As above and to printer
< IN: Input from READER device
| prog Pipe output to next program
+| prog As above and to screen
~| prog As above and to printer
Redirection and pipe character may be separated from
the file or program name by white space or not.
File specifications may be complete with user number (##) and
disk name (d) in the form:
##/d:name
Pipe specification will be first tried as is, if the file
does not exist, disk A, user 0 will be tried using the same
name.
Control C will abort program and submit file execution.
When inputing from keyboard use Control C except at
column 1. (CP/M catches it there.) If an old version of
the output file existed, it will be recovered by Control C.
It is permissable to input and output from the same file,
BUT the input specification must be first and the output
specification must be second! otherwise the order and
location of the redirection specifications is not
significant.
Thus, a C program using redirection has the following form:
#include "bdscio.h" /* standard header file */
#include "dio.h" /* directed I/O header */
... /* other externals, if any */
main(argc,argv)
char **argv;
BEGIN
... /* declarations */
dioinit(&argc,argv) /* initialize redirection */
wildexp(&argc,&argv)/* wildexp goes here if used */
... /* body of program */
dioflush(); /* Must exit here to close */
END
*/
/*
NOTES:
1. Redirection and pipes work only for TEXT. This mechanism
should not be used for binary data.
2. Use CLINK FOO DIO or L2 FOO DIO to force
linking with all the dio routines before the DEFFs are
searched.
3. Library routines GETCHAR, PUTCHAR, PRINTF, PUTS are
caught and redirected.
4. New routines TYPEF, TYPECHAR FORCE OUTPUT TO
SCREEN.
5. New routine ERROR, may be used like TYPEF to
output a message, close out the DIO operations
and exit.
*/
#include "bdscio.h"
#include "dio.h"
/*********************************************************************
* DIOINIT *
*********************************************************************/
dioinit(argcptr, argvect)
int *argcptr;
char **argvect;
{
int i, c0, c1;
_outptr = STDERR;
_conline = MAXLINE;
_conbufp = _conbuf;
_conbuf[0] = 0;
_iobyte = bdos(7, 0); /* save iobyte */
_screenflag = _pipeflag = _dioinflag = FALSE;
_printerflag = _appendflag = _rdrflag = FALSE;
for (i = 1; i < *argcptr;)
{
c0 = argvect[i][0];
c1 = argvect[i][1];
if (c0 == '+')
_screenflag = TRUE;
if (c0 == '~')
_printerflag = TRUE;
if ((c0 == '+' || c0 == '~') && c1 == '|')
{
strshift(argvect[i], 1);
_dio_pipe(i, argcptr, argvect);
}
else
if ((c0 == '+' || c0 == '~') && c1 == '>')
{
_dioappend(i, argcptr, argvect);
}
else
if (c0 == '>' && c1 == '>')
{
_dioappend(i, argcptr, argvect);
}
else
if (c0 == '>' || (c0 == '+' || c0 == '~'))
{
_dioout(i, argcptr, argvect);
}
else
if (c0 == '<')
{
_dioin(i, argcptr, argvect);
}
else
if (c0 == '|')
{
_dio_pipe(i, argcptr, argvect);
}
else
{
i++;
}
}
}
/*********************************************************************
* _DIOOUT *
*********************************************************************/
_dioout(n, argcptr, argvect)
int *argcptr, n;
char **argvect;
{
char backupfile[20];
_outptr = _dobuf;
if (argvect[n][0] == '+')
_screenflag = TRUE;
if (strlen(argvect[n]) == 1)
shiftarg(n, argcptr, argvect);
else
strshift(argvect[n], 1);
if (!strcmp(argvect[n], "LST:"))
_outptr = STDLST;
else
if (!strcmp(argvect[n], "OUT:"))
_outptr = STDPUN;
else
if (!strcmp(argvect[n], "LP1:"))
{
_outptr = STDLST;
bdos(8, (_iobyte & 0x3f) | 0x80);
}
else
if (!strcmp(argvect[n], "LP2:"))
{
_outptr = STDLST;
bdos(8, (_iobyte & 0x3f) | 0xC0);
}
else
{
strcpy(_oldfile, argvect[n]);
getprefix(backupfile, _oldfile);
strcat(backupfile, "BACKUP.$$$");
unlink(backupfile);
if (DIOIN && (strcmp(argvect[n], _infile) == 0))
{
fclose(_dibuf);
if (rename(argvect[n], backupfile) == -1)
{
fprintf(STDERR, "DIO: Failed to rename %s to %s.\n",
argvect[n], backupfile);
dioexit();
}
if (fopen(backupfile, _dibuf) == ERROR)
{
fprintf(STDERR, "DIO: Unable to open file %s for input.\n"
, backupfile);
fprintf(STDERR, "DIO: Error caused by %s.\n", errmsg(errno()));
dioexit();
}
}
else
{
rename(argvect[n], backupfile);
}
if (fcreat(argvect[n], _dobuf) == ERROR)
{
fprintf(STDERR, "DIO: Unable to open file %s for output.\n"
, argvect[n]);
fprintf(STDERR, "DIO: Error caused by %s.\n", errmsg(errno()));
dioexit();
}
}
shiftarg(n, argcptr, argvect);
}
/*********************************************************************
* _DIO_PIPE
*********************************************************************/
_dio_pipe(n, argcptr, argvect)
int *argcptr, n;
char **argvect;
{
if (strlen(argvect[n]) == 1)
shiftarg(n, argcptr, argvect);
else
strshift(argvect[n], 1);
_pipeflag = 1;
_outptr = _dobuf;
_pargv = &argvect[n];
_pargc = *argcptr - n;
*argcptr = n;
if (fcreat("TEMPOUT.$$$", _dobuf) == ERROR)
{
fprintf(STDERR, "DIO: Unable to open file TEMPOUT.$$$ for pipe.\n");
fprintf(STDERR, "DIO: Error caused by %s.\n", errmsg(errno()));
dioexit();
}
}
/*********************************************************************
* _DIOAPPEND
*********************************************************************/
_dioappend(n, argcptr, argvect)
int *argcptr, n;
char **argvect;
{
_appendflag = TRUE;
_outptr = _dobuf;
if (argvect[n][0] == '+')
_screenflag = TRUE;
if (strlen(argvect[n]) == 2)
shiftarg(n, argcptr, argvect);
else
strshift(argvect[n], 2);
if (!strcmp(argvect[n], "LST:"))
_outptr = STDLST;
else
if (!strcmp(argvect[n], "OUT:"))
_outptr = STDPUN;
else
if (!strcmp(argvect[n], "LP1:"))
{
_outptr = STDLST;
bdos(8, (_iobyte & 0x3f) | 0x80);
}
else
if (!strcmp