home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / apps / dtp / ghost / gs301src / atari / gp_tosfs.c < prev    next >
C/C++ Source or Header  |  1994-09-11  |  12KB  |  426 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_tosfs.c */
  20.  
  21. #include "memory_.h"
  22. #include "string_.h"
  23. #include "gx.h"
  24. #include "gp.h"
  25. #include "gsstruct.h"
  26. #include "gsutil.h"    /* for string_match */
  27. #include "stat_.h"
  28. #include "dirent_.h"
  29. #include <sys/param.h>    /* for MAXPATHLEN */
  30.  
  31. /* Some systems (Interactive for example) don't define MAXPATHLEN,
  32.  * so we define it here.  (This probably should be done via a Config-Script.)
  33.  */
  34.  
  35. #ifndef MAXPATHLEN
  36. #  define MAXPATHLEN 1024
  37. #endif
  38.  
  39. /* Library routines not declared in a standard header */
  40. extern char *getenv(P1(const char *));
  41. extern char *mktemp(P1(char *));
  42.  
  43. /* ------ File names ------ */
  44.  
  45. /* Define the character used for separating file names in a list. */
  46. const char gp_file_name_list_separator = ',';
  47.  
  48. /* Define the default scratch file name prefix. */
  49. const char gp_scratch_file_name_prefix[] = "gs_pr.";
  50.  
  51. /* Define the name of the null output file. */
  52. const char gp_null_file_name[] = "null";
  53.  
  54. /* Define the name that designates the current directory. */
  55. const char gp_current_directory_name[] = ".";
  56.  
  57. /* Define the string to be concatenated with the file mode */
  58. /* for opening files without end-of-line conversion. */
  59. const char gp_fmode_binary_suffix[] = "b";
  60. const char gp_fmode_rb[] = "rb";
  61. const char gp_fmode_wb[] = "wb";
  62.  
  63. /* Create and open a scratch file with a given name prefix. */
  64. /* Write the actual file name at fname. */
  65.  
  66. FILE *
  67. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  68. {
  69.     strcpy(fname, prefix);
  70.  
  71.     /* Prevent trailing X's in path from being converted by mktemp. */
  72.     if ( *fname != 0 && fname[strlen(fname) - 1] == 'X' )
  73.         strcat(fname, "-");
  74.  
  75.     strcat(fname, "XXX");
  76.     mktemp(fname);
  77.     return fopen(fname, mode);
  78. }
  79.  
  80. /* Answer whether a file name contains a directory/device specification, */
  81. /* i.e. is absolute (not directory- or device-relative). */
  82. int
  83. gp_file_name_is_absolute(const char *fname, uint len)
  84. {    /* A file name is absolute if it contains a drive specification */
  85.     /* (second character is a :) or if it start with / or \. */
  86.     return ( len >= 1 && (*fname == '/' || *fname == '\\' ||
  87.         (len >= 2 && fname[1] == ':')) );
  88. }
  89.  
  90. /* Answer the string to be used for combining a directory/device prefix */
  91. /* with a base file name.  The file name is known to not be absolute. */
  92. const char *
  93. gp_file_name_concat_string(const char *prefix, uint plen,
  94.                const char *fname, uint len)
  95. {    if ( plen > 0 )
  96.       switch ( prefix[plen - 1] )
  97.        {    case ':': case '/': case '\\': return "";
  98.        };
  99.     return "\\";
  100. }
  101.  
  102.  
  103. /* ------ File enumeration ------ */
  104.  
  105. /* Thanks to Fritz Elfert (Fritz_Elfert@wue.maus.de) for */
  106. /* the original version of the following code, and Richard Mlynarik */
  107. /* (mly@adoc.xerox.com) for an improved version. */
  108.  
  109. typedef struct dirstack_s dirstack;
  110. struct dirstack_s {
  111.     dirstack *next;
  112.     DIR *entry;
  113. };
  114. gs_private_st_ptrs1(st_dirstack, dirstack, "dirstack",
  115.   dirstack_enum_ptrs, dirstack_reloc_ptrs, next);
  116.  
  117. struct file_enum_s {
  118.     DIR *dirp;        /* pointer to current open directory   */
  119.     char *pattern;        /* original pattern                    */
  120.     char *work;        /* current path                        */
  121.     int worklen;            /* strlen (work)               */
  122.     dirstack *dstack;    /* directory stack                     */
  123.     int patlen;
  124.     int pathead;            /* how much of pattern to consider
  125.                  *  when listing files in current directory */
  126.     bool first_time;
  127.     gs_memory_t *memory;
  128. };
  129. gs_private_st_ptrs3(st_file_enum, struct file_enum_s, "file_enum",
  130.   file_enum_enum_ptrs, file_enum_reloc_ptrs, pattern, work, dstack);
  131.  
  132. /* Private procedures */
  133.  
  134. /* Do a wild-card match. */
  135. #define wildmat(str,pat)\
  136.   string_match((const byte *)str, strlen(str),\
  137.            (const byte *)pat, strlen(pat), NULL)
  138.  
  139. /* Search a string backward for a character. */
  140. /* (This substitutes for strrchr, which some systems don't provide.) */
  141. private char *
  142. rchr(char *str, char ch, int len)
  143. {    register char *p = str + len;
  144.     while ( p > str )
  145.       if ( *--p == ch ) return p;
  146.     return 0;
  147. }
  148.  
  149. /* Push a directory onto the enumeration stack. */
  150. private void
  151. pushdir(file_enum *pfen)
  152. {    dirstack *d =
  153.         gs_alloc_struct(pfen->memory, dirstack,
  154.             &st_dirstack, "gp_enumerate_files(pushdir)");
  155.     if ( d != 0 )
  156.     {    d->next  = pfen->dstack;
  157.         d->entry = pfen->dirp;
  158.         pfen->dstack = d;
  159.     }
  160. }
  161.  
  162. /* Pop a directory from the enumeration stack. */
  163. private bool
  164. popdir(file_enum *pfen)
  165. {    dirstack *d = pfen->dstack;
  166.     if ( d == 0 )
  167.       return false;
  168.     pfen->dirp = d->entry;
  169.     pfen->dstack = d->next;
  170.     gs_free_object(pfen->memory, d, "gp_enumerate_files(popdir)");
  171.     return true;
  172. }
  173.  
  174. /* Initialize an enumeration. */
  175. file_enum *
  176. gp_enumerate_files_init(const char *pat, uint patlen, gs_memory_t *mem)
  177. {    file_enum *pfen;
  178.     char *p;
  179.     char *work;
  180.  
  181.     /* Reject attempts to enumerate paths longer than the */
  182.     /* system-dependent limit. */
  183.     if ( patlen > MAXPATHLEN )
  184.         return 0;
  185.  
  186.     /* Reject attempts to enumerate with a pattern containing zeroes. */
  187.     {    const char *p1;
  188.         for (p1 = pat; p1 < pat + patlen; p1++)
  189.             if (*p1 == 0) return 0;
  190.     }
  191.         /* >>> Should crunch strings of repeated "/"'s in pat to a single "/"
  192.          * >>>  to match stupid unix filesystem "conventions" */
  193.  
  194.     pfen = gs_alloc_struct(mem, file_enum, &st_file_enum,
  195.                    "gp_enumerate_files");
  196.     if (pfen == 0)
  197.         return 0;
  198.  
  199.     /* pattern and work could be allocated as strings, */
  200.     /* but it's simpler for GC and freeing to allocate them as bytes. */
  201.  
  202.     pfen->pattern =
  203.       (char *)gs_alloc_bytes(mem, patlen + 1,
  204.                  "gp_enumerate_files(pattern)");
  205.     if (pfen->pattern == 0)
  206.         return 0;
  207.     memcpy(pfen->pattern, pat, patlen);
  208.     pfen->pattern[patlen] = 0;
  209.  
  210.     work = (char *)gs_alloc_bytes(mem, MAXPATHLEN+1,
  211.                       "gp_enumerate_files(work)");
  212.     if (work == 0)
  213.         return 0;
  214.     pfen->work = work;
  215.  
  216.     p = work;
  217.     memcpy(p, pat, patlen);
  218.     p += patlen;
  219.     *p = 0;
  220.  
  221.     /* Remove directory specifications beyond the first wild card. */
  222.     /* Some systems don't have strpbrk, so we code it open. */
  223.     p = pfen->work;
  224.     while ( !(*p == '*' || *p == '?' || *p == 0) ) p++;
  225.     while ( !(*p == '/' || *p == 0) ) p++;
  226.     if ( *p == '/' )
  227.       *p = 0;
  228.         /* Substring for first wildcard match */
  229.         pfen->pathead = p - work;
  230.  
  231.     /* Select the next higher directory-level. */
  232.         p = rchr(work, '/', p - work);
  233.         if (!p)
  234.       {    /* No directory specification */
  235.         work[0] = 0;
  236.         pfen->worklen = 0;
  237.       }
  238.         else
  239.       {    if (p == work)
  240.           {    /* Root directory -- don't turn "/" into "" */
  241.             p++;
  242.           }
  243.         *p = 0;
  244.         pfen->worklen = p - work;
  245.       }
  246.  
  247.     pfen->memory = mem;
  248.     pfen->dstack = 0;
  249.     pfen->first_time = true;
  250.     pfen->patlen = patlen;
  251.     return pfen;
  252. }
  253.  
  254. /* Enumerate the next file. */
  255. uint
  256. gp_enumerate_files_next(file_enum *pfen, char *ptr, uint maxlen)
  257. {    dir_entry *de;
  258.     char *work = pfen->work;
  259.     int worklen = pfen->worklen;
  260.     char *pattern = pfen->pattern;
  261.     int pathead = pfen->pathead;
  262.     int len;
  263.     struct stat stbuf;
  264.  
  265.     if ( pfen->first_time )
  266.       {    pfen->dirp = ((worklen == 0) ? opendir(".") : opendir(work));
  267.         if_debug1('0', "[0]file_enum:First-Open '%s'\n", work);
  268.         pfen->first_time = false;
  269.         if (pfen->dirp == 0)    /* first opendir failed */
  270.           {    gp_enumerate_files_close(pfen);
  271.             return ~(uint)0;
  272.           }
  273.       }
  274.  
  275. top:    de = readdir(pfen->dirp);
  276.     if (de == 0)
  277.       {    /* No more entries in this directory */
  278.         char *p;
  279.  
  280.         if_debug0('0', "[0]file_enum:Closedir\n");
  281.         closedir(pfen->dirp);
  282.         /* Back working directory and matching pattern up one level */
  283.         p = rchr(work,'/', worklen);
  284.         if (p != 0)
  285.           {    if (p == work) p++;
  286.             *p = 0;
  287.             worklen = p - work;
  288.           }
  289.         p = rchr(pattern,'/', pathead);
  290.         if (p != 0)
  291.           pathead = p - pattern;
  292.         else
  293.           pathead = 0;
  294.  
  295.         if (popdir(pfen))
  296.           {    /* Back up the directory tree. */
  297.             if_debug1('0', "[0]file_enum:Dir popped '%s'\n", work);
  298.             goto top;
  299.           }
  300.         else
  301.           {    if_debug0('0', "file_enum:Dirstack empty\n");
  302.             gp_enumerate_files_close(pfen);
  303.             return ~(uint)0;
  304.           }
  305.       }
  306.  
  307.     /* Skip . and .. */
  308.     len = strlen(de->d_name);
  309.     if (len <= 2 && (!strcmp(de->d_name,".") || !strcmp(de->d_name,"..") ))
  310.       goto top;
  311.     if (len + worklen + 1 > MAXPATHLEN)
  312.       /* Should be an error, I suppose */
  313.       goto top;
  314.     if (worklen == 0)
  315.       {    /* "Current" directory (evil un*x kludge) */
  316.         memcpy(work, de->d_name, len + 1);
  317.       }
  318.     else if (worklen == 1 && work[0] == '/')
  319.       {    /* Root directory */
  320.         memcpy(work + 1, de->d_name, len + 1);
  321.         len = len + 1;
  322.       }
  323.     else
  324.       {    work[worklen] = '/';
  325.         memcpy(work + worklen + 1, de->d_name, len + 1);
  326.         len = worklen + 1 + len;
  327.       }
  328.  
  329.     /* Test for a match at this directory level */
  330.     if (!string_match((byte *)work, len, (byte *)pattern, pathead, NULL))
  331.       goto top;
  332.  
  333.     /* Perhaps descend into subdirectories */
  334.     if (pathead < pfen->patlen)
  335.       {    DIR *dp;
  336.  
  337.         if (((stat(work,&stbuf) >= 0)
  338.              ? !stat_is_dir(stbuf)
  339.              /* Couldn't stat it.
  340.               * Well, perhaps it's a directory and
  341.               * we'll be able to list it anyway.
  342.               * If it isn't or we can't, no harm done. */
  343.              : 0))
  344.           goto top;
  345.  
  346.         if (pfen->patlen == pathead + 1)
  347.           {    /* Listing "foo/?/" -- return this entry */
  348.             /* if it's a directory. */
  349.             if (!stat_is_dir (stbuf))
  350.               {    /* Do directoryp test the hard way */
  351.                 dp = opendir(work);
  352.                 if (!dp) goto top;
  353.                 closedir(dp);
  354.               }
  355.             work[len++] = '/';
  356.             goto winner;
  357.           }
  358.  
  359.         /* >>> Should optimise the case in which the next level */
  360.         /* >>> of directory has no wildcards. */
  361.         dp = opendir(work);
  362. #ifdef DEBUG
  363.         {    char save_end = pattern[pathead];
  364.             pattern[pathead] = 0;
  365.             if_debug2('0', "[0]file_enum:fname='%s', p='%s'\n",
  366.                   work, pattern);
  367.             pattern[pathead] = save_end;
  368.         }
  369. #endif /* DEBUG */
  370.         if (!dp)
  371.           /* Can't list this one */
  372.           goto top;
  373.         else
  374.           {    /* Advance to the next directory-delimiter */
  375.             /* in pattern */
  376.             char *p;
  377.             for (p = pattern + pathead + 1; ; p++)
  378.               {    if (*p == 0)
  379.                   {    /* No more subdirectories to match */
  380.                     pathead = pfen->patlen;
  381.                     break;
  382.                   }
  383.                 else if (*p == '/')
  384.                   {    pathead = p - pattern;
  385.                     break;
  386.                   }
  387.               }
  388.  
  389.             pushdir(pfen);
  390.             if_debug1('0', "file_enum:Dir pushed '%s'\n",
  391.                   work);
  392.             worklen = len;
  393.             pfen->dirp = dp;
  394.             goto top;
  395.           }
  396.       }
  397.  
  398. winner:
  399.     /* We have a winner! */
  400.     pfen->worklen = worklen;
  401.     pfen->pathead = pathead;
  402.     memcpy(ptr, work, len);
  403.     return len;
  404. }
  405.  
  406. /* Clean up the file enumeration. */
  407. void
  408. gp_enumerate_files_close(file_enum *pfen)
  409. {    gs_memory_t *mem = pfen->memory;
  410.  
  411.     if_debug0('0', "[0]file_enum:Cleanup\n");
  412.     while (popdir(pfen)) ;    /* clear directory stack */
  413.     gs_free_object(mem, (byte *)pfen->work,
  414.                "gp_enumerate_close(work)");
  415.     gs_free_object(mem, (byte *)pfen->pattern,
  416.                "gp_enumerate_files_close(pattern)");
  417.     gs_free_object(mem, pfen, "gp_enumerate_files_close");
  418. }
  419.  
  420. /* Test-cases:
  421.    (../?*r*?/?*.ps) {==} 100 string filenameforall
  422.    (../?*r*?/?*.ps*) {==} 100 string filenameforall
  423.    (../?*r*?/) {==} 100 string filenameforall
  424.    (/t*?/?*.ps) {==} 100 string filenameforall
  425. */
  426.