home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / libq / IIingres.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-18  |  4.0 KB  |  191 lines

  1. # include    <ingres.h>
  2. # include    <symbol.h>
  3. # include    <opsys.h>
  4. # include    <aux.h>
  5. # include    "IIglobals.h"
  6. # include    <pipes.h>
  7. # include    <signal.h>
  8.  
  9. # undef        CLOSED
  10. # define    CLOSED    '?'
  11.  
  12.  
  13. char    *IImainpr =    "ingres";
  14.  
  15. char    IIPathname[41];
  16.  
  17. /*
  18. **    IIingres opens the needed pipes and
  19. **    forks an ingres process.
  20. **
  21. **    ingres recognizes the EQUEL flag followed by
  22. **    three control characters as being an equel processes
  23. **
  24. **    parameters to ingres are passed directly. only
  25. **    the first 9 are counted.
  26. */
  27.  
  28. IIingres(p1, p2, p3, p4, p5, p6, p7, p8, p9)
  29. char    *p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9;
  30. {
  31.     int        pipes[4];        /* pipe vector buffer */
  32.     char        eoption[15];    /* dummy variable to hold -EQUEL option */
  33.     register char    *cp;
  34.     char        *argv[12];
  35.     register char    **ap;
  36.     extern void    IIresync();
  37.     extern void    *(IIinterrupt)();
  38.     pb_t        pb;
  39.  
  40. #    ifdef xETR1
  41.     if (IIdebug)
  42.         printf("IIingres\n");
  43. #    endif
  44.     /* test if ingres is already invoked */
  45.     if (IIingpid)
  46.         IIsyserr("Attempt to invoke INGRES twice");
  47.  
  48.     IIgetpath();
  49.     /* open INGRES pipes */
  50.     if (pipe(&pipes[0]) || pipe(&pipes[2]))
  51.         IIsyserr("pipe error in IIingres");
  52.  
  53.     IIw_down = pipes[1];    /* file desc for equel->parser */
  54.     IIr_down = pipes[2];    /* file desc for parser->equel */
  55.  
  56.     /* catch interupts if they are not being ignored */
  57.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  58.         signal(SIGINT, IIresync);
  59.  
  60.     /* set up equel option flag */
  61.     cp = eoption;
  62.     *cp++ = '-';
  63.     *cp++ = EQUEL;
  64.     *cp++ = pipes[0] | 0100;
  65.     *cp++ = pipes[1] | 0100;
  66.     *cp++ = CLOSED;
  67.     *cp++ = pipes[3] | 0100;
  68.     *cp++ = CLOSED;    /* this will be the equel->ovqp pipe in the future */
  69.     *cp++ = CLOSED;    /* old ovqp->equel pipe */
  70.     /* put "6.3" at end of flag for OVQP to not do flush after
  71.      * every tuple
  72.      */
  73.     *cp++ = '6';
  74.     *cp++ = '.';
  75.     *cp++ = '3';
  76.     *cp = '\0';
  77.     if (cp - eoption >= sizeof eoption)
  78.         IIsyserr("IIingres: too big an eoption");
  79.  
  80.     if ((IIingpid = fork()) < 0)
  81.         IIsyserr("IIingres:cant fork %d", IIingpid);
  82.     /* if parent, close the unneeded files and return */
  83.     if (IIingpid)
  84.     {
  85.         if (close(pipes[0]) || close(pipes[3]))
  86.             IIsyserr("close error 1 in IIingres");
  87.         IIinput = IIr_down;
  88.         IIpb_prime(&pb, PB_NOTYPE);
  89. #        ifdef xETR1
  90.         if (IIdebug)
  91.             printf("calling ingres with '%s' database %s\n", eoption, p1);
  92. #        endif
  93.         return;
  94.     }
  95.     /* the child overlays /usr/bin/ingres */
  96.     ap = argv;
  97.     *ap++ = "ingres";
  98.     *ap++ = eoption;
  99.     *ap++ = p1;
  100.     *ap++ = p2;
  101.     *ap++ = p3;
  102.     *ap++ = p4;
  103.     *ap++ = p5;
  104.     *ap++ = p6;
  105.     *ap++ = p7;
  106.     *ap++ = p8;
  107.     *ap++ = p9;
  108.     *ap = 0;
  109.     execvp(IImainpr, argv);
  110.     IIsyserr("cannot exec INGRES in IIingres");
  111. }
  112.  
  113. /*
  114. **  IIGETPATH -- initialize the IIPathname variable
  115. **
  116. **    Parameters:
  117. **        none
  118. **
  119. **    Returns:
  120. **        none
  121. **
  122. **    Side Effects:
  123. **        Sets IIPathname to the home directory of the USERINGRES
  124. **        [unix.h] user.
  125. **
  126. **    Called By:
  127. **        IIingres.c
  128. **
  129. **    History:
  130. **        3/26/79 -- (marc) written
  131. */
  132.  
  133. IIgetpath()
  134. {
  135.     char            line[MAXLINE + 1];
  136.     static char        reenter;
  137.     register int        i;
  138.     register char        *lp;
  139.     struct iob        iobuf;
  140.     char            *field[UF_NFIELDS];
  141.  
  142.     if (reenter)
  143.         return;
  144.     else
  145.         reenter++;
  146.  
  147.     if ((i = IIfopen("/etc/passwd", &iobuf)) < 0)
  148.         IIsyserr("IIgetpath: no /etc/passwd");
  149.  
  150.     do
  151.     {
  152.         /* get a line from the passwd file */
  153.         i = 0;
  154.         for (lp = line; (*lp = IIgetc(&iobuf)) != '\n'; lp++)
  155.         {
  156.             if (*lp == -1)
  157.                 IIsyserr("IIgetpath: no user 'ingres' in /etc/passwd");
  158.             if (++i > sizeof line - 1)
  159.             {
  160.                 *lp = '\0';
  161.                 IIsyserr("IIgetpath: line overflow: \"%s\"",
  162.                 line);
  163.             }
  164.         }
  165.         *lp = '\0';
  166.         for (i = 0, lp = line; *lp != '\0'; lp++)
  167.         {
  168.             if (*lp == ':')
  169.             {
  170.                 *lp = '\0';
  171.                 if (i > UF_NFIELDS)
  172.                     IIsyserr("IIgetpath: too many fields in passwd \"%s\"",
  173.                     line);
  174.                 field[i++] = lp + 1;
  175.             }
  176.         }
  177.         /* check for enough fields for valid entry */
  178.         if (i < 3)
  179.             IIsyserr("IIgetpath: too few fields \"%s\"",
  180.             line);
  181.     }  while (!IIsequal(line, USERINGRES));
  182.     IIclose(&iobuf);
  183.  
  184.     /* check that pathname won't overflow static buffer */
  185.     if (field[i - 1] - field[i - 2] > sizeof IIPathname)
  186.         IIsyserr("IIgetpath: path too long \"%s\"", field[i - 2]);
  187.  
  188.     /* move pathname into buffer */
  189.     IIbmove(field[i - 2], IIPathname, field[i - 1] - field[i - 2]);
  190. }
  191.