Mercurial > vim
comparison src/misc1.c @ 17978:8f4cc259ed7a v8.1.1985
patch 8.1.1985: code for dealing with paths is spread out
Commit: https://github.com/vim/vim/commit/26262f87770d3a1a68b09a70152d75c2e2ae186f
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Sep 4 20:59:15 2019 +0200
patch 8.1.1985: code for dealing with paths is spread out
Problem: Code for dealing with paths is spread out.
Solution: Move path related functions from misc1.c to filepath.c.
Remove NO_EXPANDPATH.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Wed, 04 Sep 2019 21:00:04 +0200 |
parents | 46f95606b9ec |
children | d1e77015f60b |
comparison
equal
deleted
inserted
replaced
17977:7fa0c4b6bfa5 | 17978:8f4cc259ed7a |
---|---|
15 #include "version.h" | 15 #include "version.h" |
16 | 16 |
17 #if defined(MSWIN) | 17 #if defined(MSWIN) |
18 # include <lm.h> | 18 # include <lm.h> |
19 #endif | 19 #endif |
20 | |
21 static char_u *vim_version_dir(char_u *vimdir); | |
22 static char_u *remove_tail(char_u *p, char_u *pend, char_u *name); | |
23 | 20 |
24 #define URL_SLASH 1 /* path_is_url() has found "://" */ | 21 #define URL_SLASH 1 /* path_is_url() has found "://" */ |
25 #define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */ | 22 #define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */ |
26 | 23 |
27 // All user names (for ~user completion as done by shell). | 24 // All user names (for ~user completion as done by shell). |
1591 * This also works with mounts and links. | 1588 * This also works with mounts and links. |
1592 * Don't do this for MS-DOS, it will change the "current dir" for a drive. | 1589 * Don't do this for MS-DOS, it will change the "current dir" for a drive. |
1593 * For Windows: | 1590 * For Windows: |
1594 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync! | 1591 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync! |
1595 */ | 1592 */ |
1596 static char_u *homedir = NULL; | |
1597 | |
1598 void | 1593 void |
1599 init_homedir(void) | 1594 init_homedir(void) |
1600 { | 1595 { |
1601 char_u *var; | 1596 char_u *var; |
1602 | 1597 |
2044 } | 2039 } |
2045 *dst = NUL; | 2040 *dst = NUL; |
2046 } | 2041 } |
2047 | 2042 |
2048 /* | 2043 /* |
2044 * If the string between "p" and "pend" ends in "name/", return "pend" minus | |
2045 * the length of "name/". Otherwise return "pend". | |
2046 */ | |
2047 static char_u * | |
2048 remove_tail(char_u *p, char_u *pend, char_u *name) | |
2049 { | |
2050 int len = (int)STRLEN(name) + 1; | |
2051 char_u *newend = pend - len; | |
2052 | |
2053 if (newend >= p | |
2054 && fnamencmp(newend, name, len - 1) == 0 | |
2055 && (newend == p || after_pathsep(p, newend))) | |
2056 return newend; | |
2057 return pend; | |
2058 } | |
2059 | |
2060 /* | |
2061 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. | |
2062 * Return NULL if not, return its name in allocated memory otherwise. | |
2063 */ | |
2064 static char_u * | |
2065 vim_version_dir(char_u *vimdir) | |
2066 { | |
2067 char_u *p; | |
2068 | |
2069 if (vimdir == NULL || *vimdir == NUL) | |
2070 return NULL; | |
2071 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); | |
2072 if (p != NULL && mch_isdir(p)) | |
2073 return p; | |
2074 vim_free(p); | |
2075 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); | |
2076 if (p != NULL && mch_isdir(p)) | |
2077 return p; | |
2078 vim_free(p); | |
2079 return NULL; | |
2080 } | |
2081 | |
2082 /* | |
2049 * Vim's version of getenv(). | 2083 * Vim's version of getenv(). |
2050 * Special handling of $HOME, $VIM and $VIMRUNTIME. | 2084 * Special handling of $HOME, $VIM and $VIMRUNTIME. |
2051 * Also does ACP to 'enc' conversion for Win32. | 2085 * Also does ACP to 'enc' conversion for Win32. |
2052 * "mustfree" is set to TRUE when returned is allocated, it must be | 2086 * "mustfree" is set to TRUE when returned is allocated, it must be |
2053 * initialized to FALSE by the caller. | 2087 * initialized to FALSE by the caller. |
2266 vim_setenv((char_u *)"VIM", p); | 2300 vim_setenv((char_u *)"VIM", p); |
2267 didset_vim = TRUE; | 2301 didset_vim = TRUE; |
2268 } | 2302 } |
2269 } | 2303 } |
2270 return p; | 2304 return p; |
2271 } | |
2272 | |
2273 /* | |
2274 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. | |
2275 * Return NULL if not, return its name in allocated memory otherwise. | |
2276 */ | |
2277 static char_u * | |
2278 vim_version_dir(char_u *vimdir) | |
2279 { | |
2280 char_u *p; | |
2281 | |
2282 if (vimdir == NULL || *vimdir == NUL) | |
2283 return NULL; | |
2284 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); | |
2285 if (p != NULL && mch_isdir(p)) | |
2286 return p; | |
2287 vim_free(p); | |
2288 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); | |
2289 if (p != NULL && mch_isdir(p)) | |
2290 return p; | |
2291 vim_free(p); | |
2292 return NULL; | |
2293 } | |
2294 | |
2295 /* | |
2296 * If the string between "p" and "pend" ends in "name/", return "pend" minus | |
2297 * the length of "name/". Otherwise return "pend". | |
2298 */ | |
2299 static char_u * | |
2300 remove_tail(char_u *p, char_u *pend, char_u *name) | |
2301 { | |
2302 int len = (int)STRLEN(name) + 1; | |
2303 char_u *newend = pend - len; | |
2304 | |
2305 if (newend >= p | |
2306 && fnamencmp(newend, name, len - 1) == 0 | |
2307 && (newend == p || after_pathsep(p, newend))) | |
2308 return newend; | |
2309 return pend; | |
2310 } | 2305 } |
2311 | 2306 |
2312 #if defined(FEAT_EVAL) || defined(PROTO) | 2307 #if defined(FEAT_EVAL) || defined(PROTO) |
2313 void | 2308 void |
2314 vim_unsetenv(char_u *var) | 2309 vim_unsetenv(char_u *var) |
2527 } | 2522 } |
2528 return result; | 2523 return result; |
2529 } | 2524 } |
2530 | 2525 |
2531 /* | 2526 /* |
2532 * Replace home directory by "~" in each space or comma separated file name in | |
2533 * 'src'. | |
2534 * If anything fails (except when out of space) dst equals src. | |
2535 */ | |
2536 void | |
2537 home_replace( | |
2538 buf_T *buf, /* when not NULL, check for help files */ | |
2539 char_u *src, /* input file name */ | |
2540 char_u *dst, /* where to put the result */ | |
2541 int dstlen, /* maximum length of the result */ | |
2542 int one) /* if TRUE, only replace one file name, include | |
2543 spaces and commas in the file name. */ | |
2544 { | |
2545 size_t dirlen = 0, envlen = 0; | |
2546 size_t len; | |
2547 char_u *homedir_env, *homedir_env_orig; | |
2548 char_u *p; | |
2549 | |
2550 if (src == NULL) | |
2551 { | |
2552 *dst = NUL; | |
2553 return; | |
2554 } | |
2555 | |
2556 /* | |
2557 * If the file is a help file, remove the path completely. | |
2558 */ | |
2559 if (buf != NULL && buf->b_help) | |
2560 { | |
2561 vim_snprintf((char *)dst, dstlen, "%s", gettail(src)); | |
2562 return; | |
2563 } | |
2564 | |
2565 /* | |
2566 * We check both the value of the $HOME environment variable and the | |
2567 * "real" home directory. | |
2568 */ | |
2569 if (homedir != NULL) | |
2570 dirlen = STRLEN(homedir); | |
2571 | |
2572 #ifdef VMS | |
2573 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); | |
2574 #else | |
2575 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME"); | |
2576 #endif | |
2577 #ifdef MSWIN | |
2578 if (homedir_env == NULL) | |
2579 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE"); | |
2580 #endif | |
2581 /* Empty is the same as not set. */ | |
2582 if (homedir_env != NULL && *homedir_env == NUL) | |
2583 homedir_env = NULL; | |
2584 | |
2585 if (homedir_env != NULL && *homedir_env == '~') | |
2586 { | |
2587 int usedlen = 0; | |
2588 int flen; | |
2589 char_u *fbuf = NULL; | |
2590 | |
2591 flen = (int)STRLEN(homedir_env); | |
2592 (void)modify_fname((char_u *)":p", FALSE, &usedlen, | |
2593 &homedir_env, &fbuf, &flen); | |
2594 flen = (int)STRLEN(homedir_env); | |
2595 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1])) | |
2596 /* Remove the trailing / that is added to a directory. */ | |
2597 homedir_env[flen - 1] = NUL; | |
2598 } | |
2599 | |
2600 if (homedir_env != NULL) | |
2601 envlen = STRLEN(homedir_env); | |
2602 | |
2603 if (!one) | |
2604 src = skipwhite(src); | |
2605 while (*src && dstlen > 0) | |
2606 { | |
2607 /* | |
2608 * Here we are at the beginning of a file name. | |
2609 * First, check to see if the beginning of the file name matches | |
2610 * $HOME or the "real" home directory. Check that there is a '/' | |
2611 * after the match (so that if e.g. the file is "/home/pieter/bla", | |
2612 * and the home directory is "/home/piet", the file does not end up | |
2613 * as "~er/bla" (which would seem to indicate the file "bla" in user | |
2614 * er's home directory)). | |
2615 */ | |
2616 p = homedir; | |
2617 len = dirlen; | |
2618 for (;;) | |
2619 { | |
2620 if ( len | |
2621 && fnamencmp(src, p, len) == 0 | |
2622 && (vim_ispathsep(src[len]) | |
2623 || (!one && (src[len] == ',' || src[len] == ' ')) | |
2624 || src[len] == NUL)) | |
2625 { | |
2626 src += len; | |
2627 if (--dstlen > 0) | |
2628 *dst++ = '~'; | |
2629 | |
2630 /* | |
2631 * If it's just the home directory, add "/". | |
2632 */ | |
2633 if (!vim_ispathsep(src[0]) && --dstlen > 0) | |
2634 *dst++ = '/'; | |
2635 break; | |
2636 } | |
2637 if (p == homedir_env) | |
2638 break; | |
2639 p = homedir_env; | |
2640 len = envlen; | |
2641 } | |
2642 | |
2643 /* if (!one) skip to separator: space or comma */ | |
2644 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) | |
2645 *dst++ = *src++; | |
2646 /* skip separator */ | |
2647 while ((*src == ' ' || *src == ',') && --dstlen > 0) | |
2648 *dst++ = *src++; | |
2649 } | |
2650 /* if (dstlen == 0) out of space, what to do??? */ | |
2651 | |
2652 *dst = NUL; | |
2653 | |
2654 if (homedir_env != homedir_env_orig) | |
2655 vim_free(homedir_env); | |
2656 } | |
2657 | |
2658 /* | |
2659 * Like home_replace, store the replaced string in allocated memory. | |
2660 * When something fails, NULL is returned. | |
2661 */ | |
2662 char_u * | |
2663 home_replace_save( | |
2664 buf_T *buf, /* when not NULL, check for help files */ | |
2665 char_u *src) /* input file name */ | |
2666 { | |
2667 char_u *dst; | |
2668 unsigned len; | |
2669 | |
2670 len = 3; /* space for "~/" and trailing NUL */ | |
2671 if (src != NULL) /* just in case */ | |
2672 len += (unsigned)STRLEN(src); | |
2673 dst = alloc(len); | |
2674 if (dst != NULL) | |
2675 home_replace(buf, src, dst, len, TRUE); | |
2676 return dst; | |
2677 } | |
2678 | |
2679 /* | |
2680 * Compare two file names and return: | |
2681 * FPC_SAME if they both exist and are the same file. | |
2682 * FPC_SAMEX if they both don't exist and have the same file name. | |
2683 * FPC_DIFF if they both exist and are different files. | |
2684 * FPC_NOTX if they both don't exist. | |
2685 * FPC_DIFFX if one of them doesn't exist. | |
2686 * For the first name environment variables are expanded if "expandenv" is | |
2687 * TRUE. | |
2688 */ | |
2689 int | |
2690 fullpathcmp( | |
2691 char_u *s1, | |
2692 char_u *s2, | |
2693 int checkname, // when both don't exist, check file names | |
2694 int expandenv) | |
2695 { | |
2696 #ifdef UNIX | |
2697 char_u exp1[MAXPATHL]; | |
2698 char_u full1[MAXPATHL]; | |
2699 char_u full2[MAXPATHL]; | |
2700 stat_T st1, st2; | |
2701 int r1, r2; | |
2702 | |
2703 if (expandenv) | |
2704 expand_env(s1, exp1, MAXPATHL); | |
2705 else | |
2706 vim_strncpy(exp1, s1, MAXPATHL - 1); | |
2707 r1 = mch_stat((char *)exp1, &st1); | |
2708 r2 = mch_stat((char *)s2, &st2); | |
2709 if (r1 != 0 && r2 != 0) | |
2710 { | |
2711 /* if mch_stat() doesn't work, may compare the names */ | |
2712 if (checkname) | |
2713 { | |
2714 if (fnamecmp(exp1, s2) == 0) | |
2715 return FPC_SAMEX; | |
2716 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); | |
2717 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); | |
2718 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) | |
2719 return FPC_SAMEX; | |
2720 } | |
2721 return FPC_NOTX; | |
2722 } | |
2723 if (r1 != 0 || r2 != 0) | |
2724 return FPC_DIFFX; | |
2725 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) | |
2726 return FPC_SAME; | |
2727 return FPC_DIFF; | |
2728 #else | |
2729 char_u *exp1; /* expanded s1 */ | |
2730 char_u *full1; /* full path of s1 */ | |
2731 char_u *full2; /* full path of s2 */ | |
2732 int retval = FPC_DIFF; | |
2733 int r1, r2; | |
2734 | |
2735 /* allocate one buffer to store three paths (alloc()/free() is slow!) */ | |
2736 if ((exp1 = alloc(MAXPATHL * 3)) != NULL) | |
2737 { | |
2738 full1 = exp1 + MAXPATHL; | |
2739 full2 = full1 + MAXPATHL; | |
2740 | |
2741 if (expandenv) | |
2742 expand_env(s1, exp1, MAXPATHL); | |
2743 else | |
2744 vim_strncpy(exp1, s1, MAXPATHL - 1); | |
2745 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); | |
2746 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); | |
2747 | |
2748 /* If vim_FullName() fails, the file probably doesn't exist. */ | |
2749 if (r1 != OK && r2 != OK) | |
2750 { | |
2751 if (checkname && fnamecmp(exp1, s2) == 0) | |
2752 retval = FPC_SAMEX; | |
2753 else | |
2754 retval = FPC_NOTX; | |
2755 } | |
2756 else if (r1 != OK || r2 != OK) | |
2757 retval = FPC_DIFFX; | |
2758 else if (fnamecmp(full1, full2)) | |
2759 retval = FPC_DIFF; | |
2760 else | |
2761 retval = FPC_SAME; | |
2762 vim_free(exp1); | |
2763 } | |
2764 return retval; | |
2765 #endif | |
2766 } | |
2767 | |
2768 /* | |
2769 * Get the tail of a path: the file name. | |
2770 * When the path ends in a path separator the tail is the NUL after it. | |
2771 * Fail safe: never returns NULL. | |
2772 */ | |
2773 char_u * | |
2774 gettail(char_u *fname) | |
2775 { | |
2776 char_u *p1, *p2; | |
2777 | |
2778 if (fname == NULL) | |
2779 return (char_u *)""; | |
2780 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */ | |
2781 { | |
2782 if (vim_ispathsep_nocolon(*p2)) | |
2783 p1 = p2 + 1; | |
2784 MB_PTR_ADV(p2); | |
2785 } | |
2786 return p1; | |
2787 } | |
2788 | |
2789 /* | |
2790 * Get pointer to tail of "fname", including path separators. Putting a NUL | |
2791 * here leaves the directory name. Takes care of "c:/" and "//". | |
2792 * Always returns a valid pointer. | |
2793 */ | |
2794 char_u * | |
2795 gettail_sep(char_u *fname) | |
2796 { | |
2797 char_u *p; | |
2798 char_u *t; | |
2799 | |
2800 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */ | |
2801 t = gettail(fname); | |
2802 while (t > p && after_pathsep(fname, t)) | |
2803 --t; | |
2804 #ifdef VMS | |
2805 /* path separator is part of the path */ | |
2806 ++t; | |
2807 #endif | |
2808 return t; | |
2809 } | |
2810 | |
2811 /* | |
2812 * get the next path component (just after the next path separator). | |
2813 */ | |
2814 char_u * | |
2815 getnextcomp(char_u *fname) | |
2816 { | |
2817 while (*fname && !vim_ispathsep(*fname)) | |
2818 MB_PTR_ADV(fname); | |
2819 if (*fname) | |
2820 ++fname; | |
2821 return fname; | |
2822 } | |
2823 | |
2824 /* | |
2825 * Get a pointer to one character past the head of a path name. | |
2826 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. | |
2827 * If there is no head, path is returned. | |
2828 */ | |
2829 char_u * | |
2830 get_past_head(char_u *path) | |
2831 { | |
2832 char_u *retval; | |
2833 | |
2834 #if defined(MSWIN) | |
2835 /* may skip "c:" */ | |
2836 if (isalpha(path[0]) && path[1] == ':') | |
2837 retval = path + 2; | |
2838 else | |
2839 retval = path; | |
2840 #else | |
2841 # if defined(AMIGA) | |
2842 /* may skip "label:" */ | |
2843 retval = vim_strchr(path, ':'); | |
2844 if (retval == NULL) | |
2845 retval = path; | |
2846 # else /* Unix */ | |
2847 retval = path; | |
2848 # endif | |
2849 #endif | |
2850 | |
2851 while (vim_ispathsep(*retval)) | |
2852 ++retval; | |
2853 | |
2854 return retval; | |
2855 } | |
2856 | |
2857 /* | |
2858 * Return TRUE if 'c' is a path separator. | |
2859 * Note that for MS-Windows this includes the colon. | |
2860 */ | |
2861 int | |
2862 vim_ispathsep(int c) | |
2863 { | |
2864 #ifdef UNIX | |
2865 return (c == '/'); /* UNIX has ':' inside file names */ | |
2866 #else | |
2867 # ifdef BACKSLASH_IN_FILENAME | |
2868 return (c == ':' || c == '/' || c == '\\'); | |
2869 # else | |
2870 # ifdef VMS | |
2871 /* server"user passwd"::device:[full.path.name]fname.extension;version" */ | |
2872 return (c == ':' || c == '[' || c == ']' || c == '/' | |
2873 || c == '<' || c == '>' || c == '"' ); | |
2874 # else | |
2875 return (c == ':' || c == '/'); | |
2876 # endif /* VMS */ | |
2877 # endif | |
2878 #endif | |
2879 } | |
2880 | |
2881 /* | |
2882 * Like vim_ispathsep(c), but exclude the colon for MS-Windows. | |
2883 */ | |
2884 int | |
2885 vim_ispathsep_nocolon(int c) | |
2886 { | |
2887 return vim_ispathsep(c) | |
2888 #ifdef BACKSLASH_IN_FILENAME | |
2889 && c != ':' | |
2890 #endif | |
2891 ; | |
2892 } | |
2893 | |
2894 /* | |
2895 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" | |
2896 * It's done in-place. | |
2897 */ | |
2898 void | |
2899 shorten_dir(char_u *str) | |
2900 { | |
2901 char_u *tail, *s, *d; | |
2902 int skip = FALSE; | |
2903 | |
2904 tail = gettail(str); | |
2905 d = str; | |
2906 for (s = str; ; ++s) | |
2907 { | |
2908 if (s >= tail) /* copy the whole tail */ | |
2909 { | |
2910 *d++ = *s; | |
2911 if (*s == NUL) | |
2912 break; | |
2913 } | |
2914 else if (vim_ispathsep(*s)) /* copy '/' and next char */ | |
2915 { | |
2916 *d++ = *s; | |
2917 skip = FALSE; | |
2918 } | |
2919 else if (!skip) | |
2920 { | |
2921 *d++ = *s; /* copy next char */ | |
2922 if (*s != '~' && *s != '.') /* and leading "~" and "." */ | |
2923 skip = TRUE; | |
2924 if (has_mbyte) | |
2925 { | |
2926 int l = mb_ptr2len(s); | |
2927 | |
2928 while (--l > 0) | |
2929 *d++ = *++s; | |
2930 } | |
2931 } | |
2932 } | |
2933 } | |
2934 | |
2935 /* | |
2936 * Return TRUE if the directory of "fname" exists, FALSE otherwise. | |
2937 * Also returns TRUE if there is no directory name. | |
2938 * "fname" must be writable!. | |
2939 */ | |
2940 int | |
2941 dir_of_file_exists(char_u *fname) | |
2942 { | |
2943 char_u *p; | |
2944 int c; | |
2945 int retval; | |
2946 | |
2947 p = gettail_sep(fname); | |
2948 if (p == fname) | |
2949 return TRUE; | |
2950 c = *p; | |
2951 *p = NUL; | |
2952 retval = mch_isdir(fname); | |
2953 *p = c; | |
2954 return retval; | |
2955 } | |
2956 | |
2957 /* | |
2958 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally | |
2959 * and deal with 'fileignorecase'. | |
2960 */ | |
2961 int | |
2962 vim_fnamecmp(char_u *x, char_u *y) | |
2963 { | |
2964 #ifdef BACKSLASH_IN_FILENAME | |
2965 return vim_fnamencmp(x, y, MAXPATHL); | |
2966 #else | |
2967 if (p_fic) | |
2968 return MB_STRICMP(x, y); | |
2969 return STRCMP(x, y); | |
2970 #endif | |
2971 } | |
2972 | |
2973 int | |
2974 vim_fnamencmp(char_u *x, char_u *y, size_t len) | |
2975 { | |
2976 #ifdef BACKSLASH_IN_FILENAME | |
2977 char_u *px = x; | |
2978 char_u *py = y; | |
2979 int cx = NUL; | |
2980 int cy = NUL; | |
2981 | |
2982 while (len > 0) | |
2983 { | |
2984 cx = PTR2CHAR(px); | |
2985 cy = PTR2CHAR(py); | |
2986 if (cx == NUL || cy == NUL | |
2987 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy) | |
2988 && !(cx == '/' && cy == '\\') | |
2989 && !(cx == '\\' && cy == '/'))) | |
2990 break; | |
2991 len -= MB_PTR2LEN(px); | |
2992 px += MB_PTR2LEN(px); | |
2993 py += MB_PTR2LEN(py); | |
2994 } | |
2995 if (len == 0) | |
2996 return 0; | |
2997 return (cx - cy); | |
2998 #else | |
2999 if (p_fic) | |
3000 return MB_STRNICMP(x, y, len); | |
3001 return STRNCMP(x, y, len); | |
3002 #endif | |
3003 } | |
3004 | |
3005 /* | |
3006 * Concatenate file names fname1 and fname2 into allocated memory. | |
3007 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary. | |
3008 */ | |
3009 char_u * | |
3010 concat_fnames(char_u *fname1, char_u *fname2, int sep) | |
3011 { | |
3012 char_u *dest; | |
3013 | |
3014 dest = alloc(STRLEN(fname1) + STRLEN(fname2) + 3); | |
3015 if (dest != NULL) | |
3016 { | |
3017 STRCPY(dest, fname1); | |
3018 if (sep) | |
3019 add_pathsep(dest); | |
3020 STRCAT(dest, fname2); | |
3021 } | |
3022 return dest; | |
3023 } | |
3024 | |
3025 /* | |
3026 * Concatenate two strings and return the result in allocated memory. | 2527 * Concatenate two strings and return the result in allocated memory. |
3027 * Returns NULL when out of memory. | 2528 * Returns NULL when out of memory. |
3028 */ | 2529 */ |
3029 char_u * | 2530 char_u * |
3030 concat_str(char_u *str1, char_u *str2) | 2531 concat_str(char_u *str1, char_u *str2) |
3037 { | 2538 { |
3038 STRCPY(dest, str1); | 2539 STRCPY(dest, str1); |
3039 STRCPY(dest + l, str2); | 2540 STRCPY(dest + l, str2); |
3040 } | 2541 } |
3041 return dest; | 2542 return dest; |
3042 } | |
3043 | |
3044 /* | |
3045 * Add a path separator to a file name, unless it already ends in a path | |
3046 * separator. | |
3047 */ | |
3048 void | |
3049 add_pathsep(char_u *p) | |
3050 { | |
3051 if (*p != NUL && !after_pathsep(p, p + STRLEN(p))) | |
3052 STRCAT(p, PATHSEPSTR); | |
3053 } | |
3054 | |
3055 /* | |
3056 * FullName_save - Make an allocated copy of a full file name. | |
3057 * Returns NULL when out of memory. | |
3058 */ | |
3059 char_u * | |
3060 FullName_save( | |
3061 char_u *fname, | |
3062 int force) /* force expansion, even when it already looks | |
3063 * like a full path name */ | |
3064 { | |
3065 char_u *buf; | |
3066 char_u *new_fname = NULL; | |
3067 | |
3068 if (fname == NULL) | |
3069 return NULL; | |
3070 | |
3071 buf = alloc(MAXPATHL); | |
3072 if (buf != NULL) | |
3073 { | |
3074 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) | |
3075 new_fname = vim_strsave(buf); | |
3076 else | |
3077 new_fname = vim_strsave(fname); | |
3078 vim_free(buf); | |
3079 } | |
3080 return new_fname; | |
3081 } | 2543 } |
3082 | 2544 |
3083 static void | 2545 static void |
3084 prepare_to_exit(void) | 2546 prepare_to_exit(void) |
3085 { | 2547 { |
3152 | 2614 |
3153 getout(1); | 2615 getout(1); |
3154 } | 2616 } |
3155 | 2617 |
3156 /* | 2618 /* |
3157 * return TRUE if "fname" exists. | |
3158 */ | |
3159 int | |
3160 vim_fexists(char_u *fname) | |
3161 { | |
3162 stat_T st; | |
3163 | |
3164 if (mch_stat((char *)fname, &st)) | |
3165 return FALSE; | |
3166 return TRUE; | |
3167 } | |
3168 | |
3169 /* | |
3170 * Check for CTRL-C pressed, but only once in a while. | 2619 * Check for CTRL-C pressed, but only once in a while. |
3171 * Should be used instead of ui_breakcheck() for functions that check for | 2620 * Should be used instead of ui_breakcheck() for functions that check for |
3172 * each line in the file. Calling ui_breakcheck() each time takes too much | 2621 * each line in the file. Calling ui_breakcheck() each time takes too much |
3173 * time, because it can be a system call. | 2622 * time, because it can be a system call. |
3174 */ | 2623 */ |
3199 { | 2648 { |
3200 breakcheck_count = 0; | 2649 breakcheck_count = 0; |
3201 ui_breakcheck(); | 2650 ui_breakcheck(); |
3202 } | 2651 } |
3203 } | 2652 } |
3204 | |
3205 /* | |
3206 * Invoke expand_wildcards() for one pattern. | |
3207 * Expand items like "%:h" before the expansion. | |
3208 * Returns OK or FAIL. | |
3209 */ | |
3210 int | |
3211 expand_wildcards_eval( | |
3212 char_u **pat, /* pointer to input pattern */ | |
3213 int *num_file, /* resulting number of files */ | |
3214 char_u ***file, /* array of resulting files */ | |
3215 int flags) /* EW_DIR, etc. */ | |
3216 { | |
3217 int ret = FAIL; | |
3218 char_u *eval_pat = NULL; | |
3219 char_u *exp_pat = *pat; | |
3220 char *ignored_msg; | |
3221 int usedlen; | |
3222 | |
3223 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<') | |
3224 { | |
3225 ++emsg_off; | |
3226 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen, | |
3227 NULL, &ignored_msg, NULL); | |
3228 --emsg_off; | |
3229 if (eval_pat != NULL) | |
3230 exp_pat = concat_str(eval_pat, exp_pat + usedlen); | |
3231 } | |
3232 | |
3233 if (exp_pat != NULL) | |
3234 ret = expand_wildcards(1, &exp_pat, num_file, file, flags); | |
3235 | |
3236 if (eval_pat != NULL) | |
3237 { | |
3238 vim_free(exp_pat); | |
3239 vim_free(eval_pat); | |
3240 } | |
3241 | |
3242 return ret; | |
3243 } | |
3244 | |
3245 /* | |
3246 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching | |
3247 * 'wildignore'. | |
3248 * Returns OK or FAIL. When FAIL then "num_files" won't be set. | |
3249 */ | |
3250 int | |
3251 expand_wildcards( | |
3252 int num_pat, /* number of input patterns */ | |
3253 char_u **pat, /* array of input patterns */ | |
3254 int *num_files, /* resulting number of files */ | |
3255 char_u ***files, /* array of resulting files */ | |
3256 int flags) /* EW_DIR, etc. */ | |
3257 { | |
3258 int retval; | |
3259 int i, j; | |
3260 char_u *p; | |
3261 int non_suf_match; /* number without matching suffix */ | |
3262 | |
3263 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags); | |
3264 | |
3265 /* When keeping all matches, return here */ | |
3266 if ((flags & EW_KEEPALL) || retval == FAIL) | |
3267 return retval; | |
3268 | |
3269 #ifdef FEAT_WILDIGN | |
3270 /* | |
3271 * Remove names that match 'wildignore'. | |
3272 */ | |
3273 if (*p_wig) | |
3274 { | |
3275 char_u *ffname; | |
3276 | |
3277 /* check all files in (*files)[] */ | |
3278 for (i = 0; i < *num_files; ++i) | |
3279 { | |
3280 ffname = FullName_save((*files)[i], FALSE); | |
3281 if (ffname == NULL) /* out of memory */ | |
3282 break; | |
3283 # ifdef VMS | |
3284 vms_remove_version(ffname); | |
3285 # endif | |
3286 if (match_file_list(p_wig, (*files)[i], ffname)) | |
3287 { | |
3288 /* remove this matching file from the list */ | |
3289 vim_free((*files)[i]); | |
3290 for (j = i; j + 1 < *num_files; ++j) | |
3291 (*files)[j] = (*files)[j + 1]; | |
3292 --*num_files; | |
3293 --i; | |
3294 } | |
3295 vim_free(ffname); | |
3296 } | |
3297 | |
3298 /* If the number of matches is now zero, we fail. */ | |
3299 if (*num_files == 0) | |
3300 { | |
3301 VIM_CLEAR(*files); | |
3302 return FAIL; | |
3303 } | |
3304 } | |
3305 #endif | |
3306 | |
3307 /* | |
3308 * Move the names where 'suffixes' match to the end. | |
3309 */ | |
3310 if (*num_files > 1) | |
3311 { | |
3312 non_suf_match = 0; | |
3313 for (i = 0; i < *num_files; ++i) | |
3314 { | |
3315 if (!match_suffix((*files)[i])) | |
3316 { | |
3317 /* | |
3318 * Move the name without matching suffix to the front | |
3319 * of the list. | |
3320 */ | |
3321 p = (*files)[i]; | |
3322 for (j = i; j > non_suf_match; --j) | |
3323 (*files)[j] = (*files)[j - 1]; | |
3324 (*files)[non_suf_match++] = p; | |
3325 } | |
3326 } | |
3327 } | |
3328 | |
3329 return retval; | |
3330 } | |
3331 | |
3332 /* | |
3333 * Return TRUE if "fname" matches with an entry in 'suffixes'. | |
3334 */ | |
3335 int | |
3336 match_suffix(char_u *fname) | |
3337 { | |
3338 int fnamelen, setsuflen; | |
3339 char_u *setsuf; | |
3340 #define MAXSUFLEN 30 /* maximum length of a file suffix */ | |
3341 char_u suf_buf[MAXSUFLEN]; | |
3342 | |
3343 fnamelen = (int)STRLEN(fname); | |
3344 setsuflen = 0; | |
3345 for (setsuf = p_su; *setsuf; ) | |
3346 { | |
3347 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); | |
3348 if (setsuflen == 0) | |
3349 { | |
3350 char_u *tail = gettail(fname); | |
3351 | |
3352 /* empty entry: match name without a '.' */ | |
3353 if (vim_strchr(tail, '.') == NULL) | |
3354 { | |
3355 setsuflen = 1; | |
3356 break; | |
3357 } | |
3358 } | |
3359 else | |
3360 { | |
3361 if (fnamelen >= setsuflen | |
3362 && fnamencmp(suf_buf, fname + fnamelen - setsuflen, | |
3363 (size_t)setsuflen) == 0) | |
3364 break; | |
3365 setsuflen = 0; | |
3366 } | |
3367 } | |
3368 return (setsuflen != 0); | |
3369 } | |
3370 | |
3371 #if !defined(NO_EXPANDPATH) || defined(PROTO) | |
3372 | |
3373 # ifdef VIM_BACKTICK | |
3374 static int vim_backtick(char_u *p); | |
3375 static int expand_backtick(garray_T *gap, char_u *pat, int flags); | |
3376 # endif | |
3377 | |
3378 # if defined(MSWIN) | |
3379 /* | |
3380 * File name expansion code for MS-DOS, Win16 and Win32. It's here because | |
3381 * it's shared between these systems. | |
3382 */ | |
3383 | |
3384 /* | |
3385 * comparison function for qsort in dos_expandpath() | |
3386 */ | |
3387 static int | |
3388 pstrcmp(const void *a, const void *b) | |
3389 { | |
3390 return (pathcmp(*(char **)a, *(char **)b, -1)); | |
3391 } | |
3392 | |
3393 /* | |
3394 * Recursively expand one path component into all matching files and/or | |
3395 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. | |
3396 * Return the number of matches found. | |
3397 * "path" has backslashes before chars that are not to be expanded, starting | |
3398 * at "path[wildoff]". | |
3399 * Return the number of matches found. | |
3400 * NOTE: much of this is identical to unix_expandpath(), keep in sync! | |
3401 */ | |
3402 static int | |
3403 dos_expandpath( | |
3404 garray_T *gap, | |
3405 char_u *path, | |
3406 int wildoff, | |
3407 int flags, /* EW_* flags */ | |
3408 int didstar) /* expanded "**" once already */ | |
3409 { | |
3410 char_u *buf; | |
3411 char_u *path_end; | |
3412 char_u *p, *s, *e; | |
3413 int start_len = gap->ga_len; | |
3414 char_u *pat; | |
3415 regmatch_T regmatch; | |
3416 int starts_with_dot; | |
3417 int matches; | |
3418 int len; | |
3419 int starstar = FALSE; | |
3420 static int stardepth = 0; // depth for "**" expansion | |
3421 HANDLE hFind = INVALID_HANDLE_VALUE; | |
3422 WIN32_FIND_DATAW wfb; | |
3423 WCHAR *wn = NULL; // UCS-2 name, NULL when not used. | |
3424 char_u *matchname; | |
3425 int ok; | |
3426 | |
3427 /* Expanding "**" may take a long time, check for CTRL-C. */ | |
3428 if (stardepth > 0) | |
3429 { | |
3430 ui_breakcheck(); | |
3431 if (got_int) | |
3432 return 0; | |
3433 } | |
3434 | |
3435 // Make room for file name. When doing encoding conversion the actual | |
3436 // length may be quite a bit longer, thus use the maximum possible length. | |
3437 buf = alloc(MAXPATHL); | |
3438 if (buf == NULL) | |
3439 return 0; | |
3440 | |
3441 /* | |
3442 * Find the first part in the path name that contains a wildcard or a ~1. | |
3443 * Copy it into buf, including the preceding characters. | |
3444 */ | |
3445 p = buf; | |
3446 s = buf; | |
3447 e = NULL; | |
3448 path_end = path; | |
3449 while (*path_end != NUL) | |
3450 { | |
3451 /* May ignore a wildcard that has a backslash before it; it will | |
3452 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ | |
3453 if (path_end >= path + wildoff && rem_backslash(path_end)) | |
3454 *p++ = *path_end++; | |
3455 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') | |
3456 { | |
3457 if (e != NULL) | |
3458 break; | |
3459 s = p + 1; | |
3460 } | |
3461 else if (path_end >= path + wildoff | |
3462 && vim_strchr((char_u *)"*?[~", *path_end) != NULL) | |
3463 e = p; | |
3464 if (has_mbyte) | |
3465 { | |
3466 len = (*mb_ptr2len)(path_end); | |
3467 STRNCPY(p, path_end, len); | |
3468 p += len; | |
3469 path_end += len; | |
3470 } | |
3471 else | |
3472 *p++ = *path_end++; | |
3473 } | |
3474 e = p; | |
3475 *e = NUL; | |
3476 | |
3477 /* now we have one wildcard component between s and e */ | |
3478 /* Remove backslashes between "wildoff" and the start of the wildcard | |
3479 * component. */ | |
3480 for (p = buf + wildoff; p < s; ++p) | |
3481 if (rem_backslash(p)) | |
3482 { | |
3483 STRMOVE(p, p + 1); | |
3484 --e; | |
3485 --s; | |
3486 } | |
3487 | |
3488 /* Check for "**" between "s" and "e". */ | |
3489 for (p = s; p < e; ++p) | |
3490 if (p[0] == '*' && p[1] == '*') | |
3491 starstar = TRUE; | |
3492 | |
3493 starts_with_dot = *s == '.'; | |
3494 pat = file_pat_to_reg_pat(s, e, NULL, FALSE); | |
3495 if (pat == NULL) | |
3496 { | |
3497 vim_free(buf); | |
3498 return 0; | |
3499 } | |
3500 | |
3501 /* compile the regexp into a program */ | |
3502 if (flags & (EW_NOERROR | EW_NOTWILD)) | |
3503 ++emsg_silent; | |
3504 regmatch.rm_ic = TRUE; /* Always ignore case */ | |
3505 regmatch.regprog = vim_regcomp(pat, RE_MAGIC); | |
3506 if (flags & (EW_NOERROR | EW_NOTWILD)) | |
3507 --emsg_silent; | |
3508 vim_free(pat); | |
3509 | |
3510 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) | |
3511 { | |
3512 vim_free(buf); | |
3513 return 0; | |
3514 } | |
3515 | |
3516 /* remember the pattern or file name being looked for */ | |
3517 matchname = vim_strsave(s); | |
3518 | |
3519 /* If "**" is by itself, this is the first time we encounter it and more | |
3520 * is following then find matches without any directory. */ | |
3521 if (!didstar && stardepth < 100 && starstar && e - s == 2 | |
3522 && *path_end == '/') | |
3523 { | |
3524 STRCPY(s, path_end + 1); | |
3525 ++stardepth; | |
3526 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE); | |
3527 --stardepth; | |
3528 } | |
3529 | |
3530 /* Scan all files in the directory with "dir/ *.*" */ | |
3531 STRCPY(s, "*.*"); | |
3532 wn = enc_to_utf16(buf, NULL); | |
3533 if (wn != NULL) | |
3534 hFind = FindFirstFileW(wn, &wfb); | |
3535 ok = (hFind != INVALID_HANDLE_VALUE); | |
3536 | |
3537 while (ok) | |
3538 { | |
3539 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here | |
3540 if (p == NULL) | |
3541 break; // out of memory | |
3542 | |
3543 // Ignore entries starting with a dot, unless when asked for. Accept | |
3544 // all entries found with "matchname". | |
3545 if ((p[0] != '.' || starts_with_dot | |
3546 || ((flags & EW_DODOT) | |
3547 && p[1] != NUL && (p[1] != '.' || p[2] != NUL))) | |
3548 && (matchname == NULL | |
3549 || (regmatch.regprog != NULL | |
3550 && vim_regexec(®match, p, (colnr_T)0)) | |
3551 || ((flags & EW_NOTWILD) | |
3552 && fnamencmp(path + (s - buf), p, e - s) == 0))) | |
3553 { | |
3554 STRCPY(s, p); | |
3555 len = (int)STRLEN(buf); | |
3556 | |
3557 if (starstar && stardepth < 100) | |
3558 { | |
3559 /* For "**" in the pattern first go deeper in the tree to | |
3560 * find matches. */ | |
3561 STRCPY(buf + len, "/**"); | |
3562 STRCPY(buf + len + 3, path_end); | |
3563 ++stardepth; | |
3564 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE); | |
3565 --stardepth; | |
3566 } | |
3567 | |
3568 STRCPY(buf + len, path_end); | |
3569 if (mch_has_exp_wildcard(path_end)) | |
3570 { | |
3571 /* need to expand another component of the path */ | |
3572 /* remove backslashes for the remaining components only */ | |
3573 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE); | |
3574 } | |
3575 else | |
3576 { | |
3577 /* no more wildcards, check if there is a match */ | |
3578 /* remove backslashes for the remaining components only */ | |
3579 if (*path_end != 0) | |
3580 backslash_halve(buf + len + 1); | |
3581 if (mch_getperm(buf) >= 0) /* add existing file */ | |
3582 addfile(gap, buf, flags); | |
3583 } | |
3584 } | |
3585 | |
3586 vim_free(p); | |
3587 ok = FindNextFileW(hFind, &wfb); | |
3588 | |
3589 /* If no more matches and no match was used, try expanding the name | |
3590 * itself. Finds the long name of a short filename. */ | |
3591 if (!ok && matchname != NULL && gap->ga_len == start_len) | |
3592 { | |
3593 STRCPY(s, matchname); | |
3594 FindClose(hFind); | |
3595 vim_free(wn); | |
3596 wn = enc_to_utf16(buf, NULL); | |
3597 if (wn != NULL) | |
3598 hFind = FindFirstFileW(wn, &wfb); | |
3599 else | |
3600 hFind = INVALID_HANDLE_VALUE; | |
3601 ok = (hFind != INVALID_HANDLE_VALUE); | |
3602 VIM_CLEAR(matchname); | |
3603 } | |
3604 } | |
3605 | |
3606 FindClose(hFind); | |
3607 vim_free(wn); | |
3608 vim_free(buf); | |
3609 vim_regfree(regmatch.regprog); | |
3610 vim_free(matchname); | |
3611 | |
3612 matches = gap->ga_len - start_len; | |
3613 if (matches > 0) | |
3614 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, | |
3615 sizeof(char_u *), pstrcmp); | |
3616 return matches; | |
3617 } | |
3618 | |
3619 int | |
3620 mch_expandpath( | |
3621 garray_T *gap, | |
3622 char_u *path, | |
3623 int flags) /* EW_* flags */ | |
3624 { | |
3625 return dos_expandpath(gap, path, 0, flags, FALSE); | |
3626 } | |
3627 # endif // MSWIN | |
3628 | |
3629 #if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \ | |
3630 || defined(PROTO) | |
3631 /* | |
3632 * Unix style wildcard expansion code. | |
3633 * It's here because it's used both for Unix and Mac. | |
3634 */ | |
3635 static int | |
3636 pstrcmp(const void *a, const void *b) | |
3637 { | |
3638 return (pathcmp(*(char **)a, *(char **)b, -1)); | |
3639 } | |
3640 | |
3641 /* | |
3642 * Recursively expand one path component into all matching files and/or | |
3643 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. | |
3644 * "path" has backslashes before chars that are not to be expanded, starting | |
3645 * at "path + wildoff". | |
3646 * Return the number of matches found. | |
3647 * NOTE: much of this is identical to dos_expandpath(), keep in sync! | |
3648 */ | |
3649 int | |
3650 unix_expandpath( | |
3651 garray_T *gap, | |
3652 char_u *path, | |
3653 int wildoff, | |
3654 int flags, /* EW_* flags */ | |
3655 int didstar) /* expanded "**" once already */ | |
3656 { | |
3657 char_u *buf; | |
3658 char_u *path_end; | |
3659 char_u *p, *s, *e; | |
3660 int start_len = gap->ga_len; | |
3661 char_u *pat; | |
3662 regmatch_T regmatch; | |
3663 int starts_with_dot; | |
3664 int matches; | |
3665 int len; | |
3666 int starstar = FALSE; | |
3667 static int stardepth = 0; /* depth for "**" expansion */ | |
3668 | |
3669 DIR *dirp; | |
3670 struct dirent *dp; | |
3671 | |
3672 /* Expanding "**" may take a long time, check for CTRL-C. */ | |
3673 if (stardepth > 0) | |
3674 { | |
3675 ui_breakcheck(); | |
3676 if (got_int) | |
3677 return 0; | |
3678 } | |
3679 | |
3680 /* make room for file name */ | |
3681 buf = alloc(STRLEN(path) + BASENAMELEN + 5); | |
3682 if (buf == NULL) | |
3683 return 0; | |
3684 | |
3685 /* | |
3686 * Find the first part in the path name that contains a wildcard. | |
3687 * When EW_ICASE is set every letter is considered to be a wildcard. | |
3688 * Copy it into "buf", including the preceding characters. | |
3689 */ | |
3690 p = buf; | |
3691 s = buf; | |
3692 e = NULL; | |
3693 path_end = path; | |
3694 while (*path_end != NUL) | |
3695 { | |
3696 /* May ignore a wildcard that has a backslash before it; it will | |
3697 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ | |
3698 if (path_end >= path + wildoff && rem_backslash(path_end)) | |
3699 *p++ = *path_end++; | |
3700 else if (*path_end == '/') | |
3701 { | |
3702 if (e != NULL) | |
3703 break; | |
3704 s = p + 1; | |
3705 } | |
3706 else if (path_end >= path + wildoff | |
3707 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL | |
3708 || (!p_fic && (flags & EW_ICASE) | |
3709 && isalpha(PTR2CHAR(path_end))))) | |
3710 e = p; | |
3711 if (has_mbyte) | |
3712 { | |
3713 len = (*mb_ptr2len)(path_end); | |
3714 STRNCPY(p, path_end, len); | |
3715 p += len; | |
3716 path_end += len; | |
3717 } | |
3718 else | |
3719 *p++ = *path_end++; | |
3720 } | |
3721 e = p; | |
3722 *e = NUL; | |
3723 | |
3724 /* Now we have one wildcard component between "s" and "e". */ | |
3725 /* Remove backslashes between "wildoff" and the start of the wildcard | |
3726 * component. */ | |
3727 for (p = buf + wildoff; p < s; ++p) | |
3728 if (rem_backslash(p)) | |
3729 { | |
3730 STRMOVE(p, p + 1); | |
3731 --e; | |
3732 --s; | |
3733 } | |
3734 | |
3735 /* Check for "**" between "s" and "e". */ | |
3736 for (p = s; p < e; ++p) | |
3737 if (p[0] == '*' && p[1] == '*') | |
3738 starstar = TRUE; | |
3739 | |
3740 /* convert the file pattern to a regexp pattern */ | |
3741 starts_with_dot = *s == '.'; | |
3742 pat = file_pat_to_reg_pat(s, e, NULL, FALSE); | |
3743 if (pat == NULL) | |
3744 { | |
3745 vim_free(buf); | |
3746 return 0; | |
3747 } | |
3748 | |
3749 /* compile the regexp into a program */ | |
3750 if (flags & EW_ICASE) | |
3751 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */ | |
3752 else | |
3753 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */ | |
3754 if (flags & (EW_NOERROR | EW_NOTWILD)) | |
3755 ++emsg_silent; | |
3756 regmatch.regprog = vim_regcomp(pat, RE_MAGIC); | |
3757 if (flags & (EW_NOERROR | EW_NOTWILD)) | |
3758 --emsg_silent; | |
3759 vim_free(pat); | |
3760 | |
3761 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) | |
3762 { | |
3763 vim_free(buf); | |
3764 return 0; | |
3765 } | |
3766 | |
3767 /* If "**" is by itself, this is the first time we encounter it and more | |
3768 * is following then find matches without any directory. */ | |
3769 if (!didstar && stardepth < 100 && starstar && e - s == 2 | |
3770 && *path_end == '/') | |
3771 { | |
3772 STRCPY(s, path_end + 1); | |
3773 ++stardepth; | |
3774 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE); | |
3775 --stardepth; | |
3776 } | |
3777 | |
3778 /* open the directory for scanning */ | |
3779 *s = NUL; | |
3780 dirp = opendir(*buf == NUL ? "." : (char *)buf); | |
3781 | |
3782 /* Find all matching entries */ | |
3783 if (dirp != NULL) | |
3784 { | |
3785 for (;;) | |
3786 { | |
3787 dp = readdir(dirp); | |
3788 if (dp == NULL) | |
3789 break; | |
3790 if ((dp->d_name[0] != '.' || starts_with_dot | |
3791 || ((flags & EW_DODOT) | |
3792 && dp->d_name[1] != NUL | |
3793 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL))) | |
3794 && ((regmatch.regprog != NULL && vim_regexec(®match, | |
3795 (char_u *)dp->d_name, (colnr_T)0)) | |
3796 || ((flags & EW_NOTWILD) | |
3797 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0))) | |
3798 { | |
3799 STRCPY(s, dp->d_name); | |
3800 len = STRLEN(buf); | |
3801 | |
3802 if (starstar && stardepth < 100) | |
3803 { | |
3804 /* For "**" in the pattern first go deeper in the tree to | |
3805 * find matches. */ | |
3806 STRCPY(buf + len, "/**"); | |
3807 STRCPY(buf + len + 3, path_end); | |
3808 ++stardepth; | |
3809 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE); | |
3810 --stardepth; | |
3811 } | |
3812 | |
3813 STRCPY(buf + len, path_end); | |
3814 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */ | |
3815 { | |
3816 /* need to expand another component of the path */ | |
3817 /* remove backslashes for the remaining components only */ | |
3818 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE); | |
3819 } | |
3820 else | |
3821 { | |
3822 stat_T sb; | |
3823 | |
3824 /* no more wildcards, check if there is a match */ | |
3825 /* remove backslashes for the remaining components only */ | |
3826 if (*path_end != NUL) | |
3827 backslash_halve(buf + len + 1); | |
3828 /* add existing file or symbolic link */ | |
3829 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0 | |
3830 : mch_getperm(buf) >= 0) | |
3831 { | |
3832 #ifdef MACOS_CONVERT | |
3833 size_t precomp_len = STRLEN(buf)+1; | |
3834 char_u *precomp_buf = | |
3835 mac_precompose_path(buf, precomp_len, &precomp_len); | |
3836 | |
3837 if (precomp_buf) | |
3838 { | |
3839 mch_memmove(buf, precomp_buf, precomp_len); | |
3840 vim_free(precomp_buf); | |
3841 } | |
3842 #endif | |
3843 addfile(gap, buf, flags); | |
3844 } | |
3845 } | |
3846 } | |
3847 } | |
3848 | |
3849 closedir(dirp); | |
3850 } | |
3851 | |
3852 vim_free(buf); | |
3853 vim_regfree(regmatch.regprog); | |
3854 | |
3855 matches = gap->ga_len - start_len; | |
3856 if (matches > 0) | |
3857 qsort(((char_u **)gap->ga_data) + start_len, matches, | |
3858 sizeof(char_u *), pstrcmp); | |
3859 return matches; | |
3860 } | |
3861 #endif | |
3862 | |
3863 /* | |
3864 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a | |
3865 * list of file names in allocated memory. | |
3866 */ | |
3867 void | |
3868 remove_duplicates(garray_T *gap) | |
3869 { | |
3870 int i; | |
3871 int j; | |
3872 char_u **fnames = (char_u **)gap->ga_data; | |
3873 | |
3874 sort_strings(fnames, gap->ga_len); | |
3875 for (i = gap->ga_len - 1; i > 0; --i) | |
3876 if (fnamecmp(fnames[i - 1], fnames[i]) == 0) | |
3877 { | |
3878 vim_free(fnames[i]); | |
3879 for (j = i + 1; j < gap->ga_len; ++j) | |
3880 fnames[j - 1] = fnames[j]; | |
3881 --gap->ga_len; | |
3882 } | |
3883 } | |
3884 | |
3885 /* | |
3886 * Return TRUE if "p" contains what looks like an environment variable. | |
3887 * Allowing for escaping. | |
3888 */ | |
3889 static int | |
3890 has_env_var(char_u *p) | |
3891 { | |
3892 for ( ; *p; MB_PTR_ADV(p)) | |
3893 { | |
3894 if (*p == '\\' && p[1] != NUL) | |
3895 ++p; | |
3896 else if (vim_strchr((char_u *) | |
3897 #if defined(MSWIN) | |
3898 "$%" | |
3899 #else | |
3900 "$" | |
3901 #endif | |
3902 , *p) != NULL) | |
3903 return TRUE; | |
3904 } | |
3905 return FALSE; | |
3906 } | |
3907 | |
3908 #ifdef SPECIAL_WILDCHAR | |
3909 /* | |
3910 * Return TRUE if "p" contains a special wildcard character, one that Vim | |
3911 * cannot expand, requires using a shell. | |
3912 */ | |
3913 static int | |
3914 has_special_wildchar(char_u *p) | |
3915 { | |
3916 for ( ; *p; MB_PTR_ADV(p)) | |
3917 { | |
3918 // Disallow line break characters. | |
3919 if (*p == '\r' || *p == '\n') | |
3920 break; | |
3921 // Allow for escaping. | |
3922 if (*p == '\\' && p[1] != NUL && p[1] != '\r' && p[1] != '\n') | |
3923 ++p; | |
3924 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL) | |
3925 { | |
3926 // A { must be followed by a matching }. | |
3927 if (*p == '{' && vim_strchr(p, '}') == NULL) | |
3928 continue; | |
3929 // A quote and backtick must be followed by another one. | |
3930 if ((*p == '`' || *p == '\'') && vim_strchr(p, *p) == NULL) | |
3931 continue; | |
3932 return TRUE; | |
3933 } | |
3934 } | |
3935 return FALSE; | |
3936 } | |
3937 #endif | |
3938 | |
3939 /* | |
3940 * Generic wildcard expansion code. | |
3941 * | |
3942 * Characters in "pat" that should not be expanded must be preceded with a | |
3943 * backslash. E.g., "/path\ with\ spaces/my\*star*" | |
3944 * | |
3945 * Return FAIL when no single file was found. In this case "num_file" is not | |
3946 * set, and "file" may contain an error message. | |
3947 * Return OK when some files found. "num_file" is set to the number of | |
3948 * matches, "file" to the array of matches. Call FreeWild() later. | |
3949 */ | |
3950 int | |
3951 gen_expand_wildcards( | |
3952 int num_pat, /* number of input patterns */ | |
3953 char_u **pat, /* array of input patterns */ | |
3954 int *num_file, /* resulting number of files */ | |
3955 char_u ***file, /* array of resulting files */ | |
3956 int flags) /* EW_* flags */ | |
3957 { | |
3958 int i; | |
3959 garray_T ga; | |
3960 char_u *p; | |
3961 static int recursive = FALSE; | |
3962 int add_pat; | |
3963 int retval = OK; | |
3964 #if defined(FEAT_SEARCHPATH) | |
3965 int did_expand_in_path = FALSE; | |
3966 #endif | |
3967 | |
3968 /* | |
3969 * expand_env() is called to expand things like "~user". If this fails, | |
3970 * it calls ExpandOne(), which brings us back here. In this case, always | |
3971 * call the machine specific expansion function, if possible. Otherwise, | |
3972 * return FAIL. | |
3973 */ | |
3974 if (recursive) | |
3975 #ifdef SPECIAL_WILDCHAR | |
3976 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); | |
3977 #else | |
3978 return FAIL; | |
3979 #endif | |
3980 | |
3981 #ifdef SPECIAL_WILDCHAR | |
3982 /* | |
3983 * If there are any special wildcard characters which we cannot handle | |
3984 * here, call machine specific function for all the expansion. This | |
3985 * avoids starting the shell for each argument separately. | |
3986 * For `=expr` do use the internal function. | |
3987 */ | |
3988 for (i = 0; i < num_pat; i++) | |
3989 { | |
3990 if (has_special_wildchar(pat[i]) | |
3991 # ifdef VIM_BACKTICK | |
3992 && !(vim_backtick(pat[i]) && pat[i][1] == '=') | |
3993 # endif | |
3994 ) | |
3995 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); | |
3996 } | |
3997 #endif | |
3998 | |
3999 recursive = TRUE; | |
4000 | |
4001 /* | |
4002 * The matching file names are stored in a growarray. Init it empty. | |
4003 */ | |
4004 ga_init2(&ga, (int)sizeof(char_u *), 30); | |
4005 | |
4006 for (i = 0; i < num_pat; ++i) | |
4007 { | |
4008 add_pat = -1; | |
4009 p = pat[i]; | |
4010 | |
4011 #ifdef VIM_BACKTICK | |
4012 if (vim_backtick(p)) | |
4013 { | |
4014 add_pat = expand_backtick(&ga, p, flags); | |
4015 if (add_pat == -1) | |
4016 retval = FAIL; | |
4017 } | |
4018 else | |
4019 #endif | |
4020 { | |
4021 /* | |
4022 * First expand environment variables, "~/" and "~user/". | |
4023 */ | |
4024 if ((has_env_var(p) && !(flags & EW_NOTENV)) || *p == '~') | |
4025 { | |
4026 p = expand_env_save_opt(p, TRUE); | |
4027 if (p == NULL) | |
4028 p = pat[i]; | |
4029 #ifdef UNIX | |
4030 /* | |
4031 * On Unix, if expand_env() can't expand an environment | |
4032 * variable, use the shell to do that. Discard previously | |
4033 * found file names and start all over again. | |
4034 */ | |
4035 else if (has_env_var(p) || *p == '~') | |
4036 { | |
4037 vim_free(p); | |
4038 ga_clear_strings(&ga); | |
4039 i = mch_expand_wildcards(num_pat, pat, num_file, file, | |
4040 flags|EW_KEEPDOLLAR); | |
4041 recursive = FALSE; | |
4042 return i; | |
4043 } | |
4044 #endif | |
4045 } | |
4046 | |
4047 /* | |
4048 * If there are wildcards: Expand file names and add each match to | |
4049 * the list. If there is no match, and EW_NOTFOUND is given, add | |
4050 * the pattern. | |
4051 * If there are no wildcards: Add the file name if it exists or | |
4052 * when EW_NOTFOUND is given. | |
4053 */ | |
4054 if (mch_has_exp_wildcard(p)) | |
4055 { | |
4056 #if defined(FEAT_SEARCHPATH) | |
4057 if ((flags & EW_PATH) | |
4058 && !mch_isFullName(p) | |
4059 && !(p[0] == '.' | |
4060 && (vim_ispathsep(p[1]) | |
4061 || (p[1] == '.' && vim_ispathsep(p[2])))) | |
4062 ) | |
4063 { | |
4064 /* :find completion where 'path' is used. | |
4065 * Recursiveness is OK here. */ | |
4066 recursive = FALSE; | |
4067 add_pat = expand_in_path(&ga, p, flags); | |
4068 recursive = TRUE; | |
4069 did_expand_in_path = TRUE; | |
4070 } | |
4071 else | |
4072 #endif | |
4073 add_pat = mch_expandpath(&ga, p, flags); | |
4074 } | |
4075 } | |
4076 | |
4077 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) | |
4078 { | |
4079 char_u *t = backslash_halve_save(p); | |
4080 | |
4081 /* When EW_NOTFOUND is used, always add files and dirs. Makes | |
4082 * "vim c:/" work. */ | |
4083 if (flags & EW_NOTFOUND) | |
4084 addfile(&ga, t, flags | EW_DIR | EW_FILE); | |
4085 else | |
4086 addfile(&ga, t, flags); | |
4087 | |
4088 if (t != p) | |
4089 vim_free(t); | |
4090 } | |
4091 | |
4092 #if defined(FEAT_SEARCHPATH) | |
4093 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH)) | |
4094 uniquefy_paths(&ga, p); | |
4095 #endif | |
4096 if (p != pat[i]) | |
4097 vim_free(p); | |
4098 } | |
4099 | |
4100 *num_file = ga.ga_len; | |
4101 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)""; | |
4102 | |
4103 recursive = FALSE; | |
4104 | |
4105 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL; | |
4106 } | |
4107 | |
4108 # ifdef VIM_BACKTICK | |
4109 | |
4110 /* | |
4111 * Return TRUE if we can expand this backtick thing here. | |
4112 */ | |
4113 static int | |
4114 vim_backtick(char_u *p) | |
4115 { | |
4116 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); | |
4117 } | |
4118 | |
4119 /* | |
4120 * Expand an item in `backticks` by executing it as a command. | |
4121 * Currently only works when pat[] starts and ends with a `. | |
4122 * Returns number of file names found, -1 if an error is encountered. | |
4123 */ | |
4124 static int | |
4125 expand_backtick( | |
4126 garray_T *gap, | |
4127 char_u *pat, | |
4128 int flags) /* EW_* flags */ | |
4129 { | |
4130 char_u *p; | |
4131 char_u *cmd; | |
4132 char_u *buffer; | |
4133 int cnt = 0; | |
4134 int i; | |
4135 | |
4136 /* Create the command: lop off the backticks. */ | |
4137 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2); | |
4138 if (cmd == NULL) | |
4139 return -1; | |
4140 | |
4141 #ifdef FEAT_EVAL | |
4142 if (*cmd == '=') /* `={expr}`: Expand expression */ | |
4143 buffer = eval_to_string(cmd + 1, &p, TRUE); | |
4144 else | |
4145 #endif | |
4146 buffer = get_cmd_output(cmd, NULL, | |
4147 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL); | |
4148 vim_free(cmd); | |
4149 if (buffer == NULL) | |
4150 return -1; | |
4151 | |
4152 cmd = buffer; | |
4153 while (*cmd != NUL) | |
4154 { | |
4155 cmd = skipwhite(cmd); /* skip over white space */ | |
4156 p = cmd; | |
4157 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */ | |
4158 ++p; | |
4159 /* add an entry if it is not empty */ | |
4160 if (p > cmd) | |
4161 { | |
4162 i = *p; | |
4163 *p = NUL; | |
4164 addfile(gap, cmd, flags); | |
4165 *p = i; | |
4166 ++cnt; | |
4167 } | |
4168 cmd = p; | |
4169 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) | |
4170 ++cmd; | |
4171 } | |
4172 | |
4173 vim_free(buffer); | |
4174 return cnt; | |
4175 } | |
4176 # endif /* VIM_BACKTICK */ | |
4177 | |
4178 /* | |
4179 * Add a file to a file list. Accepted flags: | |
4180 * EW_DIR add directories | |
4181 * EW_FILE add files | |
4182 * EW_EXEC add executable files | |
4183 * EW_NOTFOUND add even when it doesn't exist | |
4184 * EW_ADDSLASH add slash after directory name | |
4185 * EW_ALLLINKS add symlink also when the referred file does not exist | |
4186 */ | |
4187 void | |
4188 addfile( | |
4189 garray_T *gap, | |
4190 char_u *f, /* filename */ | |
4191 int flags) | |
4192 { | |
4193 char_u *p; | |
4194 int isdir; | |
4195 stat_T sb; | |
4196 | |
4197 /* if the file/dir/link doesn't exist, may not add it */ | |
4198 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS) | |
4199 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0)) | |
4200 return; | |
4201 | |
4202 #ifdef FNAME_ILLEGAL | |
4203 /* if the file/dir contains illegal characters, don't add it */ | |
4204 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) | |
4205 return; | |
4206 #endif | |
4207 | |
4208 isdir = mch_isdir(f); | |
4209 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) | |
4210 return; | |
4211 | |
4212 /* If the file isn't executable, may not add it. Do accept directories. | |
4213 * When invoked from expand_shellcmd() do not use $PATH. */ | |
4214 if (!isdir && (flags & EW_EXEC) | |
4215 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD))) | |
4216 return; | |
4217 | |
4218 /* Make room for another item in the file list. */ | |
4219 if (ga_grow(gap, 1) == FAIL) | |
4220 return; | |
4221 | |
4222 p = alloc(STRLEN(f) + 1 + isdir); | |
4223 if (p == NULL) | |
4224 return; | |
4225 | |
4226 STRCPY(p, f); | |
4227 #ifdef BACKSLASH_IN_FILENAME | |
4228 slash_adjust(p); | |
4229 #endif | |
4230 /* | |
4231 * Append a slash or backslash after directory names if none is present. | |
4232 */ | |
4233 #ifndef DONT_ADD_PATHSEP_TO_DIR | |
4234 if (isdir && (flags & EW_ADDSLASH)) | |
4235 add_pathsep(p); | |
4236 #endif | |
4237 ((char_u **)gap->ga_data)[gap->ga_len++] = p; | |
4238 } | |
4239 #endif /* !NO_EXPANDPATH */ | |
4240 | 2653 |
4241 #if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \ | 2654 #if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \ |
4242 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ | 2655 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
4243 || defined(PROTO) | 2656 || defined(PROTO) |
4244 | 2657 |
4343 | 2756 |
4344 done: | 2757 done: |
4345 vim_free(tempname); | 2758 vim_free(tempname); |
4346 return buffer; | 2759 return buffer; |
4347 } | 2760 } |
4348 #endif | 2761 |
4349 | 2762 # if defined(FEAT_EVAL) || defined(PROTO) |
4350 /* | 2763 |
4351 * Free the list of files returned by expand_wildcards() or other expansion | |
4352 * functions. | |
4353 */ | |
4354 void | 2764 void |
4355 FreeWild(int count, char_u **files) | 2765 get_cmd_output_as_rettv( |
4356 { | 2766 typval_T *argvars, |
4357 if (count <= 0 || files == NULL) | 2767 typval_T *rettv, |
4358 return; | 2768 int retlist) |
4359 while (count--) | 2769 { |
4360 vim_free(files[count]); | 2770 char_u *res = NULL; |
4361 vim_free(files); | 2771 char_u *p; |
4362 } | 2772 char_u *infile = NULL; |
2773 int err = FALSE; | |
2774 FILE *fd; | |
2775 list_T *list = NULL; | |
2776 int flags = SHELL_SILENT; | |
2777 | |
2778 rettv->v_type = VAR_STRING; | |
2779 rettv->vval.v_string = NULL; | |
2780 if (check_restricted() || check_secure()) | |
2781 goto errret; | |
2782 | |
2783 if (argvars[1].v_type != VAR_UNKNOWN) | |
2784 { | |
2785 /* | |
2786 * Write the text to a temp file, to be used for input of the shell | |
2787 * command. | |
2788 */ | |
2789 if ((infile = vim_tempname('i', TRUE)) == NULL) | |
2790 { | |
2791 emsg(_(e_notmp)); | |
2792 goto errret; | |
2793 } | |
2794 | |
2795 fd = mch_fopen((char *)infile, WRITEBIN); | |
2796 if (fd == NULL) | |
2797 { | |
2798 semsg(_(e_notopen), infile); | |
2799 goto errret; | |
2800 } | |
2801 if (argvars[1].v_type == VAR_NUMBER) | |
2802 { | |
2803 linenr_T lnum; | |
2804 buf_T *buf; | |
2805 | |
2806 buf = buflist_findnr(argvars[1].vval.v_number); | |
2807 if (buf == NULL) | |
2808 { | |
2809 semsg(_(e_nobufnr), argvars[1].vval.v_number); | |
2810 fclose(fd); | |
2811 goto errret; | |
2812 } | |
2813 | |
2814 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++) | |
2815 { | |
2816 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p) | |
2817 if (putc(*p == '\n' ? NUL : *p, fd) == EOF) | |
2818 { | |
2819 err = TRUE; | |
2820 break; | |
2821 } | |
2822 if (putc(NL, fd) == EOF) | |
2823 { | |
2824 err = TRUE; | |
2825 break; | |
2826 } | |
2827 } | |
2828 } | |
2829 else if (argvars[1].v_type == VAR_LIST) | |
2830 { | |
2831 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL) | |
2832 err = TRUE; | |
2833 } | |
2834 else | |
2835 { | |
2836 size_t len; | |
2837 char_u buf[NUMBUFLEN]; | |
2838 | |
2839 p = tv_get_string_buf_chk(&argvars[1], buf); | |
2840 if (p == NULL) | |
2841 { | |
2842 fclose(fd); | |
2843 goto errret; /* type error; errmsg already given */ | |
2844 } | |
2845 len = STRLEN(p); | |
2846 if (len > 0 && fwrite(p, len, 1, fd) != 1) | |
2847 err = TRUE; | |
2848 } | |
2849 if (fclose(fd) != 0) | |
2850 err = TRUE; | |
2851 if (err) | |
2852 { | |
2853 emsg(_("E677: Error writing temp file")); | |
2854 goto errret; | |
2855 } | |
2856 } | |
2857 | |
2858 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell | |
2859 * echoes typeahead, that messes up the display. */ | |
2860 if (!msg_silent) | |
2861 flags += SHELL_COOKED; | |
2862 | |
2863 if (retlist) | |
2864 { | |
2865 int len; | |
2866 listitem_T *li; | |
2867 char_u *s = NULL; | |
2868 char_u *start; | |
2869 char_u *end; | |
2870 int i; | |
2871 | |
2872 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len); | |
2873 if (res == NULL) | |
2874 goto errret; | |
2875 | |
2876 list = list_alloc(); | |
2877 if (list == NULL) | |
2878 goto errret; | |
2879 | |
2880 for (i = 0; i < len; ++i) | |
2881 { | |
2882 start = res + i; | |
2883 while (i < len && res[i] != NL) | |
2884 ++i; | |
2885 end = res + i; | |
2886 | |
2887 s = alloc(end - start + 1); | |
2888 if (s == NULL) | |
2889 goto errret; | |
2890 | |
2891 for (p = s; start < end; ++p, ++start) | |
2892 *p = *start == NUL ? NL : *start; | |
2893 *p = NUL; | |
2894 | |
2895 li = listitem_alloc(); | |
2896 if (li == NULL) | |
2897 { | |
2898 vim_free(s); | |
2899 goto errret; | |
2900 } | |
2901 li->li_tv.v_type = VAR_STRING; | |
2902 li->li_tv.v_lock = 0; | |
2903 li->li_tv.vval.v_string = s; | |
2904 list_append(list, li); | |
2905 } | |
2906 | |
2907 rettv_list_set(rettv, list); | |
2908 list = NULL; | |
2909 } | |
2910 else | |
2911 { | |
2912 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL); | |
2913 #ifdef USE_CRNL | |
2914 /* translate <CR><NL> into <NL> */ | |
2915 if (res != NULL) | |
2916 { | |
2917 char_u *s, *d; | |
2918 | |
2919 d = res; | |
2920 for (s = res; *s; ++s) | |
2921 { | |
2922 if (s[0] == CAR && s[1] == NL) | |
2923 ++s; | |
2924 *d++ = *s; | |
2925 } | |
2926 *d = NUL; | |
2927 } | |
2928 #endif | |
2929 rettv->vval.v_string = res; | |
2930 res = NULL; | |
2931 } | |
2932 | |
2933 errret: | |
2934 if (infile != NULL) | |
2935 { | |
2936 mch_remove(infile); | |
2937 vim_free(infile); | |
2938 } | |
2939 if (res != NULL) | |
2940 vim_free(res); | |
2941 if (list != NULL) | |
2942 list_free(list); | |
2943 } | |
2944 | |
2945 /* | |
2946 * "system()" function | |
2947 */ | |
2948 void | |
2949 f_system(typval_T *argvars, typval_T *rettv) | |
2950 { | |
2951 get_cmd_output_as_rettv(argvars, rettv, FALSE); | |
2952 } | |
2953 | |
2954 /* | |
2955 * "systemlist()" function | |
2956 */ | |
2957 void | |
2958 f_systemlist(typval_T *argvars, typval_T *rettv) | |
2959 { | |
2960 get_cmd_output_as_rettv(argvars, rettv, TRUE); | |
2961 } | |
2962 # endif // FEAT_EVAL | |
2963 | |
2964 #endif | |
4363 | 2965 |
4364 /* | 2966 /* |
4365 * Return TRUE when need to go to Insert mode because of 'insertmode'. | 2967 * Return TRUE when need to go to Insert mode because of 'insertmode'. |
4366 * Don't do this when still processing a command or a mapping. | 2968 * Don't do this when still processing a command or a mapping. |
4367 * Don't do this when inside a ":normal" command. | 2969 * Don't do this when inside a ":normal" command. |
4437 | 3039 |
4438 for (p = fname; isalpha(*p); ++p) | 3040 for (p = fname; isalpha(*p); ++p) |
4439 ; | 3041 ; |
4440 return path_is_url(p); | 3042 return path_is_url(p); |
4441 } | 3043 } |
4442 | |
4443 /* | |
4444 * Return TRUE if "name" is a full (absolute) path name or URL. | |
4445 */ | |
4446 int | |
4447 vim_isAbsName(char_u *name) | |
4448 { | |
4449 return (path_with_url(name) != 0 || mch_isFullName(name)); | |
4450 } | |
4451 | |
4452 /* | |
4453 * Get absolute file name into buffer "buf[len]". | |
4454 * | |
4455 * return FAIL for failure, OK otherwise | |
4456 */ | |
4457 int | |
4458 vim_FullName( | |
4459 char_u *fname, | |
4460 char_u *buf, | |
4461 int len, | |
4462 int force) /* force expansion even when already absolute */ | |
4463 { | |
4464 int retval = OK; | |
4465 int url; | |
4466 | |
4467 *buf = NUL; | |
4468 if (fname == NULL) | |
4469 return FAIL; | |
4470 | |
4471 url = path_with_url(fname); | |
4472 if (!url) | |
4473 retval = mch_FullName(fname, buf, len, force); | |
4474 if (url || retval == FAIL) | |
4475 { | |
4476 /* something failed; use the file name (truncate when too long) */ | |
4477 vim_strncpy(buf, fname, len - 1); | |
4478 } | |
4479 #if defined(MSWIN) | |
4480 slash_adjust(buf); | |
4481 #endif | |
4482 return retval; | |
4483 } |