home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / sys / sun / admin / 8107 < prev    next >
Encoding:
Text File  |  1992-11-09  |  3.7 KB  |  219 lines

  1. Path: sparky!uunet!mcsun!uknet!bcc.ac.uk!link-1.ts.bcc.ac.uk!ucacmsu
  2. From: ucacmsu@ucl.ac.uk (Mr Stephen R Usher)
  3. Newsgroups: comp.sys.sun.admin
  4. Subject: LaserJet printer input filter for Postscript.
  5. Message-ID: <1992Nov10.093344.34229@bas-a.bcc.ac.uk>
  6. Date: 10 Nov 92 09:33:44 GMT
  7. Sender: news@ucl.ac.uk (Usenet News System)
  8. Organization: Bloomsbury Computing Consortium, London
  9. Lines: 208
  10.  
  11.  
  12. Due to the many requests I've had for my filter I've decided to post it
  13. here, so here it comes!
  14.  
  15. Steve
  16.  
  17. --- Cut --- Here ---
  18. /*
  19.  * ljetfilt.c    Version 1.0 10th November 1992.
  20.  *
  21.  * Copyright 1992 S.R.Usher.
  22.  *
  23.  * This source may be freely distributed and modified as long as this
  24.  * copyright notice stays intact.
  25.  *
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include <sys/fcntl.h>
  30. #include <sys/param.h>
  31.  
  32. #define BUFFER_LENGTH 1024
  33. #define GS_COMMAND_STRING "/usr/local/bin/gs -q -dNOPAUSE -sDEVICE=laserjet -sOUTPUTFILE=%s -"
  34.  
  35. #ifdef DEBUGIT
  36. FILE *debug_file;
  37. #endif
  38.  
  39. main(argc, argv)
  40. int argc;
  41. char *argv[];
  42. {
  43.     static char buffer[BUFFER_LENGTH] = { 0 };
  44.     char start_chars[3];
  45.     static int isps = 0;
  46.  
  47.     int infd, outfd;
  48.  
  49.     int error;
  50.  
  51. #ifdef DEBUGIT
  52.     char pathname[MAXPATHLEN];
  53.     int i;
  54.  
  55.     if ((debug_file = fopen("/tmp/ljdebug", "w")) == NULL)
  56.     {
  57.         exit(1);
  58.     }
  59.  
  60.     for (i = 0; i < argc; fprintf(debug_file, "%s ", argv[i++]));
  61.  
  62.     fprintf(debug_file, "\n");
  63.  
  64.     fprintf(debug_file, "Current directory is %s\n", getwd(pathname));
  65.  
  66.     fflush(debug_file);
  67. #endif
  68.     setpgid(0, 0);
  69. #ifdef FILES
  70.     switch (argc)
  71.     {
  72.     case 1:
  73.         break;
  74.     case 2:
  75.         if ((infd = open(argv[1], O_RDONLY)) == -1)
  76.         {
  77.             perror(argv[1]);
  78.             exit(1);
  79.         }
  80.  
  81.         dup2(infd, 0);
  82.         close(infd);
  83.         break;
  84.     case 3:
  85.         if ((infd = open(argv[1], O_RDONLY)) == -1)
  86.         {
  87.             perror(argv[1]);
  88.             exit(1);
  89.         }
  90.  
  91.         if ((outfd = open(argv[2], (O_WRONLY | O_CREAT | O_TRUNC), 0644)) == -1)
  92.         {
  93.             perror(argv[2]);
  94.             exit(1);
  95.         }
  96.  
  97.         dup2(infd, fileno(stdin));
  98.         dup2(outfd, fileno(stdout));
  99.         close(infd);
  100.         close(outfd);
  101.         break;
  102.     default:
  103.         usage(argv[0]);
  104.         exit(1);
  105.     }
  106. #endif /* FILES */
  107.     fgets(buffer, BUFFER_LENGTH - 1, stdin);
  108.  
  109.     if (strlen(buffer) > 2)
  110.     {
  111.         strncpy(start_chars, buffer, 2);
  112.  
  113.         start_chars[2] = '\0';
  114.  
  115.         if (!strcmp(start_chars, "%!"))
  116.             isps = 1;
  117.     }
  118.  
  119.     error = 0;
  120.  
  121.     if (isps)
  122.         error = output_to_gs(buffer);
  123.     else
  124.         error = output_to_file(buffer);
  125.  
  126.     printf("\033(s0P\004\n");
  127.  
  128.     fflush(stdout);
  129.  
  130. #ifdef DEBUGIT
  131.     fprintf(debug_file, "Returning code %d.\n", error);
  132.     fflush(debug_file);
  133.     fclose(debug_file);
  134. #endif
  135.  
  136.     exit(error);
  137. }
  138.  
  139. usage(name)
  140. char *name;
  141. {
  142.     fprintf(stderr, "Usage: %s [input-file [output-file]]\n", name);
  143. }
  144.  
  145. output_to_gs(buffer)
  146. char buffer[BUFFER_LENGTH];
  147. {
  148.     FILE *fp;
  149.     int length;
  150.     char command[1024];
  151.     char *tmpfile;
  152.  
  153.     if ((tmpfile = tempnam("/tmp", "ljflt")) == NULL)
  154.     {
  155.         perror("tempnam");
  156.         return 1;
  157.     }
  158.  
  159.     sprintf(command, GS_COMMAND_STRING, tmpfile);
  160.  
  161. #ifdef DEBUGIT
  162.     fprintf(debug_file, "Command string is \"%s\"\n", command);
  163. #endif
  164.  
  165.     if ((fp = popen(command, "w")) == NULL)
  166.     {
  167.         perror("popen");
  168.         return 1;
  169.     }
  170.  
  171.     fprintf(fp, "%s", buffer);
  172.  
  173.     while (fgets(buffer, BUFFER_LENGTH - 1, stdin) != NULL)
  174.         fprintf(fp, "%s", buffer);
  175.  
  176.     fprintf(fp, "quit\n");
  177.  
  178.     fflush(fp);
  179.  
  180.     pclose(fp);
  181.  
  182.     if ((fp = fopen(tmpfile, "r")) == NULL)
  183.     {
  184.         perror("fopen");
  185.         return 1;
  186.     }
  187.  
  188.     while ((length = read(fileno(fp), buffer, BUFFER_LENGTH)) > 0)
  189.         write(fileno(stdout), buffer, length);
  190.  
  191.     fclose(fp);
  192.  
  193.     unlink(tmpfile);
  194.  
  195.     return 0;
  196. }
  197.  
  198. output_to_file(buffer)
  199. char buffer[BUFFER_LENGTH];
  200. {
  201.     int length;
  202.     
  203.     printf("%s", buffer);
  204.     
  205.     while(fgets(buffer, BUFFER_LENGTH -1 ,stdin) != NULL)
  206.         printf("%s", buffer);
  207.  
  208.     printf("\f");
  209.  
  210.     fflush(stdout);
  211.  
  212.     return 0;
  213. }
  214. --- Cut --- Here ---
  215. --
  216. Addresses:-
  217. JANET:-        ucacmsu@uk.ac.ucl or    steve@uk.ac.ox.earth (preferable)
  218. Internet:-    ucacmsu@ucl.ac.uk or    steve@earth.ox.ac.uk (preferable)
  219.