pager.c - rohrpost - A commandline mail client to change the world as we see it.
 (HTM) git clone git://r-36.net/rohrpost
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       pager.c (1533B)
       ---
            1 /*
            2  * Copy me if you can.
            3  * by 20h
            4  */
            5 
            6 #include <unistd.h>
            7 #include <stdio.h>
            8 #include <stdlib.h>
            9 #include <string.h>
           10 #include <sys/types.h>
           11 #include <sys/wait.h>
           12 
           13 #include "ind.h"
           14 
           15 char *
           16 getpager(void)
           17 {
           18         char *ret;
           19 
           20         ret = getenv("PAGER");
           21         if (ret == NULL)
           22                 ret = "less";
           23         return ret;
           24 }
           25 
           26 char *
           27 geteditor(void)
           28 {
           29         char *ret;
           30 
           31         ret = getenv("EDITOR");
           32         if (ret == NULL)
           33                 return "vim";
           34         return ret;
           35 }
           36 
           37 void
           38 runfile(char *app, char *file)
           39 {
           40         char *cmd;
           41 
           42         cmd = smprintf("%s %s", app, file);
           43         if (runcmd(cmd, NULL, NULL, NULL, 1) < 0)
           44                 edie("pagefile");
           45         free(cmd);
           46 
           47         wait(NULL);
           48 }
           49 
           50 void
           51 editfile(char *file)
           52 {
           53         runfile(geteditor(), file);
           54 }
           55 
           56 void
           57 pagefile(char *file)
           58 {
           59         runfile(getpager(), file);
           60 }
           61 
           62 char *
           63 runstring(void (*runner)(char *), char *str, char *ext, int docmp)
           64 {
           65         char *tname, *nstr;
           66         int tfd, len;
           67 
           68 
           69         tname = mktmpfile("rohrpost", ext, &tfd);
           70         if (writefile(tname, str, strlen(str), "w+"))
           71                 edie("runstring writefile");
           72         runner(tname);
           73 
           74         if (docmp) {
           75                 nstr = readfile(tname, &len);
           76                 if (nstr == NULL)
           77                         edie("runstring readfile");
           78                 if (strcmp(str, nstr)) {
           79                         close(tfd);
           80                         unlink(tname);
           81                         return nstr;
           82                 }
           83                 if (nstr != NULL)
           84                         free(nstr);
           85         }
           86         close(tfd);
           87         unlink(tname);
           88 
           89         return NULL;
           90 }
           91 
           92 char *
           93 editstring(char *str)
           94 {
           95         return runstring(editfile, str, "", 1);
           96 }
           97 
           98 char *
           99 editstringext(char *str, char *ext)
          100 {
          101         return runstring(editfile, str, ext, 1);
          102 }
          103 
          104 void
          105 pagestring(char *str)
          106 {
          107         runstring(pagefile, str, "", 0);
          108 }
          109 
          110 void
          111 pagestringext(char *str, char *ext)
          112 {
          113         runstring(pagefile, str, ext, 0);
          114 }
          115