home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / apps / dtp / ghost / gs301src / atari / gp_atar1.c < prev    next >
C/C++ Source or Header  |  1994-09-17  |  19KB  |  777 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gp_atar1.c */
  20.  
  21. /*
  22.  * Atari-specific routines for Ghostscript. This file contains the
  23.  * standard suite of gp_* routines called by Ghostscript plus
  24.  * a few utility functions used by them.
  25.  *
  26.  * GEM initialization is performed in gp_init.
  27.  */
  28.  
  29. #include "gp_atar1.h"
  30.  
  31. /* External routines */
  32.  
  33. extern FILE *popen(P2(const char *, const char *));
  34. extern int pclose(P1(FILE *));
  35. extern void gs_exit(P1(int exit_status));    /* from gsmain.c */
  36.  
  37. extern WINLIST *WinListAdd();            /* from gp_atar3.c */
  38. extern WINLIST *WinListDelete();        /* from gp_atar3.c */
  39.  
  40. /* External Variables */
  41.  
  42. extern WINLIST *WList;                /* from gp_atar2.c */
  43.  
  44. extern WINDOW conswin;                /* from gp_atar2.c */
  45. extern WINDOW aboutwin, reswin, pagewin;    /* from gp_atar2.c */
  46. extern WINDOW iconwin, devwin, sizwin, printwin;/* from gp_atar2.c */
  47. extern EVENT Event;                /* from gp_atar2.c */
  48.  
  49. /* Global Variables */
  50.  
  51. /* Virtual Workstation Structure. */
  52.  
  53. VWRK VWork = {
  54.     0,                /* VdiHandle */
  55.     0,                /* XRes */
  56.     0,                /* YRes */
  57.     0,                /* AspectRatio */
  58.     {0, 0, 0, 0},        /* full */
  59.     0,                /* ColorBits */
  60.     0,                /* MaxRGB */
  61.     0,                /* TrueColor */
  62.     0,                /* PaletteSize */
  63.     0,                /* GSPalette */
  64.     0,                /* Palette */
  65.     0,                /* OldPalette */
  66.     0,                /* ColorReg */
  67.     0,                /* Character cell width */
  68.     0,                /* Character cell height */
  69.     0,                /* Character box width */
  70.     0                /* Character box height */
  71. };
  72.  
  73. DOCUMENT document;        /* Contains PS document information. */
  74.  
  75. /* Program State Structure. */
  76.  
  77. PSTATE State = {
  78.     0,                /* ApId */
  79.     0,                /* MultiTOS */
  80.     1,                /* Windows */
  81.     0,                /* TopWin */
  82.     0,                /* PSHelp */
  83.     0,                /* SelectPages */
  84.     FIRSTDEV,                   /* Device */
  85.     FIRSTDEV,                   /* LastDevice */
  86.     "",                /* CurrentDir[] */
  87.     "",                /* DirSpec[] */
  88.     "",                /* FileSel[] */
  89.     "",                /* Infile[] */
  90.     "",                /* Outfile[] */
  91.     "",                /* Command[] */
  92.     "",                         /* DevString[] */
  93.     1,                /* PaletteCount */
  94.     0,                /* Chunky8 */
  95.     0,                /* Gdebug */
  96.     0,                /* Debug8 */
  97.     0,                /* Debug16 */
  98.     {0, 0, 0, 0, 0, 0, 0, 0},    /* pxy[] */
  99.     &Event,            /* Event */
  100.     {0, 0, 0, 0, 0, 0, 0, 0},    /* Pobject[] */
  101.     &document            /* Doc */
  102. };
  103.  
  104. /* Gp_init does Atari-dependent initialization. It reads
  105.  * Atari-specific environment variables, initializes the
  106.  * AES, opens a virtual workstation, creates the windows,
  107.  * displays the menu, opens the console, and performs various
  108.  * other initializations.
  109.  */
  110.  
  111. void
  112. gp_init(void)
  113. {
  114.     char *env, temp[MAXLEN];
  115.     char PrgName[] = "  ST-GS";        /* CF */
  116.  
  117.     int i, j, print_disable=0;
  118.  
  119.     /* Check the GS_WIN environment variable to decide what to
  120.      * do about windows. Windows are used unless GS_WIN contains
  121.      * the string "off".
  122.      */
  123.  
  124.     if ((env = getenv("GS_WIN")) != NULL) {
  125.     if (!strcmp(env, "off") || !strcmp(env, "OFF")) {
  126.         State.Windows = 0;        
  127.         }
  128.     }
  129.  
  130.     if ((env = getenv("GS_DEVICE")) != NULL) {
  131.  
  132.       for (i=0; (strcmp(env, devices[i])) && (i<DEVICES); ++i);
  133.       
  134.       if (i < DEVICES) {
  135.         State.Device = i;
  136.     strcpy(State.DevString, devices[i]);
  137.     if (!strcmp(State.DevString, "stvdi")) {
  138.       print_disable = 1;
  139.     }
  140.       }
  141.  
  142.     }
  143.     else {
  144.       strcpy(State.DevString, "stvdi");
  145.       print_disable = 1;
  146.     }
  147.     
  148.     if (print_disable) {
  149.       objc_change(printobj, PR_PRN, 0,
  150.       printwin.canvas.g_x, printwin.canvas.g_y,
  151.       printwin.canvas.g_w, printwin.canvas.g_h,
  152.       (printobj[PR_PRN].ob_state |= DISABLED),        
  153.       printwin.opened);
  154.  
  155.       objc_change(printobj, PR_CEN, 0,
  156.       printwin.canvas.g_x, printwin.canvas.g_y,
  157.       printwin.canvas.g_w, printwin.canvas.g_h,
  158.       (printobj[PR_CEN].ob_state |= DISABLED),
  159.       printwin.opened);
  160.  
  161.       objc_change(printobj, PR_OFILE, 0,
  162.       printwin.canvas.g_x, printwin.canvas.g_y,
  163.       printwin.canvas.g_w, printwin.canvas.g_h,
  164.       (printobj[PR_OFILE].ob_state |= DISABLED),
  165.       printwin.opened);
  166.     }
  167.  
  168.  
  169.     /* Find the current directory. */
  170.  
  171.     getcwd(temp, MAXLEN);
  172.     unx2dos(temp, State.CurrentDir);
  173.  
  174.     strcat(State.CurrentDir, "\\");
  175.  
  176.     /* Initialize the AES and assign most of the VWork structure. */
  177.  
  178.     GemInit(&VWork, &State);
  179.  
  180.     if (State.Windows) {
  181.     LoadConfig(&VWork, &State);
  182.     }
  183.  
  184.     /* Some environment variables override the values
  185.      * returned by the system.
  186.      */
  187.  
  188.     if ((env = getenv("GS_DISPLAY")) != NULL) {
  189.  
  190.       if (!strcmp(env, "truecolor")) {
  191.     VWork.TrueColor = 1;        
  192.       }
  193.       else if (!strcmp(env, "chunky8") || !strcmp(env, "CHUNKY8")) {
  194.     State.Chunky8 = 1;
  195.       }
  196.  
  197.     }
  198.  
  199.     if ((env = getenv("GS_DEBUG")) != NULL) {
  200.     if (!strcmp(env, "gdebug")) {
  201.         State.Gdebug = 1;
  202.     }
  203.     else if (!strcmp(env, "debug8")) {
  204.         State.Debug8 = 1;
  205.         VWork.ColorBits = 8;
  206.         VWork.TrueColor = 0;        
  207.     }
  208.     else if (!strcmp(env, "debug16")) {
  209.         State.Debug16 = 1;
  210.         VWork.ColorBits = 16;
  211.     }
  212.     }
  213.  
  214.     /* Check whether the current video hardware is supported. */
  215.  
  216.     if (VWork.ColorBits != 1  && VWork.ColorBits != 2 &&
  217.     VWork.ColorBits != 4  && VWork.ColorBits != 8 &&
  218.     VWork.ColorBits != 16 && VWork.ColorBits != 24) {
  219.  
  220.     eprintf1("stvdi_open: %d bit color is not supported.\n",
  221.         VWork.ColorBits);
  222.  
  223.     gs_exit(-1);
  224.  
  225.     }
  226.  
  227.     VWork.MaxRGB = DITH_RGB(VWork.ColorBits) - 1;
  228.     
  229.     /* Initialize the palette. */
  230.  
  231.     if ((VWork.ColorBits > 1) && !VWork.TrueColor) {
  232.     InitPalette(&VWork, &State);
  233.     }
  234.  
  235.     if (State.Gdebug) {
  236.     eprintf1("Color Bits   = %ld\n", VWork.ColorBits);
  237.     eprintf1("True Color   = %ld\n", VWork.TrueColor);
  238.     eprintf1("Max RGB      = %ld\n", VWork.MaxRGB);
  239.     eprintf1("Palette Size = %ld\n", VWork.PaletteSize);
  240.     }
  241.  
  242.     if (State.Windows) {
  243.  
  244.     /* Initialization for GEM objects. */
  245.     /* Put pointers to all objects into an array. */
  246.  
  247.     State.Pobject[0] = menuobj;
  248.     State.Pobject[1] = aboutobj;
  249.     State.Pobject[2] = resobj;
  250.     State.Pobject[3] = iconobj;
  251.     State.Pobject[4] = devobj;
  252.     State.Pobject[5] = sizobj;
  253.     State.Pobject[6] = printobj;
  254.     State.Pobject[7] = savemsg;
  255.  
  256.     /* Fix object sizes for current resolution. */
  257.  
  258.     AdjustObjects(&VWork);
  259.  
  260.     for (j=0; j<NOBJS; j++) {
  261.         i=-1;
  262.         do {
  263.         ++i;
  264.         rsrc_obfix(State.Pobject[j], i);
  265.         } while (!(State.Pobject[j][i].ob_flags & LASTOB));
  266.     }
  267.  
  268.     /* Set the string pointers for the print dialog. */
  269.  
  270.     printobj[PR_FSTR].ob_spec = (unsigned long) State.Infile;
  271.     printobj[PR_DSTR].ob_spec = (unsigned long) State.DevString;
  272.  
  273.     if (!conswin.opened) {
  274.         WINTEXT *text = conswin.obj;
  275.  
  276.         /* Display the menu and open the console window. */
  277.  
  278.         RestoreBG(&VWork);
  279.         graf_mouse(M_OFF, 0L );
  280.  
  281.         menu_bar(menuobj, 1);
  282.  
  283.         if (State.MultiTOS) {                /* CF */
  284.         menu_register(gl_apid, PrgName);
  285.         menu_ienable(menuobj, ICON, 0);        /* use iconifier! */
  286.         }    
  287.     
  288.         /* Create the head of the window list. */
  289.         WList = WinListAdd(NULL, &conswin);
  290.  
  291.  
  292.         /* Open the console window. */
  293.  
  294.         if (TextBuffRealloc(text, LINES) != NULL ) {
  295.         TextWinOpen(&conswin, &VWork, &State);
  296.         }
  297.         else {
  298.         eprintf("gp_init: No memory for the console text buffer!\n");
  299.         graf_mouse(M_ON, 0L);
  300.         gs_exit(-1);
  301.         }
  302.  
  303.         graf_mouse(BUSY_BEE, 0L);
  304.         graf_mouse(M_ON, 0L);
  305.  
  306.     }
  307.     }
  308. }
  309.  
  310. int
  311. GemInit(VWRK *vw, PSTATE *st)
  312. {
  313.  
  314.     int GemHandle;
  315.     int WorkOut[57];
  316.     int WorkIn[11] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2};
  317.  
  318.     /* Call appl_init() to initialize the aes before using any aes
  319.      * functions.
  320.      */
  321.  
  322.     if (st->Windows) {
  323.     if ((st->ApId = appl_init()) == -1) {
  324.         eprintf("stvdi_open: appl_init() failed!\n");
  325.         gs_exit(-1);
  326.     }
  327.     }
  328.  
  329.     st->MultiTOS = (gl_ap_version >= 0x400);            /* CF */
  330.     
  331.     /* Open a virtual workstation. */
  332.  
  333.     GemHandle = graf_handle(&vw->Wchar, &vw->Hchar, &vw->Wbox, &vw->Hbox);
  334.     vw->VdiHandle = GemHandle;
  335.     v_opnvwk(WorkIn, &VWork.VdiHandle, WorkOut);
  336.  
  337.     if (vw->VdiHandle == 0) {
  338.     eprintf("stvdi_open: Could not open virtual screen workstation");
  339.     gs_exit(-1);
  340.     }
  341.  
  342.     /* Get the screen resolution, aspect ratio, and palette size. */
  343.  
  344.     vw->XRes = WorkOut[0];
  345.     vw->YRes = WorkOut[1];
  346.  
  347.     vw->AspectRatio = (float) WorkOut[3] / WorkOut[4];
  348.  
  349.     /* Get the number of color planes. */
  350.  
  351.     vq_extnd(vw->VdiHandle, 1, WorkOut);
  352.  
  353.     vw->ColorBits = WorkOut[4];
  354.     vw->TrueColor = (!WorkOut[5] || (vw->ColorBits >= 16));
  355.  
  356.     /* Get the size of the root window. */
  357.  
  358.     if (st->Windows) {
  359.     wind_get(0, WF_WORKXYWH,
  360.         &vw->full.g_x, &vw->full.g_y,
  361.         &vw->full.g_w, &vw->full.g_h);
  362.     }
  363.  
  364.     return 0;
  365. }
  366.  
  367. int
  368. InitPalette(VWRK *vw, PSTATE *st)
  369. {
  370.  
  371.     int psize;
  372.  
  373.     psize = (int)(pow(2, vw->ColorBits) + .5);
  374.     vw->PaletteSize = 3 * psize;
  375.  
  376.     /* Set up the palette--a sequence of rgb values. */
  377.  
  378.  
  379.     if ((vw->Palette = (int *)gs_malloc((uint)vw->PaletteSize,
  380.       sizeof(int), "Palette")) == NULL) {
  381.       return_error(gs_error_VMerror);
  382.     }
  383.  
  384.     if ((vw->OldPalette = (int *)gs_malloc((uint)vw->PaletteSize,
  385.       sizeof(int), "OldPalette")) == NULL) {
  386.       return_error(gs_error_VMerror);
  387.     }
  388.  
  389.     if ((vw->ColorReg = (int *)gs_malloc((uint)psize,
  390.       sizeof(int), "ColorReg")) == NULL) {
  391.       return_error(gs_error_VMerror);
  392.     }
  393.  
  394.     if (st->Debug8) {
  395.     int i;
  396.  
  397.     vw->Palette[0] = 0;
  398.     vw->Palette[1] = 0;
  399.     vw->Palette[2] = 0;
  400.  
  401.     for (i=3; i<vw->PaletteSize; i++) {
  402.         vw->Palette[i] = 1;
  403.     }
  404.  
  405.     }
  406.     else {
  407.  
  408.     /* Fill the array ColorReg. The value of ColorReg[i]
  409.      * will be the hardware color register which corresponds
  410.      * to the ith vdi color index.
  411.      */
  412.  
  413.     IndexToPixel((gx_color_index *)vw->ColorReg, psize);
  414.  
  415.     GetPalette(VWork.Palette, &VWork);    /* Get current palette. */
  416.     GetPalette(VWork.OldPalette, &VWork);    /* Save for restoration. */
  417.     VWork.GSPalette = 1;
  418.  
  419.     }
  420.  
  421.     return 0;
  422. }
  423.  
  424. int
  425. AdjustObjects(VWRK *vw)
  426. {
  427.     int ScreenWidth;
  428.     static char line1[] = "ST Ghostscript 2.6.1";
  429.     static char line2[] = "Tim Gallivan, 1994";
  430.  
  431.     /* The icon and the banner bitmaps must be adjusted for the
  432.      * strange aspect ratio in ST medium (and perhaps other)
  433.      * resolutions.
  434.      */
  435.  
  436.     if (vw->AspectRatio < .6) {
  437.     iconobj[0].ob_height = 0x1800;        /* 24 pixels high */
  438.     iconobj[1].ob_height = 0x1800;
  439.  
  440.     iconblk[0].bi_pdata   = (char *)HalfReaper;
  441.     iconblk[0].bi_hl = 24;
  442.  
  443.     aboutobj[6].ob_height = 0x1b00;        /* 27 pixels high */
  444.  
  445.     bannerblk[0].bi_pdata = (char *)HalfBanner;
  446.     bannerblk[0].bi_hl = 27;
  447.     }
  448.  
  449.     /* The About object must be changed to fit on the screen in
  450.      * low resolution. This is a major change to the object.
  451.      */
  452.  
  453.     ScreenWidth = vw->full.g_w / vw->Wchar;
  454.  
  455.     if (ScreenWidth <= aboutobj[0].ob_width) {
  456.     aboutobj[0].ob_width = 28;    aboutobj[0].ob_height = 15;
  457.  
  458.     aboutobj[1].ob_y = 7;
  459.     aboutobj[2].ob_y = 9;
  460.  
  461.     aboutobj[3].ob_x = 2;    aboutobj[3].ob_y = 11;
  462.  
  463.     aboutobj[4].ob_x = 2;    aboutobj[4].ob_y = 13;
  464.  
  465.     aboutobj[5].ob_state = SHADOWED;
  466.     aboutobj[5].ob_spec = 0xFF1111L;
  467.     aboutobj[5].ob_width = 24;
  468.     aboutobj[5].ob_height = 5;
  469.  
  470.     aboutobj[6].ob_type = G_STRING;
  471.     aboutobj[6].ob_spec = UL line1;
  472.     aboutobj[6].ob_x = 2;
  473.     aboutobj[6].ob_y = 1;
  474.     aboutobj[6].ob_width = 20;
  475.     aboutobj[6].ob_height = 1;
  476.  
  477.     aboutobj[7].ob_spec = UL line2;
  478.     aboutobj[7].ob_x = 3;
  479.     aboutobj[7].ob_y = 3;
  480.     aboutobj[7].ob_width = 18;
  481.  
  482.     }
  483. }
  484.  
  485. /* Gp_exit does Atari-dependent cleanup. It frees memory and AES
  486.  * resources, closes the virtual workstation, and calls appl_exit.
  487.  */
  488.  
  489. #define EXITMSG "[3][Ghostscript Error %d!|See the console window\
  490. |for more details.][Abort]"
  491.  
  492. void
  493. gp_exit(int exit_status, int code)
  494. {
  495.     WINLIST *wl = WList;
  496.     WINTEXT *text = conswin.obj;
  497.  
  498.     char ErrorMsg[MAXLEN];
  499.  
  500.     if (exit_status != 0 && State.Windows) {
  501.         graf_mouse(ARROW, 0L);
  502.  
  503.         sprintf(ErrorMsg, EXITMSG, exit_status);
  504.     form_alert(1, ErrorMsg);
  505.  
  506.         graf_mouse(BUSY_BEE, 0L);
  507.     }
  508.  
  509.     /* Restore the palette and free the allocated memory. */
  510.  
  511.     if (VWork.ColorBits > 1 && !VWork.TrueColor) {
  512.  
  513.     if (VWork.GSPalette) {
  514.         SetPalette(VWork.OldPalette, &VWork);
  515.  
  516.         gs_free((char *)VWork.Palette, VWork.PaletteSize,
  517.             sizeof(int), "Palette");
  518.  
  519.         gs_free((char *)VWork.OldPalette, VWork.PaletteSize,
  520.             sizeof(int), "OldPalette");
  521.  
  522.         gs_free((char *)VWork.ColorReg, VWork.PaletteSize / 3,
  523.             sizeof(int), "ColorReg");
  524.  
  525.         VWork.GSPalette = 0;
  526.     }
  527.  
  528.     }
  529.  
  530.     /* Free the resources used by the aes. */
  531.  
  532.     if (State.Windows) {
  533.         menu_bar(menuobj, 0);
  534.  
  535.     /* Free the console character buffer. I probably should treat
  536.      * the console window the same as other text windows.
  537.      */
  538.  
  539.     gs_free((char *)text->buff, text->bsize, (COLUMNS+1), "charbuff");
  540.  
  541.     /*
  542.      * Delete any scratch files for extracted pages and
  543.      * free any memory allocated for document page information.
  544.      */
  545.  
  546.     if (State.Doc->pageinf) {
  547.         char filename[MAXLEN], temp[10];
  548.         int i;
  549.  
  550.         remove(PROLOG);
  551.  
  552.         for (i=1; i <= State.Doc->maxpages; ++i) {
  553.         if (State.Doc->pageinf[i].extracted != 0) {
  554.             itoa(i, temp);
  555.  
  556.             strcpy(filename, PAGE_PREFIX);
  557.             strcat(filename, temp);
  558.             strcat(filename, ".ps");
  559.         
  560.             remove(filename);
  561.         }
  562.         }
  563.  
  564.         gs_free((char *)State.Doc->pageinf, State.Doc->maxpages,
  565.         sizeof(PAGEINFO), "page info");
  566.     }
  567.  
  568.     /* If memory was allocated for the page dialog, free it. */
  569.  
  570.     if (pagewin.obj) {
  571.         DeletePageObj(&State, &pagewin);
  572.     }
  573.  
  574.     /*
  575.      * Close and delete all open windows. Delete all window
  576.      * list entries.
  577.      */
  578.  
  579.     do {
  580.         if (!(wl->Win->iconified)) {
  581.         wind_close(wl->Win->handle);
  582.         }
  583.         wind_delete(wl->Win->handle);
  584.     }
  585.     while ((wl = WinListDelete(wl, wl->Win)));
  586.  
  587.     }
  588.  
  589.     /* Close the virtual workstation and call appl_exit */
  590.  
  591.     if (VWork.VdiHandle > 0) {
  592.       v_clsvwk(VWork.VdiHandle);
  593.     }
  594.  
  595.     if (State.Windows) {
  596.         graf_mouse(ARROW, 0L);
  597.     if (appl_exit() == 0) {
  598.         eprintf("stvdi_close: appl_exit() failed!\n");
  599.         exit_status = -1;
  600.     }
  601.     }
  602.  
  603. }
  604.  
  605.  
  606. /* ------ Miscellaneous ------ */
  607.  
  608. /* Get the string corresponding to an OS error number. */
  609. /* Unix systems support this so inconsistently that we don't attempt */
  610. /* to figure out whether it's available. */
  611. const char *
  612. gp_strerror(int errnum)
  613. {    return NULL;
  614. }
  615.  
  616. /* ------ Date and time ------ */
  617.  
  618. /* Read the current date (in days since Jan. 1, 1980) */
  619. /* and time (in milliseconds since midnight). */
  620. void
  621. gp_get_clock(long *pdt)
  622. {    long secs_since_1980;
  623.     struct timeval tp;
  624.     time_t tsec;
  625.     struct tm *tm, *localtime(P1(const time_t *));
  626.  
  627. #if gettimeofday_no_timezone
  628.     {    if ( gettimeofday(&tp) == -1 )
  629.           {    lprintf("Ghostscript: gettimeofday failed!\n");
  630.             gs_exit(1);
  631.           }
  632.         secs_since_1980 = 0;
  633.     }
  634. #else
  635.     {    struct timezone tzp;
  636.         if ( gettimeofday(&tp, &tzp) == -1 )
  637.           {    lprintf("Ghostscript: gettimeofday failed!\n");
  638.             gs_exit(1);
  639.           }
  640.         /*
  641.          * Don't adjust for timezone!  That structure returns the
  642.          * kernel's notion of the timezone, which may be different
  643.          * from the one specified in the TZ environment variable.
  644.          * In particular, the tz_minuteswest value is documented to
  645.          * need adjustment according to the daylight savings time
  646.          * rules indicated by tz_dsttime.  The latter indicates
  647.          * only the applicable rules and doesn't answer the
  648.          * question whether DST currently is in effect or not.
  649.          */
  650.         /*secs_since_1980 = -(tzp.tz_minuteswest * 60);*/
  651.         secs_since_1980 = 0;
  652.     }
  653. #endif
  654.  
  655.     /* tp.tv_sec is #secs since Jan 1, 1970 */
  656.  
  657.     /* subtract off number of seconds in 10 years */
  658.     /* leap seconds are not accounted for */
  659.     secs_since_1980 += tp.tv_sec - (long)(60 * 60 * 24 * 365.25 * 10);
  660.  
  661.     /* adjust for daylight savings time - assume dst offset is 1 hour */
  662.     tsec = tp.tv_sec;
  663.     tm = localtime(&tsec);
  664.     if ( tm->tm_isdst )
  665.         secs_since_1980 += (60 * 60);
  666.  
  667.     /* divide secs by #secs/day to get #days (integer division truncates) */
  668.     pdt[0] = secs_since_1980 / (60 * 60 * 24);
  669.     /* modulo + microsecs/1000 gives number of millisecs since midnight */
  670.     pdt[1] = (secs_since_1980 % (60 * 60 * 24)) * 1000;
  671.     /* Some Unix systems (e.g., Interactive 3.2 r3.0) return garbage */
  672.     /* in tp.tv_usec.  Try to filter out the worst of it here. */
  673.     if ( tp.tv_usec >= 0 && tp.tv_usec < 1000000 )
  674.         pdt[1] += tp.tv_usec / 1000;
  675.  
  676. #ifdef DEBUG_CLOCK
  677.     printf("tp.tv_sec = %d  tp.tv_usec = %d  pdt[0] = %ld  pdt[1] = %ld\n",
  678.         tp.tv_sec, tp.tv_usec, pdt[0], pdt[1]);
  679. #endif
  680. }
  681.  
  682. #if 0
  683. /* Read the current date (in days since Jan. 1, 1980) */
  684. /* and time (in milliseconds since midnight). */
  685.  
  686. void
  687. gp_get_clock(long *pdt)
  688. {    long secs_since_1970, secs_since_1980;
  689.     struct tm *tm, *localtime();
  690.  
  691.     if ( (secs_since_1970 = time(NULL)) == 0 )
  692.        {    perror("Ghostscript: gettimeofday failed:");
  693.         gs_exit(-1);
  694.        }
  695.  
  696.     /* secs_since_1970 is #secs since Jan 1, 1970 */
  697.  
  698.     /* subtract off number of seconds in 10 years */
  699.     /* leap seconds are not accounted for */
  700.     secs_since_1980 = secs_since_1970 - (long)(60 * 60 * 24 * 365.25 * 10);
  701.  
  702.     /* there is no time zone adjustment for the atari st */
  703.  
  704.     /* adjust for daylight savings time - assume dst offset is 1 hour */
  705.     tm = localtime(&(secs_since_1970));
  706.     if ( tm->tm_isdst )
  707.         secs_since_1980 += (60 * 60);
  708.  
  709.     /* divide secs by #secs/day to get #days (integer division truncates) */
  710.     pdt[0] = secs_since_1980 / (60 * 60 * 24);
  711.     /* modulo * 1000 gives number of millisecs since midnight */
  712.     pdt[1] = (secs_since_1980 % (60 * 60 * 24)) * 1000;
  713. #ifdef DEBUG_CLOCK
  714.     printf("secs_since_1970 = %d  usecs_since_1970 = %d  pdt[0] = %ld\
  715.   pdt[1] = %ld\n", secs_since_1970, pdt[1], pdt[0], pdt[1]);
  716. #endif
  717. }
  718. #endif
  719.  
  720. /* ------ Screen management ------ */
  721.  
  722. /* Get the environment variable that specifies the display to use. */
  723. const char *
  724. gp_getenv_display(void)
  725. {    return getenv("DISPLAY");
  726. }
  727.  
  728. /* ------ Check whether a file is the console. ------ */
  729.  
  730. int
  731. gp_file_is_console(FILE *f)
  732. {
  733.     if (!f) return 0;
  734.     if (fileno(f) >= 0 && fileno(f) <= 2) return 1;
  735.  
  736.     return 0;
  737. }
  738.  
  739. /* ------ Printer accessing ------ */
  740.  
  741. /* Open a file or a printing device. A null file name will cause
  742.  * output to be sent to "PRN:". If gp_open_printer returns
  743.  * a NULL, the file will be sent directly to the centronics port.
  744.  * This happens if fname = "CEN:" or if gp_open_scratch_file()
  745.  * fails. Usually, GS wants to interpret a NULL return value as
  746.  * an error, so this is slightly incompatible.
  747.  * "|command" opens an output pipe.
  748.  */
  749.  
  750. FILE *
  751. gp_open_printer(char *fname, int binary_mode)
  752. {
  753.     if (!strcmp(fname, "CEN:")) {    /* Direct Centronics printing. */
  754.        return NULL;
  755.     }
  756.     else {
  757.        return
  758.          (strlen(fname) == 0 ?
  759.           gp_open_scratch_file(gp_scratch_file_name_prefix, fname, "wb") :
  760.           fname[0] == '|' ?
  761.           popen(fname + 1, "wb") :
  762.           fopen(fname, "wb"));
  763.     }
  764. }
  765.  
  766. /* Close the connection to the printer. */
  767. void
  768. gp_close_printer(FILE *pfile, const char *fname)
  769. {    if ( fname[0] == '|' )
  770.         pclose(pfile);
  771.     else
  772.         fclose(pfile);
  773. }
  774.  
  775.  
  776.  
  777.