home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / patches / 7.1 / 7.1.304 < prev    next >
Encoding:
Internet Message Format  |  2008-05-28  |  11.9 KB

  1. To: vim-dev@vim.org
  2. Subject: Patch 7.1.304
  3. Fcc: outbox
  4. From: Bram Moolenaar <Bram@moolenaar.net>
  5. Mime-Version: 1.0
  6. Content-Type: text/plain; charset=ISO-8859-1
  7. Content-Transfer-Encoding: 8bit
  8. ------------
  9.  
  10. Patch 7.1.304
  11. Problem:    Shortpath_for_invalid_fname() does not work correctly and is
  12.         unnecessary complex.
  13. Solution:   Clean up shortpath_for_invalid_fname(). (mostly by Yegappan
  14.         Lakshmanan)
  15. Files:        src/eval.c
  16.  
  17.  
  18. *** ../vim-7.1.303/src/eval.c    Wed May 28 16:48:01 2008
  19. --- src/eval.c    Wed May 28 16:35:51 2008
  20. ***************
  21. *** 21068,21075 ****
  22.   static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
  23.   
  24.   /*
  25. !  * Get the short pathname of a file.
  26. !  * Returns 1 on success. *fnamelen is 0 for nonexistent path.
  27.    */
  28.       static int
  29.   get_short_pathname(fnamep, bufp, fnamelen)
  30. --- 21476,21487 ----
  31.   static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
  32.   
  33.   /*
  34. !  * Get the short path (8.3) for the filename in "fnamep".
  35. !  * Only works for a valid file name.
  36. !  * When the path gets longer "fnamep" is changed and the allocated buffer
  37. !  * is put in "bufp".
  38. !  * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
  39. !  * Returns OK on success, FAIL on failure.
  40.    */
  41.       static int
  42.   get_short_pathname(fnamep, bufp, fnamelen)
  43. ***************
  44. *** 21077,21112 ****
  45.       char_u    **bufp;
  46.       int        *fnamelen;
  47.   {
  48. !     int        l,len;
  49.       char_u    *newbuf;
  50.   
  51.       len = *fnamelen;
  52.       l = GetShortPathName(*fnamep, *fnamep, len);
  53.       if (l > len - 1)
  54.       {
  55.       /* If that doesn't work (not enough space), then save the string
  56. !      * and try again with a new buffer big enough
  57. !      */
  58.       newbuf = vim_strnsave(*fnamep, l);
  59.       if (newbuf == NULL)
  60. !         return 0;
  61.   
  62.       vim_free(*bufp);
  63.       *fnamep = *bufp = newbuf;
  64.   
  65. !     l = GetShortPathName(*fnamep,*fnamep,l+1);
  66. !     /* Really should always succeed, as the buffer is big enough */
  67.       }
  68.   
  69.       *fnamelen = l;
  70. !     return 1;
  71.   }
  72.   
  73.   /*
  74. !  * Create a short path name.  Returns the length of the buffer it needs.
  75. !  * Doesn't copy over the end of the buffer passed in.
  76.    */
  77.       static int
  78.   shortpath_for_invalid_fname(fname, bufp, fnamelen)
  79. --- 21489,21532 ----
  80.       char_u    **bufp;
  81.       int        *fnamelen;
  82.   {
  83. !     int        l, len;
  84.       char_u    *newbuf;
  85.   
  86.       len = *fnamelen;
  87.       l = GetShortPathName(*fnamep, *fnamep, len);
  88.       if (l > len - 1)
  89.       {
  90.       /* If that doesn't work (not enough space), then save the string
  91. !      * and try again with a new buffer big enough. */
  92.       newbuf = vim_strnsave(*fnamep, l);
  93.       if (newbuf == NULL)
  94. !         return FAIL;
  95.   
  96.       vim_free(*bufp);
  97.       *fnamep = *bufp = newbuf;
  98.   
  99. !     /* Really should always succeed, as the buffer is big enough. */
  100. !     l = GetShortPathName(*fnamep, *fnamep, l+1);
  101.       }
  102.   
  103.       *fnamelen = l;
  104. !     return OK;
  105.   }
  106.   
  107.   /*
  108. !  * Get the short path (8.3) for the filename in "fname". The converted
  109. !  * path is returned in "bufp".
  110. !  *
  111. !  * Some of the directories specified in "fname" may not exist. This function
  112. !  * will shorten the existing directories at the beginning of the path and then
  113. !  * append the remaining non-existing path.
  114. !  *
  115. !  * fname - Pointer to the filename to shorten.  On return, contains the
  116. !  *         pointer to the shortened pathname
  117. !  * bufp -  Pointer to an allocated buffer for the filename.
  118. !  * fnamelen - Length of the filename pointed to by fname
  119. !  *
  120. !  * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
  121.    */
  122.       static int
  123.   shortpath_for_invalid_fname(fname, bufp, fnamelen)
  124. ***************
  125. *** 21114,21198 ****
  126.       char_u    **bufp;
  127.       int        *fnamelen;
  128.   {
  129. !     char_u    *s, *p, *pbuf2, *pbuf3;
  130.       char_u    ch;
  131. !     int        len, len2, plen, slen;
  132.   
  133.       /* Make a copy */
  134. !     len2 = *fnamelen;
  135. !     pbuf2 = vim_strnsave(*fname, len2);
  136. !     pbuf3 = NULL;
  137. !     s = pbuf2 + len2 - 1; /* Find the end */
  138. !     slen = 1;
  139. !     plen = len2;
  140. !     if (after_pathsep(pbuf2, s + 1))
  141. !     {
  142. !     --s;
  143. !     ++slen;
  144. !     --plen;
  145. !     }
  146.   
  147. !     do
  148.       {
  149. !     /* Go back one path-separator */
  150. !     while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
  151. !     {
  152. !         --s;
  153. !         ++slen;
  154. !         --plen;
  155. !     }
  156. !     if (s <= pbuf2)
  157. !         break;
  158.   
  159. !     /* Remember the character that is about to be splatted */
  160. !     ch = *s;
  161. !     *s = 0; /* get_short_pathname requires a null-terminated string */
  162. !     /* Try it in situ */
  163. !     p = pbuf2;
  164. !     if (!get_short_pathname(&p, &pbuf3, &plen))
  165.       {
  166. !         vim_free(pbuf2);
  167. !         return -1;
  168.       }
  169. !     *s = ch;    /* Preserve the string */
  170. !     } while (plen == 0);
  171.   
  172. !     if (plen > 0)
  173.       {
  174. !     /* Remember the length of the new string.  */
  175. !     *fnamelen = len = plen + slen;
  176.       vim_free(*bufp);
  177. !     if (len > len2)
  178.       {
  179. !         /* If there's not enough space in the currently allocated string,
  180. !          * then copy it to a buffer big enough.
  181. !          */
  182. !         *fname= *bufp = vim_strnsave(p, len);
  183.           if (*fname == NULL)
  184. !         return -1;
  185.       }
  186.       else
  187.       {
  188. !         /* Transfer pbuf2 to being the main buffer  (it's big enough) */
  189. !         *fname = *bufp = pbuf2;
  190. !         if (p != pbuf2)
  191. !         strncpy(*fname, p, plen);
  192. !         pbuf2 = NULL;
  193. !     }
  194. !     /* Concat the next bit */
  195. !     strncpy(*fname + plen, s, slen);
  196. !     (*fname)[len] = '\0';
  197.       }
  198. !     vim_free(pbuf3);
  199. !     vim_free(pbuf2);
  200. !     return 0;
  201.   }
  202.   
  203.   /*
  204.    * Get a pathname for a partial path.
  205.    */
  206.       static int
  207.   shortpath_for_partial(fnamep, bufp, fnamelen)
  208. --- 21534,21639 ----
  209.       char_u    **bufp;
  210.       int        *fnamelen;
  211.   {
  212. !     char_u    *short_fname, *save_fname, *pbuf_unused;
  213. !     char_u    *endp, *save_endp;
  214.       char_u    ch;
  215. !     int        old_len, len;
  216. !     int        new_len, sfx_len;
  217. !     int        retval = OK;
  218.   
  219.       /* Make a copy */
  220. !     old_len = *fnamelen;
  221. !     save_fname = vim_strnsave(*fname, old_len);
  222. !     pbuf_unused = NULL;
  223. !     short_fname = NULL;
  224.   
  225. !     endp = save_fname + old_len - 1; /* Find the end of the copy */
  226. !     save_endp = endp;
  227. !     /*
  228. !      * Try shortening the supplied path till it succeeds by removing one
  229. !      * directory at a time from the tail of the path.
  230. !      */
  231. !     len = 0;
  232. !     for (;;)
  233.       {
  234. !     /* go back one path-separator */
  235. !     while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
  236. !         --endp;
  237. !     if (endp <= save_fname)
  238. !         break;        /* processed the complete path */
  239.   
  240. !     /*
  241. !      * Replace the path separator with a NUL and try to shorten the
  242. !      * resulting path.
  243. !      */
  244. !     ch = *endp;
  245. !     *endp = 0;
  246. !     short_fname = save_fname;
  247. !     len = STRLEN(short_fname) + 1;
  248. !     if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
  249.       {
  250. !         retval = FAIL;
  251. !         goto theend;
  252.       }
  253. !     *endp = ch;    /* preserve the string */
  254. !     if (len > 0)
  255. !         break;    /* successfully shortened the path */
  256.   
  257. !     /* failed to shorten the path. Skip the path separator */
  258. !     --endp;
  259. !     }
  260. !     if (len > 0)
  261.       {
  262. !     /*
  263. !      * Succeeded in shortening the path. Now concatenate the shortened
  264. !      * path with the remaining path at the tail.
  265. !      */
  266. !     /* Compute the length of the new path. */
  267. !     sfx_len = (int)(save_endp - endp) + 1;
  268. !     new_len = len + sfx_len;
  269. !     *fnamelen = new_len;
  270.       vim_free(*bufp);
  271. !     if (new_len > old_len)
  272.       {
  273. !         /* There is not enough space in the currently allocated string,
  274. !          * copy it to a buffer big enough. */
  275. !         *fname = *bufp = vim_strnsave(short_fname, new_len);
  276.           if (*fname == NULL)
  277. !         {
  278. !         retval = FAIL;
  279. !         goto theend;
  280. !         }
  281.       }
  282.       else
  283.       {
  284. !         /* Transfer short_fname to the main buffer (it's big enough),
  285. !          * unless get_short_pathname() did its work in-place. */
  286. !         *fname = *bufp = save_fname;
  287. !         if (short_fname != save_fname)
  288. !         vim_strncpy(save_fname, short_fname, len);
  289. !         save_fname = NULL;
  290. !     }
  291. !     /* concat the not-shortened part of the path */
  292. !     vim_strncpy(*fname + len, endp, sfx_len);
  293. !     (*fname)[new_len] = NUL;
  294.       }
  295. ! theend:
  296. !     vim_free(pbuf_unused);
  297. !     vim_free(save_fname);
  298. !     return retval;
  299.   }
  300.   
  301.   /*
  302.    * Get a pathname for a partial path.
  303. +  * Returns OK for success, FAIL for failure.
  304.    */
  305.       static int
  306.   shortpath_for_partial(fnamep, bufp, fnamelen)
  307. ***************
  308. *** 21222,21229 ****
  309.   
  310.       len = tflen = (int)STRLEN(tfname);
  311.   
  312. !     if (!get_short_pathname(&tfname, &pbuf, &len))
  313. !     return -1;
  314.   
  315.       if (len == 0)
  316.       {
  317. --- 21663,21670 ----
  318.   
  319.       len = tflen = (int)STRLEN(tfname);
  320.   
  321. !     if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
  322. !     return FAIL;
  323.   
  324.       if (len == 0)
  325.       {
  326. ***************
  327. *** 21232,21239 ****
  328.        * there's not a lot of point in guessing what it might be.
  329.        */
  330.       len = tflen;
  331. !     if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
  332. !         return -1;
  333.       }
  334.   
  335.       /* Count the paths backward to find the beginning of the desired string. */
  336. --- 21673,21680 ----
  337.        * there's not a lot of point in guessing what it might be.
  338.        */
  339.       len = tflen;
  340. !     if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
  341. !         return FAIL;
  342.       }
  343.   
  344.       /* Count the paths backward to find the beginning of the desired string. */
  345. ***************
  346. *** 21257,21263 ****
  347.       if (p >= tfname)
  348.           *p = '~';
  349.       else
  350. !         return -1;
  351.       }
  352.       else
  353.       ++p;
  354. --- 21698,21704 ----
  355.       if (p >= tfname)
  356.           *p = '~';
  357.       else
  358. !         return FAIL;
  359.       }
  360.       else
  361.       ++p;
  362. ***************
  363. *** 21268,21274 ****
  364.       *bufp = pbuf;
  365.       *fnamep = p;
  366.   
  367. !     return 0;
  368.   }
  369.   #endif /* WIN3264 */
  370.   
  371. --- 21709,21715 ----
  372.       *bufp = pbuf;
  373.       *fnamep = p;
  374.   
  375. !     return OK;
  376.   }
  377.   #endif /* WIN3264 */
  378.   
  379. ***************
  380. *** 21276,21282 ****
  381.    * Adjust a filename, according to a string of modifiers.
  382.    * *fnamep must be NUL terminated when called.  When returning, the length is
  383.    * determined by *fnamelen.
  384. !  * Returns valid flags.
  385.    * When there is an error, *fnamep is set to NULL.
  386.    */
  387.       int
  388. --- 21717,21723 ----
  389.    * Adjust a filename, according to a string of modifiers.
  390.    * *fnamep must be NUL terminated when called.  When returning, the length is
  391.    * determined by *fnamelen.
  392. !  * Returns VALID_ flags or -1 for failure.
  393.    * When there is an error, *fnamep is set to NULL.
  394.    */
  395.       int
  396. ***************
  397. *** 21488,21494 ****
  398.        */
  399.       if (!has_fullname && !vim_isAbsName(*fnamep))
  400.       {
  401. !         if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
  402.           return -1;
  403.       }
  404.       else
  405. --- 21929,21935 ----
  406.        */
  407.       if (!has_fullname && !vim_isAbsName(*fnamep))
  408.       {
  409. !         if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
  410.           return -1;
  411.       }
  412.       else
  413. ***************
  414. *** 21498,21504 ****
  415.           /* Simple case, already have the full-name
  416.            * Nearly always shorter, so try first time. */
  417.           l = *fnamelen;
  418. !         if (!get_short_pathname(fnamep, bufp, &l))
  419.           return -1;
  420.   
  421.           if (l == 0)
  422. --- 21939,21945 ----
  423.           /* Simple case, already have the full-name
  424.            * Nearly always shorter, so try first time. */
  425.           l = *fnamelen;
  426. !         if (get_short_pathname(fnamep, bufp, &l) == FAIL)
  427.           return -1;
  428.   
  429.           if (l == 0)
  430. ***************
  431. *** 21506,21512 ****
  432.           /* Couldn't find the filename.. search the paths.
  433.            */
  434.           l = *fnamelen;
  435. !         if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
  436.               return -1;
  437.           }
  438.           *fnamelen = l;
  439. --- 21947,21953 ----
  440.           /* Couldn't find the filename.. search the paths.
  441.            */
  442.           l = *fnamelen;
  443. !         if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
  444.               return -1;
  445.           }
  446.           *fnamelen = l;
  447. *** ../vim-7.1.303/src/version.c    Thu May 29 15:33:13 2008
  448. --- src/version.c    Thu May 29 21:41:55 2008
  449. ***************
  450. *** 668,669 ****
  451. --- 673,676 ----
  452.   {   /* Add new patch number below this line */
  453. + /**/
  454. +     304,
  455.   /**/
  456.  
  457. -- 
  458. "The future's already arrived - it's just not evenly distributed yet."
  459.         -- William Gibson
  460.  
  461.  /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
  462. ///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
  463. \\\        download, build and distribute -- http://www.A-A-P.org        ///
  464.  \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///
  465.