home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / g / gs241j11.zip / GP_DJGPP.C < prev    next >
C/C++ Source or Header  |  1992-05-09  |  8KB  |  287 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_djgpp.c */
  21. /* DJ's GCC-specific routines for Ghostscript */
  22. #include "memory_.h"
  23. #include "string_.h"
  24. #include "gx.h"
  25. #include "gp.h"
  26. #include "signal.h"
  27. #include "stat_.h"
  28. #include "time_.h"
  29. #include "dos_.h"
  30.  
  31. /* Do platform-dependent initialization. */
  32. void
  33. gp_init()
  34. {
  35. }
  36.  
  37. /* Do platform-dependent cleanup. */
  38. void
  39. gp_exit()
  40. {
  41. }
  42.  
  43. /* ------ Date and time ------ */
  44.  
  45. /* Read the current date (in days since Jan. 1, 1980) */
  46. /* and time (in milliseconds since midnight). */
  47. void
  48. gp_get_clock(long *pdt)
  49. {    union REGS osdate, ostime;
  50.     long idate;
  51.     static int mstart[12] =
  52.        { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  53.     osdate.h.ah = 0x2a;        /* get date */
  54.     intdos(&osdate, &osdate);
  55. #define da_year rshort.cx
  56. #define da_mon h.dh
  57. #define da_day h.dl
  58.     ostime.h.ah = 0x2c;        /* get time */
  59.     intdos(&ostime, &ostime);
  60. #define ti_hour h.ch
  61. #define ti_min h.cl
  62. #define ti_sec h.dh
  63. #define ti_hund h.dl
  64.     idate = (long)osdate.da_year * 365 +
  65.         (osdate.da_year / 4 + 1 +    /* account for leap years */
  66.          mstart[osdate.da_mon - 1] +    /* month is 1-origin */
  67.          osdate.da_day - 1);        /* day of month is 1-origin */
  68.     if ( osdate.da_mon <= 2 && osdate.da_year % 4 == 0 )        /* Jan. or Feb. of leap year */
  69.         idate--;
  70.     pdt[0] = idate;
  71.     pdt[1] =
  72.         (ostime.ti_hour * 60 + ostime.ti_min) * 60000L +
  73.         (ostime.ti_sec * 100 + ostime.ti_hund) * 10L;
  74. }
  75.  
  76. /* ------ Screen management ------ */
  77.  
  78. /* Write a string to the console. */
  79. void
  80. gp_console_puts(const char *str, uint size)
  81. {    fwrite(str, 1, size, stdout);
  82. }
  83.  
  84. /* Make the console current on the screen. */
  85. int
  86. gp_make_console_current(struct gx_device_s *dev)
  87. {    return 0;
  88. }
  89.  
  90. /* Make the graphics current on the screen. */
  91. int
  92. gp_make_graphics_current(struct gx_device_s *dev)
  93. {    return 0;
  94. }
  95.  
  96. /* ------ Printer accessing ------ */
  97.  
  98. /* Open a connection to a printer.  A null file name means use the */
  99. /* standard printer connected to the machine, if any. */
  100. /* Return NULL if the connection could not be opened. */
  101. extern void gp_set_printer_binary(P1(int));
  102. FILE *
  103. gp_open_printer(char *fname)
  104. {    if ( strlen(fname) == 0 || !strcmp(fname, "PRN") )
  105.        {    FILE *fp = fopen("PRN", "wb");
  106.         if(fp == NULL)
  107.             return NULL;
  108.         gp_set_printer_binary(fileno(fp));
  109.         return fp;
  110.        }
  111.     else
  112.         return fopen(fname, "wb");
  113. }
  114.  
  115. /* Close the connection to the printer. */
  116. void
  117. gp_close_printer(FILE *pfile, const char *fname)
  118. {    fclose(pfile);
  119. }
  120.  
  121. /* ------ Printer accessing ------ */
  122.  
  123. /* Put the printer into binary mode.  This is not a standard gp procedure, */
  124. /* but all MS-DOS configurations need it. */
  125. void
  126. gp_set_printer_binary(int prnfno)
  127. {    union REGS regs;
  128.     regs.h.ah = 0x44;    /* ioctl */
  129.     regs.h.al = 0;        /* get device info */
  130.     regs.rshort.bx = prnfno;
  131.     intdos(®s, ®s);
  132.     regs.h.dl |= 0x20;    /* binary (no ^Z intervention) */
  133.     regs.h.dh = 0;
  134.     regs.h.ah = 0x44;    /* ioctl */
  135.     regs.h.al = 1;        /* set device info */
  136.     intdos(®s, ®s);
  137. }
  138.  
  139. /* ------ File names ------ */
  140.  
  141. /* Define the character used for separating file names in a list. */
  142. const char gp_file_name_list_separator = ';';
  143.  
  144. /* Define the default scratch file name prefix. */
  145. const char gp_scratch_file_name_prefix[] = "_temp_";
  146.  
  147. /* Define whether case is insignificant in file names. */
  148. const int gp_file_names_ignore_case = 1;
  149.  
  150. /* Create and open a scratch file with a given name prefix. */
  151. /* Write the actual file name at fname. */
  152. FILE *
  153. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  154. {    strcpy(fname, prefix);
  155.     strcat(fname, "XXXXXX");
  156.     mktemp(fname);
  157.     return fopen(fname, mode);
  158. }
  159.  
  160. /* Answer whether a file name contains a directory/device specification, */
  161. /* i.e. is absolute (not directory- or device-relative). */
  162. int
  163. gp_file_name_is_absolute(const char *fname, uint len)
  164. {    /* A file name is absolute if it contains a drive specification */
  165.     /* (second character is a :) or if it start with / or \. */
  166.     return ( len >= 1 && (*fname == '/' || *fname == '\\' ||
  167.         (len >= 2 && fname[1] == ':')) );
  168. }
  169.  
  170. /* Answer the string to be used for combining a directory/device prefix */
  171. /* with a base file name.  The file name is known to not be absolute. */
  172. char *
  173. gp_file_name_concat_string(const char *prefix, uint plen,
  174.   const char *fname, uint len)
  175. {    if ( plen > 0 )
  176.       switch ( prefix[plen - 1] )
  177.        {    case ':': case '/': case '\\': return "";
  178.        };
  179.     return "\\";
  180. }
  181.  
  182. /* ------ File operations ------ */
  183.  
  184. /* If the file given by fname exists, fill in its status and return 1; */
  185. /* otherwise return 0. */
  186. int
  187. gp_file_status(const char *fname, file_status *pstatus)
  188. {    struct stat sbuf;
  189.     /* The RS/6000 prototype for stat doesn't include const, */
  190.     /* so we have to explicitly remove the const modifier. */
  191.     if ( stat((char *)fname, &sbuf) < 0 ) return 0;
  192.     pstatus->size_pages = (sbuf.st_size + 1023) >> 10; /* from gp_itbc.c */
  193.     pstatus->size_bytes = sbuf.st_size;
  194.     pstatus->time_referenced = sbuf.st_mtime;
  195.     pstatus->time_created = sbuf.st_ctime;
  196.     return 1;
  197. }
  198.  
  199. /* ------ File enumeration ------ */
  200.  
  201. struct file_enum_s {
  202.     ff_struct_t ffblk;
  203.     char *pattern;
  204.     int patlen;            /* allocated length of pattern */
  205.     int head_size;            /* pattern length through last */
  206.                     /* : or \ */
  207.     int first_time;
  208.     gs_memory_procs mprocs;
  209. };
  210.  
  211. /* Initialize an enumeration.  Note that * and ? in a directory */
  212. /* don't work, and \* and \? don't work. */
  213. file_enum *
  214. gp_enumerate_files_init(const char *pat, uint patlen,
  215.   proc_alloc_t palloc, proc_free_t pfree)
  216. {    file_enum *pfen = (file_enum *)(*palloc)(1, sizeof(file_enum), "gp_enumerate_files");
  217.     char *pattern;
  218.     char *p;
  219.     int hsize = 0;
  220.     int i;
  221.     if ( pfen == 0 ) return 0;
  222.     pattern = (*palloc)(patlen + 1, 1,
  223.                 "gp_enumerate_files(pattern)");
  224.     if ( pattern == 0 ) return 0;
  225.     p = pattern;
  226.     for ( i = 0; i < patlen; i++ )
  227.        {    switch ( pat[i] )
  228.          {
  229.         case '*':
  230.            /* Skip to . or end of string so DOS can do it. */
  231.            *p++ = '*';
  232.            while ( i < patlen && pat[i] != '.' ) i++;
  233.            i--;
  234.            continue;
  235.         case '\\':
  236.            i++;
  237.            if ( pat[i] != '\\' && pat[i] != '/' && pat[i] != ':' )
  238.             break;
  239.            /* falls through */
  240.         case ':':
  241.         case '/':
  242.            hsize = p + 1 - pattern;
  243.          }
  244.         *p++ = pat[i];
  245.        }
  246.     *p = 0;
  247.     pfen->pattern = pattern;
  248.     pfen->patlen = patlen;
  249.     pfen->head_size = hsize;
  250.     pfen->mprocs.alloc = palloc;
  251.     pfen->mprocs.free = pfree;
  252.     pfen->first_time = 1;
  253.     return pfen;
  254. }
  255.  
  256. /* Enumerate the next file. */
  257. uint
  258. gp_enumerate_files_next(file_enum *pfen, char *ptr, uint maxlen)
  259. {    int code;
  260.     char *p, *q;
  261.     if ( pfen->first_time )
  262.        {    code = dos_findfirst(pfen->pattern, &pfen->ffblk);
  263.         pfen->first_time = 0;
  264.        }
  265.     else
  266.         code = dos_findnext(&pfen->ffblk);
  267.     if ( code != 0 )
  268.        {    /* All done, clean up. */
  269.         gp_enumerate_files_close(pfen);
  270.         return ~(uint)0;
  271.        }
  272.     if ( maxlen < 13 + pfen->head_size ) return maxlen + 1;    /* cop out! */
  273.     memcpy(ptr, pfen->pattern, pfen->head_size);
  274.     for ( p = &pfen->ffblk.ff_name[0], q = ptr + pfen->head_size; *p; p++ )
  275.       if ( *p != ' ' ) *q++ = *p;
  276.     return q - ptr;
  277. }
  278.  
  279. /* Clean up the file enumeration. */
  280. void
  281. gp_enumerate_files_close(file_enum *pfen)
  282. {    proc_free_t pfree = pfen->mprocs.free;
  283.     (*pfree)(pfen->pattern, pfen->patlen + 1, 1,
  284.          "gp_enumerate_files_close(pattern)");
  285.     (*pfree)((char *)pfen, 1, sizeof(file_enum), "gp_enumerate_files_close");
  286. }
  287.