/*------------------------------------------------------*/ /* hpduplex.c remote laser printer driver for HP */ /* LaserJet4 with double-sided printing capability */ /* */ /* Put executable in /usr/lib/lpdfilters/hpduplex */ /* */ /* (Currently controlled by psi.ece.jhu.edu) */ /* (See /etc/printcap entry on psi) */ /*------------------------------------------------------*/ /*------------------------------------------------------*/ /* Written by Philippe O. Pouliquen and Tim Edwards */ /* Last modified: May 5, 1995 */ /*------------------------------------------------------*/ /*----------------------------------------------------------------------------------*/ /* Only machine types I know how to compile for: DEC and SGI. Set appropriately. */ /* (DEC is BSD, SGI is SYSV.) */ /*----------------------------------------------------------------------------------*/ /* #define DEC */ #define SGI /*----------------------------------------------------------------------------------*/ #include #include #include #include #include #include #include #include #include /*----------------------------------------------------------------------------------*/ /* To force double-sided printing on and off for this file only: */ /*----------------------------------------------------------------------------------*/ char duplex_temp[] = "%!\r\nstatusdict begin true setduplexmode end\r\n"; /*----------------------------------------------------------------------------------*/ /* Note: to set double-sided printing on permanently, use: */ /* char duplex_on[] = */ /* "%!\r\nserverdict begin 0 exitserver statusdict begin true */ /* setduplexmode end\r\n"; */ /* */ /* To set single-sided printing temporarily (hpsimplex), use: */ /* char simplex_temp[] = */ /* "%!\r\nstatusdict begin false setduplexmode end\r\n"; */ /* */ /* And to set single-sided printing permanently, use: */ /* char simplex_on[] = */ /* "%!\r\nserverdict begin 0 exitserver statusdict begin false */ /* setduplexmode end\r\n"; */ /*----------------------------------------------------------------------------------*/ #define fdin 0 /* File descriptor for standard input */ #define fdout 1 /* File descriptor for standard output */ /*----------------------------------------------------------------------------------*/ main(argc,argv) int argc; char *argv[]; { int hp1, i = 0; struct sockaddr_in hp1_addr; char c; /* Set up internet connection to printer hp1 */ if ((hp1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { perror("Unable to create socket"); exit(1); } hp1_addr.sin_family = AF_INET; hp1_addr.sin_port = htons(9100); /*------------------------------------*/ /* hp1's inet address = 128.220.14.86 */ /* = hex 0x560EDC80 */ /*------------------------------------*/ #ifdef DEC hp1_addr.sin_addr.S_un.S_addr = 0x560EDC80; #else hp1_addr.sin_addr.s_addr = 0x560EDC80; #endif if (connect(hp1, &hp1_addr, sizeof(struct sockaddr_in)) < 0) { perror("Unable to connect to printer hp1"); exit(1); } i = 1; if (setsockopt(hp1, IPPROTO_TCP, TCP_NODELAY, &i, sizeof(int)) < 0) { perror("setsockopt"); goto end; } /* send duplex-temp announcement to hp1 */ /* to set double-sided printing. */ write(hp1, duplex_temp, strlen(duplex_temp)); /*----------------------------------------------------------------*/ /* Read characters from standard input; hp1 takes \r\n in place */ /* of newline. . . treat as a special case. */ /*----------------------------------------------------------------*/ for(;;) { if (read(fdin, &c, 1) == 0) break; if ((c != '\n') && (c != '\r')) write(hp1, &c, 1); else { c = '\r'; write(hp1, &c, 1); c = '\n'; write(hp1, &c, 1); } } ioctl(hp1, FIONREAD, &i); if (i < 0) { perror("ioctl(FIONREAD)"); goto end; } else if (i > 0) { for (; i > 0; i--) { read(hp1, &c, 1); /* print newline and carriage-return as-is */ if ((c == '\n') || (c == '\r')) write(fdout, &c, 1); /* otherwise, print control characters nicely */ /* either in carat (^N) notation or octal ([\nnn]) */ else if ((c < 32) || (c > 126)) { i = c; if (i < 32) { c = '^'; write(fdout, &c, 1); c = i + 64; write(fdout, &c, 1); } else if (i > 126) { c = '['; write(fdout, &c, 1); c = '\\'; write(fdout, &c, 1); c = ((i & 0x00C0) >> 6) + 48; write(fdout, &c, 1); c = ((i & 0x0038) >> 3) + 48; write(fdout, &c, 1); c = ((i & 0x0007)) + 48; write(fdout, &c, 1); c = ']'; write(fdout, &c, 1); } } else write(fdout, &c, 1); } } end: close(hp1); /* send newline to stdout */ c = '\n'; write(fdout, &c, 1); }