Config* config_loadfile(const char* path) { FILE* fp = fopen(path, "r"); if (fp == NULL) { return NULL; } Config* conf = malloc(sizeof(Config)); if (conf == NULL) { //TODO will currently give a ERR_CONFIG error, should give ERR_OOM fclose(fp); return NULL; } char line[256]; Pair* current = NULL; while (fgets(line, sizeof(line), fp) != NULL) { Pair* p = parseline(line); if (p != NULL) { if (current == NULL) { current = p; conf->first = current; } else { current->next = p; } } } fclose(fp); return conf; }
main(int argc, char *argv[]) { float themeans[MAXREADBACKS]; float thessds[MAXREADBACKS]; float thecounts[MAXREADBACKS]; int x; char theline[512]; for(x=0;x<MAXREADBACKS; x++) themeans[x] = thessds[x] = thecounts[x] = 0.0; mkdir(argv[1],0755); while(gets(theline) != NULL) { if( (strncmp(theline,"_SCAN",5) == 0) && /* ONLY DO SCAN LINES */ ( isdigit(theline[strlen(theline)-2]) ) /* WITH NUMBERS... */ ) /* -2 for ^M or "3.23" */ { /* so is ok either way */ parseline(theline,themeans,thessds,thecounts, argv[1]); } } printstats(themeans, thessds, thecounts, argv[1]); }
/* * eval - Evaluate the command line that the user has just typed in * * If the user has requested a built-in command (quit, jobs, bg or fg) * then execute it immediately. Otherwise, fork a child process and * run the job in the context of the child. If the job is running in * the foreground, wait for it to terminate and then return. Note: * each child process must have a unique process group ID so that our * background children don't receive SIGINT (SIGTSTP) from the kernel * when we type ctrl-c (ctrl-z) at the keyboard. */ void eval(char *cmdline) { char *argv[MAXARGS]; int isBG = parseline(cmdline, argv); if (argv[0] == NULL) return; if (builtin_cmd(argv)) return; sigset_t allset; sigfillset(&allset); sigprocmask(SIG_BLOCK, &allset, NULL); pid_t childpid = fork(); if (childpid == -1) unix_error("fork failed"); if (childpid == 0) { sigprocmask(SIG_UNBLOCK, &allset, NULL); if (setpgid(0, 0) == -1) unix_error("setpgrp failed"); if (execv(argv[0], argv) == -1) unix_error("execv failed"); } if (isBG) { addjob(jobs, childpid, BG, cmdline); printjob(jobs, childpid); sigprocmask(SIG_UNBLOCK, &allset, NULL); } else { addjob(jobs, childpid, FG, cmdline); sigprocmask(SIG_UNBLOCK, &allset, NULL); waitfg(childpid); } return; }
void processline(bc *bc, char *line) { char token[64], *lp; struct cmd *cmd; lp=line; gettoken(token, sizeof(token), &lp); skipwhite(&lp); cmd=commandlist; while(cmd->name) { if(!strncmp(token, cmd->name, strlen(token))) { cmd->func(bc, lp); return; } ++cmd; } if(line[0]>='0' && line[0]<='9') // line number { bc->flags |= BF_NOPROMPT; lp=line; while(*lp>='0' && *lp<='9') ++lp; if(*lp) addline(bc, line); else deleteline(bc, atoi(line)); } else if(*line) { parseline(bc, line); } else bc->flags |= BF_NOPROMPT; }
/* configure this client */ bool aiccu_LoadConfig(const char *filename) { FILE *f; char buf[1000]; char filenames[256]; unsigned int line = 0; if (!filename) { aiccu_LocateFile(AICCU_CONFIG, filenames, sizeof(filenames)); filename = filenames; } f = fopen(filename, "r"); if (!f) { dolog(LOG_ERR, "Could not open config file \"%s\"\n", filename); return false; } while (fgets(buf, sizeof(buf), f)) { line++; if (parseline(buf, " ", aiccu_conf_rules, g_aiccu)) continue; dolog(LOG_WARNING, "Unknown configuration statement on line %u of %s: \"%s\"\n", line, filename, buf); } fclose(f); return true; }
int main(int argc, char **argv) { int i; FILE *fd; char *file, buf[1024]; if (argc==1 || !strcmp (argv[1], "-h")) { fprintf (stderr, "Usage: rarun2 [''|script.rr2] [options ...]\n" "> options are file directives:\n"); printf ( "program=/bin/ls\n" "arg1=/bin\n" "# arg#=...\n" "setenv=FOO=BAR\n" "timeout=3\n" "# connect=localhost:8080\n" "# listen=8080\n" "# stdout=foo.txt\n" "# stdin=input.txt\n" "# input=input.txt\n" "# chdir=/\n" "# chroot=/mnt/chroot\n" "# preload=/lib/libfoo.so\n" "# setuid=2000\n" "# seteuid=2000\n" "# setgid=2001\n" "# setegid=2001\n"); return 1; } file = argv[1]; if (*file) { fd = fopen (file, "r"); if (!fd) { fprintf (stderr, "Cannot open %s\n", file); return 1; } for (;;) { fgets (buf, sizeof (buf)-1, fd); if (feof (fd)) break; buf[strlen (buf)-1] = 0; parseline (buf); } fclose (fd); } else { for (i=2; i<argc; i++) parseline (argv[i]); } return runfile (); }
/* * eval - Evaluate the command line that the user has just typed in. * * If the user has requested a built-in command (quit, jobs, bg or fg) * then execute it immediately. Otherwise, fork a child process and * run the job in the context of the child. If the job is running in * the foreground, wait for it to terminate and then return. Note: * each child process must have a unique process group ID so that our * background children don't receive SIGINT (SIGTSTP) from the kernel * when we type ctrl-c (ctrl-z) at the keyboard. * * Requires: * "cmdline" is a NUL ('\0') terminated string with a trailing * '\n' character. "cmdline" must contain less than MAXARGS * arguments. * * Effects: * A built-in command is executed immediately. Otherwise, it attempts * to fork a child process and execute the job. If necessary, the * executable program is searched through the directories of the * search path. If not found, an error is thrown. If the job is * running as a foreground job, it waits until completion. */ static void eval(const char *cmdline) { char *argv[MAXARGS]; int bg = parseline(cmdline, argv); pid_t pid; sigset_t mask; // Checks command line is not empty. if (!argv[0]) { return; } // Checks that the command is not a built-in command. if(!builtin_cmd(argv)){ sigemptyset(&mask); sigaddset(&mask, SIGCHLD); sigprocmask(SIG_BLOCK, &mask, NULL); // A child is forked. pid = fork(); if (pid == 0) { // Put child in new group whose ID is identical to child’s PID. setpgid(0, 0); sigprocmask(SIG_UNBLOCK, &mask, NULL); if (execve(argv[0], argv, environ) == -1) { int index = 0; // Run through directories in search path to execute program. while (argv[0][0] != '.' && index < directoryCount) { if (execve(strcat(directories[index],argv[0]), argv, environ) != -1) { break; } index++; } // Command not found. char *print; asprintf(&print, "%s: Command not found\n", argv[0]); sio_error(print); } } if (bg == 0) { addjob(jobs,pid,FG,cmdline); sigprocmask(SIG_UNBLOCK, &mask, NULL); // Wait for foreground jobs to complete. waitfg(pid); } else { addjob(jobs,pid,BG,cmdline); sigprocmask(SIG_UNBLOCK, &mask, NULL); printf("[%d] (%d) %s", pid2jid(pid), pid, cmdline); } } return; }
/** Funkcja wykonuje komendę w procesie potomnym. */ void execute() { line* ln = NULL; command* com = NULL; int i; /**printf("STRLEN %d\n", strlen(buffer_to_parse));*/ /// for(i=0;i<strlen(buffer_to_parse);++i) /// printf("%c", buffer_to_parse[i]); ///printf("\n"); ln = parseline(buffer_to_parse); if((ln == NULL) || (*(ln->pipelines) == NULL) || (**(ln->pipelines) == NULL)) { ///printf("LN NULL\n"); printf("%s\n", SYNTAX_ERROR_STR); fflush(stdout); return ; } com = pickfirstcommand(ln); if((com == NULL) || (com->argv == NULL) || (com->argv[0] == NULL)) { ///printf("COM NULL\n"); if(buffer_to_parse[0] != '#') /** jeśli linia nie jest komentarzem */ { ///printf("COM NULL\n"); ///printf("%s\n", buffer_to_parse); printf("%s\n", SYNTAX_ERROR_STR); } fflush(stdout); return ; } int child_pid = fork(); if(child_pid == -1) exit(1); if(child_pid > 0) /// parent { int wait_status = waitpid(child_pid, NULL, 0); if(wait_status == -1) exit(1); } else /// child { int exec_status = execvp(com->argv[0], com->argv); if(exec_status == -1) { if(errno == ENOENT) /** 2 */ fprintf(stderr, "%s: no such file or directory\n", com->argv[0]); else if(errno == EACCES) /** 13 */ fprintf(stderr, "%s: permission denied\n", com->argv[0]); else fprintf(stderr, "%s: exec error\n", com->argv[0]); fflush(stdout); exit(EXEC_FAILURE); } } }
/* * eval - Evaluate the command line that the user has just typed in * * If the user has requested a built-in command (quit, jobs, bg or fg) * then execute it immediately. Otherwise, fork a child process and * run the job in the context of the child. If the job is running in * the foreground, wait for it to terminate and then return. Note: * each child process must have a unique process group ID so that our * background children don't receive SIGINT (SIGTSTP) from the kernel * when we type ctrl-c (ctrl-z) at the keyboard. */ void eval(char *cmdline) { char *argv[MAXARGS]; char buf[MAXLINE]; int bg; sigset_t mask; pid_t pid; strcpy(buf, cmdline); bg = parseline(buf, argv); if(argv[0] == NULL) return; if(!builtin_cmd(argv)){ //block signal if(sigemptyset(&mask) < 0) unix_error("sigemptyset error"); if(sigaddset(&mask, SIGCHLD) < 0) unix_error("sigaddset error"); if(sigprocmask(SIG_BLOCK, &mask, NULL) < 0) unix_error("sigprocmask error"); if((pid = fork()) == 0){ //set pid group to be the same as the current pid if(setpgid(0,0) < 0) unix_error("eval: set pid group error"); //unblock siganl if(sigprocmask(SIG_UNBLOCK, &mask, NULL) < 0) unix_error("sigprocmask error"); //execute program if(execve(argv[0], argv, environ)<0){ printf("%s: Command not found.\n", argv[0]); exit(1); } } if(!bg){ addjob(jobs, pid, FG, cmdline); //unblock siganl if(sigprocmask(SIG_UNBLOCK, &mask, NULL) < 0) unix_error("sigprocmask error"); waitfg(pid); }else{ addjob(jobs, pid, BG, cmdline); //unblock siganl if(sigprocmask(SIG_UNBLOCK, &mask, NULL) < 0) unix_error("sigprocmask error"); printf("[%d] (%d) %s", pid2jid(pid), pid, cmdline); } } return; }
void iniLoader::loadIniDoc(const string& filename, iniDoc& doc){ loadingDoc = &doc; ifstream iniFile(filename, ifstream::in); string line; int i = 1; while(getline(iniFile,line)){ parseline(line, i++); } }
/* * eval - Evaluate the command line that the user has just typed in * * If the user has requested a built-in command (quit, jobs, bg or fg) * then execute it immediately. Otherwise, fork a child process and * run the job in the context of the child. If the job is running in * the foreground, wait for it to terminate and then return. Note: * each child process must have a unique process group ID so that our * background children don't receive SIGINT (SIGTSTP) from the kernel * when we type ctrl-c (ctrl-z) at the keyboard. */ void eval(char *cmdline) { char * argv[MAXARGS]; int bg = parseline(cmdline, argv); int rbic = builtin_cmd(argv); if (!rbic) { pid_t child = fork(); //printf("forked\n"); if (child) { //PARENT if (bg) { int ret = addjob(jobs, child, BG, cmdline); //returns an int if (ret) { int jid = pid2jid(child); printf("[%d] (%d) %s", jobs[jid-1].jid, jobs[jid-1].pid, jobs[jid-1].cmdline); } else { printf("addjobfailed\n"); } } else { int ret = addjob(jobs, child, FG, cmdline); //returns an int if (!ret) { printf("addjobfailed\n"); } waitfg(child); } } else if( child < 0 ) { //ERROR exit(1); } else { //CHILDS setpgid( 0, 0 ); int rc = execv( argv[0], argv ); if (rc == -1) { printf( "execv error %d\n", errno); exit(1); } } } return; }
void parse(FILE *source_file, char *filename, LIST *mapping) { int linenum = 0; char line[LINE_LENGTH + 1]; while(fgets(line, LINE_LENGTH, source_file) != NULL) { parseline(line, ++linenum, filename, mapping); } }
/* eval - Evaluate a command line */ void eval(char *cmdline) { char *argv[MAXARGS]; /* Argument list execve() */ char buf[MAXLINE]; /* Holds modified command line */ int bg; /* Should the job run in bg or fg? */ pid_t pid; /* Process id */ signal(SIGCHLD, reaperthegrim); strcpy(buf, cmdline); bg = parseline(buf, argv); //bg = p3parseline(buf, argv); /* call new parseline function for cs485 project 3 */ if (argv[0] == NULL) return; /* Ignore empty lines */ if (!builtin_command(argv)) { if ((pid = fork()) == 0) { /* Child runs user job */ if (execve(argv[0], argv, environ) < 0) { printf("%s: Command not found.\n", argv[0]); exit(0); } } /* Parent waits for foreground job to terminate */ if (!bg) { int status; if (waitpid(pid, &status, 0) < 0) unix_error("waitfg: waitpid error"); } else{ printf("%d %s", pid, cmdline); int i =0; int ctr = 1; while(ctr==1) { //puts the pid into the PIDS array so we can keep track of all pids if(PIDS[i]==0) { PIDS[i]=pid; ctr=0; } else { i++; } } } } in = 0; out = 0; inpos = 0; outpos = 0; return; }
/* ----------------------------------------------------------------- */ int main(void) { char cmdline[MAXLINE]; char *argv[MAXARGS]; int argc; int status; pid_t pid; /* Loop forever to wait and process commands */ while (1) { /* Step 1: Name your shell: csc60mshell - m for mini shell */ printf("miniShell> "); fgets(cmdline, MAXLINE, stdin); argc = parseline(cmdline, argv); // call parseline to parse commands and options if (argv[0] == NULL) continue; // blank entry just gets ignored else if (strcmp("exit", argv[0] ) == 0) exit(0); // exits the mini shell built in commands else if (strcmp("cd", argv[0] ) == 0) { if (argv[1] == NULL) { chdir(getenv("HOME")); // cd chdir with no arguments defaults to home setenv("PWD", getenv("HOME"), 1); // updates the PWD directory variable too } else if (argv[1]!= NULL) { if (chdir(argv[1]) != 0) // cd dir to desired path perror(argv[1]); // if not sucessful print error setenv("PWD", argv[1], 1); // updates the PWD directory variable too } } else if (strcmp("pwd", argv[0] ) == 0) printf("%s\n", getenv("PWD")); // print current working directory else { pid = fork(); if (pid == -1) perror("Shell Program fork error"); else if (pid == 0) /* I am child process. I will execute the command, call: execvp */ process_input(argc, argv); else { /* I am parent process */ if (wait(&status) == -1) perror("Shell Program error"); else printf("Child returned status: %d\n",status); } } } // end while return 0; } // end main
CheckValidateUsfm::CheckValidateUsfm(const ustring & project, const vector < unsigned int >&books, bool gui, bool checksheet) /* It performs checks related to the USFM standard. project: project to check. books: books to check; if empty it checks them all. gui: whether to show graphical progressbar. checksheet: check whether markers are in the stylesheet of the project. */ { // Init variables. cancelled = false; mychecksheet = checksheet; // Get a list of the books to check. If no books were given, take them all. vector < unsigned int >mybooks(books.begin(), books.end()); if (mybooks.empty()) mybooks = project_get_books(project); // Get all styles in the attached stylesheet. vector <ustring> styless = stylesheet_get_markers(stylesheet_get_actual (), NULL); for (unsigned int i = 0; i < styless.size(); i++) styles.insert(styless[i]); // GUI. progresswindow = NULL; if (gui) { progresswindow = new ProgressWindow(_("Validating markers"), true); progresswindow->set_iterate(0, 1, mybooks.size()); } // Check each book. for (unsigned int bk = 0; bk < mybooks.size(); bk++) { if (gui) { progresswindow->iterate(); progresswindow->set_text(books_id_to_english(book)); if (progresswindow->cancel) { cancelled = true; return; } } book = mybooks[bk]; // Check each chapter. vector <unsigned int> chapters = project_get_chapters(project, book); for (unsigned int ch = 0; ch < chapters.size(); ch++) { chapter = chapters[ch]; vector <ustring> verses = project_get_verses(project, book, chapter); // Check each verse. for (unsigned int vs = 0; vs < verses.size(); vs++) { verse = verses[vs]; ustring line = project_retrieve_verse(project, book, chapter, verse); // Check each line. ParseLine parseline(line); for (unsigned int ln = 0; ln < parseline.lines.size(); ln++) { check(parseline.lines[ln]); } } } } }
/* * eval - Evaluate the command line that the user has just typed in * * If the user has requested a built-in command (quit, jobs, bg or fg) * then execute it immediately. Otherwise, fork a child process and * run the job in the context of the child. If the job is running in * the foreground, wait for it to terminate and then return. Note: * each child process must have a unique process group ID so that our * background children don't receive SIGINT (SIGTSTP) from the kernel * when we type ctrl-c (ctrl-z) at the keyboard. */ void eval(char *cmdline) { int bg; /* should the job run in bg or fg? */ struct cmdline_tokens tok; sigset_t prev, mask_all; int state; pid_t pid; struct job_t *job_ins; /* Parse command line */ bg = parseline(cmdline, &tok); if (bg == -1) /* parsing error */ return; if (tok.argv[0] == NULL) /* ignore empty lines */ return; /* Fig. 8.40 */ if (!builtin_command(&tok)) { Sigemptyset(&mask_all); Sigaddset(&mask_all, SIGCHLD); Sigaddset(&mask_all, SIGINT); Sigaddset(&mask_all, SIGTSTP); Sigprocmask(SIG_BLOCK, &mask_all, &prev); /* Block at beginning */ if ((pid = Fork()) == 0) { /* Child process */ Sigprocmask(SIG_SETMASK, &prev, NULL); /* Unblock inside child */ Setpgid(0, 0); /* one proc and shell in pgrp */ open_dup2_in(tok.infile); open_dup2_out(tok.outfile); if (execve(tok.argv[0], tok.argv, environ) < 0) { printf("%s: Command not found.\n", tok.argv[0]); exit(0); } } /* Parent process */ state = bg ? BG : FG; addjob(job_list, pid, state, cmdline); job_ins = getjobpid(job_list, pid); Sigprocmask(SIG_SETMASK, &prev, NULL); /* Unblock 3 signals */ if (bg) { printf("[%d] (%d) %s", pid2jid(pid), pid, cmdline); } else { wait_fg_job(job_ins -> state); } } return; }
void eval(char *cmdline) { char *argv[MAXARGS]; // argv pour execve() char buf[MAXLINE]; // contient ligne commande modifiee int bg; // arriere-plan ou premier plan ? pid_t pid; // process id int indexJobLibre; strcpy(buf, cmdline); bg = parseline(buf, argv); if (argv[0] == NULL) return; // ignorer lignes vides if (!builtin_command(argv)) { // commande integree ? // si oui, executee directement if ((pid = Fork()) == 0) { // si non, executee par un fils setpgid(0, 0); if (execvp(argv[0], argv) < 0) { printf("%s: Command not found.\n", argv[0]); exit(0); } } indexJobLibre = getIndexLibre(); tabJobs[indexJobLibre] = malloc(sizeof (job)); tabJobs[indexJobLibre]->pid = pid; strcpy(tabJobs[indexJobLibre]->commande, argv[0]); if (!bg) { // le pere attend fin du travail de premier plan tabJobs[indexJobLibre]->etat = FG; printf("dans !bg: commande = %s\n", tabJobs[indexJobLibre]->commande); printf("getIndexFG() = %d --\n", getIndexFG()); } else { // travail d'arriere-plan, on imprime le pid tabJobs[indexJobLibre]->etat = BG; printf("%d %s", pid, cmdline); } } while (getIndexFG() != -1) { printf("verif FG = %d\n", getIndexFG()); afficheJobs(); sleep(1); } return; }
int main(int argc, char **argv) { int line, tokens; char buffer[BUFSIZ], *inputline, *token[GV_MAXARGS]; /* Load the module if necessary. */ if (modfind(GVINUMMOD) < 0) { if (kldload(GVINUMKLD) < 0 && modfind(GVINUMMOD) < 0) err(1, GVINUMKLD ": Kernel module not available"); } /* Arguments given on the command line. */ if (argc > 1) { argc--; argv++; parseline(argc, argv); /* Interactive mode. */ } else { for (;;) { inputline = readline("gvinum -> "); if (inputline == NULL) { if (ferror(stdin)) { err(1, "can't read input"); } else { printf("\n"); exit(0); } } else if (*inputline) { add_history(inputline); strcpy(buffer, inputline); free(inputline); line++; /* count the lines */ tokens = gv_tokenize(buffer, token, GV_MAXARGS); if (tokens) parseline(tokens, token); } } } exit(0); }
/* * eval - Evaluate the command line that the user has just typed in * * If the user has requested a built-in command (quit, jobs, bg or fg) * then execute it immediately. Otherwise, fork a child process and * run the job in the context of the child. If the job is running in * the foreground, wait for it to terminate and then return. Note: * each child process must have a unique process group ID so that our * background children don't receive SIGINT (SIGTSTP) from the kernel * when we type ctrl-c (ctrl-z) at the keyboard. */ void eval(char *cmdline) { // Get memory for our parsed input char temp[MAXLINE]; char** argv = (char**)temp; // Return if only newline if (strcmp(cmdline, "\n") == 0) { return; } // Parse the input int background = parseline(cmdline, argv); // Check if built in command if (builtin_cmd(argv) == 0) { // Not a built in command char* programName = argv[0]; // Block sigchild signal sigset_t mask; Sigemptyset(&mask); Sigaddset(&mask, SIGCHLD); Sigprocmask(SIG_BLOCK, &mask, NULL); // Create a child process pid_t pid = Fork(); // Unblock sichild signal Sigprocmask(SIG_UNBLOCK, &mask, NULL); if (pid == 0) { // Child // Assign it a new process group id setpgid(0,0); // Run the external program Execve(programName, argv, environ); } else { // Parent if (background == 0) { // Add it to the list addjob(jobs, pid, FG, cmdline); // Make the shell wait for the process in the fg waitfg(pid); } else { // Add it to the list addjob(jobs, pid, BG, cmdline); // Get the job id int jid = pid2jid(pid); // Print info on background job printf("[%d] (%d) %s", jid, pid, cmdline); } } } return; }
void eval(char *cmdline,task_struct* head) { pid_t pid; task_struct* node; int bg; char *arg[MAXARGC]; char buf[MAXBUF]; strcpy(buf,cmdline); bg = parseline(buf,arg); if(!quit(arg)) { if(fgbg(head,arg) != 0) { return ; } if(printPid(arg) != 0) { return; } pid = Fork(); if(pid != 0) { node = (task_struct*)Malloc(sizeof(task_struct)); taskop(arg[0],pid,bg,node); add_tail_to_task(head,node); } if(pid == 0) { if(execve(arg[0],arg,environ) < 0) { printf("%s: command not found\n",arg[0]); exit(8); } } if(!bg) { int status; if(waitpid(pid,&status,0) < 0) unix_error("waitpid error"); if(WIFEXITED(status)) { printf("%d is removed\n",node->pid); delete_node(node); } } else { printf("%d %s\n",pid,cmdline); } } return ; }
void eval(char *cmdline) { char buf[MAXLINE]; char *argv[MAXARGS]; int bg; int i; struct job *p; pid_t pid, tcpid; strcpy(buf, cmdline); bg = parseline(buf, argv); if (argv[0] == NULL) return; if (!builtin_cmd(argv)) { child_block(); if ((pid = Fork()) == 0) { child_unblock(); control_unblock(); if (signal(SIGCHLD, SIG_DFL) == SIG_ERR) { printf("signal error\n"); exit(-1); } if (execve(argv[0], argv, environ) < 0) { printf("%s : Command not found.\n", argv[0]); exit(0); } } addjob(pid, cmdline); if (bg) { setpgid(pid, pid); p = selectjob(pid); printf("[%d] %d ", p->jid, p->pid); for (i = 0; p->argv[i] != NULL; i++) printf("%s ", p->argv[i]); printf("\n"); } else { forepid = pid; child_unblock(); printf("ready to fg_waitpid\n"); if (!sigsetjmp(env, 0)) { while (1) sleep(50); printf("u never see me\n"); } else printf("no fg_waitpid\n"); return; } child_unblock(); } return; }
/* * eval - Evaluate the command line that the user has just typed in * * If the user has requested a built-in command (quit, jobs, bg or fg) * then execute it immediately. Otherwise, fork a child process and * run the job in the context of the child. If the job is running in * the foreground, wait for it to terminate and then return. Note: * each child process must have a unique process group ID so that our * background children don't receive SIGINT (SIGTSTP) from the kernel * when we type ctrl-c (ctrl-z) at the keyboard. */ void eval(char *cmdline) { char *argv[MAXARGS]; //int to record for bg int bg; pid_t pid; sigset_t mask; // parse the line bg = parseline(cmdline, argv); //check if valid builtin_cmd if(!builtin_cmd(argv)) { // blocking first sigemptyset(&mask); sigaddset(&mask, SIGCHLD); sigprocmask(SIG_BLOCK, &mask, NULL); // forking if((pid = fork()) < 0){ unix_error("forking error"); } // child else if(pid == 0) { sigprocmask(SIG_UNBLOCK, &mask, NULL); setpgid(0, 0); //check if command is there if(execvp(argv[0], argv) < 0) { printf("%s: Command not found\n", argv[0]); exit(1); } } // parent add job first else { if(!bg){ addjob(jobs, pid, FG, cmdline); } else { addjob(jobs, pid, BG, cmdline); } sigprocmask(SIG_UNBLOCK, &mask, NULL); //if bg/fg if (!bg){ //wait for fg waitfg(pid); } else { //print for bg printf("[%d] (%d) %s", pid2jid(pid), pid, cmdline); } } } }
/* * eval - Evaluate the command line that the user has just typed in * * If the user has requested a built-in command (quit, jobs, bg or fg) * then execute it immediately. Otherwise, fork a child process and * run the job in the context of the child. If the job is running in * the foreground, wait for it to terminate and then return. Note: * each child process must have a unique process group ID so that our * background children don't receive SIGINT (SIGTSTP) from the kernel * when we type ctrl-c (ctrl-z) at the keyboard. */ void eval(char *cmdline) { char *argv[MAXARGS]; //args for execvp pid_t pid; sigset_t sSet; int stat; if (*cmdline == 10) return; else{ stat= parseline(cmdline, argv); if (argv[0] == NULL) return; /* ignore empty lines */ if(!builtin_cmd(argv)){ //If it is not a user command sigemptyset(&sSet); sigaddset(&sSet,SIGCHLD); sigprocmask(SIG_BLOCK, &sSet,NULL); if((pid = fork()) == 0) { /*Child runs user job*/ setpgrp(); sigprocmask(SIG_UNBLOCK, &sSet,NULL); /*Unblocked after setpgrp() because childpid should be set as group pid first without any interruption */ int stat1=execvp(argv[0], argv); //prints a message if execvp fails. if(stat1 < 0) { //If the command doesnot exists stat1=0; printf("%s: Command not found.\n",argv[0]);//prints a message if execvp fails. exit(0); /*In case execvp fails then exit will get called.*/ } } if(stat) { addjob(jobs, pid, BG, cmdline); /* request for background job then job is added with BG state*/ sigprocmask(SIG_UNBLOCK,&sSet,NULL); /*unblocks signal after adding all jobs without any type of signal*/ printf("[%d] (%d) %s",pid2jid(pid),pid,cmdline); } else { addjob(jobs, pid, FG, cmdline) ; sigprocmask(SIG_UNBLOCK,&sSet,NULL);/*unblocks signal after adding all jobs without any type of signal*/ waitfg(pid);/*parent waits*/ } } } return; }
static void process(void) { blankmax = blankcount = 1000; for (;;) { Linetype lineval = parseline(); trans_table[ifstate[depth]][lineval](); debug("process line %d %s -> %s depth %d", linenum, linetype_name[lineval], ifstate_name[ifstate[depth]], depth); } }
int main() { char line[MAXLINE]; path *pth = createpath(""); while (1) { programlist *pgl; if (printprompt() == -1) { fprintf(stderr, "Getting current directory failed.\n"); } fgets(line, MAXLINE, stdin); pgl = parseline(line, pth); int child; if(handleexit(pgl) == 0){ cleanuppgl(pgl); exit(0); } if (handlechdir(pgl) <= 0){ cleanuppgl(pgl); continue; } else if (handlepath(pgl, pth) <= 0) { cleanuppgl(pgl); continue; } child = fork(); if (child == 0) { execline(pgl, pth); /*printf("executed line\n"); */ cleanuppgl(pgl); exit(0); } else { int status; int waitret; while (1) { waitret = wait(&status); /* printf("wait ret = %d\n", waitret); */ if (waitret == -1 && errno == ECHILD) { break; } else if (waitret == -1 && errno != ECHILD) { fprintf(stderr, "Wait failed\n"); break; } } cleanuppgl(pgl); } } return 0; }
struct cmd* parseline(char** ps, char* es) { struct cmd* cmd = parsepipe(ps, es); while (peek(ps, es, "&")) { gettoken(ps, es, 0, 0); cmd = backcmd(cmd); } if (peek(ps, es, ";")) { gettoken(ps, es, 0, 0); cmd = listcmd(cmd, parseline(ps, es)); } return cmd; }
static size_t parseheader(void *contents, size_t size, size_t nmemb, void *userp) { size_t realsize = size * nmemb; struct HeaderStruct *mem = (struct HeaderStruct *)userp; mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1); if (mem->memory == NULL) { Debug("not enough memory (realloc returned NULL)\n"); exit(EXIT_FAILURE); } memcpy(&(mem->memory[mem->size]), contents, realsize); mem->memory[mem->size+=realsize] = 0; return parseline(mem, realsize); }
struct TIC_POP *tic_GetPOP(struct TIC_conf *tic, const char *sId) { char buf[1024]; struct TIC_POP *pop; /* Get a Tunnel */ sock_printf(tic->sock, "pop show %s\n", sId); /* Fetch the answer */ if (sock_getline(tic->sock, tic_buf, sizeof(tic_buf), &tic_filled, buf, sizeof(buf)) == -1) { return NULL; } /* 201 (start of info) ? */ if (buf[0] != '2' || buf[1] != '0' || buf[2] != '1') { dolog(LOG_ERR, "Couldn't show POP %s: %s.\n", sId, buf); return NULL; } /* Allocate a new struct */ pop = (struct TIC_POP *)malloc(sizeof(*pop)); if (!pop) { dolog(LOG_ERR, "Memory problem while getting POP\n"); return NULL; } memset(pop, 0, sizeof(*pop)); /* Gather the information */ while (sock_getline(tic->sock, tic_buf, sizeof(tic_buf), &tic_filled, buf, sizeof(buf)) != -1) { /* 202 (end of list) ? */ if (buf[0] == '2' && buf[1] == '0' && buf[2] == '2') break; parseline(buf, ": ", pop_rules, pop); } /* All went okay? */ if (buf[0] == '2' && buf[1] == '0' && buf[2] == '2') { dolog(LOG_INFO, "Succesfully retrieved POP information for %s\n", sId); return pop; } /* Free the structure, it is broken anyway */ tic_Free_POP(pop); dolog(LOG_ERR, "POP Get for %s went wrong: %s\n", sId, buf); return NULL; }
void parsefile(FILE *f, list *instructions, list *labels) { list *lines = list_create(); list_node *n; readfile(f, lines); for (n = list_get_root(lines); n != NULL; n = n->next) { cur_pos = cur_line = n->data; curline++; parseline(instructions, labels); } list_dispose(&lines, &free); }
struct cmd* parseblock(char **ps, char *es) { struct cmd *cmd; if(!peek(ps, es, "(")) panic("parseblock"); gettoken(ps, es, 0, 0); cmd = parseline(ps, es); if(!peek(ps, es, ")")) panic("syntax - missing )"); gettoken(ps, es, 0, 0); cmd = parseredirs(cmd, ps, es); return cmd; }