static int HelpProc(struct cmd_syndesc *as, void *arock) { struct cmd_syndesc *ts; struct cmd_item *ti; int ambig; int code = 0; if (as->parms[0].items == 0) { printf("%sCommands are:\n", NName(as->a0name, ": ")); for (ts = allSyntax; ts; ts = ts->next) { if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN)) continue; printf("%-15s %s\n", ts->name, (ts->help ? ts->help : "")); } } else { /* print out individual help topics */ for (ti = as->parms[0].items; ti; ti = ti->next) { code = 0; ts = FindSyntax(ti->data, &ambig); if (ts && (ts->flags & CMD_HIDDEN)) ts = 0; /* no hidden commands */ if (ts) { /* print out command name and help */ printf("%s%s: %s ", NName(as->a0name, " "), ts->name, (ts->help ? ts->help : "")); ts->a0name = as->a0name; PrintAliases(ts); PrintSyntax(ts); PrintFlagHelp(ts); } else { if (!ambig) fprintf(stderr, "%sUnknown topic '%s'\n", NName(as->a0name, ": "), ti->data); else { /* ambiguous, list 'em all */ fprintf(stderr, "%sAmbiguous topic '%s'; use 'apropos' to list\n", NName(as->a0name, ": "), ti->data); } code = CMD_UNKNOWNCMD; } } } return (code); }
//Run commands that are built-in shell functions static void RunBuiltInCmd(commandT* cmd) { //Send SIGCONT to a backgrounded job, but do not give it the foreground if (strncmp(cmd->argv[0], "bg", 2) == 0) { //If there are two arguments in the command... if (cmd->argc == 2) //Bring the process with the given jobNumber continueBgJob(strtol(cmd->argv[1],NULL,10)); //If there is one argument in the command... else if (cmd->argc == 1) //Bring the most recent background process to the foreground continueBgJob(-1); else { fprintf(stderr, "Too many arguments were given with bg.\n"); } } //Return a backgrounded job to the foreground else if (strncmp(cmd->argv[0], "fg", 2) == 0) { //If there are two arguments in the command... if (cmd->argc == 2) //Bring the process with the given jobNumber bringToForeground(strtol(cmd->argv[1],NULL,10)); //If there is one argument in the command... else if (cmd->argc == 1) //Bring the most recent background process to the foreground bringToForeground(-1); else { fprintf(stderr, "Too many arguments were given with fg.\n"); } } //makenew alias else if (strncmp(cmd->argv[0], "alias", 5) == 0) { //If there are two arguments in the command... if (cmd->argc == 2) //make new binding AddAlias(cmd); //show all bindings else if (cmd->argc == 1) PrintAliases(); } else if (strncmp(cmd->argv[0], "unalias", 7) == 0) { RemoveAlias(cmd->argv[1]); } else if (strncmp(cmd->argv[0], "cd", 2) == 0) { int err; //If a directory is given, go to that directory if (cmd->argc == 2) err = chdir(cmd->argv[1]); //If a directory isn't given, go to the user's home directory else err = chdir(getenv("HOME")); //If there was a problem changing directories, print an error if (err == -1) fprintf(stderr, "%s\n", "Invalid directory\n"); } //Print the list of background jobs (bgJobsHead) else if (strncmp(cmd->argv[0], "jobs", 4) == 0){ PrintBgJobList(); } else { fprintf(stderr, "%s is an unrecognized internal command\n", cmd->argv[0]); fflush(stdout); } }