home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / sys / sun / admin / 9867 < prev    next >
Encoding:
Text File  |  1993-01-02  |  6.9 KB  |  308 lines

  1. Path: sparky!uunet!cis.ohio-state.edu!ucbvax!agate!usenet.ins.cwru.edu!mcs.kent.edu!mcs.kent.edu!doleh
  2. From: doleh@mcs.kent.edu (Yaser K. Doleh)
  3. Newsgroups: comp.sys.sun.admin
  4. Subject: Re: Looking to replace NeWSprint.
  5. Message-ID: <1993Jan2.154440.28368@mcs.kent.edu>
  6. Date: 2 Jan 93 15:44:40 GMT
  7. References: <9212311411.AA09066@hrt213.brooks.af.mil> <1i0ohgINN2pt@iskut.ucs.ubc.ca>
  8. Sender: news@mcs.kent.edu
  9. Reply-To: doleh@mcs.kent.edu
  10. Distribution: comp
  11. Organization: Dept. of Math and Computer Science, Kent State U.
  12. Lines: 293
  13. Nntp-Posting-Host: rabbit.mcs.kent.edu
  14.  
  15. Here is a program that will read a raw pbm file and print it to sparcprinter
  16. We use Ghostscript to print PS files, use dvips to print dvi files.
  17. If you are interested in the script, I will post it too.
  18.  
  19. Enjoy.
  20. ------------------
  21. Yaser Doleh
  22.  
  23. to try it 
  24. sparcprint <file.pbm >/dev/lpvi0
  25.  
  26. --------------------------------------------------------------------------
  27. /* Copright 1992  Yaser K. Doleh & Jeff Bailey */
  28.  
  29. #include <stdio.h>
  30. #include <sys/types.h>
  31. #include <errno.h>
  32. #include <sys/ioctl.h>
  33. #include <sys/asynch.h>
  34. #include <signal.h>
  35. #include "lpviio.h"
  36.  
  37. #define BUFSIZE 256
  38. #define FAIL -1
  39. #define SUCCESS 0
  40. #define RAW_PBM "P4"
  41. #ifndef STATUS_FILE
  42. #define STATUS_FILE  "filter.status"
  43. #endif
  44. #define TRUE 1
  45. #define FALSE 0
  46.  
  47. #define STR_OK   "Hey it worked"
  48. #define WORKING  "I am working on it"
  49. #define BAD_FILE "What the heck was that file !!!"
  50. #define NO_MEMORY "I can't remeber what I was suppose to print"
  51. #define INCOMPLETE "The file was not all there"
  52.  
  53. typedef struct msg_tbl {
  54.     u_char    code;
  55.     char    *msg;
  56. } msg_tbl;
  57.     
  58. struct msg_tbl  err_code_str[] = {
  59.     EMOTOR, "motor malfunction",
  60.     EROS, "optical subsystem malfunction",
  61.     EFUSER, "fuser assembly malfunction",
  62.     XEROFAIL, "imaging subsystem malfunction",
  63.     ILCKOPEN, "printer cover not locked",
  64.     NOTRAY, "tray not (properly) installed",
  65.     NOPAPR, "out of media (paper)",
  66.     XITJAM, "media (paper) jam in exit path",
  67.     MISFEED, "media (paper) jam in feed path",
  68.     WDRUMX, "drum assembly almost expended",
  69.     WDEVEX, "toner cartridge almost expended",
  70.     NODRUM, "drum assembly not (properly) installed",
  71.     NODEVE, "toner cartridge not (properly) installed",
  72.     EDRUMX, "drum assembly requires replacement",
  73.     EDEVEX, "toner cartridge requires replacement",
  74.     ENGCOLD, "fuser warming up",
  75.     TIMEOUT, "printer not responding"
  76. };
  77.  
  78. struct msg_tbl  err_class_str[] = {
  79.     ENGWARN, "non-fatal printer error: ",
  80.     ENGFATL, "fatal printer error: ",
  81.     EDRVR, "fatal communication error: "
  82. };
  83.  
  84. struct queue_el {
  85.     char *buf;
  86.     int  bufs;
  87.     aio_result_t  resultp;
  88. };
  89.  
  90. int exit_err=0;
  91. int write_pending=0;
  92. struct queue_el queue_el;
  93. extern char *sys_errlist[];
  94.  
  95. output_status(dst_str)
  96. char *dst_str;
  97. {
  98.   FILE *fd;
  99.  
  100.   if ((fd = fopen(STATUS_FILE, "w")) == NULL ) {
  101.      perror(STATUS_FILE);
  102.      exit(exit_err);
  103.   }
  104.  
  105.   fprintf(fd,"%s\n",dst_str);
  106.   fclose(fd);
  107. }
  108.  
  109. queue_page(buffer, size)
  110. char *buffer;
  111. int  size;
  112. {
  113.     queue_el.buf = buffer;
  114.     queue_el.bufs = size;
  115.     aiowrite(fileno(stdout), buffer, size, 0, 0, &(queue_el.resultp));
  116.     write_pending = TRUE;
  117.     return;
  118. }
  119.  
  120. int check_page(dst_str)
  121. char *dst_str;
  122. {
  123.     struct lpvi_err err_struct;
  124.     int i;
  125.     char temp[BUFSIZE];
  126.     aio_result_t *wait_return;
  127.  
  128.     strcpy(temp, dst_str);
  129. try_again:
  130.     wait_return = (aio_result_t *)aiowait(NULL);
  131.  
  132.     if (queue_el.resultp.aio_return != queue_el.bufs ) {
  133.         dst_str[0] = (char)NULL;
  134.     ioctl(fileno(stdout), LPVIIOC_TESTIO, &err_struct);
  135.  
  136.     if (ioctl(fileno(stdout), LPVIIOC_GETERR, &err_struct) < 0) {
  137.         perror("ioctl LPVIIOC_GETERR ");
  138.         sprintf(dst_str, "ioctl: LPVIIOC_GETERR, %s",  sys_errlist[errno]);
  139.     } else {
  140.         for(i = 0; i < (sizeof(err_class_str) / sizeof(msg_tbl)); i++ ){
  141.         if (err_class_str[i].code == err_struct.err_type) {
  142.              strcpy(dst_str, err_class_str[i].msg);
  143.              break;
  144.         }
  145.         }
  146.         
  147.         for(i = 0; i < (sizeof(err_code_str) / sizeof(msg_tbl)); i++ ){
  148.         if (err_code_str[i].code == err_struct.err_code) {
  149.              strcat(dst_str, err_code_str[i].msg);
  150.              break;
  151.         }
  152.         }
  153.  
  154.         output_status(dst_str);
  155.     }
  156.  
  157.     if (err_struct.err_type == ENGWARN) {
  158.         sleep(5);
  159.         queue_page(queue_el.buf,queue_el.bufs);
  160.         goto try_again;
  161.     }
  162.         if (err_struct.err_type != 0) {
  163.         free(queue_el.buf);
  164.         write_pending = FALSE;
  165.         return(FAIL);
  166.     } 
  167.     } 
  168.     
  169.     strcpy(dst_str, temp);
  170.     output_status(dst_str);
  171.     free(queue_el.buf);
  172.     write_pending = FALSE;
  173.     return(SUCCESS);
  174. }
  175.  
  176. int print_a_page(buffer, size, dst_str)
  177. char *buffer, *dst_str;
  178. int  size;
  179. {
  180.     if (write_pending)
  181.     {
  182.     if (check_page(dst_str) == FAIL) return(FAIL);
  183.     }
  184.  
  185.     queue_page(buffer, size);
  186.     return(SUCCESS);
  187. }
  188.  
  189. char *fetch_line(buf)
  190. char *buf;
  191. {
  192.     char *ret;
  193.  
  194.     while (((ret = gets(buf)) != (char *)NULL) && buf[0] == '#');
  195.     return(ret);
  196. }
  197.  
  198. main(argc, argv)
  199. int argc;
  200. char *argv[];
  201. {
  202.   char line[BUFSIZE], dst_str[BUFSIZE], *buffer;
  203.   int  pixel_width, byte_width, height, i, c;
  204.   struct lpvi_page page;
  205.   int top=0, left=0, res=300;
  206.   int center=FALSE;
  207.  
  208.   signal(SIGPIPE, SIG_IGN);
  209.  
  210.   for (i = 1; i < argc; i++)
  211.   {
  212.     if (!strcmp(argv[i], "-r"))
  213.     {
  214.     res = atoi(argv[++i]);
  215.  
  216.     if ((res != 300) && (res != 400))
  217.     {
  218.         fprintf(stderr,"Invalid printer resolution: %d\n", res);
  219.         exit(-1);
  220.     }
  221.     continue;
  222.     }
  223.  
  224.     if (!strcmp(argv[i], "-c"))
  225.     {
  226.     center = TRUE;
  227.     continue;
  228.     }
  229.   }
  230.  
  231.   strcpy(dst_str, WORKING);
  232.   output_status(dst_str); 
  233.  
  234.   while(fetch_line(line) != (char *)NULL ) {
  235.     if (strcmp(line, RAW_PBM)) {
  236.     fprintf(stderr, BAD_FILE);
  237.     strcpy(dst_str, BAD_FILE);
  238.     exit_err = -1;
  239.     break;
  240.     }
  241.  
  242.     if ((fetch_line(line) == (char *)NULL) ||
  243.     (sscanf(line, "%d %d\n", &pixel_width, &height) != 2))
  244.     {
  245.     fprintf(stderr, BAD_FILE);
  246.     strcpy(dst_str, BAD_FILE);
  247.     exit_err = -1;
  248.     break;
  249.     }
  250.  
  251.     byte_width = (pixel_width + 7) / 8 ;
  252.  
  253.     if ((buffer = (char *)malloc(byte_width*height)) == NULL ) {
  254.     fprintf(stderr, NO_MEMORY);
  255.     strcpy(dst_str, NO_MEMORY);
  256.     exit_err = 1;
  257.     break;
  258.     }
  259.  
  260.     for (i = 0; i <  (byte_width * height); i++ ) {
  261.     if ((c = getchar()) == EOF) {
  262.         fprintf(stderr, INCOMPLETE);
  263.         strcpy(dst_str, INCOMPLETE);
  264.         exit_err = -1;
  265.         height = i / byte_width;
  266.         break;
  267.     }
  268.     buffer[i] = c;
  269.     }
  270.  
  271.     if (center)
  272.     {
  273.     top = ((10.5 * res) - height) / 2;
  274.     left = ((8 * res) - pixel_width) / 2;
  275.     if (top < 0) top = 0;
  276.     if (left < 0) left = 0;
  277.     }
  278.  
  279.     page.bitmap_width = byte_width;
  280.     page.page_width = pixel_width;
  281.     page.page_length = height;
  282.     page.top_margin = top;
  283.     page.left_margin = left;
  284.     page.resolution = res;
  285.  
  286.     if (ioctl(fileno(stdout), LPVIIOC_SETPAGE, &page) != 0) {
  287.     perror("ioctl LPVIIOC_SETPAGE ");
  288.     exit(-1);
  289.     }
  290.     if (print_a_page(buffer, byte_width * height, dst_str) == FAIL) {
  291.     exit_err = -1;
  292.     break;
  293.     }
  294.   }
  295.  
  296.   if (write_pending) {
  297.     if (check_page(dst_str) == FAIL) {
  298.     exit_err = -1;
  299.     }
  300.   }
  301.  
  302.   if (exit_err)
  303.       output_status(dst_str);
  304.   else
  305.       output_status(STR_OK);
  306.   exit(exit_err);
  307. }
  308.