home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / printing / ghostscrip / source / specific / c / gp_dos < prev    next >
Encoding:
Text File  |  1991-10-26  |  7.5 KB  |  244 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gp_dos.c */
  21. /* DOS-specific routines for Ghostscript */
  22. #include <dos.h>
  23. #include <fcntl.h>
  24. #include <dir.h>
  25. #include <signal.h>
  26. #include "string_.h"
  27. #include "gx.h"
  28. #include "gp.h"
  29. #ifdef __OVERLAY__
  30. #  include "overlay.h"
  31. #endif
  32.  
  33. /* Define the size of the C stack. */
  34. unsigned _stklen = 8000;        /* default is 4096, we need more */
  35.  
  36. /* Define the size of the overlay buffer, if relevant. */
  37. #ifdef __OVERLAY__
  38. unsigned _ovrbuffer = (1024L * OVLBUFK) / 16;
  39. #endif
  40.  
  41. /* Forward declarations */
  42. private void handle_FPE(P3(int, int, int *));
  43.  
  44. /* Do platform-dependent initialization */
  45. #if CPU_TYPE > 86
  46. /* Internal routine to set flags and read them back. */
  47. /* We use __emit__ so we don't require an assembler. */
  48. private int
  49. push_pop_flags(unsigned flags)
  50. {    __emit__(0x8b, 0x46, 6);    /* mov ax,flags */
  51.     __emit__(0x50, 0x9d);        /* push ax; popf */
  52.     __emit__(0x9c, 0x58);        /* pushf; pop ax */
  53. }
  54. #endif
  55. void
  56. gp_init()
  57. {    /*
  58.      * Detect the processor type using the following algorithms:
  59.      *    The 8088/8086 truncate shift counts mod 32,
  60.      *      the 80186 and up do not.
  61.      *    The 80186 and below fix FLAGS bits 15-12 to 1,
  62.      *      the 80286 and up do not.
  63.      *    The 80386 allows setting FLAGS bits 14-12,
  64.      *      the 80286 and below do not.
  65.      * We currently can't tell an 80386 from an 80486.
  66.      * Note that this algorithm will identify an 80386
  67.      *   running in Virtual 8086 mode as an 80386.
  68.      *   This is acceptable, because Ghostscript doesn't actually
  69.      *   use 80286 or 80386 addressing modes, only the additional
  70.      *   instructions available on these processors.
  71.      * (This algorithm is derived from the Intel manuals.)
  72.      */
  73. #if CPU_TYPE > 86
  74.     /* We have to be careful not to turn interrupts off! */
  75.     int result, type;
  76.     result = push_pop_flags(0x202);
  77.     if ( (result & 0xf000) == 0xf000 )
  78.        {    /* CPU is an 8088/8086/80186 */
  79.            {    int shc = 33;    /* force shift by variable */
  80.             result = 0xffff << shc;
  81.            }
  82.         type = (result == 0 ? 186 : 86);
  83.        }
  84.     else
  85.        {    /* CPU is an 80286/80386/... */
  86.         result = push_pop_flags(0x7202);
  87.         type = ((result & 0x7000) == 0 ? 286 : 386);
  88.        }
  89.     /* A 486 is the same as a 386. */
  90. #define CPU_EQUIV (CPU_TYPE == 486 ? 386 : CPU_TYPE)
  91.     if ( type < CPU_EQUIV )
  92.        {    eprintf1("This executable requires an 80%d or higher.\n",
  93.              CPU_EQUIV);
  94.         exit(1);
  95.        }
  96. #endif
  97.     _fmode = O_BINARY;        /* Open files in 'binary' mode */
  98.  
  99. #ifdef __OVERLAY__
  100.     /* Initialize the overlay machinery. */
  101.        {    int code;
  102. #  ifdef OVEMS
  103.         code = _OvrInitEms(OVEMS_HANDLE, OVEMS_FIRST, OVEMS_PAGES);
  104.         if ( code )
  105.             eprintf("Attempt to use EMS memory for overlays failed.\n");
  106. #  endif
  107. #  ifdef OVEXT
  108.         code = _OvrInitExt(OVEXT_START, OVEXT_LENGTH);
  109.         if ( code )
  110.             eprintf("Attempt to use extended memory for overlays failed.\n");
  111. #  endif
  112.        }
  113. #endif
  114.     /* Set up the handler for numeric exceptions. */
  115.     signal(SIGFPE, handle_FPE);
  116. }
  117.  
  118. /* Trap numeric exceptions.  Someday we will do something */
  119. /* more appropriate with these. */
  120. private void
  121. handle_FPE(int sig, int subcode, int *regs)
  122. {    eprintf("Numeric exception:\n");
  123.     fprintf(estderr,
  124. "AX=%04x  BX=%04x  CX=%04x  DX=%04x  SI=%04x  DI=%04x  BP=%04x\n",
  125.         regs[8], regs[7], regs[6], regs[5], regs[2], regs[1], regs[0]);
  126.     fprintf(estderr,
  127. "DS=%04x  ES=%04x  CS:IP=%04x:%04x\n",
  128.         regs[3], regs[4], regs[10], regs[9]);
  129.     exit(1);
  130. }
  131.  
  132. /* Read the current date (in days since Jan. 1, 1980) */
  133. /* and time (in milliseconds since midnight). */
  134. void
  135. gp_get_clock(long *pdt)
  136. {    struct date osdate;
  137.     struct time ostime;
  138.     long idate;
  139.     static int mstart[12] =
  140.        { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  141.     getdate(&osdate);
  142.     gettime(&ostime);
  143.     idate = (long)osdate.da_year * 365 +
  144.         (osdate.da_year / 4 + 1 +    /* account for leap years */
  145.          mstart[osdate.da_mon - 1] +    /* month is 1-origin */
  146.          osdate.da_day - 1);        /* day of month is 1-origin */
  147.     if ( osdate.da_mon <= 2 && osdate.da_year % 4 == 0 )        /* Jan. or Feb. of leap year */
  148.         idate--;
  149.     pdt[0] = idate;
  150.     pdt[1] =
  151.         (ostime.ti_hour * 60 + ostime.ti_min) * 60000L +
  152.         (ostime.ti_sec * 100 + ostime.ti_hund) * 10L;
  153. }
  154.  
  155. /* ------ File name syntax ------ */
  156.  
  157. /* Define the character used for separating file names in a list. */
  158. char gp_file_name_list_separator = ';';
  159.  
  160. /* Define the default scratch file name template. */
  161. char gp_scratch_file_name_template[] = "_temp_XXXXXX";
  162.  
  163. /* Answer whether a file name contains a directory/device specification, */
  164. /* i.e. is absolute (not directory- or device-relative). */
  165. int
  166. gp_file_name_is_absolute(char *fname, uint len)
  167. {    /* A file name is absolute if it contains a drive specification */
  168.     /* (second character is a :) or if it start with / or \. */
  169.     return ( len >= 1 && (*fname == '/' || *fname == '\\' ||
  170.         (len >= 2 && fname[1] == ':')) );
  171. }
  172.  
  173. /* Answer the string to be used for combining a directory/device prefix */
  174. /* with a base file name.  The file name is known to not be absolute. */
  175. char *
  176. gp_file_name_concat_string(char *prefix, uint plen, char *fname, uint len)
  177. {    if ( plen > 0 )
  178.       switch ( prefix[plen - 1] )
  179.        {    case ':': case '/': case '\\': return "";
  180.        };
  181.     return "\\";
  182. }
  183.  
  184. /* ------ File enumeration ------ */
  185.  
  186. struct file_enum_s {
  187.     struct ffblk ffblk;
  188.     char *pattern;
  189.     int first_time;
  190.     gs_memory_procs mprocs;
  191. };
  192.  
  193. /* Initialize an enumeration.  NEEDS WORK ON HANDLING * ? \. */
  194. file_enum *
  195. gp_enumerate_files_init(char *pat, uint patlen, proc_alloc_t palloc, proc_free_t pfree)
  196. {    file_enum *pfen = (file_enum *)(*palloc)(1, sizeof(file_enum), "gp_enumerate_files");
  197.     char *pattern;
  198.     if ( pfen == 0 ) return 0;
  199.     pattern = (*palloc)(patlen + 1, 1,
  200.                 "gp_enumerate_files(pattern)");
  201.     if ( pattern == 0 ) return 0;
  202.     memcpy(pattern, pat, patlen);
  203.     pattern[patlen] = 0;
  204.     pfen->pattern = pattern;
  205.     pfen->mprocs.alloc = palloc;
  206.     pfen->mprocs.free = pfree;
  207.     pfen->first_time = 1;
  208.     return pfen;
  209. }
  210.  
  211. /* Enumerate the next file. */
  212. /* DOESN'T PREFIX THE DRIVE & DIRECTORY -- THIS IS WRONG. */
  213. uint
  214. gp_enumerate_files_next(file_enum *pfen, char *ptr, uint maxlen)
  215. {    int code;
  216.     char *p, *q;
  217.     char *dta = getdta();        /* just in case! */
  218.     if ( pfen->first_time )
  219.        {    code = findfirst(pfen->pattern, &pfen->ffblk, 0);
  220.         pfen->first_time = 0;
  221.        }
  222.     else
  223.         code = findnext(&pfen->ffblk);
  224.     setdta(dta);
  225.     if ( code < 0 )
  226.        {    /* All done, clean up. */
  227.         gp_enumerate_files_close(pfen);
  228.         return ~(uint)0;
  229.        }
  230.     if ( maxlen < 13 ) return maxlen + 1;    /* cop out! */
  231.     for ( p = &pfen->ffblk.ff_name[0], q = ptr; *p; p++ )
  232.       if ( *p != ' ' ) *q++ = *p;
  233.     return q - ptr;
  234. }
  235.  
  236. /* Clean up the file enumeration. */
  237. void
  238. gp_enumerate_files_close(file_enum *pfen)
  239. {    proc_free_t pfree = pfen->mprocs.free;
  240.     (*pfree)(pfen->pattern, strlen(pfen->pattern) + 1, 1,
  241.          "gp_enumerate_files_close(pattern)");
  242.     (*pfree)((char *)pfen, 1, sizeof(file_enum), "gp_enumerate_files_close");
  243. }
  244.