Debian Codegraph @

ftp/cmdtab.c

raw
1 /*
2 Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1985, 1989, 1993, 1994
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49
50 #include <stdio.h>
51 #include <string.h>
52
53 #include "ftp_var.h"
54
55 /*
56 * User FTP -- Command Tables.
57 */
58
59 char accounthelp[] = "send account command to remote server";
60 char appendhelp[] = "append to a file";
61 char asciihelp[] = "set ascii transfer type";
62 char beephelp[] = "beep when command completed";
63 char binaryhelp[] = "set binary transfer type";
64 char casehelp[] = "toggle mget upper/lower case id mapping";
65 char cdhelp[] = "change remote working directory";
66 char cduphelp[] = "change remote working directory to parent directory";
67 char chmodhelp[] = "change file permissions of remote file";
68 char connecthelp[] = "connect to remote tftp";
69 char crhelp[] = "toggle carriage return stripping on ascii gets";
70 char deletehelp[] = "delete remote file";
71 char debughelp[] = "toggle/set debugging mode";
72 char dirhelp[] = "list contents of remote directory";
73 char disconhelp[] = "terminate ftp session";
74 char domachelp[] = "execute macro";
75 char epsv4help[] = "toggle the use of EPRT/EPSV for IPv4";
76 char formhelp[] = "set file transfer format";
77 char globhelp[] = "toggle metacharacter expansion of local file names";
78 char hashhelp[] = "toggle printing `#' for each buffer transferred";
79 char helphelp[] = "print local help information";
80 char idlehelp[] = "get (set) idle timer on remote side";
81 char ipanyhelp[] = "allow all address families";
82 char ipv4help[] = "select only IPv4 addresses";
83 char ipv6help[] = "select only IPv6 addresses";
84 char lcdhelp[] = "change local working directory";
85 char lpwdhelp[] = "print local working directory";
86 char lshelp[] = "list contents of remote directory";
87 char macdefhelp[] = "define a macro";
88 char mdeletehelp[] = "delete multiple files";
89 char mdirhelp[] = "list contents of multiple remote directories";
90 char mgethelp[] = "get multiple files";
91 char mkdirhelp[] = "make directory on the remote machine";
92 char mlshelp[] = "list contents of multiple remote directories";
93 char modtimehelp[] = "show last modification time of remote file";
94 char modehelp[] = "set file transfer mode";
95 char mputhelp[] = "send multiple files";
96 char newerhelp[] = "get file if remote file is newer than local file ";
97 char nlisthelp[] = "nlist contents of remote directory";
98 char nmaphelp[] = "set templates for default file name mapping";
99 char ntranshelp[] = "set translation table for default file name mapping";
100 char porthelp[] = "toggle use of PORT cmd for each data connection";
101 char prompthelp[] = "force interactive prompting on multiple commands";
102 char proxyhelp[] = "issue command on alternate connection";
103 char pwdhelp[] = "print working directory on remote machine";
104 char quithelp[] = "terminate ftp session and exit";
105 char quotehelp[] = "send arbitrary ftp command";
106 char receivehelp[] = "receive file";
107 char regethelp[] = "get file restarting at end of local file";
108 char remotehelp[] = "get help from remote server";
109 char renamehelp[] = "rename file";
110 char restarthelp[] = "restart file transfer at bytecount";
111 char rmdirhelp[] = "remove directory on the remote machine";
112 char rmtstatushelp[] = "show status of remote machine";
113 char runiquehelp[] = "toggle store unique for local files";
114 char resethelp[] = "clear queued command replies";
115 char sendhelp[] = "send one file";
116 char passivehelp[] = "enter passive transfer mode";
117 char sitehelp[] =
118 "send site specific command to remote server\n\t\tTry \"rhelp site\" or \"site help\" for more information";
119 char shellhelp[] = "escape to the shell";
120 char sizecmdhelp[] = "show size of remote file";
121 char statushelp[] = "show current status";
122 char structhelp[] = "set file transfer structure";
123 char suniquehelp[] = "toggle store unique on remote machine";
124 char systemhelp[] = "show remote system type";
125 char tenexhelp[] = "set tenex file transfer type";
126 char tracehelp[] = "toggle packet tracing";
127 char typehelp[] = "set file transfer type";
128 char umaskhelp[] = "get (set) umask on remote side";
129 char userhelp[] = "send new user information";
130 char verbosehelp[] = "toggle verbose mode";
131
132 static struct cmd cmdtab[] = {
133 {"!", shellhelp, 0, 0, 0, shell},
134 {"$", domachelp, 1, 0, 0, domacro},
135 {"account", accounthelp, 0, 1, 1, account},
136 {"append", appendhelp, 1, 1, 1, put},
137 {"ascii", asciihelp, 0, 1, 1, setascii},
138 {"bell", beephelp, 0, 0, 0, setbell},
139 {"binary", binaryhelp, 0, 1, 1, setbinary},
140 {"bye", quithelp, 0, 0, 0, quit},
141 {"case", casehelp, 0, 0, 1, setcase},
142 {"cd", cdhelp, 0, 1, 1, cd},
143 {"cdup", cduphelp, 0, 1, 1, cdup},
144 {"chmod", chmodhelp, 0, 1, 1, do_chmod},
145 {"close", disconhelp, 0, 1, 1, disconnect},
146 {"cr", crhelp, 0, 0, 0, setcr},
147 {"delete", deletehelp, 0, 1, 1, delete},
148 {"debug", debughelp, 0, 0, 0, setdebug},
149 {"dir", dirhelp, 1, 1, 1, ls},
150 {"disconnect", disconhelp, 0, 1, 1, disconnect},
151 {"epsv4", epsv4help, 0, 0, 0, setepsv4},
152 {"form", formhelp, 0, 1, 1, setform},
153 {"get", receivehelp, 1, 1, 1, get},
154 {"glob", globhelp, 0, 0, 0, setglob},
155 {"hash", hashhelp, 0, 0, 0, sethash},
156 {"help", helphelp, 0, 0, 1, help},
157 {"idle", idlehelp, 0, 1, 1, site_idle},
158 {"image", binaryhelp, 0, 1, 1, setbinary},
159 {"ipany", ipanyhelp, 0, 0, 0, setipany},
160 {"ipv4", ipv4help, 0, 0, 0, setipv4},
161 {"ipv6", ipv6help, 0, 0, 0, setipv6},
162 {"lcd", lcdhelp, 0, 0, 0, lcd},
163 {"lpwd", lpwdhelp, 0, 0, 0, lpwd},
164 {"ls", lshelp, 1, 1, 1, ls},
165 {"macdef", macdefhelp, 0, 0, 0, macdef},
166 {"mdelete", mdeletehelp, 1, 1, 1, mdelete},
167 {"mdir", mdirhelp, 1, 1, 1, mls},
168 {"mget", mgethelp, 1, 1, 1, mget},
169 {"mkdir", mkdirhelp, 0, 1, 1, makedir},
170 {"mls", mlshelp, 1, 1, 1, mls},
171 {"mode", modehelp, 0, 1, 1, setftmode},
172 {"modtime", modtimehelp, 0, 1, 1, modtime},
173 {"mput", mputhelp, 1, 1, 1, mput},
174 {"newer", newerhelp, 1, 1, 1, newer},
175 {"nmap", nmaphelp, 0, 0, 1, setnmap},
176 {"nlist", nlisthelp, 1, 1, 1, ls},
177 {"ntrans", ntranshelp, 0, 0, 1, setntrans},
178 {"open", connecthelp, 0, 0, 1, setpeer},
179 {"passive", passivehelp, 0, 0, 0, setpassive},
180 {"prompt", prompthelp, 0, 0, 0, setprompt},
181 {"proxy", proxyhelp, 0, 0, 1, doproxy},
182 {"sendport", porthelp, 0, 0, 0, setport},
183 {"put", sendhelp, 1, 1, 1, put},
184 {"pwd", pwdhelp, 0, 1, 1, pwd},
185 {"quit", quithelp, 0, 0, 0, quit},
186 {"quote", quotehelp, 1, 1, 1, quote},
187 {"recv", receivehelp, 1, 1, 1, get},
188 {"reget", regethelp, 1, 1, 1, reget},
189 {"rstatus", rmtstatushelp, 0, 1, 1, rmtstatus},
190 {"rhelp", remotehelp, 0, 1, 1, rmthelp},
191 {"rename", renamehelp, 0, 1, 1, renamefile},
192 {"reset", resethelp, 0, 1, 1, reset},
193 {"restart", restarthelp, 1, 1, 1, restart},
194 {"rmdir", rmdirhelp, 0, 1, 1, removedir},
195 {"runique", runiquehelp, 0, 0, 1, setrunique},
196 {"send", sendhelp, 1, 1, 1, put},
197 {"site", sitehelp, 0, 1, 1, site},
198 {"size", sizecmdhelp, 1, 1, 1, sizecmd},
199 {"status", statushelp, 0, 0, 1, status},
200 {"struct", structhelp, 0, 1, 1, setstruct},
201 {"system", systemhelp, 0, 1, 1, syst},
202 {"sunique", suniquehelp, 0, 0, 1, setsunique},
203 {"tenex", tenexhelp, 0, 1, 1, settenex},
204 {"trace", tracehelp, 0, 0, 0, settrace},
205 {"type", typehelp, 0, 1, 1, settype},
206 {"user", userhelp, 0, 1, 1, user},
207 {"umask", umaskhelp, 0, 1, 1, do_umask},
208 {"verbose", verbosehelp, 0, 0, 0, setverbose},
209 {"?", helphelp, 0, 0, 1, help},
210 {NULL, NULL, 0, 0, 0, NULL},
211 };
212
213 #define NCMDS (sizeof (cmdtab) / sizeof (cmdtab[0]) - 1)
214
215 struct cmd *
216 getcmd (char *name)
217 {
218 char *p, *q;
219 struct cmd *c, *found;
220 int nmatches, longest;
221
222 longest = 0;
223 nmatches = 0;
224 found = 0;
225 for (c = cmdtab; (p = c->c_name); c++)
226 {
227 for (q = name; *q == *p++; q++)
228 if (*q == 0) /* exact match? */
229 return c;
230 if (!*q)
231 { /* the name was a prefix */
232 if (q - name > longest)
233 {
234 longest = q - name;
235 nmatches = 1;
236 found = c;
237 }
238 else if (q - name == longest)
239 nmatches++;
240 }
241 }
242 if (nmatches > 1)
243 return (struct cmd *) -1;
244 return found;
245 }
246
247 #define HELPINDENT ((int) sizeof ("directory"))
248
249 /*
250 * Help command.
251 * Call each command handler with argc == 0 and argv[0] == name.
252 */
253 void
254 help (int argc, char *argv[])
255 {
256 struct cmd *c;
257
258 if (argc == 1)
259 {
260 int i, j, w, k;
261 int columns, width = 0, lines;
262
263 printf ("Commands may be abbreviated. Commands are:\n\n");
264 for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
265 {
266 int len = strlen (c->c_name);
267
268 if (len > width)
269 width = len;
270 }
271 width = (width + 8) & ~7;
272 columns = 80 / width;
273 if (columns == 0)
274 columns = 1;
275 lines = (NCMDS + columns - 1) / columns;
276 for (i = 0; i < lines; i++)
277 {
278 for (j = 0; j < columns; j++)
279 {
280 c = cmdtab + j * lines + i;
281 if (c->c_name && (!proxy || c->c_proxy))
282 {
283 printf ("%s", c->c_name);
284 }
285 else if (c->c_name)
286 {
287 for (k = 0; k < (int) strlen (c->c_name); k++)
288 putchar (' ');
289 }
290 if (c + lines >= &cmdtab[NCMDS])
291 {
292 printf ("\n");
293 break;
294 }
295 w = strlen (c->c_name);
296 while (w < width)
297 {
298 w = (w + 8) & ~7;
299 putchar ('\t');
300 }
301 }
302 }
303 return;
304 }
305
306 while (--argc > 0)
307 {
308 char *arg;
309 arg = *++argv;
310 c = getcmd (arg);
311 if (c == (struct cmd *) -1)
312 printf ("?Ambiguous help command %s\n", arg);
313 else if (c == (struct cmd *) 0)
314 printf ("?Invalid help command %s\n", arg);
315 else
316 printf ("%-*s\t%s\n", HELPINDENT, c->c_name, c->c_help);
317 }
318 }