commit dfcc74a1ce9f275fa591e0f27641cb40149ae2ce from: Marcel Rodrigues date: Fri Mar 18 23:14:52 2016 UTC Refactor duplicate code into new function. commit - c251d7a816a070bc307b932d5159ba5c8ba00339 commit + dfcc74a1ce9f275fa591e0f27641cb40149ae2ce blob - 0d832737b8429b37005f0dd09da4134c19051ed3 blob + cca95ad7ee970590454ca968f55627306311b6e2 --- rover.c +++ rover.c @@ -313,7 +313,25 @@ spawn(char **args) } else if (pid == 0) { /* Child process. */ execvp(args[0], args); + } +} + +static int +open_with_env(const char *env, char *path) +{ + char *program = getenv(env); + if (program) { +#ifdef RV_SHELL + strncpy(BUF1, program, BUFLEN - 1); + strncat(BUF1, " ", BUFLEN - strlen(program) - 1); + strncat(BUF1, path, BUFLEN - strlen(program) - strlen(path) - 2); + spawn((char *[]) {RV_SHELL, "-c", BUF1, NULL}); +#else + spawn((char *[]) {program, path, NULL}); +#endif + return 1; } + return 0; } /* Curses setup. */ @@ -1079,33 +1097,12 @@ main(int argc, char *argv[]) } } else if (!strcmp(key, RVK_VIEW)) { if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue; - program = getenv("PAGER"); - if (program) { -#ifdef RV_SHELL - strncpy(BUF1, program, BUFLEN - 1); - strncat(BUF1, " ", BUFLEN - strlen(program) - 1); - strncat(BUF1, ENAME(ESEL), - BUFLEN - strlen(program) - strlen(ENAME(ESEL)) - 2); - spawn((char *[]) {RV_SHELL, "-c", BUF1, NULL}); -#else - spawn((char *[]) {program, ENAME(ESEL), NULL}); -#endif - } + if (open_with_env("PAGER", ENAME(ESEL))) + cd(0); } else if (!strcmp(key, RVK_EDIT)) { if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue; - program = getenv("EDITOR"); - if (program) { -#ifdef RV_SHELL - strncpy(BUF1, program, BUFLEN - 1); - strncat(BUF1, " ", BUFLEN - strlen(program) - 1); - strncat(BUF1, ENAME(ESEL), - BUFLEN - strlen(program) - strlen(ENAME(ESEL)) - 2); - spawn((char *[]) {RV_SHELL, "-c", BUF1, NULL}); -#else - spawn((char *[]) {program, ENAME(ESEL), NULL}); -#endif + if (open_with_env("EDITOR", ENAME(ESEL))) cd(0); - } } else if (!strcmp(key, RVK_SEARCH)) { int oldsel, oldscroll, length; if (!rover.nfiles) continue;