int run(char* cmd, int input, int first, int last, int* n, int delimitFlag) { int output = STANDARD_OUTPUT; getRedir(cmd); //redir_num and fileName get assigned here. if(redir_num == OUTPUT_REDIRECTION_NUM || redir_num == APPEND_OUTPUT_REDIRECTION_NUM) { openFile(); output = fd; } else if(redir_num == INPUT_REDIRECTION_NUM) { openFile(); input = fd; } split(cmd, args); if (args[0] != NULL) { /* if (strcmp(args[0], EXIT) == 0) exit(0); */ (*n) += 1; if(delimitFlag == AMPERSAND_NUM) { return bgCommand(input, first, output); } else if(delimitFlag == PIPE_NUM) { return pipeCommand(input, first, last); } else if(delimitFlag == NO_BLOCK_ENDING) { return command(input, first, output); } } return 0; }
TString IRODS::info(const Char_t* file, Option_t* opt) { // returns meta-infos on given file. buildCommand("imeta", file, opt); return pipeCommand(); }
TString IRODS::list(const Char_t* directory) { // returns simple listing of current directory (default) or given directory. // returns empty string if directory is unknown. buildCommand("ils", directory); return pipeCommand(); }
TString IRODS::longlist(const Char_t* directory) { // returns long-format listing of current directory (default) or given directory. // returns empty string if directory is unknown. #ifdef CCIN2P3_BUILD buildCommand("ils", directory, "-l"); return pipeCommand(); #else return list(directory);//outside of CCIN2P3, only short-format list works #endif }
/** * pipeData is called when the plugin has sent some data. * This happens when one of the Javascript methods of an * plugin object is called. */ void pipeData(void) { PipeCommand command = pipe_readCommand(stdin); char *url = pipe_readString(stdin); char *hostname = pipe_readString(stdin); char *ip = pipe_readString(stdin); pipeCommand(command, url, hostname, ip); free(ip); free(hostname); free(url); }
int main() { char *cmdline, *prompt, **arglist, **cmdlist; char rfileName[32]; int result, fd_out, i, fno, cmdNum, rioFlag; int sfd = dup(STDOUT_FILENO); void setup(); prompt = DFL_PROMPT; setup(); cmdlist = emalloc(BUFSIZ); while ((cmdline = next_cmd(prompt, stdin)) != NULL) { /* io redirection */ rioFlag = getRFileName(cmdline, rfileName); if (rioFlag > 0) fd_out = r_open(rfileName); /* pipe test*/ cmdNum = pipeTest(cmdline, cmdlist); if (cmdNum == 1) { if ((arglist = splitline(cmdline)) != NULL) { if (buildIn(arglist)) { ; } else { result = execute(arglist); freelist(arglist); } } } else if (cmdNum == 2) { pipeCommand(cmdlist); } //freelist(cmdlist); free(cmdline); if (rioFlag > 0) r_close(sfd); } return 0; }
/*the operation of the run command, takes one argument*/ void runCommand(char * s){ FILE * fp; char** command; char** tokens; char * line = NULL; char *infilename, *outfilename; size_t len = 0; ssize_t read; int status; fp = fopen(s, "r"); if(fp == NULL){ perror(""); } else{ while ((read = getline(&line, &len, fp)) != -1) { //read the next line from the file and executing it tokens = tokenize(line); // Calling the tokenizer function on the input line command = (char **) malloc(MAXLINE*sizeof(char**)); //Check for input and output redirection int inCounter=0 , outCounter=0, pipeCounter = 0, appendFlag=0; int k=0; int flag = 1; int pipeFlag = 0; for(k=0;tokens[k]!=NULL;k++){ if( is_equal(tokens[k], "<\0")){ inCounter++; if(inCounter == 1){ infilename = tokens[k + 1]; } flag = 0; } if( is_equal(tokens[k], ">\0")){ outCounter++; if(outCounter == 1){ if(is_equal(tokens[k+1], ">\0")){ appendFlag=1; outfilename=tokens[k + 2]; outCounter++; k++; } else outfilename = tokens[k + 1]; } flag = 0; } if(is_equal(tokens[k], "|\0")){ pipeFlag = 1; } if(flag == 1){ command[k] = tokens[k]; } } if(pipeFlag == 1){ pipeCommand(tokens); } else{ executeFile(command, inCounter, infilename, outCounter, outfilename, appendFlag); } inCounter=0; outCounter=0; pipeCounter = 0; appendFlag=0; } fclose(fp); } int i = 0; //Free up the memory for(i=0;command[i]!=NULL;i++){ free(command[i]); } free(command); for(i=0;tokens[i]!=NULL;i++){ free(tokens[i]); } free(tokens); }
int main(int argc, char** argv){ //Setting the signal interrupt to its default function. signal(SIGINT, handler); signal(SIGCHLD, sigchldHandler); children = (pid_t *)malloc(1000*sizeof(pid_t)); children[0] = -1; //get the pid of the current process pid = getpid(); ready = 1; //Allocating space to store the previous commands. int numCmds = 0; char **cmds = (char **)malloc(1000 * sizeof(char *)); char input[MAXLINE]; char** tokens; char** command; int notEOF = 1; int i; //variables to store information about I/O redirection int inCounter=0 , outCounter=0, pipeCounter = 0, appendFlag=0; char * infilename; char * outfilename; FILE* stream = stdin; while(notEOF) { if (ready == 1){ printf("$ "); // the prompt fflush(stdin); } char *in = fgets(input, MAXLINE, stream); //taking input one line at a time //Checking for EOF if (in == NULL){ if (DEBUG) printf("EOF found\n"); exit(0); } //add the command to the command list. cmds[numCmds] = (char *)malloc(sizeof(input)); strcpy(cmds[numCmds++], input); // Calling the tokenizer function on the input line tokens = tokenize(input); command = (char **) malloc(MAXLINE*sizeof(char**)); //Check for input and output redirection int k=0; int flag = 1; int pipeFlag = 0; for(k=0;tokens[k]!=NULL;k++){ if( is_equal(tokens[k], "<\0")){ inCounter++; if(inCounter == 1){ infilename = tokens[k + 1]; } flag = 0; } if( is_equal(tokens[k], ">\0")){ outCounter++; if(outCounter == 1){ if(is_equal(tokens[k+1], ">\0")){ //if appending is done while output redirection appendFlag=1; outfilename=tokens[k + 2]; outCounter++; k++; } else outfilename = tokens[k + 1]; } flag = 0; } if(is_equal(tokens[k], "|\0")){ pipeFlag = 1; } if(flag == 1){ command[k] = tokens[k]; } } //if piping is used if(pipeFlag == 1){ pipeCommand(tokens); } else { if(inCounter > 1){ perror("More than one input redirections."); } else if(outCounter > 2 || (appendFlag == 0 && outCounter == 2)){ perror("More than one output redirections."); } else{ // check tokens and execute corresponding command if(tokens[0] == NULL){ printf(""); } else if( is_equal(tokens[0], "run\0") ) { runCommand(tokens[1]); } else if(is_equal(tokens[0], "cd\0")){ cdCommand(tokens[1]); } else if(is_equal(tokens[0], "cron\0")){ cronCommand(tokens[1]); } else if(is_equal(tokens[0], "parallel\0")){ parallelCommand(command); } else if(is_equal(tokens[0], "exit\0")){ exitCommand(); } else{ executeFile(command, inCounter, infilename, outCounter, outfilename, appendFlag); } } } inCounter = 0; outCounter = 0; appendFlag = 0; } printf("Print and deallocate %s\n", tokens[0]); // Freeing the allocated memory for(i=0;tokens[i]!=NULL;i++){ free(tokens[i]); } free(tokens); for(i=0;command[i]!=NULL;i++){ free(command[i]); } free(command); return 0; }