ftp.nice.ch/pub/next/text/framemaker/fmbib.1.3.s.tar.gz#/mifwrite.c

This is mifwrite.c in view mode; [Download] [Up]

/* mifwrite.c
 * cm, 16 jan 90
 * 
 * output module for Frame bibliography generator.  These routines 
 * handle all MIF output generated from the program.
 */

/*
 * Copyright (c) 1991 Carnegie Mellon University
 * All Rights Reserved.
 * 
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */

#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <sys/file.h>
#include <mif.h>
#include <bib.h>

#define BUFSIZE (1024)

typedef struct iostream
{
	char	fname[256];
	int	fd;
} IOstream;

static IOstream streams[3];	/* mif, bib files */

static char	buf[BUFSIZE];


/* open output files for to be generated. 
 * will exit on failure.
 */
mif_open_output(type, fname)
	register type;
	char	*fname;
{
	if( fname == NULL )	/* force to stdout */
	{
		streams[type].fd = 1;
		return;
	}
	if( (streams[type].fd = open( fname, O_WRONLY|O_TRUNC|O_CREAT, 0644)) < 0 )
			err( FATAL, "Can't open <%s>, errno=%d\n", 
				fname, errno );
	strcpy( streams[type].fname, fname );
}


/* close the current .mif input file 
 */
mif_close_output(type)
{
	close(streams[type]);
}


/* write a string to the specified file.
 */
mif_write( type, cp )
	int	type;
	char	*cp;
{
	register len;

	if( !cp ) return;

	len = strlen(cp);

	if( write( streams[type].fd, cp, len) != len )
	{
		err( FATAL, "mif_write failed <%s>, errno=%d\n", 
			streams[type].fname, errno );
	}
}

/* write a string to the specified file.
 */
mif_writebuf( type, cp, nbytes )
	int	type;
	char	*cp;
	int	nbytes;
{
	if( !cp ) return;

	if( write( streams[type].fd, cp, nbytes) != nbytes )
	{
		err( FATAL, "mif_write failed <%s>, errno=%d\n", 
			streams[type].fname, errno );
	}
}


/* write string followed by newline to the file. 
 */
mif_writeln(type, str)
	int	type;
	char 	*str;
{
	if( !str )
		return;
	mif_write( type, str );
	mif_write( type, "\n" );
}

static char *tmp = "/tmp/mif2bib.XXXXXX";

move_file( dest, src )
	char *dest, *src;
{

	mktemp(tmp);

	set_signals(1);
	link( dest, tmp );

	if( unlink( dest ) < 0 )
	{
		err( WARN, "Couldn't remove %s to rename,\n\tleaving %s, errno=%d\n",
			dest, src, errno );
		unlink( tmp );
		return;
	}
	if( link( src, dest ) <  0)
	{
		link( tmp, dest );
		unlink( tmp );
		err( WARN, "Couldn't rename, %s,\n\tleaving %s, errno=%d\n",
			dest, src, errno );
		return;

	}
	unlink( src );
	unlink( tmp );
	set_signals(0);
}

/* trap user interrupts to avoid trashing input files 
 * while overwriting them.
 */
set_signals(on)
{
	if( on )
	{
		signal( SIGHUP,  SIG_IGN );
		signal( SIGINT,  SIG_IGN );
		signal( SIGQUIT, SIG_IGN );
	}
	else
	{	
		signal( SIGHUP,  SIG_DFL );
		signal( SIGINT,  SIG_DFL );
		signal( SIGQUIT, SIG_IGN );
	}
}

/* copy the initial .mif header/Pgf stuff to the top
 * of the mif bibliography file.
 */
copy_bibtemplate( template )
	char *template;
{
	int src;
	int n;

	if( (src = open(template, O_RDONLY)) < 0 )
	{
		err(WARN, "Can't open %s, errno=%d\n",
			template, errno );
		return(0);
	}

	while((n =  read(src, buf, BUFSIZ )) > 0 )
		mif_writebuf( O_BIB, buf, n );

	if( n<0 )
		err( FATAL, "Error reading %s, errno=%d\n",
			template, errno );

	close( src );
	return(1);
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.