home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of Shareware - Software Farm 2
/
wosw_2.zip
/
wosw_2
/
PRINTING
/
P2_10.ZIP
/
PAGE2.C
next >
Wrap
C/C++ Source or Header
|
1991-11-14
|
9KB
|
354 lines
/*
* PAGE2 - program to print files on both sides of the paper
*
* Usage: PAGE2 /m:margin /f /b /r file
*
* /m:margin Allows a margin to be specified: positive values add that
* many spaces at the start of each line, negative values can
* also be used to trim leading spaces. WARNING: negative
* values cause ANY leading characters to be trimmed, so if too
* big a value is used, data may be lost
* /f Print only the front (odd numbered) pages
* /b Print only the back (even numbered) pages - only one of /f or /b
* should be given, if both are given the last one will be the one
* used.
* /r Print back pages in reverse order - this is intended for use on
* laser printers where the printout of the front pages produces
* a stack of paper that is put back in to the printer. If a
* tractor feed printer is in use, the /r option should not be used
* since the order of printing is the same for both front and back.
*
* This program creates a temporary file to hold the back page information,
* it will check both the TEMP and TMP environment variables looking for
* a directory to use. Use of a ramdisk will speed things up, although
* make sure there is enough space to hold the temporary file, it will be
* about half the size of the input file.
*
* Revision History:
*
* V1.0 11/14/91 - David goodenough Initial release
*
* This code is released to the public domain, you are permitted to distribute
* this freely, and modify it as necessary to suit you own needs. However if
* you distribyte a revised copy, this entire header message must remain
* intact and any changes should be clearly marked as your own work.
*
* If you find this useful I'd like to hear from you. Also I'd like any bug
* reports you come up with, so that I can fix them. I can be reached via
* E-mail at:
*
* RIME: ->COCONINO - use the PROGRAMR conference,
* send to David Goodenough
* Fidonet: David Goodenough at 1:125/28
* GEnie: DAVID-CPM
* CI$: >INTERNET:dg%pallio.UUCP@cs.sfsu.edu
* UUCP: pallio!dg
* Internet: dg%pallio.UUCP@cs.sfsu.edu
*
* or if you can't get any of the above to work, a postcard to:
*
* David Goodenough
* 1236 15th. Ave.
* San Francisco, CA
* 94122
*
* would be nice. Thanks!
*/
static char copyright[] =
"(C) Copyright 1991, David Goodenough. All rights reserved.";
#include <stdio.h>
#include <stdlib.h>
#include <clib.h>
#define PRINT 0
#define SAVE 1
#define BOTH 0
#define FRONT 1
#define BACK 2
struct _page_
{
struct _page_ *_next;
long _pos;
int _size;
};
struct _page_ *back = (struct _page_ *) NULL;
struct _page_ *curr;
int target;
int eofseen = FALSE;
int reverse = FALSE;
int margin = 0;
int p1 = 0;
int p2 = 0;
int mode = BOTH;
FILE *ifp, *tfp;
main(n, a)
char **a;
{
int i;
int ch;
char *cp;
struct _page_ *work;
char tmpname[256];
if (n >= 2)
{
cp = a[1];
if (*cp == '/' || *cp == '-')
{
a++;
n--;
if ((ch = *++cp | 0x20) == 'f')
mode = FRONT;
else if (ch == 'b')
mode = BACK;
else if (ch == 'r')
reverse = TRUE;
else if (ch == 'm' && *++cp == ':')
margin = atoi(++cp);
else
{
printf("Usage: PAGE2 [/m:margin /f /b /r] file\n");
exit(1);
}
}
}
if (n < 2)
{
printf("Usage: PAGE2 [/m:margin /f /b /r] file\n");
exit(1);
}
if ((ifp = fopen(a[1], "rb")) == (FILE *) NULL)
{
printf("Error: can't open %s\n", a[1]);
exit(2);
}
if (mode != FRONT)
{
if (((cp = getenv("TEMP")) != (char *) NULL ||
(cp = getenv("TMP")) != (char *) NULL) && *cp)
{
strcpy(tmpname, cp);
namefix(tmpname);
if (tmpname[strlen(tmpname) - 1] != '\\')
strcat(tmpname, "\\");
}
else
tmpname[0] = 0;
strcat(tmpname, "$PAGE$.TMP");
if ((tfp = fopen(tmpname, "w+")) == (FILE *) NULL)
{
printf("Error: can't creat temp file\n");
exit(3);
}
}
eofseen = FALSE;
prinit(FALSE);
for (;;)
{
target = PRINT;
sendpage();
makedata();
if (eofseen)
break;
target = SAVE;
sendpage();
if (eofseen)
break;
}
if (mode != FRONT)
{
if (back == (struct _page_ *) NULL)
{
printf("No back pages to print\n");
exit(0);
}
printf("Front pages done, please reload paper\nHit <ENTER> when ready: ");
while ((xkb() & 255) != 13)
;
printf("\n");
if (p1 != p2)
prchar('\f');
for (work = back; work != (struct _page_ *) NULL; work = work->_next)
{
fseek(tfp, work->_pos, 0L);
for (i = 0; i < work->_size; i++)
{
if ((ch = getc(tfp)) == EOF)
break;
prchar(ch);
}
}
}
printf("Done.\n");
fclose(ifp);
if (mode != FRONT)
{
fclose(tfp);
unlink(tmpname);
}
exit(0);
}
sendpage()
{
int lines;
int chars;
int ch;
lines = chars = 0;
while (lines < 66)
{
if ((ch = getmarg()) == EOF)
{
eofseen = TRUE;
if (lines == 0 && chars == 0)
return;
else
ch = '\f';
}
switch (ch)
{
case '\r':
chars = 0;
break;
case '\n':
lines++;
break;
case '\b':
if (chars >= margin)
chars--;
break;
case '\f':
lines = 66;
break;
default:
{
if (++chars == 80)
{
chars = 0;
lines++;
}
}
break;
}
savech(ch);
}
if (target == PRINT)
p1++;
else
p2++;
}
savech(ch)
{
if (target == PRINT)
{
if (mode != BACK)
prchar(ch);
}
else if (mode != FRONT)
{
putc(ch, tfp);
curr->_size++;
}
}
makedata()
{
struct _page_ *work;
if ((work = (struct _page_ *) malloc(sizeof(struct _page_))) ==
(struct _page *) NULL)
{
eofseen = TRUE;
printf("Out of memory\n");
return;
}
if (reverse || back == (struct _page_ *) NULL)
{
work->_next = back;
curr = back = work;
}
else
{
for (curr = back; curr->_next != (struct _page_ *) NULL;
curr = curr->_next)
;
curr->_next = work;
curr = work;
curr->_next = (struct _page_ *) NULL;
}
curr->_pos = ftell(tfp);
curr->_size = 0;
}
getmarg()
{
int ch;
static int pos = -1;
static int add = 0;
static int swallow = 0;
if (pos == -1)
{
if (margin > 0)
add = margin;
else if (margin < 0)
swallow = -margin;
pos = 0;
}
for (;;)
{
if (add)
{
add--;
return(' ');
}
switch (ch = getc(ifp))
{
case EOF:
case '\n':
case '\f':
return(ch);
case '\r':
{
pos = -1;
return(ch);
}
case '\b':
{
if (pos)
pos--;
return(ch);
}
case '\t':
{
add = 8 - (pos & 7);
pos += add;
while (add && swallow)
{
add--;
swallow--;
}
continue;
}
default:
{
pos++;
if (swallow)
{
swallow--;
continue;
}
return(ch);
}
}
}
}