Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 157 lines (138 sloc) 5.303 kb
c729c0f Importing existing code
Diego Dompe authored
1 /**
2 * Command
3 * This class is a template for a command information. It includes the command
4 * name, the function that has to be executed and the explanation of the
5 * command syntax, function arguments and command description
6 */
7 public class IpipeCommand:GLib.Object {
8
9 public string name;
10 public CmdFunc cmd_func;
11 public string usage;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
12 public string brief;
c729c0f Importing existing code
Diego Dompe authored
13 public string doc;
14 public string args;
15
16 public delegate int CmdFunc(string[]? s);
17
18 /** Command constructor. Creates an object for a specific command
19 * @param _name string of the command name
20 * @param _cmd function that has to be call for the command
21 * @param _usage syntax required in order to execute the command in the
22 * command line
23 * @param _doc description of the command funcionality
24 * @param _args explanation of the function's arguments
25 */
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
26 public IpipeCommand(string _name, CmdFunc _cmd, string _usage, string _brief,
27 string _doc, string _args) {
c729c0f Importing existing code
Diego Dompe authored
28 this.name = _name;
29 this.cmd_func = _cmd;
30 this.usage = _usage;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
31 this.brief = _brief;
c729c0f Importing existing code
Diego Dompe authored
32 this.doc = _doc;
33 this.args = _args;
34 }
35
36 public IpipeCommand.empty() {
37 this.name = "";
38 this.cmd_func = null;
39 this.usage = "";
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
40 this.brief = "";
c729c0f Importing existing code
Diego Dompe authored
41 this.doc = "";
42 this.args = "";
43 }
44
45 ~IpipeCommand() {
46 }
47 }
48
49 /**
50 * CommandManage
51 * This class is a template for a command management information. It include
52 * an array of Command and functions to manipulate them. It is capable of
53 * create and add new commands, find a especific command, execute command's
54 * functions and display the help information.
55 */
56 public class CommandManager:GLib.Object {
57
58 private IpipeCommand[] CmdMap = { };
59 /** Command constructor. Creates an object for a specific command
60 * @param _name string of the command name
61 * @param _cmd function that has to be call for the command
62 * @param _usage syntax required in order to execute the command in the
63 * command line
64 * @param _doc description of the command funcionality
65 * @param _args explanation of the function's arguments
66 */
67 public CommandManager() {
68
69 }
70
71 public void new_command(string _name, IpipeCommand.CmdFunc _cmdfunc,
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
72 string _usage, string _brief, string _doc, string _args) {
73 IpipeCommand new_cmd = new IpipeCommand(_name, _cmdfunc, _usage, _brief,
74 _doc, _args);
c729c0f Importing existing code
Diego Dompe authored
75 CmdMap += new_cmd;
76 return;
77 }
78
79 /**
80 * Look up 'name' as the name of a command, and returns in 'cmd'
81 * the CmdMap instances that matches with the given name.
82 * Return non-zero if name isn't a command name. */
83 public IpipeCommand ? find_command(string name) {
84
85 int ind;
86 for (ind = 0; ind < CmdMap.length; ind++)
87 if (strcmp(name, CmdMap[ind].name) == 0) {
88 return CmdMap[ind];
89 }
90 return null;
91 }
92 /* Execute a command. */
93 public int execute_cmd(string[]args) {
94
95 IpipeCommand Cmd = new IpipeCommand.empty();
96 Cmd = find_command(args[0]);
97 if (Cmd == null) {
98 stdout.printf("%s: No such command for IpipeClient.\n", args[0]);
99 return (1);
100 }
101
102 /* Call the function. */
103 return (Cmd.cmd_func((string[])args));
104 }
105
106 public int command_help(string[]? args) {
107 int ind;
108 int printed = 0;
109
110 for (ind = 0; ind < CmdMap.length; ind++) {
111 if (args[1] == null) {
112 if (printed == 0)
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
113 stdout.printf ("\nCOMMANDS supported:\n" +
114 "Request the syntax of an specific command with " +
115 "\"help [command]\".\nThis is the list of supported commands:\n" +
116 "\nCommand\t\t\t\tDescription\n\n");
c729c0f Importing existing code
Diego Dompe authored
117 if (CmdMap[ind].name.length < 8) {
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
118 stdout.printf("%s\t\t\t", CmdMap[ind].name);
c729c0f Importing existing code
Diego Dompe authored
119 } else if (CmdMap[ind].name.length < 16){
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
120 stdout.printf("%s\t\t", CmdMap[ind].name);
c729c0f Importing existing code
Diego Dompe authored
121 } else {
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
122 stdout.printf("%s\t", CmdMap[ind].name);
c729c0f Importing existing code
Diego Dompe authored
123 }
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
124 stdout.printf("%s.\n", CmdMap[ind].brief);
c729c0f Importing existing code
Diego Dompe authored
125 printed++;
126 } else if (strcmp(args[1], CmdMap[ind].name) == 0){
127 stdout.printf ("\nCommand: %s\n", CmdMap[ind].name);
128 stdout.printf ("Syntax: %s\n", CmdMap[ind].usage);
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
129 stdout.printf ("Description: %s.\n", CmdMap[ind].brief);
130 if (CmdMap[ind].doc != ""){
131 stdout.printf ("%s\n", CmdMap[ind].doc);
132 }
c729c0f Importing existing code
Diego Dompe authored
133 if (CmdMap[ind].args != ""){
134 stdout.printf ("Arguments: %s\n", CmdMap[ind].args);
135 }
136 printed++;
137 break;
138 }
139 }
140 stdout.printf("\n");
141
142 if (printed == 0) {
143 stdout.
144 printf
145 ("No commands match with '%s'. Possible commands are:\n\n",
146 args[1]);
147
148 for (ind = 0; ind < CmdMap.length; ind++) {
149 stdout.printf("-%s\n", CmdMap[ind].name);
150 }
151 return -1;
152 }
153 return 0;
154 }
155 }
156
Something went wrong with that request. Please try again.