home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GP_OS2.C < prev    next >
C/C++ Source or Header  |  1994-08-01  |  16KB  |  558 lines

  1. /* Copyright (C) 1992, 1993 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_os2.c */
  20. /* Common platform-specific routines for OS/2 and MS-DOS */
  21. /* compiled with GCC/EMX */
  22.  
  23. #define INCL_DOS
  24. #include <os2.h>
  25.  
  26. #include "stdio_.h"
  27. #include "string_.h"
  28. #include <fcntl.h>
  29.  
  30. #ifdef __IBMC__
  31. #define popen fopen    /* doesn't support popen */
  32. #else
  33. #include <dos.h>
  34. #endif
  35. /* Define the regs union tag for short registers. */
  36. #  define rshort x
  37. #define intdos(a,b) _int86(0x21, a, b)
  38.  
  39. #include "memory_.h"
  40. #include "string_.h"
  41. #include "gx.h"
  42. #include "gsexit.h"
  43. #include "gsmemory.h"
  44. #include "gsstruct.h"
  45. #include "gp.h"
  46. #include "gsutil.h"
  47. #include "stdlib.h"    /* need _osmode */
  48. #include "time_.h"
  49. #include <time.h>        /* should this be in time_.h? */
  50. #include "gdevpm.h"
  51. #ifdef __EMX__
  52. #include <sys/emxload.h>
  53. #endif
  54.  
  55. /* ------ Miscellaneous ------ */
  56.  
  57. /* Get the string corresponding to an OS error number. */
  58. /* All reasonable compilers support it. */
  59. const char *
  60. gp_strerror(int errnum)
  61. {    return strerror(errnum);
  62. }
  63.  
  64. /* use Unix version for date and time */
  65. /* ------ Date and time ------ */
  66.  
  67. /* Read the current date (in days since Jan. 1, 1980) */
  68. /* and time (in milliseconds since midnight). */
  69. void
  70. gp_get_clock(long *pdt)
  71. {    long secs_since_1980;
  72.     struct timeval tp;
  73.     struct timezone tzp;
  74.     time_t tsec;
  75.     struct tm *tm, *localtime();
  76.  
  77.     if ( gettimeofday(&tp, &tzp) == -1 )
  78.        {    lprintf("Ghostscript: gettimeofday failed!\n");
  79.         gs_exit(1);
  80.        }
  81.  
  82.     /* tp.tv_sec is #secs since Jan 1, 1970 */
  83.  
  84.     /* subtract off number of seconds in 10 years */
  85.     /* leap seconds are not accounted for */
  86.     secs_since_1980 = tp.tv_sec - (long)(60 * 60 * 24 * 365.25 * 10);
  87.  
  88.     /* adjust for timezone */
  89.     secs_since_1980 -= (tzp.tz_minuteswest * 60);
  90.  
  91.     /* adjust for daylight savings time - assume dst offset is 1 hour */
  92.     tsec = tp.tv_sec;
  93.     tm = localtime(&tsec);
  94.     if ( tm->tm_isdst )
  95.         secs_since_1980 += (60 * 60);
  96.  
  97.     /* divide secs by #secs/day to get #days (integer division truncates) */
  98.     pdt[0] = secs_since_1980 / (60 * 60 * 24);
  99.     /* modulo + microsecs/1000 gives number of millisecs since midnight */
  100.     pdt[1] = (secs_since_1980 % (60 * 60 * 24)) * 1000 + tp.tv_usec / 1000;
  101. #ifdef DEBUG_CLOCK
  102.     printf("tp.tv_sec = %d  tp.tv_usec = %d  pdt[0] = %ld  pdt[1] = %ld\n",
  103.         tp.tv_sec, tp.tv_usec, pdt[0], pdt[1]);
  104. #endif
  105. }
  106.  
  107.  
  108. /* ------ Console management ------ */
  109.  
  110. /* Answer whether a given file is the console (input or output). */
  111. /* This is not a standard gp procedure, */
  112. /* but the MS Windows configuration needs it, */
  113. /* and other MS-DOS configurations might need it someday. */
  114. /* Don't know if it is needed for OS/2. */
  115. bool
  116. gp_file_is_console(FILE *f)
  117. {
  118. #ifndef __DLL__
  119.     if (_osmode == DOS_MODE) {
  120.     union REGS regs;
  121.     if ( f == NULL )
  122.         return false;
  123.     regs.h.ah = 0x44;    /* ioctl */
  124.     regs.h.al = 0;        /* get device info */
  125.     regs.rshort.bx = fileno(f);
  126.     intdos(®s, ®s);
  127.     return ((regs.h.dl & 0x80) != 0 && (regs.h.dl & 3) != 0);
  128.     }
  129. #endif
  130.     if ( (f==stdin) || (f==stdout) || (f==stderr) )
  131.     return true;
  132.     return false;
  133. }
  134.  
  135. /* ------ File names ------ */
  136.  
  137. /* Define the character used for separating file names in a list. */
  138. const char gp_file_name_list_separator = ';';
  139.  
  140. /* Define the default scratch file name prefix. */
  141. const char gp_scratch_file_name_prefix[] = "gs";
  142.  
  143. /* Define the name of the null output file. */
  144. const char gp_null_file_name[] = "nul";
  145.  
  146. /* Define the name that designates the current directory. */
  147. const char gp_current_directory_name[] = ".";
  148.  
  149. /* Define the string to be concatenated with the file mode */
  150. /* for opening files without end-of-line conversion. */
  151. const char gp_fmode_binary_suffix[] = "b";
  152. /* Define the file modes for binary reading or writing. */
  153. const char gp_fmode_rb[] = "rb";
  154. const char gp_fmode_wb[] = "wb";
  155.  
  156. /* Answer whether a file name contains a directory/device specification, */
  157. /* i.e. is absolute (not directory- or device-relative). */
  158. bool
  159. gp_file_name_is_absolute(const char *fname, uint len)
  160. {    /* A file name is absolute if it contains a drive specification */
  161.     /* (second character is a :) or if it start with / or \. */
  162.     return ( len >= 1 && (*fname == '/' || *fname == '\\' ||
  163.         (len >= 2 && fname[1] == ':')) );
  164. }
  165.  
  166. /* Answer the string to be used for combining a directory/device prefix */
  167. /* with a base file name.  The file name is known to not be absolute. */
  168. const char *
  169. gp_file_name_concat_string(const char *prefix, uint plen,
  170.   const char *fname, uint len)
  171. {    if ( plen > 0 )
  172.       switch ( prefix[plen - 1] )
  173.        {    case ':': case '/': case '\\': return "";
  174.        };
  175.     return "\\";
  176. }
  177.  
  178.  
  179. /* ------ File enumeration ------ */
  180.  
  181.  
  182. struct file_enum_s {
  183.     FILEFINDBUF3 findbuf;
  184.     HDIR hdir;
  185.     char *pattern;
  186.     int patlen;            /* orig pattern length */
  187.     int pat_size;            /* allocate space for pattern */
  188.     int first_time;
  189.     gs_memory_t *memory;
  190. };
  191. gs_private_st_ptrs1(st_file_enum, struct file_enum_s, "file_enum",
  192.   file_enum_enum_ptrs, file_enum_reloc_ptrs, pattern);
  193.  
  194. /* Initialize an enumeration.  may NEED WORK ON HANDLING * ? \. */
  195. file_enum *
  196. gp_enumerate_files_init(const char *pat, uint patlen, gs_memory_t *mem)
  197. {    file_enum *pfen = gs_alloc_struct(mem, file_enum, &st_file_enum, "gp_enumerate_files");
  198.     int pat_size = 2 * patlen + 1;
  199.     char *pattern;
  200.     if ( pfen == 0 ) return 0;
  201.  
  202.     /* pattern could be allocated as a string, */
  203.     /* but it's simpler for GC and freeing to allocate it as bytes. */
  204.  
  205.     pattern = (char *)gs_alloc_bytes(mem, pat_size,
  206.                      "gp_enumerate_files(pattern)");
  207.     if ( pattern == 0 ) return 0;
  208.     memcpy(pattern, pat, patlen);
  209.     pattern[patlen] = 0;
  210.     pfen->pattern = pattern;
  211.     pfen->patlen = patlen;
  212.     pfen->pat_size = pat_size;
  213.     pfen->memory = mem;
  214.     pfen->first_time = 1;
  215.     pfen->hdir = HDIR_CREATE;
  216.     return pfen;
  217. }
  218.  
  219. /* Enumerate the next file. */
  220. uint
  221. gp_enumerate_files_next(file_enum *pfen, char *ptr, uint maxlen)
  222. {
  223.     APIRET rc;
  224.     ULONG cFilenames = 1;
  225. #ifndef __DLL__
  226.     if (_osmode == DOS_MODE) {
  227.     /* CAN'T DO IT SO JUST RETURN THE PATTERN. */
  228.     if ( pfen->first_time )
  229.     {    char *pattern = pfen->pattern;
  230.         uint len = strlen(pattern);
  231.         pfen->first_time = 0;
  232.         if ( len > maxlen )
  233.             return maxlen + 1;
  234.         strcpy(ptr, pattern);
  235.         return len;
  236.     }
  237.     return -1;
  238.     }
  239. #endif
  240.  
  241.     /* else OS/2 */
  242.     if ( pfen->first_time ) {
  243.     rc = DosFindFirst(pfen->pattern, &pfen->hdir, FILE_NORMAL,
  244.         &pfen->findbuf, sizeof(pfen->findbuf), 
  245.         &cFilenames, FIL_STANDARD);
  246.     pfen->first_time = 0;
  247.     }
  248.     else {
  249.     rc = DosFindNext(pfen->hdir, &pfen->findbuf, sizeof(pfen->findbuf),
  250.         &cFilenames);
  251.     }
  252.     if (rc)
  253.     return -1;
  254.     
  255.     if (pfen->findbuf.cchName < maxlen) {
  256.     strcpy(ptr, pfen->findbuf.achName);
  257.     return pfen->findbuf.cchName;
  258.     }
  259.  
  260.     strncpy(ptr, pfen->findbuf.achName, maxlen);
  261.     return maxlen+1;
  262.  
  263. }
  264.  
  265. /* Clean up the file enumeration. */
  266. void
  267. gp_enumerate_files_close(file_enum *pfen)
  268. {
  269.     gs_memory_t *mem = pfen->memory;
  270. #ifndef __DLL__
  271.     if (_osmode == OS2_MODE)
  272.         DosFindClose(pfen->hdir);
  273. #endif
  274.     gs_free_object(mem, pfen->pattern,
  275.                "gp_enumerate_files_close(pattern)");
  276.     gs_free_object(mem, pfen, "gp_enumerate_files_close");
  277. }
  278.  
  279. /*************************************************************/
  280. /* from gp_iwatc.c and gp_itbc.c */
  281.  
  282. /* Intel processor, EMX/GCC specific routines for Ghostscript */
  283. #include <signal.h>
  284. #include "stat_.h"
  285. #include "string_.h"
  286.  
  287. /* Library routines not declared in a standard header */
  288. extern char *getenv(P1(const char *));
  289.  
  290. /* Forward declarations */
  291. private void handle_FPE(P1(int));
  292.  
  293. /* Do platform-dependent initialization. */
  294. void
  295. gp_init(void)
  296. {
  297.     /* keep gsos2.exe in memory for number of minutes specified in */
  298.     /* environment variable GS_LOAD */
  299. #ifdef __EMX__
  300.     _emxload_env("GS_LOAD");
  301. #endif
  302.     /* Set up the handler for numeric exceptions. */
  303.     signal(SIGFPE, handle_FPE);
  304.     gp_init_console();
  305. }
  306.  
  307.  
  308. /* Trap numeric exceptions.  Someday we will do something */
  309. /* more appropriate with these. */
  310. private void
  311. handle_FPE(int sig)
  312. {    eprintf("Numeric exception:\n");
  313.     exit(1);
  314. }
  315.  
  316. extern int gs_exit_status;
  317. /* Do platform-dependent cleanup. */
  318. void
  319. gp_exit(int exit_status, int code)
  320. {
  321.     if (exit_status && (_osmode == OS2_MODE))
  322.         DosSleep(2000);
  323. }
  324.  
  325. /* ------ Printer accessing ------ */
  326.  
  327. /* Put a printer file (which might be stdout) into binary or text mode. */
  328. /* This is not a standard gp procedure, */
  329. /* but all MS-DOS configurations need it. */
  330. void
  331. gp_set_printer_binary(int prnfno, int binary)
  332. {
  333. #ifndef __IBMC__
  334.     union REGS regs;
  335.     regs.h.ah = 0x44;    /* ioctl */
  336.     regs.h.al = 0;        /* get device info */
  337.     regs.rshort.bx = prnfno;
  338.     intdos(®s, ®s);
  339.     if ( ((regs.rshort.flags)&1) != 0 || !(regs.h.dl & 0x80) )
  340.         return;        /* error, or not a device */
  341.     if ( binary )
  342.         regs.h.dl |= 0x20;    /* binary (no ^Z intervention) */
  343.     else
  344.         regs.h.dl &= ~0x20;    /* text */
  345.     regs.h.dh = 0;
  346.     regs.h.ah = 0x44;    /* ioctl */
  347.     regs.h.al = 1;        /* set device info */
  348.     intdos(®s, ®s);
  349. #endif
  350. }
  351.  
  352. /* Open a connection to a printer.  A null file name means use the */
  353. /* standard printer connected to the machine, if any. */
  354. /* "|command" opens an output pipe. */
  355. /* Return NULL if the connection could not be opened. */
  356. FILE *
  357. gp_open_printer(char *fname, int binary_mode)
  358. {    FILE *pfile;
  359.     if ( strlen(fname) == 0 ) 
  360.         pfile = fopen("PRN", (binary_mode ? "wb" : "w"));
  361. #ifdef __DLL__
  362.     else if (fname[0] == '|')
  363. #else
  364.     else if ( (_osmode == OS2_MODE) && (fname[0] == '|') )
  365. #endif
  366.         pfile = popen(fname+1, (binary_mode ? "wb" : "w"));
  367.     else
  368.         pfile = fopen(fname, (binary_mode ? "wb" : "w"));
  369.     if ( pfile == (FILE *)NULL )
  370.         return (FILE *)NULL;
  371. #ifndef __DLL__
  372.     if (_osmode == DOS_MODE)
  373.         gp_set_printer_binary(fileno(pfile), binary_mode);
  374. #endif
  375.     return pfile;
  376. }
  377.  
  378. /* Close the connection to the printer. */
  379. void
  380. gp_close_printer(FILE *pfile, const char *fname)
  381. {    
  382. #ifdef __DLL__
  383.     if (fname[0] == '|')
  384. #else
  385.     if ( (_osmode == OS2_MODE) && (fname[0] == '|') )
  386. #endif
  387.         pclose(pfile);
  388.     else
  389.         fclose(pfile);
  390. }
  391.  
  392. /* ------ File names ------ */
  393.  
  394. /* Create and open a scratch file with a given name prefix. */
  395. /* Write the actual file name at fname. */
  396. FILE *
  397. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  398. {    char *temp;
  399.     if ( (temp = getenv("TEMP")) == NULL )
  400.         *fname = 0;
  401.     else
  402.     {    char last = '\\';
  403.         strcpy(fname, temp);
  404.         /* Prevent X's in path from being converted by mktemp. */
  405.         for ( temp = fname; *temp; temp++ )
  406.             *temp = last = tolower(*temp);
  407.         switch ( last )
  408.         {
  409.         default:
  410.             strcat(fname, "\\");
  411.         case ':': case '\\':
  412.             ;
  413.         }
  414.     }
  415.     strcat(fname, prefix);
  416.     strcat(fname, "XXXXXX");
  417.     mktemp(fname);
  418.     return fopen(fname, mode);
  419. }
  420.  
  421. #ifdef __DLL__
  422. /* The DLL version must not be allowed direct access to stdin and stdout */
  423. /* Instead these are redirected to the gsdll_callback */
  424. #include "gsdll.h"
  425. #include <stdarg.h>
  426.  
  427. /* for redirecting stdin/out/err */
  428. #include "stream.h"
  429. #include "gxiodev.h"            /* must come after stream.h */
  430. extern stream *gs_stream_stdin;        /* from ziodev.c */
  431. extern stream *gs_stream_stdout;    /* from ziodev.c */
  432. extern stream *gs_stream_stderr;    /* from ziodev.c */
  433.  
  434.  
  435. /* ====== Substitute for stdio ====== */
  436.  
  437. /* this code has been derived from gp_mswin.c */
  438.  
  439. /* Forward references */
  440. private void pm_std_init(void);
  441. private void pm_pipe_init(void);
  442. private stream_proc_process(pm_std_read_process);
  443. private stream_proc_process(pm_std_write_process);
  444.  
  445. /* Use a pseudo IODevice to get pm_stdio_init called at the right time. */
  446. /* This is bad architecture; we'll fix it later. */
  447. private iodev_proc_init(pm_stdio_init);
  448. gx_io_device gs_iodev_wstdio = {
  449.     "wstdio", "Special",
  450.     { pm_stdio_init, iodev_no_open_device,
  451.       iodev_no_open_file, iodev_no_fopen, iodev_no_fclose,
  452.       iodev_no_delete_file, iodev_no_rename_file,
  453.       iodev_no_file_status, iodev_no_enumerate_files
  454.     }
  455. };
  456.  
  457. /* Discard the contents of the buffer when reading. */
  458. void
  459. pm_std_read_reset(stream *s)
  460. {    s_std_read_reset(s);
  461.     s->end_status = 0;
  462. }
  463.  
  464. /* Do one-time initialization */
  465. private int
  466. pm_stdio_init(gx_io_device *iodev, gs_memory_t *mem)
  467. {
  468. /* reinitialize stdin/out/err to use callback */
  469. /* assume stream has already been initialized for the real stdin */
  470.     if ( gp_file_is_console(gs_stream_stdin->file) )
  471.     {    /* Allocate a real buffer for stdin. */
  472.         /* The size must not exceed the size of the */
  473.         /* lineedit buffer.  (This is a hack.) */
  474. #define pm_stdin_buf_size 160
  475.         static const stream_procs pin =
  476.         {    s_std_noavailable, s_std_noseek, pm_std_read_reset,
  477.             s_std_read_flush, s_std_close,
  478.             pm_std_read_process
  479.         };
  480.         byte *buf = (byte *)gs_malloc(pm_stdin_buf_size, 1,
  481.                           "pm_stdin_init");
  482.         s_std_init(gs_stream_stdin, buf, pm_stdin_buf_size,
  483.                &pin, s_mode_read);
  484.         gs_stream_stdin->file = NULL;
  485.     }
  486.  
  487.     {    static const stream_procs pout =
  488.         {    s_std_noavailable, s_std_noseek, s_std_write_reset,
  489.             s_std_write_flush, s_std_close,
  490.             pm_std_write_process
  491.         };
  492.         if ( gp_file_is_console(gs_stream_stdout->file) )
  493.         {    gs_stream_stdout->procs = pout;
  494.             gs_stream_stdout->file = NULL;
  495.         }
  496.         if ( gp_file_is_console(gs_stream_stderr->file) )
  497.         {    gs_stream_stderr->procs = pout;
  498.             gs_stream_stderr->file = NULL;
  499.         }
  500.     }
  501.     return 0;
  502. }
  503.  
  504.  
  505. /* We should really use a private buffer for line reading, */
  506. /* because we can't predict the size of the supplied input area.... */
  507. private int
  508. pm_std_read_process(stream_state *st, stream_cursor_read *ignore_pr,
  509.   stream_cursor_write *pw, bool last)
  510. {
  511.     int count = pw->limit - pw->ptr;
  512.  
  513.     if ( count == 0 )         /* empty buffer */
  514.         return 1;
  515.  
  516. /* callback to get more input */
  517.     count = (*pgsdll_callback)(GSDLL_STDIN, pw->ptr+1, count);
  518.     if (count == 0) {
  519.         /* EOF */
  520.         /* what should we do? */
  521.         return EOFC;
  522.     }
  523.  
  524.     pw->ptr += count;
  525.     return 1;
  526. }
  527.  
  528. private int
  529. pm_std_write_process(stream_state *st, stream_cursor_read *pr,
  530.   stream_cursor_write *ignore_pw, bool last)
  531. {    uint count = pr->limit - pr->ptr;
  532.     (*pgsdll_callback)(GSDLL_STDOUT, (char *)(pr->ptr + 1), count);
  533.     pr->ptr = pr->limit;
  534.     return 0;
  535. }
  536.  
  537. /* This is used instead of the stdio version. */
  538. /* The declaration must be identical to that in <stdio.h>. */
  539. int 
  540. fprintf(FILE *file, const char *fmt, ...)
  541. {
  542. int count;
  543. va_list args;
  544.     va_start(args,fmt);
  545.     if ( gp_file_is_console(file) ) {
  546.         char buf[1024];
  547.         count = vsprintf(buf,fmt,args);
  548.         (*pgsdll_callback)(GSDLL_STDOUT, buf, count);
  549.     }
  550.     else {
  551.         count = vfprintf(file, fmt, args);
  552.     }
  553.     va_end(args);
  554.     return count;
  555. }
  556. #endif /* __DLL__ */
  557.  
  558.