2 * utils.c - miscellaneous utilities
4 * This file is part of zsh, the Z shell.
6 * Copyright (c) 1992-1997 Paul Falstad
9 * Permission is hereby granted, without written agreement and without
10 * license or royalty fees, to use, copy, modify, and distribute this
11 * software and to distribute modified versions of this software for any
12 * purpose, provided that the above copyright notice and the following
13 * two paragraphs appear in all copies of this software.
15 * In no event shall Paul Falstad or the Zsh Development Group be liable
16 * to any party for direct, indirect, special, incidental, or consequential
17 * damages arising out of the use of this software and its documentation,
18 * even if Paul Falstad and the Zsh Development Group have been advised of
19 * the possibility of such damage.
21 * Paul Falstad and the Zsh Development Group specifically disclaim any
22 * warranties, including, but not limited to, the implied warranties of
23 * merchantability and fitness for a particular purpose. The software
24 * provided hereunder is on an "as is" basis, and Paul Falstad and the
25 * Zsh Development Group have no obligation to provide maintenance,
26 * support, updates, enhancements, or modifications.
33 /* name of script being sourced */
36 mod_export
char *scriptname
; /* is sometimes a function name */
38 /* filename of script or other file containing code source e.g. autoload */
41 mod_export
char *scriptfilename
;
43 #ifdef MULTIBYTE_SUPPORT
44 struct widechar_array
{
48 typedef struct widechar_array
*Widechar_array
;
51 * The wordchars variable turned into a wide character array.
52 * This is much more convenient for testing.
54 struct widechar_array wordchars_wide
;
57 * The same for the separators (IFS) array.
59 struct widechar_array ifs_wide
;
61 /* Function to set one of the above from the multibyte array */
64 set_widearray(char *mb_array
, Widechar_array wca
)
72 if (!isset(MULTIBYTE
))
76 VARARR(wchar_t, tmpwcs
, strlen(mb_array
));
77 wchar_t *wcptr
= tmpwcs
;
82 int mblen
= mb_metacharlenconv(mb_array
, &wci
);
86 /* No good unless all characters are convertible */
89 *wcptr
++ = (wchar_t)wci
;
92 * This generates a warning from the compiler (and is
93 * indeed useless) if chars are unsigned. It's
94 * extreme paranoia anyway.
97 fprintf(stderr
, "BUG: Bad cast to wchar_t\n");
102 wca
->len
= wcptr
- tmpwcs
;
103 wca
->chars
= (wchar_t *)zalloc(wca
->len
* sizeof(wchar_t));
104 wmemcpy(wca
->chars
, tmpwcs
, wca
->len
);
113 zwarning(const char *cmd
, const char *fmt
, va_list ap
)
116 zleentry(ZLE_CMD_TRASH
);
119 if (unset(SHINSTDIN
) || locallevel
) {
120 nicezputs(scriptname
? scriptname
: argzero
, stderr
);
121 fputc((unsigned char)':', stderr
);
123 nicezputs(cmd
, stderr
);
124 fputc((unsigned char)':', stderr
);
127 * scriptname is set when sourcing scripts, so that we get the
128 * correct name instead of the generic name of whatever
129 * program/script is running. It's also set in shell functions,
130 * so test locallevel, too.
132 nicezputs((isset(SHINSTDIN
) && !locallevel
) ? "zsh" :
133 scriptname
? scriptname
: argzero
, stderr
);
134 fputc((unsigned char)':', stderr
);
137 zerrmsg(stderr
, fmt
, ap
);
143 zerr(VA_ALIST1(const char *fmt
))
147 VA_DEF_ARG(const char *fmt
);
149 if (errflag
|| noerrs
) {
156 VA_GET_ARG(ap
, fmt
, const char *);
157 zwarning(NULL
, fmt
, ap
);
164 zerrnam(VA_ALIST2(const char *cmd
, const char *fmt
))
168 VA_DEF_ARG(const char *cmd
);
169 VA_DEF_ARG(const char *fmt
);
171 if (errflag
|| noerrs
)
175 VA_GET_ARG(ap
, cmd
, const char *);
176 VA_GET_ARG(ap
, fmt
, const char *);
177 zwarning(cmd
, fmt
, ap
);
184 zwarn(VA_ALIST1(const char *fmt
))
188 VA_DEF_ARG(const char *fmt
);
190 if (errflag
|| noerrs
)
194 VA_GET_ARG(ap
, fmt
, const char *);
195 zwarning(NULL
, fmt
, ap
);
201 zwarnnam(VA_ALIST2(const char *cmd
, const char *fmt
))
205 VA_DEF_ARG(const char *cmd
);
206 VA_DEF_ARG(const char *fmt
);
208 if (errflag
|| noerrs
)
212 VA_GET_ARG(ap
, cmd
, const char *);
213 VA_GET_ARG(ap
, fmt
, const char *);
214 zwarning(cmd
, fmt
, ap
);
223 dputs(VA_ALIST1(const char *message
))
229 VA_DEF_ARG(const char *message
);
231 VA_START(ap
, message
);
232 VA_GET_ARG(ap
, message
, const char *);
233 if ((filename
= getsparam("ZSH_DEBUG_LOG")) != NULL
&&
234 (file
= fopen(filename
, "a")) != NULL
) {
235 zerrmsg(file
, message
, ap
);
238 zerrmsg(stderr
, message
, ap
);
246 * This works around an occasional problem with dllwrap on Cygwin, seen
247 * on at least two installations. It fails to find the last symbol
248 * exported in alphabetical order (in our case zwarnnam). Until this is
249 * properly categorised and fixed we add a dummy symbol at the end.
252 zz_plural_z_alpha(void)
259 zerrmsg(FILE *file
, const char *fmt
, va_list ap
)
266 #ifdef HAVE_STRERROR_R
267 #define ERRBUFSIZE (80)
269 char errbuf
[ERRBUFSIZE
];
273 if ((unset(SHINSTDIN
) || locallevel
) && lineno
)
274 fprintf(file
, "%ld: ", (long)lineno
);
276 fputc((unsigned char)' ', file
);
283 str
= va_arg(ap
, const char *);
284 nicezputs(str
, file
);
288 str
= va_arg(ap
, const char *);
289 num
= va_arg(ap
, int);
290 num
= metalen(str
, num
);
291 s
= zhalloc(num
+ 1);
299 lnum
= va_arg(ap
, long);
300 fprintf(file
, "%ld", lnum
);
304 num
= va_arg(ap
, int);
305 fprintf(file
, "%d", num
);
311 num
= va_arg(ap
, int);
312 #ifdef MULTIBYTE_SUPPORT
314 zputs(wcs_nicechar(num
, NULL
, NULL
), file
);
316 zputs(nicechar(num
), file
);
320 /* print the corresponding message for this errno */
321 num
= va_arg(ap
, int);
323 fputs("interrupt\n", file
);
327 errmsg
= strerror(num
);
328 /* If the message is not about I/O problems, it looks better *
329 * if we uncapitalize the first letter of the message */
333 fputc(tulower(errmsg
[0]), file
);
334 fputs(errmsg
+ 1, file
);
339 putc(*fmt
== Meta
? *++fmt
^ 32 : *fmt
, file
);
346 /* Output a single character, for the termcap routines. *
347 * This is used instead of putchar since it can be a macro. */
357 /* Output a single character, for the termcap routines. */
368 * Turn a character into a visible representation thereof. The visible
369 * string is put together in a static buffer, and this function returns
370 * a pointer to it. Printable characters stand for themselves, DEL is
371 * represented as "^?", newline and tab are represented as "\n" and
372 * "\t", and normal control characters are represented in "^C" form.
373 * Characters with bit 7 set, if unprintable, are represented as "\M-"
374 * followed by the visible representation of the character with bit 7
375 * stripped off. Tokens are interpreted, rather than being treated as
376 * literal characters.
378 * Note that the returned string is metafied, so that it must be
379 * treated like any other zsh internal string (and not, for example,
382 * This function is used even if MULTIBYTE_SUPPORT is defined: we
383 * use it as a fallback in case we couldn't identify a wide character
384 * in a multibyte string.
397 if (isset(PRINTEIGHTBIT
))
409 } else if (c
== '\n') {
412 } else if (c
== '\t') {
415 } else if (c
< 0x20) {
421 * The resulting string is still metafied, so check if
422 * we are returning a character in the range that needs metafication.
423 * This can't happen if the character is printed "nicely", so
424 * this results in a maximum of two bytes total (plus the null).
436 #ifdef MULTIBYTE_SUPPORT
437 static mbstate_t mb_shiftstate
;
440 * Initialise multibyte state: called before a sequence of
441 * wcs_nicechar() or mb_metacharlenconv().
446 mb_metacharinit(void)
448 memset(&mb_shiftstate
, 0, sizeof(mb_shiftstate
));
452 * The number of bytes we need to allocate for a "nice" representation
453 * of a multibyte character.
455 * We double MB_CUR_MAX to take account of the fact that
456 * we may need to metafy. In fact the representation probably
457 * doesn't allow every character to be in the meta range, but
458 * we don't need to be too pedantic.
460 * The 12 is for the output of a UCS-4 code; we don't actually
461 * need this at the same time as MB_CUR_MAX, but again it's
462 * not worth calculating more exactly.
464 #define NICECHAR_MAX (12 + 2*MB_CUR_MAX)
466 * Input a wide character. Output a printable representation,
467 * which is a metafied multibyte string. With widthp return
468 * the printing width.
470 * swide, if non-NULL, is used to help the completion code, which needs
471 * to know the printing width of the each part of the representation.
472 * *swide is set to the part of the returned string where the wide
473 * character starts. Any string up to that point is ASCII characters,
474 * so the width of it is (*swide - <return_value>). Anything left is
475 * a single wide character corresponding to the remaining width.
476 * Either the initial ASCII part or the wide character part may be empty
477 * (but not both). (Note the complication that the wide character
478 * part may contain metafied characters.)
480 * The caller needs to call mb_metacharinit() before the first call, to
481 * set up the multibyte shift state for a range of characters.
486 wcs_nicechar(wchar_t c
, size_t *widthp
, char **swidep
)
489 static int bufalloc
= 0, newalloc
;
492 VARARR(char, mbstr
, MB_CUR_MAX
);
495 * We want buf to persist beyond the return. MB_CUR_MAX and hence
496 * NICECHAR_MAX may not be constant, so we have to allocate this at
497 * run time. (We could probably get away with just allocating a
498 * large buffer, in practice.) For efficiency, only reallocate if
499 * we really need to, since this function will be called frequently.
501 newalloc
= NICECHAR_MAX
;
502 if (bufalloc
!= newalloc
)
505 buf
= (char *)zrealloc(buf
, bufalloc
);
509 if (!iswprint(c
) && (c
< 0x80 || !isset(PRINTEIGHTBIT
))) {
513 } else if (c
== L
'\n') {
516 } else if (c
== L
'\t') {
519 } else if (c
< 0x20) {
522 } else if (c
>= 0x80) {
528 ret
= wcrtomb(mbstr
, c
, &mb_shiftstate
);
531 memset(&mb_shiftstate
, 0, sizeof(mb_shiftstate
));
533 * Can't or don't want to convert character: use UCS-2 or
534 * UCS-4 code in print escape format.
536 * This comparison fails and generates a compiler warning
537 * if wchar_t is 16 bits, but the code is still correct.
540 sprintf(buf
, "\\U%.8x", (unsigned int)c
);
543 } else if (c
>= 0x100) {
544 sprintf(buf
, "\\u%.4x", (unsigned int)c
);
548 strcpy(buf
, nicechar((int)c
));
550 * There may be metafied characters from nicechar(),
551 * so compute width and end position independently.
554 *widthp
= ztrlen(buf
);
556 *swidep
= buf
+ strlen(buf
);
560 *swidep
= buf
+ *widthp
;
565 int wcw
= WCWIDTH(c
);
574 for (mbptr
= mbstr
; ret
; s
++, mbptr
++, ret
--) {
575 DPUTS(s
>= buf
+ NICECHAR_MAX
,
576 "BUG: buffer too small in wcs_nicechar");
579 DPUTS(s
>= buf
+ NICECHAR_MAX
,
580 "BUG: buffer too small for metafied char in wcs_nicechar");
595 /* assume a single-byte character if not valid */
596 if (wc
== WEOF
|| unset(MULTIBYTE
))
599 /* if not printable, assume width 1 */
606 #endif /* MULTIBYTE_SUPPORT */
608 /* get a symlink-free pathname for s relative to PWD */
618 s
= tricat((pwd
[1]) ? pwd
: "", "/", s
);
624 /* Check whether a string contains the *
625 * name of the present directory. */
631 struct stat sbuf
, tbuf
;
633 if (stat(unmeta(s
), &sbuf
) == 0 && stat(".", &tbuf
) == 0)
634 if (sbuf
.st_dev
== tbuf
.st_dev
&& sbuf
.st_ino
== tbuf
.st_ino
)
639 static char xbuf
[PATH_MAX
*2];
649 return (char **) zshcalloc(sizeof(char **));
651 for (t
= s
, t0
= 0; *t
; t
++)
654 q
= r
= (char **) zalloc(sizeof(char **) * (t0
+ 2));
656 while ((t
= strchr(s
, '/'))) {
657 *q
++ = ztrduppfx(s
, t
- s
);
671 /* expands symlinks and .. or . expressions */
672 /* if flag = 0, only expand .. and . expressions */
679 char xbuf2
[PATH_MAX
*2], xbuf3
[PATH_MAX
*2];
682 opp
= pp
= slashsplit(s
);
684 if (!strcmp(*pp
, ".")) {
688 if (!strcmp(*pp
, "..")) {
692 if (!strcmp(xbuf
, "/"))
694 p
= xbuf
+ strlen(xbuf
);
699 sprintf(xbuf2
, "%s/%s", xbuf
, *pp
);
700 t0
= readlink(unmeta(xbuf2
), xbuf3
, PATH_MAX
);
707 metafy(xbuf3
, t0
, META_NOALLOC
);
710 xsymlinks(xbuf3
+ 1);
721 * expand symlinks in s, and remove other weird things:
722 * note that this always expands symlinks.
740 print_if_link(char *s
)
744 if (xsymlinks(s
+ 1))
745 printf(" -> "), zputs(*xbuf
? xbuf
: "/", stdout
);
749 /* print a directory */
753 fprintdir(char *s
, FILE *f
)
755 Nameddir d
= finddir(s
);
761 fputs(unmeta(d
->node
.nam
), f
);
762 fputs(unmeta(s
+ strlen(d
->dir
)), f
);
766 /* Returns the current username. It caches the username *
767 * and uid to try to avoid requerying the password files *
768 * or NIS/NIS+ database. */
773 char *cached_username
;
783 current_uid
= getuid();
784 if (current_uid
!= cached_uid
) {
785 cached_uid
= current_uid
;
786 zsfree(cached_username
);
787 if ((pswd
= getpwuid(current_uid
)))
788 cached_username
= ztrdup(pswd
->pw_name
);
790 cached_username
= ztrdup("");
792 #else /* !HAVE_GETPWUID */
793 cached_uid
= getuid();
794 #endif /* !HAVE_GETPWUID */
795 return cached_username
;
798 /* static variables needed by finddir(). */
800 static char *finddir_full
;
801 static Nameddir finddir_last
;
802 static int finddir_best
;
804 /* ScanFunc used by finddir(). */
808 finddir_scan(HashNode hn
, UNUSED(int flags
))
810 Nameddir nd
= (Nameddir
) hn
;
812 if(nd
->diff
> finddir_best
&& !dircmp(nd
->dir
, finddir_full
)
813 && !(nd
->node
.flags
& ND_NOABBREV
)) {
815 finddir_best
=nd
->diff
;
819 /* See if a path has a named directory as its prefix. *
820 * If passed a NULL argument, it will invalidate any *
821 * cached information. */
827 static struct nameddir homenode
= { {NULL
, "", 0}, NULL
, 0 };
829 Shfunc func
= getshfunc("zsh_directory_name");
831 /* Invalidate directory cache if argument is NULL. This is called *
832 * whenever a node is added to or removed from the hash table, and *
833 * whenever the value of $HOME changes. (On startup, too.) */
835 homenode
.dir
= home
? home
: "";
836 homenode
.diff
= home
? strlen(home
) : 0;
840 finddir_full
= zalloc(ffsz
= PATH_MAX
);
842 return finddir_last
= NULL
;
845 /* It's not safe to use the cache while we have function transformations.*/
846 if(!func
&& !strcmp(s
, finddir_full
) && *finddir_full
)
849 if ((int)strlen(s
) >= ffsz
) {
851 finddir_full
= zalloc(ffsz
= strlen(s
) * 2);
853 strcpy(finddir_full
, s
);
856 finddir_scan(&homenode
.node
, 0);
857 scanhashtable(nameddirtab
, 0, 0, 0, finddir_scan
, 0);
860 char *dir_meta
= metafy(finddir_full
, strlen(finddir_full
),
862 char **ares
= subst_string_by_func(func
, "d", dir_meta
);
864 if (ares
&& arrlen(ares
) >= 2 &&
865 (len
= (int)zstrtol(ares
[1], NULL
, 10)) > finddir_best
) {
866 /* better duplicate this string since it's come from REPLY */
867 finddir_last
= (Nameddir
)hcalloc(sizeof(struct nameddir
));
868 finddir_last
->node
.nam
= zhtricat("[", dupstring(ares
[0]), "]");
869 finddir_last
->dir
= dupstrpfx(finddir_full
, len
);
870 finddir_last
->diff
= len
- strlen(finddir_last
->node
.nam
);
873 if (dir_meta
!= finddir_full
)
880 /* add a named directory */
884 adduserdir(char *s
, char *t
, int flags
, int always
)
889 /* We don't maintain a hash table in non-interactive shells. */
893 /* The ND_USERNAME flag means that this possible hash table *
894 * entry is derived from a passwd entry. Such entries are *
895 * subordinate to explicitly generated entries. */
896 if ((flags
& ND_USERNAME
) && nameddirtab
->getnode2(nameddirtab
, s
))
899 /* Normal parameter assignments generate calls to this function, *
900 * with always==0. Unless the AUTO_NAME_DIRS option is set, we *
901 * don't let such assignments actually create directory names. *
902 * Instead, a reference to the parameter as a directory name can *
903 * cause the actual creation of the hash table entry. */
904 if (!always
&& unset(AUTONAMEDIRS
) &&
905 !nameddirtab
->getnode2(nameddirtab
, s
))
908 if (!t
|| *t
!= '/' || strlen(t
) >= PATH_MAX
) {
909 /* We can't use this value as a directory, so simply remove *
910 * the corresponding entry in the hash table, if any. */
911 HashNode hn
= nameddirtab
->removenode(nameddirtab
, s
);
914 nameddirtab
->freenode(hn
);
919 nd
= (Nameddir
) zshcalloc(sizeof *nd
);
920 nd
->node
.flags
= flags
;
921 eptr
= t
+ strlen(t
);
922 while (eptr
> t
&& eptr
[-1] == '/')
926 * Don't abbreviate multiple slashes at the start of a
927 * named directory, since these are sometimes used for
932 nd
->dir
= ztrduppfx(t
, eptr
- t
);
933 /* The variables PWD and OLDPWD are not to be displayed as ~PWD etc. */
934 if (!strcmp(s
, "PWD") || !strcmp(s
, "OLDPWD"))
935 nd
->node
.flags
|= ND_NOABBREV
;
936 nameddirtab
->addnode(nameddirtab
, ztrdup(s
), nd
);
939 /* Get a named directory: this function can cause a directory name *
940 * to be added to the hash table, if it isn't there already. */
944 getnameddir(char *name
)
950 /* Check if it is already in the named directory table */
951 if ((nd
= (Nameddir
) nameddirtab
->getnode(nameddirtab
, name
)))
952 return dupstring(nd
->dir
);
954 /* Check if there is a scalar parameter with this name whose value *
955 * begins with a `/'. If there is, add it to the hash table and *
956 * return the new value. */
957 if ((pm
= (Param
) paramtab
->getnode(paramtab
, name
)) &&
958 (PM_TYPE(pm
->node
.flags
) == PM_SCALAR
) &&
959 (str
= getsparam(name
)) && *str
== '/') {
960 pm
->node
.flags
|= PM_NAMEDDIR
;
961 adduserdir(name
, str
, 0, 1);
967 /* Retrieve an entry from the password table/database for this user. */
969 if ((pw
= getpwnam(name
))) {
970 char *dir
= isset(CHASELINKS
) ? xsymlink(pw
->pw_dir
)
971 : ztrdup(pw
->pw_dir
);
972 adduserdir(name
, dir
, ND_USERNAME
, 1);
973 str
= dupstring(dir
);
978 #endif /* HAVE_GETPWNAM */
980 /* There are no more possible sources of directory names, so give up. */
986 dircmp(char *s
, char *t
)
989 for (; *s
== *t
; s
++, t
++)
992 if (!*s
&& *t
== '/')
999 * Extra functions to call before displaying the prompt.
1000 * The data is a Prepromptfn.
1003 static LinkList prepromptfns
;
1005 /* Add a function to the list of pre-prompt functions. */
1009 addprepromptfn(voidvoidfnptr_t func
)
1011 Prepromptfn ppdat
= (Prepromptfn
)zalloc(sizeof(struct prepromptfn
));
1014 prepromptfns
= znewlinklist();
1015 zaddlinknode(prepromptfns
, ppdat
);
1018 /* Remove a function from the list of pre-prompt functions. */
1022 delprepromptfn(voidvoidfnptr_t func
)
1026 for (ln
= firstnode(prepromptfns
); ln
; ln
= nextnode(ln
)) {
1027 Prepromptfn ppdat
= (Prepromptfn
)getdata(ln
);
1028 if (ppdat
->func
== func
) {
1029 (void)remnode(prepromptfns
, ln
);
1030 zfree(ppdat
, sizeof(struct prepromptfn
));
1035 dputs("BUG: failed to delete node from prepromptfns");
1040 * Functions to call at a particular time even if not at
1041 * the prompt. This is handled by zle. The data is a
1042 * Timedfn. The functions must be in time order, but this
1043 * is enforced by addtimedfn().
1045 * Note on debugging: the code in sched.c currently assumes it's
1046 * the only user of timedfns for the purposes of checking whether
1047 * there's a function on the list. If this becomes no longer the case,
1048 * the DPUTS() tests in sched.c need rewriting.
1052 mod_export LinkList timedfns
;
1054 /* Add a function to the list of timed functions. */
1058 addtimedfn(voidvoidfnptr_t func
, time_t when
)
1060 Timedfn tfdat
= (Timedfn
)zalloc(sizeof(struct timedfn
));
1065 timedfns
= znewlinklist();
1066 zaddlinknode(timedfns
, tfdat
);
1068 LinkNode ln
= firstnode(timedfns
);
1071 * Insert the new element in the linked list. We do
1072 * rather too much work here since the standard
1073 * functions insert after a given node, whereas we
1074 * want to insert the new data before the first element
1075 * with a greater time.
1077 * In practice, the only use of timed functions is
1078 * sched, which only adds the one function; so this
1079 * whole branch isn't used beyond the following block.
1082 zaddlinknode(timedfns
, tfdat
);
1087 LinkNode next
= nextnode(ln
);
1089 zaddlinknode(timedfns
, tfdat
);
1092 tfdat2
= (Timedfn
)getdata(next
);
1093 if (when
< tfdat2
->when
) {
1094 zinsertlinknode(timedfns
, ln
, tfdat
);
1103 * Delete a function from the list of timed functions.
1104 * Note that if the function apperas multiple times only
1105 * the first occurrence will be removed.
1107 * Note also that when zle calls the function it does *not*
1108 * automatically delete the entry from the list. That must
1109 * be done by the function called. This is recommended as otherwise
1110 * the function will keep being called immediately. (It just so
1111 * happens this "feature" fits in well with the only current use
1112 * of timed functions.)
1117 deltimedfn(voidvoidfnptr_t func
)
1121 for (ln
= firstnode(timedfns
); ln
; ln
= nextnode(ln
)) {
1122 Timedfn ppdat
= (Timedfn
)getdata(ln
);
1123 if (ppdat
->func
== func
) {
1124 (void)remnode(timedfns
, ln
);
1125 zfree(ppdat
, sizeof(struct timedfn
));
1130 dputs("BUG: failed to delete node from timedfns");
1134 /* the last time we checked mail */
1137 time_t lastmailcheck
;
1139 /* the last time we checked the people in the WATCH variable */
1145 * Call a function given by "name" with optional arguments
1146 * "lnklist". If these are present the first argument is the function name.
1148 * If "arrayp" is not zero, we also look through
1149 * the array "name"_functions and execute functions found there.
1151 * If "retval" is not NULL, the return value of the first hook function to
1152 * return non-zero is stored in *"retval". The return value is not otherwise
1153 * available as the calling context is restored.
1158 callhookfunc(char *name
, LinkList lnklst
, int arrayp
, int *retval
)
1162 * Save stopmsg, since user doesn't get a chance to respond
1163 * to a list of jobs generated in a hook.
1165 int osc
= sfcontext
, osm
= stopmsg
, stat
= 1, ret
= 0;
1167 sfcontext
= SFC_HOOK
;
1169 if ((shfunc
= getshfunc(name
))) {
1170 ret
= doshfunc(shfunc
, lnklst
, 1);
1176 int namlen
= strlen(name
);
1177 VARARR(char, arrnam
, namlen
+ HOOK_SUFFIX_LEN
);
1178 memcpy(arrnam
, name
, namlen
);
1179 memcpy(arrnam
+ namlen
, HOOK_SUFFIX
, HOOK_SUFFIX_LEN
);
1181 if ((arrptr
= getaparam(arrnam
))) {
1182 for (; *arrptr
; arrptr
++) {
1183 if ((shfunc
= getshfunc(*arrptr
))) {
1184 int newret
= doshfunc(shfunc
, lnklst
, 1);
1201 /* do pre-prompt stuff */
1207 static time_t lastperiodic
;
1209 int period
= getiparam("PERIOD");
1210 int mailcheck
= getiparam("MAILCHECK");
1212 if (isset(PROMPTSP
) && isset(PROMPTCR
) && !use_exit_printed
&& shout
) {
1213 /* The PROMPT_SP heuristic will move the prompt down to a new line
1214 * if there was any dangling output on the line (assuming the terminal
1215 * has automatic margins, but we try even if hasam isn't set).
1216 * Unfortunately it interacts badly with ZLE displaying message
1217 * when ^D has been pressed. So just disable PROMPT_SP logic in
1220 int percents
= opts
[PROMPTPERCENT
];
1221 opts
[PROMPTPERCENT
] = 1;
1222 str
= promptexpand("%B%S%#%s%b", 0, NULL
, NULL
, NULL
);
1223 opts
[PROMPTPERCENT
] = percents
;
1224 fprintf(shout
, "%s%*s\r", str
, (int)columns
- 1 - !hasxn
, "");
1228 /* If NOTIFY is not set, then check for completed *
1229 * jobs before we print the prompt. */
1235 /* If a shell function named "precmd" exists, *
1236 * then execute it. */
1237 callhookfunc("precmd", NULL
, 1, NULL
);
1241 /* If 1) the parameter PERIOD exists, 2) a hook function for *
1242 * "periodic" exists, 3) it's been greater than PERIOD since we *
1243 * executed any such hook, then execute it now. */
1244 if (period
&& (time(NULL
) > lastperiodic
+ period
) &&
1245 !callhookfunc("periodic", NULL
, 1, NULL
))
1246 lastperiodic
= time(NULL
);
1250 /* If WATCH is set, then check for the *
1251 * specified login/logout events. */
1253 if ((int) difftime(time(NULL
), lastwatch
) > getiparam("LOGCHECK")) {
1255 lastwatch
= time(NULL
);
1262 if (mailcheck
&& (int) difftime(time(NULL
), lastmailcheck
) > mailcheck
) {
1265 if (mailpath
&& *mailpath
&& **mailpath
)
1266 checkmailpath(mailpath
);
1269 if ((mailfile
= getsparam("MAIL")) && *mailfile
) {
1278 lastmailcheck
= time(NULL
);
1282 for(ln
= firstnode(prepromptfns
); ln
; ln
= nextnode(ln
)) {
1283 Prepromptfn ppnode
= (Prepromptfn
)getdata(ln
);
1291 checkmailpath(char **s
)
1297 for (v
= *s
; *v
&& *v
!= '?'; v
++);
1306 zerr("empty MAILPATH component: %s", *s
);
1307 } else if (mailstat(unmeta(*s
), &st
) == -1) {
1308 if (errno
!= ENOENT
)
1309 zerr("%e: %s", errno
, *s
);
1310 } else if (S_ISDIR(st
.st_mode
)) {
1312 DIR *lock
= opendir(unmeta(*s
));
1313 char buf
[PATH_MAX
* 2], **arr
, **ap
;
1321 while ((fn
= zreaddir(lock
, 1)) && !errflag
) {
1323 sprintf(buf
, "%s/%s?%s", *s
, fn
, u
);
1325 sprintf(buf
, "%s/%s", *s
, fn
);
1326 addlinknode(l
, dupstring(buf
));
1330 ap
= arr
= (char **) zhalloc(ct
* sizeof(char *));
1332 while ((*ap
++ = (char *)ugetnode(l
)));
1337 if (st
.st_size
&& st
.st_atime
<= st
.st_mtime
&&
1338 st
.st_mtime
> lastmailcheck
) {
1340 fprintf(shout
, "You have new mail.\n");
1344 int uusav
= underscoreused
;
1346 usav
= zalloc(underscoreused
);
1349 memcpy(usav
, underscore
, underscoreused
);
1354 if (! parsestr(u
)) {
1361 setunderscore(usav
);
1366 if (isset(MAILWARNING
) && st
.st_atime
> st
.st_mtime
&&
1367 st
.st_atime
> lastmailcheck
&& st
.st_size
) {
1368 fprintf(shout
, "The mail in %s has been read.\n", unmeta(*s
));
1377 /* This prints the XTRACE prompt. */
1389 int l
, t
= opts
[XTRACE
];
1390 char *s
= dupstring(prompt4
);
1394 s
= unmetafy(promptexpand(metafy(s
, l
, META_NOALLOC
),
1395 0, NULL
, NULL
, NULL
), &l
);
1398 fprintf(xtrerr
, "%s", s
);
1412 gettyinfo(struct ttyinfo
*ti
)
1415 #ifdef HAVE_TERMIOS_H
1416 # ifdef HAVE_TCGETATTR
1417 if (tcgetattr(SHTTY
, &ti
->tio
) == -1)
1419 if (ioctl(SHTTY
, TCGETS
, &ti
->tio
) == -1)
1421 zerr("bad tcgets: %e", errno
);
1423 # ifdef HAVE_TERMIO_H
1424 ioctl(SHTTY
, TCGETA
, &ti
->tio
);
1426 ioctl(SHTTY
, TIOCGETP
, &ti
->sgttyb
);
1427 ioctl(SHTTY
, TIOCLGET
, &ti
->lmodes
);
1428 ioctl(SHTTY
, TIOCGETC
, &ti
->tchars
);
1429 ioctl(SHTTY
, TIOCGLTC
, &ti
->ltchars
);
1437 settyinfo(struct ttyinfo
*ti
)
1440 #ifdef HAVE_TERMIOS_H
1441 # ifdef HAVE_TCGETATTR
1443 # define TCSADRAIN 1 /* XXX Princeton's include files are screwed up */
1445 while (tcsetattr(SHTTY
, TCSADRAIN
, &ti
->tio
) == -1 && errno
== EINTR
)
1448 while (ioctl(SHTTY
, TCSETS
, &ti
->tio
) == -1 && errno
== EINTR
)
1451 /* zerr("settyinfo: %e",errno);*/
1453 # ifdef HAVE_TERMIO_H
1454 ioctl(SHTTY
, TCSETA
, &ti
->tio
);
1456 ioctl(SHTTY
, TIOCSETN
, &ti
->sgttyb
);
1457 ioctl(SHTTY
, TIOCLSET
, &ti
->lmodes
);
1458 ioctl(SHTTY
, TIOCSETC
, &ti
->tchars
);
1459 ioctl(SHTTY
, TIOCSLTC
, &ti
->ltchars
);
1465 /* the default tty state */
1468 mod_export
struct ttyinfo shttyinfo
;
1470 /* != 0 if we need to call resetvideo() */
1473 mod_export
int resetneeded
;
1476 /* window size changed */
1479 mod_export
int winchanged
;
1483 adjustlines(int signalled
)
1485 int oldlines
= lines
;
1488 if (signalled
|| lines
<= 0)
1489 lines
= shttyinfo
.winsize
.ws_row
;
1491 shttyinfo
.winsize
.ws_row
= lines
;
1492 #endif /* TIOCGWINSZ */
1494 DPUTS(signalled
, "BUG: Impossible TIOCGWINSZ rows");
1495 lines
= tclines
> 0 ? tclines
: 24;
1499 termflags
&= ~TERM_SHORT
;
1501 termflags
|= TERM_SHORT
;
1503 return (lines
!= oldlines
);
1507 adjustcolumns(int signalled
)
1509 int oldcolumns
= columns
;
1512 if (signalled
|| columns
<= 0)
1513 columns
= shttyinfo
.winsize
.ws_col
;
1515 shttyinfo
.winsize
.ws_col
= columns
;
1516 #endif /* TIOCGWINSZ */
1518 DPUTS(signalled
, "BUG: Impossible TIOCGWINSZ cols");
1519 columns
= tccolumns
> 0 ? tccolumns
: 80;
1523 termflags
&= ~TERM_NARROW
;
1525 termflags
|= TERM_NARROW
;
1527 return (columns
!= oldcolumns
);
1530 /* check the size of the window and adjust if necessary. *
1531 * The value of from: *
1532 * 0: called from update_job or setupvals *
1533 * 1: called from the SIGWINCH handler *
1534 * 2: called from the LINES parameter callback *
1535 * 3: called from the COLUMNS parameter callback */
1539 adjustwinsize(int from
)
1541 static int getwinsz
= 1;
1543 int ttyrows
= shttyinfo
.winsize
.ws_row
;
1544 int ttycols
= shttyinfo
.winsize
.ws_col
;
1548 if (getwinsz
|| from
== 1) {
1552 if (ioctl(SHTTY
, TIOCGWINSZ
, (char *)&shttyinfo
.winsize
) == 0) {
1553 resetzle
= (ttyrows
!= shttyinfo
.winsize
.ws_row
||
1554 ttycols
!= shttyinfo
.winsize
.ws_col
);
1555 if (from
== 0 && resetzle
&& ttyrows
&& ttycols
)
1556 from
= 1; /* Signal missed while a job owned the tty? */
1557 ttyrows
= shttyinfo
.winsize
.ws_row
;
1558 ttycols
= shttyinfo
.winsize
.ws_col
;
1560 /* Set to value from environment on failure */
1561 shttyinfo
.winsize
.ws_row
= lines
;
1562 shttyinfo
.winsize
.ws_col
= columns
;
1563 resetzle
= (from
== 1);
1566 resetzle
= from
== 1;
1567 #endif /* TIOCGWINSZ */
1575 /* Calling setiparam() here calls this function recursively, but *
1576 * because we've already called adjustlines() and adjustcolumns() *
1577 * here, recursive calls are no-ops unless a signal intervenes. *
1578 * The commented "else return;" above might be a safe shortcut, *
1579 * but I'm concerned about what happens on race conditions; e.g., *
1580 * suppose the user resizes his xterm during `eval $(resize)'? */
1581 if (adjustlines(from
) && zgetenv("LINES"))
1582 setiparam("LINES", lines
);
1583 if (adjustcolumns(from
) && zgetenv("COLUMNS"))
1584 setiparam("COLUMNS", columns
);
1588 resetzle
= adjustlines(0);
1591 resetzle
= adjustcolumns(0);
1596 if (interact
&& from
>= 2 &&
1597 (shttyinfo
.winsize
.ws_row
!= ttyrows
||
1598 shttyinfo
.winsize
.ws_col
!= ttycols
)) {
1599 /* shttyinfo.winsize is already set up correctly */
1600 ioctl(SHTTY
, TIOCSWINSZ
, (char *)&shttyinfo
.winsize
);
1602 #endif /* TIOCGWINSZ */
1604 if (zleactive
&& resetzle
) {
1607 #endif /* TIOCGWINSZ */
1609 zleentry(ZLE_CMD_REFRESH
);
1610 zleentry(ZLE_CMD_RESET_PROMPT
);
1614 /* Move a fd to a place >= 10 and mark the new fd in fdtable. If the fd *
1615 * is already >= 10, it is not moved. If it is invalid, -1 is returned. */
1621 if(fd
!= -1 && fd
< 10) {
1623 int fe
= fcntl(fd
, F_DUPFD
, 10);
1625 int fe
= movefd(dup(fd
));
1631 if (fd
> max_zsh_fd
) {
1632 while (fd
>= fdtable_size
)
1633 fdtable
= zrealloc(fdtable
,
1634 (fdtable_size
*= 2)*sizeof(*fdtable
));
1637 fdtable
[fd
] = FDT_INTERNAL
;
1642 /* Move fd x to y. If x == -1, fd y is closed. */
1651 while (y
>= fdtable_size
)
1652 fdtable
= zrealloc(fdtable
, (fdtable_size
*= 2)*sizeof(*fdtable
));
1654 if ((fdtable
[y
] = fdtable
[x
]) && y
> max_zsh_fd
)
1660 /* Close the given fd, and clear it from fdtable. */
1667 fdtable
[fd
] = FDT_UNUSED
;
1668 while (max_zsh_fd
> 0 && fdtable
[max_zsh_fd
] == FDT_UNUSED
)
1672 if (fd
== coprocout
)
1680 extern char *_mktemp(char *);
1683 /* Get a unique filename for use as a temporary file. If "prefix" is
1684 * NULL, the name is relative to $TMPPREFIX; If it is non-NULL, the
1685 * unique suffix includes a prefixed '.' for improved readability. If
1686 * "use_heap" is true, we allocate the returned name on the heap. */
1690 gettempname(const char *prefix
, int use_heap
)
1692 char *ret
, *suffix
= prefix
? ".XXXXXX" : "XXXXXX";
1695 if (!prefix
&& !(prefix
= getsparam("TMPPREFIX")))
1696 prefix
= DEFAULT_TMPPREFIX
;
1698 ret
= dyncat(unmeta(prefix
), suffix
);
1700 ret
= bicat(unmeta(prefix
), suffix
);
1703 /* Zsh uses mktemp() safely, so silence the warnings */
1704 ret
= (char *) _mktemp(ret
);
1706 ret
= (char *) mktemp(ret
);
1715 gettempfile(const char *prefix
, int use_heap
, char **tempname
)
1720 char *suffix
= prefix
? ".XXXXXX" : "XXXXXX";
1722 if (!prefix
&& !(prefix
= getsparam("TMPPREFIX")))
1723 prefix
= DEFAULT_TMPPREFIX
;
1725 fn
= dyncat(unmeta(prefix
), suffix
);
1727 fn
= bicat(unmeta(prefix
), suffix
);
1739 if (!(fn
= gettempname(prefix
, use_heap
))) {
1743 if ((fd
= open(fn
, O_RDWR
| O_CREAT
| O_EXCL
, 0600)) >= 0)
1748 } while (errno
== EEXIST
&& ++failures
< 16);
1754 /* Check if a string contains a token */
1758 has_token(const char *s
)
1766 /* Delete a character in a string */
1772 while ((str
[0] = str
[1]))
1781 return (isupper(c
) ? tolower(c
) : c
);
1789 return (islower(c
) ? toupper(c
) : c
);
1792 /* copy len chars from t into s, and null terminate */
1796 ztrncpy(char *s
, char *t
, int len
)
1803 /* copy t into *s and update s */
1807 strucpy(char **s
, char *t
)
1811 while ((*u
++ = *t
++));
1817 struncpy(char **s
, char *t
, int n
)
1827 /* Return the number of elements in an array of pointers. *
1828 * It doesn't count the NULL pointer at the end. */
1836 for (count
= 0; *s
; s
++, count
++);
1840 /* Skip over a balanced pair of parenthesis. */
1844 skipparens(char inpar
, char outpar
, char **s
)
1851 for (level
= 1; *++*s
&& level
;)
1854 else if (**s
== outpar
)
1860 /* Convert string to zlong (see zsh.h). This function (without the z) *
1861 * is contained in the ANSI standard C library, but a lot of them seem *
1866 zstrtol(const char *s
, char **t
, int base
)
1868 const char *inp
, *trunc
= NULL
;
1869 zulong calc
= 0, newcalc
= 0;
1875 if ((neg
= (*s
== '-')))
1883 else if (*++s
== 'x' || *s
== 'X')
1889 if (base
< 2 || base
> 36) {
1890 zerr("invalid base (must be 2 to 36 inclusive): %d", base
);
1892 } else if (base
<= 10)
1893 for (; *s
>= '0' && *s
< ('0' + base
); s
++) {
1896 newcalc
= calc
* base
+ *s
- '0';
1905 for (; idigit(*s
) || (*s
>= 'a' && *s
< ('a' + base
- 10))
1906 || (*s
>= 'A' && *s
< ('A' + base
- 10)); s
++) {
1909 newcalc
= calc
*base
+ (idigit(*s
) ? (*s
- '0') : (*s
& 0x1f) + 9);
1919 * Special case: check for a number that was just too long for
1921 * Extra special case: the lowest negative number would trigger
1922 * the first test, but is actually representable correctly.
1923 * This is a 1 in the top bit, all others zero, so test for
1926 if (!trunc
&& (zlong
)calc
< 0 &&
1927 (!neg
|| calc
& ~((zulong
)1 << (8*sizeof(zulong
)-1))))
1934 zwarn("number truncated after %d digits: %s", (int)(trunc
- inp
), inp
);
1938 return neg
? -(zlong
)calc
: (zlong
)calc
;
1943 setblock_fd(int turnonblocking
, int fd
, long *modep
)
1947 # define NONBLOCK (O_NDELAY|O_NONBLOCK)
1948 # else /* !O_NONBLOCK */
1949 # define NONBLOCK O_NDELAY
1950 # endif /* !O_NONBLOCK */
1951 #else /* !O_NDELAY */
1953 # define NONBLOCK O_NONBLOCK
1954 # else /* !O_NONBLOCK */
1956 # endif /* !O_NONBLOCK */
1957 #endif /* !O_NDELAY */
1962 if (!fstat(fd
, &st
) && !S_ISREG(st
.st_mode
)) {
1963 *modep
= fcntl(fd
, F_GETFL
, 0);
1965 if (!turnonblocking
) {
1966 /* We want to know if blocking was off */
1967 if ((*modep
& NONBLOCK
) ||
1968 !fcntl(fd
, F_SETFL
, *modep
| NONBLOCK
))
1970 } else if ((*modep
& NONBLOCK
) &&
1971 !fcntl(fd
, F_SETFL
, *modep
& ~NONBLOCK
)) {
1972 /* Here we want to know if the state changed */
1977 #endif /* NONBLOCK */
1986 setblock_stdin(void)
1989 return setblock_fd(1, 0, &mode
);
1993 * Check for pending input on fd. If polltty is set, we may need to
1994 * use termio to look for input. As a final resort, go to non-blocking
1995 * input and try to read a character, which in this case will be
1996 * returned in *readchar.
1998 * Note that apart from setting (and restoring) non-blocking input,
1999 * this function does not change the input mode. The calling function
2000 * should have set cbreak mode if necessary.
2005 read_poll(int fd
, int *readchar
, int polltty
, zlong microseconds
)
2012 struct timeval expire_tv
;
2023 #if defined(HAS_TIO) && !defined(__CYGWIN__)
2025 * Under Solaris, at least, reading from the terminal in non-canonical
2026 * mode requires that we use the VMIN mechanism to poll. Any attempt
2027 * to check any other way, or to set the terminal to non-blocking mode
2028 * and poll that way, fails; it will just for canonical mode input.
2029 * We should probably use this mechanism if the user has set non-canonical
2030 * mode, in which case testing here for isatty() and ~ICANON would be
2031 * better than testing whether bin_read() set it, but for now we've got
2034 * Under Cygwin, you won't be surprised to here, this mechanism,
2035 * although present, doesn't work, and we *have* to use ordinary
2036 * non-blocking reads to find out if there is a character present
2037 * in non-canonical mode.
2039 * I am assuming Solaris is nearer the UNIX norm. This is not necessarily
2040 * as plausible as it sounds, but it seems the right way to guess.
2045 if ((polltty
= ti
.tio
.c_cc
[VMIN
])) {
2046 ti
.tio
.c_cc
[VMIN
] = 0;
2047 /* termios timeout is 10ths of a second */
2048 ti
.tio
.c_cc
[VTIME
] = (int) (microseconds
/ (zlong
)100000);
2056 expire_tv
.tv_sec
= (int) (microseconds
/ (zlong
)1000000);
2057 expire_tv
.tv_usec
= microseconds
% (zlong
)1000000;
2060 ret
= select(fd
+1, (SELECT_ARG_2_T
) &foofd
, NULL
, NULL
, &expire_tv
);
2063 if (ioctl(fd
, FIONREAD
, (char *) &val
) == 0)
2070 * Final attempt: set non-blocking read and try to read a character.
2071 * Praise Bill, this works under Cygwin (nothing else seems to).
2073 if ((polltty
|| setblock_fd(0, fd
, &mode
)) && read(fd
, &c
, 1) > 0) {
2078 fcntl(fd
, F_SETFL
, mode
);
2082 ti
.tio
.c_cc
[VMIN
] = 1;
2083 ti
.tio
.c_cc
[VTIME
] = 0;
2096 fprintf(shout
, "zsh: sure you want to delete all the files in ");
2098 nicezputs(pwd
[1] ? unmeta(pwd
) : "", shout
);
2101 nicezputs(s
, shout
);
2102 if(isset(RMSTARWAIT
)) {
2103 fputs("? (waiting ten seconds)", shout
);
2109 fputs(" [yn]? ", shout
);
2112 return (getquery("ny", 1) == 'y');
2120 while (read(SHTTY
, &c
, 1) != 1) {
2121 if (errno
!= EINTR
|| errflag
|| retflag
|| breaks
|| contflag
)
2125 write(SHTTY
, &c
, 1);
2138 ioctl(SHTTY
, FIONREAD
, (char *)&val
);
2150 getquery(char *valid_chars
, int purge
)
2153 int isem
= !strcmp(term
, "emacs");
2160 ti
.tio
.c_lflag
&= ~ECHO
;
2162 ti
.tio
.c_lflag
&= ~ICANON
;
2163 ti
.tio
.c_cc
[VMIN
] = 1;
2164 ti
.tio
.c_cc
[VTIME
] = 0;
2167 ti
.sgttyb
.sg_flags
&= ~ECHO
;
2169 ti
.sgttyb
.sg_flags
|= CBREAK
;
2173 if (noquery(purge
)) {
2175 settyinfo(&shttyinfo
);
2176 write(SHTTY
, "n\n", 2);
2180 while ((c
= read1char(0)) >= 0) {
2192 if (strchr(valid_chars
, c
)) {
2199 write(SHTTY
, &c
, 1);
2201 write(SHTTY
, "\n", 1);
2205 while ((d
= read1char(1)) >= 0 && d
!= '\n');
2207 if (c
!= '\n' && !valid_chars
) {
2208 #ifdef MULTIBYTE_SUPPORT
2209 if (isset(MULTIBYTE
) && c
>= 0) {
2211 * No waiting for a valid character, and no draining;
2212 * we should ensure we haven't stopped in the middle
2213 * of a multibyte character.
2217 memset(&mbs
, 0, sizeof(mbs
));
2219 size_t ret
= mbrlen(&cc
, 1, &mbs
);
2221 if (ret
!= MB_INCOMPLETE
)
2230 write(SHTTY
, "\n", 1);
2233 settyinfo(&shttyinfo
);
2238 static char *guess
, *best
;
2242 spscan(HashNode hn
, UNUSED(int scanflags
))
2246 nd
= spdist(hn
->nam
, guess
, (int) strlen(guess
) / 4 + 1);
2253 /* spellcheck a word */
2254 /* fix s ; if hist is nonzero, fix the history list too */
2258 spckword(char **s
, int hist
, int cmd
, int ask
)
2265 int autocd
= cmd
&& isset(AUTOCD
) && strcmp(*s
, ".") && strcmp(*s
, "..");
2267 if ((histdone
& HISTFLAG_NOEXEC
) || **s
== '-' || **s
== '%')
2269 if (!strcmp(*s
, "in"))
2271 if (!(*s
)[0] || !(*s
)[1])
2273 if (shfunctab
->getnode(shfunctab
, *s
) ||
2274 builtintab
->getnode(builtintab
, *s
) ||
2275 cmdnamtab
->getnode(cmdnamtab
, *s
) ||
2276 aliastab
->getnode(aliastab
, *s
) ||
2277 reswdtab
->getnode(reswdtab
, *s
))
2279 else if (isset(HASHLISTALL
)) {
2280 cmdnamtab
->filltable(cmdnamtab
);
2281 if (cmdnamtab
->getnode(cmdnamtab
, *s
))
2285 if (*t
== Tilde
|| *t
== Equals
|| *t
== String
)
2291 for (t
= *s
; *t
; t
++)
2294 if (**s
== Tilde
&& !*t
)
2296 if (**s
== String
&& !*t
) {
2298 if (itype_end(guess
, IIDENT
, 1) == guess
)
2302 scanhashtable(paramtab
, 1, 0, 0, spscan
, 0);
2303 } else if (**s
== Equals
) {
2306 if (hashcmd(guess
= *s
+ 1, pathchecked
))
2310 scanhashtable(aliastab
, 1, 0, 0, spscan
, 0);
2311 scanhashtable(cmdnamtab
, 1, 0, 0, spscan
, 0);
2314 if (*guess
== Tilde
|| *guess
== String
) {
2318 guess
= dupstring(guess
);
2325 preflen
= strlen(guess
) - strlen(t
);
2327 if (access(unmeta(guess
), F_OK
) == 0)
2329 best
= spname(guess
);
2331 if (hashcmd(guess
, pathchecked
))
2334 scanhashtable(reswdtab
, 1, 0, 0, spscan
, 0);
2335 scanhashtable(aliastab
, 1, 0, 0, spscan
, 0);
2336 scanhashtable(shfunctab
, 1, 0, 0, spscan
, 0);
2337 scanhashtable(builtintab
, 1, 0, 0, spscan
, 0);
2338 scanhashtable(cmdnamtab
, 1, 0, 0, spscan
, 0);
2341 for (pp
= cdpath
; *pp
; pp
++) {
2342 char bestcd
[PATH_MAX
+ 1];
2344 /* Less than d here, instead of less than or equal *
2345 * as used in spscan(), so that an autocd is chosen *
2346 * only when it is better than anything so far, and *
2347 * so we prefer directories earlier in the cdpath. */
2348 if ((thisdist
= mindist(*pp
, *s
, bestcd
)) < d
) {
2349 best
= dupstring(bestcd
);
2358 if (best
&& (int)strlen(best
) > 1 && strcmp(best
, guess
)) {
2362 /* do not correct the result of an expansion */
2363 if (strncmp(guess
, best
, preflen
))
2365 /* replace the temporarily expanded prefix with the original */
2366 u
= (char *) hcalloc(t
- *s
+ strlen(best
+ preflen
) + 1);
2367 strncpy(u
, *s
, t
- *s
);
2368 strcpy(u
+ (t
- *s
), best
+ preflen
);
2370 u
= (char *) hcalloc(strlen(best
) + 2);
2371 strcpy(u
+ 1, best
);
2375 *guess
= *best
= ztokens
[ic
- Pound
];
2382 pptbuf
= promptexpand(sprompt
, 0, best
, guess
, NULL
);
2383 zputs(pptbuf
, shout
);
2387 x
= getquery("nyae \t", 0);
2392 if (x
== 'y' || x
== ' ' || x
== '\t') {
2393 *s
= dupstring(best
);
2396 } else if (x
== 'a') {
2397 histdone
|= HISTFLAG_NOEXEC
;
2398 } else if (x
== 'e') {
2399 histdone
|= HISTFLAG_NOEXEC
| HISTFLAG_RECALL
;
2407 * Helper for ztrftime. Called with a pointer to the length left
2408 * in the buffer, and a new string length to decrement from that.
2409 * Returns 0 if the new length fits, 1 otherwise. We assume a terminating
2410 * NUL and return 1 if that doesn't fit.
2414 ztrftimebuf(int *bufsizeptr
, int decr
)
2416 if (*bufsizeptr
<= decr
)
2418 *bufsizeptr
-= decr
;
2423 * Like the system function, this returns the number of characters
2424 * copied, not including the terminating NUL. This may be zero
2425 * if the string didn't fit.
2427 * As an extension, try to detect an error in strftime --- typically
2428 * not enough memory --- and return -1. Not guaranteed to be portable,
2429 * since the strftime() interface doesn't make any guarantees about
2430 * the state of the buffer if it returns zero.
2435 ztrftime(char *buf
, int bufsize
, char *fmt
, struct tm
*tm
)
2438 #ifndef HAVE_STRFTIME
2439 static char *astr
[] =
2440 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
2441 static char *estr
[] =
2442 {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
2443 "Aug", "Sep", "Oct", "Nov", "Dec"};
2445 char *origbuf
= buf
;
2460 * Assume this format will take up at least two
2461 * characters. Not always true, but if that matters
2462 * we are so close to the edge it's not a big deal.
2463 * Fix up some longer cases specially when we get to them.
2465 if (ztrftimebuf(&bufsize
, 2))
2469 if (tm
->tm_mday
> 9 || !strip
)
2470 *buf
++ = '0' + tm
->tm_mday
/ 10;
2471 *buf
++ = '0' + tm
->tm_mday
% 10;
2477 if (tm
->tm_mday
> 9)
2478 *buf
++ = '0' + tm
->tm_mday
/ 10;
2481 *buf
++ = '0' + tm
->tm_mday
% 10;
2488 if (tm
->tm_hour
> 9)
2489 *buf
++ = '0' + tm
->tm_hour
/ 10;
2496 *buf
++ = '0' + tm
->tm_hour
% 10;
2502 hr12
= tm
->tm_hour
% 12;
2510 *buf
++ = '0' + (hr12
% 10);
2513 if (tm
->tm_mon
> 8 || !strip
)
2514 *buf
++ = '0' + (tm
->tm_mon
+ 1) / 10;
2515 *buf
++ = '0' + (tm
->tm_mon
+ 1) % 10;
2518 if (tm
->tm_min
> 9 || !strip
)
2519 *buf
++ = '0' + tm
->tm_min
/ 10;
2520 *buf
++ = '0' + tm
->tm_min
% 10;
2523 if (tm
->tm_sec
> 9 || !strip
)
2524 *buf
++ = '0' + tm
->tm_sec
/ 10;
2525 *buf
++ = '0' + tm
->tm_sec
% 10;
2528 if (tm
->tm_year
> 9 || !strip
)
2529 *buf
++ = '0' + (tm
->tm_year
/ 10) % 10;
2530 *buf
++ = '0' + tm
->tm_year
% 10;
2533 /* Guard against premature end of string */
2537 #ifndef HAVE_STRFTIME
2541 * Not worth handling this natively if
2544 int year
, digits
, testyear
;
2545 year
= tm
->tm_year
+ 1900;
2548 while (testyear
> 9) {
2552 if (ztrftimebuf(&bufsize
, digits
))
2554 sprintf(buf
, "%d", year
);
2559 if (ztrftimebuf(&bufsize
, strlen(astr
[tm
->tm_wday
]) - 2))
2561 strucpy(&buf
, astr
[tm
->tm_wday
]);
2564 if (ztrftimebuf(&bufsize
, strlen(estr
[tm
->tm_mon
]) - 2))
2566 strucpy(&buf
, estr
[tm
->tm_mon
]);
2569 *buf
++ = (tm
->tm_hour
> 11) ? 'p' : 'a';
2579 * Remember we've already allowed for two characters
2580 * in the accounting in bufsize (but nowhere else).
2583 sprintf(tmp
, strip
? "%%-%c" : "%%%c", fmt
[-1]);
2584 if (!strftime(buf
, bufsize
+ 2, tmp
, tm
))
2594 bufsize
-= decr
- 2;
2599 if (ztrftimebuf(&bufsize
, 1))
2604 return buf
- origbuf
;
2609 zjoin(char **arr
, int delim
, int heap
)
2612 char **s
, *ret
, *ptr
;
2614 for (s
= arr
; *s
; s
++)
2615 len
+= strlen(*s
) + 1 + (imeta(delim
) ? 1 : 0);
2617 return heap
? "" : ztrdup("");
2618 ptr
= ret
= (heap
? (char *) hcalloc(len
) : (char *) zshcalloc(len
));
2619 for (s
= arr
; *s
; s
++) {
2623 *ptr
++ = delim
^ 32;
2628 ptr
[-1 - (imeta(delim
) ? 1 : 0)] = '\0';
2632 /* Split a string containing a colon separated list *
2633 * of items into an array of strings. */
2637 colonsplit(char *s
, int uniq
)
2640 char *t
, **ret
, **ptr
, **p
;
2642 for (t
= s
, ct
= 0; *t
; t
++) /* count number of colons */
2645 ptr
= ret
= (char **) zalloc(sizeof(char **) * (ct
+ 2));
2650 /* move t to point at next colon */
2651 for (; *t
&& *t
!= ':'; t
++);
2653 for (p
= ret
; p
< ptr
; p
++)
2654 if ((int)strlen(*p
) == t
- s
&& ! strncmp(*p
, s
, t
- s
))
2656 *ptr
= (char *) zalloc((t
- s
) + 1);
2657 ztrncpy(*ptr
++, s
, t
- s
);
2673 * Don't need to handle mutlibyte characters, they can't
2674 * be IWSEP. Do need to check for metafication.
2676 while (*t
&& iwsep(*t
== Meta
? t
[1] ^ 32 : *t
)) {
2687 * haven't worked out what allownull does; it's passed down from
2688 * sepsplit but all the cases it's used are either 0 or 1 without
2689 * a comment. it seems to be something to do with the `nulstring'
2690 * which i think is some kind of a metafication thing, so probably
2691 * allownull's value is associated with whether we are using
2693 * see findsep() below for handling of `quote' argument
2698 spacesplit(char *s
, int allownull
, int heap
, int quote
)
2700 char *t
, **ret
, **ptr
;
2701 int l
= sizeof(*ret
) * (wordcount(s
, NULL
, -!allownull
) + 1);
2702 char *(*dup
)(const char *) = (heap
? dupstring
: ztrdup
);
2704 ptr
= ret
= (heap
? (char **) hcalloc(l
) : (char **) zshcalloc(l
));
2708 * we will be stripping quoted separators by hacking string,
2709 * so make sure it's hackable.
2717 if (*s
&& itype_end(s
, ISEP
, 1) != s
)
2718 *ptr
++ = dup(allownull
? "" : nulstring
);
2719 else if (!allownull
&& t
!= s
)
2722 char *iend
= itype_end(s
, ISEP
, 1);
2727 else if (quote
&& *s
== '\\') {
2732 (void)findsep(&s
, NULL
, quote
);
2733 if (s
> t
|| allownull
) {
2734 *ptr
= (heap
? (char *) hcalloc((s
- t
) + 1) :
2735 (char *) zshcalloc((s
- t
) + 1));
2736 ztrncpy(*ptr
++, t
, s
- t
);
2738 *ptr
++ = dup(nulstring
);
2742 if (!allownull
&& t
!= s
)
2749 * Find a separator. Return 0 if already at separator, 1 if separator
2750 * found later, else -1. (Historical note: used to return length into
2751 * string but this is all that is necessary and is less ambiguous with
2752 * multibyte characters around.)
2754 * *s is the string we are looking along, which will be updated
2755 * to the point we have got to.
2757 * sep is a possibly multicharacter separator to look for. If NULL,
2758 * use normal separator characters. If *sep is NULL, split on individual
2761 * quote is a flag that '\<sep>' should not be treated as a separator.
2762 * in this case we need to be able to strip the backslash directly
2763 * in the string, so the calling function must have sent us something
2764 * modifiable. currently this only works for sep == NULL. also in
2765 * in this case only, we need to turn \\ into \.
2770 findsep(char **s
, char *sep
, int quote
)
2780 for (t
= *s
; *t
; t
+= ilen
) {
2781 if (quote
&& *t
== '\\') {
2787 ilen
= MB_METACHARLENCONV(t
+1, &c
);
2788 if (WC_ZISTYPE(c
, ISEP
)) {
2790 /* then advance over new character, length ilen */
2792 /* treat *t (backslash) as normal byte */
2799 ilen
= MB_METACHARLENCONV(t
, &c
);
2800 if (WC_ZISTYPE(c
, ISEP
))
2810 * NULL separator just means advance past first character,
2814 *s
+= MB_METACHARLEN(*s
);
2819 for (i
= 0; **s
; i
++) {
2821 * The following works for multibyte characters by virtue of
2822 * the fact that sep may be a string (and we don't care how
2823 * it divides up, we need to match all of it).
2825 for (t
= sep
, tt
= *s
; *t
&& *tt
&& *t
== *tt
; t
++, tt
++);
2828 *s
+= MB_METACHARLEN(*s
);
2835 findword(char **s
, char *sep
)
2846 while (! findsep(s
, sep
, 0)) {
2852 for (t
= *s
; *t
; t
+= sl
) {
2854 sl
= MB_METACHARLENCONV(t
, &c
);
2855 if (!WC_ZISTYPE(c
, ISEP
))
2859 (void)findsep(s
, sep
, 0);
2865 wordcount(char *s
, char *sep
, int mul
)
2872 for (; (c
= findsep(&s
, sep
, 0)) >= 0; s
+= sl
)
2873 if ((c
&& *(s
+ sl
)) || mul
)
2881 if ((*s
&& itype_end(s
, ISEP
, 1) != s
) ||
2882 (mul
< 0 && t
!= s
))
2885 char *ie
= itype_end(s
, ISEP
, 1);
2891 (void)findsep(&s
, NULL
, 0);
2896 if (mul
< 0 && t
!= s
)
2904 sepjoin(char **s
, char *sep
, int heap
)
2911 return heap
? "" : ztrdup("");
2913 /* optimise common case that ifs[0] is space */
2914 if (ifs
&& *ifs
!= ' ') {
2916 sep
= dupstrpfx(ifs
, MB_METACHARLEN(ifs
));
2924 for (t
= s
, l
= 1 - sl
; *t
; l
+= strlen(*t
) + sl
, t
++);
2925 r
= p
= (heap
? (char *) hcalloc(l
) : (char *) zshcalloc(l
));
2938 sepsplit(char *s
, char *sep
, int allownull
, int heap
)
2941 char *t
, *tt
, **r
, **p
;
2944 return spacesplit(s
, allownull
, heap
, 0);
2947 n
= wordcount(s
, sep
, 1);
2948 r
= p
= (heap
? (char **) hcalloc((n
+ 1) * sizeof(char *)) :
2949 (char **) zshcalloc((n
+ 1) * sizeof(char *)));
2953 (void)findsep(&t
, sep
, 0);
2954 *p
= (heap
? (char *) hcalloc(t
- tt
+ 1) :
2955 (char *) zshcalloc(t
- tt
+ 1));
2956 strncpy(*p
, tt
, t
- tt
);
2957 (*p
)[t
- tt
] = '\0';
2966 /* Get the definition of a shell function */
2970 getshfunc(char *nam
)
2972 return (Shfunc
) shfunctab
->getnode(shfunctab
, nam
);
2976 * Call the function func to substitute string orig by setting
2977 * the parameter reply.
2978 * Return the array from reply, or NULL if the function returned
2980 * The returned value comes directly from the parameter and
2981 * so should be used before there is any chance of that
2982 * being changed or unset.
2983 * If arg1 is not NULL, it is used as an initial argument to
2984 * the function, with the original string as the second argument.
2989 subst_string_by_func(Shfunc func
, char *arg1
, char *orig
)
2991 int osc
= sfcontext
;
2992 LinkList l
= newlinklist();
2995 addlinknode(l
, func
->node
.nam
);
2997 addlinknode(l
, arg1
);
2998 addlinknode(l
, orig
);
2999 sfcontext
= SFC_SUBST
;
3001 if (doshfunc(func
, l
, 1))
3004 ret
= getaparam("reply");
3014 char **t
= (char **) zalloc((s
) ? (2 * sizeof s
) : (sizeof s
));
3027 if ((vb
= getsparam("ZBEEP"))) {
3029 vb
= getkeystring(vb
, &len
, GETKEYS_BINDKEY
, NULL
);
3030 write(SHTTY
, vb
, len
);
3031 } else if (isset(BEEP
))
3032 write(SHTTY
, "\07", 1);
3042 DPUTS(!s
, "freearray() with zero argument");
3051 equalsplit(char *s
, char **t
)
3053 for (; *s
&& *s
!= '='; s
++);
3062 static int specialcomma
;
3064 /* the ztypes table */
3067 mod_export
short int typtab
[256];
3069 /* initialize the ztypes table */
3078 for (t0
= 0; t0
!= 256; t0
++)
3080 for (t0
= 0; t0
!= 32; t0
++)
3081 typtab
[t0
] = typtab
[t0
+ 128] = ICNTRL
;
3082 typtab
[127] = ICNTRL
;
3083 for (t0
= '0'; t0
<= '9'; t0
++)
3084 typtab
[t0
] = IDIGIT
| IALNUM
| IWORD
| IIDENT
| IUSER
;
3085 for (t0
= 'a'; t0
<= 'z'; t0
++)
3086 typtab
[t0
] = typtab
[t0
- 'a' + 'A'] = IALPHA
| IALNUM
| IIDENT
| IUSER
| IWORD
;
3087 #ifndef MULTIBYTE_SUPPORT
3089 * This really doesn't seem to me the right thing to do when
3090 * we have multibyte character support... it was a hack to assume
3091 * eight bit characters `worked' for some values of work before
3092 * we could test for them properly. I'm not 100% convinced
3093 * having IIDENT here is a good idea at all, but this code
3094 * should disappear into history...
3096 for (t0
= 0240; t0
!= 0400; t0
++)
3097 typtab
[t0
] = IALPHA
| IALNUM
| IIDENT
| IUSER
| IWORD
;
3099 /* typtab['.'] |= IIDENT; */ /* Allow '.' in variable names - broken */
3100 typtab
['_'] = IIDENT
| IUSER
;
3101 typtab
['-'] = typtab
['.'] = IUSER
;
3102 typtab
[' '] |= IBLANK
| INBLANK
;
3103 typtab
['\t'] |= IBLANK
| INBLANK
;
3104 typtab
['\n'] |= INBLANK
;
3105 typtab
['\0'] |= IMETA
;
3106 typtab
[STOUC(Meta
) ] |= IMETA
;
3107 typtab
[STOUC(Marker
)] |= IMETA
;
3108 for (t0
= (int)STOUC(Pound
); t0
<= (int)STOUC(Comma
); t0
++)
3109 typtab
[t0
] |= ITOK
| IMETA
;
3110 for (t0
= (int)STOUC(Snull
); t0
<= (int)STOUC(Nularg
); t0
++)
3111 typtab
[t0
] |= ITOK
| IMETA
| INULL
;
3112 for (s
= ifs
? ifs
: DEFAULT_IFS
; *s
; s
++) {
3113 int c
= STOUC(*s
== Meta
? *++s
^ 32 : *s
);
3114 #ifdef MULTIBYTE_SUPPORT
3116 /* see comment for wordchars below */
3128 for (s
= wordchars
? wordchars
: DEFAULT_WORDCHARS
; *s
; s
++) {
3129 int c
= STOUC(*s
== Meta
? *++s
^ 32 : *s
);
3130 #ifdef MULTIBYTE_SUPPORT
3133 * If we have support for multibyte characters, we don't
3134 * handle non-ASCII characters here; instead, we turn
3135 * wordchars into a wide character array.
3136 * (We may actually have a single-byte 8-bit character set,
3137 * but it works the same way.)
3144 #ifdef MULTIBYTE_SUPPORT
3145 set_widearray(wordchars
, &wordchars_wide
);
3146 set_widearray(ifs
, &ifs_wide
);
3148 for (s
= SPECCHARS
; *s
; s
++)
3149 typtab
[STOUC(*s
)] |= ISPECIAL
;
3151 typtab
[STOUC(',')] |= ISPECIAL
;
3152 if (isset(BANGHIST
) && bangchar
&& interact
&& isset(SHINSTDIN
))
3153 typtab
[bangchar
] |= ISPECIAL
;
3158 makecommaspecial(int yesno
)
3160 if ((specialcomma
= yesno
) != 0)
3161 typtab
[STOUC(',')] |= ISPECIAL
;
3163 typtab
[STOUC(',')] &= ~ISPECIAL
;
3168 #ifdef MULTIBYTE_SUPPORT
3169 /* A wide-character version of the iblank() macro. */
3172 wcsiblank(wint_t wc
)
3174 if (iswspace(wc
) && wc
!= L
'\n')
3180 * zistype macro extended to support wide characters.
3181 * Works for IIDENT, IWORD, IALNUM, ISEP.
3182 * We don't need this for IWSEP because that only applies to
3183 * a fixed set of ASCII characters.
3184 * Note here that use of multibyte mode is not tested:
3185 * that's because for ZLE this is unconditional,
3186 * not dependent on the option. The caller must decide.
3191 wcsitype(wchar_t c
, int itype
)
3195 VARARR(char, outstr
, MB_CUR_MAX
);
3197 if (!isset(MULTIBYTE
))
3198 return zistype(c
, itype
);
3201 * Strategy: the shell requires that the multibyte representation
3202 * be an extension of ASCII. So see if converting the character
3203 * produces an ASCII character. If it does, use zistype on that.
3204 * If it doesn't, use iswalnum on the original character.
3205 * If that fails, resort to the appropriate wide character array.
3207 memset(&mbs
, 0, sizeof(mbs
));
3208 len
= wcrtomb(outstr
, c
, &mbs
);
3211 /* NULL is special */
3212 return zistype(0, itype
);
3213 } else if (len
== 1 && isascii(outstr
[0])) {
3214 return zistype(outstr
[0], itype
);
3218 if (!isset(POSIXIDENTIFIERS
))
3226 * If we are handling combining characters, any punctuation
3227 * characters with zero width needs to be considered part of
3228 * a word. If we are not handling combining characters then
3229 * logically they are still part of the word, even if they
3230 * don't get displayed properly, so always do this.
3232 if (IS_COMBINING(c
))
3234 return !!wmemchr(wordchars_wide
.chars
, c
, wordchars_wide
.len
);
3237 return !!wmemchr(ifs_wide
.chars
, c
, ifs_wide
.len
);
3250 * Find the end of a set of characters in the set specified by itype;
3251 * one of IALNUM, IIDENT, IWORD or IUSER. For non-ASCII characters, we assume
3252 * alphanumerics are part of the set, with the exception that
3253 * identifiers are not treated that way if POSIXIDENTIFIERS is set.
3255 * See notes above for identifiers.
3256 * Returns the same pointer as passed if not on an identifier character.
3257 * If "once" is set, just test the first character, i.e. (outptr !=
3258 * inptr) tests whether the first character is valid in an identifier.
3260 * Currently this is only called with itype IIDENT, IUSER or ISEP.
3265 itype_end(const char *ptr
, int itype
, int once
)
3267 #ifdef MULTIBYTE_SUPPORT
3268 if (isset(MULTIBYTE
) &&
3269 (itype
!= IIDENT
|| !isset(POSIXIDENTIFIERS
))) {
3273 int len
= mb_metacharlenconv(ptr
, &wc
);
3279 /* invalid, treat as single character */
3280 int chr
= STOUC(*ptr
== Meta
? ptr
[1] ^ 32 : *ptr
);
3281 /* in this case non-ASCII characters can't match */
3282 if (chr
> 127 || !zistype(chr
,itype
))
3284 } else if (len
== 1 && isascii(*ptr
)) {
3285 /* ASCII: can't be metafied, use standard test */
3286 if (!zistype(*ptr
,itype
))
3290 * Valid non-ASCII character.
3294 if (!iswalnum(wc
) &&
3295 !wmemchr(wordchars_wide
.chars
, wc
,
3296 wordchars_wide
.len
))
3301 if (!wmemchr(ifs_wide
.chars
, wc
, ifs_wide
.len
))
3318 int chr
= STOUC(*ptr
== Meta
? ptr
[1] ^ 32 : *ptr
);
3319 if (!zistype(chr
,itype
))
3321 ptr
+= (*ptr
== Meta
) ? 2 : 1;
3328 * Nasty. The first argument is const char * because we
3329 * don't modify it here. However, we really want to pass
3330 * back the same type as was passed down, to allow idioms like
3331 * p = itype_end(p, IIDENT, 0);
3332 * So returning a const char * isn't really the right thing to do.
3333 * Without having two different functions the following seems
3334 * to be the best we can do.
3345 y
= x
= (char **) zhalloc(sizeof(char *) * (arrlen(s
) + 1));
3347 while ((*x
++ = dupstring(*s
++)));
3358 y
= x
= (char **) zalloc(sizeof(char *) * (arrlen(s
) + 1));
3360 while ((*x
++ = ztrdup(*s
++)));
3366 #ifdef MULTIBYTE_SUPPORT
3368 mod_export
wchar_t **
3369 wcs_zarrdup(wchar_t **s
)
3373 y
= x
= (wchar_t **) zalloc(sizeof(wchar_t *) * (arrlen((char **)s
) + 1));
3375 while ((*x
++ = wcs_ztrdup(*s
++)));
3380 #endif /* MULTIBYTE_SUPPORT */
3384 spname(char *oldname
)
3386 char *p
, spnameguess
[PATH_MAX
+ 1], spnamebest
[PATH_MAX
+ 1];
3387 static char newname
[PATH_MAX
+ 1];
3388 char *new = newname
, *old
= oldname
;
3389 int bestdist
= 0, thisdist
, thresh
, maxthresh
= 0;
3391 /* This loop corrects each directory component of the path, stopping *
3392 * when any correction distance would exceed the distance threshold. *
3393 * NULL is returned only if the first component cannot be corrected; *
3394 * otherwise a copy of oldname with a corrected prefix is returned. *
3395 * Rationale for this, if there ever was any, has been forgotten. */
3403 for (; *old
!= '/' && *old
!= '\0'; old
++)
3404 if (p
< spnameguess
+ PATH_MAX
)
3407 /* Every component is allowed a single distance 2 correction or two *
3408 * distance 1 corrections. Longer ones get additional corrections. */
3409 thresh
= (int)(p
- spnameguess
) / 4 + 1;
3412 if ((thisdist
= mindist(newname
, spnameguess
, spnamebest
)) >= thresh
) {
3413 /* The next test is always true, except for the first path *
3414 * component. We could initialize bestdist to some large *
3415 * constant instead, and then compare to that constant here, *
3416 * because an invariant is that we've never exceeded the *
3417 * threshold for any component so far; but I think that looks *
3418 * odd to the human reader, and we may make use of the total *
3419 * distance for all corrections at some point in the future. */
3420 if (bestdist
< maxthresh
) {
3421 strcpy(new, spnameguess
);
3427 maxthresh
= bestdist
+ thresh
;
3428 bestdist
+= thisdist
;
3430 for (p
= spnamebest
; (*new = *p
++);)
3437 mindist(char *dir
, char *mindistguess
, char *mindistbest
)
3447 sprintf(buf
, "%s/%s", dir
, mindistguess
);
3448 if (access(unmeta(buf
), F_OK
) == 0) {
3449 strcpy(mindistbest
, mindistguess
);
3452 if (!(dd
= opendir(unmeta(dir
))))
3454 while ((fn
= zreaddir(dd
, 0))) {
3455 nd
= spdist(fn
, mindistguess
,
3456 (int)strlen(mindistguess
) / 4 + 1);
3457 if (nd
<= mindistd
) {
3458 strcpy(mindistbest
, fn
);
3470 spdist(char *s
, char *t
, int thresh
)
3472 /* TODO: Correction for non-ASCII and multibyte-input keyboards. */
3474 const char qwertykeymap
[] =
3475 "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
3480 \n\n\n\n\n\n\n\n\n\n\n\n\n\n\
3485 \n\n\n\n\n\n\n\n\n\n\n\n\n\n";
3486 const char dvorakkeymap
[] =
3487 "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
3492 \n\n\n\n\n\n\n\n\n\n\n\n\n\n\
3497 \n\n\n\n\n\n\n\n\n\n\n\n\n\n";
3499 if ( isset( DVORAK
) )
3500 keymap
= dvorakkeymap
;
3502 keymap
= qwertykeymap
;
3506 /* any number of upper/lower mistakes allowed (dist = 1) */
3507 for (p
= s
, q
= t
; *p
&& tulower(*p
) == tulower(*q
); p
++, q
++);
3512 for (p
= s
, q
= t
; *p
&& *q
; p
++, q
++)
3514 continue; /* don't consider "aa" transposed, ash */
3515 else if (p
[1] == q
[0] && q
[1] == p
[0]) /* transpositions */
3516 return spdist(p
+ 2, q
+ 2, thresh
- 1) + 1;
3517 else if (p
[1] == q
[0]) /* missing letter */
3518 return spdist(p
+ 1, q
+ 0, thresh
- 1) + 2;
3519 else if (p
[0] == q
[1]) /* missing letter */
3520 return spdist(p
+ 0, q
+ 1, thresh
- 1) + 2;
3523 if ((!*p
&& strlen(q
) == 1) || (!*q
&& strlen(p
) == 1))
3525 for (p
= s
, q
= t
; *p
&& *q
; p
++, q
++)
3526 if (p
[0] != q
[0] && p
[1] == q
[1]) {
3530 /* mistyped letter */
3532 if (!(z
= strchr(keymap
, p
[0])) || *z
== '\n' || *z
== '\t')
3533 return spdist(p
+ 1, q
+ 1, thresh
- 1) + 1;
3535 if (*q
== keymap
[t0
- 15] || *q
== keymap
[t0
- 14] ||
3536 *q
== keymap
[t0
- 13] ||
3537 *q
== keymap
[t0
- 1] || *q
== keymap
[t0
+ 1] ||
3538 *q
== keymap
[t0
+ 13] || *q
== keymap
[t0
+ 14] ||
3539 *q
== keymap
[t0
+ 15])
3540 return spdist(p
+ 1, q
+ 1, thresh
- 1) + 2;
3542 } else if (*p
!= *q
)
3547 /* set cbreak mode, or the equivalent */
3557 ti
.tio
.c_lflag
&= ~ICANON
;
3558 ti
.tio
.c_cc
[VMIN
] = 1;
3559 ti
.tio
.c_cc
[VTIME
] = 0;
3561 ti
.sgttyb
.sg_flags
|= CBREAK
;
3566 /* give the tty to some process */
3570 attachtty(pid_t pgrp
)
3575 #ifdef HAVE_TCSETPGRP
3576 if (SHTTY
!= -1 && tcsetpgrp(SHTTY
, pgrp
) == -1 && !ep
)
3579 if (SHTTY
!= -1 && setpgrp() == -1 && !ep
)
3583 if (SHTTY
!= -1 && ioctl(SHTTY
, TIOCSPGRP
, &arg
) == -1 && !ep
)
3587 if (pgrp
!= mypgrp
&& kill(-pgrp
, 0) == -1)
3590 if (errno
!= ENOTTY
)
3592 zwarn("can't set tty pgrp: %e", errno
);
3602 /* get the process group associated with the tty */
3613 #ifdef HAVE_TCSETPGRP
3614 arg
= tcgetpgrp(SHTTY
);
3616 ioctl(SHTTY
, TIOCGPGRP
, &arg
);
3623 /* Escape tokens and null characters. Buf is the string which should be *
3624 * escaped. len is the length of the string. If len is -1, buf should be *
3625 * null terminated. If len is non-negative and the third parameter is not *
3626 * META_DUP, buf should point to an at least len+1 long memory area. The *
3627 * return value points to the quoted string. If the given string does not *
3628 * contain any special character which should be quoted and the third *
3629 * parameter is not META_(HEAP|)DUP, buf is returned unchanged (a *
3630 * terminating null character is appended to buf if necessary). Otherwise *
3631 * the third `heap' argument determines the method used to allocate space *
3632 * for the result. It can have the following values: *
3633 * META_REALLOC: use zrealloc on buf *
3634 * META_HREALLOC: use hrealloc on buf *
3635 * META_USEHEAP: get memory from the heap. This leaves buf unchanged. *
3636 * META_NOALLOC: buf points to a memory area which is long enough to hold *
3637 * the quoted form, just quote it and return buf. *
3638 * META_STATIC: store the quoted string in a static area. The original *
3639 * string should be at most PATH_MAX long. *
3640 * META_ALLOC: allocate memory for the new string with zalloc(). *
3641 * META_DUP: leave buf unchanged and allocate space for the return *
3642 * value even if buf does not contains special characters *
3643 * META_HEAPDUP: same as META_DUP, but uses the heap */
3647 metafy(char *buf
, int len
, int heap
)
3651 static char mbuf
[PATH_MAX
*2+1];
3654 for (e
= buf
, len
= 0; *e
; len
++)
3658 for (e
= buf
; e
< buf
+ len
;)
3662 if (meta
|| heap
== META_DUP
|| heap
== META_HEAPDUP
) {
3665 buf
= zrealloc(buf
, len
+ meta
+ 1);
3668 buf
= hrealloc(buf
, len
, len
+ meta
+ 1);
3672 buf
= memcpy(zalloc(len
+ meta
+ 1), buf
, len
);
3676 buf
= memcpy(zhalloc(len
+ meta
+ 1), buf
, len
);
3680 if (len
> PATH_MAX
) {
3681 fprintf(stderr
, "BUG: len = %d > PATH_MAX in metafy\n", len
);
3685 buf
= memcpy(mbuf
, buf
, len
);
3691 fprintf(stderr
, "BUG: metafy called with invalid heap value\n");
3697 e
= t
= buf
+ len
+ meta
;
3699 if (imeta(*--t
= *--p
)) {
3712 * Take a null-terminated, metafied string in s into a literal
3713 * representation by converting in place. The length is in *len
3714 * len is non-NULL; if len is NULL, you don't know the length of
3715 * the final string, but if it's to be supplied to some system
3716 * routine that always uses NULL termination, such as a filename
3717 * interpreter, that doesn't matter. Note the NULL termination
3718 * is always copied for purposes of that kind.
3723 unmetafy(char *s
, int *len
)
3727 for (p
= s
; *p
&& *p
!= Meta
; p
++);
3728 for (t
= p
; (*t
= *p
++);)
3736 /* Return the character length of a metafied substring, given the *
3737 * unmetafied substring length. */
3741 metalen(const char *s
, int len
)
3755 * This function converts a zsh internal string to a form which can be
3756 * passed to a system call as a filename. The result is stored in a
3757 * single static area, sized to fit. If there is no Meta character
3758 * the original string is returned.
3763 unmeta(const char *file_name
)
3772 for (t
= file_name
; *t
; t
++) {
3778 * don't need allocation... free if it's long, see below
3780 if (sz
> 4 * PATH_MAX
) {
3785 return (char *) file_name
;
3788 newsz
= (t
- file_name
) + 1;
3790 * Optimisation: don't resize if we don't have to.
3791 * We need a new allocation if
3792 * - nothing was allocated before
3793 * - the new string is larger than the old one
3794 * - the old string was larger than an arbitrary limit but the
3795 * new string isn't so that we free up significant space by resizing.
3797 if (!fn
|| newsz
> sz
|| (sz
> 4 * PATH_MAX
&& newsz
<= 4 * PATH_MAX
))
3802 fn
= (char *)zalloc(sz
);
3806 * will quite likely crash in the caller anyway...
3812 for (t
= file_name
, p
= fn
; *t
; p
++)
3813 if ((*p
= *t
++) == Meta
)
3820 * Unmetafy and compare two strings, comparing unsigned character values.
3821 * "a\0" sorts after "a".
3823 * Currently this is only used in hash table sorting, where the
3824 * keys are names of hash nodes and where we don't use strcoll();
3825 * it's not clear if that's right but it does guarantee the ordering
3826 * of shell structures on output.
3828 * As we don't use strcoll(), it seems overkill to convert multibyte
3829 * characters to wide characters for comparison every time. In the case
3830 * of UTF-8, Unicode ordering is preserved when sorted raw, and for
3831 * other character sets we rely on an extension of ASCII so the result,
3832 * while it may not be correct, is at least rational.
3837 ztrcmp(char const *s1
, char const *s2
)
3841 while(*s1
&& *s1
== *s2
) {
3848 else if(c1
== STOUC(Meta
))
3852 else if(c2
== STOUC(Meta
))
3863 /* Return the unmetafied length of a metafied string. */
3867 ztrlen(char const *s
)
3871 for (l
= 0; *s
; l
++) {
3875 fprintf(stderr
, "BUG: unexpected end of string in ztrlen()\n");
3884 /* Subtract two pointers in a metafied string. */
3888 ztrsub(char const *t
, char const *s
)
3896 fprintf(stderr
, "BUG: substring ends in the middle of a metachar in ztrsub()\n");
3908 zreaddir(DIR *dir
, int ignoredots
)
3916 } while(ignoredots
&& de
->d_name
[0] == '.' &&
3917 (!de
->d_name
[1] || (de
->d_name
[1] == '.' && !de
->d_name
[2])));
3919 return metafy(de
->d_name
, -1, META_STATIC
);
3922 /* Unmetafy and output a string. Tokens are skipped. */
3926 zputs(char const *s
, FILE *stream
)
3939 if (fputc(c
, stream
) < 0)
3945 #ifndef MULTIBYTE_SUPPORT
3946 /* Create a visibly-represented duplicate of a string. */
3950 nicedup(char const *s
, int heap
)
3952 int c
, len
= strlen(s
) * 5 + 1;
3953 VARARR(char, buf
, len
);
3956 while ((c
= *s
++)) {
3959 c
= ztokens
[c
- Pound
];
3965 /* The result here is metafied */
3971 return heap
? dupstring(buf
) : ztrdup(buf
);
3977 nicedupstring(char const *s
)
3979 return nicedup(s
, 1);
3983 #ifndef MULTIBYTE_SUPPORT
3984 /* Unmetafy and output a string, displaying special characters readably. */
3988 nicezputs(char const *s
, FILE *stream
)
3992 while ((c
= *s
++)) {
3995 c
= ztokens
[c
- Pound
];
4001 if(zputs(nicechar(c
), stream
) < 0)
4008 /* Return the length of the visible representation of a metafied string. */
4012 niceztrlen(char const *s
)
4017 while ((c
= *s
++)) {
4020 c
= ztokens
[c
- Pound
];
4026 l
+= strlen(nicechar(c
));
4034 #ifdef MULTIBYTE_SUPPORT
4036 * Version of both nicezputs() and niceztrlen() for use with multibyte
4037 * characters. Input is a metafied string; output is the screen width of
4040 * If the FILE * is not NULL, output to that, too.
4042 * If outstrp is not NULL, set *outstrp to a zalloc'd version of
4043 * the output (still metafied).
4045 * If "heap" is non-zero, use the heap for *outstrp, else zalloc.
4050 mb_niceformat(const char *s
, FILE *stream
, char **outstrp
, int heap
)
4053 int umlen
, outalloc
, outleft
, eol
= 0;
4055 char *ums
, *ptr
, *fmt
, *outstr
, *outptr
;
4059 outleft
= outalloc
= 5 * strlen(s
);
4060 outptr
= outstr
= zalloc(outalloc
);
4062 outleft
= outalloc
= 0;
4063 outptr
= outstr
= NULL
;
4068 * is this necessary at this point? niceztrlen does this
4069 * but it's used in lots of places. however, one day this may
4073 ptr
= unmetafy(ums
, ¨en
);
4075 memset(&mbs
, 0, sizeof mbs
);
4077 size_t cnt
= eol
? MB_INVALID
: mbrtowc(&c
, ptr
, umlen
, &mbs
);
4084 /* The byte didn't convert, so output it as a \M-... sequence. */
4085 fmt
= nicechar(*ptr
);
4088 /* Get mbs out of its undefined state. */
4089 memset(&mbs
, 0, sizeof mbs
);
4092 /* Careful: converting '\0' returns 0, but a '\0' is a
4093 * real character for us, so we should consume 1 byte. */
4097 fmt
= wcs_nicechar(c
, &newl
, NULL
);
4108 /* Append to output string */
4109 int outlen
= strlen(fmt
);
4110 if (outlen
>= outleft
) {
4111 /* Reallocate to twice the length */
4112 int outoffset
= outptr
- outstr
;
4114 outleft
+= outalloc
;
4116 outstr
= zrealloc(outstr
, outalloc
);
4117 outptr
= outstr
+ outoffset
;
4119 memcpy(outptr
, fmt
, outlen
);
4120 /* Update start position */
4122 /* Update available bytes */
4130 /* Use more efficient storage for returned string */
4131 *outstrp
= heap
? dupstring(outstr
) : ztrdup(outstr
);
4138 /* ztrdup multibyte string with nice formatting */
4142 nicedup(const char *s
, int heap
)
4146 (void)mb_niceformat(s
, NULL
, &retstr
, heap
);
4153 * The guts of mb_metacharlenconv(). This version assumes we are
4154 * processing a true multibyte character string without tokens, and
4155 * takes the shift state as an argument.
4160 mb_metacharlenconv_r(const char *s
, wint_t *wcp
, mbstate_t *mbsp
)
4162 size_t ret
= MB_INVALID
;
4167 for (ptr
= s
; *ptr
; ) {
4169 inchar
= *++ptr
^ 32;
4171 "BUG: unexpected end of string in mb_metacharlen()\n");
4175 ret
= mbrtowc(&wc
, &inchar
, 1, mbsp
);
4177 if (ret
== MB_INVALID
)
4179 if (ret
== MB_INCOMPLETE
)
4188 /* No valid multibyte sequence */
4189 memset(mbsp
, 0, sizeof(*mbsp
));
4191 return 1 + (*s
== Meta
); /* Treat as single byte character */
4193 return 0; /* Probably shouldn't happen */
4197 * Length of metafied string s which contains the next multibyte
4198 * character; single (possibly metafied) character if string is not null
4199 * but character is not valid (e.g. possibly incomplete at end of string).
4200 * Returned value is guaranteed not to reach beyond the end of the
4201 * string (assuming correct metafication).
4203 * If wcp is not NULL, the converted wide character is stored there.
4204 * If no conversion could be done WEOF is used.
4209 mb_metacharlenconv(const char *s
, wint_t *wcp
)
4211 if (!isset(MULTIBYTE
)) {
4212 /* treat as single byte, possibly metafied */
4214 *wcp
= (wint_t)(*s
== Meta
? s
[1] ^ 32 : *s
);
4215 return 1 + (*s
== Meta
);
4218 * We have to handle tokens here, since we may be looking
4219 * through a tokenized input. Obviously this isn't
4220 * a valid multibyte character, so just return WEOF
4221 * and let the caller handle it as a single character.
4223 * TODO: I've a sneaking suspicion we could do more here
4224 * to prevent the caller always needing to handle invalid
4225 * characters specially, but sometimes it may need to know.
4233 return mb_metacharlenconv_r(s
, wcp
, &mb_shiftstate
);
4237 * Total number of multibyte characters in metafied string s.
4238 * Same answer as iterating mb_metacharlen() and counting calls
4239 * until end of string.
4241 * If width is 1, return total character width rather than number.
4246 mb_metastrlen(char *ptr
, int width
)
4248 char inchar
, *laststart
;
4251 int num
, num_in_char
;
4253 if (!isset(MULTIBYTE
))
4258 num
= num_in_char
= 0;
4260 memset(&mb_shiftstate
, 0, sizeof(mb_shiftstate
));
4263 inchar
= *++ptr
^ 32;
4267 ret
= mbrtowc(&wc
, &inchar
, 1, &mb_shiftstate
);
4269 if (ret
== MB_INCOMPLETE
) {
4272 if (ret
== MB_INVALID
) {
4273 /* Reset, treat as single character */
4274 memset(&mb_shiftstate
, 0, sizeof(mb_shiftstate
));
4275 ptr
= laststart
+ (*laststart
== Meta
) + 1;
4279 * Returns -1 if not a printable character. We
4280 * turn this into 1 for backward compatibility.
4282 int wcw
= WCWIDTH(wc
);
4294 /* If incomplete, treat remainder as trailing single bytes */
4295 return num
+ num_in_char
;
4301 /* Simple replacement for mb_metacharlenconv */
4305 metacharlenconv(const char *x
, int *c
)
4308 * Here we don't use STOUC() on the chars since they
4309 * may be compared against other chars and this will fail
4310 * if chars are signed and the high bit is set.
4323 #endif /* MULTIBYTE_SUPPORT */
4325 /* check for special characters in the string */
4329 hasspecial(char const *s
)
4332 if (ispecial(*s
== Meta
? *++s
^ 32 : *s
))
4340 addunprintable(char *v
, const char *u
, const char *uend
)
4342 for (; u
< uend
; u
++) {
4344 * Just do this byte by byte; there's no great
4345 * advantage in being clever with multibyte
4346 * characters if we don't think they're printable.
4350 c
= STOUC(*++u
^ 32);
4357 if ('0' <= u
[1] && u
[1] <= '7') {
4363 case '\007': *v
++ = '\\'; *v
++ = 'a'; break;
4364 case '\b': *v
++ = '\\'; *v
++ = 'b'; break;
4365 case '\f': *v
++ = '\\'; *v
++ = 'f'; break;
4366 case '\n': *v
++ = '\\'; *v
++ = 'n'; break;
4367 case '\r': *v
++ = '\\'; *v
++ = 'r'; break;
4368 case '\t': *v
++ = '\\'; *v
++ = 't'; break;
4369 case '\v': *v
++ = '\\'; *v
++ = 'v'; break;
4373 *v
++ = '0' + ((c
>> 6) & 7);
4374 *v
++ = '0' + ((c
>> 3) & 7);
4375 *v
++ = '0' + (c
& 7);
4384 * Quote the string s and return the result.
4386 * If e is non-zero, the
4387 * pointer it points to may point to a position in s and in e the position
4388 * of the corresponding character in the quoted string is returned.
4390 * The last argument is a QT_ value defined in zsh.h other than QT_NONE.
4392 * The string may be metafied and contain tokens.
4397 quotestring(const char *s
, char **e
, int instring
)
4402 * With QT_BACKSLASH we may need to use $'\300' stuff.
4403 * Keep memory usage within limits by allocating temporary
4404 * storage and using heap for correct size at end.
4406 int alloclen
= (instring
== QT_BACKSLASH
? 7 : 4) * strlen(s
) + 1;
4407 char *buf
= zshcalloc(alloclen
);
4412 DPUTS(instring
< QT_BACKSLASH
|| instring
> QT_DOLLARS
,
4413 "BUG: bad quote type in quotestring");
4416 if (instring
== QT_DOLLARS
) {
4418 * As we test for printability here we need to be able
4419 * to look for multibyte characters.
4423 uend
= u
+ MB_METACHARLENCONV(u
, &cc
);
4425 if (e
&& !sf
&& *e
<= u
) {
4430 #ifdef MULTIBYTE_SUPPORT
4441 if (isset(BANGHIST
) && cc
== (wchar_t)bangchar
)
4449 v
= addunprintable(v
, u
, uend
);
4457 * Here there are syntactic special characters, so
4458 * we start by going through bytewise.
4461 int dobackslash
= 0;
4464 if (*u
== Tick
|| *u
== Qtick
) {
4468 while (*u
&& *u
!= c
)
4474 } else if ((*u
== Qstring
|| *u
== '$') && u
[1] == '\'' &&
4475 instring
== QT_DOUBLE
) {
4477 * We don't need to quote $'...' inside a double-quoted
4478 * string. This is largely cosmetic; it looks neater
4479 * if we don't but it doesn't do any harm since the
4483 } else if ((*u
== String
|| *u
== Qstring
) &&
4484 (u
[1] == Inpar
|| u
[1] == Inbrack
|| u
[1] == Inbrace
)) {
4485 char c
= (u
[1] == Inpar
? Outpar
: (u
[1] == Inbrace
?
4486 Outbrace
: Outbrack
));
4492 while (*u
&& (*u
!= c
|| level
)) {
4503 else if (ispecial(*u
) &&
4504 ((*u
!= '=' && *u
!= '~') ||
4506 (isset(MAGICEQUALSUBST
) &&
4507 (u
[-1] == '=' || u
[-1] == ':')) ||
4508 (*u
== '~' && isset(EXTENDEDGLOB
))) &&
4509 (instring
== QT_BACKSLASH
||
4510 (isset(BANGHIST
) && *u
== (char)bangchar
&&
4511 instring
!= QT_SINGLE
) ||
4512 (instring
== QT_DOUBLE
&&
4513 (*u
== '$' || *u
== '`' || *u
== '\"' || *u
== '\\')) ||
4514 (instring
== QT_SINGLE
&& *u
== '\''))) {
4515 if (*u
== '\n' || (instring
== QT_SINGLE
&& *u
== '\'')) {
4516 if (unset(RCQUOTES
)) {
4522 } else if (*u
== '\n')
4523 *v
++ = '"', *v
++ = '\n', *v
++ = '"';
4525 *v
++ = '\'', *v
++ = '\'';
4530 * We'll need a backslash, but don't add it
4531 * yet since if the character isn't printable
4532 * we'll have to upgrade it to $'...'.
4538 if (itok(*u
) || instring
!= QT_BACKSLASH
) {
4539 /* Needs to be passed straight through. */
4547 * Now check if the output is unprintable in the
4548 * current character set.
4550 uend
= u
+ MB_METACHARLENCONV(u
, &cc
);
4552 #ifdef MULTIBYTE_SUPPORT
4567 v
= addunprintable(v
, u
, uend
);
4577 DPUTS(e
&& !sf
, "BUG: Wild pointer *e in quotestring()");
4580 zfree(buf
, alloclen
);
4584 /* Unmetafy and output a string, quoted if it contains special characters. */
4588 quotedzputs(char const *s
, FILE *stream
)
4592 /* check for empty string */
4594 return fputs("''", stream
);
4597 return zputs(s
, stream
);
4599 if (isset(RCQUOTES
)) {
4600 /* use rc-style quotes-within-quotes for the whole string */
4601 if(fputc('\'', stream
) < 0)
4610 if(fputc('\'', stream
) < 0)
4612 } else if(c
== '\n' && isset(CSHJUNKIEQUOTES
)) {
4613 if(fputc('\\', stream
) < 0)
4616 if(fputc(c
, stream
) < 0)
4619 if(fputc('\'', stream
) < 0)
4622 /* use Bourne-style quoting, avoiding empty quoted strings */
4631 if(fputc('\'', stream
) < 0)
4635 if(fputs("\\'", stream
) < 0)
4639 if(fputc('\'', stream
) < 0)
4643 if(c
== '\n' && isset(CSHJUNKIEQUOTES
)) {
4644 if(fputc('\\', stream
) < 0)
4647 if(fputc(c
, stream
) < 0)
4652 if(fputc('\'', stream
) < 0)
4659 /* Double-quote a metafied string. */
4663 dquotedztrdup(char const *s
)
4665 int len
= strlen(s
) * 4 + 2;
4666 char *buf
= zalloc(len
);
4667 char *p
= buf
, *ret
;
4669 if(isset(CSHJUNKIEQUOTES
)) {
4734 ret
= metafy(buf
, p
- buf
, META_DUP
);
4739 /* Unmetafy and output a string, double quoting it in its entirety. */
4743 dquotedzputs(char const *s
, FILE *stream
)
4745 char *d
= dquotedztrdup(s
);
4746 int ret
= zputs(d
, stream
);
4753 # if defined(HAVE_NL_LANGINFO) && defined(CODESET) && !defined(__STDC_ISO_10646__)
4754 /* Convert a character from UCS4 encoding to UTF-8 */
4757 ucs4toutf8(char *dest
, unsigned int wval
)
4763 else if (wval
< 0x800)
4765 else if (wval
< 0x10000)
4767 else if (wval
< 0x200000)
4769 else if (wval
< 0x4000000)
4774 switch (len
) { /* falls through except to the last case */
4775 case 6: dest
[5] = (wval
& 0x3f) | 0x80; wval
>>= 6;
4776 case 5: dest
[4] = (wval
& 0x3f) | 0x80; wval
>>= 6;
4777 case 4: dest
[3] = (wval
& 0x3f) | 0x80; wval
>>= 6;
4778 case 3: dest
[2] = (wval
& 0x3f) | 0x80; wval
>>= 6;
4779 case 2: dest
[1] = (wval
& 0x3f) | 0x80; wval
>>= 6;
4780 *dest
= wval
| ((0xfc << (6 - len
)) & 0xfc);
4782 case 1: *dest
= wval
;
4791 * The following only occurs once or twice in the code, but in different
4792 * places depending how character set conversion is implemented.
4794 #define CHARSET_FAILED() \
4795 if (how & GETKEY_DOLLAR_QUOTE) { \
4796 while ((*tdest++ = *++s)) { \
4797 if (how & GETKEY_UPDATE_OFFSET) { \
4798 if (s - sstart > *misc) \
4801 if (*s == Snull) { \
4802 *len = (s - sstart) + 1; \
4807 *len = tdest - buf; \
4815 * Decode a key string, turning it into the literal characters.
4816 * The value returned is a newly allocated string from the heap.
4818 * The length is returned in *len. This is usually the length of
4819 * the final unmetafied string. The exception is the case of
4820 * a complete GETKEY_DOLLAR_QUOTE conversion where *len is the
4821 * length of the input string which has been used (up to and including
4822 * the terminating single quote); as the final string is metafied and
4823 * NULL-terminated its length is not required. If both GETKEY_DOLLAR_QUOTE
4824 * and GETKEY_UPDATE_OFFSET are present in "how", the string is not
4825 * expected to be terminated (this is used in completion to parse
4826 * a partial $'...'-quoted string) and the length passed back is
4827 * that of the converted string. Note in both cases that this is a length
4828 * in bytes (i.e. the same as given by a raw pointer difference), not
4829 * characters, which may occupy multiple bytes.
4831 * how is a set of bits from the GETKEY_ values defined in zsh.h;
4832 * not all combinations of bits are useful. Callers will typically
4833 * use one of the GETKEYS_ values which define sets of bits.
4834 * Note, for example that:
4835 * - GETKEY_SINGLE_CHAR must not be combined with GETKEY_DOLLAR_QUOTE.
4836 * - GETKEY_UPDATE_OFFSET is only allowed if GETKEY_DOLLAR_QUOTE is
4839 * *misc is used for various purposes:
4840 * - If GETKEY_BACKSLASH_MINUS is set, it indicates the presence
4841 * of \- in the input.
4842 * - If GETKEY_BACKSLASH_C is set, it indicates the presence
4843 * of \c in the input.
4844 * - If GETKEY_UPDATE_OFFSET is set, it is set on input to some
4845 * mystical completion offset and is updated to a new offset based
4846 * on the converted characters. All Hail the Completion System
4847 * [makes the mystic completion system runic sign in the air].
4849 * The return value is unmetafied unless GETKEY_DOLLAR_QUOTE is
4855 getkeystring(char *s
, int *len
, int how
, int *misc
)
4858 char *t
, *tdest
= NULL
, *u
= NULL
, *sstart
= s
, *tbuf
= NULL
;
4860 int meta
= 0, control
= 0;
4862 #if defined(HAVE_WCHAR_H) && defined(HAVE_WCTOMB) && defined(__STDC_ISO_10646__)
4867 # if defined(HAVE_NL_LANGINFO) && defined(CODESET)
4868 # if defined(HAVE_ICONV)
4871 size_t inbytes
, outbytes
;
4877 DPUTS((how
& GETKEY_UPDATE_OFFSET
) &&
4878 (how
& ~(GETKEYS_DOLLARS_QUOTE
|GETKEY_UPDATE_OFFSET
)),
4879 "BUG: offset updating in getkeystring only supported with $'.");
4880 DPUTS((how
& (GETKEY_DOLLAR_QUOTE
|GETKEY_SINGLE_CHAR
)) ==
4881 (GETKEY_DOLLAR_QUOTE
|GETKEY_SINGLE_CHAR
),
4882 "BUG: incompatible options in getkeystring");
4884 if (how
& GETKEY_SINGLE_CHAR
)
4887 /* Length including terminating NULL */
4890 * We're not necessarily guaranteed the output string will
4891 * be no longer than the input with \u and \U when output
4892 * characters need to be metafied. As this is the only
4893 * case where the string can get longer (?I think),
4894 * include it in the allocation length here but don't
4895 * bother taking account of other factors.
4897 for (t
= s
; *t
; t
++) {
4903 if (t
[1] == 'u' || t
[1] == 'U')
4904 maxlen
+= MB_CUR_MAX
* 2;
4907 /* skip the backslash and the following character */
4912 if (how
& GETKEY_DOLLAR_QUOTE
) {
4914 * We're going to unmetafy into a new string, but
4915 * to get a proper metafied input we're going to metafy
4916 * into an intermediate buffer. This is necessary if we have
4917 * \u and \U's with multiple metafied bytes. We can't
4918 * simply remetafy the entire string because there may
4919 * be tokens (indeed, we know there are lexical nulls floating
4920 * around), so we have to be aware character by character
4921 * what we are converting.
4923 * In this case, buf is the final buffer (as usual),
4924 * but t points into a temporary buffer that just has
4925 * to be long enough to hold the result of one escape
4926 * code transformation. We count this is a full multibyte
4927 * character (MB_CUR_MAX) with every character metafied
4928 * (*2) plus a little bit of fuzz (for e.g. the odd backslash).
4930 buf
= tdest
= zhalloc(maxlen
);
4931 t
= tbuf
= zhalloc(MB_CUR_MAX
* 3 + 1);
4933 t
= buf
= zhalloc(maxlen
);
4937 if (*s
== '\\' && s
[1]) {
4939 if ((how
& GETKEY_UPDATE_OFFSET
) && s
- sstart
< *misc
) {
4971 if (!(how
& GETKEY_EMACS
)) {
4982 /* HERE: GETKEY_UPDATE_OFFSET */
4983 if (how
& GETKEY_EMACS
) {
4986 meta
= 1 + control
; /* preserve the order of ^ and meta */
4994 /* HERE: GETKEY_UPDATE_OFFSET */
4995 if (how
& GETKEY_EMACS
) {
5011 if (how
& GETKEY_BACKSLASH_MINUS
) {
5017 if (how
& GETKEY_BACKSLASH_C
) {
5025 if ((how
& GETKEY_UPDATE_OFFSET
) && s
- sstart
< *misc
)
5029 if ((how
& GETKEY_UPDATE_OFFSET
) && s
- sstart
< *misc
) {
5030 (*misc
) -= 6; /* HERE don't really believe this */
5032 * We've now adjusted the offset for all the input
5033 * characters, so we need to add for each
5034 * byte of output below.
5038 for (i
=(*s
== 'u' ? 4 : 8); i
>0; i
--) {
5039 if (*++s
&& idigit(*s
))
5040 wval
= wval
* 16 + (*s
- '0');
5041 else if (*s
&& ((*s
>= 'a' && *s
<= 'f') ||
5042 (*s
>= 'A' && *s
<= 'F')))
5043 wval
= wval
* 16 + (*s
& 0x1f) + 9;
5049 if (how
& GETKEY_SINGLE_CHAR
) {
5053 #if defined(HAVE_WCHAR_H) && defined(HAVE_WCTOMB) && defined(__STDC_ISO_10646__)
5054 count
= wctomb(t
, (wchar_t)wval
);
5056 zerr("character not in range");
5059 if ((how
& GETKEY_UPDATE_OFFSET
) && s
- sstart
< *misc
)
5063 # if defined(HAVE_NL_LANGINFO) && defined(CODESET)
5064 if (!strcmp(nl_langinfo(CODESET
), "UTF-8")) {
5065 count
= ucs4toutf8(t
, wval
);
5067 if ((how
& GETKEY_UPDATE_OFFSET
) && s
- sstart
< *misc
)
5071 ICONV_CONST
char *inptr
= inbuf
;
5072 const char *codesetstr
= nl_langinfo(CODESET
);
5075 /* store value in big endian form */
5076 for (i
=3;i
>=0;i
--) {
5077 inbuf
[i
] = wval
& 0xff;
5082 * If the code set isn't handled, we'd better
5083 * assume it's US-ASCII rather than just failing
5084 * hopelessly. Solaris has a weird habit of
5085 * returning 646. This is handled by the
5086 * native iconv(), but not by GNU iconv; what's
5087 * more, some versions of the native iconv don't
5088 * handle standard names like ASCII.
5090 * This should only be a problem if there's a
5091 * mismatch between the NLS and the iconv in use,
5092 * which probably only means if libiconv is in use.
5093 * We checked at configure time if our libraries
5094 * pulled in _libiconv_version, which should be
5097 * It shouldn't ever be NULL, but while we're
5100 #ifdef ICONV_FROM_LIBICONV
5101 if (!codesetstr
|| !*codesetstr
)
5102 codesetstr
= "US-ASCII";
5104 cd
= iconv_open(codesetstr
, "UCS-4BE");
5105 #ifdef ICONV_FROM_LIBICONV
5106 if (cd
== (iconv_t
)-1 && !strcmp(codesetstr
, "646")) {
5107 codesetstr
= "US-ASCII";
5108 cd
= iconv_open(codesetstr
, "UCS-4BE");
5111 if (cd
== (iconv_t
)-1) {
5112 zerr("cannot do charset conversion (iconv failed)");
5115 count
= iconv(cd
, &inptr
, &inbytes
, &t
, &outbytes
);
5117 if (count
== (size_t)-1) {
5118 zerr("character not in range");
5121 if ((how
& GETKEY_UPDATE_OFFSET
) && s
- sstart
< *misc
)
5124 zerr("cannot do charset conversion (iconv not available)");
5129 zerr("cannot do charset conversion (NLS not supported)");
5133 if (how
& GETKEY_DOLLAR_QUOTE
) {
5135 for (t2
= tbuf
; t2
< t
; t2
++) {
5138 *tdest
++ = *t2
^ 32;
5142 /* reset temporary buffer after handling */
5148 if (how
& GETKEY_DOLLAR_QUOTE
) {
5150 * Usually \' and \\ will have the initial
5151 * \ turned into a Bnull, however that's not
5152 * necessarily the case when called from
5161 /* HERE: GETKEY_UPDATE_OFFSET? */
5162 if ((idigit(*s
) && *s
< '8') || *s
== 'x') {
5163 if (!(how
& GETKEY_OCTAL_ESC
)) {
5166 else if (*s
!= 'x') {
5171 if (s
[1] && s
[2] && s
[3]) {
5176 *t
++ = zstrtol(s
+ (*s
== 'x'), &s
,
5177 (*s
== 'x') ? 16 : 8);
5184 if (!(how
& GETKEY_EMACS
) && *s
!= '\\') {
5193 } else if ((how
& GETKEY_DOLLAR_QUOTE
) && *s
== Snull
) {
5194 /* return length to following character */
5195 *len
= (s
- sstart
) + 1;
5198 } else if (*s
== '^' && !control
&& (how
& GETKEY_CTRL
) && s
[1]) {
5201 #ifdef MULTIBYTE_SUPPORT
5202 } else if ((how
& GETKEY_SINGLE_CHAR
) &&
5203 isset(MULTIBYTE
) && STOUC(*s
) > 127) {
5206 len
= mb_metacharlenconv(s
, &wc
);
5213 } else if (*s
== Meta
)
5218 * We need to be quite careful here. We haven't
5219 * necessarily got an input stream with all tokens
5220 * removed, so the majority of tokens need passing
5221 * through untouched and without Meta handling.
5222 * However, me may need to handle tokenized
5225 if (meta
|| control
) {
5227 * Presumably we should be using meta or control
5228 * on the character representing the token.
5230 * Special case: $'\M-\\' where the token is a Bnull.
5231 * This time we dump the Bnull since we're
5232 * replacing the whole thing. The lexer
5233 * doesn't know about the meta or control modifiers.
5235 if ((how
& GETKEY_DOLLAR_QUOTE
) && *s
== Bnull
)
5238 *t
++ = ztokens
[*s
- Pound
];
5239 } else if (how
& GETKEY_DOLLAR_QUOTE
) {
5241 * We don't want to metafy this, it's a real
5247 * Bnull is a backslash which quotes a couple
5248 * of special characters that always appear
5249 * literally next. See strquote handling
5250 * in gettokstr() in lex.c. We need
5251 * to retain the Bnull (as above) so that quote
5252 * handling in completion can tell where the
5257 /* reset temporary buffer, now handled */
5280 if (how
& GETKEY_DOLLAR_QUOTE
) {
5282 for (t2
= tbuf
; t2
< t
; t2
++) {
5285 *tdest
++ = *t2
^ 32;
5290 * Reset use of temporary buffer.
5294 if ((how
& GETKEY_SINGLE_CHAR
) && t
!= tmp
) {
5295 *misc
= STOUC(tmp
[0]);
5300 * When called from completion, where we use GETKEY_UPDATE_OFFSET to
5301 * update the index into the metafied editor line, we don't necessarily
5302 * have the end of a $'...' quotation, else we should do.
5304 DPUTS((how
& (GETKEY_DOLLAR_QUOTE
|GETKEY_UPDATE_OFFSET
)) ==
5305 GETKEY_DOLLAR_QUOTE
, "BUG: unterminated $' substitution");
5307 if (how
& GETKEY_DOLLAR_QUOTE
)
5309 if (how
& GETKEY_SINGLE_CHAR
)
5312 *len
= ((how
& GETKEY_DOLLAR_QUOTE
) ? tdest
: t
) - buf
;
5316 /* Return non-zero if s is a prefix of t. */
5320 strpfx(const char *s
, const char *t
)
5322 while (*s
&& *s
== *t
)
5327 /* Return non-zero if s is a suffix of t. */
5331 strsfx(char *s
, char *t
)
5333 int ls
= strlen(s
), lt
= strlen(t
);
5336 return !strcmp(t
+ lt
- ls
, s
);
5349 for (s
= buf
; s
< buf
+ PATH_MAX
- 4 && n
--; )
5350 *s
++ = '.', *s
++ = '.', *s
++ = '/';
5359 /* Change directory, without following symlinks. Returns 0 on success, -1 *
5360 * on failure. Sets errno to ENOTDIR if any symlinks are encountered. If *
5361 * fchdir() fails, or the current directory is unreadable, we might end up *
5362 * in an unwanted directory in case of failure. */
5366 lchdir(char const *path
, struct dirsav
*d
, int hard
)
5373 char buf
[PATH_MAX
+ 1], *ptr
;
5379 ds
.ino
= ds
.dev
= 0;
5385 if ((*path
== '/' || !hard
) &&
5386 (d
!= &ds
|| hard
)){
5397 if (!d
->dev
&& !d
->ino
) {
5399 d
->dev
= st1
.st_dev
;
5400 d
->ino
= st1
.st_ino
;
5404 if (d
->dirfd
< 0 && (d
->dirfd
= open(".", O_RDONLY
| O_NOCTTY
)) < 0 &&
5405 zgetdir(d
) && *d
->dirname
!= '/')
5406 d
->dirfd
= open("..", O_RDONLY
| O_NOCTTY
);
5414 for (pptr
= path
; *pptr
; level
++) {
5415 while (*pptr
&& *pptr
++ != '/');
5416 while (*pptr
== '/')
5421 return zchdir((char *) path
);
5426 zwarn("failed to chdir(/): %e", errno
);
5439 for(pptr
= path
; *++pptr
&& *pptr
!= '/'; ) ;
5440 if(pptr
- path
> PATH_MAX
) {
5444 for(ptr
= buf
; path
!= pptr
; )
5447 if(lstat(buf
, &st1
)) {
5451 if(!S_ISDIR(st1
.st_mode
)) {
5461 if(lstat(".", &st2
)) {
5465 if(st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
) {
5470 if (restoredir(d
)) {
5486 #endif /* HAVE_LSTAT */
5491 restoredir(struct dirsav
*d
)
5496 if (d
->dirname
&& *d
->dirname
== '/')
5497 return chdir(d
->dirname
);
5499 if (d
->dirfd
>= 0) {
5500 if (!fchdir(d
->dirfd
)) {
5503 } else if (chdir(d
->dirname
)) {
5510 d
->dirfd
= err
= -1;
5515 err
= upchdir(d
->level
);
5516 else if (d
->level
< 0)
5518 if (d
->dev
|| d
->ino
) {
5520 if (sbuf
.st_ino
!= d
->ino
|| sbuf
.st_dev
!= d
->dev
)
5527 /* Check whether the shell is running with privileges in effect. *
5528 * This is the case if EITHER the euid is zero, OR (if the system *
5529 * supports POSIX.1e (POSIX.6) capability sets) the process' *
5530 * Effective or Inheritable capability sets are non-empty. */
5538 #ifdef HAVE_CAP_GET_PROC
5540 cap_t caps
= cap_get_proc();
5542 /* POSIX doesn't define a way to test whether a capability set *
5543 * is empty or not. Typical. I hope this is conforming... */
5544 cap_flag_value_t val
;
5546 for(n
= 0; !cap_get_flag(caps
, n
, CAP_EFFECTIVE
, &val
); n
++)
5554 #endif /* HAVE_CAP_GET_PROC */
5560 mode_to_octal(mode_t mode
)
5591 #ifdef MAILDIR_SUPPORT
5593 * Stat a file. If it's a maildir, check all messages
5594 * in the maildir and present the grand total as a file.
5595 * The fields in the 'struct stat' are from the mail directory.
5596 * The following fields are emulated:
5599 * st_size total number of bytes in all files
5600 * st_blocks total number of messages
5601 * st_atime access time of newest file in maildir
5602 * st_mtime modify time of newest file in maildir
5603 * st_mode S_IFDIR changed to S_IFREG
5605 * This is good enough for most mail-checking applications.
5610 mailstat(char *path
, struct stat
*st
)
5614 struct stat st_ret
, st_tmp
;
5615 static struct stat st_ret_last
;
5616 char *dir
, *file
= 0;
5618 time_t atime
= 0, mtime
= 0;
5619 size_t plen
= strlen(path
), dlen
;
5621 /* First see if it's a directory. */
5622 if ((i
= stat(path
, st
)) != 0 || !S_ISDIR(st
->st_mode
))
5626 st_ret
.st_nlink
= 1;
5628 st_ret
.st_blocks
= 0;
5629 st_ret
.st_mode
&= ~S_IFDIR
;
5630 st_ret
.st_mode
|= S_IFREG
;
5632 /* See if cur/ is present */
5633 dir
= appstr(ztrdup(path
), "/cur");
5634 if (stat(dir
, &st_tmp
) || !S_ISDIR(st_tmp
.st_mode
)) return 0;
5635 st_ret
.st_atime
= st_tmp
.st_atime
;
5637 /* See if tmp/ is present */
5639 dir
= appstr(dir
, "/tmp");
5640 if (stat(dir
, &st_tmp
) || !S_ISDIR(st_tmp
.st_mode
)) return 0;
5641 st_ret
.st_mtime
= st_tmp
.st_mtime
;
5645 dir
= appstr(dir
, "/new");
5646 if (stat(dir
, &st_tmp
) || !S_ISDIR(st_tmp
.st_mode
)) return 0;
5647 st_ret
.st_mtime
= st_tmp
.st_mtime
;
5649 #if THERE_IS_EXACTLY_ONE_MAILDIR_IN_MAILPATH
5651 static struct stat st_new_last
;
5652 /* Optimization - if new/ didn't change, nothing else did. */
5653 if (st_tmp
.st_dev
== st_new_last
.st_dev
&&
5654 st_tmp
.st_ino
== st_new_last
.st_ino
&&
5655 st_tmp
.st_atime
== st_new_last
.st_atime
&&
5656 st_tmp
.st_mtime
== st_new_last
.st_mtime
) {
5660 st_new_last
= st_tmp
;
5664 /* Loop over new/ and cur/ */
5665 for (i
= 0; i
< 2; i
++) {
5667 dir
= appstr(dir
, i
? "/cur" : "/new");
5668 if ((dd
= opendir(dir
)) == NULL
) {
5673 dlen
= strlen(dir
) + 1; /* include the "/" */
5674 while ((fn
= readdir(dd
)) != NULL
) {
5675 if (fn
->d_name
[0] == '.')
5679 file
= appstr(file
, fn
->d_name
);
5681 file
= tricat(dir
, "/", fn
->d_name
);
5683 if (stat(file
, &st_tmp
) != 0)
5685 st_ret
.st_size
+= st_tmp
.st_size
;
5687 if (st_tmp
.st_atime
!= st_tmp
.st_mtime
&&
5688 st_tmp
.st_atime
> atime
)
5689 atime
= st_tmp
.st_atime
;
5690 if (st_tmp
.st_mtime
> mtime
)
5691 mtime
= st_tmp
.st_mtime
;
5698 if (atime
) st_ret
.st_atime
= atime
;
5699 if (mtime
) st_ret
.st_mtime
= mtime
;
5701 *st
= st_ret_last
= st_ret
;