nsxiv9fa0bbc
Simplified it_shell_cmd() using SXIV_IMG env-variable
+28 −43
2 files changed
- M commands.c +19 −38
- M config.h +9 −5
| @@ -16,7 +16,9 @@ | |||
| 16 | 16 | * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA. | |
| 17 | 17 | */ | |
| 18 | 18 | ||
| 19 | + | #define _POSIX_C_SOURCE 200112L /* for setenv(3) */ | |
| 19 | 20 | #include <stdlib.h> | |
| 21 | + | ||
| 20 | 22 | #include <string.h> | |
| 21 | 23 | #include <unistd.h> | |
| 22 | 24 | #include <sys/wait.h> | |
| @@ -345,45 +347,27 @@ int it_open_with(arg_t a) { | |||
| 345 | 347 | warn("could not exec: %s", prog); | |
| 346 | 348 | exit(1); | |
| 347 | 349 | } else if (pid < 0) { | |
| 348 | − | warn("could not for. program was: %s", prog); | |
| 350 | + | warn("could not fork. program was: %s", prog); | |
| 349 | 351 | } | |
| 350 | 352 | ||
| 351 | 353 | return 0; | |
| 352 | 354 | } | |
| 353 | 355 | ||
| 354 | 356 | 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; | |
| 359 | 359 | pid_t pid; | |
| 360 | 360 | ||
| 361 | − | if (!cline || !*cline) | |
| 361 | + | if (!cmdline || !*cmdline) | |
| 362 | 362 | return 0; | |
| 363 | 363 | ||
| 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); | |
| 370 | 369 | 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 | − | } | |
| 385 | 370 | } | |
| 386 | − | *cn = '\0'; | |
| 387 | 371 | ||
| 388 | 372 | win_set_cursor(&win, CURSOR_WATCH); | |
| 389 | 373 | ||
| @@ -402,17 +386,16 @@ int it_shell_cmd(arg_t a) { | |||
| 402 | 386 | WEXITSTATUS(status), cmdline); | |
| 403 | 387 | ||
| 404 | 388 | if (mode == MODE_IMAGE) { | |
| 405 | − | if (fileidx < tns.cnt) | |
| 406 | − | tns_load(&tns, fileidx, &files[fileidx], False, True); | |
| 407 | 389 | img_close(&img, 1); | |
| 408 | 390 | 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; | |
| 416 | 399 | } | |
| 417 | 400 | ||
| 418 | 401 | end: | |
| @@ -420,7 +403,5 @@ end: | |||
| 420 | 403 | win_set_cursor(&win, CURSOR_ARROW); | |
| 421 | 404 | /* else: cursor gets reset in redraw() */ | |
| 422 | 405 | ||
| 423 | − | free(cmdline); | |
| 424 | − | ||
| 425 | 406 | return 1; | |
| 426 | 407 | } | |
| @@ -110,11 +110,15 @@ static const keymap_t keys[] = { | |||
| 110 | 110 | /* open current image with given program: */ | |
| 111 | 111 | { True, XK_g, it_open_with, (arg_t) "gimp" }, | |
| 112 | 112 | ||
| 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\"" }, | |
| 118 | 122 | }; | |
| 119 | 123 | ||
| 120 | 124 | /* mouse button mappings for image mode: */ | |