home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / gawk213s.lzh / GAWK213S / VMS_FWRI.C < prev    next >
C/C++ Source or Header  |  1993-07-29  |  8KB  |  210 lines

  1. /*
  2.  * vms_fwrite.c - augmentation for the fwrite() function.
  3.  */
  4.  
  5. /*
  6.  * Copyright (C) 1991 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 1, or (at your option)
  14.  * 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, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  */
  25.  
  26. #include "awk.h"    /* really "../awk.h" */
  27.  
  28. #ifndef NO_TTY_FWRITE
  29. #include "vms.h"
  30. #include <stdio.h>
  31. #include <errno.h>
  32.  
  33. #ifdef VAXC_BUILTINS
  34. #pragma builtins        /* VAXC V3.0 & up */
  35. # define find_c(s,n,c) ((n) - _LOCC((c),(n),(s)))
  36. #else    /*VAXC_BUILTINS*/
  37. static int find_c( const char *s, int n, char c ) {
  38.     register const char *t = (const char *)memchr(s, c, n);
  39.     return (t == 0 ? n : t - s);    /* 0..n-1, or n if not found */
  40. }
  41. #endif    /*VAXC_BUILTINS*/
  42. #define is_stdout(file_no) ((file_no) == 1)    /* fileno(stdout) */
  43. #define is_stderr(file_no) ((file_no) == 2)    /* fileno(stderr) */
  44.  
  45. #define PREFIX_CR  (0x8D << 16) /* leading carriage return */
  46. #define POSTFIX_CR (0x8D << 24) /* trailing carriage return (=> lf/cr) */
  47.  
  48. static short  channel[_NFILE] = {0};
  49. static FILE  *prev_file = 0;
  50. static int    prev_file_num;
  51.  
  52.     /*
  53.      * VAXCRTL's fwrite() seems to flush after every character when
  54.      * writing to a terminal.  This routine is a limited functionality
  55.      * substitute that is *much* faster.  However, calls to fwrite()
  56.      * should not be mixed with other stdio calls to the same file
  57.      * unless fflush() is always called first.    Also, this routine
  58.      * will not detect that a freopen() call has finished with the
  59.      * original terminal; tty_fclose() should be used to close a file.
  60.      */
  61. #ifdef fwrite
  62. # undef fwrite
  63. #endif
  64. /* tty_fwrite() - performance hack for fwrite() to a terminal */
  65. size_t
  66. tty_fwrite( const void *buf, size_t size, size_t number, FILE *file )
  67. {
  68.     static long evfn = -1;
  69.     short chan;
  70.     int file_num, result;
  71.  
  72.     if (!file || !*file)
  73.     return 0 * (errno = EBADF);    /* kludge alert! */
  74.     else if (file == prev_file)
  75.     file_num = prev_file_num;
  76.     else    /* note: VAXCRTL's fileno() is a function, not just a macro */
  77.     prev_file_num = file_num = fileno(file),  prev_file = file;
  78.  
  79.     chan = file_num < _NFILE ? channel[file_num] : -1;
  80.     if (chan == 0) {    /* if not initialized, need to assign a channel */
  81.     if (isatty(file_num) > 0) {    /* isatty: 1=yes, 0=no, -1=problem */
  82.         Dsc  device;
  83.         char devnam[255+1];
  84.         fgetname(file, devnam);            /* get 'file's name */
  85.         device.len = strlen(device.adr = devnam);    /* create descriptor */
  86.         if (vmswork(SYS$ASSIGN(&device, &chan, 0, (Dsc *)0))) {
  87.         /* get an event flag; use #0 if problem */
  88.         if (evfn == -1 && vmsfail(LIB$GET_EF(&evfn)))  evfn = 0;
  89.         } else  chan = 0;        /* $ASSIGN failed */
  90.     }
  91.     /* store channel for later use; -1 => don't repeat failed init attempt */
  92.     channel[file_num] = (chan > 0 ? chan : -1);
  93.     }
  94.     if (chan > 0) {        /* chan > 0 iff 'file' is a terminal */
  95.     struct _iosbw { u_short status, count; u_long rt_kludge; } iosb;
  96.     register u_long sts = 1;
  97.     register char  *pt = (char *)buf;
  98.     register int    offset, pos, count = size * number;
  99.     u_long cc_fmt, io_func = IO$_WRITEVBLK;
  100.     int    extra = 0;
  101.     result = 0;
  102.     if (is_stderr(file_num))    /* if it's SYS$ERROR (stderr)... */
  103.         io_func |= IO$M_CANCTRLO;    /* cancel ^O (resume tty output) */
  104.     while (count > 0) {
  105.         /* special handling for line-feeds to make them be 'newlines' */
  106.         offset = 0;
  107.         if (*pt == '\n') {        /* got at least one leading line-feed */
  108.         cc_fmt = PREFIX_CR,  extra++;    /* precede 1st LF with a CR */
  109.         do  offset++;
  110.             while (offset < count && *(pt + offset) == '\n');
  111.         } else
  112.         cc_fmt = 0;
  113.         /* look for another line-feed; if found, break line there */
  114.         pos = offset + find_c(pt + offset, count - offset, '\n');
  115.         if (pos >= BUFSIZ)    pos = BUFSIZ - 1;   /* throttle quota usage */
  116.         else if (pos < count)  pos++,  cc_fmt |= POSTFIX_CR,  extra++;
  117.         /* wait for previous write, if any, to complete */
  118.         if (pt > (char *)buf) {
  119.         sts = SYS$SYNCH(evfn, &iosb);
  120.         if (vmswork(sts))  sts = iosb.status,  result += iosb.count;
  121.         if (vmsfail(sts))  break;
  122.         }
  123.         /* queue an asynchronous write */
  124.         sts = SYS$QIO(evfn, chan, io_func, &iosb, (u_long (*)())0, 0,
  125.               pt, pos, 0, cc_fmt, 0, 0);
  126.         if (vmsfail(sts))  break;    /*(should never happen)*/
  127.         pt += pos,    count -= pos;
  128.     }
  129.     /* wait for last write to complete */
  130.     if (pt > (char *)buf && vmswork(sts)) {
  131.         sts = SYS$SYNCH(evfn, &iosb);
  132.         if (vmswork(sts))  sts = iosb.status,  result += iosb.count;
  133.     }
  134.     if (vmsfail(sts))  errno = EVMSERR,  vaxc$errno = sts;
  135.     else if (iosb.rt_kludge == 0)  result = number + extra;
  136.     result -= extra;    /* subtract the additional carriage-returns */
  137.     } else {                /* use stdio */
  138.     /* Note: we assume that we're writing text, not binary data.
  139.        For stream format files, 'size' and 'number' are effectively
  140.        interchangable, and fwrite works fine.  However, for record
  141.        format files, 'size' governs the maximum record length, so
  142.         fwrite(string, size(char), strlen(string), file)
  143.        will produce a sequence of 1-byte records, which is hardly
  144.        what we want in this (assumed) situation.  Line-feeds ('\n')
  145.        are converted into newlines (ie, record separators) by the
  146.        run-time library, but strings that don't end with a newline
  147.        still become separate records.  The simplest work around
  148.        is just to use fputs() instead of fwrite(); unfortunately,
  149.        we have to provide special treatment for NULs ('\0's).
  150.        At present, only stdout might be in record format (via
  151.        >$'filename' redirection on the command line).
  152.     */
  153.     if (size > 1) {        /* not used by GAWK */
  154.         result = fwrite((void *)buf, size, number, file);
  155.     } else if (*((char *)buf + number - 1) == '\n' || !is_stdout(file_num)) {
  156.         result = fwrite((void *)buf, number, size, file);
  157.         result = result * number / size;    /*(same as 'result = number')*/
  158.     } else {
  159. #ifdef NO_ALLOCA
  160. # define alloca(n) ((n) <= abuf_siz ? abuf : \
  161.             (abuf_siz > 0 ? (void *)free(abuf) : (void *)0), \
  162.             (abuf = malloc(abuf_siz = (n)+20)))
  163.         static void *abuf = 0;
  164.         static size_t abuf_siz = 0;
  165. #endif /*NO_ALLOCA*/
  166.         register char *pt = (char *)buf;
  167.         register int   pos,  count = number;
  168.         if (pt[count] != '\0') {    /*(out of bounds, but relatively safe)*/
  169.         pt = (char *)alloca(count + 1);
  170.         memcpy(pt, buf, count),  pt[count] = '\0';
  171.         /* if exiting this block undoes the alloca(), we're hosed :-( */
  172.         }
  173.         result = 0;
  174.         while (count > 0) {
  175.         pos = find_c(pt, count, '\0');
  176.         if (fputs(pt, file) < 0)  break;
  177.         if (pos < count) {
  178.             if (fputc('\0', file) < 0)    break;
  179.             pos++;        /* 0..n-1 -> 1..n */
  180.         }
  181.         result += pos,    pt += pos,  count -= pos;
  182.         }
  183.     }
  184.     }
  185.     return result;
  186. }
  187. #define fwrite(b,s,n,f) tty_fwrite((b),(s),(n),(f))
  188.  
  189. #ifdef fclose
  190. # undef fclose
  191. #endif
  192. /* tty_fclose() - keep tty_fwrite() up to date when closing a file */
  193. int
  194. tty_fclose( FILE *file )
  195. {
  196.     if (file && *file) {  /* note: VAXCRTL stdio has extra level of indirection */
  197.     int   file_num = fileno(file);
  198.     short chan = file_num < _NFILE ? channel[file_num] : -1;
  199.     if (chan > 0)
  200.         (void)SYS$DASSGN(chan); /* deassign the channel (ie, close) */
  201.     if (file_num < _NFILE)
  202.         channel[file_num] = 0;  /* clear stale info */
  203.     }
  204.     prev_file = 0;            /* force tty_fwrite() to reset */
  205.     return fclose(file);
  206. }
  207. #define fclose(f) tty_fclose(f)
  208.  
  209. #endif    /*!NO_TTY_FWRITE*/
  210.