1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/*
This program is similar to a program of the same name found on UNIX.
It prints the files named in the command tail with headings
except as modified below.
usage: pr [-i -ln -on -pname -tn -wn] file1[ file2 ... filen]
where: -i = accept files from stdin
-ln = set lines per page to n
-on = set page offset to n
-pname = output to file <name>
-tn = set tabs to n cols
-wn = set page width to n
note: the expr 'PAGE(mesg)' found in col 1 will cause a formfeed
and the 'mesg' to be included in the title line on this and
each subsequent page until EOF or another PAGE.
*/
#include <stdio.h>
#define TAB_DEFAULT 4
#define PAGE_LENGTH 60
#define PAGE_OFFSET 0
#define PAGE_WIDTH 80
#define MAX_ARGS 70
#define MAX_FILES 64
#define PATH_LENGTH 63
#define PAGE(head)
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
int page_length = PAGE_LENGTH;
int page_offset = PAGE_OFFSET;
int page_width = PAGE_WIDTH;
int tab_width = TAB_DEFAULT;
char *xargv[ MAX_ARGS ];
unsigned xargc;
char filenames [MAX_FILES] [PATH_LENGTH + 1];
char *print_name = "PRN:";
extern long atoi();
char title [80];
char date [20];
char time [20];
int ln, pn;
PAGE (MAIN)
main(argc, argv) /* copy file to printer */
int argc;
char *argv [];
{
FILE *file, *lp;
int fi = 0;
int read_stdin = FALSE;
int pn;
char *cp;
if (argc < 2) /* No args so tell 'em how it works */
{
fprintf(stderr,
"usage:\n\npr %s %s\n\n",
"[-i] [-lnn] [-onn] [-p<name>] [-tn] [-wnn]",
"[file1[ file2 ... filen]]");
fprintf(stderr,
"where: i = read 'stdin' for filenames to print\n");
fprintf(stderr,
" l = lines-per-page and nn <= 120\n");
fprintf(stderr,
" o = page offset and nn <= 120\n");
fprintf(stderr,
" p = print redirection and\n");
fprintf(stderr,
" <name> = pathname or devicename\n");
fprintf(stderr,
" t = spaces-per-tab and n <= 8\n");
fprintf(stderr,
" w = page width and nn <= 160\n\n");
fprintf(stderr,
"Notes: PAGE(<title text of any length>) in col 1 of text file\n");
fprintf(stderr,
" and <title text...> the title you want.\n\n");
fprintf(stderr,
" C pgms should include the following macro:\n\n");
fprintf(stderr,
" #define PAGE(title)\n\n");
fprintf(stderr,
" < and > not required and should not be used\n\n");
exit(0);
}
xargc = xargs("pr", argc, argv, xargv, MAX_ARGS);
for (pn = 0; pn < xargc; pn++)
{
if (*xargv[pn] == '-')
{
cp = xargv[pn] + 1;
switch (tolower(*cp))
{
case 'i':/* wants help */
read_stdin = TRUE;
break;
case 'l':/* page length change */
page_length = (int) atoi(cp + 1);
if ((page_length <= 0) || (page_length > 120))
page_length = PAGE_LENGTH;
break;
case 'p':/* direct output to file */
print_name = cp + 1;
break;
case 't':/* tab width change */
tab_width = (int) atoi(cp + 1);
if ((tab_width <= 0) || (tab_width > 8))
tab_width = TAB_DEFAULT;
break;
case 'o':/* page offset change */
page_offset = (int) atoi(cp + 1);
if ((page_offset < 0) || (page_offset > 120))
page_offset = PAGE_OFFSET;
break;
case 'w':/* page width change */
page_width = (int) atoi(cp + 1);
if ((page_width <= 0) || (page_width > 160))
page_width = PAGE_WIDTH;
break;
default:
fprintf(stderr, "pr: Invalid option = %s\n",
xargv[pn]);
}
}
else /* must be a path name */
{
if (fi < MAX_FILES)
strcpy(filenames[fi++], xargv[pn]);
else
{
fprintf(stderr, "pr: "
"Exceeded maximum file capacity\n");
break;
}
}
}
if ((lp = fopen(print_name, "w")) == 0)
{
fprintf(stderr, "pr: Unable to open %s as output\n", print_name);
exit(1);
}
if (read_stdin)
{
for(;;)
{
if (fi == MAX_FILES)
{
fputs("pr: Exceeded maximum file capacity\n",
stderr);
break;
}
cp = gets(filenames [fi], PATH_LENGTH);
if (!cp)
break;
else fi++;
}
}
/* now print each file */
for (pn = 0; pn < fi; pn++)
prt(filenames [pn], lp); /* print the file */
}
PAGE (NEW PAGE)
new_page (fnp, lp)
char *fnp;
FILE *lp;
{
if (ln < 3)
return;
++pn;
if (pn > 1)
fputc('\f', lp);
fprintf(lp, "%s %s %s PAGE %d: %s\n\n",
fnp, date, time, pn, title);
ln = 2;
}
PAGE (PRINT FILE)
prt (fnp, lp)
char fnp[];
FILE *lp;
{
FILE *inp_file;
int i, j, col;
char line [256], *st, *et, *sp;
inp_file = fopen(fnp, "r");
if (!inp_file)
{
fprintf(stderr, "pr: unable to open %s\n", fnp);
return;
}
else
fprintf(stderr, "pr: printing %s\n", fnp);
pn = 0;
ln = 999;
gdates(date); /* get date */
gtimes(time); /* and time */
*title = '\0';
while (fgets(line, 256, inp_file))
{
if (strncmp(line, "PAGE", 4) == 0)
{
if (st = index(line, '('))
{
et = index(line, ')');
strncpy(title, st + 1, (et) ? et - st - 1 : 160);
}
ln = 999;
}
if (ln > page_length)
new_page(fnp, lp);
if (page_offset)
indent(lp);
for (col = (page_offset) ? page_offset : 1, sp = &line[0];
*sp; sp++)
{
switch (*sp)
{
case '\t': /* tab character */
do
{
fputc(' ', lp);
col++;
if (col > page_width)
{
fputc('\n', lp);
col = (page_offset) ? page_offset : 1;
ln++;
if (ln > page_length)
new_page(fnp, lp);
if (page_offset)
indent(lp);
break;
}
} while ((col - 1) % tab_width);
break;
case '\f': /* form feed character */
new_page(fnp, lp);
break;
default:
fputc(*sp, lp);
++col;
if (col > page_width)
{
fputc('\n', lp);
col = (page_offset) ? page_offset - 1 : 0;
ln++;
if (ln > page_length)
new_page(fnp, lp);
if (page_offset)
indent(lp);
}
}
} /* of line print (for) */
++ln;
} /* of while not eof */
fclose(inp_file);
fputc(014, lp);
} /* of print */
indent(lp)
FILE *lp;
{
int i;
for(i = 1; i < page_offset; i++)
fputc(' ', lp);
}
|