XINU
Functions
xsh_help.c File Reference
#include <xinu.h>
#include <stdio.h>
#include <string.h>
Include dependency graph for xsh_help.c:

Go to the source code of this file.

Functions

shellcmd xsh_help (int nargs, char *args[])
 

Function Documentation

◆ xsh_help()

shellcmd xsh_help ( int  nargs,
char *  args[] 
)

Definition at line 11 of file xsh_help.c.

References cmdent::cfunc, cmdtab, cmdent::cname, fprintf(), ncmd, NULLCH, printf(), stderr, strncmp(), and strnlen().

12 {
13  int32 i;
14  char *argv[2]; /* argument vector for call */
15  char *src, *cmp; /* used for string compare */
16  int32 len; /* length of a command name */
17  int32 maxlen; /* maximum length of all */
18  /* command names */
19  int32 cols; /* number of columns in the */
20  /* formatted command list */
21  int32 spac; /* space per column in the */
22  /* formatted command list */
23  int32 lines; /* total lines of output in the */
24  /* formatted command list */
25  int32 j; /* index of commands across one */
26  /* line of formatted output */
27 
28  /* For argument '--help', emit help about the 'help' command */
29 
30  if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) {
31 
32  printf("Use:\n");
33  printf("\t%s [command]\n", args[0]);
34  printf("Description:\n");
35  printf("\tProvides a list of shell commands or\n");
36  printf("\thelp information for a specific command\n");
37  printf("Options:\n");
38  printf("\tcommand\tspecific command for which to\n");
39  printf("\t\tdisplay help information\n");
40  printf("\t--help\tdisplay this help and exit\n");
41  return 0;
42  }
43 
44  /* Check for valid number of arguments */
45 
46  if (nargs > 2) {
47  fprintf(stderr, "%s: too many arguments\n", args[0]);
48  fprintf(stderr, "Try '%s --help' for more information\n",
49  args[0]);
50  return 1;
51  }
52 
53  /* Output help for specific command given as an argument */
54 
55  if (nargs == 2) {
56  for (i = 0; i < ncmd; i++) {
57  src = cmdtab[i].cname;
58  cmp = args[1];
59  while (*src != NULLCH) {
60  if (*src != *cmp) {
61  break;
62  }
63  src++;
64  cmp++;
65  }
66  if ( (*src != NULLCH) || (*cmp != NULLCH) ) {
67  continue;
68  }
69 
70  /* prepare args for shell command */
71 
72  argv[0] = args[1];
73  argv[1] = "--help";
74  (*cmdtab[i].cfunc) (2, argv);
75  return 0;
76  }
77  printf("%s: no such command as '%s'\n", args[0], args[1]);
78  return 1;
79  }
80 
81  /* No arguments -- print a list of shell commands */
82 
83  printf("\nshell commands are:\n\n");
84 
85  /* Calculate the maximum length of a command name */
86 
87  maxlen = 0;
88  for (i = 0; i < ncmd; i++) {
89  len = strnlen(cmdtab[i].cname, 80);
90  if (len > maxlen) {
91  maxlen = len;
92  }
93  }
94 
95  /* Calculate the number of command names per line */
96 
97  cols = 80/(maxlen+1);
98  if (cols > 6) {
99  cols = 6;
100  }
101 
102  /* Calculate the width of a column */
103 
104  spac = 80/cols;
105 
106  /* Calculate the number of lines of output */
107 
108  lines = (ncmd+(cols-1))/cols;
109 
110  /* print the lines of command names */
111 
112  for (i=0; i<lines; i++) {
113  for (j=i; j<ncmd; j+=lines) {
114  len = strnlen(cmdtab[j].cname,80);
115  printf("%s", cmdtab[j].cname);
116  while (len < spac) {
117  printf(" ");
118  len++;
119  }
120  }
121  printf("\n");
122  }
123  return 0;
124 }
int32 strncmp(const char *, const char *, int32)
uint32 ncmd
XINUシェルが提供するコマンドの数
Definition: shell.c:35
#define stderr
Definition: stdio.h:17
int32 printf(const char *,...)
Definition: printf.c:13
char * cname
コマンド名称
Definition: shell.h:100
int int32
符号あり32ビット整数(int)
Definition: kernel.h:11
const struct cmdent cmdtab[]
XINUシェルが提供するコマンドを管理する配列
Definition: shell.c:10
int32 fprintf(int, char *,...)
Definition: fprintf.c:14
int32(* cfunc)(int32, char *[])
コマンドの機能を提供する関数(正確には関数ポインタ)
Definition: shell.h:104
int32 strnlen(const char *, uint32)
NULL終端された文字列の長さを返す。NULL終端は長さに含まない。
Definition: strnlen.c:14
#define NULLCH
NULL文字(NULL終端)
Definition: kernel.h:70
Here is the call graph for this function: