home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GP_VMS.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  12KB  |  393 lines

  1. /* Copyright (C) 1989, 1992, 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gp_vms.c */
  20. /* VAX/VMS specific routines for Ghostscript */
  21. #include "string_.h"
  22. #include "gx.h"
  23. #include "gp.h"
  24. #include <stat.h>
  25. #include <unixio.h>
  26.  
  27. extern char *getenv(P1(const char *));
  28.  
  29. /* Apparently gcc doesn't allow extra arguments for fopen: */
  30. #ifdef VMS        /* DEC C */
  31. #  define fopen_VMS fopen
  32. #else            /* gcc */
  33. #  define fopen_VMS(name, mode, m1, m2) fopen(name, mode)
  34. #endif
  35.  
  36.  
  37. /* VMS string descriptor structure */
  38. #define DSC$K_DTYPE_T 14
  39. #define DSC$K_CLASS_S  1
  40. struct dsc$descriptor_s {
  41.     unsigned short    dsc$w_length;
  42.     unsigned char    dsc$b_dtype;
  43.     unsigned char    dsc$b_class;
  44.     char        *dsc$a_pointer;
  45. };
  46. typedef struct dsc$descriptor_s descrip;
  47.  
  48. /* VMS RMS constants */
  49. #define RMS$_NMF    99018
  50. #define RMS$_NORMAL 65537
  51. #define NAM$C_MAXRSS  255
  52.  
  53. struct file_enum_s {
  54.   uint context, length;
  55.   descrip *pattern;
  56. };
  57.  
  58. extern uint
  59.   LIB$FIND_FILE(descrip *, descrip *, uint *, descrip *, descrip *,
  60.         uint *, uint *),
  61.   LIB$FIND_FILE_END(uint *),
  62.   SYS$FILESCAN (descrip *, uint *, uint *),
  63.   SYS$PUTMSG (uint *, int (*)(), descrip *, uint);
  64.  
  65. private uint
  66. strlength(char *str, uint maxlen, char term)
  67. {    uint i = 0;
  68.     while ( i < maxlen && str[i] != term ) i++;
  69.     return i;
  70. }
  71.  
  72. /* Do platform-dependent initialization. */
  73. void
  74. gp_init(void)
  75. {
  76. }
  77.  
  78. /* Do platform-dependent cleanup. */
  79. void
  80. gp_exit(int exit_status, int code)
  81. {
  82. }
  83.  
  84. /* ------ Date and time ------ */
  85.  
  86. /* Read the current date (in days since Jan. 1, 1980) */
  87. /* and time (in milliseconds since midnight). */
  88. void
  89. gp_get_clock(long *pdt)
  90. {    struct {uint _l0, _l1;} binary_date;
  91.     long lib$day(), sys$bintim();
  92.     long days, days0, seconds;
  93.     char *jan_1_1980 = "1-JAN-1980";
  94.     char *midnight   = "00:00:00.00";
  95.     descrip str_desc;
  96.  
  97.     /* Get days from system zero date (November 17, 1858) to present. */
  98.     (void) lib$day (&days0);
  99.  
  100.     /* For those interested, Wednesday, November 17, 1858 is the base
  101.        of the Modified Julian Day system adopted by the Smithsonian
  102.        Astrophysical Observatory in 1957 for satellite tracking.  (The
  103.        year 1858 preceded the oldest star catalog in use at the
  104.        observatory.)  VMS uses quadword time stamps which are offsets
  105.        in 100 nanosecond units from November 17, 1858.  With a 63-bit
  106.        absolute time representation (sign bit must be clear), VMS will
  107.        have no trouble with time until 31-JUL-31086 02:48:05.47. */
  108.  
  109.     /* Convert January 1, 1980 into a binary absolute time */
  110.     str_desc.dsc$w_length  = strlen(jan_1_1980);
  111.     str_desc.dsc$a_pointer = jan_1_1980;
  112.     (void) sys$bintim (&str_desc, &binary_date);
  113.  
  114.     /* Now get days from system zero date to January 1, 1980 */
  115.     (void) lib$day (&days, &binary_date);
  116.  
  117.     /* Now compute number of days since January 1, 1980 */
  118.     pdt[0] = 1 + days0 - days;
  119.  
  120.     /* Convert midnight into a binary delta time */
  121.     str_desc.dsc$w_length  = strlen(midnight);
  122.     str_desc.dsc$a_pointer = midnight;
  123.     (void)  sys$bintim (&str_desc, &binary_date);
  124.  
  125.     /* Now get number 10 millisecond time units since midnight */
  126.     (void) lib$day (&days, &binary_date, &seconds);
  127.     pdt[1] = 10 * seconds;
  128. }
  129.  
  130. /* ------ Screen management ------ */
  131.  
  132. /* Get the environment variable that specifies the display to use. */
  133. const char *
  134. gp_getenv_display(void)
  135. {    return getenv("DECW$DISPLAY");
  136. }
  137.  
  138. /* ------ Printer accessing ------ */
  139.  
  140. /* Open a connection to a printer.  A null file name means use the */
  141. /* standard printer connected to the machine, if any. */
  142. /* Return NULL if the connection could not be opened. */
  143. FILE *
  144. gp_open_printer(char *fname, int binary_mode)
  145. {
  146.      if (strlen(fname) == 0)
  147.     {    strcpy(fname, gp_scratch_file_name_prefix);
  148.         strcat(fname, "XXXXXX");
  149.         mktemp(fname);
  150.     }
  151.     if ( binary_mode )
  152.     {    /*
  153.          * Printing must be done exactly byte to byte,
  154.          * using "passall".  However the standard VMS symbiont
  155.          * does not treat stream-LF files correctly in this respect,
  156.          * but throws away \n characters.  Giving the file
  157.          * the record type "undefined", but accessing it as a
  158.          * normal stream-LF file does the trick.
  159.          */
  160.         return fopen_VMS(fname, "w", "rfm = udf", "ctx = stm");
  161.     }
  162.     else
  163.     {    /* Open as a normal text stream file. */
  164.         return fopen_VMS(fname, "w", "rfm = var", "rat = cr");
  165.     }
  166. }
  167.  
  168. /* Close the connection to the printer. */
  169. void
  170. gp_close_printer(FILE *pfile, const char *fname)
  171. {    fclose(pfile);
  172. }
  173.  
  174. /* ------ File names ------ */
  175.  
  176. /* Define the character used for separating file names in a list. */
  177. const char gp_file_name_list_separator = ',';
  178.  
  179. /* Define the default scratch file name prefix. */
  180. const char gp_scratch_file_name_prefix[] = "_temp_";
  181.  
  182. /* Define the name of the null output file. */
  183. const char gp_null_file_name[] = "NLA0:";
  184.  
  185. /* Define the name that designates the current directory. */
  186. const char gp_current_directory_name[] = "[]";
  187.  
  188. /* Define the string to be concatenated with the file mode */
  189. /* for opening files without end-of-line conversion. */
  190. const char gp_fmode_binary_suffix[] = "";
  191. /* Define the file modes for binary reading or writing. */
  192. const char gp_fmode_rb[] = "r";
  193. const char gp_fmode_wb[] = "w";
  194.  
  195. /* Create and open a scratch file with a given name prefix. */
  196. /* Write the actual file name at fname. */
  197. FILE *
  198. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  199. {    strcpy(fname, prefix);
  200.     strcat(fname, "XXXXXX");
  201.     mktemp(fname);
  202.     return fopen(fname, mode);
  203. }
  204. /*  Answer whether a file name contains a directory/device specification, i.e.,
  205.  *  is absolute (not directory- or device-relative).  Since for VMS, the concept
  206.  *  of an "absolute" file reference has no meaning.  As Ghostscript is here
  207.  *  merely checking to see if it will make sense to paste a path to the front
  208.  *  of the file name, we use the VMS system service SYS$FILESCAN to check that
  209.  *  the file name has no node, device, root, or directory specification: if all
  210.  *  four of these items are missing from the file name then it is considered to
  211.  *  a relative file name to which a path may be prefixed. (Roots are associated
  212.  *  with rooted logical names.)
  213.  */
  214.  
  215. bool
  216. gp_file_name_is_absolute(const char *fname, uint len)
  217. {
  218.     descrip str_desc;
  219.     /* SYS$FILESCAN takes a uint *, but we want to extract bits. */
  220.     union {
  221.         uint i;
  222.         struct {
  223.          unsigned fscn$v_node : 1;
  224.          unsigned fscn$v_device : 1;
  225.          unsigned fscn$v_root : 1;
  226.          unsigned fscn$v_directory : 1;
  227.          unsigned fscn$v_name : 1;
  228.          unsigned fscn$v_type : 1;
  229.          unsigned fscn$v_version : 1;
  230.          unsigned fscn$v_fill_23 : 1;
  231.         } s;
  232.     } flags;
  233.     uint zero = 0;
  234.  
  235.     str_desc.dsc$w_length  = len;
  236.     str_desc.dsc$a_pointer = (char *)fname;
  237.     SYS$FILESCAN (&str_desc, &zero, &flags.i);
  238.     if ( flags.s.fscn$v_directory || flags.s.fscn$v_root ||
  239.          flags.s.fscn$v_device    || flags.s.fscn$v_node) return true;
  240.     else return false;
  241. }
  242.  
  243. /* Answer the string to be used for combining a directory/device prefix */
  244. /* with a base file name.  The file name is known to not be absolute. */
  245. const char *
  246. gp_file_name_concat_string(const char *prefix, uint plen,
  247.                const char *fname, uint len)
  248. {
  249.     /*  Full VAX/VMS paths are of the form:
  250.      *
  251.      *    device:[root.][directory.subdirectory]filename.extension;version
  252.      *    logical:filename.extension;version
  253.      *
  254.      *  Roots are fairly rare and associated typically with rooted logical
  255.      *  names.
  256.      *
  257.      *  Examples:
  258.      *
  259.      *    DUA1:[GHOSTSCRIPT]GHOST.PS;1
  260.      *    THOR_DEC:[DOOF.A.B.C.D]FILE.DAT;-3
  261.      *    LOG:GHOST.PS  (LOG is a logical defined as DUA1:[GHOSTSCRIPT])
  262.      *    LOG:DOOF.DAT  (LOG is defined as DUA1, current directory is
  263.      *                   is used as the directory spec.)
  264.      *
  265.      */
  266.     if ( plen > 0 )
  267.       switch ( prefix[plen - 1] )
  268.        {    case ':': case ']': return "";
  269.        };
  270.     return ":";
  271. }
  272.  
  273. /* ------ Wild card file search procedures ------ */
  274.  
  275. private void
  276. gp_free_enumeration(file_enum *pfen)
  277. {
  278.     if (pfen) {
  279.       LIB$FIND_FILE_END(&pfen->context);
  280.       gs_free(pfen->pattern->dsc$a_pointer, pfen->length, 1,
  281.           "GP_ENUM(pattern)");
  282.       gs_free((char *)pfen->pattern, sizeof(descrip), 1,
  283.           "GP_ENUM(descriptor)");
  284.       gs_free((char *)pfen, sizeof(file_enum), 1,
  285.           "GP_ENUM(file_enum)");
  286.     }
  287. }
  288.  
  289. /* Begin an enumeration.  See gp.h for details. */
  290.  
  291. file_enum *
  292. gp_enumerate_files_init(const char *pat, uint patlen,
  293.   gs_memory_t *memory)
  294. {
  295.     file_enum *pfen;
  296.     uint i, len;
  297.     char *c, *newpat;
  298.  
  299.     pfen = (file_enum *)gs_malloc(sizeof (file_enum), 1,
  300.                       "GP_ENUM(file_enum)");
  301.     pfen->pattern = (descrip *)gs_malloc(sizeof (descrip), 1,
  302.                          "GP_ENUM(descriptor)");
  303.     newpat = (char *)gs_malloc(patlen, 1, "GP_ENUM(pattern)");
  304.  
  305.     /*  Copy the pattern removing backslash quoting characters and
  306.      *  transforming unquoted question marks, '?', to percent signs, '%'.
  307.      *  (VAX/VMS uses the wildcard '%' to represent exactly one character
  308.      *  and '*' to represent zero or more characters.  Any combination and
  309.      *  number of interspersed wildcards is permitted.)
  310.      */
  311.     c = newpat;
  312.     for ( i = 0; i < patlen; pat++, i++ )
  313.       switch (*pat) {
  314.         case '?'  :
  315.         *c++ = '%'; break;
  316.         case '\\' :
  317.         i++;
  318.         if (i < patlen) *c++ = *++pat;
  319.         break;
  320.         default   :
  321.         *c++ = *pat; break;
  322.       }
  323.     len = c - newpat;
  324.  
  325.     /* Pattern may not exceed 255 characters */
  326.     if (len > 255) {
  327.       gs_free(newpat, patlen, 1, "GP_ENUM(pattern)");
  328.       gs_free((char *)pfen->pattern, sizeof (descrip), 1,
  329.           "GP_ENUM(descriptor)");
  330.       gs_free((char *)pfen, sizeof (file_enum), 1, "GP_ENUM(file_enum)");
  331.       return (file_enum *)0;
  332.     }
  333.  
  334.     pfen->context = 0;
  335.     pfen->length = patlen;
  336.     pfen->pattern->dsc$w_length  = len;
  337.     pfen->pattern->dsc$b_dtype   = DSC$K_DTYPE_T;
  338.     pfen->pattern->dsc$b_class   = DSC$K_CLASS_S;
  339.     pfen->pattern->dsc$a_pointer = newpat;
  340.  
  341.     return pfen;
  342. }
  343.  
  344. /* Return the next file name in the enumeration.  The client passes in */
  345. /* a scratch string and a max length.  If the name of the next file fits, */
  346. /* the procedure returns the length.  If it doesn't fit, the procedure */
  347. /* returns max length +1.  If there are no more files, the procedure */
  348. /* returns -1. */
  349.  
  350. uint
  351. gp_enumerate_files_next(file_enum *pfen, char *ptr, uint maxlen)
  352. {
  353.     char *c, filnam[NAM$C_MAXRSS];
  354.     descrip result = {NAM$C_MAXRSS, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
  355.     uint i, len;
  356.   
  357.     result.dsc$a_pointer = filnam;
  358.  
  359.     /* Find the next file which matches the pattern */
  360.     i = LIB$FIND_FILE(pfen->pattern, &result, &pfen->context,
  361.               (descrip *)0, (descrip *)0, (uint *)0, (uint *)0);
  362.  
  363.     /* Check the return status */
  364.     if (i == RMS$_NMF) {
  365.       gp_free_enumeration (pfen);
  366.       return (uint)-1;
  367.     }
  368.     else if (i != RMS$_NORMAL) return 0;
  369.     else if ((len = strlength (filnam, NAM$C_MAXRSS, ' ')) > maxlen)
  370.       return maxlen+1;
  371.  
  372.     /* Copy the returned filename over to the input string ptr */
  373.     c = ptr;
  374.     for (i = 0; i < len; i++) *c++ = filnam[i];
  375.  
  376.     return len;
  377. }
  378.  
  379. /* Clean up a file enumeration.  This is only called to abandon */
  380. /* an enumeration partway through: ...next should do it if there are */
  381. /* no more files to enumerate.  This should deallocate the file_enum */
  382. /* structure and any subsidiary structures, strings, buffers, etc. */
  383.  
  384. void
  385. gp_enumerate_files_close(file_enum *pfen)
  386. {    gp_free_enumeration (pfen);
  387. }
  388.  
  389. const char *
  390. gp_strerror(int errnum)
  391. {    return NULL;
  392. }
  393.