Krolyxon
Commit 9fa0bbc

Branches

Refs →

nsxiv9fa0bbc

Simplified it_shell_cmd() using SXIV_IMG env-variable

Author
Bert
Date
2011-09-03 15:58 +0200
Parent
de6b58a
Commit
9fa0bbca17f6346f10cffb618d2905dd3711c291

+28 −43

2 files changed

  1. M commands.c +19 −38
  2. M config.h +9 −5
M commands.c View
@@ -16,7 +16,9 @@
1616 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
1717 */
1818
19+#define _POSIX_C_SOURCE 200112L /* for setenv(3) */
1920 #include <stdlib.h>
21+
2022 #include <string.h>
2123 #include <unistd.h>
2224 #include <sys/wait.h>
@@ -345,45 +347,27 @@ int it_open_with(arg_t a) {
345347 warn("could not exec: %s", prog);
346348 exit(1);
347349 } else if (pid < 0) {
348− warn("could not for. program was: %s", prog);
350+ warn("could not fork. program was: %s", prog);
349351 }
350352
351353 return 0;
352354 }
353355
354356 int it_shell_cmd(arg_t a) {
355− const char *cline = (const char*) a;
356− char *cn, *cmdline;
357− const char *co, *fpath;
358− int fpcnt, fplen, status;
357+ int n, status;
358+ const char *cmdline = (const char*) a;
359359 pid_t pid;
360360
361− if (!cline || !*cline)
361+ if (!cmdline || !*cmdline)
362362 return 0;
363363
364− /* build command line: */
365− fpcnt = 0;
366− co = cline - 1;
367− while ((co = strchr(co + 1, '#')))
368− fpcnt++;
369− if (!fpcnt)
364+ n = mode == MODE_IMAGE ? fileidx : tns.sel;
365+
366+ if (setenv("SXIV_IMG", files[n].path, 1) < 0) {
367+ warn("could not change env.-variable: SXIV_IMG. command line was: %s",
368+ cmdline);
370369 return 0;
371− fpath = files[mode == MODE_IMAGE ? fileidx : tns.sel].path;
372− fplen = strlen(fpath);
373− cn = cmdline = (char*) s_malloc((strlen(cline) + fpcnt * (fplen + 2)) *
374− sizeof(char));
375− /* replace all '#' with filename: */
376− for (co = cline; *co; co++) {
377− if (*co == '#') {
378− *cn++ = '"';
379− strcpy(cn, fpath);
380− cn += fplen;
381− *cn++ = '"';
382− } else {
383− *cn++ = *co;
384− }
385370 }
386− *cn = '\0';
387371
388372 win_set_cursor(&win, CURSOR_WATCH);
389373
@@ -402,17 +386,16 @@ int it_shell_cmd(arg_t a) {
402386 WEXITSTATUS(status), cmdline);
403387
404388 if (mode == MODE_IMAGE) {
405− if (fileidx < tns.cnt)
406− tns_load(&tns, fileidx, &files[fileidx], False, True);
407389 img_close(&img, 1);
408390 load_image(fileidx);
409− } else {
410− if (!tns_load(&tns, tns.sel, &files[tns.sel], True, False)) {
411− remove_file(tns.sel, 0);
412− tns.dirty = 1;
413− if (tns.sel >= tns.cnt)
414− tns.sel = tns.cnt - 1;
415− }
391+ }
392+ if (!tns_load(&tns, n, &files[n], True, mode == MODE_IMAGE) &&
393+ mode == MODE_THUMB)
394+ {
395+ remove_file(tns.sel, 0);
396+ tns.dirty = 1;
397+ if (tns.sel >= tns.cnt)
398+ tns.sel = tns.cnt - 1;
416399 }
417400
418401 end:
@@ -420,7 +403,5 @@ end:
420403 win_set_cursor(&win, CURSOR_ARROW);
421404 /* else: cursor gets reset in redraw() */
422405
423− free(cmdline);
424−
425406 return 1;
426407 }
M config.h View
@@ -110,11 +110,15 @@ static const keymap_t keys[] = {
110110 /* open current image with given program: */
111111 { True, XK_g, it_open_with, (arg_t) "gimp" },
112112
113− /* run shell command line on current file ('#' is replaced by file path: */
114− { True, XK_less, it_shell_cmd, (arg_t) "mogrify -rotate -90 #" },
115− { True, XK_greater, it_shell_cmd, (arg_t) "mogrify -rotate +90 #" },
116− { True, XK_comma, it_shell_cmd, (arg_t) "jpegtran -rotate 270 -copy all -outfile # #" },
117− { True, XK_period, it_shell_cmd, (arg_t) "jpegtran -rotate 90 -copy all -outfile # #" },
113+ /* run shell command line on current file ("$SXIV_IMG"): */
114+ { True, XK_less, it_shell_cmd, (arg_t) \
115+ "mogrify -rotate -90 \"$SXIV_IMG\"" },
116+ { True, XK_greater, it_shell_cmd, (arg_t) \
117+ "mogrify -rotate +90 \"$SXIV_IMG\"" },
118+ { True, XK_comma, it_shell_cmd, (arg_t) \
119+ "jpegtran -rotate 270 -copy all -outfile \"$SXIV_IMG\" \"$SXIV_IMG\"" },
120+ { True, XK_period, it_shell_cmd, (arg_t) \
121+ "jpegtran -rotate 90 -copy all -outfile \"$SXIV_IMG\" \"$SXIV_IMG\"" },
118122 };
119123
120124 /* mouse button mappings for image mode: */