[comp.sources.x] v11i072: dbiff -- double-enhanced xbiff, Part02/02

pfuetz@agd.fhg.de (02/15/91)

Submitted-by: pfuetz@agd.fhg.de
Posting-number: Volume 11, Issue 72
Archive-name: dbiff/part02

#!/bin/sh
# To unshare, sh or unshar this file
echo libsst.c 1>&2
sed -e 's/^X//' > libsst.c <<'E!O!F! libsst.c'
X/* libsst.c - SPARC sound tools library
X**
X** Copyright (C) 1989 by Jef Poskanzer.
X**
X** Permission to use, copy, modify, and distribute this software and its
X** documentation for any purpose and without fee is hereby granted, provided
X** that the above copyright notice appear in all copies and that both that
X** copyright notice and this permission notice appear in supporting
X** documentation.  This software is provided "as is" without express or
X** implied warranty.
X*/
X
X#include <stdio.h>
X#include <fcntl.h>
X#include "libsst.h"
X
X#define AUDBUF 1024
X
Xint
Xsst_open(play_level, record_level)
X    int play_level, record_level;
X    {
X    int fd, i, gr, ger, gx;
X    struct audio_ioctl ai;
X    char *getenv(), *ep;
X
X    fd = open( "/dev/audio", O_RDWR );
X    if ( fd < 0 )
X	{
X	perror( "sst_open: open /dev/audio" );
X	return( 1 );
X	}
X
X    /* Shrink audio device's queue size, to cut down time delay. */
X    i = AUDBUF;
X    if ( ioctl( fd, AUDIOSETQSIZE, &i ) < 0 )
X	{
X	perror( "sst_open: SETQSIZE" );
X	return( 1 );
X	}
X
X    /* Set gains.  -10 <= ger <= 18,  -18 <= gr <= 12,  -18 <= gx <= 12. */
X    if (!play_level) 
X    {
X	play_level = 75;
X	if ( (ep = getenv( "SST_PLAY" )) != NULL )
X	{
X	    play_level = atoi( ep );
X	    if ( play_level < 0 || play_level > 99 )
X	    {
X		fprintf( stderr, "sst_open: SST_PLAY must be between 0 and 99\n" );
X		return( 1 );
X	    }
X	}
X    }
X    if (!record_level) 
X    {
X	record_level = 75;
X	if ( (ep = getenv( "SST_RECORD" )) != NULL )
X	{
X	    record_level = atoi( ep );
X	    if ( record_level < 0 || record_level > 99 )
X	    {
X		fprintf( stderr, "sst_open: SST_RECORD must be between 0 and 99\n" );
X		return( 1 );
X	    }
X	}
X    }
X
X    play_level = play_level * 59 / 100 - 28;
X    ger = play_level / 2;
X    gr = play_level - ger;
X    if ( ger < -10 )
X	{
X	ger = -10;
X	gr = play_level - ger;
X	}
X    if ( gr > 12 )
X	{
X	gr = 12;
X	ger = play_level - gr;
X	}
X    gx = record_level * 31 / 100 - 18;
X    sst_set_gr( fd, gr );
X    sst_set_ger( fd, ger );
X    sst_set_gx( fd, gx );
X
X    /*  Initialize the MMR2 register to send the output to either
X    **  the speaker or the earphone jack, depending on SST_EARPHONES.
X    */
X    ai.control = AUDIO_MAP_MMR2;
X    if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
X	{
X	perror( "sst_open: GETREG MMR2" );
X	return( 1 );
X	}
X    if ( (ep = getenv( "SST_EARPHONES" )) != NULL )
X	ai.data[0] &= ~AUDIO_MMR2_BITS_LS;
X    else
X	ai.data[0] |= AUDIO_MMR2_BITS_LS;
X    if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
X	{
X	perror( "sst_open: SETREG MMR2" );
X	return( 1 );
X	}
X
X    return fd;
X    }
X
Xvoid
Xsst_close( fd )
Xint fd;
X    {
X    struct audio_ioctl ai;
X
X    ai.control = AUDIO_MAP_MMR1;
X    ai.data[0] = 0;
X    if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
X	{
X	perror( "sst_close: SETREG MMR1" );
X	}
X    ai.control = AUDIO_MAP_MMR2;
X    ai.data[0] = 0;
X    if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
X	{
X	perror( "sst_close: SETREG MMR2" );
X	}
X    close( fd );
X    }
X
X/* These are tables of values to be loaded into various gain registers.
X*/
X
Xstatic unsigned char ger_table[][2] = {
X    0xaa,	0xaa,	/* -10db */
X    0x79,	0xac,
X    0x41,	0x99,
X    0x9c,	0xde,
X    0x74,	0x9c,	/* -6db */
X    0x6a,	0xae,
X    0xab,	0xdf,
X    0x64,	0xab,
X    0x2a,	0xbd,
X    0x5c,	0xce,
X    0x00,	0x99,	/* 0db */
X    0x43,	0xdd,
X    0x52,	0xef,
X    0x55,	0x42,
X    0x31,	0xdd,
X    0x43,	0x1f,
X    0x40,	0xdd,	/* 6db */
X    0x44,	0x0f,
X    0x31,	0x1f,
X    0x10,	0xdd,
X    0x41,	0x0f,
X    0x60,	0x0b,
X    0x42,	0x10,	/* 12db */
X    0x11,	0x0f,
X    0x72,	0x00,
X    0x21,	0x10,
X    0x22,	0x00,
X    0x00,	0x0b,
X    0x00,	0x0f,	/* 18db */
X    };
X
X
Xstatic unsigned char gr_gx_table[][2] = {
X    0x8b,	0x7c,	/* -18db */
X    0x8b,	0x35,
X    0x8b,	0x24,
X    0x91,	0x23,
X    0x91,	0x2a,
X    0x91,	0x3b,
X    0x91,	0xf9,	/* -12db */
X    0x91,	0xb6,
X    0x91,	0xa4,
X    0x92,	0x32,
X    0x92,	0xaa,
X    0x93,	0xb3,
X    0x9f,	0x91,	/* -6db */
X    0x9b,	0xf9,
X    0x9a,	0x4a,
X    0xa2,	0xa2,
X    0xaa,	0xa3,
X    0xbb,	0x52,
X    0x08,	0x08,	/* 0db */
X    0x3d,	0xac,
X    0x25,	0x33,
X    0x21,	0x22,
X    0x12,	0xa2,
X    0x11,	0x3b,
X    0x10,	0xf2,	/* 6db */
X    0x02,	0xca,
X    0x01,	0x5a,
X    0x01,	0x12,
X    0x00,	0x32,
X    0x00,	0x13,
X    0x00,	0x0e,	/* 12db */
X    };
X
Xvoid
Xsst_set_ger( fd, value )
Xint fd, value;
X    {
X    struct audio_ioctl ai;
X
X    if ( ( value < -10 ) || ( value > 18 ) )
X	{
X	fprintf( stderr, "sst_set_ger: GER %d out of range\n", value );
X	return;
X	}
X
X    /*  Add 10 to the value to get the index into the table.  */
X    ai.control = AUDIO_MAP_GER;
X    ai.data[0] = ger_table[value + 10][1];
X    ai.data[1] = ger_table[value + 10][0];
X
X    if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
X	{
X	perror( "sst_set_ger: SETREG GER" );
X	}
X
X    ai.control = AUDIO_MAP_MMR1;
X    if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
X	{
X	perror( "sst_set_ger: GETREG MMR1" );
X	}
X    ai.data[0] |= AUDIO_MMR1_BITS_LOAD_GER;
X    if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
X	{
X	perror( "sst_set_ger: SETREG MMR1" );
X	}
X    }
X
Xvoid
Xsst_set_gr( fd, value )
Xint fd, value;
X    {
X    struct audio_ioctl ai;
X
X    if ( ( value < -18 ) || ( value > 12 ) )
X	{
X	fprintf( stderr, "sst_set_gr: GR %d out of range\n", value );
X	return;
X	}
X
X    ai.control = AUDIO_MAP_GR;
X    ai.data[0] = gr_gx_table[value + 18][1];
X    ai.data[1] = gr_gx_table[value + 18][0];
X
X    if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
X	{
X	perror( "sst_set_gr: SETREG GR" );
X	}
X
X    ai.control = AUDIO_MAP_MMR1;
X    if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
X	{
X	perror( "sst_set_gr: GETREG MMR1" );
X	}
X    ai.data[0] |= AUDIO_MMR1_BITS_LOAD_GR;
X    if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
X	{
X	perror( "sst_set_gr: SETREG MMR1" );
X	}
X    }
X
Xvoid
Xsst_set_gx( fd, value )
Xint fd, value;
X    {
X    struct audio_ioctl ai;
X
X    if ( ( value < -18 ) || ( value > 12 ) )
X	{
X	fprintf( stderr, "sst_set_gx: GX %d out of range\n", value );
X	return;
X	}
X
X    /*  We add 18 to get the index into the table, since entry 0 represents
X    *  -18db.
X    */
X    ai.control = AUDIO_MAP_GX;
X    ai.data[0] = gr_gx_table[value + 18][1];
X    ai.data[1] = gr_gx_table[value + 18][0];
X
X    if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
X	{
X	perror( "sst_set_gx: SETREG GX" );
X	}
X
X    ai.control = AUDIO_MAP_MMR1;
X    if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
X	{
X	perror( "sst_set_gx: GETREG MMR1" );
X	}
X    ai.data[0] |= AUDIO_MMR1_BITS_LOAD_GX;
X    if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
X	{
X	perror( "sst_set_gx: SETREG MMR1" );
X	}
X    }
X
Xvoid
Xsst_tones( fd, dhz1, dhz2, thz, rhz, usec )
Xint fd, dhz1, dhz2, thz, rhz, usec;
X    {
X    struct audio_ioctl ai;
X    int dval1, dval2, tval, rval;
X    unsigned char oldmmr2, newmmr2;
X
X    if ( dhz1 == 0 )
X	dval1 = 0;
X    else
X	{
X	dval1 = ( dhz1 * 128 + 63 ) / 1000;
X	if ( ( dval1 < 1 ) || ( dval1 > 255 ) )
X	    {
X	    fprintf(stderr, "sst_tones: dhz1 %d out of range\n", dhz1 );
X	    return;
X	    }
X	}
X
X    if ( dhz2 == 0 )
X	dval2 = 0;
X    else
X	{
X	dval2 = ( dhz2 * 128 + 63 ) / 1000;
X	if ( ( dval2 < 1 ) || ( dval2 > 255 ) )
X	    {
X	    fprintf(stderr, "sst_tones: dhz2 %d out of range\n", dhz2 );
X	    return;
X	    }
X	}
X
X    if ( thz == 0 )
X	tval = 0;
X    else
X	{
X	tval = ( thz * 128 + 63 ) / 2000;
X	if ( ( tval < 1 ) || ( tval > 255 ) )
X	    {
X	    fprintf(stderr, "sst_tones: thz %d out of range\n", thz );
X	    return;
X	    }
X	}
X
X    if ( rhz == 0 )
X	rval = 0;
X    else
X	{
X	rval = ( rhz * 128 + 63 ) / 2000;
X	if ( ( rval < 1 ) || ( rval > 255 ) )
X	    {
X	    fprintf(stderr, "sst_tones: rhz %d out of range\n", dhz2 );
X	    return;
X	    }
X	}
X
X    if ( ( dval1 != 0 || dval2 != 0 ) && ( tval != 0 || rval != 0 ) )
X	{
X	fprintf(stderr, "sst_tones: cannot use DTMF and TONE or RINGER at the same time\n", dhz2 );
X	return;
X	}
X
X    if ( tval != 0 && rval != 0 )
X	{
X	fprintf(stderr, "sst_tones: cannot use TONE and RINGER at the same time\n", dhz2 );
X	return;
X	}
X
X    ai.control = AUDIO_MAP_MMR2;
X    if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 )
X	{
X	perror( "sst_tones: GETREG MMR2" );
X	}
X    oldmmr2 = newmmr2 = ai.data[0];
X
X    if ( dval1 != 0 || dval2 != 0 )
X	{
X	newmmr2 |= AUDIO_MMR2_BITS_DTMF;
X	ai.control = AUDIO_MAP_FTGR;
X	ai.data[0] = dval1;
X	ai.data[1] = dval2;
X	if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
X	    {
X	    perror( "sst_tones: SETREG FTGR" );
X	    }
X	}
X
X    if ( tval != 0 )
X	{
X	newmmr2 |= AUDIO_MMR2_BITS_TONE;
X	ai.control = AUDIO_MAP_FTGR;
X	ai.data[0] = tval;
X	ai.data[1] = 0;
X	if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
X	    {
X	    perror( "sst_tones: SETREG FTGR" );
X	    }
X	}
X
X    if ( rval != 0 )
X	{
X	newmmr2 |= AUDIO_MMR2_BITS_RINGER;
X	ai.control = AUDIO_MAP_FTGR;
X	ai.data[0] = rval;
X	ai.data[1] = 0;
X	if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
X	    {
X	    perror( "sst_tones: SETREG FTGR" );
X	    }
X	}
X
X    ai.control = AUDIO_MAP_MMR2;
X    ai.data[0] = newmmr2;
X    if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
X	{
X	perror( "sst_tones: SETREG MMR2" );
X	}
X
X    usleep( usec );
X
X    ai.data[0] = oldmmr2;
X    if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 )
X	{
X	perror( "sst_tones: SETREG MMR2" );
X	}
X    }
X
Xvoid
Xsst_dtmf( fd, dial, usecper, usecpause )
Xint fd, usecper, usecpause;
Xchar *dial;
X    {
X    char *cp;
X
X    for ( cp = dial; *cp != '\0'; cp++ )
X	{
X	switch ( *cp )
X	    {
X	    case '1': sst_tones( fd, 703, 1211, 0, 0, usecper ); break;
X	    case '2': sst_tones( fd, 703, 1336, 0, 0, usecper ); break;
X	    case '3': sst_tones( fd, 703, 1492, 0, 0, usecper ); break;
X	    case 'A': sst_tones( fd, 703, 1648, 0, 0, usecper ); break;
X	    case '4': sst_tones( fd, 773, 1211, 0, 0, usecper ); break;
X	    case '5': sst_tones( fd, 773, 1336, 0, 0, usecper ); break;
X	    case '6': sst_tones( fd, 773, 1492, 0, 0, usecper ); break;
X	    case 'B': sst_tones( fd, 773, 1648, 0, 0, usecper ); break;
X	    case '7': sst_tones( fd, 859, 1211, 0, 0, usecper ); break;
X	    case '8': sst_tones( fd, 859, 1336, 0, 0, usecper ); break;
X	    case '9': sst_tones( fd, 859, 1492, 0, 0, usecper ); break;
X	    case 'C': sst_tones( fd, 859, 1648, 0, 0, usecper ); break;
X	    case '*': sst_tones( fd, 945, 1211, 0, 0, usecper ); break;
X	    case '0': sst_tones( fd, 945, 1336, 0, 0, usecper ); break;
X	    case '#': sst_tones( fd, 945, 1492, 0, 0, usecper ); break;
X	    case 'D': sst_tones( fd, 945, 1648, 0, 0, usecper ); break;
X
X	    case ' ': case '-': case '(': case ')': case '+':
X	    continue;	/* ignore */
X
X	    case ',': usleep( usecper ); break;	/* big pause */
X
X	    default:
X	    fprintf( stderr, "sst_dtmf: unknown dialing code '%c'\n", *cp );
X	    }
X	usleep( usecpause );
X	}
X    }
E!O!F! libsst.c
echo libsst.h 1>&2
sed -e 's/^X//' > libsst.h <<'E!O!F! libsst.h'
X/* libsst.h - include file for SPARC sound tools library
X**
X** Copyright (C) 1989 by Jef Poskanzer.
X**
X** Permission to use, copy, modify, and distribute this software and its
X** documentation for any purpose and without fee is hereby granted, provided
X** that the above copyright notice appear in all copies and that both that
X** copyright notice and this permission notice appear in supporting
X** documentation.  This software is provided "as is" without express or
X** implied warranty.
X*/
X
X#include <sys/ioctl.h>
X#ifdef SUNOS4_1
X#define AUDIO_4_0_3_COMPAT
X#define AUDIO_CHIP
X#include <sbusdev/audio_79C30.h>
X#include <multimedia/libaudio.h>
X#include <multimedia/audio_device.h>
X#else
X#include <sbusdev/audioreg.h>
X#endif
X#include <sun/audioio.h>
X
X#define SAMPLES_PER_SECOND 8192
X
Xint sst_open( );
Xvoid sst_close( /* int fd */ );
X
Xvoid sst_set_ger( /* int fd, value */ );
Xvoid sst_set_gr( /* int fd, value */ );
Xvoid sst_set_gx( /* int fd, value */ );
X
Xvoid sst_tones( /* int fd, dhz1, dhz2, thz, rhz, usec */ );
Xvoid sst_dtmf( /* int fd, char *dial, int usecper, usecpause */ );
E!O!F! libsst.h
echo libst.c 1>&2
sed -e 's/^X//' > libst.c <<'E!O!F! libst.c'
X/* libst.c - portable sound tools library
X*/
X
X/*
X** This routine converts from linear to ulaw.
X**
X** Craig Reese: IDA/Supercomputing Research Center
X** Joe Campbell: Department of Defense
X** 29 September 1989
X**
X** References:
X** 1) CCITT Recommendation G.711  (very difficult to follow)
X** 2) "A New Digital Technique for Implementation of Any
X**     Continuous PCM Companding Law," Villeret, Michel,
X**     et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
X**     1973, pg. 11.12-11.17
X** 3) MIL-STD-188-113,"Interoperability and Performance Standards
X**     for Analog-to_Digital Conversion Techniques,"
X**     17 February 1987
X**
X** Input: Signed 16 bit linear sample
X** Output: 8 bit ulaw sample
X*/
X
X#define ZEROTRAP    /* turn on the trap as per the MIL-STD */
X#define BIAS 0x84   /* define the add-in bias for 16 bit samples */
X#define CLIP 32635
X
Xunsigned char
Xst_linear_to_ulaw( sample )
Xint sample;
X    {
X    static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
X                               4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
X                               5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
X                               5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
X                               6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
X                               6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
X                               6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
X                               6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
X                               7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
X                               7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
X                               7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
X                               7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
X                               7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
X                               7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
X                               7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
X                               7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
X    int sign, exponent, mantissa;
X    unsigned char ulawbyte;
X
X    /* Get the sample into sign-magnitude. */
X    sign = (sample >> 8) & 0x80;		/* set aside the sign */
X    if ( sign != 0 ) sample = -sample;		/* get magnitude */
X    if ( sample > CLIP ) sample = CLIP;		/* clip the magnitude */
X
X    /* Convert from 16 bit linear to ulaw. */
X    sample = sample + BIAS;
X    exponent = exp_lut[( sample >> 7 ) & 0xFF];
X    mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F;
X    ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa );
X#ifdef ZEROTRAP
X    if ( ulawbyte == 0 ) ulawbyte = 0x02;	/* optional CCITT trap */
X#endif
X
X    return ulawbyte;
X    }
X
X/*
X** This routine converts from ulaw to 16 bit linear.
X**
X** Craig Reese: IDA/Supercomputing Research Center
X** 29 September 1989
X**
X** References:
X** 1) CCITT Recommendation G.711  (very difficult to follow)
X** 2) MIL-STD-188-113,"Interoperability and Performance Standards
X**     for Analog-to_Digital Conversion Techniques,"
X**     17 February 1987
X**
X** Input: 8 bit ulaw sample
X** Output: signed 16 bit linear sample
X*/
X
Xint
Xst_ulaw_to_linear_slow( ulawbyte )
Xunsigned char ulawbyte;
X    {
X    static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
X    int sign, exponent, mantissa, sample;
X
X    ulawbyte = ~ ulawbyte;
X    sign = ( ulawbyte & 0x80 );
X    exponent = ( ulawbyte >> 4 ) & 0x07;
X    mantissa = ulawbyte & 0x0F;
X    sample = exp_lut[exponent] + ( mantissa << ( exponent + 3 ) );
X    if ( sign != 0 ) sample = -sample;
X
X    return sample;
X    }
E!O!F! libst.c
echo libst.h 1>&2
sed -e 's/^X//' > libst.h <<'E!O!F! libst.h'
X/* libst.h - include file for portable sound tools library
X**
X** Copyright (C) 1989 by Jef Poskanzer.
X**
X** Permission to use, copy, modify, and distribute this software and its
X** documentation for any purpose and without fee is hereby granted, provided
X** that the above copyright notice appear in all copies and that both that
X** copyright notice and this permission notice appear in supporting
X** documentation.  This software is provided "as is" without express or
X** implied warranty.
X*/
X
X#define SAMPLES_PER_SECOND 8192
X
X#define MINLIN -32768
X#define MAXLIN 32767
X#define LINCLIP(x) do { if ( x < MINLIN ) x = MINLIN ; else if ( x > MAXLIN ) x = MAXLIN; } while ( 0 )
X
Xunsigned char st_linear_to_ulaw( /* int sample */ );
Xint st_ulaw_to_linear_slow( /* unsigned char ulawbyte */ );
X
X/*
X** This macro converts from ulaw to 16 bit linear, faster.
X**
X** Jef Poskanzer
X** 23 October 1989
X**
X** Input: 8 bit ulaw sample
X** Output: signed 16 bit linear sample
X*/
X#define st_ulaw_to_linear(ulawbyte) ulaw_table[ulawbyte]
X
Xstatic int ulaw_table[256] = {
X    -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
X    -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
X    -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
X    -11900, -11388, -10876, -10364,  -9852,  -9340,  -8828,  -8316,
X     -7932,  -7676,  -7420,  -7164,  -6908,  -6652,  -6396,  -6140,
X     -5884,  -5628,  -5372,  -5116,  -4860,  -4604,  -4348,  -4092,
X     -3900,  -3772,  -3644,  -3516,  -3388,  -3260,  -3132,  -3004,
X     -2876,  -2748,  -2620,  -2492,  -2364,  -2236,  -2108,  -1980,
X     -1884,  -1820,  -1756,  -1692,  -1628,  -1564,  -1500,  -1436,
X     -1372,  -1308,  -1244,  -1180,  -1116,  -1052,   -988,   -924,
X      -876,   -844,   -812,   -780,   -748,   -716,   -684,   -652,
X      -620,   -588,   -556,   -524,   -492,   -460,   -428,   -396,
X      -372,   -356,   -340,   -324,   -308,   -292,   -276,   -260,
X      -244,   -228,   -212,   -196,   -180,   -164,   -148,   -132,
X      -120,   -112,   -104,    -96,    -88,    -80,    -72,    -64,
X       -56,    -48,    -40,    -32,    -24,    -16,     -8,      0,
X     32124,  31100,  30076,  29052,  28028,  27004,  25980,  24956,
X     23932,  22908,  21884,  20860,  19836,  18812,  17788,  16764,
X     15996,  15484,  14972,  14460,  13948,  13436,  12924,  12412,
X     11900,  11388,  10876,  10364,   9852,   9340,   8828,   8316,
X      7932,   7676,   7420,   7164,   6908,   6652,   6396,   6140,
X      5884,   5628,   5372,   5116,   4860,   4604,   4348,   4092,
X      3900,   3772,   3644,   3516,   3388,   3260,   3132,   3004,
X      2876,   2748,   2620,   2492,   2364,   2236,   2108,   1980,
X      1884,   1820,   1756,   1692,   1628,   1564,   1500,   1436,
X      1372,   1308,   1244,   1180,   1116,   1052,    988,    924,
X       876,    844,    812,    780,    748,    716,    684,    652,
X       620,    588,    556,    524,    492,    460,    428,    396,
X       372,    356,    340,    324,    308,    292,    276,    260,
X       244,    228,    212,    196,    180,    164,    148,    132,
X       120,    112,    104,     96,     88,     80,     72,     64,
X	56,     48,     40,     32,     24,     16,      8,      0 };
E!O!F! libst.h
echo play.c 1>&2
sed -e 's/^X//' > play.c <<'E!O!F! play.c'
X/* play.c - play a sound file on the speaker
X **
X ** Copyright (C) 1989 by Jef Poskanzer.
X **
X ** Permission to use, copy, modify, and distribute this software and its
X ** documentation for any purpose and without fee is hereby granted, provided
X ** that the above copyright notice appear in all copies and that both that
X ** copyright notice and this permission notice appear in supporting
X ** documentation.  This software is provided "as is" without express or
X ** implied warranty.
X */
X
X#include <stdio.h>
X#include <fcntl.h>
X#include <sys/file.h>
X#include <sys/signal.h>
X#include "libsst.h"
X
X#define MYBUF 256
X
Xint sst_fd;
X
Xvoid (*sighup_handler)();
Xvoid (*sigint_handler)();
X
Xplay_sound(sound_file, volume)
X    char *sound_file;
X    int volume;
X{
X    int rrtn, wrtn;
X    unsigned char buf[MYBUF];
X    int file_fd;
X    int sighandler();
X    
X    sst_fd = sst_open(volume,0);
X    sighup_handler = signal( SIGHUP, sighandler );
X    sigint_handler = signal( SIGINT, sighandler );
X    
X    file_fd = open( sound_file, O_RDONLY );
X    if ( file_fd < 0 )
X    {
X	perror( sound_file );
X	return( 1 );
X    }
X#ifdef SUNOS4_1
X    {
X    int err;
X    Audio_hdr Dev_hdr;
X    Audio_hdr File_hdr;
X
X    err = audio_get_play_config( sst_fd, &Dev_hdr );
X
X    if ( err != AUDIO_SUCCESS )
X    {
X	perror( "Not a valid audio device" );
X	return( 1 );
X    }
X
X    err = audio_read_filehdr( file_fd, &File_hdr, (char *) NULL, 0 );
X
X    if ( err != AUDIO_SUCCESS )
X    {
X	perror( "Not a valid audio file" );
X	return( 1 );
X    }
X
X    /* Shameless pilfered from /usr/demo/SOUND/play.c */
X    if ( audio_cmp_hdr(&Dev_hdr, &File_hdr) != 0 ) {
X	if ( audio_drain( sst_fd, FALSE) != AUDIO_SUCCESS) {
X	    perror( "draining audio device" );
X	    return( 1 );
X	}
X    /* Fancy code to reconfigure audio device for given file header omitted */
X    }
X    }
X#endif
X    
X    for ( ; ; )
X    {
X	rrtn = read( file_fd, buf, MYBUF );
X	if ( rrtn < 0 )
X	{
X	    perror( "read" );
X	    return( 1 );
X	}
X	if ( rrtn == 0 )
X	    break;
X	
X	for ( ; ; )
X	{
X	    wrtn = write( sst_fd, buf, rrtn );
X	    if ( wrtn < 0 )
X	    {
X		perror( "write" );
X		return( 1 );
X	    }
X	    if ( wrtn != 0 )
X		break;
X	    usleep( 1000 );
X	}
X	if ( wrtn != rrtn )
X	{
X	    fprintf( stderr, "play: rrtn = %d, wrtn = %d\n", rrtn, wrtn );
X	    return( 1 );
X	}
X    }
X    
X    close(file_fd);
X#ifdef SUNOS4_1
X    if ( audio_drain( sst_fd, FALSE) != AUDIO_SUCCESS) {
X	perror( "draining audio device" );
X	return( 1 );
X    }
X#endif
X    sst_close( sst_fd );
X    (void) signal( SIGHUP, sighup_handler );
X    (void) signal( SIGINT, sigint_handler );
X}
X
Xstatic int
Xsighandler(sig, code, scp, addr)
X    int sig, code;
X    struct sigcontext *scp;
X    char *addr;
X{
X    sst_close( sst_fd );
X    if (sig == SIGHUP && sighup_handler)
X	(*sighup_handler)(sig, code, scp, addr);
X    else if (sigint_handler)
X	(*sigint_handler)(sig, code, scp, addr);
X    exit( 1 );
X}
E!O!F! play.c
echo regexp.3 1>&2
sed -e 's/^X//' > regexp.3 <<'E!O!F! regexp.3'
X.TH REGEXP 3 local
X.DA 30 Nov 1985
X.SH NAME
Xregcomp, regexec, regsub, regerror \- regular expression handler
X.SH SYNOPSIS
X.ft B
X.nf
X#include <regexp.h>
X
Xregexp *regcomp(exp)
Xchar *exp;
X
Xint regexec(prog, string)
Xregexp *prog;
Xchar *string;
X
Xregsub(prog, source, dest)
Xregexp *prog;
Xchar *source;
Xchar *dest;
X
Xregerror(msg)
Xchar *msg;
X.SH DESCRIPTION
XThese functions implement
X.IR egrep (1)-style
Xregular expressions and supporting facilities.
X.PP
X.I Regcomp
Xcompiles a regular expression into a structure of type
X.IR regexp ,
Xand returns a pointer to it.
XThe space has been allocated using
X.IR malloc (3)
Xand may be released by
X.IR free .
X.PP
X.I Regexec
Xmatches a NUL-terminated \fIstring\fR against the compiled regular expression
Xin \fIprog\fR.
XIt returns 1 for success and 0 for failure, and adjusts the contents of
X\fIprog\fR's \fIstartp\fR and \fIendp\fR (see below) accordingly.
X.PP
XThe members of a
X.I regexp
Xstructure include at least the following (not necessarily in order):
X.PP
X.RS
Xchar *startp[NSUBEXP];
X.br
Xchar *endp[NSUBEXP];
X.RE
X.PP
Xwhere
X.I NSUBEXP
Xis defined (as 10) in the header file.
XOnce a successful \fIregexec\fR has been done using the \fIregexp\fR,
Xeach \fIstartp\fR-\fIendp\fR pair describes one substring
Xwithin the \fIstring\fR,
Xwith the \fIstartp\fR pointing to the first character of the substring and
Xthe \fIendp\fR pointing to the first character following the substring.
XThe 0th substring is the substring of \fIstring\fR that matched the whole
Xregular expression.
XThe others are those substrings that matched parenthesized expressions
Xwithin the regular expression, with parenthesized expressions numbered
Xin left-to-right order of their opening parentheses.
X.PP
X.I Regsub
Xcopies \fIsource\fR to \fIdest\fR, making substitutions according to the
Xmost recent \fIregexec\fR performed using \fIprog\fR.
XEach instance of `&' in \fIsource\fR is replaced by the substring
Xindicated by \fIstartp\fR[\fI0\fR] and
X\fIendp\fR[\fI0\fR].
XEach instance of `\e\fIn\fR', where \fIn\fR is a digit, is replaced by
Xthe substring indicated by
X\fIstartp\fR[\fIn\fR] and
X\fIendp\fR[\fIn\fR].
X.PP
X.I Regerror
Xis called whenever an error is detected in \fIregcomp\fR, \fIregexec\fR,
Xor \fIregsub\fR.
XThe default \fIregerror\fR writes the string \fImsg\fR,
Xwith a suitable indicator of origin,
Xon the standard
Xerror output
Xand invokes \fIexit\fR(2).
X.I Regerror
Xcan be replaced by the user if other actions are desirable.
X.SH "REGULAR EXPRESSION SYNTAX"
XA regular expression is zero or more \fIbranches\fR, separated by `|'.
XIt matches anything that matches one of the branches.
X.PP
XA branch is zero or more \fIpieces\fR, concatenated.
XIt matches a match for the first, followed by a match for the second, etc.
X.PP
XA piece is an \fIatom\fR possibly followed by `*', `+', or `?'.
XAn atom followed by `*' matches a sequence of 0 or more matches of the atom.
XAn atom followed by `+' matches a sequence of 1 or more matches of the atom.
XAn atom followed by `?' matches a match of the atom, or the null string.
X.PP
XAn atom is a regular expression in parentheses (matching a match for the
Xregular expression), a \fIrange\fR (see below), `.'
X(matching any single character), `^' (matching the null string at the
Xbeginning of the input string), `$' (matching the null string at the
Xend of the input string), a `\e' followed by a single character (matching
Xthat character), or a single character with no other significance
X(matching that character).
X.PP
XA \fIrange\fR is a sequence of characters enclosed in `[]'.
XIt normally matches any single character from the sequence.
XIf the sequence begins with `^',
Xit matches any single character \fInot\fR from the rest of the sequence.
XIf two characters in the sequence are separated by `\-', this is shorthand
Xfor the full list of ASCII characters between them
X(e.g. `[0-9]' matches any decimal digit).
XTo include a literal `]' in the sequence, make it the first character
X(following a possible `^').
XTo include a literal `\-', make it the first or last character.
X.SH AMBIGUITY
XIf a regular expression could match two different parts of the input string,
Xit will match the one which begins earliest.
XIf both begin in the same place	but match different lengths, or match
Xthe same length in different ways, life gets messier, as follows.
X.PP
XIn general, the possibilities in a list of branches are considered in
Xleft-to-right order, the possibilities for `*', `+', and `?' are
Xconsidered longest-first, nested constructs are considered from the
Xoutermost in, and concatenated constructs are considered leftmost-first.
XThe match that will be chosen is the one that uses the earliest
Xpossibility in the first choice that has to be made.
XIf there is more than one choice, the next will be made in the same manner
X(earliest possibility) subject to the decision on the first choice.
XAnd so forth.
X.PP
XFor example, `(ab|a)b*c' could match `abc' in one of two ways.
XThe first choice is between `ab' and `a'; since `ab' is earlier, and does
Xlead to a successful overall match, it is chosen.
XSince the `b' is already spoken for,
Xthe `b*' must match its last possibility\(emthe empty string\(emsince
Xit must respect the earlier choice.
X.PP
XIn the particular case where no `|'s are present and there is only one
X`*', `+', or `?', the net effect is that the longest possible
Xmatch will be chosen.
XSo `ab*', presented with `xabbbby', will match `abbbb'.
XNote that if `ab*' is tried against `xabyabbbz', it
Xwill match `ab' just after `x', due to the begins-earliest rule.
X(In effect, the decision on where to start the match is the first choice
Xto be made, hence subsequent choices must respect it even if this leads them
Xto less-preferred alternatives.)
X.SH SEE ALSO
Xegrep(1), expr(1)
X.SH DIAGNOSTICS
X\fIRegcomp\fR returns NULL for a failure
X(\fIregerror\fR permitting),
Xwhere failures are syntax errors, exceeding implementation limits,
Xor applying `+' or `*' to a possibly-null operand.
X.SH HISTORY
XBoth code and manual page were
Xwritten at U of T.
XThey are intended to be compatible with the Bell V8 \fIregexp\fR(3),
Xbut are not derived from Bell code.
X.SH BUGS
XEmpty branches and empty regular expressions are not portable to V8.
X.PP
XThe restriction against
Xapplying `*' or `+' to a possibly-null operand is an artifact of the
Xsimplistic implementation.
X.PP
XDoes not support \fIegrep\fR's newline-separated branches;
Xneither does the V8 \fIregexp\fR(3), though.
X.PP
XDue to emphasis on
Xcompactness and simplicity,
Xit's not strikingly fast.
XIt does give special attention to handling simple cases quickly.
E!O!F! regexp.3
echo regexp.c 1>&2
sed -e 's/^X//' > regexp.c <<'E!O!F! regexp.c'
X/*
X * regcomp and regexec -- regsub and regerror are elsewhere
X *
X *	Copyright (c) 1986 by University of Toronto.
X *	Written by Henry Spencer.  Not derived from licensed software.
X *
X *	Permission is granted to anyone to use this software for any
X *	purpose on any computer system, and to redistribute it freely,
X *	subject to the following restrictions:
X *
X *	1. The author is not responsible for the consequences of use of
X *		this software, no matter how awful, even if they arise
X *		from defects in it.
X *
X *	2. The origin of this software must not be misrepresented, either
X *		by explicit claim or by omission.
X *
X *	3. Altered versions must be plainly marked as such, and must not
X *		be misrepresented as being the original software.
X *
X * Beware that some of this code is subtly aware of the way operator
X * precedence is structured in regular expressions.  Serious changes in
X * regular-expression syntax might require a total rethink.
X */
X#include <stdio.h>
X#include "regexp.h"
X#include "regmagic.h"
X
X/*
X * The "internal use only" fields in regexp.h are present to pass info from
X * compile to execute that permits the execute phase to run lots faster on
X * simple cases.  They are:
X *
X * regstart	char that must begin a match; '\0' if none obvious
X * reganch	is the match anchored (at beginning-of-line only)?
X * regmust	string (pointer into program) that match must include, or NULL
X * regmlen	length of regmust string
X *
X * Regstart and reganch permit very fast decisions on suitable starting points
X * for a match, cutting down the work a lot.  Regmust permits fast rejection
X * of lines that cannot possibly match.  The regmust tests are costly enough
X * that regcomp() supplies a regmust only if the r.e. contains something
X * potentially expensive (at present, the only such thing detected is * or +
X * at the start of the r.e., which can involve a lot of backup).  Regmlen is
X * supplied because the test in regexec() needs it and regcomp() is computing
X * it anyway.
X */
X
X/*
X * Structure for regexp "program".  This is essentially a linear encoding
X * of a nondeterministic finite-state machine (aka syntax charts or
X * "railroad normal form" in parsing technology).  Each node is an opcode
X * plus a "next" pointer, possibly plus an operand.  "Next" pointers of
X * all nodes except BRANCH implement concatenation; a "next" pointer with
X * a BRANCH on both ends of it is connecting two alternatives.  (Here we
X * have one of the subtle syntax dependencies:  an individual BRANCH (as
X * opposed to a collection of them) is never concatenated with anything
X * because of operator precedence.)  The operand of some types of node is
X * a literal string; for others, it is a node leading into a sub-FSM.  In
X * particular, the operand of a BRANCH node is the first node of the branch.
X * (NB this is *not* a tree structure:  the tail of the branch connects
X * to the thing following the set of BRANCHes.)  The opcodes are:
X */
X
X/* definition	number	opnd?	meaning */
X#define	END	0	/* no	End of program. */
X#define	BOL	1	/* no	Match "" at beginning of line. */
X#define	EOL	2	/* no	Match "" at end of line. */
X#define	ANY	3	/* no	Match any one character. */
X#define	ANYOF	4	/* str	Match any character in this string. */
X#define	ANYBUT	5	/* str	Match any character not in this string. */
X#define	BRANCH	6	/* node	Match this alternative, or the next... */
X#define	BACK	7	/* no	Match "", "next" ptr points backward. */
X#define	EXACTLY	8	/* str	Match this string. */
X#define	NOTHING	9	/* no	Match empty string. */
X#define	STAR	10	/* node	Match this (simple) thing 0 or more times. */
X#define	PLUS	11	/* node	Match this (simple) thing 1 or more times. */
X#define	OPEN	20	/* no	Mark this point in input as start of #n. */
X			/*	OPEN+1 is number 1, etc. */
X#define	CLOSE	30	/* no	Analogous to OPEN. */
X
X/*
X * Opcode notes:
X *
X * BRANCH	The set of branches constituting a single choice are hooked
X *		together with their "next" pointers, since precedence prevents
X *		anything being concatenated to any individual branch.  The
X *		"next" pointer of the last BRANCH in a choice points to the
X *		thing following the whole choice.  This is also where the
X *		final "next" pointer of each individual branch points; each
X *		branch starts with the operand node of a BRANCH node.
X *
X * BACK		Normal "next" pointers all implicitly point forward; BACK
X *		exists to make loop structures possible.
X *
X * STAR,PLUS	'?', and complex '*' and '+', are implemented as circular
X *		BRANCH structures using BACK.  Simple cases (one character
X *		per match) are implemented with STAR and PLUS for speed
X *		and to minimize recursive plunges.
X *
X * OPEN,CLOSE	...are numbered at compile time.
X */
X
X/*
X * A node is one char of opcode followed by two chars of "next" pointer.
X * "Next" pointers are stored as two 8-bit pieces, high order first.  The
X * value is a positive offset from the opcode of the node containing it.
X * An operand, if any, simply follows the node.  (Note that much of the
X * code generation knows about this implicit relationship.)
X *
X * Using two bytes for the "next" pointer is vast overkill for most things,
X * but allows patterns to get big without disasters.
X */
X#define	OP(p)	(*(p))
X#define	NEXT(p)	(((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
X#define	OPERAND(p)	((p) + 3)
X
X/*
X * See regmagic.h for one further detail of program structure.
X */
X
X
X/*
X * Utility definitions.
X */
X#ifndef CHARBITS
X#define	UCHARAT(p)	((int)*(unsigned char *)(p))
X#else
X#define	UCHARAT(p)	((int)*(p)&CHARBITS)
X#endif
X
X#define	FAIL(m)	{ regerror(m); return(NULL); }
X#define	ISMULT(c)	((c) == '*' || (c) == '+' || (c) == '?')
X#define	META	"^$.[()|?+*\\"
X
X/*
X * Flags to be passed up and down.
X */
X#define	HASWIDTH	01	/* Known never to match null string. */
X#define	SIMPLE		02	/* Simple enough to be STAR/PLUS operand. */
X#define	SPSTART		04	/* Starts with * or +. */
X#define	WORST		0	/* Worst case. */
X
X/*
X * Global work variables for regcomp().
X */
Xstatic char *regparse;		/* Input-scan pointer. */
Xstatic int regnpar;		/* () count. */
Xstatic char regdummy;
Xstatic char *regcode;		/* Code-emit pointer; &regdummy = don't. */
Xstatic long regsize;		/* Code size. */
X
X/*
X * Forward declarations for regcomp()'s friends.
X */
X#ifndef STATIC
X#define	STATIC	static
X#endif
XSTATIC char *reg();
XSTATIC char *regbranch();
XSTATIC char *regpiece();
XSTATIC char *regatom();
XSTATIC char *regnode();
XSTATIC char *regnext();
XSTATIC void regc();
XSTATIC void reginsert();
XSTATIC void regtail();
XSTATIC void regoptail();
X#ifdef STRCSPN
XSTATIC int strcspn();
X#endif
X
X/*
X - regcomp - compile a regular expression into internal code
X *
X * We can't allocate space until we know how big the compiled form will be,
X * but we can't compile it (and thus know how big it is) until we've got a
X * place to put the code.  So we cheat:  we compile it twice, once with code
X * generation turned off and size counting turned on, and once "for real".
X * This also means that we don't allocate space until we are sure that the
X * thing really will compile successfully, and we never have to move the
X * code and thus invalidate pointers into it.  (Note that it has to be in
X * one piece because free() must be able to free it all.)
X *
X * Beware that the optimization-preparation code in here knows about some
X * of the structure of the compiled regexp.
X */
Xregexp *
Xregcomp(exp)
Xchar *exp;
X{
X	register regexp *r;
X	register char *scan;
X	register char *longest;
X	register int len;
X	int flags;
X	extern char *malloc();
X
X	if (exp == NULL)
X		FAIL("NULL argument");
X
X	/* First pass: determine size, legality. */
X	regparse = exp;
X	regnpar = 1;
X	regsize = 0L;
X	regcode = &regdummy;
X	regc(MAGIC);
X	if (reg(0, &flags) == NULL)
X		return(NULL);
X
X	/* Small enough for pointer-storage convention? */
X	if (regsize >= 32767L)		/* Probably could be 65535L. */
X		FAIL("regexp too big");
X
X	/* Allocate space. */
X	r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
X	if (r == NULL)
X		FAIL("out of space");
X
X	/* Second pass: emit code. */
X	regparse = exp;
X	regnpar = 1;
X	regcode = r->program;
X	regc(MAGIC);
X	if (reg(0, &flags) == NULL)
X		return(NULL);
X
X	/* Dig out information for optimizations. */
X	r->regstart = '\0';	/* Worst-case defaults. */
X	r->reganch = 0;
X	r->regmust = NULL;
X	r->regmlen = 0;
X	scan = r->program+1;			/* First BRANCH. */
X	if (OP(regnext(scan)) == END) {		/* Only one top-level choice. */
X		scan = OPERAND(scan);
X
X		/* Starting-point info. */
X		if (OP(scan) == EXACTLY)
X			r->regstart = *OPERAND(scan);
X		else if (OP(scan) == BOL)
X			r->reganch++;
X
X		/*
X		 * If there's something expensive in the r.e., find the
X		 * longest literal string that must appear and make it the
X		 * regmust.  Resolve ties in favor of later strings, since
X		 * the regstart check works with the beginning of the r.e.
X		 * and avoiding duplication strengthens checking.  Not a
X		 * strong reason, but sufficient in the absence of others.
X		 */
X		if (flags&SPSTART) {
X			longest = NULL;
X			len = 0;
X			for (; scan != NULL; scan = regnext(scan))
X				if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
X					longest = OPERAND(scan);
X					len = strlen(OPERAND(scan));
X				}
X			r->regmust = longest;
X			r->regmlen = len;
X		}
X	}
X
X	return(r);
X}
X
X/*
X - reg - regular expression, i.e. main body or parenthesized thing
X *
X * Caller must absorb opening parenthesis.
X *
X * Combining parenthesis handling with the base level of regular expression
X * is a trifle forced, but the need to tie the tails of the branches to what
X * follows makes it hard to avoid.
X */
Xstatic char *
Xreg(paren, flagp)
Xint paren;			/* Parenthesized? */
Xint *flagp;
X{
X	register char *ret;
X	register char *br;
X	register char *ender;
X	register int parno;
X	int flags;
X
X	*flagp = HASWIDTH;	/* Tentatively. */
X
X	/* Make an OPEN node, if parenthesized. */
X	if (paren) {
X		if (regnpar >= NSUBEXP)
X			FAIL("too many ()");
X		parno = regnpar;
X		regnpar++;
X		ret = regnode(OPEN+parno);
X	} else
X		ret = NULL;
X
X	/* Pick up the branches, linking them together. */
X	br = regbranch(&flags);
X	if (br == NULL)
X		return(NULL);
X	if (ret != NULL)
X		regtail(ret, br);	/* OPEN -> first. */
X	else
X		ret = br;
X	if (!(flags&HASWIDTH))
X		*flagp &= ~HASWIDTH;
X	*flagp |= flags&SPSTART;
X	while (*regparse == '|') {
X		regparse++;
X		br = regbranch(&flags);
X		if (br == NULL)
X			return(NULL);
X		regtail(ret, br);	/* BRANCH -> BRANCH. */
X		if (!(flags&HASWIDTH))
X			*flagp &= ~HASWIDTH;
X		*flagp |= flags&SPSTART;
X	}
X
X	/* Make a closing node, and hook it on the end. */
X	ender = regnode((paren) ? CLOSE+parno : END);	
X	regtail(ret, ender);
X
X	/* Hook the tails of the branches to the closing node. */
X	for (br = ret; br != NULL; br = regnext(br))
X		regoptail(br, ender);
X
X	/* Check for proper termination. */
X	if (paren && *regparse++ != ')') {
X		FAIL("unmatched ()");
X	} else if (!paren && *regparse != '\0') {
X		if (*regparse == ')') {
X			FAIL("unmatched ()");
X		} else
X			FAIL("junk on end");	/* "Can't happen". */
X		/* NOTREACHED */
X	}
X
X	return(ret);
X}
X
X/*
X - regbranch - one alternative of an | operator
X *
X * Implements the concatenation operator.
X */
Xstatic char *
Xregbranch(flagp)
Xint *flagp;
X{
X	register char *ret;
X	register char *chain;
X	register char *latest;
X	int flags;
X
X	*flagp = WORST;		/* Tentatively. */
X
X	ret = regnode(BRANCH);
X	chain = NULL;
X	while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
X		latest = regpiece(&flags);
X		if (latest == NULL)
X			return(NULL);
X		*flagp |= flags&HASWIDTH;
X		if (chain == NULL)	/* First piece. */
X			*flagp |= flags&SPSTART;
X		else
X			regtail(chain, latest);
X		chain = latest;
X	}
X	if (chain == NULL)	/* Loop ran zero times. */
X		(void) regnode(NOTHING);
X
X	return(ret);
X}
X
X/*
X - regpiece - something followed by possible [*+?]
X *
X * Note that the branching code sequences used for ? and the general cases
X * of * and + are somewhat optimized:  they use the same NOTHING node as
X * both the endmarker for their branch list and the body of the last branch.
X * It might seem that this node could be dispensed with entirely, but the
X * endmarker role is not redundant.
X */
Xstatic char *
Xregpiece(flagp)
Xint *flagp;
X{
X	register char *ret;
X	register char op;
X	register char *next;
X	int flags;
X
X	ret = regatom(&flags);
X	if (ret == NULL)
X		return(NULL);
X
X	op = *regparse;
X	if (!ISMULT(op)) {
X		*flagp = flags;
X		return(ret);
X	}
X
X	if (!(flags&HASWIDTH) && op != '?')
X		FAIL("*+ operand could be empty");
X	*flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
X
X	if (op == '*' && (flags&SIMPLE))
X		reginsert(STAR, ret);
X	else if (op == '*') {
X		/* Emit x* as (x&|), where & means "self". */
X		reginsert(BRANCH, ret);			/* Either x */
X		regoptail(ret, regnode(BACK));		/* and loop */
X		regoptail(ret, ret);			/* back */
X		regtail(ret, regnode(BRANCH));		/* or */
X		regtail(ret, regnode(NOTHING));		/* null. */
X	} else if (op == '+' && (flags&SIMPLE))
X		reginsert(PLUS, ret);
X	else if (op == '+') {
X		/* Emit x+ as x(&|), where & means "self". */
X		next = regnode(BRANCH);			/* Either */
X		regtail(ret, next);
X		regtail(regnode(BACK), ret);		/* loop back */
X		regtail(next, regnode(BRANCH));		/* or */
X		regtail(ret, regnode(NOTHING));		/* null. */
X	} else if (op == '?') {
X		/* Emit x? as (x|) */
X		reginsert(BRANCH, ret);			/* Either x */
X		regtail(ret, regnode(BRANCH));		/* or */
X		next = regnode(NOTHING);		/* null. */
X		regtail(ret, next);
X		regoptail(ret, next);
X	}
X	regparse++;
X	if (ISMULT(*regparse))
X		FAIL("nested *?+");
X
X	return(ret);
X}
X
X/*
X - regatom - the lowest level
X *
X * Optimization:  gobbles an entire sequence of ordinary characters so that
X * it can turn them into a single node, which is smaller to store and
X * faster to run.  Backslashed characters are exceptions, each becoming a
X * separate node; the code is simpler that way and it's not worth fixing.
X */
Xstatic char *
Xregatom(flagp)
Xint *flagp;
X{
X	register char *ret;
X	int flags;
X
X	*flagp = WORST;		/* Tentatively. */
X
X	switch (*regparse++) {
X	case '^':
X		ret = regnode(BOL);
X		break;
X	case '$':
X		ret = regnode(EOL);
X		break;
X	case '.':
X		ret = regnode(ANY);
X		*flagp |= HASWIDTH|SIMPLE;
X		break;
X	case '[': {
X			register int class;
X			register int classend;
X
X			if (*regparse == '^') {	/* Complement of range. */
X				ret = regnode(ANYBUT);
X				regparse++;
X			} else
X				ret = regnode(ANYOF);
X			if (*regparse == ']' || *regparse == '-')
X				regc(*regparse++);
X			while (*regparse != '\0' && *regparse != ']') {
X				if (*regparse == '-') {
X					regparse++;
X					if (*regparse == ']' || *regparse == '\0')
X						regc('-');
X					else {
X						class = UCHARAT(regparse-2)+1;
X						classend = UCHARAT(regparse);
X						if (class > classend+1)
X							FAIL("invalid [] range");
X						for (; class <= classend; class++)
X							regc(class);
X						regparse++;
X					}
X				} else
X					regc(*regparse++);
X			}
X			regc('\0');
X			if (*regparse != ']')
X				FAIL("unmatched []");
X			regparse++;
X			*flagp |= HASWIDTH|SIMPLE;
X		}
X		break;
X	case '(':
X		ret = reg(1, &flags);
X		if (ret == NULL)
X			return(NULL);
X		*flagp |= flags&(HASWIDTH|SPSTART);
X		break;
X	case '\0':
X	case '|':
X	case ')':
X		FAIL("internal urp");	/* Supposed to be caught earlier. */
X		break;
X	case '?':
X	case '+':
X	case '*':
X		FAIL("?+* follows nothing");
X		break;
X	case '\\':
X		if (*regparse == '\0')
X			FAIL("trailing \\");
X		ret = regnode(EXACTLY);
X		regc(*regparse++);
X		regc('\0');
X		*flagp |= HASWIDTH|SIMPLE;
X		break;
X	default: {
X			register int len;
X			register char ender;
X
X			regparse--;
X			len = strcspn(regparse, META);
X			if (len <= 0)
X				FAIL("internal disaster");
X			ender = *(regparse+len);
X			if (len > 1 && ISMULT(ender))
X				len--;		/* Back off clear of ?+* operand. */
X			*flagp |= HASWIDTH;
X			if (len == 1)
X				*flagp |= SIMPLE;
X			ret = regnode(EXACTLY);
X			while (len > 0) {
X				regc(*regparse++);
X				len--;
X			}
X			regc('\0');
X		}
X		break;
X	}
X
X	return(ret);
X}
X
X/*
X - regnode - emit a node
X */
Xstatic char *			/* Location. */
Xregnode(op)
Xchar op;
X{
X	register char *ret;
X	register char *ptr;
X
X	ret = regcode;
X	if (ret == &regdummy) {
X		regsize += 3;
X		return(ret);
X	}
X
X	ptr = ret;
X	*ptr++ = op;
X	*ptr++ = '\0';		/* Null "next" pointer. */
X	*ptr++ = '\0';
X	regcode = ptr;
X
X	return(ret);
X}
X
X/*
X - regc - emit (if appropriate) a byte of code
X */
Xstatic void
Xregc(b)
Xchar b;
X{
X	if (regcode != &regdummy)
X		*regcode++ = b;
X	else
X		regsize++;
X}
X
X/*
X - reginsert - insert an operator in front of already-emitted operand
X *
X * Means relocating the operand.
X */
Xstatic void
Xreginsert(op, opnd)
Xchar op;
Xchar *opnd;
X{
X	register char *src;
X	register char *dst;
X	register char *place;
X
X	if (regcode == &regdummy) {
X		regsize += 3;
X		return;
X	}
X
X	src = regcode;
X	regcode += 3;
X	dst = regcode;
X	while (src > opnd)
X		*--dst = *--src;
X
X	place = opnd;		/* Op node, where operand used to be. */
X	*place++ = op;
X	*place++ = '\0';
X	*place++ = '\0';
X}
X
X/*
X - regtail - set the next-pointer at the end of a node chain
X */
Xstatic void
Xregtail(p, val)
Xchar *p;
Xchar *val;
X{
X	register char *scan;
X	register char *temp;
X	register int offset;
X
X	if (p == &regdummy)
X		return;
X
X	/* Find last node. */
X	scan = p;
X	for (;;) {
X		temp = regnext(scan);
X		if (temp == NULL)
X			break;
X		scan = temp;
X	}
X
X	if (OP(scan) == BACK)
X		offset = scan - val;
X	else
X		offset = val - scan;
X	*(scan+1) = (offset>>8)&0377;
X	*(scan+2) = offset&0377;
X}
X
X/*
X - regoptail - regtail on operand of first argument; nop if operandless
X */
Xstatic void
Xregoptail(p, val)
Xchar *p;
Xchar *val;
X{
X	/* "Operandless" and "op != BRANCH" are synonymous in practice. */
X	if (p == NULL || p == &regdummy || OP(p) != BRANCH)
X		return;
X	regtail(OPERAND(p), val);
X}
X
X/*
X * regexec and friends
X */
X
X/*
X * Global work variables for regexec().
X */
Xstatic char *reginput;		/* String-input pointer. */
Xstatic char *regbol;		/* Beginning of input, for ^ check. */
Xstatic char **regstartp;	/* Pointer to startp array. */
Xstatic char **regendp;		/* Ditto for endp. */
X
X/*
X * Forwards.
X */
XSTATIC int regtry();
XSTATIC int regmatch();
XSTATIC int regrepeat();
X
X#ifdef DEBUG
Xint regnarrate = 0;
Xvoid regdump();
XSTATIC char *regprop();
X#endif
X
X/*
X - regexec - match a regexp against a string
X */
Xint
Xregexec(prog, string)
Xregister regexp *prog;
Xregister char *string;
X{
X	register char *s;
X	extern char *strchr();
X
X	/* Be paranoid... */
X	if (prog == NULL || string == NULL) {
X		regerror("NULL parameter");
X		return(0);
X	}
X
X	/* Check validity of program. */
X	if (UCHARAT(prog->program) != MAGIC) {
X		regerror("corrupted program");
X		return(0);
X	}
X
X	/* If there is a "must appear" string, look for it. */
X	if (prog->regmust != NULL) {
X		s = string;
X		while ((s = strchr(s, prog->regmust[0])) != NULL) {
X			if (strncmp(s, prog->regmust, prog->regmlen) == 0)
X				break;	/* Found it. */
X			s++;
X		}
X		if (s == NULL)	/* Not present. */
X			return(0);
X	}
X
X	/* Mark beginning of line for ^ . */
X	regbol = string;
X
X	/* Simplest case:  anchored match need be tried only once. */
X	if (prog->reganch)
X		return(regtry(prog, string));
X
X	/* Messy cases:  unanchored match. */
X	s = string;
X	if (prog->regstart != '\0')
X		/* We know what char it must start with. */
X		while ((s = strchr(s, prog->regstart)) != NULL) {
X			if (regtry(prog, s))
X				return(1);
X			s++;
X		}
X	else
X		/* We don't -- general case. */
X		do {
X			if (regtry(prog, s))
X				return(1);
X		} while (*s++ != '\0');
X
X	/* Failure. */
X	return(0);
X}
X
X/*
X - regtry - try match at specific point
X */
Xstatic int			/* 0 failure, 1 success */
Xregtry(prog, string)
Xregexp *prog;
Xchar *string;
X{
X	register int i;
X	register char **sp;
X	register char **ep;
X
X	reginput = string;
X	regstartp = prog->startp;
X	regendp = prog->endp;
X
X	sp = prog->startp;
X	ep = prog->endp;
X	for (i = NSUBEXP; i > 0; i--) {
X		*sp++ = NULL;
X		*ep++ = NULL;
X	}
X	if (regmatch(prog->program + 1)) {
X		prog->startp[0] = string;
X		prog->endp[0] = reginput;
X		return(1);
X	} else
X		return(0);
X}
X
X/*
X - regmatch - main matching routine
X *
X * Conceptually the strategy is simple:  check to see whether the current
X * node matches, call self recursively to see whether the rest matches,
X * and then act accordingly.  In practice we make some effort to avoid
X * recursion, in particular by going through "ordinary" nodes (that don't
X * need to know whether the rest of the match failed) by a loop instead of
X * by recursion.
X */
Xstatic int			/* 0 failure, 1 success */
Xregmatch(prog)
Xchar *prog;
X{
X	register char *scan;	/* Current node. */
X	char *next;		/* Next node. */
X	extern char *strchr();
X
X	scan = prog;
X#ifdef DEBUG
X	if (scan != NULL && regnarrate)
X		fprintf(stderr, "%s(\n", regprop(scan));
X#endif
X	while (scan != NULL) {
X#ifdef DEBUG
X		if (regnarrate)
X			fprintf(stderr, "%s...\n", regprop(scan));
X#endif
X		next = regnext(scan);
X
X		switch (OP(scan)) {
X		case BOL:
X			if (reginput != regbol)
X				return(0);
X			break;
X		case EOL:
X			if (*reginput != '\0')
X				return(0);
X			break;
X		case ANY:
X			if (*reginput == '\0')
X				return(0);
X			reginput++;
X			break;
X		case EXACTLY: {
X				register int len;
X				register char *opnd;
X
X				opnd = OPERAND(scan);
X				/* Inline the first character, for speed. */
X				if (*opnd != *reginput)
X					return(0);
X				len = strlen(opnd);
X				if (len > 1 && strncmp(opnd, reginput, len) != 0)
X					return(0);
X				reginput += len;
X			}
X			break;
X		case ANYOF:
X			if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
X				return(0);
X			reginput++;
X			break;
X		case ANYBUT:
X			if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
X				return(0);
X			reginput++;
X			break;
X		case NOTHING:
X			break;
X		case BACK:
X			break;
X		case OPEN+1:
X		case OPEN+2:
X		case OPEN+3:
X		case OPEN+4:
X		case OPEN+5:
X		case OPEN+6:
X		case OPEN+7:
X		case OPEN+8:
X		case OPEN+9: {
X				register int no;
X				register char *save;
X
X				no = OP(scan) - OPEN;
X				save = reginput;
X
X				if (regmatch(next)) {
X					/*
X					 * Don't set startp if some later
X					 * invocation of the same parentheses
X					 * already has.
X					 */
X					if (regstartp[no] == NULL)
X						regstartp[no] = save;
X					return(1);
X				} else
X					return(0);
X			}
X			break;
X		case CLOSE+1:
X		case CLOSE+2:
X		case CLOSE+3:
X		case CLOSE+4:
X		case CLOSE+5:
X		case CLOSE+6:
X		case CLOSE+7:
X		case CLOSE+8:
X		case CLOSE+9: {
X				register int no;
X				register char *save;
X
X				no = OP(scan) - CLOSE;
X				save = reginput;
X
X				if (regmatch(next)) {
X					/*
X					 * Don't set endp if some later
X					 * invocation of the same parentheses
X					 * already has.
X					 */
X					if (regendp[no] == NULL)
X						regendp[no] = save;
X					return(1);
X				} else
X					return(0);
X			}
X			break;
X		case BRANCH: {
X				register char *save;
X
X				if (OP(next) != BRANCH)		/* No choice. */
X					next = OPERAND(scan);	/* Avoid recursion. */
X				else {
X					do {
X						save = reginput;
X						if (regmatch(OPERAND(scan)))
X							return(1);
X						reginput = save;
X						scan = regnext(scan);
X					} while (scan != NULL && OP(scan) == BRANCH);
X					return(0);
X					/* NOTREACHED */
X				}
X			}
X			break;
X		case STAR:
X		case PLUS: {
X				register char nextch;
X				register int no;
X				register char *save;
X				register int min;
X
X				/*
X				 * Lookahead to avoid useless match attempts
X				 * when we know what character comes next.
X				 */
X				nextch = '\0';
X				if (OP(next) == EXACTLY)
X					nextch = *OPERAND(next);
X				min = (OP(scan) == STAR) ? 0 : 1;
X				save = reginput;
X				no = regrepeat(OPERAND(scan));
X				while (no >= min) {
X					/* If it could work, try it. */
X					if (nextch == '\0' || *reginput == nextch)
X						if (regmatch(next))
X							return(1);
X					/* Couldn't or didn't -- back up. */
X					no--;
X					reginput = save + no;
X				}
X				return(0);
X			}
X			break;
X		case END:
X			return(1);	/* Success! */
X			break;
X		default:
X			regerror("memory corruption");
X			return(0);
X			break;
X		}
X
X		scan = next;
X	}
X
X	/*
X	 * We get here only if there's trouble -- normally "case END" is
X	 * the terminating point.
X	 */
X	regerror("corrupted pointers");
X	return(0);
X}
X
X/*
X - regrepeat - repeatedly match something simple, report how many
X */
Xstatic int
Xregrepeat(p)
Xchar *p;
X{
X	register int count = 0;
X	register char *scan;
X	register char *opnd;
X
X	scan = reginput;
X	opnd = OPERAND(p);
X	switch (OP(p)) {
X	case ANY:
X		count = strlen(scan);
X		scan += count;
X		break;
X	case EXACTLY:
X		while (*opnd == *scan) {
X			count++;
X			scan++;
X		}
X		break;
X	case ANYOF:
X		while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
X			count++;
X			scan++;
X		}
X		break;
X	case ANYBUT:
X		while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
X			count++;
X			scan++;
X		}
X		break;
X	default:		/* Oh dear.  Called inappropriately. */
X		regerror("internal foulup");
X		count = 0;	/* Best compromise. */
X		break;
X	}
X	reginput = scan;
X
X	return(count);
X}
X
X/*
X - regnext - dig the "next" pointer out of a node
X */
Xstatic char *
Xregnext(p)
Xregister char *p;
X{
X	register int offset;
X
X	if (p == &regdummy)
X		return(NULL);
X
X	offset = NEXT(p);
X	if (offset == 0)
X		return(NULL);
X
X	if (OP(p) == BACK)
X		return(p-offset);
X	else
X		return(p+offset);
X}
X
X#ifdef DEBUG
X
XSTATIC char *regprop();
X
X/*
X - regdump - dump a regexp onto stdout in vaguely comprehensible form
X */
Xvoid
Xregdump(r)
Xregexp *r;
X{
X	register char *s;
X	register char op = EXACTLY;	/* Arbitrary non-END op. */
X	register char *next;
X	extern char *strchr();
X
X
X	s = r->program + 1;
X	while (op != END) {	/* While that wasn't END last time... */
X		op = OP(s);
X		printf("%2d%s", s-r->program, regprop(s));	/* Where, what. */
X		next = regnext(s);
X		if (next == NULL)		/* Next ptr. */
X			printf("(0)");
X		else 
X			printf("(%d)", (s-r->program)+(next-s));
X		s += 3;
X		if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
X			/* Literal string, where present. */
X			while (*s != '\0') {
X				putchar(*s);
X				s++;
X			}
X			s++;
X		}
X		putchar('\n');
X	}
X
X	/* Header fields of interest. */
X	if (r->regstart != '\0')
X		printf("start `%c' ", r->regstart);
X	if (r->reganch)
X		printf("anchored ");
X	if (r->regmust != NULL)
X		printf("must have \"%s\"", r->regmust);
X	printf("\n");
X}
X
X/*
X - regprop - printable representation of opcode
X */
Xstatic char *
Xregprop(op)
Xchar *op;
X{
X	register char *p;
X	static char buf[50];
X
X	(void) strcpy(buf, ":");
X
X	switch (OP(op)) {
X	case BOL:
X		p = "BOL";
X		break;
X	case EOL:
X		p = "EOL";
X		break;
X	case ANY:
X		p = "ANY";
X		break;
X	case ANYOF:
X		p = "ANYOF";
X		break;
X	case ANYBUT:
X		p = "ANYBUT";
X		break;
X	case BRANCH:
X		p = "BRANCH";
X		break;
X	case EXACTLY:
X		p = "EXACTLY";
X		break;
X	case NOTHING:
X		p = "NOTHING";
X		break;
X	case BACK:
X		p = "BACK";
X		break;
X	case END:
X		p = "END";
X		break;
X	case OPEN+1:
X	case OPEN+2:
X	case OPEN+3:
X	case OPEN+4:
X	case OPEN+5:
X	case OPEN+6:
X	case OPEN+7:
X	case OPEN+8:
X	case OPEN+9:
X		sprintf(buf+strlen(buf), "OPEN%d", OP(op)-OPEN);
X		p = NULL;
X		break;
X	case CLOSE+1:
X	case CLOSE+2:
X	case CLOSE+3:
X	case CLOSE+4:
X	case CLOSE+5:
X	case CLOSE+6:
X	case CLOSE+7:
X	case CLOSE+8:
X	case CLOSE+9:
X		sprintf(buf+strlen(buf), "CLOSE%d", OP(op)-CLOSE);
X		p = NULL;
X		break;
X	case STAR:
X		p = "STAR";
X		break;
X	case PLUS:
X		p = "PLUS";
X		break;
X	default:
X		regerror("corrupted opcode");
X		break;
X	}
X	if (p != NULL)
X		(void) strcat(buf, p);
X	return(buf);
X}
X#endif
X
X/*
X * The following is provided for those people who do not have strcspn() in
X * their C libraries.  They should get off their butts and do something
X * about it; at least one public-domain implementation of those (highly
X * useful) string routines has been published on Usenet.
X */
X#ifdef STRCSPN
X/*
X * strcspn - find length of initial segment of s1 consisting entirely
X * of characters not from s2
X */
X
Xstatic int
Xstrcspn(s1, s2)
Xchar *s1;
Xchar *s2;
X{
X	register char *scan1;
X	register char *scan2;
X	register int count;
X
X	count = 0;
X	for (scan1 = s1; *scan1 != '\0'; scan1++) {
X		for (scan2 = s2; *scan2 != '\0';)	/* ++ moved down. */
X			if (*scan1 == *scan2++)
X				return(count);
X		count++;
X	}
X	return(count);
X}
X#endif
E!O!F! regexp.c
echo regexp.h 1>&2
sed -e 's/^X//' > regexp.h <<'E!O!F! regexp.h'
X/*
X * Definitions etc. for regexp(3) routines.
X *
X * Caveat:  this is V8 regexp(3) [actually, a reimplementation thereof],
X * not the System V one.
X */
X#define NSUBEXP  10
Xtypedef struct regexp {
X	char *startp[NSUBEXP];
X	char *endp[NSUBEXP];
X	char regstart;		/* Internal use only. */
X	char reganch;		/* Internal use only. */
X	char *regmust;		/* Internal use only. */
X	int regmlen;		/* Internal use only. */
X	char program[1];	/* Unwarranted chumminess with compiler. */
X} regexp;
X
Xextern regexp *regcomp();
Xextern int regexec();
Xextern void regsub();
Xextern void regerror();
E!O!F! regexp.h
echo regmagic.h 1>&2
sed -e 's/^X//' > regmagic.h <<'E!O!F! regmagic.h'
X/*
X * The first byte of the regexp internal "program" is actually this magic
X * number; the start node begins in the second byte.
X */
X#define	MAGIC	0234
E!O!F! regmagic.h
echo regsub.c 1>&2
sed -e 's/^X//' > regsub.c <<'E!O!F! regsub.c'
X/*
X * regsub
X *
X *	Copyright (c) 1986 by University of Toronto.
X *	Written by Henry Spencer.  Not derived from licensed software.
X *
X *	Permission is granted to anyone to use this software for any
X *	purpose on any computer system, and to redistribute it freely,
X *	subject to the following restrictions:
X *
X *	1. The author is not responsible for the consequences of use of
X *		this software, no matter how awful, even if they arise
X *		from defects in it.
X *
X *	2. The origin of this software must not be misrepresented, either
X *		by explicit claim or by omission.
X *
X *	3. Altered versions must be plainly marked as such, and must not
X *		be misrepresented as being the original software.
X */
X#include <stdio.h>
X#include "regexp.h"
X#include "regmagic.h"
X
X#ifndef CHARBITS
X#define	UCHARAT(p)	((int)*(unsigned char *)(p))
X#else
X#define	UCHARAT(p)	((int)*(p)&CHARBITS)
X#endif
X
X/*
X - regsub - perform substitutions after a regexp match
X */
Xvoid
Xregsub(prog, source, dest)
Xregexp *prog;
Xchar *source;
Xchar *dest;
X{
X	register char *src;
X	register char *dst;
X	register char c;
X	register int no;
X	register int len;
X	extern char *strncpy();
X
X	if (prog == NULL || source == NULL || dest == NULL) {
X		regerror("NULL parm to regsub");
X		return;
X	}
X	if (UCHARAT(prog->program) != MAGIC) {
X		regerror("damaged regexp fed to regsub");
X		return;
X	}
X
X	src = source;
X	dst = dest;
X	while ((c = *src++) != '\0') {
X		if (c == '&')
X			no = 0;
X	

--
Dan Heller
------------------------------------------------
O'Reilly && Associates 		      Zyrcom Inc
Senior Writer			       President
argv@ora.com			argv@zipcode.com