/* * ------------------------------------------------------------------------ * PRINT.C * ------------------------------------------------------------------------ * by Russ Wetmore for Star Systems Software, Inc. * (Version 1.1 10 Oct 1985) * ------------------------------------------------------------------------ * This is a quick and dirty file lister * The main reason I wrote this was to print source files which * have tabs that aren't 8 characters apart. (I use tabs 4 apart * for C programming.) This implies (naturally) that if you're going * to be editing this file with MINCE, your tabs should be set to 4. * ------------------------------------------------------------------------ * (As an aside, this is also an example of using the "standard" I/O * functions supplied in the Alcyon (DRI) C package which aren't * "officially" supported, using my STDIO.H header file.) * ------------------------------------------------------------------------ * See PRINT.DOC for info on how to invoke this program. * ------------------------------------------------------------------------ */ #include "stdio.h" /* #include "ctype.h" */ #include "define.h" #include "osbind.h" /* alter these values to those for your printer */ /* Width of page in characters for condensed mode (12 cpi) */ #define CONDWID 96 /* Length of page in lines for 8 lines per inch */ #define CONDLEN 88 /* Width of page in characters for normal mode (10 cpi) */ #define NORMWID 80 /* Length of page in lines for 6 lines per inch */ #define NORMLEN 66 /* Default number of spaces per tab position */ #define DEFTABS 8 /* Print string which selects condensed print */ #define CONDPRT "\033\027" /* Print string which selects 8 lines per inch */ #define EIGHTPER "\0338" /* Print string which selects normal print */ #define NORMPRT "\033\023" /* Print string which selects 6 lines per inch */ #define SIXPER "\0336" /* Print string which does a form feed (top of form) */ #define FF "\014" /* Print string which does a CR/LF (Note, your printer may only need CR!) */ #define CRLF "\015\012" #define DAPRT 0 #define DAAUX 1 #define DACON 2 #define DAMIDI 3 #define DAIKBD 4 #define void /**/ /* global variables */ char *filenam, *datitle, UnkSwi[] = "Unknown switch: "; short lnnum, pgnum, tabval, titval, widval, lenval; static void putlst(c) char c; { /* Why? 'Cause, opening PRT: doesn't give you this flexibility */ while (!Bcostat(DAPRT)) { /* if printer isn't ready... */ if (Bconstat(DACON)) { /* if key pending... */ if ((char)Bconin(DACON) == 0x03) { /* & key is ^C... */ printf("\nUSER ABORT!\n\n"); /* PUNT! */ exit(1); } } } Bconout(DAPRT, c); /* else, put char to printer */ } static void spaces(i) short i; /* Print specified number of spaces to printer, with test for abort */ { short j; for (j = 0; j < i; ++j, putlst(' ') ) ; /* cheap C construct */ } static void PrtUsage(s1, s2) char *s1, *s2; /* User goofed - remind him of proper invocation params */ { if (s1) printf("ERROR: %s%s\n\n", s1, s2); /* This is cheap - if filename or switches aren't provided, the program should ask for them. This is an MS-DOS type exit */ printf("Usage: PRINT [D:]FILENAME.EXT [-C] [-T] [-S#] [-W#] [-L#]\n"); printf("(spaces between switches!)\n"); printf(" -C = condensed print (default: normal)\n"); printf(" -T = page titles (default: none)\n"); printf(" -S# = space between tabs (default: %3d)\n", DEFTABS); printf(" -W# = printer width (default: %3d)\n", NORMWID); printf(" -L# = page length in lines (default: %3d)\n", NORMLEN); printf("\nPress any key... "); while ( !Bconstat(DACON) ) ; /* wait for a key */ Bconin(DACON); /* (then, swallow it (necessary?)) */ exit(1); } static void StrPrt(s) char *s; /* Put a null terminated string to the printer, with test for abort */ { while (*s) putlst(*s++); } static void doTitle() /* Execute a CR/LF and print title if time to */ { if (++lnnum > lenval - 3) { /* if we're three lines from bottom... */ if (pgnum++) StrPrt(FF); /* if not first page, send a FF */ StrPrt(CRLF); /* flush print buffer */ if (titval) { /* if user wants a title, print one */ sprintf(datitle, "%s - page %3d%s", filenam, pgnum, CRLF); StrPrt(datitle); } else StrPrt(CRLF); /* no title, so blank line instead */ StrPrt(CRLF); /* in either case, another CRLF */ lnnum = 4; /* line count is now 4 */ } else StrPrt(CRLF); /* not time for title, just CRLF */ } void main(argc, argv) short argc; char *argv[]; /* THE program. "argc" and "argv" are standard UNIX type param passing */ { FILE *in_file; short c, i, j, k; char *s; if (argc < 2) PrtUsage(0, 0); /* not enough info to go on. Punt */ tabval = DEFTABS; /* Set up default values */ widval = NORMWID; lenval = NORMLEN; titval = FALSE; StrPrt(NORMPRT); StrPrt(SIXPER); /* Set printer to default mode */ filenam = argv[1]; /* point to filename which to print */ for (i = 2; i < argc; ++i) { /* parse out switches */ if (argv[i][0] != '-') PrtUsage(UnkSwi, argv[i]); j = atoi(&argv[i][2]); /* get numeric argument, if any */ switch ( toupper(argv[i][1]) ) { /* Condensed print */ case 'C': StrPrt(CONDPRT); StrPrt(EIGHTPER); lenval = CONDLEN; widval = CONDWID; break; /* Do a title at top of each page */ case 'T': titval = TRUE; break; /* Set tab distance */ case 'S': tabval = j; break; /* Set page width */ case 'W': widval = j; break; /* Set page length */ case 'L': lenval = j; break; /* HUH? Unknown switch provided */ default: PrtUsage(UnkSwi, argv[i]); break; } } if ( !(in_file = fopen(argv[1], "r")) ) { printf("Couldn't open %s for input!\n", argv[1]); exit(1); } s = calloc(widval); /* allocate space for print line buffer */ datitle = calloc(widval); /* and for title */ pgnum = 0; lnnum = lenval; /* init page # to 0 and set up ln count */ while ( fgets(s, widval, in_file) ) { /* fetch a line from the file */ doTitle(); /* do a title if requested */ for (i = j = 0; j < strlen(s); ++j) { switch (c = s[j]) { /* TAB */ case 9: k = tabval - (i % tabval); spaces(k); i += k; /* LF */ case 10: /* skip CR or LF 'cause we do it ourselves */ /* CR */ case 13: break; /* other... */ default: if (c < 32 || c > 126) c = '.'; /* fix non ascii */ putlst(c); ++i; break; } } } printf("\n\nFile printed!\n"); fclose(in_file); StrPrt(CRLF); StrPrt(FF); /* flush print buffer and top form */ free(s); free(datitle); /* free calloc'ed strings (necessary?) */ }