1N/A/*
1N/A parted - a frontend to libparted
1N/A Copyright (C) 1999-2000, 2007, 2009-2010 Free Software Foundation,
1N/A Inc.
1N/A
1N/A This program is free software; you can redistribute it and/or modify
1N/A it under the terms of the GNU General Public License as published by
1N/A the Free Software Foundation; either version 3 of the License, or
1N/A (at your option) any later version.
1N/A
1N/A This program is distributed in the hope that it will be useful,
1N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A GNU General Public License for more details.
1N/A
1N/A You should have received a copy of the GNU General Public License
1N/A along with this program. If not, see <http://www.gnu.org/licenses/>.
1N/A*/
1N/A
1N/A#include <config.h>
1N/A#include "command.h"
1N/A#include "ui.h"
1N/A
1N/A#include <parted/debug.h>
1N/A
1N/A#include <stdlib.h>
1N/A#include <string.h>
1N/A#include "xalloc.h"
1N/A
1N/ACommand*
1N/Acommand_create (const StrList* names,
1N/A int (*method) (PedDevice** dev),
1N/A const StrList* summary,
1N/A const StrList* help,
1N/A const int non_interactive)
1N/A{
1N/A Command* cmd;
1N/A
1N/A cmd = xmalloc (sizeof (Command));
1N/A
1N/A if (non_interactive)
1N/A cmd->non_interactive = 1;
1N/A else
1N/A cmd->non_interactive = 0;
1N/A
1N/A cmd->names = (StrList*) names;
1N/A cmd->method = method;
1N/A cmd->summary = (StrList*) summary;
1N/A cmd->help = (StrList*) help;
1N/A
1N/A return cmd;
1N/A}
1N/A
1N/Avoid
1N/Acommand_destroy (Command* cmd)
1N/A{
1N/A str_list_destroy (cmd->names);
1N/A str_list_destroy (cmd->summary);
1N/A str_list_destroy (cmd->help);
1N/A free (cmd);
1N/A}
1N/A
1N/Avoid
1N/Acommand_register (Command** list, Command* cmd)
1N/A{
1N/A int i;
1N/A
1N/A for (i = 0; list [i]; i++);
1N/A
1N/A list [i] = cmd;
1N/A list [i + 1] = (Command*) NULL;
1N/A}
1N/A
1N/ACommand*
1N/Acommand_get (Command** list, char* name)
1N/A{
1N/A int i;
1N/A int partial_match = -1;
1N/A int ambiguous = 0;
1N/A
1N/A if (!name)
1N/A return NULL;
1N/A
1N/A for (i=0; list [i]; i++) {
1N/A switch (str_list_match_any (list [i]->names, name)) {
1N/A case 2:
1N/A return list [i];
1N/A
1N/A case 1:
1N/A if (!ambiguous) {
1N/A if (partial_match == -1) {
1N/A partial_match = i;
1N/A } else {
1N/A partial_match = -1;
1N/A ambiguous = 1;
1N/A }
1N/A }
1N/A }
1N/A }
1N/A
1N/A if (partial_match == -1)
1N/A return NULL;
1N/A else
1N/A return list [partial_match];
1N/A}
1N/A
1N/AStrList*
1N/Acommand_get_names (Command** list)
1N/A{
1N/A Command** walk;
1N/A StrList* result = NULL;
1N/A
1N/A for (walk = list; *walk; walk++)
1N/A result = str_list_join (result,
1N/A str_list_duplicate ((*walk)->names));
1N/A return result;
1N/A}
1N/A
1N/Avoid
1N/Acommand_print_summary (Command* cmd)
1N/A{
1N/A fputs (" ", stdout);
1N/A str_list_print_wrap (cmd->summary, screen_width(), 2, 8);
1N/A putchar ('\n');
1N/A}
1N/A
1N/Avoid
1N/Acommand_print_help (Command* cmd)
1N/A{
1N/A command_print_summary (cmd);
1N/A if (cmd->help) {
1N/A fputs ("\n\t", stdout);
1N/A str_list_print_wrap (cmd->help, screen_width(), 8, 8);
1N/A }
1N/A}
1N/A
1N/Aint
1N/Acommand_run (Command* cmd, PedDevice** dev)
1N/A{
1N/A return cmd->method (dev);
1N/A}