/* Always succeeds */ FILE* pager_start(cmd_params_st *params) { FILE *fp; const char *pager; if (params && params->no_pager != 0) return stdout; #ifdef HAVE_ISATTY if (isatty(STDOUT_FILENO) == 0) return stdout; #endif pager = get_pager(); if (pager == NULL || pager[0] == 0) return stdout; if (!getenv("LESS")) { setenv("LESS", "FRSX", 1); } fp = popen(pager, "w"); if (fp == NULL) { /* no pager */ fprintf(stderr, "unable to start pager; check your $PAGER environment variable\n"); fp = stdout; } return fp; }
/* * Print out the headlines for each message * in the passed message list. */ int from(void *v) { int *msgvec = v; int *ip, n; FILE *obuf = stdout; char *cp; (void)&obuf; (void)&cp; if (is_a_tty[0] && is_a_tty[1] && (cp = value("crt")) != NULL) { for (n = 0, ip = msgvec; *ip; ip++) n++; if (n > (*cp == '\0' ? screensize() : atoi(cp)) + 3) { cp = get_pager(); if (sigsetjmp(pipejmp, 1)) goto endpipe; if ((obuf = Popen(cp, "w", NULL, 1)) == NULL) { perror(cp); obuf = stdout; } else safe_signal(SIGPIPE, onpipe); } } for (ip = msgvec; *ip != 0; ip++) printhead(*ip, obuf, mb.mb_threaded); if (--ip >= msgvec) setdot(&message[*ip - 1]); endpipe: if (obuf != stdout) { safe_signal(SIGPIPE, SIG_IGN); Pclose(obuf); safe_signal(SIGPIPE, dflpipe); } return(0); }
/* * ~p command. */ static void print_collf(FILE *collf, struct header *hp) { char *lbuf = NULL; FILE *obuf = stdout; struct attachment *ap; char *cp; enum gfield gf; size_t linecnt, maxlines, linesize = 0, linelen, count, count2; (void)&obuf; (void)&cp; fflush(collf); rewind(collf); count = count2 = fsize(collf); if (is_a_tty[0] && is_a_tty[1] && (cp = value("crt")) != NULL) { for (linecnt = 0; fgetline(&lbuf, &linesize, &count2, NULL, collf, 0); linecnt++); rewind(collf); maxlines = (*cp == '\0' ? screensize() : atoi(cp)); maxlines -= 4; if (hp->h_to) maxlines--; if (hp->h_subject) maxlines--; if (hp->h_cc) maxlines--; if (hp->h_bcc) maxlines--; if (hp->h_attach) maxlines--; maxlines -= myaddrs(hp) != NULL || hp->h_from != NULL; maxlines -= value("ORGANIZATION") != NULL || hp->h_organization != NULL; maxlines -= value("replyto") != NULL || hp->h_replyto != NULL; maxlines -= value("sender") != NULL || hp->h_sender != NULL; if (linecnt > maxlines) { cp = get_pager(); if (sigsetjmp(pipejmp, 1)) goto endpipe; obuf = Popen(cp, "w", NULL, 1); if (obuf == NULL) { perror(cp); obuf = stdout; } else safe_signal(SIGPIPE, onpipe); } } fprintf(obuf, catgets(catd, CATSET, 62, "-------\nMessage contains:\n")); gf = GIDENT|GTO|GSUBJECT|GCC|GBCC|GNL|GFILES; if (value("fullnames")) gf |= GCOMMA; puthead(hp, obuf, gf, SEND_TODISP, CONV_NONE, NULL, NULL); while (fgetline(&lbuf, &linesize, &count, &linelen, collf, 1)) prout(lbuf, linelen, obuf); if (hp->h_attach != NULL) { fputs(catgets(catd, CATSET, 63, "Attachments:"), obuf); for (ap = hp->h_attach; ap != NULL; ap = ap->a_flink) { if (ap->a_msgno) fprintf(obuf, " message %u", ap->a_msgno); else fprintf(obuf, " %s", ap->a_name); if (ap->a_flink) putc(',', obuf); } putc('\n', obuf); } endpipe: if (obuf != stdout) { safe_signal(SIGPIPE, SIG_IGN); Pclose(obuf); safe_signal(SIGPIPE, dflpipe); } if (lbuf) free(lbuf); }
static int type1(int *msgvec, int doign, int page, int pipe, int decode, char *cmd, off_t *tstats) { int *ip; struct message *mp; char *cp; int nlines; off_t mstats[2]; /* * Must be static to become excluded from sigsetjmp(). */ static FILE *obuf; #ifdef __GNUC__ /* Avoid longjmp clobbering */ (void) &cp; (void) &cmd; (void) &obuf; #endif obuf = stdout; if (sigsetjmp(pipestop, 1)) goto close_pipe; if (pipe) { cp = value("SHELL"); if (cp == NULL) cp = SHELL; obuf = Popen(cmd, "w", cp, 1); if (obuf == NULL) { perror(cmd); obuf = stdout; } else { safe_signal(SIGPIPE, brokpipe); } } else if (value("interactive") != NULL && (page || (cp = value("crt")) != NULL)) { nlines = 0; if (!page) { for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) { if ((message[*ip-1].m_have & HAVE_BODY) == 0) { if ((get_body(&message[*ip - 1])) != OKAY) return 1; } nlines += message[*ip - 1].m_lines; } } if (page || nlines > (*cp ? atoi(cp) : realscreenheight)) { cp = get_pager(); obuf = Popen(cp, "w", NULL, 1); if (obuf == NULL) { perror(cp); obuf = stdout; } else safe_signal(SIGPIPE, brokpipe); } } for (ip = msgvec; *ip && ip - msgvec < msgCount; ip++) { mp = &message[*ip - 1]; touch(mp); setdot(mp); uncollapse1(mp, 1); if (value("quiet") == NULL) fprintf(obuf, catgets(catd, CATSET, 17, "Message %2d:\n"), *ip); send(mp, obuf, doign ? ignore : 0, NULL, pipe && value("piperaw") ? SEND_MBOX : decode ? SEND_SHOW : doign ? SEND_TODISP : SEND_TODISP_ALL, mstats); if (pipe && value("page")) { putc('\f', obuf); } if (tstats) { tstats[0] += mstats[0]; tstats[1] += mstats[1]; } } close_pipe: if (obuf != stdout) { /* * Ignore SIGPIPE so it can't cause a duplicate close. */ safe_signal(SIGPIPE, SIG_IGN); Pclose(obuf); safe_signal(SIGPIPE, dflpipe); } return(0); }
/* With no arguments specified: works as printenv | sort | less if we have defined pager in env variabels, that pager is used instead of less if we define parameters digenv works as: ./digenv [params] -> printenv | grep params | sort | less */ int main(int argc, char **argv, char **envp) { /* true for childprocesses after each fork */ bool isChild; pipe(pipes[DIGENV]); isChild = safe_fork(); if ( isChild ) { /* child pager */ prepare_send_pipe(DIGENV); execlp("printenv","printenv",NULL); _exit(EXIT_FAILURE); } receive_from_pipe(DIGENV); wait_for_child_to_terminate(); pipe(pipes[GREP]); isChild = safe_fork(); if ( isChild ) { prepare_send_pipe(GREP); if (argc > 1) { /* runs the binary "grep" with the arguments in argv */ execvp("grep", argv); }else { /* cat simply rewrites input to output, just like a grep without arguments ;) */ execvp("cat", argv); } _exit(EXIT_FAILURE); } receive_from_pipe(GREP); wait_for_child_to_terminate(); pipe(pipes[SORT]); isChild = safe_fork(); if ( isChild ) { prepare_send_pipe(SORT); /* runs the sort binary with to parameters but with piped input */ execlp("/usr/bin/sort","sort",NULL); /* If we get here, call to sort has failed. */ perror("Sorting failed"); _exit(EXIT_FAILURE); } receive_from_pipe(SORT); wait_for_child_to_terminate(); /* runs the specified pager with input from pipe */ execlp(get_pager(), get_pager(), NULL); /* if we get here, pager has failed. */ perror("Invalid pager"); _exit(EXIT_FAILURE); return EXIT_FAILURE; }