Adds the 'script' command

git-svn-id: file:///home/svn/framework3/trunk@5178 4d416f70-5f16-0410-b530-b9f4589650da
unstable
HD Moore 2007-10-23 22:23:34 +00:00
parent 55ddf3865a
commit 13426097eb
3 changed files with 82 additions and 36 deletions

View File

@ -8,7 +8,15 @@
#ifndef _CMD_H
#define _CMD_H
/* Base */
int process_input(char *, int);
void parse(char *, int *, char * []);
void chomp(char *);
void cmd_script(int, char * []);
/* XXX: Re-do help to specify a category and print the commands in that category? */
void cmd_help(int, char * []);
void cmd_fork(int, char * []);

View File

@ -13,6 +13,7 @@
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include "cmd.h"
@ -137,3 +138,25 @@ void cmd_quit(int argc, char * argv[])
{
exit(0);
}
void cmd_script(int argc, char * argv[])
{
FILE *fd;
char buff[2048];
fd = fopen(argv[1], "r");
if (fd == NULL) {
perror("fopen");
return;
}
printf("Executing script %s\n", argv[1]);
while (fgets(buff, sizeof(buff), fd)) {
chomp(buff);
process_input(buff, sizeof(buff));
}
fclose(fd);
}

View File

@ -16,7 +16,6 @@
#include "cmd.h"
void parse(char *, int *, char * []);
void chomp(char *);
@ -32,6 +31,7 @@ struct __cmdhandler
struct __cmdhandler handlerlist[] =
{
{ "help", &cmd_help, 1, 0, 0 },
{ "script", &cmd_script, 1, 1, 1 },
{ "fork", &cmd_fork, 1, 0, 0 },
{ "exec", &cmd_exec, 1, 1, 14 },
{ "system", &cmd_system, 1, 1, 14 },
@ -89,6 +89,7 @@ struct __cmdhandler handlerlist[] =
#define HANDLERLIST_SIZE (sizeof(handlerlist) / sizeof(struct __cmdhandler))
#define MAX_ARGV 15
#define VERSION "0.01"
int main(int argc, char **argv) {
int sig;
@ -119,13 +120,9 @@ int main(int argc, char **argv) {
{
char cmd[2048];
char cmd_bak[sizeof(cmd)];
char * argv[MAX_ARGV];
char buf[1024];
int argc;
int i, hit;
char *cwd;
if(getcwd(buf, sizeof(buf)) == NULL)
cwd = "(unknown)";
else
@ -136,42 +133,60 @@ int main(int argc, char **argv) {
memset(cmd, 0, sizeof(cmd));
if(fgets(cmd, sizeof(cmd), stdin) == NULL)
exit(0);
chomp(cmd);
memcpy(cmd_bak, cmd, sizeof(cmd_bak));
parse(cmd, &argc, argv);
if(argc == 0)
continue;
for(hit = i = 0; i < HANDLERLIST_SIZE; i++)
{
if(strcmp(argv[0], handlerlist[i].cmd) == 0)
{
hit = 1;
if(handlerlist[i].arg_process)
{
if(argc > handlerlist[i].arg_max+1)
printf("%s: Too many arguments\n", argv[0]);
else if(argc < handlerlist[i].arg_min+1)
printf("%s: Too few arguments\n", argv[0]);
else
handlerlist[i].handler(argc, argv);
}
else
{
handlerlist[i].handler(cmd_bak + strlen(handlerlist[i].cmd) + 1);
}
}
}
if(hit == 0)
{
printf("%s: Unknown command.\n", argv[0]);
}
process_input(cmd, sizeof(cmd));
}
}
int process_input(char *cmd, int cmd_size) {
char * argv[MAX_ARGV];
int argc;
int i, hit;
char *bak;
parse(cmd, &argc, argv);
if(argc == 0)
return(0);
bak = strdup(cmd);
for(hit = i = 0; i < HANDLERLIST_SIZE; i++)
{
if(strcmp(argv[0], handlerlist[i].cmd) == 0)
{
hit = 1;
if(handlerlist[i].arg_process)
{
if(argc > handlerlist[i].arg_max+1)
printf("%s: Too many arguments\n", argv[0]);
else if(argc < handlerlist[i].arg_min+1)
printf("%s: Too few arguments\n", argv[0]);
else
handlerlist[i].handler(argc, argv);
}
else
{
handlerlist[i].handler(bak + strlen(handlerlist[i].cmd) + 1);
}
}
}
if(hit == 0)
{
printf("%s: Unknown command.\n", argv[0]);
}
free(bak);
return 0;
}
void parse(char * str, int * const argc, char * argv[])
{
*argc = 0;