📄 pushd.def
字号:
intdirs_builtin (list) WORD_LIST *list;{ int flags, desired_index, index_flag, vflag; intmax_t i; char *temp, *w; for (flags = vflag = index_flag = 0, desired_index = -1, w = ""; list; list = list->next) { if (ISOPTION (list->word->word, 'l')) { flags |= LONGFORM; } else if (ISOPTION (list->word->word, 'c')) { flags |= CLEARSTAK; } else if (ISOPTION (list->word->word, 'v')) { vflag |= 2; } else if (ISOPTION (list->word->word, 'p')) { vflag |= 1; } else if (ISOPTION (list->word->word, '-')) { list = list->next; break; } else if (*list->word->word == '+' || *list->word->word == '-') { int sign; if (legal_number (w = list->word->word + 1, &i) == 0) { sh_invalidnum (list->word->word); builtin_usage (); return (EXECUTION_FAILURE); } sign = (*list->word->word == '+') ? 1 : -1; desired_index = get_dirstack_index (i, sign, &index_flag); } else { sh_invalidopt (list->word->word); builtin_usage (); return (EXECUTION_FAILURE); } } if (flags & CLEARSTAK) { clear_directory_stack (); return (EXECUTION_SUCCESS); } if (index_flag && (desired_index < 0 || desired_index > directory_list_offset)) { pushd_error (directory_list_offset, w); return (EXECUTION_FAILURE); }#define DIRSTACK_FORMAT(temp) \ (flags & LONGFORM) ? temp : polite_directory_format (temp) /* The first directory printed is always the current working directory. */ if (index_flag == 0 || (index_flag == 1 && desired_index == 0)) { temp = get_working_directory ("dirs"); if (temp == 0) temp = savestring (_("<no current directory>")); if (vflag & 2) printf ("%2d %s", 0, DIRSTACK_FORMAT (temp)); else printf ("%s", DIRSTACK_FORMAT (temp)); free (temp); if (index_flag) { putchar ('\n'); return (sh_chkwrite (EXECUTION_SUCCESS)); } }#define DIRSTACK_ENTRY(i) \ (flags & LONGFORM) ? pushd_directory_list[i] \ : polite_directory_format (pushd_directory_list[i]) /* Now print the requested directory stack entries. */ if (index_flag) { if (vflag & 2) printf ("%2d %s", directory_list_offset - desired_index, DIRSTACK_ENTRY (desired_index)); else printf ("%s", DIRSTACK_ENTRY (desired_index)); } else for (i = directory_list_offset - 1; i >= 0; i--) if (vflag >= 2) printf ("\n%2d %s", directory_list_offset - (int)i, DIRSTACK_ENTRY (i)); else printf ("%s%s", (vflag & 1) ? "\n" : " ", DIRSTACK_ENTRY (i)); putchar ('\n'); return (sh_chkwrite (EXECUTION_SUCCESS));}static voidpushd_error (offset, arg) int offset; char *arg;{ if (offset == 0) builtin_error (_("directory stack empty")); else sh_erange (arg, _("directory stack index"));}static voidclear_directory_stack (){ register int i; for (i = 0; i < directory_list_offset; i++) free (pushd_directory_list[i]); directory_list_offset = 0;}/* Switch to the directory in NAME. This uses the cd_builtin to do the work, so if the result is EXECUTION_FAILURE then an error message has already been printed. */static intcd_to_string (name) char *name;{ WORD_LIST *tlist; WORD_LIST *dir; int result; dir = make_word_list (make_word (name), NULL); tlist = make_word_list (make_word ("--"), dir); result = cd_builtin (tlist); dispose_words (tlist); return (result);}static intchange_to_temp (temp) char *temp;{ int tt; tt = temp ? cd_to_string (temp) : EXECUTION_FAILURE; if (tt == EXECUTION_SUCCESS) dirs_builtin ((WORD_LIST *)NULL); return (tt);}static voidadd_dirstack_element (dir) char *dir;{ if (directory_list_offset == directory_list_size) pushd_directory_list = strvec_resize (pushd_directory_list, directory_list_size += 10); pushd_directory_list[directory_list_offset++] = dir;}static intget_dirstack_index (ind, sign, indexp) intmax_t ind; int sign, *indexp;{ if (indexp) *indexp = sign > 0 ? 1 : 2; /* dirs +0 prints the current working directory. */ /* dirs -0 prints last element in directory stack */ if (ind == 0 && sign > 0) return 0; else if (ind == directory_list_offset) { if (indexp) *indexp = sign > 0 ? 2 : 1; return 0; } else if (ind >= 0 && ind <= directory_list_offset) return (sign > 0 ? directory_list_offset - ind : ind); else return -1;}/* Used by the tilde expansion code. */char *get_dirstack_from_string (string) char *string;{ int ind, sign, index_flag; intmax_t i; sign = 1; if (*string == '-' || *string == '+') { sign = (*string == '-') ? -1 : 1; string++; } if (legal_number (string, &i) == 0) return ((char *)NULL); index_flag = 0; ind = get_dirstack_index (i, sign, &index_flag); if (index_flag && (ind < 0 || ind > directory_list_offset)) return ((char *)NULL); if (index_flag == 0 || (index_flag == 1 && ind == 0)) return (get_string_value ("PWD")); else return (pushd_directory_list[ind]);}#ifdef INCLUDE_UNUSEDchar *get_dirstack_element (ind, sign) intmax_t ind; int sign;{ int i; i = get_dirstack_index (ind, sign, (int *)NULL); return (i < 0 || i > directory_list_offset) ? (char *)NULL : pushd_directory_list[i];}#endifvoidset_dirstack_element (ind, sign, value) intmax_t ind; int sign; char *value;{ int i; i = get_dirstack_index (ind, sign, (int *)NULL); if (ind == 0 || i < 0 || i > directory_list_offset) return; free (pushd_directory_list[i]); pushd_directory_list[i] = savestring (value);}WORD_LIST *get_directory_stack (flags) int flags;{ register int i; WORD_LIST *ret; char *d, *t; for (ret = (WORD_LIST *)NULL, i = 0; i < directory_list_offset; i++) { d = (flags&1) ? polite_directory_format (pushd_directory_list[i]) : pushd_directory_list[i]; ret = make_word_list (make_word (d), ret); } /* Now the current directory. */ d = get_working_directory ("dirstack"); i = 0; /* sentinel to decide whether or not to free d */ if (d == 0) d = "."; else { t = polite_directory_format (d); /* polite_directory_format sometimes returns its argument unchanged. If it does not, we can free d right away. If it does, we need to mark d to be deleted later. */ if (t != d) { free (d); d = t; } else /* t == d, so d is what we want */ i = 1; } ret = make_word_list (make_word (d), ret); if (i) free (d); return ret; /* was (REVERSE_LIST (ret, (WORD_LIST *)); */}#ifdef LOADABLE_BUILTINchar * const dirs_doc[] = {N_("Display the list of currently remembered directories. Directories\n\ find their way onto the list with the `pushd' command; you can get\n\ back up through the list with the `popd' command.\n\ \n\ Options:\n\ -c clear the directory stack by deleting all of the elements\n\ -l do not print tilde-prefixed versions of directories relative\n\ to your home directory\n\ -p print the directory stack with one entry per line\n\ -v print the directory stack with one entry per line prefixed\n\ with its position in the stack\n\ \n\ Arguments:\n\ +N Displays the Nth entry counting from the left of the list shown by\n\ dirs when invoked without options, starting with zero.\n\ \n\ -N Displays the Nth entry counting from the right of the list shown by\n\ dirs when invoked without options, starting with zero."), (char *)NULL};char * const pushd_doc[] = {N_("Adds a directory to the top of the directory stack, or rotates\n\ the stack, making the new top of the stack the current working\n\ directory. With no arguments, exchanges the top two directories.\n\ \n\ Options:\n\ -n Suppresses the normal change of directory when adding\n\ directories to the stack, so only the stack is manipulated.\n\ \n\ Arguments:\n\ +N Rotates the stack so that the Nth directory (counting\n\ from the left of the list shown by `dirs', starting with\n\ zero) is at the top.\n\ \n\ -N Rotates the stack so that the Nth directory (counting\n\ from the right of the list shown by `dirs', starting with\n\ zero) is at the top.\n\ \n\ dir Adds DIR to the directory stack at the top, making it the\n\ new current working directory.\n\ \n\ The `dirs' builtin displays the directory stack."), (char *)NULL};char * const popd_doc[] = {N_("Removes entries from the directory stack. With no arguments, removes\n\ the top directory from the stack, and changes to the new top directory.\n\ \n\ Options:\n\ -n Suppresses the normal change of directory when removing\n\ directories from the stack, so only the stack is manipulated.\n\ \n\ Arguments:\n\ +N Removes the Nth entry counting from the left of the list\n\ shown by `dirs', starting with zero. For example: `popd +0'\n\ removes the first directory, `popd +1' the second.\n\ \n\ -N Removes the Nth entry counting from the right of the list\n\ shown by `dirs', starting with zero. For example: `popd -0'\n\ removes the last directory, `popd -1' the next to last.\n\ \n\ The `dirs' builtin displays the directory stack."), (char *)NULL};struct builtin pushd_struct = { "pushd", pushd_builtin, BUILTIN_ENABLED, pushd_doc, "pushd [+N | -N] [-n] [dir]", 0};struct builtin popd_struct = { "popd", popd_builtin, BUILTIN_ENABLED, popd_doc, "popd [+N | -N] [-n]", 0};struct builtin dirs_struct = { "dirs", dirs_builtin, BUILTIN_ENABLED, dirs_doc, "dirs [-clpv] [+N] [-N]", 0};#endif /* LOADABLE_BUILTIN */#endif /* PUSHD_AND_POPD */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -