1 /* $Header: /src/pub/tcsh/tw.parse.c,v 3.105 2005/03/03 16:40:53 kim Exp $ */
3 * tw.parse.c: Everyone has taken a shot in this futile effort to
4 * lexically analyze a csh line... Well we cannot good
5 * a job as good as sh.lex.c; but we try. Amazing that
6 * it works considering how many hands have touched this code
9 * Copyright (c) 1980, 1991 The Regents of the University of California.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 RCSID("$Id: tw.parse.c,v 3.105 2005/03/03 16:40:53 kim Exp $")
46 #endif /* WINNT_NATIVE */
47 #define EVEN(x) (((x) & 1) != 1)
49 #define DOT_NONE 0 /* Don't display dot files */
50 #define DOT_NOT 1 /* Don't display dot or dot-dot */
51 #define DOT_ALL 2 /* Display all dot files */
53 /* TW_NONE, TW_COMMAND, TW_VARIABLE, TW_LOGNAME, */
54 /* TW_FILE, TW_DIRECTORY, TW_VARLIST, TW_USER, */
55 /* TW_COMPLETION, TW_ALIAS, TW_SHELLVAR, TW_ENVVAR, */
56 /* TW_BINDING, TW_WORDLIST, TW_LIMIT, TW_SIGNAL */
57 /* TW_JOB, TW_EXPLAIN, TW_TEXT, TW_GRPNAME */
58 static void (*tw_start_entry[]) __P((DIR *, Char *)) = {
59 tw_file_start, tw_cmd_start, tw_var_start, tw_logname_start,
60 tw_file_start, tw_file_start, tw_vl_start, tw_logname_start,
61 tw_complete_start, tw_alias_start, tw_var_start, tw_var_start,
62 tw_bind_start, tw_wl_start, tw_limit_start, tw_sig_start,
63 tw_job_start, tw_file_start, tw_file_start, tw_grpname_start
66 static Char * (*tw_next_entry[]) __P((Char *, int *)) = {
67 tw_file_next, tw_cmd_next, tw_var_next, tw_logname_next,
68 tw_file_next, tw_file_next, tw_var_next, tw_logname_next,
69 tw_var_next, tw_var_next, tw_shvar_next, tw_envvar_next,
70 tw_bind_next, tw_wl_next, tw_limit_next, tw_sig_next,
71 tw_job_next, tw_file_next, tw_file_next, tw_grpname_next
74 static void (*tw_end_entry[]) __P((void)) = {
75 tw_dir_end, tw_dir_end, tw_dir_end, tw_logname_end,
76 tw_dir_end, tw_dir_end, tw_dir_end, tw_logname_end,
77 tw_dir_end, tw_dir_end, tw_dir_end, tw_dir_end,
78 tw_dir_end, tw_dir_end, tw_dir_end, tw_dir_end,
79 tw_dir_end, tw_dir_end, tw_dir_end, tw_grpname_end
84 /* Set to TRUE if recexact is set and an exact match is found
85 * along with other, longer, matches.
90 int match_unique_match = FALSE;
91 int non_unique_match = FALSE;
92 static int SearchNoDirErr = 0; /* t_search returns -2 if dir is unreadable */
94 /* state so if a completion is interrupted, the input line doesn't get
96 int InsideCompletion = 0;
98 /* do the expand or list on the command line -- SHOULD BE REPLACED */
100 static void extract_dir_and_name __P((Char *, Char *, Char *));
101 static int insert_meta __P((Char *, Char *, Char *, int));
102 static Char *tilde __P((Char *, Char *));
104 static int expand_dir __P((Char *, Char *, DIR **, COMMAND));
106 static int nostat __P((Char *));
107 static Char filetype __P((Char *, Char *));
108 static int t_glob __P((Char ***, int));
109 static int c_glob __P((Char ***));
110 static int is_prefix __P((Char *, Char *));
111 static int is_prefixmatch __P((Char *, Char *, int));
112 static int is_suffix __P((Char *, Char *));
113 static int recognize __P((Char *, Char *, int, int, int,
115 static int ignored __P((Char *));
116 static int isadirectory __P((Char *, Char *));
118 static int tw_collect_items __P((COMMAND, int, Char *, Char *,
119 Char *, Char *, int));
120 static int tw_collect __P((COMMAND, int, Char *, Char *,
121 Char **, Char *, int, DIR *));
123 static Char tw_suffix __P((int, Char *, Char *, Char *,
125 static void tw_fixword __P((int, Char *, Char *, Char *, int));
126 static void tw_list_items __P((int, int, int));
127 static void add_scroll_tab __P((Char *));
128 static void choose_scroll_tab __P((Char **, int));
129 static void free_scroll_tab __P((void));
130 static int find_rows __P((Char *[], int, int));
134 * If we find a set command, then we break a=b to a= and word becomes
135 * b else, we don't break a=b. [don't use that; splits words badly and
136 * messes up tw_complete()]
138 #define isaset(c, w) ((w)[-1] == '=' && \
139 ((c)[0] == 's' && (c)[1] == 'e' && (c)[2] == 't' && \
140 ((c[3] == ' ' || (c)[3] == '\t'))))
143 #define QLINESIZE (INBUFSIZE + 1)
145 /* TRUE if character must be quoted */
146 #define tricky(w) (cmap(w, _META | _DOL | _QF | _QB | _ESC | _GLOB) && w != '#')
147 /* TRUE if double quotes don't protect character */
148 #define tricky_dq(w) (cmap(w, _DOL | _QB))
152 * > 1: No. of items found
153 * = 1: Exactly one match / spelling corrected
154 * = 0: No match / spelling was correct
155 * < 0: Error (incl spelling correction impossible)
158 tenematch(inputline, num_read, command)
159 Char *inputline; /* match string prefix */
160 int num_read; /* # actually in inputline */
161 COMMAND command; /* LIST or RECOGNIZE or PRINT_HELP */
164 Char qline[QLINESIZE];
165 Char qu = 0, *pat = STRNULL;
166 Char *str_end, *cp, *wp, *wordp;
167 Char *cmd_start, *word_start, *word;
168 Char *ocmd_start = NULL, *oword_start = NULL, *oword = NULL;
171 int looking; /* what we are looking for */
172 int search_ret; /* what search returned for debugging */
175 if (num_read > QLINESIZE - 1)
177 str_end = &inputline[num_read];
179 word_start = inputline;
180 word = cmd_start = wp = qline;
181 for (cp = inputline; cp < str_end; cp++) {
182 if (!cmap(qu, _ESC)) {
183 if (cmap(*cp, _QF|_ESC)) {
184 if (qu == 0 || qu == *cp) {
189 if (qu != '\'' && cmap(*cp, _QB)) {
190 if ((backq ^= 1) != 0) {
191 ocmd_start = cmd_start;
192 oword_start = word_start;
195 word = cmd_start = wp + 1;
198 cmd_start = ocmd_start;
199 word_start = oword_start;
209 /* Don't quote '/' to make the recognize stuff work easily */
210 /* Don't quote '$' in double quotes */
212 if (cmap(*cp, _ESC) && cp < str_end - 1 && cp[1] == HIST)
214 else if (qu && (tricky(*cp) || *cp == '~') && !(qu == '\"' && tricky_dq(*cp)))
218 if (ismetahash(*wp) /* || isaset(cmd_start, wp + 1) */)
219 word = wp + 1, word_start = cp + 1;
228 * Avoid a nasty message from the RTU 4.1A & RTU 5.0 compiler concerning
229 * the "overuse of registers". According to the compiler release notes,
230 * incorrect code may be produced unless the offending expression is
231 * rewritten. Therefore, we can't just ignore it, DAS DEC-90.
233 space_left = QLINESIZE - 1;
234 space_left -= word - qline;
236 space_left = QLINESIZE - 1 - (int) (word - qline);
240 * SPECIAL HARDCODED COMPLETIONS:
241 * first word of command -> TW_COMMAND
242 * everything else -> TW_ZERO
245 looking = starting_a_command(word - 1, qline) ?
246 TW_COMMAND : TW_ZERO;
251 xprintf(CGETS(30, 1, "starting_a_command %d\n"), looking);
252 xprintf("\ncmd_start:%S:\n", cmd_start);
253 xprintf("qline:%S:\n", qline);
255 for (wp = qline; *wp; wp++)
256 xprintf("%c", *wp & QUOTE ? '-' : ' ');
258 xprintf("word:%S:\n", word);
260 /* Must be last, so wp is still pointing to the end of word */
261 for (wp = word; *wp; wp++)
262 xprintf("%c", *wp & QUOTE ? '-' : ' ');
266 if ((looking == TW_COMMAND || looking == TW_ZERO) &&
267 (command == RECOGNIZE || command == LIST || command == SPELL ||
268 command == RECOGNIZE_SCROLL)) {
270 xprintf(CGETS(30, 2, "complete %d "), looking);
272 looking = tw_complete(cmd_start, &wordp, &pat, looking, &suf);
274 xprintf(CGETS(30, 3, "complete %d %S\n"), looking, pat);
279 Char buffer[FILSIZ + 1], *bptr;
281 Char *items[2], **ptr;
285 case RECOGNIZE_SCROLL:
287 if (adrof(STRautocorrect)) {
288 if ((slshp = Strrchr(wordp, '/')) != NULL && slshp[1] != '\0') {
290 for (bptr = wordp; bptr < slshp; bptr++) {
292 * do not try to correct spelling of words containing
293 * globbing characters
304 search_ret = t_search(wordp, wp, command, space_left, looking, 1,
308 if (search_ret == -2) {
309 Char rword[FILSIZ + 1];
311 (void) Strcpy(rword, slshp);
312 if (slshp != STRNULL)
314 search_ret = spell_me(wordp, QLINESIZE - (wordp - qline), looking,
316 if (search_ret == 1) {
317 (void) Strcat(wordp, rword);
318 wp = wordp + (int) Strlen(wordp);
319 search_ret = t_search(wordp, wp, command, space_left,
320 looking, 1, pat, suf);
323 if (*wp && insert_meta(word_start, str_end, word, !qu) < 0)
324 return -1; /* error inserting */
328 for (bptr = word_start; bptr < str_end; bptr++) {
330 * do not try to correct spelling of words containing globbing
336 search_ret = spell_me(wordp, QLINESIZE - (wordp - qline), looking,
338 if (search_ret == 1) {
339 if (insert_meta(word_start, str_end, word, !qu) < 0)
340 return -1; /* error inserting */
350 (void) Strncpy(buffer, wordp, FILSIZ + 1);
354 count = (looking == TW_COMMAND && Strchr(wordp, '/') == 0) ?
356 t_glob(&ptr, looking == TW_COMMAND);
359 print_by_column(STRNULL, ptr, count, 0);
361 DeleteBack(str_end - word_start);/* get rid of old word */
362 for (i = 0; i < count; i++)
363 if (ptr[i] && *ptr[i]) {
364 (void) quote(ptr[i]);
365 if (insert_meta(0, 0, ptr[i], 0) < 0 ||
366 InsertStr(STRspace) < 0) {
368 return -1; /* error inserting */
377 if (dollar(buffer, word)) {
378 if (insert_meta(word_start, str_end, buffer, !qu) < 0)
379 return -1; /* error inserting */
385 if ((bptr = dnormalize(wordp, symlinks == SYM_IGNORE ||
386 symlinks == SYM_EXPAND)) != NULL) {
387 (void) Strcpy(buffer, bptr);
389 if (insert_meta(word_start, str_end, buffer, !qu) < 0)
390 return -1; /* error inserting */
395 case COMMAND_NORMALIZE:
396 if (!cmd_expand(wordp, buffer))
398 if (insert_meta(word_start, str_end, buffer, !qu) < 0)
399 return -1; /* error inserting */
404 search_ret = t_search(wordp, wp, LIST, space_left, looking, 1,
409 xprintf(CGETS(30, 4, "%s: Internal match error.\n"), progname);
413 } /* end tenematch */
417 * Return a list of files that match the pattern
428 gflag = 0, tglob(*v);
430 getexit(osetexit); /* make sure to come back here */
448 int fwd, i, ac = gargc;
450 for (i = 0, fwd = 0; i < ac; i++)
451 if (!executable(NULL, av[i], 0)) {
471 * Return a list of commands that match the pattern
477 Char *pat = **v, *cmd, **av;
478 Char dir[MAXPATHLEN+1];
486 av = (Char **) xmalloc((size_t) (at * sizeof(Char *)));
489 tw_cmd_start(NULL, NULL);
490 while ((cmd = tw_cmd_next(dir, &flag)) != NULL)
491 if (Gmatch(cmd, pat)) {
494 av = (Char **) xrealloc((ptr_t) av,
495 (size_t) (at * sizeof(Char *)));
497 av[ac++] = Strsave(cmd);
508 * change the word before the cursor.
509 * cp must point to the start of the unquoted word.
510 * cpend to the end of it.
511 * word is the text that has to be substituted.
513 * try to keep all the quote characters of the user's input.
514 * change quote type only if necessary.
517 insert_meta(cp, cpend, word, closequotes)
523 Char buffer[2 * FILSIZ + 1], *bptr, *wptr;
524 int in_sync = (cp != NULL);
526 int ndel = (int) (cp ? cpend - cp : 0);
530 for (bptr = buffer, wptr = word;;) {
531 if (bptr > buffer + 2 * FILSIZ - 5)
536 if (in_sync && !cmap(qu, _ESC) && cmap(*cp, _QF|_ESC))
537 if (qu == 0 || qu == *cp) {
549 if (cmap(w, _ESC | _QF))
550 wq = QUOTE; /* quotes are always quoted */
552 if (!wq && qu && tricky(w) && !(qu == '\"' && tricky_dq(w))) {
553 /* We have to unquote the character */
565 } else if (qu && w == qu) {
567 if (bptr > buffer && bptr[-1] == qu) {
568 /* User misunderstanding :) */
579 else if (wq && qu == '\"' && tricky_dq(w)) {
585 } else if (wq && ((!qu && (tricky(w) || (w == HISTSUB && bptr == buffer))) || (!cmap(qu, _ESC) && w == HIST))) {
590 if (in_sync && *cp++ != w)
593 l = NLSSize(wptr, -1);
597 if (in_sync && *cp++ != w)
606 if (closequotes && qu && !cmap(qu, _ESC))
611 return InsertStr(buffer);
612 } /* end insert_meta */
617 * return true if check matches initial chars in template
618 * This differs from PWB imatch in that if check is null
619 * it matches anything
622 is_prefix(check, template)
623 Char *check, *template;
625 for (; *check; check++, template++)
626 if ((*check & TRIM) != (*template & TRIM))
629 } /* end is_prefix */
633 * return true if check matches initial chars in template
634 * This differs from PWB imatch in that if check is null
635 * it matches anything
636 * and matches on shortening of commands
639 is_prefixmatch(check, template, igncase)
640 Char *check, *template;
645 for (; *check; check++, template++) {
646 if ((*check & TRIM) != (*template & TRIM)) {
647 MCH1 = (*check & TRIM);
648 MCH2 = (*template & TRIM);
649 MCH1 = Isupper(MCH1) ? Tolower(MCH1) : MCH1;
650 MCH2 = Isupper(MCH2) ? Tolower(MCH2) : MCH2;
652 if (!igncase && ((*check & TRIM) == '-' ||
653 (*check & TRIM) == '.' ||
654 (*check & TRIM) == '_')) {
655 MCH1 = MCH2 = (*check & TRIM);
658 } else if (MCH1 == '-') {
661 for (;*template && (*template & TRIM) != MCH1 &&
662 (*template & TRIM) != MCH2; template++)
674 } /* end is_prefixmatch */
678 * Return true if the chars in template appear at the
679 * end of check, I.e., are it's suffix.
682 is_suffix(check, template)
683 Char *check, *template;
687 for (t = template; *t++;)
689 for (c = check; *c++;)
694 if (c == check || (*--t & TRIM) != (*--c & TRIM))
697 } /* end is_suffix */
701 * Return true if this is an ignored item
710 if ((vp = adrof(STRfignore)) == NULL || (cp = vp->vec) == NULL)
712 for (; *cp != NULL; cp++)
713 if (is_suffix(item, *cp))
720 /* starting_a_command():
721 * return true if the command starting at wordstart is a command
724 starting_a_command(wordstart, inputline)
725 Char *wordstart, *inputline;
727 Char *ptr, *ncmdstart;
730 cmdstart[] = {'`', ';', '&', '(', '|', '\0'},
731 cmdalive[] = {' ', '\t', '\'', '"', '<', '>', '\0'};
734 * Find if the number of backquotes is odd or even.
736 for (ptr = wordstart, count = 0;
738 count += (*ptr-- == '`'))
741 * if the number of backquotes is even don't include the backquote char in
742 * the list of command starting delimiters [if it is zero, then it does not
745 ncmdstart = cmdstart + EVEN(count);
748 * look for the characters previous to this word if we find a command
749 * starting delimiter we break. if we find whitespace and another previous
750 * word then we are not a command
752 * count is our state machine: 0 looking for anything 1 found white-space
755 for (count = 0; wordstart >= inputline; wordstart--) {
756 if (*wordstart == '\0')
758 if (Strchr(ncmdstart, *wordstart)) {
759 for (ptr = wordstart, bsl = 0; *(--ptr) == '\\'; bsl++);
769 if ((ptr = Strchr(cmdalive, *wordstart)) != NULL)
771 if (count == 1 && !ptr)
775 if (wordstart > inputline)
776 switch (*wordstart) {
777 case '&': /* Look for >& */
778 while (wordstart > inputline &&
779 (*--wordstart == ' ' || *wordstart == '\t'))
781 if (*wordstart == '>')
784 case '(': /* check for foreach, if etc. */
785 while (wordstart > inputline &&
786 (*--wordstart == ' ' || *wordstart == '\t'))
788 if (!iscmdmeta(*wordstart) &&
789 (*wordstart != ' ' && *wordstart != '\t'))
796 } /* end starting_a_command */
800 * Object: extend what user typed up to an ambiguity.
802 * On first match, copy full item (assume it'll be the only match)
803 * On subsequent matches, shorten exp_name to the first
804 * character mismatch between exp_name and item.
805 * If we shorten it back to the prefix length, stop searching.
808 recognize(exp_name, item, name_length, numitems, enhanced, igncase)
809 Char *exp_name, *item;
810 int name_length, numitems, enhanced, igncase;
816 if (numitems == 1) { /* 1st match */
817 copyn(exp_name, item, MAXNAMLEN);
820 if (!enhanced && !igncase) {
821 for (x = exp_name, ent = item; *x && (*x & TRIM) == (*ent & TRIM); x++, ent++)
824 for (x = exp_name, ent = item; *x; x++, ent++) {
827 MCH1 = Isupper(MCH1) ? Tolower(MCH1) : MCH1;
828 MCH2 = Isupper(MCH2) ? Tolower(MCH2) : MCH2;
833 if (*x || !*ent) /* Shorter or exact match */
834 copyn(exp_name, item, MAXNAMLEN);
836 *x = '\0'; /* Shorten at 1st char diff */
837 if (!(match_unique_match || is_set(STRrecexact) || (enhanced && *ent)) && len == name_length) /* Ambiguous to prefix? */
838 return (-1); /* So stop now and save time */
840 } /* end recognize */
843 /* tw_collect_items():
844 * Collect items that match target.
846 * Returns the spelling distance of the closest match.
848 * Returns the number of items found.
849 * If none found, but some ignored items were found,
850 * It returns the -number of ignored items.
853 tw_collect_items(command, looking, exp_dir, exp_name, target, pat, flags)
856 Char *exp_dir, *exp_name, *target, *pat;
860 int done = FALSE; /* Search is done */
861 int showdots; /* Style to show dot files */
862 int nignored = 0; /* Number of fignored items */
863 int numitems = 0; /* Number of matched items */
864 int name_length = (int) Strlen(target); /* Length of prefix (file name) */
865 int exec_check = flags & TW_EXEC_CHK;/* need to check executability */
866 int dir_check = flags & TW_DIR_CHK; /* Need to check for directories */
867 int text_check = flags & TW_TEXT_CHK;/* Need to check for non-directories */
868 int dir_ok = flags & TW_DIR_OK; /* Ignore directories? */
869 int gpat = flags & TW_PAT_OK; /* Match against a pattern */
870 int ignoring = flags & TW_IGN_OK; /* Use fignore? */
871 int d = 4, nd; /* Spelling distance */
873 Char buf[MAXPATHLEN+1];
875 int len, enhanced = 0;
883 if ((ptr = varval(STRlistflags)) != STRNULL)
896 while (!done && (item = (*tw_next_entry[looking])(exp_dir, &flags))) {
898 xprintf("item = %S\n", item);
905 * Don't match . files on null prefix match
907 if (showdots == DOT_NOT && (ISDOT(item) || ISDOTDOT(item)))
909 if (name_length == 0 && item[0] == '.' && showdots == DOT_NONE)
914 #if defined(_UWIN) || defined(__CYGWIN__)
915 /* Turn foo.{exe,com,bat} into foo since UWIN's readdir returns
916 * the file with the .exe, .com, .bat extension
919 size_t ext = strlen((char *)item) - 4;
920 if ((ext > 0) && (strcasecmp((char *)&item[ext], ".exe") == 0 ||
921 strcasecmp((char *)&item[ext], ".bat") == 0 ||
922 strcasecmp((char *)&item[ext], ".com") == 0))
925 #if defined(__CYGWIN__)
926 strlwr((char *)item);
927 #endif /* __CYGWIN__ */
930 #endif /* _UWIN || __CYGWIN__ */
931 exec_check = flags & TW_EXEC_CHK;
932 dir_ok = flags & TW_DIR_OK;
946 case SPELL: /* correct the spelling of the last bit */
947 if (name_length == 0) {/* zero-length word can't be misspelled */
948 exp_name[0] = '\0';/* (not trying is important for ~) */
953 if (gpat && !Gmatch(item, pat))
956 * Swapped the order of the spdist() arguments as suggested
957 * by eeide@asylum.cs.utah.edu (Eric Eide)
959 nd = spdist(target, item); /* test the item against original */
960 if (nd <= d && nd != 4) {
961 if (!(exec_check && !executable(exp_dir, item, dir_ok))) {
962 (void) Strcpy(exp_name, item);
964 if (d == 0) /* if found it exactly */
969 if (spdir(exp_name, exp_dir, item, target)) {
970 if (exec_check && !executable(exp_dir, exp_name, dir_ok))
974 * We don't want to stop immediately, because
975 * we might find an exact/better match later.
988 case RECOGNIZE_SCROLL:
990 if ((vp = adrof(STRcomplete)) != NULL && vp->vec != NULL) {
992 for (cp = vp->vec; *cp; cp++) {
993 if (Strcmp(*cp, STRigncase) == 0)
995 if (Strcmp(*cp, STRenhance) == 0)
1000 if (enhanced || igncase) {
1001 if (!is_prefixmatch(target, item, igncase))
1004 if (!is_prefix(target, item))
1008 if (exec_check && !executable(exp_dir, item, dir_ok))
1011 if (dir_check && !isadirectory(exp_dir, item))
1014 if (text_check && isadirectory(exp_dir, item))
1018 * Only pattern match directories if we're checking
1021 if (gpat && !Gmatch(item, pat) &&
1022 (dir_check || !isadirectory(exp_dir, item)))
1026 * Remove duplicates in command listing and completion
1027 * AFEB added code for TW_LOGNAME and TW_USER cases
1029 if (looking == TW_COMMAND || looking == TW_LOGNAME
1030 || looking == TW_USER || command == LIST) {
1031 copyn(buf, item, MAXPATHLEN);
1032 len = (int) Strlen(buf);
1035 if (!(dir_ok && exec_check))
1037 if (filetype(exp_dir, item) == '/') {
1045 buf[len++] = filetype(exp_dir, item);
1052 if ((looking == TW_COMMAND || looking == TW_USER
1053 || looking == TW_LOGNAME) && tw_item_find(buf))
1056 /* maximum length 1 (NULL) + 1 (~ or $) + 1 (filetype) */
1057 ptr = tw_item_add(len + 3);
1058 copyn(ptr, buf, MAXPATHLEN);
1059 if (command == LIST)
1064 if (command == RECOGNIZE || command == RECOGNIZE_ALL ||
1065 command == RECOGNIZE_SCROLL) {
1066 if (ignoring && ignored(item)) {
1070 else if (command == RECOGNIZE_SCROLL) {
1071 add_scroll_tab(item);
1075 if (match_unique_match || is_set(STRrecexact)) {
1076 if (StrQcmp(target, item) == 0) { /* EXACT match */
1077 copyn(exp_name, item, MAXNAMLEN);
1078 numitems = 1; /* fake into expanding */
1079 non_unique_match = TRUE;
1084 if (recognize(exp_name, item, name_length, ++numitems,
1086 if (command != RECOGNIZE_SCROLL)
1088 if (enhanced && (int)Strlen(exp_name) < name_length)
1089 copyn(exp_name, target, MAXNAMLEN);
1097 xprintf("done item = %S\n", item);
1102 if (command == RECOGNIZE_SCROLL) {
1103 if ((cnt <= curchoice) || (curchoice == -1)) {
1107 } else if (numitems > 1) {
1109 curchoice = cnt - 1;
1110 choose_scroll_tab(&exp_name, cnt);
1116 if (command == SPELL)
1119 if (ignoring && numitems == 0 && nignored > 0)
1128 * Find and return the appropriate suffix character
1132 tw_suffix(looking, exp_dir, exp_name, target, name)
1134 Char *exp_dir, *exp_name, *target, *name;
1140 (void) strip(exp_name);
1149 * Don't consider array variables or empty variables
1151 if ((vp = adrof(exp_name)) != NULL && vp->vec != NULL) {
1152 if ((ptr = vp->vec[0]) == NULL || *ptr == '\0' ||
1156 else if ((ptr = tgetenv(exp_name)) == NULL || *ptr == '\0')
1161 return isadirectory(exp_dir, ptr) ? '/' : ' ';
1169 return isadirectory(exp_dir, exp_name) ? '/' : ' ';
1189 } /* end tw_suffix */
1193 * Repair a word after a spalling or a recognizwe
1196 tw_fixword(looking, word, dir, exp_name, max_word_length)
1198 Char *word, *dir, *exp_name;
1199 int max_word_length;
1205 copyn(word, STRtilde, 1);
1209 if ((ptr = Strrchr(word, '$')) != NULL)
1210 *++ptr = '\0'; /* Delete after the dollar */
1218 copyn(word, dir, max_word_length); /* put back dir part */
1226 (void) quote(exp_name);
1227 catn(word, exp_name, max_word_length); /* add extended name */
1228 } /* end tw_fixword */
1232 * Collect items. Return -1 in case we were interrupted or
1233 * the return value of tw_collect
1234 * This is really a wrapper for tw_collect_items, serving two
1236 * 1. Handles interrupt cleanups.
1237 * 2. Retries if we had no matches, but there were ignored matches
1240 tw_collect(command, looking, exp_dir, exp_name, target, pat, flags, dir_fd)
1243 Char *exp_dir, *exp_name, **target, *pat;
1247 static int ni; /* static so we don't get clobbered */
1251 xprintf("target = %S\n", *target);
1256 (*tw_start_entry[looking])(dir_fd, pat);
1257 InsideCompletion = 1;
1259 /* interrupted, clean up */
1261 InsideCompletion = 0;
1264 #if defined(SOLARIS2) && defined(i386) && !defined(__GNUC__)
1265 /* Compiler bug? (from PWP) */
1266 if ((looking == TW_LOGNAME) || (looking == TW_USER))
1269 if (looking == TW_GRPNAME)
1273 #else /* !(SOLARIS2 && i386 && !__GNUC__) */
1274 (*tw_end_entry[looking])();
1275 #endif /* !(SOLARIS2 && i386 && !__GNUC__) */
1280 if ((ni = tw_collect_items(command, looking, exp_dir, exp_name,
1283 flags & ~TW_IGN_OK)) >= 0) {
1285 InsideCompletion = 0;
1287 #if defined(SOLARIS2) && defined(i386) && !defined(__GNUC__)
1288 /* Compiler bug? (from PWP) */
1289 if ((looking == TW_LOGNAME) || (looking == TW_USER))
1292 if (looking == TW_GRPNAME)
1296 #else /* !(SOLARIS2 && i386 && !__GNUC__) */
1297 (*tw_end_entry[looking])();
1298 #endif /* !(SOLARIS2 && i386 && !__GNUC__) */
1303 } /* end tw_collect */
1307 * List the items that were found
1309 * NOTE instead of looking at numerical vars listmax and listmaxrows
1310 * we can look at numerical var listmax, and have a string value
1311 * listmaxtype (or similar) than can have values 'items' and 'rows'
1312 * (by default interpreted as 'items', for backwards compatibility)
1315 tw_list_items(looking, numitems, list_max)
1316 int looking, numitems, list_max;
1325 if ((ptr = varval(STRlistmax)) != STRNULL) {
1327 if (!Isdigit(*ptr)) {
1331 max_items = max_items * 10 + *ptr++ - '0';
1333 if ((max_items > 0) && (numitems > max_items) && list_max)
1334 max_items = numitems;
1339 if (max_items == 0 && (ptr = varval(STRlistmaxrows)) != STRNULL) {
1343 if (!Isdigit(*ptr)) {
1347 max_rows = max_rows * 10 + *ptr++ - '0';
1349 if (max_rows != 0 && looking != TW_JOB)
1350 rows = find_rows(tw_item_get(), numitems, TRUE);
1352 rows = numitems; /* underestimate for lines wider than the termH */
1353 if ((max_rows > 0) && (rows > max_rows) && list_max)
1360 if (max_items || max_rows) {
1366 name = CGETS(30, 5, "items");
1370 name = CGETS(30, 6, "rows");
1374 sname = strsave(name);
1375 xprintf(CGETS(30, 7, "There are %d %s, list them anyway? [n/y] "),
1379 /* We should be in Rawmode here, so no \n to catch */
1380 (void) read(SHIN, &tc, 1);
1381 xprintf("%c\r\n", tc); /* echo the char, do a newline */
1383 * Perhaps we should use the yesexpr from the
1386 if (strchr(CGETS(30, 13, "Yy"), tc) == NULL)
1390 if (looking != TW_SIGNAL)
1391 qsort((ptr_t) tw_item_get(), (size_t) numitems, sizeof(Char *),
1392 (int (*) __P((const void *, const void *))) fcompare);
1393 if (looking != TW_JOB)
1394 print_by_column(STRNULL, tw_item_get(), numitems, TRUE);
1397 * print one item on every line because jobs can have spaces
1398 * and it is confusing.
1401 Char **w = tw_item_get();
1403 for (i = 0; i < numitems; i++) {
1404 xprintf("%S", w[i]);
1410 } /* end tw_list_items */
1414 * Perform a RECOGNIZE, LIST or SPELL command on string "word".
1417 * >= 0: SPELL command: "distance" (see spdist())
1418 * other: No. of items found
1419 * < 0: Error (message or beep is output)
1423 t_search(word, wp, command, max_word_length, looking, list_max, pat, suf)
1424 Char *word, *wp; /* original end-of-word */
1426 int max_word_length, looking, list_max;
1430 int numitems, /* Number of items matched */
1431 flags = 0, /* search flags */
1432 gpat = pat[0] != '\0', /* Glob pattern search */
1433 nd; /* Normalized directory return */
1434 Char exp_dir[FILSIZ + 1], /* dir after ~ expansion */
1435 dir[FILSIZ + 1], /* /x/y/z/ part in /x/y/z/f */
1436 exp_name[MAXNAMLEN + 1], /* the recognized (extended) */
1437 name[MAXNAMLEN + 1], /* f part in /d/d/d/f name */
1438 *target; /* Target to expand/correct/list */
1444 * bugfix by Marty Grossman (grossman@CC5.BBN.COM): directory listing can
1445 * dump core when interrupted
1449 non_unique_match = FALSE; /* See the recexact code below */
1451 extract_dir_and_name(word, dir, name);
1454 * SPECIAL HARDCODED COMPLETIONS:
1455 * foo$variable -> TW_VARIABLE
1456 * ~user -> TW_LOGNAME
1459 if ((*word == '~') && (Strchr(word, '/') == NULL)) {
1460 looking = TW_LOGNAME;
1462 gpat = 0; /* Override pattern mechanism */
1464 else if ((target = Strrchr(name, '$')) != 0 &&
1465 (Strchr(name, '/') == NULL)) {
1467 looking = TW_VARIABLE;
1468 gpat = 0; /* Override pattern mechanism */
1474 * Try to figure out what we should be looking for
1476 if (looking & TW_PATH) {
1477 gpat = 0; /* pattern holds the pathname to be used */
1478 copyn(exp_dir, pat, MAXNAMLEN);
1479 if (exp_dir[Strlen(exp_dir) - 1] != '/')
1480 catn(exp_dir, STRslash, MAXNAMLEN);
1481 catn(exp_dir, dir, MAXNAMLEN);
1486 switch (looking & ~TW_PATH) {
1495 if (Strchr(word, '/') || (looking & TW_PATH)) {
1497 flags |= TW_EXEC_CHK;
1501 /* PWP: don't even bother when doing ALL of the commands */
1502 if (looking == TW_COMMAND && (*word == '\0'))
1510 gpat = 0; /* pattern holds the name of the variable */
1514 if (command == LIST && pat != NULL) {
1527 * let fignore work only when we are not using a pattern
1529 flags |= (gpat == 0) ? TW_IGN_OK : TW_PAT_OK;
1532 xprintf(CGETS(30, 8, "looking = %d\n"), looking);
1549 if ((nd = expand_dir(dir, exp_dir, &dir_fd, command)) != 0)
1554 flags |= TW_DIR_CHK;
1558 * This is supposed to expand the directory stack.
1561 * 2. directories with the same name
1567 * Supposed to do delayed expansion, but it is inconsistent
1568 * from a user-interface point of view, since it does not
1569 * immediately obey addsuffix
1571 if ((nd = expand_dir(dir, exp_dir, &dir_fd, command)) != 0)
1573 if (isadirectory(exp_dir, name)) {
1574 if (exp_dir[0] != '\0' || name[0] != '\0') {
1575 catn(dir, name, MAXNAMLEN);
1576 if (dir[Strlen(dir) - 1] != '/')
1577 catn(dir, STRslash, MAXNAMLEN);
1578 if ((nd = expand_dir(dir, exp_dir, &dir_fd, command)) != 0)
1580 if (word[Strlen(word) - 1] != '/')
1581 catn(word, STRslash, MAXNAMLEN);
1586 if ((nd = expand_dir(dir, exp_dir, &dir_fd, command)) != 0)
1591 flags |= TW_TEXT_CHK;
1594 if ((nd = expand_dir(dir, exp_dir, &dir_fd, command)) != 0)
1598 case TW_PATH | TW_TEXT:
1599 case TW_PATH | TW_FILE:
1600 case TW_PATH | TW_DIRECTORY:
1601 case TW_PATH | TW_COMMAND:
1602 if ((dir_fd = opendir(short2str(exp_dir))) == NULL) {
1603 if (command == RECOGNIZE)
1605 xprintf("%S: %s", exp_dir, strerror(errno));
1606 if (command != RECOGNIZE)
1611 if (exp_dir[Strlen(exp_dir) - 1] != '/')
1612 catn(exp_dir, STRslash, MAXNAMLEN);
1614 looking &= ~TW_PATH;
1618 flags |= TW_TEXT_CHK;
1625 flags |= TW_DIR_CHK;
1629 copyn(target, word, MAXNAMLEN); /* so it can match things */
1633 abort(); /* Cannot happen */
1643 * Check if the spelling was already correct
1644 * From: Rob McMahon <cudcv@cu.warwick.ac.uk>
1646 if (command == SPELL && getpwnam(short2str(word)) != NULL) {
1652 copyn(name, word, MAXNAMLEN); /* name sans ~ */
1653 if (looking == TW_LOGNAME)
1660 copyn(target, word, MAXNAMLEN); /* so it can match things */
1664 xprintf(CGETS(30, 9,
1665 "\n%s internal error: I don't know what I'm looking for!\n"),
1671 numitems = tw_collect(command, looking, exp_dir, exp_name,
1672 &target, pat, flags, dir_fd);
1679 case RECOGNIZE_SCROLL:
1683 tw_fixword(looking, word, dir, exp_name, max_word_length);
1685 if (!match_unique_match && is_set(STRaddsuffix) && numitems == 1) {
1690 case 0: /* Automatic suffix */
1691 suffix[0] = tw_suffix(looking, exp_dir, exp_name, target, name);
1694 case -1: /* No suffix */
1697 default: /* completion specified suffix */
1698 suffix[0] = (Char) suf;
1701 catn(word, suffix, max_word_length);
1706 tw_list_items(looking, numitems, list_max);
1711 tw_fixword(looking, word, dir, exp_name, max_word_length);
1715 xprintf("Bad tw_command\n");
1718 } /* end t_search */
1721 /* extract_dir_and_name():
1722 * parse full path in file into 2 parts: directory and file names
1723 * Should leave final slash (/) at end of dir.
1726 extract_dir_and_name(path, dir, name)
1727 Char *path, *dir, *name;
1731 p = Strrchr(path, '/');
1734 p = Strrchr(path, ':');
1735 #endif /* WINNT_NATIVE */
1737 copyn(name, path, MAXNAMLEN);
1742 copyn(name, p, MAXNAMLEN);
1743 copyn(dir, path, p - path);
1745 } /* end extract_dir_and_name */
1749 * expand "/$old1/$old2/old3/"
1750 * to "/value_of_old1/value_of_old2/old3/"
1760 for (space = FILSIZ, p = new; *old && space > 0;)
1766 if (expdollar(&p, &old, &space, QUOTE) == NULL)
1775 * expand ~person/foo to home_directory_of_person/foo
1776 * or =<stack-entry> to <dir in stack entry>
1786 for (p = new, o = &old[1]; *o && *o != '/'; *p++ = *o++)
1794 /* Special case: if the home directory expands to "/", we do
1795 * not want to create "//" by appending a slash from o.
1797 if (new[0] == '/' && new[1] == '\0' && *o == '/')
1800 (void) Strcat(new, o);
1804 if ((p = globequal(new, old)) == NULL) {
1813 (void) Strcpy(new, old);
1820 * Open the directory given, expanding ~user and $var
1821 * Optionally normalize the path given
1824 expand_dir(dir, edir, dfd, cmd)
1830 Char tdir[MAXPATHLEN + 1];
1832 if ((dollar(tdir, dir) == 0) ||
1833 (tilde(edir, tdir) == 0) ||
1834 !(nd = dnormalize(*edir ? edir : STRdot, symlinks == SYM_IGNORE ||
1835 symlinks == SYM_EXPAND)) ||
1836 ((*dfd = opendir(short2str(nd))) == NULL)) {
1838 if (cmd == SPELL || SearchNoDirErr)
1841 * From: Amos Shapira <amoss@cs.huji.ac.il>
1842 * Print a better message when completion fails
1844 xprintf("\n%S %s\n",
1846 (*tdir ? tdir : dir),
1847 (errno == ENOTDIR ? CGETS(30, 10, "not a directory") :
1848 (errno == ENOENT ? CGETS(30, 11, "not found") :
1849 CGETS(30, 12, "unreadable"))));
1858 * Copy and append a / if there was one
1860 for (p = edir; *p; p++)
1863 for (p = nd; *p; p++)
1868 for (d = edir, s = nd; (*d++ = *s++) != '\0';)
1878 } /* end expand_dir */
1882 * Returns true if the directory should not be stat'd,
1884 * This way, things won't grind to a halt when you complete in /afs
1885 * or very large directories.
1894 if ((vp = adrof(STRnostat)) == NULL || (cp = vp->vec) == NULL)
1896 for (; *cp != NULL; cp++) {
1897 if (Strcmp(*cp, STRstar) == 0)
1899 if (Gmatch(dir, *cp))
1907 * Return a character that signifies a filetype
1908 * symbology from 4.3 ls command.
1920 * From: veals@crchh84d.bnr.ca (Percy Veals)
1921 * An extra stat is required for HPUX CDF files.
1923 struct stat hpstatb;
1924 #endif /* S_ISCDF */
1926 if (nostat(dir)) return(' ');
1928 (void) Strcpy(path, dir);
1929 catn(path, file, (int) (sizeof(path) / sizeof(Char)));
1931 if (lstat(ptr = short2str(path), &statb) != -1)
1932 /* see above #define of lstat */
1935 if (S_ISLNK(statb.st_mode)) { /* Symbolic link */
1936 if (adrof(STRlistlinks)) {
1937 if (stat(ptr, &statb) == -1)
1939 else if (S_ISDIR(statb.st_mode))
1949 if (S_ISSOCK(statb.st_mode)) /* Socket */
1953 if (S_ISFIFO(statb.st_mode)) /* Named Pipe */
1957 if (S_ISHIDDEN(statb.st_mode)) /* Hidden Directory [aix] */
1961 (void) strcat(ptr, "+"); /* Must append a '+' and re-stat(). */
1962 if ((stat(ptr, &hpstatb) != -1) && S_ISCDF(hpstatb.st_mode))
1963 return ('+'); /* Context Dependent Files [hpux] */
1966 if (S_ISNWK(statb.st_mode)) /* Network Special [hpux] */
1970 if (S_ISCHR(statb.st_mode)) /* char device */
1974 if (S_ISBLK(statb.st_mode)) /* block device */
1978 if (S_ISDIR(statb.st_mode)) /* normal Directory */
1981 if (statb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))
1986 } /* end filetype */
1990 * Return trus if the file is a directory
1993 isadirectory(dir, file) /* return 1 if dir/file is a directory */
1994 Char *dir, *file; /* uses stat rather than lstat to get dest. */
1997 Char path[MAXPATHLEN];
2000 (void) Strcpy(path, dir);
2001 catn(path, file, (int) (sizeof(path) / sizeof(Char)));
2002 if (stat(short2str(path), &statb) >= 0) { /* resolve through
2005 if (S_ISSOCK(statb.st_mode)) /* Socket */
2009 if (S_ISFIFO(statb.st_mode)) /* Named Pipe */
2012 if (S_ISDIR(statb.st_mode)) /* normal Directory */
2017 } /* end isadirectory */
2022 * Return how many rows needed to print sorted down columns
2025 find_rows(items, count, no_file_suffix)
2027 int count, no_file_suffix;
2029 int i, columns, rows;
2030 unsigned int maxwidth = 0;
2032 for (i = 0; i < count; i++) /* find widest string */
2033 maxwidth = max(maxwidth, (unsigned int) Strlen(items[i]));
2035 maxwidth += no_file_suffix ? 1 : 2; /* for the file tag and space */
2036 columns = (TermH + 1) / maxwidth; /* PWP: terminal size change */
2039 rows = (count + (columns - 1)) / columns;
2042 } /* end rows_needed_by_print_by_column */
2045 /* print_by_column():
2046 * Print sorted down columns or across columns when the first
2047 * word of $listflags shell variable contains 'x'.
2051 print_by_column(dir, items, count, no_file_suffix)
2052 Char *dir, *items[];
2053 int count, no_file_suffix;
2055 int i, r, c, columns, rows;
2056 unsigned int w, wx, maxwidth = 0;
2060 lbuffed = 0; /* turn off line buffering */
2063 across = ((val = varval(STRlistflags)) != STRNULL) &&
2064 (Strchr(val, 'x') != NULL);
2066 for (i = 0; i < count; i++) { /* find widest string */
2067 maxwidth = max(maxwidth, (unsigned int) NLSStringWidth(items[i]));
2070 maxwidth += no_file_suffix ? 1 : 2; /* for the file tag and space */
2071 columns = TermH / maxwidth; /* PWP: terminal size change */
2072 if (!columns || !isatty(didfds ? 1 : SHOUT))
2074 rows = (count + (columns - 1)) / columns;
2077 for (r = 0; r < rows; r++) {
2078 for (c = 0; c < columns; c++) {
2079 i = across ? (i + 1) : (c * rows + r);
2083 w = (unsigned int) Strlen(items[i]);
2086 if (no_file_suffix) {
2087 /* Print the command name */
2088 Char f = items[i][w - 1];
2089 items[i][w - 1] = 0;
2090 print_with_color(items[i], w - 1, f);
2093 /* Print filename followed by '/' or '*' or ' ' */
2094 print_with_color(items[i], w, filetype(dir, items[i]));
2097 #else /* ifndef COLOR_LS_F */
2098 if (no_file_suffix) {
2099 /* Print the command name */
2100 xprintf("%S", items[i]);
2103 /* Print filename followed by '/' or '*' or ' ' */
2104 xprintf("%-S%c", items[i],
2105 filetype(dir, items[i]));
2108 #endif /* COLOR_LS_F */
2110 if (c < (columns - 1)) { /* Not last column? */
2111 w = (unsigned int) NLSStringWidth(items[i]) + wx;
2112 for (; w < maxwidth; w++)
2124 lbuffed = 1; /* turn back on line buffering */
2126 } /* end print_by_column */
2130 * Compare strings ignoring the quoting chars
2134 const Char *str1, *str2;
2136 for (; *str1 && samecase(*str1 & TRIM) == samecase(*str2 & TRIM);
2140 * The following case analysis is necessary so that characters which look
2141 * negative collate low against normal characters but high against the
2142 * end-of-string NUL.
2144 if (*str1 == '\0' && *str2 == '\0')
2146 else if (*str1 == '\0')
2148 else if (*str2 == '\0')
2151 return ((*str1 & TRIM) - (*str2 & TRIM));
2156 * Comparison routine for qsort
2159 fcompare(file1, file2)
2160 Char **file1, **file2;
2162 return (int) collate(*file1, *file2);
2163 } /* end fcompare */
2167 * Concatenate src onto tail of des.
2168 * Des is a string whose maximum length is count.
2169 * Always null terminate.
2172 catn(des, src, count)
2177 while (--count >= 0 && *des)
2179 while (--count >= 0)
2180 if ((*des++ = *src++) == 0)
2187 * like strncpy but always leave room for trailing \0
2188 * and always null terminate.
2191 copyn(des, src, count)
2195 while (--count >= 0)
2196 if ((*des++ = *src++) == 0)
2203 * like it's normal string counter-part
2204 * [apollo uses that in tc.os.c, so it cannot be static]
2215 /* Search the STR_environ for the entry matching str. */
2216 for (var = STR_environ; var != NULL && *var != NULL; var++)
2217 if (Strlen(*var) >= len && (*var)[len] == '=') {
2218 /* Temporarily terminate the string so we can copy the variable
2221 res = StrQcmp(*var, str);
2222 /* Restore the '=' and return a pointer to the value of the
2223 environment variable. */
2226 return (&((*var)[len + 1]));
2232 struct scroll_tab_list *scroll_tab = 0;
2235 add_scroll_tab(item)
2238 struct scroll_tab_list *new_scroll;
2240 new_scroll = (struct scroll_tab_list *) xmalloc((size_t)
2241 sizeof(struct scroll_tab_list));
2242 new_scroll->element = Strsave(item);
2243 new_scroll->next = scroll_tab;
2244 scroll_tab = new_scroll;
2248 choose_scroll_tab(exp_name, cnt)
2252 struct scroll_tab_list *loop;
2256 ptr = (Char **) xmalloc((size_t) sizeof(Char *) * cnt);
2258 for(loop = scroll_tab; loop && (tmp >= 0); loop = loop->next)
2259 ptr[--tmp] = loop->element;
2261 qsort((ptr_t) ptr, (size_t) cnt, sizeof(Char *),
2262 (int (*) __P((const void *, const void *))) fcompare);
2264 copyn(*exp_name, ptr[curchoice], (int) Strlen(ptr[curchoice]));
2271 struct scroll_tab_list *loop;
2275 scroll_tab = scroll_tab->next;
2276 xfree((ptr_t) loop->element);
2277 xfree((ptr_t) loop);