示例#1
0
/*
* Parses the input into commands and arguments using whitespace
* Then calls the associated function or error
* Maximum number of arguments is 5
*/
void parse(char *in)
{
	int argNum = 1;
	char *arg, *argArr[6] = {NULL, NULL, NULL, NULL, NULL, NULL};
	arg = (char *) malloc(sizeof(char) * (strlen(in) + 1));

	/* Parse command */
	arg = strtok(in, " \r\n\t");
	argArr[0] = arg;
	if (argArr[0] == NULL) {
		fprintf(stderr, "ERROR: unknown command\n");
		return;
	}

	/* Parse arguments */
	arg = strtok(NULL, " \r\n\t");
	while (arg != NULL && argNum < 6) {
		if (strcmp(arg, " ") && strcmp(arg, "\r")
			&& strcmp(arg, "\n")) {
			argArr[argNum] = arg;
			argNum++;
		}
		arg = strtok(NULL, " \r\n\t");
	}

	/* Execute according to command */
	if (strcmp(argArr[0], "exit") == 0) {
		printf("EXITING SHELL...\n");
		exit(EXIT_SUCCESS);
	} else if (strcmp(argArr[0], "cd") == 0) {
		cmd_cd(argArr[1]);
	} else if (strcmp(argArr[0], "pushd") == 0) {
		char *oldwd = (char *) malloc(sizeof(char) * strlen(cwd));
		strcpy(oldwd, cwd);
		if (cmd_cd(argArr[1]) == 0)
			dirStack = stackPush(dirStack, oldwd);
	} else if (strcmp(argArr[0], "popd") == 0) {
		if (dirStack != NULL) {
			cmd_cd(dirStack->str);
			dirStack = stackPop(dirStack);
		} else {
			fprintf(stderr, "ERROR: stack is empty\n");
		}
	} else if (strcmp(argArr[0], "dirs") == 0) {
		stackPrint(dirStack);
	} else if (strcmp(argArr[0], "path") == 0) {
		if (argArr[1] == NULL && argArr[1] == NULL)
			listPrint(pathStack);
		else if (strcmp(argArr[1], "+") == 0)
			pathFind(1, argArr[2]);
		else if (strcmp(argArr[1], "-") == 0)
			pathFind(-1, argArr[2]);
		else
			fprintf(stderr, "ERROR: bad flag: %s\n", argArr[1]);
	} else {
		cmd_exec(argArr, argNum);
	}
};
示例#2
0
文件: shell.c 项目: sethebner/shell
int cmd_back
    (
        char** args
    )
{
    args[0] = "cd";
    args[1] = "..";
    return cmd_cd( args );
} /* cmd_back() */
示例#3
0
int main() {
  char *input = NULL;
  size_t len = 0;
  ssize_t input_status;

  init_globals();

  char curr_dir[MAX_PATH_SIZE];
  getcwd(curr_dir, MAX_PATH_SIZE);
  printf("\n%s $ ", curr_dir);

 INPUT_LOOP:
  while ( (input_status = getline(&input, &len, stdin)) != -1) {

    if (strcmp(input, "exit\n") == 0) {
      input_status = 0;
      break;
    } else if (strncmp(input, "path", 4) == 0) {
      char *mod_ptr = NULL;
      if ( (mod_ptr = strstr(input, "+")) ) {
	cmd_path('+', mod_ptr+2);
      } else if ( (mod_ptr = strstr(input, "-")) ) {
	cmd_path('-', mod_ptr+2);
      } else {
	cmd_path('\0', NULL);
      }
    } else if (strncmp(input, "cd", 2) == 0) {
      cmd_cd(input+3);
    } else if (input[0] != '\n') {
      if (process_input(input)) {
	input_status = -1;
	break;
      }
    }
    
    getcwd(curr_dir, MAX_PATH_SIZE);
    printf("%s $ ", curr_dir);
  }

  // check if something went wrong and let the user try to input again.
  if (input_status == -1) {
    fprintf(stderr, "error: could not get or process the input line, %s\n", strerror(errno));
    goto INPUT_LOOP;
  } else {
    // normal exit condition reached, so let's put our toys away.
    if (input) {
      free (input);
      while (PATH.length) {
	free(pop_front(&PATH));
      }
    }
  }

  return 0;
}
示例#4
0
文件: shell.c 项目: sethebner/shell
int cmd_cd_list
    (
        char** args
    )
{
    cmd_cd( args );

    args[0] = "ls";
    args[1] = NULL;
    return shell_launch( args );
} /* cmd_cd_list() */
示例#5
0
/**
 * Handle the built in commands: cd and exit.
 */
int handle_builtin(char *args[ARGS_SIZE]) {

    if (strcmp(args[0], BI_CD) == 0) {
        cmd_cd(args);
        return TRUE;

    } else if (strcmp(args[0], BI_EXIT) == 0) {
        cmd_exit();
        return TRUE;
    } else if (strcmp(args[0], BI_CHKENV) == 0) {
        cmd_check_env(args);
        return TRUE;
    }

    return FALSE;
}
示例#6
0
static int do_cmd(int cmd, int argc, const char *argv[])
{
	switch (cmd) {
		case CMD_INTERACTIVE:
		case CMD_UNKNOWN:
			return cmd_loop(argc, argv);
			break;
		case CMD_CD:
			return cmd_cd(argv[0]);
			break;
		case CMD_PWD:
			return cmd_pwd();
			break;
		case CMD_LS:
			return cmd_ls(argc, argv);
			break;
		case CMD_MKDIR:
			return cmd_mkdir(argc, argv);
			break;
		case CMD_LN:
			return cmd_ln(argc, argv);
			break;
		case CMD_RM:
			return cmd_rm(argc, argv);
			break;
		case CMD_MV:
			return cmd_mv(argc, argv);
			break;
		case CMD_CP:
			return cmd_cp(argc, argv);
			break;
		case CMD_CAT:
			return cmd_cat(argc, argv);
			break;
		case CMD_STAT:
			return cmd_stat(argc, argv);
			break;
		case CMD_QUIT:
			exit(EXIT_SUCCESS);
			break;
		default:
			return -1;
			break;
	}
}
示例#7
0
文件: clt_cmd.c 项目: rfrey/ft_p
int		read_cmd(int sock, char *cmd)
{
	if (ft_strequ(cmd, "quit"))
		return (1);
	else if (ft_strequ(cmd, "pwd"))
		cmd_pwd(sock);
	else if (ft_strnequ(cmd, "ls", 2))
		cmd_ls(sock);
	else if (ft_strnequ(cmd, "cd", 2))
		cmd_cd(sock);
	else if (ft_strnequ(cmd, "get ", 4))
		cmd_get(sock, cmd);
	else if (ft_strnequ(cmd, "put ", 4))
		cmd_put(sock, cmd);
	else
		cmd_unknow(cmd);
	return (0);
}
示例#8
0
// 运行客户端
void run(char* ip, char* pt) {
    int  sockfd = connectToHost(ip, pt);
    if (getReplyCode(sockfd) != SERVICE_READY)
        errorReport("Service Connect Error!");
    while (userLogin(sockfd) != 0)      // 调用登录函数userLogin
        printf("Please try again.\n");
    int isQuit = 0;
    char buf[BUF_SIZE];
    while (!isQuit) {
        printf("[Client command] ");
        fgets(buf, sizeof(buf), stdin);
        switch (cmdToNum(buf)) {
            case GET:
                cmd_get(sockfd, buf);
                break;
            case PUT:
                cmd_put(sockfd, buf);
                break;
            case PWD:
                cmd_pwd(sockfd);
                break;
            case DIR:
                cmd_dir(sockfd);
                break;
            case CD:
                cmd_cd(sockfd, buf);
                break;
            case HELP:
                cmd_help();
                break;
            case QUIT:
                cmd_quit(sockfd);
                isQuit = 1;
                break;
            default:
                cmd_help();
                break;
        }
    }
    close(sockfd);
}
示例#9
0
int parse_cmd(int fd, conn_item* item)
{
	cmd_header* ch;
	assert(fd > 0 && item && item->cmd_rd_buf);
		
	ch = (cmd_header*)item->cmd_rd_buf;
	switch(ch->cmd_type) {
		case CMD_LS_DIR:
			cmd_ls(item);
			item->state = TFTP_SEND_DATA;
			break;
		
		case CMD_CD_DIR:
			cmd_cd(item);
			item->state = TFTP_SEND_DATA;
			break;

		case CMD_UPLOAD_FILE:
			if(try_cmd_upload(item) == T_FTP_FAIL) {
				item->state = TFTP_SEND_DATA;
			}
			else {
				item->state = TFTP_UPLOAD_FILE;
			}
			break;

		case CMD_DOWNLOAD_FILE:
			break;

		case CMD_CANCLE_TRANS:
			break;

		default:
			break;
	}

	return T_FTP_SUCCESS;
}
示例#10
0
文件: shell.c 项目: Rotte/osm-k
int run_command(char* cmdline) {
  char* argv[BUFFER_SIZE];
  int argc = tokenize(cmdline, argv);
  if (argc == 0) {
    return 0;
  }
  if (strcmp(argv[0], "wait") == 0) {
    return cmd_wait(argc, argv);
  } else if (strcmp(argv[0], "ls") == 0) {
    return cmd_ls(argc, argv);
  } else if (strcmp(argv[0], "cp") == 0) {
    return cmd_cp(argc, argv);
  } else if (strcmp(argv[0], "show") == 0) {
    return cmd_show(argc, argv);
  } else if (strcmp(argv[0], "rm") == 0) {
    return cmd_rm(argc, argv);
  } else if (strcmp(argv[0], "pwd") == 0) {
    return cmd_pwd(argc, argv);
  } else if (strcmp(argv[0], "cd") == 0) {
    return cmd_cd(argc, argv);
  } else if (strcmp(argv[0], "cmp") == 0) {
    return cmd_cmp(argc, argv);
  } else if (strcmp(argv[0], "echo") == 0) {
    return cmd_echo(argc, argv);
  } else if (strcmp(argv[0], "mkdir") == 0) {
    return cmd_mkdir(argc, argv);
  } else if (strcmp(argv[0], "rmdir") == 0) {
    return cmd_rmdir(argc, argv);
  } else {
    int k = strlen(argv[argc-1]);
    if (argv[argc-1][k-1] == '&') {
      argv[argc-1][k-1] = '\0';
      return background_run(cmdline);
    } else {
      return cmd_run(cmdline);
    }
  }
}
示例#11
0
文件: command.c 项目: osstudy/glidix
int execCommand(char *cmd)
{
	if (*cmd == '#') return 0;

	// remove comments
	int currentlyInString = 0;
	char *commentScan = cmd;

	while (*commentScan != 0)
	{
		if ((*commentScan) == '"') currentlyInString = !currentlyInString;
		else if (((*commentScan) == '#') && (!currentlyInString))
		{
			*commentScan = 0;
			break;
		};

		commentScan++;
	};

	if (*cmd == 0) return 0;

	char processedCommand[1024];
	char *put = processedCommand;
	char *scan = cmd;
	while (*scan != 0)
	{
		if (*scan == '\\')
		{
			scan++;
			if (*scan == 0) break;
			*put++ = *scan++;
			continue;
		}
		else if (*scan == '$')
		{
			char envname[256];
			char *envnameput = envname;
			scan++;
			while (isalnum(*scan))
			{
				*envnameput++ = *scan++;
			};

			*envnameput = 0;
			char *value = getenv(envname);
			if (value != NULL)
			{
				strcpy(put, value);
				put += strlen(value);
			};
		}
		else
		{
			*put++ = *scan++;
		};
	};
	*put = 0;

	int argc = 0;
	char **argv = NULL;
	char *nextToStrtok = processedCommand;

	while (1)
	{
		//char *token = strtok(nextToStrtok, nextSplitString);
		char *token = nextToStrtok;

#if 0
		nextToStrtok = NULL;
		if (token != NULL)
		{
			if (strlen(token) == 0) continue;
		};
#endif

		if (token != NULL)
		{
			while (isspace(*token)) token++;
			if (*token == 0)
			{
				token = NULL;
			}
			else
			{
				const char *termString = " \t";
				if (*token == '"')
				{
					termString = "\"";
					token++;
				};

				char *endpos = strpbrk(token, termString);
				if (endpos == NULL)
				{
					nextToStrtok = NULL;
				}
				else
				{
					*endpos = 0;
					nextToStrtok = endpos+1;
				};
			};
		};

		int shouldAdd = 0;
		if (token == NULL)
		{
			shouldAdd = 1;
		}
		else
		{
			if (strlen(token) > 0)
			{
				shouldAdd = 1;
			};
		};

		if (shouldAdd)
		{
			argv = realloc(argv, sizeof(char*)*(argc+1));
			argv[argc++] = token;
		};

		if (token == NULL) break;
	};

	if (argc == 1) return 0;

	if (strcmp(argv[0], "cd") == 0)
	{
		int status = cmd_cd(argc-1, argv);
		free(argv);
		return status;
	}
	else if (strcmp(argv[0], "exit") == 0)
	{
		int status = cmd_exit(argc-1, argv);
		free(argv);
		return status;
	}
	else if (strcmp(argv[0], "echo") == 0)
	{
		int status = cmd_echo(argc-1, argv);
		free(argv);
		return status;
	}
	else if (strcmp(argv[0], "diag") == 0)
	{
		_glidix_diag();
		free(argv);
		return 0;
	};

	char execpath[256];
	if (findCommand(execpath, argv[0]) == -1)
	{
		free(argv);
		fprintf(stderr, "%s: command not found\n", argv[0]);
		return 1;
	};

	pid_t pid = fork();
	if (pid == 0)
	{
		if (execv(execpath, argv) != 0)
		{
			perror(argv[0]);
			return 1;
		};

		// the compiler won't stop moaning otherwise.
		// even though execv() obviously doesn't return.
		return 0;
	}
	else
	{
		free(argv);
		shellChildPid = pid;
		int status;
		while (1)
		{
			int ret = waitpid(pid, &status, 0);
			
			// EINTR is the only possible error here, so if it occurs, try again.
			if (ret != -1)
			{
				break;
			};
		};

		shellChildPid = 0;
		
		if (WIFSIGNALLED(status))
		{
			int termsig = WTERMSIG(status);
			switch (termsig)
			{
			case SIGSEGV:
				fprintf(stderr, "Invalid memory access\n");
				break;
			case SIGSYS:
				fprintf(stderr, "Invalid system call\n");
				break;
			};
		};
		
		return status;
	};
};
示例#12
0
/*
	execute command
*/
EXPORT	INT	exec_cmd(B *cmd)
{
	INT	ac;
	B	*av[N_ARGS];

	ac = setup_param(cmd, av);
	if (ac < 1) return 0;

	if (strcmp(av[0], "date") == 0) {
		cmd_date(ac, av);
	} else if (strcmp(av[0], "attach") == 0) {
		cmd_attach(ac, av);
	} else if (strcmp(av[0], "detach") == 0) {
		cmd_detach(ac, av);
	} else if (strcmp(av[0], "mkdir") == 0) {
		cmd_mkdir(ac, av);
	} else if (strcmp(av[0], "rmdir") == 0) {
		cmd_rmdir(ac, av);
	} else if (strcmp(av[0], "pwd") == 0) {
		cmd_pwd(ac, av);
	} else if (strcmp(av[0], "cd") == 0) {
		cmd_cd(ac, av);
	} else if (strcmp(av[0], "rm") == 0) {
		cmd_rm(ac, av);
	} else if (strcmp(av[0], "mv") == 0) {
		cmd_mv(ac, av);
	} else if (strcmp(av[0], "ls") == 0) {
		cmd_ls(ac, av);
	} else if (strcmp(av[0], "tp") == 0 || strcmp(av[0], "tpx") == 0) {
		cmd_tp(ac, av);
	} else if (strcmp(av[0], "cp") == 0) {
		cmd_cp(ac, av);
	} else if (strcmp(av[0], "trunc") == 0) {
		cmd_trunc(ac, av);
	} else if (strcmp(av[0], "df") == 0) {
		cmd_df(ac, av);
	} else if (strcmp(av[0], "sync") == 0) {
		cmd_sync(ac, av);
	} else if (strcmp(av[0], "chmod") == 0) {
		cmd_chmod(ac, av);
	} else if (strcmp(av[0], "ref") == 0) {
		cmd_ref(ac, av);
	} else if (strcmp(av[0], "load") == 0) {
		cmd_load(ac, av);
	} else if (strcmp(av[0], "loadspg") == 0) {
		cmd_loadspg(ac, av);
	} else if (strcmp(av[0], "unload") == 0) {
		cmd_unload(ac, av);
	} else if (strcmp(av[0], "call") == 0) {
		cmd_call(ac, av);
	} else if (strncmp(av[0], "?", 1) == 0) {
		P("date     [y m d [h m s]]\n");
		P("attach   devnm connm\n");
		P("detach   connm\n");
		P("cd       dir\n");
		P("pwd      \n");
		P("ls       [-t][-l][dir]\n");
		P("mkdir    dir [mode]\n");
		P("rmdir    dir\n");
		P("rm       path\n");
		P("mv       o-path n-path\n");
		P("trunc    path len\n");
		P("df       path\n");
		P("sync     [path [d]]\n");
		P("chmod    path mode\n");
		P("tp       path [ofs len]\n");
		P("tpx      path [ofs len]\n");
		P("cp       s-path d-path/dir [wofs [wlen]]\n");
		P("ref      [item]\n");
		P("call     addr [p1 p2 p3]\n");
		P("load     path\n");
		P("loadspg  path [arg ...]\n");
		P("unload   progid\n");
#ifdef	NET_SAMPLE
		P("net      execute network sample\n");

	} else if (strcmp(av[0], "net") == 0) {
IMPORT	void	net_test(void);
		net_test();
#endif
	} else {
		return 0;
	}
	return 1;
}