home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / HACKSRC.ZIP / PAGER.C < prev    next >
Text File  |  1985-10-27  |  9KB  |  430 lines

  1. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  2. /* pager.c - version 1.0.3 */
  3.  
  4. /* This file contains the command routine dowhatis() and a pager. */
  5. /* Also readmail() and doshell(), and generally the things that
  6.    contact the outside world. */
  7.  
  8. #include <stdio.h>
  9. #include <signal.h>
  10. #include "hack.h"
  11. extern int CO, LI;    /* usually COLNO and ROWNO+2 */
  12. extern char *CD;
  13. extern char quitchars[];
  14. extern char *getenv(), *getlogin();
  15. int done1();
  16.  
  17. dowhatis()
  18. {
  19.     FILE *fp;
  20.     char bufr[BUFSZ+6];
  21.     register char *buf = &bufr[6], *ep, q;
  22.     extern char readchar();
  23.  
  24.     if(!(fp = fopen("data","r")))
  25.         pline("Cannot open data file!");
  26.     else {
  27.         pline("Specify what? ");
  28.         q = readchar();
  29. #ifdef DGK
  30.         if (index(quitchars, q))
  31.             return(0);
  32. #endif DGK
  33.         if(q != '\t')
  34.         while(fgets(buf,BUFSZ,fp))
  35.             if(*buf == q) {
  36.             ep = index(buf, '\n');
  37.             if(ep) *ep = 0;
  38.             /* else: bad data file */
  39.             /* Expand tab 'by hand' */
  40.             if(buf[1] == '\t'){
  41.                 buf = bufr;
  42.                 buf[0] = q;
  43.                 (void) strncpy(buf+1, "       ", 7);
  44.             }
  45.             pline(buf);
  46.             if(ep[-1] == ';') {
  47.                 pline("More info? ");
  48.                 if(readchar() == 'y') {
  49.                     page_more(fp,1); /* does fclose() */
  50.                     return(0);
  51.                 }
  52.             }
  53.             (void) fclose(fp);     /* kopper@psuvax1 */
  54.             return(0);
  55.             }
  56.         pline("I've never heard of such things.");
  57.         (void) fclose(fp);
  58.     }
  59.     return(0);
  60. }
  61.  
  62. /* make the paging of a file interruptible */
  63. static int got_intrup;
  64.  
  65. intruph(){
  66.     got_intrup++;
  67. }
  68.  
  69. /* simple pager, also used from dohelp() */
  70. page_more(fp,strip)
  71. FILE *fp;
  72. int strip;    /* nr of chars to be stripped from each line (0 or 1) */
  73. {
  74.     register char *bufr, *ep;
  75.     int (*prevsig)() = signal(SIGINT, intruph);
  76. #ifdef DGK
  77.     /* There seems to be a bug in ANSI.SYS  The first tab character
  78.      * after a clear screen sequence is not expanded correctly.  Thus
  79.      * expand the tabs by hand -dgk
  80.      */
  81.     int tabstop = 8, spaces;
  82.     char buf[BUFSIZ], *bufp, *bufrp;
  83.  
  84.     set_pager(0);
  85.     bufr = (char *) alloc((unsigned) CO);
  86.     while (fgets(buf, BUFSIZ, fp) && (!strip || *buf == '\t') && !got_intrup){
  87.         bufp = buf;
  88.         bufrp = bufr;
  89.         while (*bufp && *bufp != '\n') {
  90.             if (*bufp == '\t') {
  91.                 spaces = tabstop - (bufrp - bufr) % tabstop;
  92.                 while (spaces--)
  93.                     *bufrp++ = ' ';
  94.                 bufp++;
  95.             } else
  96.                 *bufrp++ = *bufp++;
  97.         }
  98.         *bufrp = '\0';
  99. #else
  100.     set_pager(0);
  101.     bufr = (char *) alloc((unsigned) CO);
  102.     bufr[CO-1] = 0;
  103.     while(fgets(bufr,CO-1,fp) && (!strip || *bufr == '\t') && !got_intrup){
  104.         ep = index(bufr, '\n');
  105.         if(ep)
  106.             *ep = 0;
  107. #endif DGK
  108.         if(page_line(bufr+strip)) {
  109.             set_pager(2);
  110.             goto ret;
  111.         }
  112.     }
  113.     set_pager(1);
  114. ret:
  115.     free(bufr);
  116.     (void) fclose(fp);
  117.     (void) signal(SIGINT, prevsig);
  118.     got_intrup = 0;
  119. }
  120.  
  121. static boolean whole_screen = TRUE;
  122. #define    PAGMIN    12    /* minimum # of lines for page below level map */
  123.  
  124. set_whole_screen() {    /* called in termcap as soon as LI is known */
  125.     whole_screen = (LI-ROWNO-2 <= PAGMIN || !CD);
  126. }
  127.  
  128. #ifdef NEWS
  129. readnews() {
  130.     register int ret;
  131.  
  132.     whole_screen = TRUE;    /* force a docrt(), our first */
  133.     ret = page_file(NEWS, TRUE);
  134.     set_whole_screen();
  135.     return(ret);        /* report whether we did docrt() */
  136. }
  137. #endif NEWS
  138.  
  139. set_pager(mode)
  140. register int mode;    /* 0: open  1: wait+close  2: close */
  141. {
  142.     static boolean so;
  143.     if(mode == 0) {
  144.         if(!whole_screen) {
  145.             /* clear topline */
  146.             clrlin();
  147.             /* use part of screen below level map */
  148.             curs(1, ROWNO+4);
  149.         } else {
  150.             cls();
  151.         }
  152.         so = flags.standout;
  153.         flags.standout = 1;
  154.     } else {
  155.         if(mode == 1) {
  156.             curs(1, LI);
  157.             more();
  158.         }
  159.         flags.standout = so;
  160.         if(whole_screen)
  161.             docrt();
  162.         else {
  163.             curs(1, ROWNO+4);
  164.             cl_eos();
  165.         }
  166.     }
  167. }
  168.  
  169. page_line(s)        /* returns 1 if we should quit */
  170. register char *s;
  171. {
  172.     extern char morc;
  173.  
  174.     if(cury == LI-1) {
  175.         if(!*s)
  176.             return(0);    /* suppress blank lines at top */
  177.         putchar('\n');
  178.         cury++;
  179.         cmore("q\033");
  180.         if(morc) {
  181.             morc = 0;
  182.             return(1);
  183.         }
  184.         if(whole_screen)
  185.             cls();
  186.         else {
  187.             curs(1, ROWNO+4);
  188.             cl_eos();
  189.         }
  190.     }
  191.     puts(s);
  192.     cury++;
  193.     return(0);
  194. }
  195.  
  196. /*
  197.  * Flexible pager: feed it with a number of lines and it will decide
  198.  * whether these should be fed to the pager above, or displayed in a
  199.  * corner.
  200.  * Call:
  201.  *    cornline(0, title or 0)    : initialize
  202.  *    cornline(1, text)    : add text to the chain of texts
  203.  *    cornline(2, morcs)    : output everything and cleanup
  204.  *    cornline(3, 0)        : cleanup
  205.  */
  206.  
  207. cornline(mode, text)
  208. int mode;
  209. char *text;
  210. {
  211.     static struct line {
  212.         struct line *next_line;
  213.         char *line_text;
  214.     } *texthead, *texttail;
  215.     static int maxlen;
  216.     static int linect;
  217.     register struct line *tl;
  218.  
  219.     if(mode == 0) {
  220.         texthead = 0;
  221.         maxlen = 0;
  222.         linect = 0;
  223.         if(text) {
  224.             cornline(1, text);    /* title */
  225.             cornline(1, "");    /* blank line */
  226.         }
  227.         return;
  228.     }
  229.  
  230.     if(mode == 1) {
  231.         register int len;
  232.  
  233.         if(!text) return;    /* superfluous, just to be sure */
  234.         linect++;
  235.         len = strlen(text);
  236.         if(len > maxlen)
  237.         maxlen = len;
  238.         tl = (struct line *)
  239.         alloc((unsigned)(len + sizeof(struct line) + 1));
  240.         tl->next_line = 0;
  241.         tl->line_text = (char *)(tl + 1);
  242.         (void) strcpy(tl->line_text, text);
  243.         if(!texthead)
  244.         texthead = tl;
  245.         else
  246.         texttail->next_line = tl;
  247.         texttail = tl;
  248.         return;
  249.     }
  250.  
  251.     /* --- now we really do it --- */
  252.     if(mode == 2 && linect == 1)                /* topline only */
  253.         pline(texthead->line_text);
  254.     else
  255.     if(mode == 2) {
  256.         register int curline, lth;
  257.  
  258.         if(flags.toplin == 1) more();    /* ab@unido */
  259.         remember_topl();
  260.  
  261.         lth = CO - maxlen - 2;           /* Use full screen width */
  262.         if (linect < LI && lth >= 10) {             /* in a corner */
  263.         home ();
  264.         cl_end ();
  265.         flags.toplin = 0;
  266.         curline = 1;
  267.         for (tl = texthead; tl; tl = tl->next_line) {
  268.             curs (lth, curline);
  269.             if(curline > 1)
  270.             cl_end ();
  271.             putsym(' ');
  272.             putstr (tl->line_text);
  273.             curline++;
  274.         }
  275.         curs (lth, curline);
  276.         cl_end ();
  277.         cmore (text);
  278.         home ();
  279.         cl_end ();
  280.         docorner (lth, curline-1);
  281.         } else {                    /* feed to pager */
  282.         set_pager(0);
  283.         for (tl = texthead; tl; tl = tl->next_line) {
  284.             if (page_line (tl->line_text)) {
  285.             set_pager(2);
  286.             goto cleanup;
  287.             }
  288.         }
  289.         if(text) {
  290.             cgetret(text);
  291.             set_pager(2);
  292.         } else
  293.             set_pager(1);
  294.         }
  295.     }
  296.  
  297. cleanup:
  298.     while(tl = texthead) {
  299.         texthead = tl->next_line;
  300.         free((char *) tl);
  301.     }
  302. }
  303.  
  304. dohelp()
  305. {
  306.     char c;
  307.  
  308.     pline ("Long or short help? ");
  309.     while (((c = readchar ()) != 'l') && (c != 's') && !index(quitchars,c))
  310.         bell ();
  311.     if (!index(quitchars, c))
  312.         (void) page_file((c == 'l') ? HELP : SHELP, FALSE);
  313.     return(0);
  314. }
  315.  
  316. page_file(fnam, silent)    /* return: 0 - cannot open fnam; 1 - otherwise */
  317. register char *fnam;
  318. boolean silent;
  319. {
  320. #ifdef DEF_PAGER            /* this implies that UNIX is defined */
  321.       {
  322.     /* use external pager; this may give security problems */
  323.  
  324.     register int fd = open(fnam, 0);
  325.  
  326.     if(fd < 0) {
  327.         if(!silent) pline("Cannot open %s.", fnam);
  328.         return(0);
  329.     }
  330.     if(child(1)){
  331.         extern char *catmore;
  332.  
  333.         /* Now that child() does a setuid(getuid()) and a chdir(),
  334.            we may not be able to open file fnam anymore, so make
  335.            it stdin. */
  336.         (void) close(0);
  337.         if(dup(fd)) {
  338.             if(!silent) printf("Cannot open %s as stdin.\n", fnam);
  339.         } else {
  340.             execl(catmore, "page", (char *) 0);
  341.             if(!silent) printf("Cannot exec %s.\n", catmore);
  342.         }
  343.         exit(1);
  344.     }
  345.     (void) close(fd);
  346.       }
  347. #else DEF_PAGER
  348.       {
  349.     FILE *f;            /* free after Robert Viduya */
  350.  
  351.     if ((f = fopen (fnam, "r")) == (FILE *) 0) {
  352.         if(!silent) {
  353.             home(); perror (fnam); flags.toplin = 1;
  354.             pline ("Cannot open %s.", fnam);
  355.         }
  356.         return(0);
  357.     }
  358.     page_more(f, 0);
  359.       }
  360. #endif DEF_PAGER
  361.  
  362.     return(1);
  363. }
  364.  
  365. #ifdef UNIX
  366. #ifdef SHELL
  367. dosh(){
  368. register char *str;
  369.     if(child(0)) {
  370.         if(str = getenv("SHELL"))
  371.             execl(str, str, (char *) 0);
  372.         else
  373.             execl("/bin/sh", "sh", (char *) 0);
  374.         pline("sh: cannot execute.");
  375.         exit(1);
  376.     }
  377.     return(0);
  378. }
  379. #endif SHELL
  380.  
  381. #ifdef NOWAITINCLUDE
  382. union wait {        /* used only for the cast  (union wait *) 0  */
  383.     int w_status;
  384.     struct {
  385.         unsigned short w_Termsig:7;
  386.         unsigned short w_Coredump:1;
  387.         unsigned short w_Retcode:8;
  388.     } w_T;
  389. };
  390.  
  391. #else
  392.  
  393. #ifdef BSD
  394. #include    <sys/wait.h>
  395. #else
  396. #include    <wait.h>
  397. #endif BSD
  398. #endif NOWAITINCLUDE
  399.  
  400. child(wt) {
  401. register int f = fork();
  402.     if(f == 0){        /* child */
  403.         settty((char *) 0);        /* also calls end_screen() */
  404.         (void) setuid(getuid());
  405.         (void) setgid(getgid());
  406. #ifdef CHDIR
  407.         (void) chdir(getenv("HOME"));
  408. #endif CHDIR
  409.         return(1);
  410.     }
  411.     if(f == -1) {    /* cannot fork */
  412.         pline("Fork failed. Try again.");
  413.         return(0);
  414.     }
  415.     /* fork succeeded; wait for child to exit */
  416.     (void) signal(SIGINT,SIG_IGN);
  417.     (void) signal(SIGQUIT,SIG_IGN);
  418.     (void) wait((union wait *) 0);
  419.     gettty();
  420.     setftty();
  421.     (void) signal(SIGINT,done1);
  422. #ifdef WIZARD
  423.     if(wizard) (void) signal(SIGQUIT,SIG_DFL);
  424. #endif WIZARD
  425.     if(wt) getret();
  426.     docrt();
  427.     return(0);
  428. }
  429. #endif UNIX