/* Add zero or more directories to the front of the source path. */ enum mi_cmd_result mi_cmd_env_dir (char *command, char **argv, int argc) { int i; int optind = 0; int reset = 0; char *optarg; enum opt { RESET_OPT }; static struct mi_opt opts[] = { {"r", RESET_OPT, 0}, 0 }; dont_repeat (); if (mi_version (uiout) < 2) { for (i = argc - 1; i >= 0; --i) env_execute_cli_command ("dir", argv[i]); return MI_CMD_DONE; } /* Otherwise mi level is 2 or higher. */ while (1) { int opt = mi_getopt ("mi_cmd_env_dir", argc, argv, opts, &optind, &optarg); if (opt < 0) break; switch ((enum opt) opt) { case RESET_OPT: reset = 1; break; } } argv += optind; argc -= optind; if (reset) { /* Reset means setting to default path first. */ xfree (source_path); init_source_path (); } for (i = argc - 1; i >= 0; --i) env_mod_path (argv[i], &source_path); init_last_source_visited (); ui_out_field_string (uiout, "source-path", source_path); forget_cached_source_info (); return MI_CMD_DONE; }
void mi_cmd_env_dir (char *command, char **argv, int argc) { struct ui_out *uiout = current_uiout; int i; int oind = 0; int reset = 0; char *oarg; enum opt { RESET_OPT }; static const struct mi_opt opts[] = { {"r", RESET_OPT, 0}, { 0, 0, 0 } }; dont_repeat (); if (mi_version (uiout) < 2) { for (i = argc - 1; i >= 0; --i) env_execute_cli_command ("dir", argv[i]); return; } /* Otherwise mi level is 2 or higher. */ while (1) { int opt = mi_getopt ("-environment-directory", argc, argv, opts, &oind, &oarg); if (opt < 0) break; switch ((enum opt) opt) { case RESET_OPT: reset = 1; break; } } argv += oind; argc -= oind; if (reset) { /* Reset means setting to default path first. */ xfree (source_path); init_source_path (); } for (i = argc - 1; i >= 0; --i) env_mod_path (argv[i], &source_path); uiout->field_string ("source-path", source_path); forget_cached_source_info (); }
void cd_command (char *dir, int from_tty) { int len; /* Found something other than leading repetitions of "/..". */ int found_real_path; char *p; /* If the new directory is absolute, repeat is a no-op; if relative, repeat might be useful but is more likely to be a mistake. */ dont_repeat (); if (dir == 0) error_no_arg (_("new working directory")); dir = tilde_expand (dir); make_cleanup (xfree, dir); if (chdir (dir) < 0) perror_with_name (dir); #ifdef HAVE_DOS_BASED_FILE_SYSTEM /* There's too much mess with DOSish names like "d:", "d:.", "d:./foo" etc. Instead of having lots of special #ifdef'ed code, simply get the canonicalized name of the current directory. */ dir = getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)); #endif len = strlen (dir); if (IS_DIR_SEPARATOR (dir[len - 1])) { /* Remove the trailing slash unless this is a root directory (including a drive letter on non-Unix systems). */ if (!(len == 1) /* "/" */ #ifdef HAVE_DOS_BASED_FILE_SYSTEM && !(len == 3 && dir[1] == ':') /* "d:/" */ #endif ) len--; } dir = savestring (dir, len); if (IS_ABSOLUTE_PATH (dir)) current_directory = dir; else { if (IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])) current_directory = concat (current_directory, dir, (char *)NULL); else current_directory = concat (current_directory, SLASH_STRING, dir, (char *)NULL); xfree (dir); } /* Now simplify any occurrences of `.' and `..' in the pathname. */ found_real_path = 0; for (p = current_directory; *p;) { if (IS_DIR_SEPARATOR (p[0]) && p[1] == '.' && (p[2] == 0 || IS_DIR_SEPARATOR (p[2]))) strcpy (p, p + 2); else if (IS_DIR_SEPARATOR (p[0]) && p[1] == '.' && p[2] == '.' && (p[3] == 0 || IS_DIR_SEPARATOR (p[3]))) { if (found_real_path) { /* Search backwards for the directory just before the "/.." and obliterate it and the "/..". */ char *q = p; while (q != current_directory && !IS_DIR_SEPARATOR (q[-1])) --q; if (q == current_directory) /* current_directory is a relative pathname ("can't happen"--leave it alone). */ ++p; else { strcpy (q - 1, p + 3); p = q - 1; } } else /* We are dealing with leading repetitions of "/..", for example "/../..", which is the Mach super-root. */ p += 3; } else { found_real_path = 1; ++p; } } forget_cached_source_info (); if (from_tty) pwd_command ((char *) 0, 1); }