home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume11 / uport-herc / part01 / g_finish.c < prev    next >
C/C++ Source or Header  |  1990-04-06  |  6KB  |  240 lines

  1. #ifndef lint
  2. static char sccsid[] = "@(#) g_finish.c 5.1 89/02/20";
  3. #endif
  4.  
  5. /*
  6.  *    Copyright (c) David T. Lewis 1987, 1988
  7.  *    All rights reserved.
  8.  *
  9.  *    Permission is granted to use this for any personal noncommercial use.
  10.  *    You may not distribute source or executable code for profit, nor
  11.  *    may you distribute it with a commercial product without the written
  12.  *    consent of the author.  Please send modifications to the author for
  13.  *    inclusion in updates to the program.  Thanks.
  14.  */
  15.  
  16.  
  17. /* Wed Feb 10 20:58:37 EST 1988                        */
  18. /* Detach shared memory and do any other clean up chores.        */
  19. /* If printing, the print job is dispatched to the print spooler.    */
  20. /* This routine is specific to Microport System V/AT, and presumes     */
  21. /* that shared memory segments have been defined (usually at system     */
  22. /* boot time) such that    the shared memory keys are "B8000L" for CGA     */
  23. /* adapter, and so on. */
  24. /*
  25.  *    V386HERC
  26.  *    BILLHATCH - to write printer output to stdout instead of to
  27.  *            print spooler
  28.  *    w. hatch
  29.  *     Sat Feb 17 18:44:57 EST 1990
  30.  */
  31.  
  32. #define MODULE "g_finish"
  33.  
  34. #include "config.h"
  35. #include <stdio.h>
  36. #if MIX_C
  37. #else
  38. #include <sys/types.h>
  39. #include <string.h>
  40. #if TCC
  41. #else
  42. #include <malloc.h>
  43. #endif /* TCC */
  44. #endif /* MIX_C */
  45. #if MS_DOS
  46. extern int g_setmod();
  47. extern int g_text;
  48. #else
  49. #include <sys/ipc.h>
  50. #endif /* MS_DOS */
  51. #if HAS_SIG
  52. #include <signal.h>
  53. #endif /* HAS_SIG */
  54. #include "bitmaps.h"
  55. #include "graphics.h"
  56. #include "modes.h"
  57.  
  58. extern struct GL_graphics graphics;
  59. extern int errno;
  60. extern int g_clear();
  61. extern char *getenv();
  62. #if HAS_SIG
  63. extern SIG_TYPE (*gl_last_int_handler)();
  64. #endif /* HAS_SIG */
  65.  
  66. static FILE *fhandle;
  67.  
  68. extern int IBM_send_print();
  69. extern int LJ_send_print();
  70.  
  71. static int open_print()
  72. {
  73.     char printprog[60];
  74.     char *envptr;
  75.     int istat;
  76. #ifdef    BILLHATCH
  77.     /* output to stdout instead of print spooler */
  78.     fhandle=stdout;
  79.     return(0);
  80. #endif
  81.     /* Open a pipe to the print spooler (or straight to printer)    */
  82.  
  83.     /* Use the environment variable PLOTDEV if set. */
  84.  
  85.     strcpy(printprog, PRINTPROG);
  86.     strcat(printprog, " ");
  87.     strcat(printprog, LP_DEV_FLAG);
  88. #if HAS_ENV
  89.     if ((envptr = getenv(GL_PLOT_DEV)) != NULL)  {
  90.         strcat(printprog, envptr);
  91.     }
  92.     else  {
  93.         strcat(printprog, PRINTDEV);
  94.     }
  95. #else
  96.     /* Some systems do not understand getenv()    */
  97.     strcat(printprog, PRINTDEV);
  98. #endif /* HAS_ENV */
  99.  
  100. #if HAS_PIPES    /* Pipe to a print spooler program.            */
  101.     if ((fhandle = fopen(printprog, "w")) == NULL)  {
  102.         fprintf(stderr, "%s:  Error piping to %s\n", MODULE,printprog);
  103.         return(1); 
  104.     }
  105. #else        /* No pipes; write directly to the printer device.    */
  106. #if HAS_STDPRN    /* Use predefined value of "stdprn"            */
  107.     fhandle = stdprn;    
  108. #else        /* Hope that stream I/O works for the printer device    */
  109.     if ((fhandle = fopen(DOSPRINTER, "w")) == NULL)  {
  110.         fprintf(stderr, "%s:  Error opening %s\n", MODULE,DOSPRINTER);
  111.         return(1); 
  112.     }
  113. #endif /* HAS_STDPRN */
  114. #endif /* HAS_PIPES */
  115.     return(0);
  116. }
  117.  
  118. static int close_print()
  119. {
  120.     int istat;
  121. #ifdef    BILLHATCH
  122.     return(0);
  123. #endif
  124. #if HAS_PIPES
  125.     if ((istat=pclose(fhandle)) < 0) return(istat);
  126. #else    /* No pipe to close, but close the stream to printer if open    */
  127. #if HAS_STDPRN    /* Don't need to do anything -- stdprn stays open    */
  128. #else        /* Close the stream pointing at the printer        */
  129.     if ((istat=fclose(fhandle)) < 0) return(istat);
  130. #endif /* HAS_STDPRN */
  131. #endif /* HAS_PIPES */
  132.     return(0);
  133. }
  134.  
  135. int g_finish()
  136. {
  137.     int istat;
  138.  
  139.     graphics.initialized = FALSE;
  140.  
  141. #if HAS_SIG
  142. #if DO_CLEANUP
  143.     /* Restore the SIGINT signal handler that was in effect prior    */
  144.     /* to the call to g_init().                    */
  145.     signal(SIGINT, gl_last_int_handler);
  146. #endif /* DO_CLEANUP */
  147. #endif /* HAS_SIG */
  148.  
  149.     switch (graphics.grafmode)  {
  150.  
  151.         case CGA_HI_RES_MODE:
  152.             /* Erase graphics memory.        */
  153.             if (g_clear()) return(1);
  154.             /* Detach the shared memory segment.    */
  155. #if SVAT
  156.             if (shmdt(graphics.cgamem) < 0) return(errno);
  157. #endif /* SVAT */
  158.             break;
  159.  
  160.         case CGA_COLOR_MODE:
  161.             /* Erase graphics memory.        */
  162.             if (g_clear()) return(1);
  163.             /* Detach the shared memory segment.    */
  164. #if SVAT
  165.             if (shmdt(graphics.cgamem) < 0) return(errno);
  166. #endif /* SVAT */
  167.             break;
  168.  
  169.         case EGA_COLOR_MODE:
  170.             /* Erase graphics memory.        */
  171.             if (g_clear()) return(1);
  172.             /* Detach the shared memory segment.    */
  173. #if SVAT
  174.             if (shmdt(graphics.egamem) < 0) return(errno);
  175. #endif /* SVAT */
  176.             break;
  177.  
  178.         case HERC_P0_MODE:
  179.         case HERC_P1_MODE:
  180. #ifdef V386HERC
  181.             weh_close_herc();
  182. #else
  183.             /* Erase graphics memory.        */
  184.             if (g_clear()) return(1);
  185.             /* Detach the shared memory segment.    */
  186. #if SVAT
  187.             if (shmdt(graphics.hercmem) < 0) return(errno);
  188. #endif /* SVAT */
  189. #endif /* V386HERC */
  190.             break;
  191.  
  192.         case IBM_PRINTER:
  193.  
  194.             /* Print the buffer */
  195.             if ((istat=open_print()) !=0) return(istat);
  196.             if ((istat=IBM_send_print(fhandle)) !=0) return(istat);
  197.             if ((istat=close_print()) !=0) return(istat);
  198.  
  199.             /* Free the allocated memory. */
  200.             free(graphics.printbuf1);
  201.             free(graphics.printbuf2);
  202.             free(graphics.printbuf3);
  203.  
  204.             break;
  205.  
  206.         case LJ_PRINTER:
  207.  
  208.             /* Print the buffer */
  209.             if ((istat=open_print()) !=0) return(istat);
  210.             if ((istat=LJ_send_print(fhandle)) !=0) return(istat);
  211.             if ((istat=close_print()) !=0) return(istat);
  212.  
  213.             /* Free the allocated memory. */
  214.             free(graphics.lj_buf1);
  215.             free(graphics.lj_buf2);
  216.             free(graphics.lj_buf3);
  217.  
  218.             break;
  219.  
  220.         default:
  221.             return(1);
  222.     }
  223.  
  224.     /* Tell the hardware to go back to default (text) mode, using    */
  225.     /* the MODEPROG program.  The default action of the mode    */
  226.     /* program is assumed to be text mode.                */
  227.  
  228.     if (graphics.grafmode <= MAXVIDEO)  {
  229. #if MS_DOS
  230.         g_setmod(g_text);
  231. #else
  232.         system(MODEPROG);
  233. #endif /* MS_DOS */
  234.     }
  235.  
  236.     return(0);
  237. }
  238.  
  239.  
  240.