home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / GAWKNT.ZIP / SRC.ZIP / IOP.C < prev    next >
C/C++ Source or Header  |  1994-03-07  |  8KB  |  316 lines

  1. /*
  2.  * iop.c - do i/o related things.
  3.  */
  4.  
  5. /* 
  6.  * Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993 the Free Software Foundation, Inc.
  7.  * 
  8.  * This file is part of GAWK, the GNU implementation of the
  9.  * AWK Progamming Language.
  10.  * 
  11.  * GAWK is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  * 
  16.  * GAWK is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  * 
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with GAWK; see the file COPYING.  If not, write to
  23.  * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  */
  25.  
  26. #include "awk.h"
  27.  
  28. #ifndef atarist
  29. #define INVALID_HANDLE (-1)
  30. #else
  31. #include <stddef.h>
  32. #include <fcntl.h>
  33. #define INVALID_HANDLE  (__SMALLEST_VALID_HANDLE - 1)
  34. #endif  /* atarist */
  35.  
  36.  
  37. #ifdef TEST
  38. int bufsize = 8192;
  39.  
  40. void
  41. fatal(s)
  42. char *s;
  43. {
  44.     printf("%s\n", s);
  45.     exit(1);
  46. }
  47. #endif
  48.  
  49. int
  50. optimal_bufsize(fd)
  51. int fd;
  52. {
  53.     struct stat stb;
  54.  
  55. #ifdef VMS
  56.     /*
  57.      * These values correspond with the RMS multi-block count used by
  58.      * vms_open() in vms/vms_misc.c.
  59.      */
  60.     if (isatty(fd) > 0)
  61.         return BUFSIZ;
  62.     else if (fstat(fd, &stb) < 0)
  63.         return 8*512;    /* conservative in case of DECnet access */
  64.     else
  65.         return 32*512;
  66.  
  67. #else
  68.     /*
  69.      * System V doesn't have the file system block size in the
  70.      * stat structure. So we have to make some sort of reasonable
  71.      * guess. We use stdio's BUFSIZ, since that is what it was
  72.      * meant for in the first place.
  73.      */
  74. #ifdef BLKSIZE_MISSING
  75. #define    DEFBLKSIZE    BUFSIZ
  76. #else
  77. #define DEFBLKSIZE    (stb.st_blksize ? stb.st_blksize : BUFSIZ)
  78. #endif
  79.  
  80. #ifdef TEST
  81.     return bufsize;
  82. #else
  83. #ifndef atarist
  84.     if (isatty(fd))
  85. #else
  86.     /*
  87.      * On ST redirected stdin does not have a name attached
  88.      * (this could be hard to do to) and fstat would fail
  89.      */
  90.     if (0 == fd || isatty(fd))
  91. #endif  /*atarist */
  92.         return BUFSIZ;
  93. #ifndef BLKSIZE_MISSING
  94.     /* VMS POSIX 1.0: st_blksize is never assigned a value, so zero it */
  95.     stb.st_blksize = 0;
  96. #endif
  97.     if (fstat(fd, &stb) == -1)
  98.         fatal("can't stat fd %d (%s)", fd, strerror(errno));
  99.     if (lseek(fd, (off_t)0, 0) == -1)
  100.         return DEFBLKSIZE;
  101.     return ((int) (stb.st_size < DEFBLKSIZE ? stb.st_size : DEFBLKSIZE));
  102. #endif    /*! TEST */
  103. #endif    /*! VMS */
  104. }
  105.  
  106. IOBUF *
  107. iop_alloc(fd)
  108. int fd;
  109. {
  110.     IOBUF *iop;
  111.  
  112.     if (fd == INVALID_HANDLE)
  113.         return NULL;
  114.     emalloc(iop, IOBUF *, sizeof(IOBUF), "iop_alloc");
  115.     iop->flag = 0;
  116.     if (isatty(fd))
  117.         iop->flag |= IOP_IS_TTY;
  118.     iop->size = optimal_bufsize(fd);
  119.     iop->secsiz = -2;
  120.     errno = 0;
  121.     iop->fd = fd;
  122.     iop->off = iop->buf = NULL;
  123.     iop->cnt = 0;
  124.     return iop;
  125. }
  126.  
  127. /*
  128.  * Get the next record.  Uses a "split buffer" where the latter part is
  129.  * the normal read buffer and the head part is an "overflow" area that is used
  130.  * when a record spans the end of the normal buffer, in which case the first
  131.  * part of the record is copied into the overflow area just before the
  132.  * normal buffer.  Thus, the eventual full record can be returned as a
  133.  * contiguous area of memory with a minimum of copying.  The overflow area
  134.  * is expanded as needed, so that records are unlimited in length.
  135.  * We also mark both the end of the buffer and the end of the read() with
  136.  * a sentinel character (the current record separator) so that the inside
  137.  * loop can run as a single test.
  138.  */
  139. int
  140. get_a_record(out, iop, grRS, errcode)
  141. char **out;
  142. IOBUF *iop;
  143. register int grRS;
  144. int *errcode;
  145. {
  146.     register char *bp = iop->off;
  147.     char *bufend;
  148.     char *start = iop->off;            /* beginning of record */
  149.     char rs;
  150.     int saw_newline = 0, eat_whitespace = 0;    /* used iff grRS==0 */
  151.  
  152.     if (iop->cnt == EOF)    /* previous read hit EOF */
  153.         return EOF;
  154.  
  155.     if (grRS == 0) {    /* special case:  grRS == "" */
  156.         rs = '\n';
  157.     } else
  158.         rs = (char) grRS;
  159.  
  160.     /* set up sentinel */
  161.     if (iop->buf) {
  162.         bufend = iop->buf + iop->size + iop->secsiz;
  163.         *bufend = rs;
  164.     } else
  165.         bufend = NULL;
  166.  
  167.     for (;;) {    /* break on end of record, read error or EOF */
  168.  
  169.         /* Following code is entered on the first call of this routine
  170.          * for a new iop, or when we scan to the end of the buffer.
  171.          * In the latter case, we copy the current partial record to
  172.          * the space preceding the normal read buffer.  If necessary,
  173.          * we expand this space.  This is done so that we can return
  174.          * the record as a contiguous area of memory.
  175.          */
  176.         if ((iop->flag & IOP_IS_INTERNAL) == 0 && bp >= bufend) {
  177.             char *oldbuf = NULL;
  178.             char *oldsplit = iop->buf + iop->secsiz;
  179.             long len;    /* record length so far */
  180.  
  181.             if ((iop->flag & IOP_IS_INTERNAL) != 0)
  182.                 cant_happen();
  183.  
  184.             len = bp - start;
  185.             if (len > iop->secsiz) {
  186.                 /* expand secondary buffer */
  187.                 if (iop->secsiz == -2)
  188.                     iop->secsiz = 256;
  189.                 while (len > iop->secsiz)
  190.                     iop->secsiz *= 2;
  191.                 oldbuf = iop->buf;
  192.                 emalloc(iop->buf, char *,
  193.                     iop->size+iop->secsiz+2, "get_a_record");
  194.                 bufend = iop->buf + iop->size + iop->secsiz;
  195.                 *bufend = rs;
  196.             }
  197.             if (len > 0) {
  198.                 char *newsplit = iop->buf + iop->secsiz;
  199.  
  200.                 if (start < oldsplit) {
  201.                     memcpy(newsplit - len, start,
  202.                             oldsplit - start);
  203.                     memcpy(newsplit - (bp - oldsplit),
  204.                             oldsplit, bp - oldsplit);
  205.                 } else
  206.                     memcpy(newsplit - len, start, len);
  207.             }
  208.             bp = iop->end = iop->off = iop->buf + iop->secsiz;
  209.             start = bp - len;
  210.             if (oldbuf) {
  211.                 free(oldbuf);
  212.                 oldbuf = NULL;
  213.             }
  214.         }
  215.         /* Following code is entered whenever we have no more data to
  216.          * scan.  In most cases this will read into the beginning of
  217.          * the main buffer, but in some cases (terminal, pipe etc.)
  218.          * we may be doing smallish reads into more advanced positions.
  219.          */
  220.         if (bp >= iop->end) {
  221.             if ((iop->flag & IOP_IS_INTERNAL) != 0) {
  222.                 iop->cnt = EOF;
  223.                 break;
  224.             }
  225.             iop->cnt = read(iop->fd, iop->end, bufend - iop->end);
  226.             if (iop->cnt == -1) {
  227.                 if (! do_unix && errcode != NULL) {
  228.                     *errcode = errno;
  229.                     iop->cnt = EOF;
  230.                     break;
  231.                 } else
  232.                     fatal("error reading input: %s",
  233.                         strerror(errno));
  234.             } else if (iop->cnt == 0) {
  235.                 iop->cnt = EOF;
  236.                 break;
  237.             }
  238.             iop->end += iop->cnt;
  239.             *iop->end = rs;
  240.         }
  241.         if (grRS == 0) {
  242.             extern int default_FS;
  243.  
  244.             if (default_FS && (bp == start || eat_whitespace)) {
  245.                 while (bp < iop->end && isspace(*bp))
  246.                     bp++;
  247.                 if (bp == iop->end) {
  248.                     eat_whitespace = 1;
  249.                     continue;
  250.                 } else
  251.                     eat_whitespace = 0;
  252.             }
  253.             if (saw_newline && *bp == rs) {
  254.                 bp++;
  255.                 break;
  256.             }
  257.             saw_newline = 0;
  258.         }
  259.  
  260.         while (*bp++ != rs)
  261.             ;
  262.  
  263.         if (bp <= iop->end) {
  264.             if (grRS == 0)
  265.                 saw_newline = 1;
  266.             else
  267.                 break;
  268.         } else
  269.             bp--;
  270.  
  271.         if ((iop->flag & IOP_IS_INTERNAL) != 0)
  272.             iop->cnt = bp - start;
  273.     }
  274.     if (iop->cnt == EOF
  275.         && (((iop->flag & IOP_IS_INTERNAL) != 0) || start == bp))
  276.         return EOF;
  277.  
  278.     iop->off = bp;
  279.     bp--;
  280.     if (*bp != rs)
  281.         bp++;
  282.     *bp = '\0';
  283.     if (grRS == 0) {
  284.         if (*--bp == rs)
  285.             *bp = '\0';
  286.         else
  287.             bp++;
  288.     }
  289.  
  290.     *out = start;
  291.     return bp - start;
  292. }
  293.  
  294. #ifdef TEST
  295. main(argc, argv)
  296. int argc;
  297. char *argv[];
  298. {
  299.     IOBUF *iop;
  300.     char *out;
  301.     int cnt;
  302.     char rs[2];
  303.  
  304.     rs[0] = 0;
  305.     if (argc > 1)
  306.         bufsize = atoi(argv[1]);
  307.     if (argc > 2)
  308.         rs[0] = *argv[2];
  309.     iop = iop_alloc(0);
  310.     while ((cnt = get_a_record(&out, iop, rs[0], NULL)) > 0) {
  311.         fwrite(out, 1, cnt, stdout);
  312.         fwrite(rs, 1, 1, stdout);
  313.     }
  314. }
  315. #endif
  316.