home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / printing / ghostscrip / source / specific / c / gp_vms < prev   
Encoding:
Text File  |  1991-10-26  |  9.5 KB  |  299 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_vms.c */
  21. /* VAX/VMS specific routines for Ghostscript */
  22. #include "gs.h"
  23.  
  24. #define MAX_VMS_FILENAME_LEN 255
  25.  
  26. /* VMS string descriptor structure */
  27. #define DSC$K_DTYPE_T 14
  28. #define DSC$K_CLASS_S  1
  29. struct dsc$descriptor_s {
  30.     unsigned short    dsc$w_length;
  31.     unsigned char    dsc$b_dtype;
  32.     unsigned char    dsc$b_class;
  33.     char        *dsc$a_pointer;
  34. };
  35. typedef struct dsc$descriptor_s descrip;
  36.  
  37. /* VMS RMS constants */
  38. #define RMS$_NMF    99018
  39. #define RMS$_NORMAL 65537
  40.  
  41. struct file_enum_s {
  42.   uint context, length;
  43.   descrip *pattern;
  44. };
  45. typedef struct file_enum_s file_enum;
  46.  
  47. extern uint
  48.   LIB$FIND_FILE(descrip *, descrip *, uint *, descrip *, descrip *,
  49.         uint *, uint *),
  50.   LIB$FIND_FILE_END(uint *),
  51.   SYS$FILESCAN (descrip *, uint *, uint *);
  52.  
  53. private uint
  54. strlength(char *str, uint maxlen, char term)
  55. {    uint i = 0;
  56.     while ( i < maxlen && str[i] != term ) i++;
  57.     return i;
  58. }
  59.  
  60. /* Do platform-dependent interpreter initialization */
  61. void
  62. gp_init()
  63. {
  64. }
  65.  
  66. /* Read the current date (in days since Jan. 1, 1980) */
  67. /* and time (in milliseconds since midnight). */
  68. void
  69. gp_get_clock(long *pdt)
  70. {    struct {uint _l0, _l1;} binary_date;
  71.     long lib$day(), sys$bintim();
  72.     long days, days0, seconds;
  73.     char *jan_1_1980 = "1-JAN-1980";
  74.     char *midnight   = "00:00:00.00";
  75.     descrip str_desc;
  76.  
  77.     /* Get days from system zero date (November 17, 1858) to present. */
  78.     (void) lib$day (&days0);
  79.  
  80.     /* For those interested, Wednesday, November 17, 1858 is the base
  81.        of the Modified Julian Day system adopted by the Smithsonian
  82.        Astrophysical Observatory in 1957 for satellite tracking.  (The
  83.        year 1858 preceded the oldest star catalog in use at the
  84.        observatory.)  VMS uses quadword time stamps which are offsets
  85.        in 100 nanosecond units from November 17, 1858.  With a 63-bit
  86.        absolute time representation (sign bit must be clear), VMS will
  87.        have no trouble with time until 31-JUL-31086 02:48:05.47. */
  88.  
  89.     /* Convert January 1, 1980 into a binary absolute time */
  90.     str_desc.dsc$w_length  = strlen(jan_1_1980);
  91.     str_desc.dsc$a_pointer = jan_1_1980;
  92.     (void) sys$bintim (&str_desc, &binary_date);
  93.  
  94.     /* Now get days from system zero date to January 1, 1980 */
  95.     (void) lib$day (&days, &binary_date);
  96.  
  97.     /* Now compute number of days since January 1, 1980 */
  98.     pdt[0] = 1 + days0 - days;
  99.  
  100.     /* Convert midnight into a binary delta time */
  101.     str_desc.dsc$w_length  = strlen(midnight);
  102.     str_desc.dsc$a_pointer = midnight;
  103.     (void)  sys$bintim (&str_desc, &binary_date);
  104.  
  105.     /* Now get number 10 millisecond time units since midnight */
  106.     (void) lib$day (&days, &binary_date, &seconds);
  107.     pdt[1] = 10 * seconds;
  108. }
  109.  
  110. /* ------ File name syntax ------ */
  111.  
  112. /* Define the character used for separating file names in a list. */
  113. char gp_file_name_list_separator = ',';
  114.  
  115. /* Define the default scratch file name template. */
  116. char gp_scratch_file_name_template[] = "_temp_XXXXXX";
  117.  
  118. /*  Answer whether a file name contains a directory/device specification, i.e.,
  119.  *  is absolute (not directory- or device-relative).  Since for VMS, the concept
  120.  *  of an "absolute" file reference has no meaning.  As Ghostscript is here
  121.  *  merely checking to see if it will make sense to paste a path to the front
  122.  *  of the file name, we use the VMS system service SYS$FILESCAN to check that
  123.  *  the file name has no node, device, root, or directory specification: if all
  124.  *  four of these items are missing from the file name then it is considered to
  125.  *  a relative file name to which a path may be prefixed. (Roots are associated
  126.  *  with rooted logical names.)
  127.  */
  128.  
  129. int
  130. gp_file_name_is_absolute(char *fname, uint len)
  131. {
  132.     descrip str_desc;
  133.     struct { unsigned fscn$v_node : 1;
  134.          unsigned fscn$v_device : 1;
  135.          unsigned fscn$v_root : 1;
  136.          unsigned fscn$v_directory : 1;
  137.          unsigned fscn$v_name : 1;
  138.          unsigned fscn$v_type : 1;
  139.          unsigned fscn$v_version : 1;
  140.          unsigned fscn$v_fill_23 : 1;} flags;
  141.  
  142.     str_desc.dsc$w_length  = len;
  143.     str_desc.dsc$a_pointer = fname;
  144.     SYS$FILESCAN (&str_desc, &0L,  &flags);
  145.     if ( flags.fscn$v_directory || flags.fscn$v_root ||
  146.          flags.fscn$v_device    || flags.fscn$v_node) return 1;
  147.     else return 0;
  148. }
  149.  
  150. /* Answer the string to be used for combining a directory/device prefix */
  151. /* with a base file name.  The file name is known to not be absolute. */
  152. char *
  153. gp_file_name_concat_string(char *prefix, uint plen, char *fname, uint len)
  154. {
  155.     /*  Full VAX/VMS paths are of the form:
  156.      *
  157.      *    device:[root.][directory.subdirectory]filename.extension;version
  158.      *    logical:filename.extension;version
  159.      *
  160.      *  Roots are fairly rare and associated typically with rooted logical
  161.      *  names.
  162.      *
  163.      *  Examples:
  164.      *
  165.      *    DUA1:[GHOSTSCRIPT]GHOST.PS;1
  166.      *    THOR_DEC:[DOOF.A.B.C.D]FILE.DAT;-3
  167.      *    LOG:GHOST.PS  (LOG is a logical defined as DUA1:[GHOSTSCRIPT])
  168.      *    LOG:DOOF.DAT  (LOG is defined as DUA1, current directory is
  169.      *                   is used as the directory spec.)
  170.      *
  171.      */
  172.     if ( plen > 0 )
  173.       switch ( prefix[plen - 1] )
  174.        {    case ':': case ']': return "";
  175.        };
  176.     return ":";
  177. }
  178.  
  179. /* ------ Wild card file search procedures ------ */
  180.  
  181. private void
  182. gp_free_enumeration(file_enum *pfen)
  183. {
  184.     if (pfen) {
  185.       LIB$FIND_FILE_END(&pfen->context);
  186.       gs_free(pfen->pattern->dsc$a_pointer, pfen->length, 1,
  187.           "GP_ENUM(pattern)");
  188.       gs_free((char *)pfen->pattern, sizeof(descrip), 1,
  189.           "GP_ENUM(descriptor)");
  190.       gs_free((char *)pfen, sizeof(file_enum), 1,
  191.           "GP_ENUM(file_enum)");
  192.     }
  193. }
  194.  
  195. /* Begin an enumeration.  pat is a C string that may contain *s or ?s. */
  196. /* The implementor should copy the string to a safe place. */
  197. /* If the operating system doesn't support correct, arbitrarily placed */
  198. /* *s and ?s, the implementation should modify the string so that it */
  199. /* will return a conservative superset of the request.  E.g., if the OS */
  200. /* doesn't implement ? (single-character wild card), any consecutive */
  201. /* string of ?s should be interpreted as *.  Note that \ can appear in */
  202. /* the pattern also, as a quoting character. */
  203.  
  204. file_enum *
  205. gp_enumerate_files_init(char *pat, uint patlen)
  206. {
  207.     file_enum *pfen;
  208.     uint i, len;
  209.     char *c, *newpat;
  210.  
  211.     pfen = (file_enum *)gs_malloc(sizeof (file_enum), 1,
  212.                       "GP_ENUM(file_enum)");
  213.     pfen->pattern = (descrip *)gs_malloc(sizeof (descrip), 1,
  214.                          "GP_ENUM(descriptor)");
  215.     newpat = (char *)gs_malloc(patlen, 1, "GP_ENUM(pattern)");
  216.  
  217.     /*  Copy the pattern removing backslash quoting characters and
  218.      *  transforming unquoted question marks, '?', to percent signs, '%'.
  219.      *  (VAX/VMS uses the wildcard '%' to represent exactly one character
  220.      *  and '*' to represent zero or more characters.  Any combination and
  221.      *  number of interspersed wildcards is permitted.)
  222.      */
  223.     c = newpat;
  224.     for ( i = 0; i < patlen; pat++, i++ )
  225.       switch (*pat) {
  226.         case '?'  :
  227.         *c++ = '%'; break;
  228.         case '\\' :
  229.         i++;
  230.         if (i < patlen) *c++ = *++pat;
  231.         break;
  232.         default   :
  233.         *c++ = *pat; break;
  234.       }
  235.     len = c - newpat;
  236.  
  237.     /* Pattern may not exceed 255 characters */
  238.     if (len > 255) {
  239.       gs_free(newpat, patlen, 1, "GP_ENUM(pattern)");
  240.       gs_free((char *)pfen->pattern, sizeof (descrip), 1,
  241.           "GP_ENUM(descriptor)");
  242.       gs_free((char *)pfen, sizeof (file_enum), 1, "GP_ENUM(file_enum)");
  243.       return (file_enum *)0;
  244.     }
  245.  
  246.     pfen->context = 0;
  247.     pfen->length = patlen;
  248.     pfen->pattern->dsc$w_length  = len;
  249.     pfen->pattern->dsc$b_dtype   = DSC$K_DTYPE_T;
  250.     pfen->pattern->dsc$b_class   = DSC$K_CLASS_S;
  251.     pfen->pattern->dsc$a_pointer = newpat;
  252.  
  253.     return pfen;
  254. }
  255.  
  256. /* Return the next file name in the enumeration.  The client passes in */
  257. /* a scratch string and a max length.  If the name of the next file fits, */
  258. /* the procedure returns the length.  If it doesn't fit, the procedure */
  259. /* returns max length +1.  If there are no more files, the procedure */
  260. /* returns -1. */
  261.  
  262. uint
  263. gp_enumerate_files_next(file_enum *pfen, char *ptr, uint maxlen)
  264. {
  265.     char *c, filnam[MAX_VMS_FILENAME_LEN];
  266.     descrip result = {MAX_VMS_FILENAME_LEN, DSC$K_DTYPE_T,
  267.               DSC$K_CLASS_S, filnam};
  268.     uint i, len;
  269.  
  270.     /* Find the next file which matches the pattern */
  271.     i = LIB$FIND_FILE(pfen->pattern, &result, &pfen->context,
  272.               (descrip *)0, (descrip *)0, (uint *)0, (uint *)0);
  273.  
  274.     /* Check the return status */
  275.     if (i == RMS$_NMF) {
  276.       gp_free_enumeration (pfen);
  277.       return (uint)-1;
  278.     }
  279.     else if (i != RMS$_NORMAL) return 0;
  280.     else if ((len = strlength (filnam, MAX_VMS_FILENAME_LEN, ' ')) > maxlen)
  281.       return maxlen+1;
  282.  
  283.     /* Copy the returned filename over to the input string ptr */
  284.     c = ptr;
  285.     for (i = 0; i < len; i++) *c++ = filnam[i];
  286.  
  287.     return len;
  288. }
  289.  
  290. /* Clean up a file enumeration.  This is only called to abandon */
  291. /* an enumeration partway through: ...next should do it if there are */
  292. /* no more files to enumerate.  This should deallocate the file_enum */
  293. /* structure and any subsidiary structures, strings, buffers, etc. */
  294.  
  295. void
  296. gp_enumerate_files_close(file_enum *pfen)
  297. {    gp_free_enumeration (pfen);
  298. }
  299.