+
+/* If the last character of PATH is a directory separator, then strip it. */
+
+static void
+strip_trailing_directory_separator (char *path)
+{
+ const int last = strlen (path) - 1;
+
+ if (last < 0)
+ return; /* No stripping is needed if PATH is the empty string. */
+
+ if (IS_DIR_SEPARATOR (path[last]))
+ path[last] = '\0';
+}
+
+/* Return the path substitution rule that matches FROM.
+ Return NULL if no rule matches. */
+
+static struct substitute_path_rule *
+find_substitute_path_rule (const char *from)
+{
+ struct substitute_path_rule *rule = substitute_path_rules;
+
+ while (rule != NULL)
+ {
+ if (FILENAME_CMP (rule->from, from) == 0)
+ return rule;
+ rule = rule->next;
+ }
+
+ return NULL;
+}
+
+/* Add a new substitute-path rule at the end of the current list of rules.
+ The new rule will replace FROM into TO. */
+
+static void
+add_substitute_path_rule (char *from, char *to)
+{
+ struct substitute_path_rule *rule;
+ struct substitute_path_rule *new_rule;
+
+ new_rule = xmalloc (sizeof (struct substitute_path_rule));
+ new_rule->from = xstrdup (from);
+ new_rule->to = xstrdup (to);
+ new_rule->next = NULL;
+
+ /* If the list of rules are empty, then insert the new rule
+ at the head of the list. */
+
+ if (substitute_path_rules == NULL)
+ {
+ substitute_path_rules = new_rule;
+ return;
+ }
+
+ /* Otherwise, skip to the last rule in our list and then append
+ the new rule. */
+
+ rule = substitute_path_rules;
+ while (rule->next != NULL)
+ rule = rule->next;
+
+ rule->next = new_rule;
+}
+
+/* Remove the given source path substitution rule from the current list
+ of rules. The memory allocated for that rule is also deallocated. */
+
+static void
+delete_substitute_path_rule (struct substitute_path_rule *rule)
+{
+ if (rule == substitute_path_rules)
+ substitute_path_rules = rule->next;
+ else
+ {
+ struct substitute_path_rule *prev = substitute_path_rules;
+
+ while (prev != NULL && prev->next != rule)
+ prev = prev->next;
+
+ gdb_assert (prev != NULL);
+
+ prev->next = rule->next;
+ }
+
+ xfree (rule->from);
+ xfree (rule->to);
+ xfree (rule);
+}
+
+/* Implement the "show substitute-path" command. */
+
+static void
+show_substitute_path_command (char *args, int from_tty)
+{
+ struct substitute_path_rule *rule = substitute_path_rules;
+ char **argv;
+ char *from = NULL;
+
+ argv = gdb_buildargv (args);
+ make_cleanup_freeargv (argv);
+
+ /* We expect zero or one argument. */
+
+ if (argv != NULL && argv[0] != NULL && argv[1] != NULL)
+ error (_("Too many arguments in command"));
+
+ if (argv != NULL && argv[0] != NULL)
+ from = argv[0];
+
+ /* Print the substitution rules. */
+
+ if (from != NULL)
+ printf_filtered
+ (_("Source path substitution rule matching `%s':\n"), from);
+ else
+ printf_filtered (_("List of all source path substitution rules:\n"));
+
+ while (rule != NULL)
+ {
+ if (from == NULL || FILENAME_CMP (rule->from, from) == 0)
+ printf_filtered (" `%s' -> `%s'.\n", rule->from, rule->to);
+ rule = rule->next;
+ }
+}
+
+/* Implement the "unset substitute-path" command. */
+
+static void
+unset_substitute_path_command (char *args, int from_tty)
+{
+ struct substitute_path_rule *rule = substitute_path_rules;
+ char **argv = gdb_buildargv (args);
+ char *from = NULL;
+ int rule_found = 0;
+
+ /* This function takes either 0 or 1 argument. */
+
+ make_cleanup_freeargv (argv);
+ if (argv != NULL && argv[0] != NULL && argv[1] != NULL)
+ error (_("Incorrect usage, too many arguments in command"));
+
+ if (argv != NULL && argv[0] != NULL)
+ from = argv[0];
+
+ /* If the user asked for all the rules to be deleted, ask him
+ to confirm and give him a chance to abort before the action
+ is performed. */
+
+ if (from == NULL
+ && !query (_("Delete all source path substitution rules? ")))
+ error (_("Canceled"));
+
+ /* Delete the rule matching the argument. No argument means that
+ all rules should be deleted. */
+
+ while (rule != NULL)
+ {
+ struct substitute_path_rule *next = rule->next;
+
+ if (from == NULL || FILENAME_CMP (from, rule->from) == 0)
+ {
+ delete_substitute_path_rule (rule);
+ rule_found = 1;
+ }
+
+ rule = next;
+ }
+
+ /* If the user asked for a specific rule to be deleted but
+ we could not find it, then report an error. */
+
+ if (from != NULL && !rule_found)
+ error (_("No substitution rule defined for `%s'"), from);
+}
+
+/* Add a new source path substitution rule. */
+
+static void
+set_substitute_path_command (char *args, int from_tty)
+{
+ char *from_path, *to_path;
+ char **argv;
+ struct substitute_path_rule *rule;
+
+ argv = gdb_buildargv (args);
+ make_cleanup_freeargv (argv);
+
+ if (argv == NULL || argv[0] == NULL || argv [1] == NULL)
+ error (_("Incorrect usage, too few arguments in command"));
+
+ if (argv[2] != NULL)
+ error (_("Incorrect usage, too many arguments in command"));
+
+ if (*(argv[0]) == '\0')
+ error (_("First argument must be at least one character long"));
+
+ /* Strip any trailing directory separator character in either FROM
+ or TO. The substitution rule already implicitly contains them. */
+ strip_trailing_directory_separator (argv[0]);
+ strip_trailing_directory_separator (argv[1]);
+
+ /* If a rule with the same "from" was previously defined, then
+ delete it. This new rule replaces it. */
+
+ rule = find_substitute_path_rule (argv[0]);
+ if (rule != NULL)
+ delete_substitute_path_rule (rule);
+
+ /* Insert the new substitution rule. */
+
+ add_substitute_path_rule (argv[0], argv[1]);
+}
+