xref: /onnv-gate/usr/src/cmd/parted/command.c (revision 9663:ace9a2ac3683)
1 /*
2     parted - a frontend to libparted
3     Copyright (C) 1999, 2000, 2007 Free Software Foundation, Inc.
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include <config.h>
20 #include "command.h"
21 #include "ui.h"
22 
23 #include <parted/debug.h>
24 
25 #include <stdlib.h>
26 #include <string.h>
27 #include "xalloc.h"
28 
29 Command*
command_create(const StrList * names,int (* method)(PedDevice ** dev),const StrList * summary,const StrList * help,const int non_interactive)30 command_create (const StrList* names,
31 		int (*method) (PedDevice** dev),
32 		const StrList* summary,
33 		const StrList* help,
34                 const int non_interactive)
35 {
36 	Command*	cmd;
37 
38 	cmd = xmalloc (sizeof (Command));
39 
40         if (non_interactive)
41                 cmd->non_interactive = 1;
42         else
43                 cmd->non_interactive = 0;
44 
45 	cmd->names = (StrList*) names;
46 	cmd->method = method;
47 	cmd->summary = (StrList*) summary;
48 	cmd->help = (StrList*) help;
49 
50 	return cmd;
51 }
52 
53 void
command_destroy(Command * cmd)54 command_destroy (Command* cmd)
55 {
56 	str_list_destroy (cmd->names);
57 	str_list_destroy (cmd->summary);
58 	str_list_destroy (cmd->help);
59 	free (cmd);
60 }
61 
62 void
command_register(Command ** list,Command * cmd)63 command_register (Command** list, Command* cmd)
64 {
65 	int	i;
66 
67 	for (i = 0; list [i]; i++);
68 
69 	list [i] = cmd;
70 	list [i + 1] = (Command*) NULL;
71 }
72 
73 Command*
command_get(Command ** list,char * name)74 command_get (Command** list, char* name)
75 {
76 	int		i;
77 	int		partial_match = -1;
78 	int		ambiguous = 0;
79 
80 	if (!name)
81 		return NULL;
82 
83 	for (i=0; list [i]; i++) {
84 		switch (str_list_match_any (list [i]->names, name)) {
85 		case 2:
86 			return list [i];
87 
88 		case 1:
89 			if (!ambiguous) {
90 				if (partial_match == -1) {
91 					partial_match = i;
92 				} else {
93 					partial_match = -1;
94 					ambiguous = 1;
95 				}
96 			}
97 		}
98 	}
99 
100 	if (partial_match == -1)
101 		return NULL;
102 	else
103 		return list [partial_match];
104 }
105 
106 StrList*
command_get_names(Command ** list)107 command_get_names (Command** list)
108 {
109 	Command**	walk;
110 	StrList*	result = NULL;
111 
112 	for (walk = list; *walk; walk++)
113 		result = str_list_join (result,
114 					str_list_duplicate ((*walk)->names));
115 	return result;
116 }
117 
118 void
command_print_summary(Command * cmd)119 command_print_summary (Command* cmd)
120 {
121         fputs ("  ", stdout);
122 	str_list_print_wrap (cmd->summary, screen_width(), 2, 8);
123 	putchar ('\n');
124 }
125 
126 void
command_print_help(Command * cmd)127 command_print_help (Command* cmd)
128 {
129 	command_print_summary (cmd);
130 	if (cmd->help) {
131                 fputs ("\n\t", stdout);
132 		str_list_print_wrap (cmd->help, screen_width(), 8, 8);
133 	}
134 }
135 
136 int
command_run(Command * cmd,PedDevice ** dev)137 command_run (Command* cmd, PedDevice** dev)
138 {
139 	return cmd->method (dev);
140 }
141 
142