From e6b49bac2eed9c5ce7680bdbb4a677e4b82e77cc Mon Sep 17 00:00:00 2001 From: Fumitoshi UKAI Date: Wed, 26 Dec 2001 18:29:33 +0000 Subject: [w3m-dev 02749] cleanup code for editor,mailer,extbrowser From: Hironori Sakamoto --- ChangeLog | 18 +++++++++- etc.c | 64 ++++++++++++++++++++++++++++++++- form.c | 25 ++++--------- main.c | 119 +++++++++++++------------------------------------------------- proto.h | 4 ++- 5 files changed, 115 insertions(+), 115 deletions(-) diff --git a/ChangeLog b/ChangeLog index ecb15a8..0065793 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2001-12-27 Hironori Sakamoto + + * [w3m-dev 02749] cleanup code for editor,mailer,extbrowser + * etc.c (myExtCommand): added + * etc.c (myEditor): added + * form.c (input_textarea): use myEditor() + * main.c (pipeBuf): use myExtCommand() + * main.c (editBf): use myEditor() + * main.c (editScr): use myEditor() + * main.c (followA): use myExtCommand() + * main.c (cmd_loadURL): use myExtCommand() + * main.c (invoke_browser): use myExtCommand() + * main.c (execdict): use myExtCommand() + * proto.h (myExtCommand): added + * proto.h (myEditor): added + 2001-12-27 Hironori Sakamoto * [w3m-dev 02748] cleanup code for restoring cursor position @@ -1655,4 +1671,4 @@ * release-0-2-1 * import w3m-0.2.1 -$Id: ChangeLog,v 1.183 2001/12/26 18:17:57 ukai Exp $ +$Id: ChangeLog,v 1.184 2001/12/26 18:29:33 ukai Exp $ diff --git a/etc.c b/etc.c index c7596e2..6350fab 100644 --- a/etc.c +++ b/etc.c @@ -1,4 +1,4 @@ -/* $Id: etc.c,v 1.15 2001/12/25 18:15:00 ukai Exp $ */ +/* $Id: etc.c,v 1.16 2001/12/26 18:29:33 ukai Exp $ */ #include "fm.h" #include #include "myctype.h" @@ -1059,6 +1059,68 @@ mySystem(char *command, int background) system(command); } +Str +myExtCommand(char *cmd, char *arg, int redirect) +{ + Str tmp = NULL; + char *p; + int set_arg = FALSE; + + for (p = cmd; *p; p++) { + if (*p == '%' && *(p + 1) == 's' && !set_arg) { + if (tmp == NULL) + tmp = Strnew_charp_n(cmd, (int)(p - cmd)); + Strcat_charp(tmp, arg); + set_arg = TRUE; + p++; + } + else { + if (tmp) + Strcat_char(tmp, *p); + } + } + if (!set_arg) + tmp = Strnew_m_charp(cmd, (redirect ? " < " : " "), arg, NULL); + return tmp; +} + +Str +myEditor(char *cmd, char *file, int line) +{ + Str tmp = NULL; + char *p; + int set_file = FALSE, set_line = FALSE; + + for (p = cmd; *p; p++) { + if (*p == '%' && *(p + 1) == 's' && !set_file) { + if (tmp == NULL) + tmp = Strnew_charp_n(cmd, (int)(p - cmd)); + Strcat_charp(tmp, file); + set_file = TRUE; + p++; + } + else if (*p == '%' && *(p + 1) == 'd' && !set_line && line > 0) { + if (tmp == NULL) + tmp = Strnew_charp_n(cmd, (int)(p - cmd)); + Strcat(tmp, Sprintf("%d", line)); + set_line = TRUE; + p++; + } + else { + if (tmp) + Strcat_char(tmp, *p); + } + } + if (!set_file) { + if (tmp == NULL) + tmp = Strnew_charp(cmd); + if (!set_line && line > 0 && strcasestr(cmd, "vi")) + Strcat(tmp, Sprintf(" +%d", line)); + Strcat_m_charp(tmp, " ", file, NULL); + } + return tmp; +} + char * expandName(char *name) { diff --git a/form.c b/form.c index 4efffc2..d4a5556 100644 --- a/form.c +++ b/form.c @@ -1,4 +1,4 @@ -/* $Id: form.c,v 1.7 2001/11/29 09:34:14 ukai Exp $ */ +/* $Id: form.c,v 1.8 2001/12/26 18:29:33 ukai Exp $ */ /* * HTML forms */ @@ -453,14 +453,14 @@ form_fputs_decode(Str s, FILE * f) void input_textarea(FormItemList *fi) { - Str tmpname = tmpfname(TMPF_DFL, NULL); + char *tmpf = tmpfname(TMPF_DFL, NULL)->ptr; Str tmp; FILE *f; #ifdef JP_CHARSET char code = DisplayCode, ic; #endif - f = fopen(tmpname->ptr, "w"); + f = fopen(tmpf, "w"); if (f == NULL) { disp_err_message("Can't open temporary file", FALSE); return; @@ -468,25 +468,14 @@ input_textarea(FormItemList *fi) if (fi->value) form_fputs_decode(fi->value, f); fclose(f); - if (strcasestr(Editor, "%s")) - if (strcasestr(Editor, "%d")) - tmp = Sprintf(Editor, 1, tmpname->ptr); - else - tmp = Sprintf(Editor, tmpname->ptr); - else { - if (strcasestr(Editor, "%d")) - tmp = Sprintf(Editor, 1); - else - tmp = Strnew_charp(Editor); - Strcat_m_charp(tmp, " ", tmpname->ptr, NULL); - } + fmTerm(); - system(tmp->ptr); + system(myEditor(Editor, tmpf, 1)->ptr); fmInit(); if (fi->readonly) return; - f = fopen(tmpname->ptr, "r"); + f = fopen(tmpf, "r"); if (f == NULL) { disp_err_message("Can't open temporary file", FALSE); return; @@ -509,7 +498,7 @@ input_textarea(FormItemList *fi) Strcat(fi->value, tmp); } fclose(f); - unlink(tmpname->ptr); + unlink(tmpf); } void diff --git a/main.c b/main.c index 0bc804a..5f603a6 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.49 2001/12/26 18:17:57 ukai Exp $ */ +/* $Id: main.c,v 1.50 2001/12/26 18:29:33 ukai Exp $ */ #define MAINPROGRAM #include "fm.h" #include @@ -1656,11 +1656,7 @@ pipeBuf(void) saveBuffer(Currentbuf, f); fclose(f); pushText(fileToDelete, tmpf); - if (strcasestr(cmd, "%s")) - cmd = Sprintf(cmd, tmpf)->ptr; - else - cmd = Sprintf("%s < %s", cmd, tmpf)->ptr; - buf = getpipe(cmd); + buf = getpipe(myExtCommand(cmd, tmpf, TRUE)->ptr); if (buf == NULL) { disp_message("Execution failed", FALSE); } @@ -2238,31 +2234,11 @@ editBf(void) if (Currentbuf->frameset != NULL) fbuf = Currentbuf->linkBuffer[LB_FRAME]; copyBuffer(&sbuf, Currentbuf); - if (Currentbuf->edit) { - cmd = unquote_mailcap(Currentbuf->edit, - Currentbuf->real_type, - fn, + if (Currentbuf->edit) + cmd = unquote_mailcap(Currentbuf->edit, Currentbuf->real_type, fn, checkHeader(Currentbuf, "Content-Type:"), NULL); - } - else { - char *file = shell_quote(fn); - int linenum = CUR_LINENUMBER(Currentbuf); - if (strcasestr(Editor, "%s")) { - if (strcasestr(Editor, "%d")) - cmd = Sprintf(Editor, linenum, file); - else - cmd = Sprintf(Editor, file); - } - else { - if (strcasestr(Editor, "%d")) - cmd = Sprintf(Editor, linenum); - else if (strcasestr(Editor, "vi")) - cmd = Sprintf("%s +%d", Editor, linenum); - else - cmd = Strnew_charp(Editor); - Strcat_m_charp(cmd, " ", file, NULL); - } - } + else + cmd = myEditor(Editor, shell_quote(fn), CUR_LINENUMBER(Currentbuf)); fmTerm(); system(cmd->ptr); fmInit(); @@ -2309,45 +2285,21 @@ editBf(void) void editScr(void) { - int lnum; - Str cmd; - Str tmpf; + char *tmpf; FILE *f; - tmpf = tmpfname(TMPF_DFL, NULL); - f = fopen(tmpf->ptr, "w"); + tmpf = tmpfname(TMPF_DFL, NULL)->ptr; + f = fopen(tmpf, "w"); if (f == NULL) { - cmd = Sprintf("Can't open %s", tmpf->ptr); - disp_err_message(cmd->ptr, TRUE); + disp_err_message(Sprintf("Can't open %s", tmpf)->ptr, TRUE); return; } saveBuffer(Currentbuf, f); fclose(f); - if (Currentbuf->currentLine) - lnum = Currentbuf->currentLine->linenumber; - else - lnum = 1; - if (strcasestr(Editor, "%s")) { - if (strcasestr(Editor, "%d")) - cmd = Sprintf(Editor, lnum, tmpf->ptr); - else - cmd = Sprintf(Editor, tmpf->ptr); - } - else { - if (strcasestr(Editor, "%d")) - cmd = Sprintf(Editor, lnum); - else if (strcasestr(Editor, "vi")) - cmd = Sprintf("%s +%d", Editor, lnum); - else - cmd = Strnew_charp(Editor); - Strcat_m_charp(cmd, " ", tmpf->ptr, NULL); - } fmTerm(); - system(cmd->ptr); + system(myEditor(Editor, tmpf, CUR_LINENUMBER(Currentbuf))->ptr); fmInit(); - unlink(tmpf->ptr); - gotoLine(Currentbuf, lnum); - arrangeCursor(Currentbuf); + unlink(tmpf); displayBuffer(Currentbuf, B_FORCE_REDRAW); } @@ -2652,14 +2604,9 @@ followA(void) } if (!strncasecmp(a->url, "mailto:", 7)) { /* invoke external mailer */ - Str tmp; - char *to = shell_quote(url_unquote(a->url + 7)); - if (strcasestr(Mailer, "%s")) - tmp = Sprintf(Mailer, to); - else - tmp = Strnew_m_charp(Mailer, " ", to, NULL); fmTerm(); - system(tmp->ptr); + system(myExtCommand(Mailer, shell_quote(url_unquote(a->url + 7)), + FALSE)->ptr); fmInit(); displayBuffer(Currentbuf, B_FORCE_REDRAW); return; @@ -3595,14 +3542,9 @@ cmd_loadURL(char *url, ParsedURL *current) if (!strncasecmp(url, "mailto:", 7)) { /* invoke external mailer */ - Str tmp; - char *to = shell_quote(url + 7); - if (strcasestr(Mailer, "%s")) - tmp = Sprintf(Mailer, to); - else - tmp = Strnew_m_charp(Mailer, " ", to, NULL); fmTerm(); - system(tmp->ptr); + system(myExtCommand(Mailer, shell_quote(url_unquote(url + 7)), + FALSE)->ptr); fmInit(); displayBuffer(Currentbuf, B_FORCE_REDRAW); return; @@ -4302,7 +4244,7 @@ rFrame(void) static void invoke_browser(char *url) { - Str tmp; + Str cmd; char *browser = NULL; int bg = 0; @@ -4332,22 +4274,15 @@ invoke_browser(char *url) } if (browser == NULL || *browser == '\0') return; - url = shell_quote(url); - if (strcasestr(browser, "%s")) { - tmp = Sprintf(browser, url); - Strremovetrailingspaces(tmp); - if (Strlastchar(tmp) == '&') { - Strshrink(tmp, 1); - bg = 1; - } - } - else { - tmp = Strnew_charp(browser); - Strcat_char(tmp, ' '); - Strcat_charp(tmp, url); + + cmd = myExtCommand(browser, shell_quote(url), FALSE); + Strremovetrailingspaces(cmd); + if (Strlastchar(cmd) == '&') { + Strshrink(cmd, 1); + bg = 1; } fmTerm(); - mySystem(tmp->ptr, bg); + mySystem(cmd->ptr, bg); fmInit(); displayBuffer(Currentbuf, B_FORCE_REDRAW); } @@ -4666,19 +4601,15 @@ static void execdict(char *word) { Buffer *buf; - Str cmd; MySignalHandler(*prevtrap) (); if (word == NULL || *word == '\0') { displayBuffer(Currentbuf, B_NORMAL); return; } - cmd = Strnew_charp(DICTCMD); - Strcat_char(cmd, ' '); - Strcat_charp(cmd, word); prevtrap = signal(SIGINT, intTrap); crmode(); - buf = getshell(cmd->ptr); + buf = getshell(myExtCommand(DICTCMD, shell_quote(word), FALSE)->ptr); buf->filename = word; word = conv_from_system(word); buf->buffername = Sprintf("%s %s", DICTBUFFERNAME, word)->ptr; diff --git a/proto.h b/proto.h index 7be5f60..fb19509 100644 --- a/proto.h +++ b/proto.h @@ -1,4 +1,4 @@ -/* $Id: proto.h,v 1.21 2001/12/26 18:17:57 ukai Exp $ */ +/* $Id: proto.h,v 1.22 2001/12/26 18:29:33 ukai Exp $ */ /* * This file was automatically generated by version 1.7 of cextract. * Manual editing not recommended. @@ -479,6 +479,8 @@ extern char *last_modified(Buffer *buf); extern Str romanNumeral(int n); extern Str romanAlphabet(int n); extern void mySystem(char *command, int background); +extern Str myExtCommand(char *cmd, char *arg, int redirect); +extern Str myEditor(char *cmd, char *file, int line); extern char *file_to_url(char *file); extern char *expandName(char *name); extern Str tmpfname(int type, char *ext); -- cgit v1.2.3