w3m

Unnamed repository; edit this file to name it for gitweb.
git clone https://logand.com/git/w3m.git/
Log | Files | Refs | README

commit 183254943602f097c23e36e90aeaa670dd212241
parent 11a54a9f801384ee38c5e02854327c0a6982f46a
Author: ukai <ukai>
Date:   Wed, 22 Jan 2003 16:10:27 +0000

[w3m-dev 03676] cleanup for pipe
* etc.c (open_pipe_rw): added
* file.c (uncompress_stream): rewrite using open_pipe_rw
* image.c (openImgdisplay): rewrite using open_pipe_rw
* local.c (localcgi_popen_rw): deleted
	(localcgi_post): rewrite using open_pipe_rw
* proto.h (open_pipe_rw): added
* search.c (migemor): initialized to NULL
	(migemow): initialized to NULL
	(migemo_pid): initialized to 0
	(open_migemo): rewrite using open_pipe_rw
From: Hironori SAKAMOTO <hsaka@mth.biglobe.ne.jp>

Diffstat:
MChangeLog | 14++++++++++++++
Metc.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Mfile.c | 106+++++++++++++++++++++++++++++++------------------------------------------------
Mimage.c | 30+++---------------------------
Mlocal.c | 65++++++++---------------------------------------------------------
Mproto.h | 1+
Msearch.c | 33+++++----------------------------
7 files changed, 124 insertions(+), 177 deletions(-)

diff --git a/ChangeLog b/ChangeLog @@ -1,5 +1,19 @@ 2003-01-23 Hironori SAKAMOTO <hsaka@mth.biglobe.ne.jp> + * [w3m-dev 03676] cleanup for pipe + * etc.c (open_pipe_rw): added + * file.c (uncompress_stream): rewrite using open_pipe_rw + * image.c (openImgdisplay): rewrite using open_pipe_rw + * local.c (localcgi_popen_rw): deleted + (localcgi_post): rewrite using open_pipe_rw + * proto.h (open_pipe_rw): added + * search.c (migemor): initialized to NULL + (migemow): initialized to NULL + (migemo_pid): initialized to 0 + (open_migemo): rewrite using open_pipe_rw + +2003-01-23 Hironori SAKAMOTO <hsaka@mth.biglobe.ne.jp> + * [w3m-dev 03675] x-face * file.c (xface2xpm): use getImage() (readHeader): check same file diff --git a/etc.c b/etc.c @@ -1339,6 +1339,58 @@ setup_child(int child, int i, int f) fmInitialized = FALSE; } +pid_t +open_pipe_rw(FILE **fr, FILE **fw) +{ + int fdr[2]; + int fdw[2]; + pid_t pid; + + if (fr && pipe(fdr) < 0) + goto err0; + if (fw && pipe(fdw) < 0) + goto err1; + + flush_tty(); + pid = fork(); + if (pid < 0) + goto err2; + if (pid == 0) { + /* child */ + if (fr) { + close(fdr[0]); + dup2(fdr[1], 1); + } + if (fw) { + close(fdw[1]); + dup2(fdw[0], 0); + } + } + else { + if (fr) { + close(fdr[1]); + *fr = fdopen(fdr[0], "r"); + } + if (fw) { + close(fdw[0]); + *fw = fdopen(fdw[1], "w"); + } + } + return pid; + err2: + if (fw) { + close(fdw[0]); + close(fdw[1]); + } + err1: + if (fr) { + close(fdr[0]); + close(fdr[1]); + } + err0: + return (pid_t) -1; +} + void myExec(char *command) { diff --git a/file.c b/file.c @@ -7723,8 +7723,8 @@ inputAnswer(char *prompt) static void uncompress_stream(URLFile *uf, char **src) { - int pid1; - int fd1[2]; + pid_t pid1; + FILE *f1; char *expand_cmd = GUNZIP_CMDNAME; char *expand_name = GUNZIP_NAME; char *tmpf = NULL; @@ -7748,82 +7748,58 @@ uncompress_stream(URLFile *uf, char **src) } uf->compression = CMP_NOCOMPRESS; - if (pipe(fd1) < 0) { - UFclose(uf); - return; - } - if (uf->scheme != SCM_LOCAL #ifdef USE_IMAGE && !image_source #endif ) { tmpf = tmpfname(TMPF_DFL, ext)->ptr; - if (save2tmp(*uf, tmpf) < 0) { - UFclose(uf); - return; - } -#if 0 - if (uf->scheme != SCM_FTP) -#endif - UFclose(uf); - if (src) - *src = tmpf; - else - uf->scheme = SCM_LOCAL; } - flush_tty(); - /* fd1[0]: read, fd1[1]: write */ - if ((pid1 = fork()) == 0) { - close(fd1[0]); - dup2(fd1[1], 1); - dup2(fd1[1], 2); + pid1 = open_pipe_rw(&f1, NULL); + if (pid1 < 0) { + UFclose(uf); + return; + } + if (pid1 == 0) { + /* child */ + pid_t pid2; + FILE *f2; + dup2(1, 2); /* stderr>&stdout */ setup_child(TRUE, -1, UFfileno(uf)); - if (tmpf) { -#ifdef USE_BINMODE_STREAM - int tmpfd = open(tmpf, O_RDONLY | O_BINARY); -#else - int tmpfd = open(tmpf, O_RDONLY); -#endif - if (tmpfd < 0) { - close(fd1[1]); - exit(1); - } - dup2(tmpfd, 0); - } - else { - /* child */ - int pid2; - int fd2[2]; - if (pipe(fd2) < 0) { - close(fd1[1]); - UFclose(uf); - exit(1); - } - if ((pid2 = fork()) == 0) { - /* child */ - Str buf = Strnew_size(SAVE_BUF_SIZE); - close(fd2[0]); - while (UFread(uf, buf, SAVE_BUF_SIZE)) { - if (write(fd2[1], buf->ptr, buf->length) < 0) { - close(fd2[1]); - exit(0); - } - } - close(fd2[1]); - exit(0); + pid2 = open_pipe_rw(NULL, &f2); + if (pid2 < 0) { + UFclose(uf); + exit(1); + } + if (pid2 > 0) { + Str buf = Strnew_size(SAVE_BUF_SIZE); + FILE *f = NULL; + if (tmpf) + f = fopen(tmpf, "wb"); + while (UFread(uf, buf, SAVE_BUF_SIZE)) { + if (Strfputs(buf, f2) < 0) + break; + if (f) + Strfputs(buf, f); } - close(fd2[1]); - dup2(fd2[0], 0); + fclose(f2); + if (f) + fclose(f); + exit(0); } + /* child */ execlp(expand_cmd, expand_name, NULL); - exit(0); + exit(1); } - close(fd1[1]); - if (tmpf == NULL) - UFclose(uf); - uf->stream = newFileStream(fdopen(fd1[0], "rb"), (void (*)())pclose); + if (tmpf) { + if (src) + *src = tmpf; + else + uf->scheme = SCM_LOCAL; + } + UFhalfclose(uf); + uf->stream = newFileStream(f1, (void (*)())fclose); } static FILE * diff --git a/image.c b/image.c @@ -89,24 +89,12 @@ termImage() static int openImgdisplay() { - int fdr[2], fdw[2]; - char *cmd; - - if (pipe(fdr) < 0) - goto err0; - if (pipe(fdw) < 0) - goto err1; - - flush_tty(); - Imgdisplay_pid = fork(); + Imgdisplay_pid = open_pipe_rw(&Imgdisplay_rf, &Imgdisplay_wf); if (Imgdisplay_pid < 0) - goto err2; + goto err0; if (Imgdisplay_pid == 0) { /* child */ - close(fdr[0]); - close(fdw[1]); - dup2(fdw[0], 0); - dup2(fdr[1], 1); + char *cmd; setup_child(FALSE, 2, -1); set_environ("W3M_TTY", ttyname_tty()); if (!strchr(Imgdisplay, '/')) @@ -116,21 +104,9 @@ openImgdisplay() myExec(cmd); /* XXX: ifdef __EMX__, use start /f ? */ } - close(fdr[1]); - close(fdw[0]); - Imgdisplay_rf = fdopen(fdr[0], "r"); - Imgdisplay_wf = fdopen(fdw[1], "w"); activeImage = TRUE; return TRUE; - err2: - close(fdw[0]); - close(fdw[1]); - err1: - close(fdr[0]); - close(fdr[1]); err0: - Imgdisplay_rf = NULL; - Imgdisplay_wf = NULL; Imgdisplay_pid = 0; activeImage = FALSE; return FALSE; diff --git a/local.c b/local.c @@ -355,60 +355,10 @@ cgi_filename(char *fn, int *status) return fn; } -static pid_t -localcgi_popen_rw(int *p_fdr, int *p_fdw) -{ - int fdr[2], fdw[2]; - pid_t pid; - Str emsg; - - if (pipe(fdr) < 0) { - emsg = Sprintf("localcgi_popen_rw: pipe: %s", strerror(errno)); - goto pipe_err0; - } - if (p_fdw && pipe(fdw) < 0) { - emsg = Sprintf("localcgi_popen_rw: pipe: %s", strerror(errno)); - goto pipe_err1; - } - - flush_tty(); - if ((pid = fork()) < 0) { - emsg = Sprintf("localcgi_popen_rw: fork: %s", strerror(errno)); - goto pipe_err2; - } - else if (!pid) { - close(fdr[0]); - dup2(fdr[1], 1); - if (p_fdw) { - close(fdw[1]); - dup2(fdw[0], 0); - } - setup_child(TRUE, 2, -1); - } - else { - close(fdr[1]); - *p_fdr = fdr[0]; - if (p_fdw) { - close(fdw[0]); - *p_fdw = fdw[1]; - } - } - return pid; - pipe_err2: - close(fdw[0]); - close(fdw[1]); - pipe_err1: - close(fdr[0]); - close(fdr[1]); - pipe_err0: - disp_err_message(emsg->ptr, FALSE); - return (pid_t) - 1; -} - FILE * localcgi_post(char *uri, char *qstr, FormList *request, char *referer) { - int fdr, fdw = -1; + FILE *fr = NULL, *fw = NULL; int status; pid_t pid; char *file; @@ -418,18 +368,19 @@ localcgi_post(char *uri, char *qstr, FormList *request, char *referer) return NULL; writeLocalCookie(); if (request && request->enctype != FORM_ENCTYPE_MULTIPART) - pid = localcgi_popen_rw(&fdr, &fdw); + pid = open_pipe_rw(&fr, &fw); else - pid = localcgi_popen_rw(&fdr, NULL); + pid = open_pipe_rw(&fr, NULL); if (pid < 0) return NULL; else if (pid) { - if (fdw > 0) { - write(fdw, request->body, request->length); - close(fdw); + if (fw) { + fwrite(request->body, sizeof(char), request->length, fw); + fclose(fw); } - return fdopen(fdr, "r"); + return fr; } + setup_child(TRUE, 2, -1); if (qstr == NULL) set_cgi_environ(uri, file, uri); diff --git a/proto.h b/proto.h @@ -416,6 +416,7 @@ extern void set_int(void); extern void getTCstr(void); extern void setlinescols(void); extern void setupscreen(void); +extern pid_t open_pipe_rw(FILE **fr, FILE **fw); extern int initscr(void); extern int write1(char c); extern void endline(void); diff --git a/search.c b/search.c @@ -14,9 +14,9 @@ set_mark(Line *l, int pos, int epos) #ifdef USE_MIGEMO /* Migemo: romaji --> kana+kanji in regexp */ -static FILE *migemor, *migemow; +static FILE *migemor = NULL, *migemow = NULL; static int migemo_running; -static int migemo_pid; +static int migemo_pid = 0; void init_migemo() @@ -35,41 +35,18 @@ init_migemo() static int open_migemo(char *migemo_command) { - int fdr[2]; - int fdw[2]; - - if (pipe(fdr) < 0) - goto err0; - if (pipe(fdw) < 0) - goto err1; - - flush_tty(); - /* migemow:fdw[1] -|-> fdw[0]=0 {migemo} fdr[1]=1 -|-> fdr[0]:migemor */ - migemo_pid = fork(); + migemo_pid = open_pipe_rw(&migemor, &migemow); if (migemo_pid < 0) - goto err2; + goto err0; if (migemo_pid == 0) { /* child */ - close(fdr[0]); - close(fdw[1]); - dup2(fdw[0], 0); - dup2(fdr[1], 1); setup_child(FALSE, 2, -1); myExec(migemo_command); /* XXX: ifdef __EMX__, use start /f ? */ } - close(fdr[1]); - close(fdw[0]); - migemor = fdopen(fdr[0], "r"); - migemow = fdopen(fdw[1], "w"); return 1; - err2: - close(fdw[0]); - close(fdw[1]); - err1: - close(fdr[0]); - close(fdr[1]); err0: + migemo_pid = 0; migemo_active = migemo_running = 0; return 0; }