commit 6fb9cd5900009c31ae6fed631d870bf66ef53c56
parent b6d48210807f8b245adc9088246b913eb4e8b8f7
Author: ukai <ukai>
Date: Wed, 26 Dec 2001 18:29:33 +0000
[w3m-dev 02749] cleanup code for editor,mailer,extbrowser
From: Hironori Sakamoto <hsaka@mth.biglobe.ne.jp>
Diffstat:
M | ChangeLog | | | 16 | ++++++++++++++++ |
M | etc.c | | | 62 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | form.c | | | 23 | ++++++----------------- |
M | main.c | | | 117 | +++++++++++++++++--------------------------------------------------------------- |
M | proto.h | | | 2 | ++ |
5 files changed, 110 insertions(+), 110 deletions(-)
diff --git a/ChangeLog b/ChangeLog
@@ -1,5 +1,21 @@
2001-12-27 Hironori Sakamoto <hsaka@mth.biglobe.ne.jp>
+ * [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 <hsaka@mth.biglobe.ne.jp>
+
* [w3m-dev 02748] cleanup code for restoring cursor position
* buffer.c (reshapeBuffer): delete top, linenum, cursorY, pos,
currentColumn, formitem
diff --git a/etc.c b/etc.c
@@ -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
@@ -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
@@ -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
@@ -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);