home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / dial / d_script2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-28  |  3.8 KB  |  185 lines

  1. #
  2. # include  <stdio.h>
  3. # include  "d_proto.h"
  4. # include  "d_returns.h"
  5. # include  "d_structs.h"
  6.  
  7. # define    MAXFILES    15
  8.  
  9. extern char *strcpy ();
  10. extern char *strdup ();
  11. extern unsigned d_scline;
  12. extern char d_scfile[];
  13. extern int d_nfields;
  14. extern char *d_fields[];
  15. extern int errno;
  16.  
  17. FILE  *d_scfp;            /*  file pointer for script file  */
  18. static struct opfile *d_files[MAXFILES];
  19. static int d_nopen;
  20.  
  21. /*
  22.  *     D_SCOPEN
  23.  *
  24.  *     this routine opens the script file and sets up a few globals for
  25.  *     use by later routines.
  26.  *
  27.  *     scriptfile -- path name of the script file to be used.
  28. */
  29.  
  30. d_scopen (scriptfile, nfields, fields)
  31.   int nfields;
  32.   char scriptfile[], *fields[];
  33.     {
  34.     char *calloc();
  35.     register int index, i;
  36.  
  37.     /*  Don't let them open so many files that they overrun my array  */
  38.     if (d_nopen > MAXFILES)
  39.     {
  40. #ifdef D_LOG
  41.     d_log ("d_scopen", "Too many files opened");
  42. #endif D_LOG
  43.     return (D_FATAL);
  44.     }
  45.  
  46.     /*  If this is any other than the first file to be opened,
  47.      *  then I alloc space for it and save status info.
  48.      */
  49.     if (d_nopen != 0)
  50.     {
  51.     index = d_nopen - 1;
  52.     /*NOSTRICT*/
  53.     d_files[index] = (struct opfile *)calloc (1, sizeof (struct opfile));
  54.     if (d_files[index] == 0)
  55.     {
  56.         d_log ("d_scopen", "couldn't alloc space for struct");
  57.         return (D_FATAL);
  58.     }
  59.     d_files[index]->o_line = d_scline;
  60.     d_files[index]->o_chan = d_scfp;
  61.     (void) strcpy (d_files[index]->o_fname, d_scfile);
  62.     d_files[index]->o_nfields = d_nfields;
  63.     for (i=0; i < d_nfields; i++)
  64.         d_files[index]->o_fields[i] = d_fields[i];
  65.     for (i=0; i < nfields; i++)
  66.         d_fields[i] = strdup(fields[i+1]);
  67.     d_nfields = nfields-1;
  68.  
  69.     }
  70.     d_nopen++;
  71.  
  72.     if ((d_scfp = fopen(scriptfile, "r")) == NULL)
  73.     {
  74. #ifdef D_LOG
  75.     d_log ("d_scopen", "Can't open script file '%s' (errno %d)",
  76.                         scriptfile, errno);
  77. #endif D_LOG
  78.     /*  restore the old file setup incase this was part of a failed
  79.      *  alternate.
  80.      */
  81.     d_scclose();
  82.     return (D_FATAL);
  83.     }
  84.  
  85.     d_scline = 0;
  86.     (void) strcpy (d_scfile, scriptfile);
  87.  
  88. #ifdef D_LOG
  89.     d_log ("d_scopen", "script file '%s' being used", scriptfile);
  90. #endif D_LOG
  91.  
  92.     return (D_OK);
  93. }
  94.  
  95. d_scclose()
  96.     {
  97.     register int index, i;
  98.  
  99.     if (d_nopen <= 0)
  100.     return (0);
  101.  
  102.  
  103.     if (d_scfp > 0)
  104.     fclose (d_scfp);
  105. #ifdef D_LOG
  106.     d_log ("d_scclose", "Closing script file '%s'", d_scfile);
  107. #endif D_LOG
  108.     for (i=0; i < d_nfields; i++)
  109.     free(d_fields[i]);
  110.  
  111.     if (--d_nopen > 0)
  112.     {
  113.     index = d_nopen - 1;
  114.     d_scfp = d_files[index]->o_chan;
  115.     (void) strcpy (d_scfile, d_files[index]->o_fname);
  116.     d_scline = d_files[index]->o_line;
  117.     d_nfields = d_files[index]->o_nfields;
  118.     for (i = 0; i < d_nfields; i++)
  119.         d_fields[i] = d_files[index]->o_fields[i];
  120.     free (d_files[index]);
  121. #ifdef D_LOG
  122.     d_log ("d_scclose", "Resuming use of script file '%s'", d_scfile);
  123. #endif D_LOG
  124.     return (d_nopen);
  125.     }
  126.  
  127.     return (0);
  128. }
  129.  
  130.  
  131.  
  132. /*
  133.  *     D_SCRGETLINE
  134.  *
  135.  *     this routine is called to read a line in from the script file.  it
  136.  *     handles the possibility of an escaped newline.
  137.  *
  138.  *     linebuf -- place to load the line
  139.  */
  140.  
  141. d_scgetline(linebuf, channel)
  142.   char  *linebuf;
  143.   FILE *channel;
  144.     {
  145.     register int  c, count;
  146.     register char *cp;
  147.  
  148.     count = 0;
  149.     cp = linebuf;
  150.     d_scline++;
  151.     while ((c = getc(channel)) != EOF)
  152.     {
  153.     if (c == '\n')
  154.     {                   /*  if newline, see if it's escaped  */
  155.         if ((cp > linebuf) && (cp[-1] == '\\'))
  156.         {
  157.         cp--;
  158.         count--;
  159.         d_scline++;
  160.             continue;
  161.         }
  162.         if (count == 0) {  /* skip past empty lines */
  163.         d_scline++;
  164.         continue;
  165.         }
  166.         break;
  167.         }
  168.  
  169.     if (++count > MAXSCRLINE)
  170.     {                   /*  line is too long */
  171.         d_scerr("line too long");
  172.         return(D_FATAL);
  173.         }
  174.  
  175.     *cp++ = c;
  176.     }
  177.  
  178.     /*  The line parsing routine actually needs two nulls at the
  179.      *  end to guarentee that it doesn't skip it.
  180.      */
  181.     *cp++ = '\0';
  182.     *cp = '\0';
  183.     return(count);
  184. }
  185.