/* PR.C --- a file print utility for Epson and Gemini printers */ #include "bdscio.h" #define WYSE_50 /* comment out if WYSE-50 isn't being used */ #define EPSON /* comment out if Epson printer isn't being used, Gemini printer will then be presumed */ #define LF 10 #define ESC 27 #define STDLST 2 #define STDERR 4 #ifdef EPSON #define PRN_CS 15 #else #define PRN_CS "\033B\003" /* ctrl-O reportedly doesn't always work on Gemini */ #endif #define PRN_NS "\033O" #define PRN_RP "\033@" #define PRN_S5 "\033A\005" #define PRN_S8 "\0330" #define PRN_SS "\033S\001" #ifdef WYSE_50 #define HM_WIDTH 46 #endif #define BOOLEAN char int num_flags; char pr_buffer[BUFSIZ]; BOOLEAN cflag, fflag, hflag, iflag, nflag, sflag, tflag; main(argc, argv) int argc; char *argv[]; { char *fname; int s, i, loop, n, page_length; wildexp(&argc, &argv); cflag = fflag = hflag = iflag = nflag = sflag = tflag = FALSE; if (argc == 1 || (argc == 2 && *argv[1] == '-')) pr_usage(); if (*argv[1] == '-') { for (i = 1; s = argv[1][i]; i++) { switch (s) { case 'C': cflag = TRUE; break; case 'F': fflag = TRUE; break; case 'H': hflag = TRUE; break; case 'I': iflag = TRUE; break; case 'N': nflag = TRUE; cflag = TRUE; break; case 'S': sflag = TRUE; cflag = TRUE; break; case 'T': tflag = TRUE; break; default: pr_usage(); break; } } } num_flags = cflag + fflag + hflag + iflag + nflag + sflag + tflag; n = (num_flags == 0) ? 0 : 1; fprintf(STDLST, "%s%s", PRN_RP, PRN_NS); if (sflag) { #ifdef EPSON fprintf(STDLST, "%c%s%s", PRN_CS, PRN_SS, PRN_S5); #else fprintf(STDLST, "%s%s%s", PRN_CS, PRN_SS, PRN_S5); #endif page_length = 144; } else if (cflag) { #ifdef EPSON fprintf(STDLST, "%c%s", PRN_CS, PRN_S8); #else fprintf(STDLST, "%c%s", PRN_CS, PRN_S8); #endif page_length = 80; } else page_length = 60; for (loop = 1 + n; loop < argc; loop++) { if (fopen(argv[loop], pr_buffer) == ERROR) { fprintf(STDERR, "\tError: unable to open %s\n", argv[loop]); continue; } if ((fname = strchr(argv[loop], ':')) == NULL) fname = argv[loop]; else fname++; pr_text(fname, page_length); fclose(pr_buffer); } fprintf(STDLST, "%s", PRN_RP); #ifdef WYSE_50 hostmsg(" "); #endif exit(); } void pr_usage() { exit(fprintf(STDERR, "\tUsage: pr [-cfhinst] [u/d:]filename.ext\n")); } void pr_text(filename, page_length) char *filename; int page_length; { char line[MAXLINE], raw_line[MAXLINE]; int position, line_count, line_number, page_number; page_number = 1; line_number = line_count = 0; pr_title(filename, page_number); while (fgets(raw_line, pr_buffer)) { if (++line_count > page_length) { pr_title(filename, ++page_number); line_count = 1; } position = left_margin(); if (nflag) { fprintf(STDLST, "%05u: ", ++line_number); position += 8; } translate(raw_line, line, position); fprintf(STDLST, "%s", line); } fprintf(STDLST, "%c", '\f'); } void pr_title(filename, page_number) char *filename; int page_number; { #ifdef WYSE_50 char message[40]; #endif int num_spaces, width, position; width = (cflag) ? 112 + (8 * iflag) : 62 + (5 * iflag); num_spaces = width - strlen(filename); if (page_number != 1) fprintf(STDLST, "%c", '\f'); #ifdef WYSE_50 sprintf(message, "Printing Page %03u of %s", page_number, filename); hostmsg(message); #endif position = left_margin(); fprintf(STDLST, "%s", filename); while (num_spaces--) fprintf(STDLST, "%c", ' '); fprintf(STDLST, "Page %03u\n\n\n", page_number); } void translate(source, destination, position) char *source, *destination; int position; { int tab_interval, c, num_spaces; tab_interval = (tflag) ? 4 : 8; while (*source) { c = (hflag) ? *source++ : *source++ & 0x7F; switch (c) { case '\t': num_spaces = tab_interval - (position % tab_interval); position += num_spaces; while (num_spaces--) *destination++ = ' '; break; default: if (!fflag) if (iscntrl(c)) break; *destination++ = c; position++; break; } } *destination = '\0'; } int left_margin() { int position, num_spaces; if (iflag) return (0); if ((num_flags == 1 && (tflag || fflag || hflag)) || num_flags == 0) num_spaces = 5; else num_spaces = 8; position = num_spaces; while (num_spaces--) fprintf(STDLST, "%c", ' '); return (position); } BOOLEAN iscntrl(c) char c; { if (c == '\t' || c == '\r' || c == LF) return (FALSE); else return ((c < 0x20 || c == 0x7F) ? TRUE : FALSE); } #ifdef WYSE_50 void hostmsg(string) char *string; { int blanks; blanks = HM_WIDTH - strlen(string); fprintf(STDERR, "%cF%s", ESC, string); while (--blanks) fprintf(STDERR, "%c", ' '); fprintf(STDERR, "%c", '\r'); } #endif char *strchr(string, character) char *string; int character; { while (*string && *string != character) string++; return (*string ? string : NULL); }