1 /* pushd.c, created from pushd.def. */
7 #if defined (HAVE_UNISTD_H)
15 #include <tilde/tilde.h>
26 static char *m_badarg = "%s: bad argument";
28 /* The list of remembered directories. */
29 static char **pushd_directory_list = (char **)NULL;
31 /* Number of existing slots in this list. */
32 static int directory_list_size;
34 /* Offset to the end of the list. */
35 static int directory_list_offset;
37 static void pushd_error ();
38 static void clear_directory_stack ();
39 static int cd_to_string ();
40 static int change_to_temp ();
41 static int get_dirstack_index ();
42 static void add_dirstack_element ();
47 #define CLEARSTAK 0x08
53 char *temp, *current_directory, *top;
58 /* If there is no argument list then switch current and
62 if (directory_list_offset == 0)
64 builtin_error ("no other directory");
65 return (EXECUTION_FAILURE);
68 current_directory = get_working_directory ("pushd");
69 if (current_directory == 0)
70 return (EXECUTION_FAILURE);
72 j = directory_list_offset - 1;
73 temp = pushd_directory_list[j];
74 pushd_directory_list[j] = current_directory;
75 j = change_to_temp (temp);
80 for (flags = 0; list; list = list->next)
82 if (ISOPTION (list->word->word, 'n'))
86 else if (ISOPTION (list->word->word, '-'))
91 else if (list->word->word[0] == '-' && list->word->word[1] == '\0')
92 /* Let `pushd -' work like it used to. */
94 else if (((direction = list->word->word[0]) == '+') || direction == '-')
96 if (legal_number (list->word->word + 1, &num) == 0)
98 builtin_error (m_badarg, list->word->word);
100 return (EXECUTION_FAILURE);
103 if (direction == '-')
104 num = directory_list_offset - num;
106 if (num > directory_list_offset || num < 0)
108 pushd_error (directory_list_offset, list->word->word);
109 return (EXECUTION_FAILURE);
113 else if (*list->word->word == '-')
115 bad_option (list->word->word);
117 return (EXECUTION_FAILURE);
125 /* Rotate the stack num times. Remember, the current
126 directory acts like it is part of the stack. */
127 temp = get_working_directory ("pushd");
131 j = ((flags & NOCD) == 0) ? change_to_temp (temp) : EXECUTION_SUCCESS;
138 top = pushd_directory_list[directory_list_offset - 1];
140 for (j = directory_list_offset - 2; j > -1; j--)
141 pushd_directory_list[j + 1] = pushd_directory_list[j];
143 pushd_directory_list[j + 1] = temp;
150 j = ((flags & NOCD) == 0) ? change_to_temp (temp) : EXECUTION_SUCCESS;
156 return (EXECUTION_SUCCESS);
158 /* Change to the directory in list->word->word. Save the current
159 directory on the top of the stack. */
160 current_directory = get_working_directory ("pushd");
161 if (current_directory == 0)
162 return (EXECUTION_FAILURE);
164 j = ((flags & NOCD) == 0) ? cd_builtin (list) : EXECUTION_SUCCESS;
165 if (j == EXECUTION_SUCCESS)
167 add_dirstack_element ((flags & NOCD) ? savestring (list->word->word) : current_directory);
168 dirs_builtin ((WORD_LIST *)NULL);
169 return (EXECUTION_SUCCESS);
173 free (current_directory);
174 return (EXECUTION_FAILURE);
178 /* Pop the directory stack, and then change to the new top of the stack.
179 If LIST is non-null it should consist of a word +N or -N, which says
180 what element to delete from the stack. The default is the top one. */
190 for (flags = 0, which = 0L, direction = '+'; list; list = list->next)
192 if (ISOPTION (list->word->word, 'n'))
196 else if (ISOPTION (list->word->word, '-'))
201 else if (((direction = list->word->word[0]) == '+') || direction == '-')
203 if (legal_number (list->word->word + 1, &which) == 0)
205 builtin_error (m_badarg, list->word->word);
207 return (EXECUTION_FAILURE);
210 else if (*list->word->word == '-')
212 bad_option (list->word->word);
214 return (EXECUTION_FAILURE);
220 if (which > directory_list_offset || (directory_list_offset == 0 && which == 0))
222 pushd_error (directory_list_offset, list ? list->word->word : "");
223 return (EXECUTION_FAILURE);
226 /* Handle case of no specification, or top of stack specification. */
227 if ((direction == '+' && which == 0) ||
228 (direction == '-' && which == directory_list_offset))
230 i = ((flags & NOCD) == 0) ? cd_to_string (pushd_directory_list[directory_list_offset - 1])
232 if (i != EXECUTION_SUCCESS)
234 free (pushd_directory_list[--directory_list_offset]);
238 /* Since an offset other than the top directory was specified,
239 remove that directory from the list and shift the remainder
240 of the list into place. */
241 i = (direction == '+') ? directory_list_offset - which : which;
242 free (pushd_directory_list[i]);
243 directory_list_offset--;
245 /* Shift the remainder of the list into place. */
246 for (; i < directory_list_offset; i++)
247 pushd_directory_list[i] = pushd_directory_list[i + 1];
250 dirs_builtin ((WORD_LIST *)NULL);
251 return (EXECUTION_SUCCESS);
254 /* Print the current list of directories on the directory stack. */
259 int flags, desired_index, index_flag, vflag;
263 for (flags = vflag = index_flag = 0, desired_index = -1, w = ""; list; list = list->next)
265 if (ISOPTION (list->word->word, 'l'))
269 else if (ISOPTION (list->word->word, 'c'))
273 else if (ISOPTION (list->word->word, 'v'))
277 else if (ISOPTION (list->word->word, 'p'))
281 else if (ISOPTION (list->word->word, '-'))
286 else if (*list->word->word == '+' || *list->word->word == '-')
289 if (legal_number (w = list->word->word + 1, &i) == 0)
291 builtin_error (m_badarg, list->word->word);
293 return (EXECUTION_FAILURE);
295 sign = (*list->word->word == '+') ? 1 : -1;
296 desired_index = get_dirstack_index (i, sign, &index_flag);
300 bad_option (list->word->word);
302 return (EXECUTION_FAILURE);
306 if (flags & CLEARSTAK)
308 clear_directory_stack ();
309 return (EXECUTION_SUCCESS);
312 if (index_flag && (desired_index < 0 || desired_index > directory_list_offset))
314 pushd_error (directory_list_offset, w);
315 return (EXECUTION_FAILURE);
318 #define DIRSTACK_FORMAT(temp) \
319 (flags & LONGFORM) ? temp : polite_directory_format (temp)
321 /* The first directory printed is always the current working directory. */
322 if (index_flag == 0 || (index_flag == 1 && desired_index == 0))
324 temp = get_working_directory ("dirs");
326 temp = savestring ("<no current directory>");
328 printf ("%2d %s", 0, DIRSTACK_FORMAT (temp));
330 printf ("%s", DIRSTACK_FORMAT (temp));
335 return EXECUTION_SUCCESS;
339 #define DIRSTACK_ENTRY(i) \
340 (flags & LONGFORM) ? pushd_directory_list[i] \
341 : polite_directory_format (pushd_directory_list[i])
343 /* Now print the requested directory stack entries. */
347 printf ("%2d %s", directory_list_offset - desired_index,
348 DIRSTACK_ENTRY (desired_index));
350 printf ("%s", DIRSTACK_ENTRY (desired_index));
353 for (i = directory_list_offset - 1; i >= 0; i--)
355 printf ("\n%2d %s", directory_list_offset - (int)i, DIRSTACK_ENTRY (i));
357 printf ("%s%s", (vflag & 1) ? "\n" : " ", DIRSTACK_ENTRY (i));
361 return (EXECUTION_SUCCESS);
365 pushd_error (offset, arg)
370 builtin_error ("directory stack empty");
372 builtin_error ("%s: bad directory stack index", arg);
374 builtin_error ("bad directory stack index");
378 clear_directory_stack ()
382 for (i = 0; i < directory_list_offset; i++)
383 free (pushd_directory_list[i]);
384 directory_list_offset = 0;
387 /* Switch to the directory in NAME. This uses the cd_builtin to do the work,
388 so if the result is EXECUTION_FAILURE then an error message has already
397 tlist = make_word_list (make_word (name), NULL);
398 result = cd_builtin (tlist);
399 dispose_words (tlist);
404 change_to_temp (temp)
409 tt = temp ? cd_to_string (temp) : EXECUTION_FAILURE;
411 if (tt == EXECUTION_SUCCESS)
412 dirs_builtin ((WORD_LIST *)NULL);
418 add_dirstack_element (dir)
423 if (directory_list_offset == directory_list_size)
425 j = (directory_list_size += 10) * sizeof (char *);
426 pushd_directory_list = (char **)xrealloc (pushd_directory_list, j);
428 pushd_directory_list[directory_list_offset++] = dir;
432 get_dirstack_index (ind, sign, indexp)
433 int ind, sign, *indexp;
436 *indexp = sign > 0 ? 1 : 2;
438 /* dirs +0 prints the current working directory. */
439 /* dirs -0 prints last element in directory stack */
440 if (ind == 0 && sign > 0)
442 else if (ind == directory_list_offset)
445 *indexp = sign > 0 ? 2 : 1;
449 return (sign > 0 ? directory_list_offset - ind : ind);
453 get_dirstack_element (ind, sign)
458 i = get_dirstack_index (ind, sign, (int *)NULL);
459 return (i < 0 || i > directory_list_offset) ? (char *)NULL
460 : pushd_directory_list[i];
464 set_dirstack_element (ind, sign, value)
470 i = get_dirstack_index (ind, sign, (int *)NULL);
471 if (ind == 0 || i < 0 || i > directory_list_offset)
473 free (pushd_directory_list[i]);
474 pushd_directory_list[i] = savestring (value);
478 get_directory_stack ()
484 for (ret = (WORD_LIST *)NULL, i = 0; i < directory_list_offset; i++)
486 d = polite_directory_format (pushd_directory_list[i]);
487 ret = make_word_list (make_word (d), ret);
489 /* Now the current directory. */
490 d = get_working_directory ("dirstack");
491 i = 0; /* sentinel to decide whether or not to free d */
496 t = polite_directory_format (d);
497 /* polite_directory_format sometimes returns its argument unchanged.
498 If it does not, we can free d right away. If it does, we need to
499 mark d to be deleted later. */
505 else /* t == d, so d is what we want */
508 ret = make_word_list (make_word (d), ret);
511 return ret; /* was (REVERSE_LIST (ret, (WORD_LIST *)); */
514 static char *dirs_doc[] = {
515 "Display the list of currently remembered directories. Directories",
516 "find their way onto the list with the `pushd' command; you can get",
517 "back up through the list with the `popd' command.",
519 "The -l flag specifies that `dirs' should not print shorthand versions",
520 "of directories which are relative to your home directory. This means",
521 "that `~/bin' might be displayed as `/homes/bfox/bin'. The -v flag",
522 "causes `dirs' to print the directory stack with one entry per line,",
523 "prepending the directory name with its position in the stack. The -p",
524 "flag does the same thing, but the stack position is not prepended.",
525 "The -c flag clears the directory stack by deleting all of the elements.",
527 "+N displays the Nth entry counting from the left of the list shown by",
528 " dirs when invoked without options, starting with zero.",
530 "-N displays the Nth entry counting from the right of the list shown by",
531 " dirs when invoked without options, starting with zero.",
535 static char *pushd_doc[] = {
536 "Adds a directory to the top of the directory stack, or rotates",
537 "the stack, making the new top of the stack the current working",
538 "directory. With no arguments, exchanges the top two directories.",
540 "+N Rotates the stack so that the Nth directory (counting",
541 " from the left of the list shown by `dirs') is at the top.",
543 "-N Rotates the stack so that the Nth directory (counting",
544 " from the right) is at the top.",
546 "-n suppress the normal change of directory when adding directories",
547 " to the stack, so only the stack is manipulated.",
549 "dir adds DIR to the directory stack at the top, making it the",
550 " new current working directory.",
552 "You can see the directory stack with the `dirs' command.",
556 static char *popd_doc[] = {
557 "Removes entries from the directory stack. With no arguments,",
558 "removes the top directory from the stack, and cd's to the new",
561 "+N removes the Nth entry counting from the left of the list",
562 " shown by `dirs', starting with zero. For example: `popd +0'",
563 " removes the first directory, `popd +1' the second.",
565 "-N removes the Nth entry counting from the right of the list",
566 " shown by `dirs', starting with zero. For example: `popd -0'",
567 " removes the last directory, `popd -1' the next to last.",
569 "-n suppress the normal change of directory when removing directories",
570 " from the stack, so only the stack is manipulated.",
572 "You can see the directory stack with the `dirs' command.",
576 struct builtin pushd_struct = {
581 "pushd [+N | -N] [-n] [dir]",
585 struct builtin popd_struct = {
590 "popd [+N | -N] [-n]",
594 struct builtin dirs_struct = {
599 "dirs [-clpv] [+N] [-N]",