home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / GSPMSRC / SRC / GP_OS2.C < prev    next >
C/C++ Source or Header  |  1993-12-25  |  9KB  |  293 lines

  1. /* Copyright (C) 1989, 1992 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_os2.c */
  21. /* OS/2 2.x specific routines for Ghostscript */
  22. #define INCL_DOS
  23. #define INCL_WIN
  24.  
  25. #include "memory_.h"
  26. #include "string_.h"
  27. #include "gx.h"
  28. #include "gp.h"
  29. #include "stat_.h"
  30. #include "time_.h"
  31. #include <sys/timeb.h>
  32. #include <os2.h>
  33. #include "gsos2.h"
  34.  
  35. HAB  habGS;     /*  Anchor-block handle for the GhostScript thread.  */
  36.  
  37. /*-------------------------------------------------------------------------*/
  38. /*  Variables and procedures shared with gsos2b.exe.                       */
  39. HWND hwndFrame;
  40. HEV  hevPipesOpened;
  41. /*-------------------------------------------------------------------------*/
  42.  
  43. extern HWND  hwndClient;        /*  declared in gdevpm.c  */
  44.  
  45. FILE *my_popen (char *fname);
  46. void my_pclose (FILE *pfile);
  47.  
  48.  
  49. /* Do platform-dependent initialization. */
  50. void
  51. gp_init()
  52. {
  53.     habGS = WinInitialize (0L);
  54. }
  55.  
  56. /* Do platform-dependent cleanup. */
  57. void
  58. gp_exit(int exit_status, int code)
  59. {
  60. /*  Disable thread switching within the process until the Ghostscript  */
  61. /*    thread is terminated by calling _endthread.                      */
  62.     DosEnterCritSec();
  63.  
  64.     WinPostMsg (hwndFrame, WM_QUIT, 0L, 0L);
  65.     WinTerminate (habGS);
  66. }
  67.  
  68. /* ------ Date and time ------ */
  69.  
  70. /* Read the current date (in days since Jan. 1, 1980) */
  71. /* and time (in milliseconds since midnight). */
  72. void
  73. gp_get_clock(long *pdt)
  74. {
  75.     long secs_since_1980;
  76.     struct timeb tb;
  77.  
  78.     ftime (&tb);
  79.  
  80.     /* tb.time is #secs since Jan 1, 1970 */
  81.     /* subtract off number of seconds in 10 years */
  82.     /* leap seconds are not accounted for */
  83.     secs_since_1980 = tb.time - (long)(60 * 60 * 24 * 365.25 * 10);
  84.  
  85.     /* adjust for timezone */
  86.     secs_since_1980 -= (tb.timezone * 60);
  87.  
  88.     /* adjust for daylight savings time - assume dst offset is 1 hour */
  89.     if (tb.dstflag)
  90.         secs_since_1980 += (60 * 60);
  91.  
  92.     /* divide secs by #secs/day to get #days (integer division truncates) */
  93.     pdt[0] = secs_since_1980 / (60 * 60 * 24);
  94.     /* modulo + millisecs gives number of millisecs since midnight */
  95.     pdt[1] = (secs_since_1980 % (60 * 60 * 24)) * 1000 + tb.millitm;
  96. }
  97.  
  98. /* ------ Screen management ------ */
  99.  
  100. /* Write a string to the console. */
  101. void
  102. gp_console_puts(const char *str, uint size)
  103. {       fwrite(str, 1, size, stdout);
  104. }
  105.  
  106. /* Make the console current on the screen. */
  107. int
  108. gp_make_console_current(gx_device *dev)
  109. {       return 0;
  110. }
  111.  
  112. /* Make the graphics current on the screen. */
  113. int
  114. gp_make_graphics_current(gx_device *dev)
  115. {       return 0;
  116. }
  117.  
  118. /* ------ Printer accessing ------ */
  119.  
  120. /* Open a connection to a printer.  A null file name means use the */
  121. /* standard printer connected to the machine, if any. */
  122. /* "|command" opens an output pipe. */
  123. /* Return NULL if the connection could not be opened. */
  124. FILE *
  125. gp_open_printer(char *fname, int binary_mode)
  126. {       return
  127.           (strlen(fname) == 0 ?
  128.            fopen ("lpt1", "wb") :
  129.            fname[0] == '|' ?
  130.            my_popen(fname + 1) :
  131.            fopen(fname, "wb"));
  132. }
  133.  
  134.  
  135. FILE *
  136. my_popen (char *fname)
  137. {
  138.     FILE  *fp;
  139.     ULONG  ulPostCt;
  140.  
  141.     DosResetEventSem (hevPipesOpened, &ulPostCt);
  142.  
  143.     fp = fopen (PIPE_GS_CMDPIPE, "wb");
  144.     fputs (fname, fp);
  145.     fflush (fp);
  146.  
  147.     DosWaitEventSem (hevPipesOpened, SEM_INDEFINITE_WAIT);
  148.  
  149.     return (fp);
  150. }
  151.  
  152. void
  153. my_pclose (FILE *pfile)
  154. {
  155.     fclose (pfile);
  156. }
  157.  
  158. /* Close the connection to the printer. */
  159. void
  160. gp_close_printer(FILE *pfile, const char *fname)
  161. {       if ( fname[0] == '|' )
  162.                 my_pclose(pfile);
  163.         else
  164.                 fclose(pfile);
  165. }
  166.  
  167. /* ------ File names ------ */
  168.  
  169. /* Define the character used for separating file names in a list. */
  170. const char gp_file_name_list_separator = ';';
  171.  
  172. /* Define the default scratch file name prefix. */
  173. const char gp_scratch_file_name_prefix[] = "tm";
  174.  
  175. /* Define whether case is insignificant in file names. */
  176. const int gp_file_names_ignore_case = 1;
  177.  
  178. /* Define the string to be concatenated with the file mode */
  179. /* for opening files without end-of-line conversion. */
  180. const char gp_fmode_binary_suffix[] = "b";
  181. /* Define the file modes for binary reading or writing. */
  182. const char gp_fmode_rb[] = "rb";
  183. const char gp_fmode_wb[] = "wb";
  184.  
  185. /* Create and open a scratch file with a given name prefix. */
  186. /* Write the actual file name at fname. */
  187. FILE *
  188. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  189. {       strcpy(fname, prefix);
  190.         strcat(fname, "XXXXXX");
  191.         mktemp(fname);
  192.         return fopen(fname, mode);
  193. }
  194.  
  195. /* Answer whether a file name contains a directory/device specification, */
  196. /* i.e. is absolute (not directory- or device-relative). */
  197. int
  198. gp_file_name_is_absolute(const char *fname, uint len)
  199. {       /* A file name is absolute if it contains a drive specification */
  200.         /* (second character is a :) or if it start with / or \. */
  201.         return ( len >= 1 && (*fname == '/' || *fname == '\\' ||
  202.                 (len >= 2 && fname[1] == ':')) );
  203. }
  204.  
  205. /* Answer the string to be used for combining a directory/device prefix */
  206. /* with a base file name.  The file name is known to not be absolute. */
  207. const char *
  208. gp_file_name_concat_string(const char *prefix, uint plen,
  209.                            const char *fname, uint len)
  210. {       if ( plen > 0 && prefix[plen - 1] == '/' )
  211.                 return "";
  212.         return "/";
  213. }
  214.  
  215. /* ------ File operations ------ */
  216.  
  217. /* If the file given by fname exists, fill in its status and return 1; */
  218. /* otherwise return 0. */
  219. int
  220. gp_file_status(const char *fname, file_status *pstatus)
  221. {       struct stat sbuf;
  222.         /* The RS/6000 prototype for stat doesn't include const, */
  223.         /* so we have to explicitly remove the const modifier. */
  224.         if ( stat((char *)fname, &sbuf) < 0 ) return 0;
  225.         pstatus->size_pages = stat_blocks(&sbuf);       /* st_blocks is */
  226.                                         /* missing on some systems, */
  227.                                         /* see stat_.h */
  228.         pstatus->size_bytes = sbuf.st_size;
  229.         pstatus->time_referenced = sbuf.st_mtime;
  230.         pstatus->time_created = sbuf.st_ctime;
  231.         return 1;
  232. }
  233.  
  234. /* ------ File enumeration ------ */
  235.  
  236. /****** THIS IS NOT SUPPORTED ON THE OS/2 2.x SYSTEMS. ******/
  237. /****** THIS IS NOT SUPPORTED ON UNIX SYSTEMS. ******/
  238. /* Amazingly enough, there is no standard Unix library routine */
  239. /* for enumerating the files matching a pattern, */
  240. /* or even for enumerating (conveniently) the files in a directory. */
  241.  
  242. struct file_enum_s {
  243.         char *pattern;
  244.         int first_time;
  245.         const gs_memory_procs *mprocs;
  246. };
  247.  
  248. /* Initialize an enumeration.  NEEDS WORK ON HANDLING * ? \. */
  249. file_enum *
  250. gp_enumerate_files_init(const char *pat, uint patlen,
  251.   const gs_memory_procs *mprocs)
  252. {       file_enum *pfen = (file_enum *)(*mprocs->alloc)(1, sizeof(file_enum), "gp_enumerate_files");
  253.         char *pattern;
  254.         if ( pfen == 0 ) return 0;
  255.         pattern = (*mprocs->alloc)(patlen + 1, 1,
  256.                             "gp_enumerate_files(pattern)");
  257.         if ( pattern == 0 ) return 0;
  258.         memcpy(pattern, pat, patlen);
  259.         pattern[patlen] = 0;
  260.         pfen->pattern = pattern;
  261.         pfen->mprocs = mprocs;
  262.         pfen->first_time = 1;
  263.         return pfen;
  264. }
  265.  
  266. /* Enumerate the next file. */
  267. /* PUNT: JUST RETURN THE PATTERN. */
  268. uint
  269. gp_enumerate_files_next(file_enum *pfen, char *ptr, uint maxlen)
  270. {       if ( pfen->first_time )
  271.         {       char *pattern = pfen->pattern;
  272.                 uint len = strlen(pattern);
  273.                 pfen->first_time = 0;
  274.                 if ( len > maxlen )
  275.                         return maxlen + 1;
  276.                 strcpy(ptr, pattern);
  277.                 return len;
  278.         }
  279.         return -1;
  280. }
  281.  
  282. /* Clean up the file enumeration. */
  283. void
  284. gp_enumerate_files_close(file_enum *pfen)
  285. {       const gs_memory_procs *mprocs = pfen->mprocs;
  286.         (*mprocs->free)(pfen->pattern, strlen(pfen->pattern) + 1, 1,
  287.                         "gp_enumerate_files_close(pattern)");
  288.         (*mprocs->free)((char *)pfen, 1, sizeof(file_enum),
  289.                         "gp_enumerate_files_close");
  290. }
  291.  
  292.  
  293.