<commit_before>/* 
 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
 *
 * $Id: setBuffering.c,v 1.3 1998/12/02 13:27:54 simonm Exp $
 *
 * hSetBuffering Runtime Support
 */

#include "Rts.h"
#include "stgio.h"

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

#define SB_NB (0)
#define SB_LB (-1)
#define SB_BB (-2)

StgInt
setBuffering(ptr, size)
StgForeignPtr ptr;
StgInt size;
{
    IOFileObject* fo = (IOFileObject*)ptr;
    int flags, rc=0;
    int input, isaterm;
    struct termios tio;
    struct stat sb;
   

    /* First off, flush old buffer.. */
    if ( (fo->flags & FILEOBJ_FLUSH) ) {
       rc = flushBuffer(ptr);
    }
    if (rc<0) return rc;

    /* Let go of old buffer, and reset buffer pointers. */
    if ( fo->buf != NULL ) {
       free(fo->buf);
       fo->bufWPtr = 0;
       fo->bufRPtr = 0;
       fo->bufSize = 0;
       fo->buf     = NULL;
    }

    while ((flags = fcntl(fo->fd, F_GETFL)) < 0) {
	if (errno != EINTR) {
	    cvtErrno();
	    stdErrno();
	    return -1;
	}
    }
    flags &= O_ACCMODE;
    input = flags == O_RDONLY || flags == O_RDWR;

    isaterm = input && isatty(fo->fd);

    switch (size) {
    case SB_NB:
        fo->flags &= ~FILEOBJ_LB & ~FILEOBJ_BB;

	if (isaterm) {
	    /* Switch over to canonical mode. */
	    if (tcgetattr(fo->fd, &tio) < 0) {
		cvtErrno();
		stdErrno();
		return -1;
	    }
	    tio.c_lflag &=  ~ICANON;
	    tio.c_cc[VMIN] = 1;
	    tio.c_cc[VTIME] = 0;
	    if (tcsetattr(fo->fd, TCSANOW, &tio) < 0) {
		cvtErrno();
		stdErrno();
		return -1;
	    }
	}
	return 0;
    case SB_LB:
        fo->flags &= ~FILEOBJ_BB;
	fo->flags |= FILEOBJ_LB;
        size = BUFSIZ;
	break;
    case SB_BB:

#if HAVE_ST_BLKSIZE
	while (fstat(fo->fd, &sb) < 0) {
	   /* not very likely.. */
	   if ( errno != EINTR ) {
	      cvtErrno();
	      stdErrno();
	      return -1;
	   }
        }
	size = sb.st_blksize;
#else
	size = BUFSIZ;
#endif
        fo->flags &= ~FILEOBJ_LB;
	fo->flags |= FILEOBJ_BB;
	/* fall through */
    default:
	break;
    }
  
    if ( size > 0) {
       fo->buf = malloc(size*sizeof(char));
       if (fo->buf == NULL) {
           return -1;
       }
       fo->bufSize = size;
    }
    if (isaterm) {

	/*
	 * Try to switch back to cooked mode.
	 */

	if (tcgetattr(fo->fd, &tio) < 0) {
	    cvtErrno();
	    stdErrno();
	    return -1;
	}
	tio.c_lflag |= ICANON;
	if (tcsetattr(fo->fd, TCSANOW, &tio) < 0) {
	    cvtErrno();
	    stdErrno();
	    return -1;
	}
    }
    return 0;
}

StgInt const_BUFSIZ() { return BUFSIZ; }
<commit_msg>[project @ 1999-03-01 09:26:45 by sof] mingw32: stay well clear of any tty stuff - just don't have support for it.<commit_after>/* 
 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
 *
 * $Id: setBuffering.c,v 1.4 1999/03/01 09:26:45 sof Exp $
 *
 * hSetBuffering Runtime Support
 */

#include "Rts.h"
#include "stgio.h"

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

#define SB_NB (0)
#define SB_LB (-1)
#define SB_BB (-2)

StgInt
setBuffering(ptr, size)
StgForeignPtr ptr;
StgInt size;
{
    IOFileObject* fo = (IOFileObject*)ptr;
    int flags, rc=0;
    int input, isaterm;
    struct termios tio;
    struct stat sb;
   

    /* First off, flush old buffer.. */
    if ( (fo->flags & FILEOBJ_FLUSH) ) {
       rc = flushBuffer(ptr);
    }
    if (rc<0) return rc;

    /* Let go of old buffer, and reset buffer pointers. */
    if ( fo->buf != NULL ) {
       free(fo->buf);
       fo->bufWPtr = 0;
       fo->bufRPtr = 0;
       fo->bufSize = 0;
       fo->buf     = NULL;
    }

#ifndef mingw32_TARGET_OS
    while ((flags = fcntl(fo->fd, F_GETFL)) < 0) {
	if (errno != EINTR) {
	    cvtErrno();
	    stdErrno();
	    return -1;
	}
    }
    flags &= O_ACCMODE;
    input = flags == O_RDONLY || flags == O_RDWR;

    isaterm = input && isatty(fo->fd);
#endif

    switch (size) {
    case SB_NB:
        fo->flags &= ~FILEOBJ_LB & ~FILEOBJ_BB;

#ifndef mingw32_TARGET_OS
	if (isaterm) {
	    /* Switch over to canonical mode. */
	    if (tcgetattr(fo->fd, &tio) < 0) {
		cvtErrno();
		stdErrno();
		return -1;
	    }
	    tio.c_lflag &=  ~ICANON;
	    tio.c_cc[VMIN] = 1;
	    tio.c_cc[VTIME] = 0;
	    if (tcsetattr(fo->fd, TCSANOW, &tio) < 0) {
		cvtErrno();
		stdErrno();
		return -1;
	    }
	}
#endif
	return 0;
    case SB_LB:
        fo->flags &= ~FILEOBJ_BB;
	fo->flags |= FILEOBJ_LB;
        size = BUFSIZ;
	break;
    case SB_BB:

#if HAVE_ST_BLKSIZE
	while (fstat(fo->fd, &sb) < 0) {
	   /* not very likely.. */
	   if ( errno != EINTR ) {
	      cvtErrno();
	      stdErrno();
	      return -1;
	   }
        }
	size = sb.st_blksize;
#else
	size = BUFSIZ;
#endif
        fo->flags &= ~FILEOBJ_LB;
	fo->flags |= FILEOBJ_BB;
	/* fall through */
    default:
	break;
    }
  
    if ( size > 0) {
       fo->buf = malloc(size*sizeof(char));
       if (fo->buf == NULL) {
           return -1;
       }
       fo->bufSize = size;
    }
#ifndef mingw32_TARGET_OS
    if (isaterm) {

	/*
	 * Try to switch back to cooked mode.
	 */

	if (tcgetattr(fo->fd, &tio) < 0) {
	    cvtErrno();
	    stdErrno();
	    return -1;
	}
	tio.c_lflag |= ICANON;
	if (tcsetattr(fo->fd, TCSANOW, &tio) < 0) {
	    cvtErrno();
	    stdErrno();
	    return -1;
	}
    }
#endif
    return 0;
}

StgInt const_BUFSIZ() { return BUFSIZ; }

{'fraction_non_alphanumeric': 0.1580639399892838, 'fraction_numerical': 0.021789605286658333, 'mean_word_length': 2.225806451612903, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6517850', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2291, 'n_words': 539}