home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / rvi / part2 / rv_init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  4.4 KB  |  219 lines

  1. #include "rv.h"
  2. #include <signal.h>
  3.  
  4. #ifdef CURSHDR
  5. #include "curshdr.h"
  6. #endif
  7.  
  8. extern char editlist[256]; /* List of files to edit */
  9.  
  10. void
  11. ed_init()
  12. /*
  13.  * Initialize access to ed
  14.  */
  15. {
  16.     INT in, out;
  17.     int pipebroke(), alarmring();
  18.     boolean synced = 0;
  19.     char buf[128];
  20.  
  21.     alarm(RV_TIMEOUT);
  22.     (void) signal(SIGPIPE, pipebroke);
  23.     (void) signal(SIGALRM, alarmring);
  24.     /*
  25.      * Open pipe streams to ed
  26.      */
  27.     if ((in = atoi(Argv[1])) <= 0 || (out = atoi(Argv[2])) <= 0)
  28.         panic("Bad pipe descriptors\n\n");
  29.     if ((file.fi_fpin  = fdopen(in , "r")) == NULL ||
  30.         (file.fi_fpout = fdopen(out, "w")) == NULL)
  31.         panic("Bad fdopen on pipe stream\n\n");
  32.  
  33.     /*
  34.      * All versions of ed are not alike.  In particular the format
  35.      * of error messages widely varies between systems.  (See the
  36.      * 'H' command in System V).
  37.      *
  38.      * Rvi supports three versions of ed: System V, 4bsd, and Cray.
  39.      * This piece of code attempts to intuit which version of ed we
  40.      * have here.
  41.      */
  42.  
  43.     if (fputs("zzz\n$=\nH\n$=\n", file.fi_fpout) == NULL)
  44.         panic("Cannot write to ed\n\n");
  45.     fflush(file.fi_fpout);
  46.  
  47.     for (;;) {
  48.         if (fgets(buf, 126, file.fi_fpin) == NULL)
  49.             panic("Cannot read from ed\n\n");
  50.         if (set_debug > 1)
  51.             fprintf(stderr, "1 %s", buf);
  52.         if (synced) {
  53.             if (buf[0] != '?') {
  54.                 /*
  55.                  * If the H command is supported
  56.                  */
  57.                 file.fi_sysv = TRUE;
  58.                 /*
  59.                  * The H command is a toggle.  Force it "on"
  60.                  * if we just toggled it off.
  61.                  */
  62.                 if (atoi(buf) != 0 || buf[0] == '0')
  63.                     fputs("H\n", file.fi_fpout);
  64.             }
  65.             break;
  66.         }
  67.         if (atoi(buf) != 0 || buf[0] == '0')
  68.             synced = TRUE;
  69.     }
  70.  
  71.     /* One line of output waiting to be read at this point */
  72.  
  73.     if (set_debug > 1)
  74.         fprintf(stderr, "sysv=%d\n", file.fi_sysv);
  75.  
  76.     if (file.fi_sysv) {
  77.         /*
  78.          * The Cray version of ed is based on SystemV, however
  79.          * the format of error messages generated by the 'H' command
  80.          * are not compatible.
  81.          *
  82.          *
  83.          * System V format:
  84.          *
  85.          * ?
  86.          * error message
  87.          *
  88.          *
  89.          * Cray format:
  90.          *
  91.          * ? error message
  92.          *
  93.          */
  94.         fputs("zzz\nf\n", file.fi_fpout);
  95.         fflush(file.fi_fpout);
  96.         (void) fgets(buf, 126, file.fi_fpin);
  97.         if (set_debug > 1)
  98.             fprintf(stderr, "2 %s", buf);
  99.         /*
  100.          * Skip last 'H' response
  101.          */
  102.         if (fgets(buf, 126, file.fi_fpin) == NULL)
  103.             panic("Cannot read from ed\n\n");
  104.         if (set_debug > 1)
  105.             fprintf(stderr, "3 %s", buf);
  106.         if (buf[0] != '?')
  107.             panic("Sync problem in ed_init\n\n");
  108.         if (buf[1] != '\n')
  109.             file.fi_cray = TRUE;
  110.         else {
  111.             (void) fgets(buf, 126, file.fi_fpin);
  112.             if (set_debug > 1)
  113.                 fprintf(stderr, "4 %s", buf);
  114.         }
  115.     } else { /* 4.2bsd */
  116.         fputs("f\n", file.fi_fpout);
  117.         fflush(file.fi_fpout);
  118.         (void) fgets(buf, 126, file.fi_fpin);
  119.         if (set_debug > 1)
  120.             fprintf(stderr, "5 %s", buf);
  121.     }
  122.  
  123.     if (set_debug > 1) 
  124.         fprintf(stderr, "cray=%d\n", file.fi_cray);
  125.     /*
  126.      * Get current file name (used in syncs)
  127.      */
  128.     if (fgets(buf, 126, file.fi_fpin) == NULL)
  129.         panic("Cannot read from ed\n\n");
  130.     buf[strlen(buf)-1] = '\0'; /* Strip trailing \n */
  131.     if (buf[0] == '\0' || buf[0] == ' ') { /* If no file name */
  132.         strcpy(buf, "/dev/null");
  133.         fputs("f /dev/null\n", file.fi_fpout);
  134.     }
  135.     if (set_debug > 1)
  136.         fprintf(stderr, "name: %s.\n", buf);
  137.     strcpy(file.fi_name, buf);
  138.     
  139.     alarm(0);
  140.     signal(SIGINT, quit);
  141.     nextfile = editlist;
  142.     if (editlist[0] != '\0')
  143.         edit(NULL);
  144.     else {
  145.         fetch_window(1, TRUE);
  146.         sizemsg();
  147.     }
  148. }
  149.  
  150. pipebroke()
  151. {
  152.     panic("Broken pipe\n\n");
  153.     /*NOTREACHED*/
  154. }
  155.  
  156. alarmring()
  157. {
  158.     panic("Connection timed out - ed now has control.\n\n");
  159.     /*NOTREACHED*/
  160. }
  161.     
  162.  
  163. initialize()
  164. {
  165.     char *s;
  166.     extern char *getenv();
  167.  
  168.     initscr();
  169.     noecho();
  170. #ifdef CRAY
  171.     nl();
  172. #else
  173.     nonl();
  174. #endif
  175.  
  176.     scrollok(stdscr, TRUE);
  177.     scrollok(curscr, TRUE);
  178.  
  179. #ifdef USG
  180.     idlok(stdscr, TRUE);
  181.     idlok(curscr, TRUE);
  182.     cbreak();
  183.     keypad(stdscr, set_timeout ? 1 : 2);
  184.  
  185. # ifdef CURSHDR
  186.     /*
  187.      * Kludge - force VT100 terminals to emulate insert/delete line
  188.      * with scrolling windows.  I think this looks better.  AEK
  189.      */
  190.     if (SP->term_costs.ilfixed < INFINITY && SP->term_costs.ilfixed > 10)
  191.         SP->term_costs.ilfixed = 10;
  192. # endif CURSHDR
  193. #else
  194.     crmode();
  195. #endif !USG
  196.  
  197.     /*
  198.      * Allocate the window buffer
  199.      */
  200.     nwin_lines = LINES*3+4;
  201.     line_array = (struct li_line *) xalloc(sizeof(struct li_line) * 
  202.         (nwin_lines + 6));
  203.     /*
  204.      * Kludge: leave two lines unused below line_addr so that pointer
  205.      * comparisons on segmented architectures (e.g. iAPX286) wont
  206.      * wrap around to 0xffff
  207.      */
  208.     line_array += 2;
  209.  
  210.     rv_finit();
  211.  
  212.     set_scroll = LINES/2;
  213.     clear();
  214.     ed_init();
  215.     if ((s = getenv("RVINIT")) != NULL)
  216.         rv_linecmd(s);
  217.     refresh();
  218. }
  219.