383 |
(void) signal (SIGPIPE, lostpeer); |
(void) signal (SIGPIPE, lostpeer); |
384 |
} |
} |
385 |
|
|
|
struct cmd * |
|
|
getcmd (char *name) |
|
|
{ |
|
|
char *p, *q; |
|
|
struct cmd *c, *found; |
|
|
int nmatches, longest; |
|
|
|
|
|
longest = 0; |
|
|
nmatches = 0; |
|
|
found = 0; |
|
|
for (c = cmdtab; (p = c->c_name); c++) |
|
|
{ |
|
|
for (q = name; *q == *p++; q++) |
|
|
if (*q == 0) /* exact match? */ |
|
|
return (c); |
|
|
if (!*q) |
|
|
{ /* the name was a prefix */ |
|
|
if (q - name > longest) |
|
|
{ |
|
|
longest = q - name; |
|
|
nmatches = 1; |
|
|
found = c; |
|
|
} |
|
|
else if (q - name == longest) |
|
|
nmatches++; |
|
|
} |
|
|
} |
|
|
if (nmatches > 1) |
|
|
return ((struct cmd *)-1); |
|
|
return (found); |
|
|
} |
|
|
|
|
386 |
/* |
/* |
387 |
* Slice a string up into argc/argv. |
* Slice a string up into argc/argv. |
388 |
*/ |
*/ |
534 |
return ((char *) 0); |
return ((char *) 0); |
535 |
} |
} |
536 |
|
|
|
#define HELPINDENT ((int) sizeof ("directory")) |
|
|
|
|
|
/* |
|
|
* Help command. |
|
|
* Call each command handler with argc == 0 and argv[0] == name. |
|
|
*/ |
|
|
void |
|
|
help (int argc, char *argv[]) |
|
|
{ |
|
|
struct cmd *c; |
|
|
|
|
|
if (argc == 1) |
|
|
{ |
|
|
int i, j, w, k; |
|
|
int columns, width = 0, lines; |
|
|
|
|
|
printf ("Commands may be abbreviated. Commands are:\n\n"); |
|
|
for (c = cmdtab; c < &cmdtab[NCMDS]; c++) |
|
|
{ |
|
|
int len = strlen (c->c_name); |
|
|
|
|
|
if (len > width) |
|
|
width = len; |
|
|
} |
|
|
width = (width + 8) &~ 7; |
|
|
columns = 80 / width; |
|
|
if (columns == 0) |
|
|
columns = 1; |
|
|
lines = (NCMDS + columns - 1) / columns; |
|
|
for (i = 0; i < lines; i++) |
|
|
{ |
|
|
for (j = 0; j < columns; j++) |
|
|
{ |
|
|
c = cmdtab + j * lines + i; |
|
|
if (c->c_name && (!proxy || c->c_proxy)) |
|
|
{ |
|
|
printf ("%s", c->c_name); |
|
|
} |
|
|
else if (c->c_name) |
|
|
{ |
|
|
for (k=0; k < strlen (c->c_name); k++) |
|
|
{ |
|
|
(void) putchar (' '); |
|
|
} |
|
|
} |
|
|
if (c + lines >= &cmdtab[NCMDS]) |
|
|
{ |
|
|
printf ("\n"); |
|
|
break; |
|
|
} |
|
|
w = strlen (c->c_name); |
|
|
while (w < width) { |
|
|
w = (w + 8) &~ 7; |
|
|
(void) putchar ('\t'); |
|
|
} |
|
|
} |
|
|
} |
|
|
return; |
|
|
} |
|
|
while (--argc > 0) |
|
|
{ |
|
|
char *arg; |
|
|
arg = *++argv; |
|
|
c = getcmd(arg); |
|
|
if (c == (struct cmd *)-1) |
|
|
printf("?Ambiguous help command %s\n", arg); |
|
|
else if (c == (struct cmd *)0) |
|
|
printf("?Invalid help command %s\n", arg); |
|
|
else |
|
|
printf("%-*s\t%s\n", HELPINDENT, |
|
|
c->c_name, c->c_help); |
|
|
} |
|
|
} |
|