/* PR.C --- a file print utility for Epson and Gemini printers */ #include "stdio.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 LINEMAX 60 #define MAXCOL 105 #define TAB_SIZE 8 #define MAXLINE 255 #ifdef WYSE_50 #define HM_WIDTH 46 #endif #define PRN_NS "\033O" #define PRN_RP "\033@" #define PRN_S5 "\033A\005" #define PRN_S8 "\0330" #define PRN_SS "\033S\001" #define PRN_LSG "\033K\340\001" #ifdef EPSON #define PRN_CS 15 #define PRN_S21 "\033J\025" #else #define PRN_CS "\033B\003" /* ctrl-O reportedly doesn't always work on Gemini */ #define PRN_S21 "\033J\016" /* space 14/144 instead of 21/216 */ #endif #define strchr index /* the more 'modern' name of the function */ typedef int BOOLEAN; extern char chset[96][8]; #ifdef WYSE_50 char message[40]; #endif unsigned pg_num = 1; BOOLEAN cflag = FALSE, lflag = FALSE, nflag = FALSE; main(argc, argv) int argc; char *argv[]; { FILE *fp, *fopen(); char *fname, *index(); int s, i, pg_len = 80; if (argc == 1 || (argc == 2 && *argv[1] == '-') || argc > 3) pr_usage(); if (*argv[1] == '-') { for (i = 1; s = argv[1][i]; i++) { switch (s) { case 'C': cflag = TRUE; pg_len = 144; break; case 'L': lflag = TRUE; break; case 'N': nflag = TRUE; break; default: pr_usage(); break; } } } --argc; if ((fp = fopen(argv[argc], "r")) == NULL) exit(fprintf(stderr, "\tError: unable to open %s\n", argv[argc])); if ((fname = strchr(argv[argc], ':')) == NULL) fname = argv[argc]; else fname++; if (lflag) landscape(fname, fp); else pr_text(fname, pg_len, fp); fclose(fp); #ifdef WYSE_50 hostmsg(" "); #endif exit(); } void pr_usage() { exit(fprintf(stderr, "\tUsage: pr [-cln] [d:]filename.ext\n")); } void pr_text(filename, pg_len, fp) char *filename; int pg_len; FILE *fp; { char line[MAXLINE], raw_line[MAXLINE]; int position, ln_cnt = 0, ln_num = 0; #ifdef EPSON fprintf(stdlst, "%s%s%c", PRN_RP, PRN_NS, PRN_CS); #else fprintf(stdlst, "%s%s%s", PRN_RP, PRN_NS, PRN_CS); #endif if (cflag) fprintf(stdlst, "%s%s", PRN_SS, PRN_S5); else fprintf(stdlst, "%s", PRN_S8); pr_title(filename); while (fgets(raw_line, MAXLINE, fp)) { if (++ln_cnt > pg_len) { pr_title(filename, ++pg_num); ln_cnt = 1; } position = left_margin(); if (nflag) { fprintf(stdlst, "%05u: ", ++ln_num); position += 8; } translate(raw_line, line, position); fprintf(stdlst, "%s", line); } fprintf(stdlst, "%c%s", '\f', PRN_RP); } void pr_title(filename) char *filename; { int num_spaces, width = 112, position; num_spaces = width - strlen(filename); if (pg_num != 1) fprintf(stdlst, "%c", '\f'); #ifdef WYSE_50 sprintf(message, "Printing Page %03u of %s", pg_num, filename); hostmsg(message); #endif position = left_margin(); fprintf(stdlst, "%s", filename); while (num_spaces--) fprintf(stdlst, "%c", ' '); fprintf(stdlst, "Page %03u\n\n\n", pg_num); } void translate(source, destination, position) char *source, *destination; int position; { int num_spaces, ch; while (*source) { ch = *source++ & 0x7F; switch (ch) { case '\t': num_spaces = TAB_SIZE - (position % TAB_SIZE); position += num_spaces; while (num_spaces--) *destination++ = ' '; break; default: if (!special(ch)) { *destination++ = ch; position++; } break; } } *destination = '\0'; } int left_margin() { int num_spaces = TAB_SIZE; while (num_spaces--) fprintf(stdlst, "%c", ' '); return (TAB_SIZE); } BOOLEAN special(ch) char ch; { if (ch == '\t' || ch == '\r' || ch == LF) return (FALSE); else return (iscntrl(ch)); } #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 landscape(filename, fp) char *filename; FILE *fp; { int columns; char pagebuf[LINEMAX][MAXCOL]; fprintf(stdlst, "%s%s", PRN_RP, PRN_NS); do { columns = getpage(fp, pagebuf); fillpage(pagebuf, columns); printpage(filename, pagebuf, columns); } while (!feof(fp)); fprintf(stdlst, "%s", PRN_RP); } int getpage(fp, pagebuf) FILE *fp; char pagebuf[LINEMAX][MAXCOL]; { register char *chptr; register int i = 0, j = 0; int maxcols = 0, character; chptr = &pagebuf[i][0]; do { character = getc(fp); if (character == '\n' || character == EOF || character == '\f') { *chptr = '\0'; if (j > maxcols) maxcols = j; j = 0; chptr = &pagebuf[++i][0]; if (character != '\n') break; } else if (character == '\t') while (j < MAXCOL) { *chptr++ = ' '; j++; if ((j % 8) == 0) break; } else { *chptr++ = character; j++; } if (!(j < MAXCOL)) { j--; *--chptr = '\0'; if (j > maxcols) maxcols = j; j = 0; chptr = &pagebuf[++i][0]; } } while (i < LINEMAX); while (i < LINEMAX) pagebuf[i++][0] = '\0'; return (maxcols); } void fillpage(pagebuf, cols) char pagebuf[LINEMAX][MAXCOL]; int cols; { int len; register int i, j; register char *chptr; for (i = 0; i < LINEMAX; i++) { len = strlen(pagebuf[i]); for (j = len, chptr = &pagebuf[i][len]; j < cols; j++) *chptr++ = ' '; } } void printpage(filename, pagebuf, cols) char *filename, pagebuf[LINEMAX][MAXCOL]; int cols; { char outchar; register int i, j, k; register char *chset_ptr; #ifdef WYSE_50 sprintf(message, "Printing Page %03u of %s", pg_num++, filename); hostmsg(message); #endif for (j = 0; j < cols; j++) { fprintf(stdlst, "%s", PRN_LSG); for (i = LINEMAX - 1; i >= 0; i--) { outchar = pagebuf[i][j]; chset_ptr = &chset[outchar - ' '][0]; for (k = 0; k < 8; k++) putc(*chset_ptr++, stdlst); } fprintf(stdlst, "%s%c", PRN_S21, '\r'); } fprintf(stdlst, "%c", '\f'); }