From 203ee503203b9c1710b38de3a79422a2cebdc5fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Aur=C3=A9lio=20Graciotto=20Silva?= Date: Mon, 11 May 2015 17:08:57 -0300 Subject: [PATCH] Remove some global variables. --- lib/disco.c | 2 +- tcase/lib/playinput.c | 61 ++++++++---------------- tcase/lib/tcase_ex.c | 105 +++++++++++++++++++++--------------------- 3 files changed, 73 insertions(+), 95 deletions(-) diff --git a/lib/disco.c b/lib/disco.c index 7af3234..c2173be 100644 --- a/lib/disco.c +++ b/lib/disco.c @@ -276,7 +276,7 @@ gravarq(FILE *fp, char buf[], int len) { int i = fwrite(buf, 1, len, fp); if (i != len) { - fprintf(stderr, "Error saving data to file %s.%s (%d from %d bytes)", nomearq(fp), extarq(fp), i, len); + fprintf(stderr, "Error saving data to file %s%s (%d from %d bytes)", nomearq(fp), extarq(fp), i, len); return ERRO; } return OK; diff --git a/tcase/lib/playinput.c b/tcase/lib/playinput.c index eb8bb44..8a56726 100644 --- a/tcase/lib/playinput.c +++ b/tcase/lib/playinput.c @@ -172,22 +172,18 @@ pty_fork(int *ptrfdm, int pipe_out[], int pipe_err[]) } } -int -playinput(int *ret_code, int input, int (*tr_in)(),int (*tr_out)(), - int (*tr_err)(), long vtimer, long rtimer, - char *prog, char *argv[]) +int playinput(int *ret_code, FILE *fp_in, int (*tr_in)(), FILE *fp_out, int (*tr_out)(), FILE *fp_err, int (*tr_err)(), long vtimer, char *prog, char *argv[]) { -int i, c, bytes, ret1, k, j; -struct itimerval t1; -long tt2, tt1; -struct rlimit resource_limit; -int fim_le; -int pipe_out[2], pipe_err[2], fdm; -pid_t pid, pipe_fork(); + int i, c, bytes, ret1, k, j; + struct itimerval t1; + long tt2, tt1; + struct rlimit resource_limit; + int fim_le; + int pipe_out[2], pipe_err[2], fdm; + pid_t pid, pipe_fork(); - - pid = pty_fork(&fdm, pipe_out, pipe_err); + pid = pty_fork(&fdm, pipe_out, pipe_err); if (pid < 0) return ERRO; @@ -204,14 +200,6 @@ pid_t pid, pipe_fork(); signal(SIGVTALRM, SIG_DFL); setitimer(ITIMER_VIRTUAL, &t1, NULL); } - if (rtimer != 0) - { - tick2timeval(rtimer, &t1.it_value); - t1.it_interval.tv_sec = t1.it_value.tv_sec ; - t1.it_interval.tv_usec = t1.it_value.tv_usec; - signal(SIGALRM, SIG_DFL); - setitimer(ITIMER_REAL, &t1, NULL); - } execv(prog,argv ); return ERRO; } @@ -228,7 +216,7 @@ pid_t pid, pipe_fork(); bytes = read(pipe_out[0],buff,BUFFSIZE); else bytes = 0; - if (tr_out != NULL && (k = tr_out(buff, bytes,pid)) != bytes) + if (tr_out != NULL && (k = tr_out(fp_out, buff, bytes,pid)) != bytes) { if (k != ERRO) k = OK; goto fim; @@ -240,7 +228,7 @@ pid_t pid, pipe_fork(); bytes = read(pipe_err[0],buff,BUFFSIZE); else bytes = 0; - if (tr_err != NULL && (k = tr_err(buff, bytes, pid)) != bytes) + if (tr_err != NULL && (k = tr_err(fp_err, buff, bytes, pid)) != bytes) { if (k != ERRO) k = OK; goto fim; @@ -275,23 +263,14 @@ pid_t pid, pipe_fork(); } } - /* sometimes, itimer doesn't work then this is a - guarantee for aborting looping mutants */ - tt2 = gettimedad() - tt1; - if (rtimer != 0 && tt2 > rtimer) - { - kill(pid, SIGKILL); - k = 1; c = -1; - goto fim; - } - } + } do { if (ioctl(pipe_out[0],FIONREAD,&bytes) != -1 && bytes > 0) bytes = read(pipe_out[0],buff,BUFFSIZE); else bytes = 0; - if (tr_out != NULL && (k =tr_out(buff, bytes, pid)) != bytes) + if (tr_out != NULL && (k =tr_out(fp_out, buff, bytes, pid)) != bytes) { if (k != ERRO) k = OK; goto fim; @@ -304,7 +283,7 @@ pid_t pid, pipe_fork(); bytes = read(pipe_err[0],buff,BUFFSIZE); else bytes = 0; - if (tr_err != NULL && (k = tr_err(buff, bytes, pid))!=bytes) + if (tr_err != NULL && (k = tr_err(fp_err, buff, bytes, pid))!=bytes) { if (k != ERRO) k = OK; goto fim; @@ -391,7 +370,7 @@ pipe_fork(int pipe_in[], int pipe_out[], int pipe_err[]) /** * @param tr_out Handler function for data read from stdin. Its parameters are: byte buffer, size of byte buffer, pid of child process. */ -int playbatch(int *ret_code, int input, int (*tr_in)(),int (*tr_out)(), int (*tr_err)(), long vtimer, char *prog, char *argv[]) +int playbatch(int *ret_code, FILE *fp_in, int (*tr_in)(), FILE *fp_out, int (*tr_out)(), FILE *fp_err, int (*tr_err)(), long vtimer, char *prog, char *argv[]) { int pid; int pipe_out[2], pipe_err[2], pipe_in[2]; @@ -440,7 +419,7 @@ int playbatch(int *ret_code, int input, int (*tr_in)(),int (*tr_out)(), int (*tr } // Handle data if (tr_out != NULL) { - result = tr_out(buff, bytes, pid); + result = tr_out(fp_out, buff, bytes, pid); if (result == ERRO || result != bytes) { result = ERRO; goto fim; @@ -460,7 +439,7 @@ int playbatch(int *ret_code, int input, int (*tr_in)(),int (*tr_out)(), int (*tr } // Handle data if (tr_err != NULL) { - result = tr_err(buff, bytes, pid); + result = tr_err(fp_err, buff, bytes, pid); if (result == ERRO || result != bytes) { result == ERRO; goto fim; @@ -468,7 +447,7 @@ int playbatch(int *ret_code, int input, int (*tr_in)(),int (*tr_out)(), int (*tr } if ( ! fim_le) { - bytes = tr_in(buff, BUFFSIZE); + bytes = tr_in(fp_in, buff, BUFFSIZE); if (bytes < 0) { result = ERRO; goto fim; @@ -504,7 +483,7 @@ int playbatch(int *ret_code, int input, int (*tr_in)(),int (*tr_out)(), int (*tr } // Handle data if (tr_out != NULL) { - result = tr_out(buff, bytes, pid); + result = tr_out(fp_out, buff, bytes, pid); if (result == ERRO || result != bytes) { result = ERRO; goto fim; @@ -527,7 +506,7 @@ int playbatch(int *ret_code, int input, int (*tr_in)(),int (*tr_out)(), int (*tr } // Handle data if (tr_err != NULL) { - result = tr_err(buff, bytes, pid); + result = tr_err(fp_err, buff, bytes, pid); if (result == ERRO || result != bytes) { result == ERRO; goto fim; diff --git a/tcase/lib/tcase_ex.c b/tcase/lib/tcase_ex.c index dd1e812..109c7f7 100644 --- a/tcase/lib/tcase_ex.c +++ b/tcase/lib/tcase_ex.c @@ -77,50 +77,45 @@ tcase_ex_bat(char *dir, char *test, char *direx, char *exec, char *param) } -FILE *fpin, *fpout, *fperr; - -int le_input(char *buff, int n) +int le_input(FILE *file, char *buff, int n) { -int nread; - - nread = fread( buff, 1, n, fpin); - if (nread <= 0) - { - if ( ferror(fpin)) - { - msg("Error reading input file"); - return ERRO; + int nread; + nread = fread( buff, 1, n, file); + if (nread <= 0) { + if (ferror(file)) { + msg("Error reading input file"); + return ERRO; + } } - if (feof(fpin)) - return 0; - } - return nread; + if (feof(file)) { + return 0; + } else { + return nread; + } } -int trata_out(char *buff, int n, pid_t pid) +int trata_out(FILE *file, char *buff, int n, pid_t pid) { -int nread; - nread = gravarq(fpout, buff, n); - if (nread != OK) - return ERRO; - return n; + int nread; + nread = gravarq(file, buff, n); + if (nread != OK) { + return ERRO; + } + return n; } -int trata_err(char *buff, int n, pid_t pid) +int trata_err(FILE *file, char *buff, int n, pid_t pid) { -int nread; - nread = gravarq(fperr, buff, n); - if (nread != OK) - return ERRO; - return n; + int nread; + nread = gravarq(file, buff, n); + if (nread != OK) { + return ERRO; + } + return n; } - -exec_from_ascii(direx, exec, instrum, param, dir, teste, - reg, timeout, use_instrum, shel, interactive) -char *exec, *instrum, *param, *direx, *dir, *teste, *shel; -REG_TCASE *reg; -int timeout, use_instrum, interactive; +int +exec_from_ascii(char *direx, char *exec, char *instrum, char *param, char *dir, char *teste, REG_TCASE *reg, int timeout, int use_instrum, char *shel, int interactive) { long t1, tt1, t2, tt2; int xret_cod, ret_cod, k; @@ -131,6 +126,9 @@ char *p; char inst_exec_fullname[PATH_MAX]; char test_fullname[PATH_MAX]; char **argv; + FILE *fpin, *fpout, *fperr; + + if (tty_save(STDIN_FILENO) == ERRO) return ERRO; @@ -138,23 +136,24 @@ char *p; t1 = gettimechild(); tt1 = times(&xtms); - fpin = abrearq(dir, teste, SUFIXO_INPUT, 0); - if (fpin == NULL) - return ERRO; - fpout = criarq(dir, teste, SUFIXO_OUTPUT); - if (fpout == NULL) - { - fecharq(fpin); - return ERRO; - } - fperr = criarq(dir, teste, SUFIXO_ERRO); - if (fperr == NULL) - { - fecharq(fpin); - fecharq(fpout); - return ERRO; - } - monta_nome(exec_fullname, direx, exec, ""); + fpin = abrearq(dir, teste, SUFIXO_INPUT, 0); + if (fpin == NULL) { + return ERRO; + } + + fpout = criarq(dir, teste, SUFIXO_OUTPUT); + if (fpout == NULL) { + fecharq(fpin); + return ERRO; + } + + fperr = criarq(dir, teste, SUFIXO_ERRO); + if (fperr == NULL) { + fecharq(fpin); + fecharq(fpout); + return ERRO; + } + monta_nome(exec_fullname, direx, exec, ""); if (use_instrum) { monta_nome(inst_exec_fullname, direx, instrum, ""); @@ -167,10 +166,10 @@ char *p; if (interactive) { /* aloca_master(); */ - k = playinput(&ret_cod, fileno(fpin), le_input, trata_out, trata_err, timeout*100, timeout*100, p, argv); + k = playinput(&ret_cod, fpin, le_input, fpout, trata_out, fperr, trata_err, timeout*100, p, argv); /* libera_master(); */ } else { - k = playbatch(&ret_cod, fileno(fpin), le_input, trata_out, trata_err, timeout*100, p, argv); + k = playbatch(&ret_cod, fpin, le_input, fpout, trata_out, fperr, trata_err, timeout*100, p, argv); } free_array_str(argv);