head	1.25;
access;
symbols
	netbsd-11-0-RC4:1.25
	netbsd-11-0-RC3:1.25
	netbsd-11-0-RC2:1.25
	netbsd-11-0-RC1:1.25
	perseant-exfatfs-base-20250801:1.25
	netbsd-11:1.25.0.2
	netbsd-11-base:1.25
	netbsd-10-1-RELEASE:1.24
	perseant-exfatfs-base-20240630:1.24
	perseant-exfatfs:1.24.0.28
	perseant-exfatfs-base:1.24
	netbsd-8-3-RELEASE:1.24
	netbsd-9-4-RELEASE:1.24
	netbsd-10-0-RELEASE:1.24
	netbsd-10-0-RC6:1.24
	netbsd-10-0-RC5:1.24
	netbsd-10-0-RC4:1.24
	netbsd-10-0-RC3:1.24
	netbsd-10-0-RC2:1.24
	netbsd-10-0-RC1:1.24
	netbsd-10:1.24.0.26
	netbsd-10-base:1.24
	netbsd-9-3-RELEASE:1.24
	cjep_sun2x-base1:1.24
	cjep_sun2x:1.24.0.24
	cjep_sun2x-base:1.24
	cjep_staticlib_x-base1:1.24
	netbsd-9-2-RELEASE:1.24
	cjep_staticlib_x:1.24.0.22
	cjep_staticlib_x-base:1.24
	netbsd-9-1-RELEASE:1.24
	phil-wifi-20200421:1.24
	phil-wifi-20200411:1.24
	is-mlppp:1.24.0.20
	is-mlppp-base:1.24
	phil-wifi-20200406:1.24
	netbsd-8-2-RELEASE:1.24
	netbsd-9-0-RELEASE:1.24
	netbsd-9-0-RC2:1.24
	netbsd-9-0-RC1:1.24
	phil-wifi-20191119:1.24
	netbsd-9:1.24.0.18
	netbsd-9-base:1.24
	phil-wifi-20190609:1.24
	netbsd-8-1-RELEASE:1.24
	netbsd-8-1-RC1:1.24
	pgoyette-compat-merge-20190127:1.24
	pgoyette-compat-20190127:1.24
	pgoyette-compat-20190118:1.24
	pgoyette-compat-1226:1.24
	pgoyette-compat-1126:1.24
	pgoyette-compat-1020:1.24
	pgoyette-compat-0930:1.24
	pgoyette-compat-0906:1.24
	netbsd-7-2-RELEASE:1.23
	pgoyette-compat-0728:1.24
	netbsd-8-0-RELEASE:1.24
	phil-wifi:1.24.0.16
	phil-wifi-base:1.24
	pgoyette-compat-0625:1.24
	netbsd-8-0-RC2:1.24
	pgoyette-compat-0521:1.24
	pgoyette-compat-0502:1.24
	pgoyette-compat-0422:1.24
	netbsd-8-0-RC1:1.24
	pgoyette-compat-0415:1.24
	pgoyette-compat-0407:1.24
	pgoyette-compat-0330:1.24
	pgoyette-compat-0322:1.24
	pgoyette-compat-0315:1.24
	netbsd-7-1-2-RELEASE:1.23
	pgoyette-compat:1.24.0.14
	pgoyette-compat-base:1.24
	netbsd-7-1-1-RELEASE:1.23
	matt-nb8-mediatek:1.24.0.12
	matt-nb8-mediatek-base:1.24
	perseant-stdc-iso10646:1.24.0.10
	perseant-stdc-iso10646-base:1.24
	netbsd-8:1.24.0.8
	netbsd-8-base:1.24
	prg-localcount2-base3:1.24
	prg-localcount2-base2:1.24
	prg-localcount2-base1:1.24
	prg-localcount2:1.24.0.6
	prg-localcount2-base:1.24
	pgoyette-localcount-20170426:1.24
	bouyer-socketcan-base1:1.24
	pgoyette-localcount-20170320:1.24
	netbsd-7-1:1.23.0.16
	netbsd-7-1-RELEASE:1.23
	netbsd-7-1-RC2:1.23
	netbsd-7-nhusb-base-20170116:1.23
	bouyer-socketcan:1.24.0.4
	bouyer-socketcan-base:1.24
	pgoyette-localcount-20170107:1.24
	netbsd-7-1-RC1:1.23
	pgoyette-localcount-20161104:1.24
	netbsd-7-0-2-RELEASE:1.23
	localcount-20160914:1.24
	netbsd-7-nhusb:1.23.0.14
	netbsd-7-nhusb-base:1.23
	pgoyette-localcount-20160806:1.24
	pgoyette-localcount-20160726:1.24
	pgoyette-localcount:1.24.0.2
	pgoyette-localcount-base:1.24
	netbsd-7-0-1-RELEASE:1.23
	netbsd-7-0:1.23.0.12
	netbsd-7-0-RELEASE:1.23
	netbsd-7-0-RC3:1.23
	netbsd-7-0-RC2:1.23
	netbsd-7-0-RC1:1.23
	netbsd-5-2-3-RELEASE:1.18
	netbsd-5-1-5-RELEASE:1.18
	netbsd-6-0-6-RELEASE:1.22
	netbsd-6-1-5-RELEASE:1.22
	netbsd-7:1.23.0.10
	netbsd-7-base:1.23
	yamt-pagecache-base9:1.23
	yamt-pagecache-tag8:1.22.2.1
	netbsd-6-1-4-RELEASE:1.22
	netbsd-6-0-5-RELEASE:1.22
	tls-earlyentropy:1.23.0.8
	tls-earlyentropy-base:1.23
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.23
	riastradh-drm2-base3:1.23
	netbsd-6-1-3-RELEASE:1.22
	netbsd-6-0-4-RELEASE:1.22
	netbsd-5-2-2-RELEASE:1.18
	netbsd-5-1-4-RELEASE:1.18
	netbsd-6-1-2-RELEASE:1.22
	netbsd-6-0-3-RELEASE:1.22
	netbsd-5-2-1-RELEASE:1.18
	netbsd-5-1-3-RELEASE:1.18
	netbsd-6-1-1-RELEASE:1.22
	riastradh-drm2-base2:1.23
	riastradh-drm2-base1:1.23
	riastradh-drm2:1.23.0.4
	riastradh-drm2-base:1.23
	netbsd-6-1:1.22.0.10
	netbsd-6-0-2-RELEASE:1.22
	netbsd-6-1-RELEASE:1.22
	netbsd-6-1-RC4:1.22
	netbsd-6-1-RC3:1.22
	agc-symver:1.23.0.6
	agc-symver-base:1.23
	netbsd-6-1-RC2:1.22
	netbsd-6-1-RC1:1.22
	yamt-pagecache-base8:1.23
	netbsd-5-2:1.18.0.24
	netbsd-6-0-1-RELEASE:1.22
	yamt-pagecache-base7:1.23
	netbsd-5-2-RELEASE:1.18
	netbsd-5-2-RC1:1.18
	matt-nb6-plus-nbase:1.22
	yamt-pagecache-base6:1.23
	netbsd-6-0:1.22.0.8
	netbsd-6-0-RELEASE:1.22
	netbsd-6-0-RC2:1.22
	tls-maxphys:1.23.0.2
	tls-maxphys-base:1.23
	matt-nb6-plus:1.22.0.6
	matt-nb6-plus-base:1.22
	netbsd-6-0-RC1:1.22
	yamt-pagecache-base5:1.23
	yamt-pagecache-base4:1.23
	netbsd-6:1.22.0.4
	netbsd-6-base:1.22
	netbsd-5-1-2-RELEASE:1.18
	netbsd-5-1-1-RELEASE:1.18
	yamt-pagecache-base3:1.22
	yamt-pagecache-base2:1.22
	yamt-pagecache:1.22.0.2
	yamt-pagecache-base:1.22
	cherry-xenmp:1.21.0.2
	cherry-xenmp-base:1.21
	bouyer-quota2-nbase:1.20
	bouyer-quota2:1.20.0.2
	bouyer-quota2-base:1.20
	matt-mips64-premerge-20101231:1.20
	matt-nb5-mips64-premerge-20101231:1.18
	matt-nb5-pq3:1.18.0.22
	matt-nb5-pq3-base:1.18
	netbsd-5-1:1.18.0.20
	netbsd-5-1-RELEASE:1.18
	netbsd-5-1-RC4:1.18
	matt-nb5-mips64-k15:1.18
	netbsd-5-1-RC3:1.18
	netbsd-5-1-RC2:1.18
	netbsd-5-1-RC1:1.18
	netbsd-5-0-2-RELEASE:1.18
	matt-nb5-mips64-premerge-20091211:1.18
	matt-premerge-20091211:1.20
	OPENBSD20091026:1.1.1.3
	OPENBSD:1.1.1
	matt-nb5-mips64-u2-k2-k4-k7-k8-k9:1.18
	matt-nb4-mips64-k7-u2a-k9b:1.18
	matt-nb5-mips64-u1-k1-k5:1.18
	matt-nb5-mips64:1.18.0.18
	netbsd-5-0-1-RELEASE:1.18
	jym-xensuspend-nbase:1.18
	netbsd-5-0:1.18.0.16
	netbsd-5-0-RELEASE:1.18
	netbsd-5-0-RC4:1.18
	netbsd-5-0-RC3:1.18
	netbsd-5-0-RC2:1.18
	jym-xensuspend:1.18.0.14
	jym-xensuspend-base:1.18
	netbsd-5-0-RC1:1.18
	netbsd-5:1.18.0.12
	netbsd-5-base:1.18
	matt-mips64-base2:1.18
	matt-mips64:1.17.0.22
	mjf-devfs2:1.18.0.10
	mjf-devfs2-base:1.18
	netbsd-4-0-1-RELEASE:1.17
	wrstuden-revivesa-base-3:1.18
	wrstuden-revivesa-base-2:1.18
	wrstuden-fixsa-newbase:1.17
	wrstuden-revivesa-base-1:1.18
	yamt-pf42-base4:1.18
	yamt-pf42-base3:1.18
	hpcarm-cleanup-nbase:1.18
	yamt-pf42-baseX:1.18
	yamt-pf42-base2:1.18
	wrstuden-revivesa:1.18.0.8
	wrstuden-revivesa-base:1.18
	yamt-pf42:1.18.0.6
	yamt-pf42-base:1.18
	keiichi-mipv6-nbase:1.18
	keiichi-mipv6:1.18.0.4
	keiichi-mipv6-base:1.18
	matt-armv6-nbase:1.18
	matt-armv6-prevmlocking:1.17.18.1
	wrstuden-fixsa-base-1:1.17
	netbsd-4-0:1.17.0.20
	netbsd-4-0-RELEASE:1.17
	cube-autoconf:1.18.0.2
	cube-autoconf-base:1.18
	netbsd-4-0-RC5:1.17
	netbsd-4-0-RC4:1.17
	netbsd-4-0-RC3:1.17
	netbsd-4-0-RC2:1.17
	netbsd-4-0-RC1:1.17
	matt-armv6:1.17.0.18
	matt-armv6-base:1.18
	matt-mips64-base:1.17
	hpcarm-cleanup:1.17.0.16
	hpcarm-cleanup-base:1.18
	netbsd-3-1-1-RELEASE:1.17
	netbsd-3-0-3-RELEASE:1.17
	wrstuden-fixsa:1.17.0.14
	wrstuden-fixsa-base:1.17
	abandoned-netbsd-4-base:1.17
	abandoned-netbsd-4:1.17.0.8
	netbsd-3-1:1.17.0.10
	netbsd-3-1-RELEASE:1.17
	netbsd-3-0-2-RELEASE:1.17
	netbsd-3-1-RC4:1.17
	netbsd-3-1-RC3:1.17
	netbsd-3-1-RC2:1.17
	netbsd-3-1-RC1:1.17
	netbsd-4:1.17.0.12
	netbsd-4-base:1.17
	chap-midi-nbase:1.17
	netbsd-3-0-1-RELEASE:1.17
	chap-midi:1.17.0.6
	chap-midi-base:1.17
	netbsd-3-0:1.17.0.4
	netbsd-3-0-RELEASE:1.17
	netbsd-3-0-RC6:1.17
	netbsd-3-0-RC5:1.17
	netbsd-3-0-RC4:1.17
	netbsd-3-0-RC3:1.17
	netbsd-3-0-RC2:1.17
	netbsd-3-0-RC1:1.17
	netbsd-2-0-3-RELEASE:1.16.2.1
	netbsd-2-1:1.16.2.1.0.4
	netbsd-2-1-RELEASE:1.16.2.1
	netbsd-2-1-RC6:1.16.2.1
	netbsd-2-1-RC5:1.16.2.1
	netbsd-2-1-RC4:1.16.2.1
	netbsd-2-1-RC3:1.16.2.1
	netbsd-2-1-RC2:1.16.2.1
	netbsd-2-1-RC1:1.16.2.1
	netbsd-2-0-2-RELEASE:1.16.2.1
	netbsd-3:1.17.0.2
	netbsd-3-base:1.17
	netbsd-2-0-1-RELEASE:1.16.2.1
	netbsd-2:1.16.2.1.0.2
	netbsd-2-base:1.16.2.1
	netbsd-2-0-RELEASE:1.16.2.1
	netbsd-2-0-RC5:1.16.2.1
	netbsd-2-0-RC4:1.16.2.1
	netbsd-2-0-RC3:1.16.2.1
	netbsd-2-0-RC2:1.16.2.1
	netbsd-2-0-RC1:1.16.2.1
	netbsd-2-0:1.16.0.2
	netbsd-2-0-base:1.16
	netbsd-1-6-PATCH002-RELEASE:1.15
	netbsd-1-6-PATCH002:1.15
	netbsd-1-6-PATCH002-RC4:1.15
	netbsd-1-6-PATCH002-RC3:1.15
	netbsd-1-6-PATCH002-RC2:1.15
	netbsd-1-6-PATCH002-RC1:1.15
	netbsd-1-6-PATCH001:1.15
	netbsd-1-6-PATCH001-RELEASE:1.15
	netbsd-1-6-PATCH001-RC3:1.15
	netbsd-1-6-PATCH001-RC2:1.15
	netbsd-1-6-PATCH001-RC1:1.15
	fvdl_fs64_base:1.15
	netbsd-1-6-RELEASE:1.15
	netbsd-1-6-RC3:1.15
	netbsd-1-6-RC2:1.15
	netbsd-1-6-RC1:1.15
	netbsd-1-6:1.15.0.2
	netbsd-1-6-base:1.15
	netbsd-1-5-PATCH003:1.10.4.1
	netbsd-1-5-PATCH002:1.10
	netbsd-1-5-PATCH001:1.10
	netbsd-1-5-RELEASE:1.10
	netbsd-1-5-BETA2:1.10
	netbsd-1-5-BETA:1.10
	netbsd-1-4-PATCH003:1.9
	netbsd-1-5-ALPHA2:1.10
	netbsd-1-5:1.10.0.4
	netbsd-1-5-base:1.10
	minoura-xpg4dl-base:1.10
	minoura-xpg4dl:1.10.0.2
	netbsd-1-4-PATCH002:1.9
	wrstuden-devbsize-19991221:1.9
	wrstuden-devbsize:1.9.0.6
	wrstuden-devbsize-base:1.9
	comdex-fall-1999:1.9.0.4
	comdex-fall-1999-base:1.9
	netbsd-1-4-PATCH001:1.9
	netbsd-1-4-RELEASE:1.9
	netbsd-1-4:1.9.0.2
	netbsd-1-4-base:1.9
	netbsd-1-3-PATCH003:1.7.2.1
	netbsd-1-3-PATCH003-CANDIDATE2:1.7.2.1
	netbsd-1-3-PATCH003-CANDIDATE1:1.7.2.1
	netbsd-1-3-PATCH003-CANDIDATE0:1.7.2.1
	netbsd-1-3-PATCH002:1.7.2.1
	netbsd-1-3-PATCH001:1.7.2.1
	netbsd-1-3-RELEASE:1.7
	netbsd-1-3-BETA:1.7
	netbsd-1-3:1.7.0.2
	netbsd-1-3-base:1.7
	netbsd-1-2-PATCH001:1.6
	netbsd-1-2-RELEASE:1.6
	netbsd-1-2-BETA:1.6
	netbsd-1-2:1.6.0.6
	netbsd-1-2-base:1.6
	netbsd-1-1-PATCH001:1.6
	netbsd-1-1-RELEASE:1.6
	netbsd-1-1:1.6.0.2
	netbsd-1-1-base:1.6
	lite-2:1.1.1.2
	CSRG:1.1.1
	netbsd-1-0-PATCH06:1.5
	netbsd-1-0-PATCH05:1.5
	netbsd-1-0-PATCH04:1.5
	netbsd-1-0-PATCH03:1.5
	netbsd-1-0-PATCH02:1.5
	netbsd-1-0-PATCH1:1.5
	netbsd-1-0-PATCH0:1.5
	netbsd-1-0-RELEASE:1.5
	netbsd-1-0:1.5.0.2
	netbsd-1-0-base:1.5
	netbsd-0-9-RELEASE:1.2
	netbsd-0-9-BETA:1.2
	netbsd-0-9-ALPHA2:1.2
	netbsd-0-9-ALPHA:1.2
	netbsd-0-9:1.2.0.2
	netbsd-0-9-base:1.2
	netbsd-0-8:1.1.1.1
	netbsd-alpha-1:1.1.1.1
	patchkit-0-2-2:1.1.1.1
	WFJ-386bsd-01:1.1.1.1
	WFJ-920714:1.1.1;
locks; strict;
comment	@ * @;


1.25
date	2024.10.06.19.31.26;	author rillig;	state Exp;
branches;
next	1.24;
commitid	5ubE12hssTWKxDsF;

1.24
date	2016.01.16.17.01.22;	author christos;	state Exp;
branches
	1.24.28.1;
next	1.23;
commitid	Cx7CPejXy2JJ7bRy;

1.23
date	2012.03.20.20.34.58;	author matt;	state Exp;
branches;
next	1.22;

1.22
date	2011.09.06.18.16.01;	author joerg;	state Exp;
branches
	1.22.2.1;
next	1.21;

1.21
date	2011.03.05.16.37.50;	author christos;	state Exp;
branches;
next	1.20;

1.20
date	2009.11.06.15.13.27;	author joerg;	state Exp;
branches;
next	1.19;

1.19
date	2009.10.26.21.11.28;	author christos;	state Exp;
branches;
next	1.18;

1.18
date	2007.10.05.07.36.45;	author lukem;	state Exp;
branches;
next	1.17;

1.17
date	2004.06.20.22.20.16;	author jmc;	state Exp;
branches
	1.17.18.1;
next	1.16;

1.16
date	2003.08.07.11.14.33;	author agc;	state Exp;
branches
	1.16.2.1;
next	1.15;

1.15
date	2002.01.31.19.36.47;	author tv;	state Exp;
branches;
next	1.14;

1.14
date	2002.01.21.21.49.58;	author tv;	state Exp;
branches;
next	1.13;

1.13
date	2001.11.14.14.57.04;	author tv;	state Exp;
branches;
next	1.12;

1.12
date	2001.11.14.06.16.09;	author tv;	state Exp;
branches;
next	1.11;

1.11
date	2001.03.05.20.19.54;	author wiz;	state Exp;
branches;
next	1.10;

1.10
date	2000.05.08.00.52.18;	author tv;	state Exp;
branches
	1.10.4.1;
next	1.9;

1.9
date	97.12.29.19.52.57;	author cgd;	state Exp;
branches;
next	1.8;

1.8
date	97.12.02.22.34.05;	author cgd;	state Exp;
branches;
next	1.7;

1.7
date	97.10.19.04.40.03;	author lukem;	state Exp;
branches
	1.7.2.1;
next	1.6;

1.6
date	95.09.28.05.37.41;	author tls;	state Exp;
branches;
next	1.5;

1.5
date	94.06.19.03.58.41;	author glass;	state Exp;
branches;
next	1.4;

1.4
date	93.10.30.00.28.50;	author glass;	state Exp;
branches;
next	1.3;

1.3
date	93.08.02.17.54.45;	author mycroft;	state Exp;
branches;
next	1.2;

1.2
date	93.06.18.21.50.35;	author glass;	state Exp;
branches;
next	1.1;

1.1
date	93.03.21.09.45.37;	author cgd;	state Exp;
branches
	1.1.1.1;
next	;

1.24.28.1
date	2025.08.02.05.58.28;	author perseant;	state Exp;
branches;
next	;
commitid	23j6GFaDws3O875G;

1.22.2.1
date	2012.04.17.00.09.35;	author yamt;	state Exp;
branches;
next	;

1.17.18.1
date	2007.11.06.23.35.47;	author matt;	state Exp;
branches;
next	;

1.16.2.1
date	2004.06.22.07.21.29;	author tron;	state Exp;
branches;
next	;

1.10.4.1
date	2002.03.20.21.49.08;	author he;	state Exp;
branches;
next	;

1.7.2.1
date	98.01.29.10.53.36;	author mellon;	state Exp;
branches;
next	;

1.1.1.1
date	93.03.21.09.45.37;	author cgd;	state Exp;
branches;
next	1.1.1.2;

1.1.1.2
date	95.09.28.04.33.40;	author tls;	state Exp;
branches;
next	1.1.1.3;

1.1.1.3
date	2009.10.26.21.08.59;	author christos;	state Exp;
branches;
next	;


desc
@@


1.25
log
@usr.bin: fix lint warning about "effectively discards 'const'"

No binary change.
@
text
@/*	$OpenBSD: misc.c,v 1.41 2009/10/14 17:19:47 sthen Exp $	*/
/*	$NetBSD: misc.c,v 1.24 2016/01/16 17:01:22 christos Exp $	*/

/*
 * Copyright (c) 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Ozan Yigit at York University.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#include <sys/cdefs.h>
__RCSID("$NetBSD: misc.c,v 1.24 2016/01/16 17:01:22 christos Exp $");
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <err.h>
#include "mdef.h"
#include "stdd.h"
#include "extern.h"
#include "pathnames.h"


char *ep;		/* first free char in strspace */
static char *strspace;	/* string space for evaluation */
char *endest;		/* end of string space	       */
static size_t strsize = STRSPMAX;
static size_t bufsize = BUFSIZE;

unsigned char *buf;			/* push-back buffer	       */
unsigned char *bufbase;			/* the base for current ilevel */
unsigned char *bbase[MAXINP];		/* the base for each ilevel    */
unsigned char *bp; 			/* first available character   */
unsigned char *endpbb;			/* end of push-back buffer     */


/*
 * find the index of second str in the first str.
 */
ptrdiff_t
indx(const char *s1, const char *s2)
{
	const char *t;

	t = strstr(s1, s2);
	if (t == NULL)
		return (-1);
	else
		return (t - s1);
}
/*
 *  pushback - push character back onto input
 */
void
pushback(int c)
{
	if (c == EOF)
		return;
	if (bp >= endpbb)
		enlarge_bufspace();
	*bp++ = c;
}

/*
 *  pbstr - push string back onto input
 *          pushback is replicated to improve
 *          performance.
 */
void
pbstr(const char *s)
{
	size_t n;

	n = strlen(s);
	while ((size_t)(endpbb - bp) <= n)
		enlarge_bufspace();
	while (n > 0)
		*bp++ = s[--n];
}

/*
 *  pbnum - convert number to string, push back on input.
 */
void
pbnum(int n)
{
	pbnumbase(n, 10, 0);
}

void
pbnumbase(int n, int base, int d)
{
	static char digits[36] = "0123456789abcdefghijklmnopqrstuvwxyz";
	int num;
	int printed = 0;

	if (base > 36)
		m4errx(1, "base %d > 36: not supported.", base);

	if (base < 2)
		m4errx(1, "bad base %d for conversion.", base);

	num = (n < 0) ? -n : n;
	do {
		pushback(digits[num % base]);
		printed++;
	}
	while ((num /= base) > 0);

	if (n < 0)
		printed++;
	while (printed++ < d)
		pushback('0');

	if (n < 0)
		pushback('-');
}

/*
 *  pbunsigned - convert unsigned long to string, push back on input.
 */
void
pbunsigned(unsigned long n)
{
	do {
		pushback(n % 10 + '0');
	}
	while ((n /= 10) > 0);
}

void 
initspaces(void)
{
	int i;

	strspace = xalloc(strsize+1, NULL);
	ep = strspace;
	endest = strspace+strsize;
	buf = (unsigned char *)xalloc(bufsize, NULL);
	bufbase = buf;
	bp = buf;
	endpbb = buf + bufsize;
	for (i = 0; i < MAXINP; i++)
		bbase[i] = buf;
}

void 
enlarge_strspace(void)
{
	char *newstrspace;
	int i;

	strsize *= 2;
	newstrspace = malloc(strsize + 1);
	if (!newstrspace)
		errx(1, "string space overflow");
	memcpy(newstrspace, strspace, strsize/2);
	for (i = 0; i <= sp; i++) 
		if (sstack[i])
			mstack[i].sstr = (mstack[i].sstr - strspace) 
			    + newstrspace;
	ep = (ep-strspace) + newstrspace;
	free(strspace);
	strspace = newstrspace;
	endest = strspace + strsize;
}

void
enlarge_bufspace(void)
{
	unsigned char *newbuf;
	int i;

	bufsize += bufsize/2;
	newbuf = xrealloc(buf, bufsize, "too many characters pushed back");
	for (i = 0; i < MAXINP; i++)
		bbase[i] = (bbase[i]-buf)+newbuf;
	bp = (bp-buf)+newbuf;
	bufbase = (bufbase-buf)+newbuf;
	buf = newbuf;
	endpbb = buf+bufsize;
}

/*
 *  chrsave - put single char on string space
 */
void
chrsave(int c)
{
	if (ep >= endest) 
		enlarge_strspace();
	*ep++ = c;
}

/*
 * read in a diversion file, and dispose it.
 */
void
getdiv(int n)
{
	int c;

	if (active == outfile[n])
		m4errx(1, "undivert: diversion still active.");
	rewind(outfile[n]);
	while ((c = getc(outfile[n])) != EOF)
		putc(c, active);
	(void) fclose(outfile[n]);
	outfile[n] = NULL;
}

/*
 * killdiv - get rid of the diversion files
 */
void
killdiv(void)
{
	int n;

	for (n = 0; n < maxout; n++)
		if (outfile[n] != NULL) {
			(void) fclose(outfile[n]);
		}
}

void
m4errx(int exval, const char *fmt, ...)
{
	fprintf(stderr, "%s: ", getprogname());
	fprintf(stderr, "%s at line %lu: ", CURRENT_NAME, CURRENT_LINE);
	if (fmt != NULL) {
		va_list ap;

		va_start(ap, fmt);
		vfprintf(stderr, fmt, ap);
		va_end(ap);
	}
	fprintf(stderr, "\n");
	exit(exval);
}

/*
 * resizedivs: allocate more diversion files */
void
resizedivs(int n)
{
	int i;

	outfile = (FILE **)xrealloc(outfile, sizeof(FILE *) * n, 
	    "too many diverts %d", n);
	for (i = maxout; i < n; i++)
		outfile[i] = NULL;
	maxout = n;
}

void *
xalloc(size_t n, const char *fmt, ...)
{
	void *p = malloc(n);

	if (p == NULL) {
		if (fmt == NULL)
			err(1, "malloc");
		else {
			va_list va;
			
			va_start(va, fmt);
			verr(1, fmt, va);
			va_end(va);
		}
	}
	return p;
}

void *
xrealloc(void *old, size_t n, const char *fmt, ...)
{
	char *p = realloc(old, n);

	if (p == NULL) {
		free(old);
		if (fmt == NULL)
			err(1, "realloc");
		else {
			va_list va;

			va_start(va, fmt);
			verr(1, fmt, va);
			va_end(va);
	    	}
	}
	return p;
}

char *
xstrdup(const char *s)
{
	char *p = strdup(s);
	if (p == NULL)
		err(1, "strdup");
	return p;
}

int 
obtain_char(struct input_file *f)
{
	if (f->c == EOF)
		return EOF;

	f->c = fgetc(f->file);
#ifndef REAL_FREEZE
	if (freezef)
		fputc(f->c, freezef);
#endif
	if (f->c == '\n')
		f->lineno++;

	return f->c;
}

void 
set_input(struct input_file *f, FILE *real, const char *name)
{
	f->file = real;
	f->lineno = 1;
	f->c = 0;
	f->name = xstrdup(name);
	emit_synchline();
}

void
do_emit_synchline(void)
{
	fprintf(active, "#line %lu \"%s\"\n",
	    infile[ilevel].lineno, infile[ilevel].name);
	infile[ilevel].synch_lineno = infile[ilevel].lineno;
}

void 
release_input(struct input_file *f)
{
	if (f->file != stdin)
	    fclose(f->file);
	f->c = EOF;
	/*
	 * XXX can't free filename, as there might still be 
	 * error information pointing to it.
	 */
}

void
doprintlineno(struct input_file *f)
{
	pbunsigned(TOKEN_LINE(f));
}

void
doprintfilename(struct input_file *f)
{
	pbstr(rquote);
	pbstr(f->name);
	pbstr(lquote);
}

/* 
 * buffer_mark/dump_buffer: allows one to save a mark in a buffer,
 * and later dump everything that was added since then to a file.
 */
size_t
buffer_mark(void)
{
	return bp - buf;
}


void
dump_buffer(FILE *f, size_t m)
{
	unsigned char *s;

	for (s = bp; (size_t)(s - buf) > m;)
		fputc(*--s, f);
}
@


1.24
log
@record all input for fake freeze
@
text
@d2 1
a2 1
/*	$NetBSD: misc.c,v 1.23 2012/03/20 20:34:58 matt Exp $	*/
d39 1
a39 1
__RCSID("$NetBSD: misc.c,v 1.23 2012/03/20 20:34:58 matt Exp $");
d74 1
a74 1
	char *t;
@


1.24.28.1
log
@Sync with HEAD
@
text
@d2 1
a2 1
/*	$NetBSD: misc.c,v 1.25 2024/10/06 19:31:26 rillig Exp $	*/
d39 1
a39 1
__RCSID("$NetBSD: misc.c,v 1.25 2024/10/06 19:31:26 rillig Exp $");
d74 1
a74 1
	const char *t;
@


1.23
log
@Use C89 function definitions
@
text
@d2 1
a2 1
/*	$NetBSD: misc.c,v 1.22 2011/09/06 18:16:01 joerg Exp $	*/
d39 1
a39 1
__RCSID("$NetBSD: misc.c,v 1.22 2011/09/06 18:16:01 joerg Exp $");
d342 4
@


1.22
log
@Move usage and onintr to make them static. Add __dead as needed.
@
text
@d2 1
a2 1
/*	$NetBSD: misc.c,v 1.21 2011/03/05 16:37:50 christos Exp $	*/
d39 1
a39 1
__RCSID("$NetBSD: misc.c,v 1.21 2011/03/05 16:37:50 christos Exp $");
d163 1
a163 1
initspaces()
d179 1
a179 1
enlarge_strspace()
d200 1
a200 1
enlarge_bufspace()
d359 1
a359 1
do_emit_synchline()
d397 1
a397 1
buffer_mark()
@


1.22.2.1
log
@sync with head
@
text
@d2 1
a2 1
/*	$NetBSD$	*/
d39 1
a39 1
__RCSID("$NetBSD$");
d163 1
a163 1
initspaces(void)
d179 1
a179 1
enlarge_strspace(void)
d200 1
a200 1
enlarge_bufspace(void)
d359 1
a359 1
do_emit_synchline(void)
d397 1
a397 1
buffer_mark(void)
@


1.21
log
@use TOKEN_LINE from MirBSD
@
text
@d2 1
a2 1
/*	$NetBSD: misc.c,v 1.20 2009/11/06 15:13:27 joerg Exp $	*/
d39 1
a39 1
__RCSID("$NetBSD: misc.c,v 1.20 2009/11/06 15:13:27 joerg Exp $");
a242 8
void
onintr(int signo)
{
#define intrmessage	"m4: interrupted.\n"
	write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
	_exit(1);
}

d247 1
a247 1
killdiv()
a334 9
void
usage(void)
{
	fprintf(stderr, "usage: %s [-gPs] [-Dname[=value]] [-d flags] "
			"[-I dirname] [-o filename]\n"
			"\t[-t macro] [-Uname] [file ...]\n", getprogname());
	exit(1);
}

@


1.20
log
@Fix __progname mess.
@
text
@d2 1
a2 1
/*	$NetBSD: misc.c,v 1.19 2009/10/26 21:11:28 christos Exp $	*/
d39 1
a39 1
__RCSID("$NetBSD: misc.c,v 1.19 2009/10/26 21:11:28 christos Exp $");
d398 1
a398 1
	pbunsigned(f->lineno);
@


1.19
log
@resolve conflicts.
@
text
@d2 1
a2 1
/*	$NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $	*/
d39 1
a39 1
__RCSID("$NetBSD$");
a264 2
extern char *__progname;

d268 1
a268 1
	fprintf(stderr, "%s: ", __progname);
@


1.18
log
@Convert to using raise_default_signal(3).
@
text
@d1 2
a2 2
/*	$NetBSD: misc.c,v 1.17 2004/06/20 22:20:16 jmc Exp $	*/
/*	$OpenBSD: misc.c,v 1.25 2001/10/10 11:17:37 espie Exp $	*/
a34 1

a37 1

d39 1
a39 8
#if defined(__RCSID) && !defined(lint)
#if 0
static char sccsid[] = "@@(#)misc.c	8.1 (Berkeley) 6/6/93";
#else
__RCSID("$NetBSD: misc.c,v 1.17 2004/06/20 22:20:16 jmc Exp $");
#endif
#endif /* not lint */

d42 2
d48 1
a48 1
#include <util.h>
d54 1
d61 5
a65 5
char *buf;			/* push-back buffer	       */
char *bufbase;			/* the base for current ilevel */
char *bbase[MAXINP];		/* the base for each ilevel    */
char *bp; 			/* first available character   */
char *endpbb;			/* end of push-back buffer     */
d72 1
a72 3
indx(s1, s2)
	const char *s1;
	const char *s2;
d79 2
a80 1
	return (t - s1);
a81 1

d83 1
a83 1
 *  putback - push character back onto input
d86 1
a86 2
putback(c)
	int c;
d97 1
a97 1
 *          putback is replicated to improve
d101 1
a101 2
pbstr(s)
	const char *s;
d106 1
a106 1
	while (endpbb - bp <= n)
d116 7
a122 2
pbnum(n)
	int n;
d124 1
d126 7
d136 2
a137 1
		putback(num % 10 + '0');
d139 1
a139 1
	while ((num /= 10) > 0);
d142 6
a147 1
		putback('-');
d154 1
a154 2
pbunsigned(n)
	unsigned long n;
d157 1
a157 1
		putback(n % 10 + '0');
d167 1
a167 1
	strspace = xalloc(strsize+1);
d170 1
a170 1
	buf = (char *)xalloc(bufsize);
d202 1
a202 1
	char *newbuf;
d205 2
a206 4
	bufsize *= 2;
	newbuf = realloc(buf, bufsize);
	if (!newbuf)
		errx(1, "too many characters pushed back");
d219 1
a219 2
chrsave(c)
	int c;
d230 1
a230 2
getdiv(n)
	int n;
d235 1
a235 1
		errx(1, "undivert: diversion still active");
d244 1
a244 2
onintr(signo)
	int signo;
d247 1
a247 2
	write(STDERR_FILENO, intrmessage, sizeof(intrmessage));
	(void)raise_default_signal(signo);
d265 18
d286 1
a286 2
resizedivs(n)
	int n;
d290 2
a291 3
	outfile = (FILE **)realloc(outfile, sizeof(FILE *) * n);
	if (outfile == NULL)
		    errx(1, "too many diverts %d", n);
d298 20
a317 2
xalloc(n)
	size_t n;
d319 1
a319 1
	char *p = malloc(n);
d321 12
a332 2
	if (p == NULL)
		err(1, "malloc");
d337 1
a337 2
xstrdup(s)
	const char *s;
d346 1
a346 2
usage(progname)
	const char *progname;
d348 3
a350 2
	fprintf(stderr, "usage: %s [-Pg] [-Dname[=val]] [-I dirname] [-Uname]\n", progname);
	fprintf(stderr, "\t[-d flags] [-o trfile] [-t macro]\n");
d355 1
a355 2
obtain_char(f)
	struct input_file *f;
d359 3
a361 1
	else if (f->c == '\n')
a363 1
	f->c = fgetc(f->file);
d368 1
a368 4
set_input(f, real, name)
	struct input_file *f;
	FILE *real;
	const char *name;
d374 9
d386 1
a386 2
release_input(f)
	struct input_file *f;
d398 1
a398 2
doprintlineno(f)
	struct input_file *f;
d404 1
a404 2
doprintfilename(f)
	struct input_file *f;
d423 1
a423 3
dump_buffer(f, m)
	FILE *f;
	size_t m;
d425 1
a425 1
	char *s;
d427 1
a427 1
	for (s = bp; s-buf > m;)
@


1.17
log
@Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')

Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).

Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.

Fixes PR's: PR#17762 PR#25944
@
text
@d1 1
a1 1
/*	$NetBSD: misc.c,v 1.16 2003/08/07 11:14:33 agc Exp $	*/
d45 1
a45 1
__RCSID("$NetBSD: misc.c,v 1.16 2003/08/07 11:14:33 agc Exp $");
d55 1
d245 1
@


1.17.18.1
log
@sync with HEAD
@
text
@d1 1
a1 1
/*	$NetBSD: misc.c,v 1.18 2007/10/05 07:36:45 lukem Exp $	*/
d45 1
a45 1
__RCSID("$NetBSD: misc.c,v 1.18 2007/10/05 07:36:45 lukem Exp $");
a54 1
#include <util.h>
a243 1
	(void)raise_default_signal(signo);
@


1.16
log
@Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22365, verified by myself.
@
text
@d1 1
a1 1
/*	$NetBSD: misc.c,v 1.15 2002/01/31 19:36:47 tv Exp $	*/
d36 4
d45 1
a45 1
__RCSID("$NetBSD: misc.c,v 1.15 2002/01/31 19:36:47 tv Exp $");
@


1.16.2.1
log
@Pull up revision 1.17 (requested by jmc in ticket #527):
Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')
Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).
Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.
Fixes PR's: PR#17762 PR#25944
@
text
@d1 1
a1 1
/*	$NetBSD$	*/
a35 4
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif

d41 1
a41 1
__RCSID("$NetBSD$");
@


1.15
log
@Protect __RCSID and __COPYRIGHT from being invoked if not defined.
@
text
@d1 1
a1 1
/*	$NetBSD: misc.c,v 1.14 2002/01/21 21:49:58 tv Exp $	*/
d19 1
a19 5
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
d41 1
a41 1
__RCSID("$NetBSD: misc.c,v 1.14 2002/01/21 21:49:58 tv Exp $");
@


1.14
log
@Make compilable from src/tools/m4 on non-NetBSD hosts.
@
text
@d1 1
a1 1
/*	$NetBSD: misc.c,v 1.13 2001/11/14 14:57:04 tv Exp $	*/
d41 1
a41 1
#ifndef lint
d45 1
a45 1
__RCSID("$NetBSD: misc.c,v 1.13 2001/11/14 14:57:04 tv Exp $");
@


1.13
log
@Get first getopt string right; provide better usage message.
@
text
@d1 1
a1 1
/*	$NetBSD: misc.c,v 1.12 2001/11/14 06:16:09 tv Exp $	*/
d45 1
a45 1
__RCSID("$NetBSD: misc.c,v 1.12 2001/11/14 06:16:09 tv Exp $");
a49 1
#include <err.h>
a58 1

@


1.12
log
@Pull in various changes from OpenBSD, most from Marc Espie, including:
* Provide some GNUisms as extensions.
* Provide dynamically growable string space.
* Make define(defn(foo)) work correctly for builtins.
(The current version is supposed to be capable of satisfying autoconf.)

All still relevant NetBSD changes have been preserved in this version, and
formatting and style fixes have been applied in various places.

Thanks to Masao Uebayashi <uebayasi@@soum.co.jp> for pointing this out.
@
text
@d1 1
a1 1
/*	$NetBSD: misc.c,v 1.11 2001/03/05 20:19:54 wiz Exp $	*/
d45 1
a45 1
__RCSID("$NetBSD: misc.c,v 1.11 2001/03/05 20:19:54 wiz Exp $");
d301 2
a302 1
usage()
d304 2
a305 1
	fprintf(stderr, "usage: m4 [-P] [-Dname[=val]] [-Uname] [-I dirname...]\n");
@


1.11
log
@Fix appearance of bogus 0xff at EOF reported by me in bin/12287.
@
text
@d1 2
a2 1
/*	$NetBSD: misc.c,v 1.10 2000/05/08 00:52:18 tv Exp $	*/
d45 1
a45 1
__RCSID("$NetBSD: misc.c,v 1.10 2000/05/08 00:52:18 tv Exp $");
a51 1
#include <unistd.h>
d54 1
d61 14
d78 1
a78 1
int
d80 2
a81 2
	char *s1;
	char *s2;
d98 5
a102 16
	if (bp < endpbb)
		*bp++ = c;
	else
		errx(1, "too many characters pushed back");
}

/*
 *  putbackeof - push EOF back onto input
 */
void
putbackeof()
{
	if (bp < endpbb)
		*bp++ = EOF;
	else
		errx(1, "too many characters pushed back");
d112 1
a112 1
	char *s;
d114 1
a114 2
	char *es;
	pbent *zp;
d116 5
a120 11
	es = s;
	zp = bp;

	while (*es)
		es++;
	es--;
	while (es >= s)
		if (zp < endpbb)
			*zp++ = (unsigned char)*es--;
	if ((bp = zp) == endpbb)
		errx(1, "too many characters pushed back");
d143 68
d215 1
a215 1
	char c;
d217 3
a219 4
	if (ep < endest)
		*ep++ = c;
	else
		errx(1, "string space overflow");
a229 1
	FILE *dfil;
d233 3
a237 14
	m4temp[UNIQUE] = n + '0';
	if ((dfil = fopen(m4temp, "r")) == NULL)
		err(1, "%s: cannot undivert", m4temp);
	else
		while ((c = getc(dfil)) != EOF)
			putc(c, active);
	(void) fclose(dfil);

#ifdef vms
	if (remove(m4temp))
#else
	if (unlink(m4temp) == -1)
#endif
		err(1, "%s: cannot unlink", m4temp);
d244 3
a246 1
	errx(1, "interrupted");
d257 1
a257 1
	for (n = 0; n < MAXOUT; n++)
a259 6
			m4temp[UNIQUE] = n + '0';
#ifdef vms
			(void) remove(m4temp);
#else
			(void) unlink(m4temp);
#endif
d263 17
a279 1
char *
d281 1
a281 1
	unsigned long n;
d300 48
a347 3
char *
basename(s)
	char *s;
d349 2
a350 1
	char *p;
d352 8
a359 2
	if ((p = strrchr(s, '/')) == NULL)
		return s;
d361 8
a368 1
	return ++p;
d371 1
d373 3
a375 1
usage()
d377 4
a380 2
	fprintf(stderr, "usage: m4 [-P] [-Dname[=val]] [-Uname]\n");
	exit(1);
@


1.10
log
@Add -P to the usage message (forgot this, long ago).
@
text
@d1 1
a1 1
/*	$NetBSD: misc.c,v 1.9 1997/12/29 19:52:57 cgd Exp $	*/
d44 1
a44 1
__RCSID("$NetBSD: misc.c,v 1.9 1997/12/29 19:52:57 cgd Exp $");
d81 1
a81 1
	unsigned char c;
@


1.10.4.1
log
@Pull up revision 1.11 (requested by jmc):
  Fix a conversion bug internal to m4 which causes it to output
  0xff as the last byte of output.
@
text
@d1 1
a1 1
/*	$NetBSD: misc.c,v 1.10 2000/05/08 00:52:18 tv Exp $	*/
d44 1
a44 1
__RCSID("$NetBSD: misc.c,v 1.10 2000/05/08 00:52:18 tv Exp $");
d81 1
a81 1
	int c;
@


1.9
log
@fix an inconsistency between signed- and unsigned-char machines:
make sure that pushed-back 0xff character isn't accidentally
interpreted as an EOF because of sign extension when chars were being
assigned to pbents.  (signed-char machines lost.)  To do this, make
putback() and pbstr() operate only on unsigned chars, and add a
putbackeof() function to do the obvious thing when necessary.
@
text
@d1 1
a1 1
/*	$NetBSD: misc.c,v 1.8 1997/12/02 22:34:05 cgd Exp $	*/
d44 1
a44 1
__RCSID("$NetBSD: misc.c,v 1.8 1997/12/02 22:34:05 cgd Exp $");
d251 1
a251 1
	fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n");
@


1.8
log
@add a typedef which describes elements in the push-back buffer.  Make
that typedef 'short'.  'char' (which was previously used) because char
may be unsigned and ((char)EOF) != EOF if that is the case.  That was
causing the (char)EOF (0xff) pushed back in main to be interepreted as
a character, and, in some cases, to be written to the output.  'short'
was used rather than 'signed char' because if the latter is used,
0xff characters in the input would confuse m4.  (No point in introducing
(more?) 8-bit lossage.)
@
text
@d1 1
a1 1
/*	$NetBSD: misc.c,v 1.7 1997/10/19 04:40:03 lukem Exp $	*/
d44 1
a44 1
__RCSID("$NetBSD: misc.c,v 1.7 1997/10/19 04:40:03 lukem Exp $");
d81 1
a81 1
	pbent c;
d90 12
d121 1
a121 1
			*zp++ = *es--;
@


1.7
log
@WARNSify, fix .Nm usage, getopt returns -1 not EOF, deprecate register
@
text
@d1 1
a1 1
/*	$NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $	*/
d44 1
a44 1
__RCSID("$NetBSD$");
d81 1
a81 1
	char c;
d99 1
a99 1
	char *zp;
@


1.7.2.1
log
@Pull up 1.8 and 1.9 (cgd)
@
text
@d1 1
a1 1
/*	$NetBSD: misc.c,v 1.9 1997/12/29 19:52:57 cgd Exp $	*/
d44 1
a44 1
__RCSID("$NetBSD: misc.c,v 1.9 1997/12/29 19:52:57 cgd Exp $");
d81 1
a81 1
	unsigned char c;
a89 12
 *  putbackeof - push EOF back onto input
 */
void
putbackeof()
{
	if (bp < endpbb)
		*bp++ = EOF;
	else
		errx(1, "too many characters pushed back");
}

/*
d99 1
a99 1
	pbent *zp;
d109 1
a109 1
			*zp++ = (unsigned char)*es--;
@


1.6
log
@Sync with 4.4BSD-Lite2
@
text
@d1 1
a1 1
/*	$NetBSD: $	*/
d39 1
d44 1
a44 1
static char rcsid[] = "$NetBSD: $";
d49 1
d65 2
a66 2
char *s1;
char *s2;
d68 6
a73 10
	register char *t;
	register char *p;
	register char *m;

	for (p = s1; *p; p++) {
		for (t = p, m = s2; *m && *m == *t; m++, t++);
		if (!*m)
			return (p - s1);
	}
	return (-1);
d75 1
d81 1
a81 1
char c;
d86 1
a86 1
		oops("too many characters pushed back");
d96 1
a96 1
register char *s;
d98 2
a99 2
	register char *es;
	register char *zp;
d111 1
a111 1
		oops("too many characters pushed back");
d119 1
a119 1
int n;
d121 1
a121 1
	register int num;
d138 1
a138 1
char c;
d143 1
a143 1
		oops("string space overflow");
d151 1
a151 1
int n;
d153 2
a154 2
	register int c;
	register FILE *dfil;
d157 1
a157 1
		oops("%s: diversion still active.", "undivert");
d162 1
a162 1
		oops("%s: cannot undivert.", m4temp);
d173 1
a173 1
		oops("%s: cannot unlink.", m4temp);
d180 1
a180 1
	oops("interrupted.");
d189 1
a189 1
	register int n;
d205 1
a205 1
unsigned long n;
d207 1
a207 1
	register char *p = malloc(n);
d210 1
a210 1
		oops("malloc: %s", strerror(errno));
d216 1
a216 1
const char *s;
d218 1
a218 1
	register char *p = strdup(s);
d220 1
a220 1
		oops("strdup: %s", strerror(errno));
d226 1
a226 1
register char *s;
d228 1
a228 2
	register char *p;
	extern char *strrchr();
a240 29
}

#if __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif

void
#if __STDC__
oops(const char *fmt, ...)
#else
oops(fmt, va_alist)
	char *fmt;
	va_dcl
#endif
{
	va_list ap;
#if __STDC__
	va_start(ap, fmt);
#else
	va_start(ap);
#endif
	(void)fprintf(stderr, "%s: ", progname);
	(void)vfprintf(stderr, fmt, ap);
	va_end(ap);
	(void)fprintf(stderr, "\n");
	exit(1);
	/* NOTREACHED */
@


1.5
log
@upgrade to 4.4-lite m4.  no local changes of note
@
text
@d1 2
d40 1
d42 3
@


1.4
log
@latest m4 from ozan.  now does the right thing w/respect to sendmail
@
text
@d1 46
a46 6
/*  File   : misc.c
    Author : Ozan Yigit
    Updated: 26-Mar-1993
    Purpose: Miscellaneous support code for PD M4.
*/

d48 12
a59 55
#include "extr.h"
#include "ourlims.h"

#ifdef	DUFFCP

/*  This version of the ANSI standard function memcpy()
    uses Duff's Device (tm Tom Duff)  to unroll the copying loop:
	while (count-- > 0) *to++ = *from++;
*/
void memcpy(to, from, count)
    register char *from, *to;
    register int count;
    {
	if (count > 0) {
	    register int loops = (count+8-1) >> 3;	/* div 8 round up */

	    switch (count & (8-1)) {	/* mod 8 */
	        case 0: do {	*to++ = *from++;
		case 7:		*to++ = *from++;
		case 6:		*to++ = *from++;
		case 5:		*to++ = *from++;
		case 4:		*to++ = *from++;
		case 3:		*to++ = *from++;
		case 2:		*to++ = *from++;
		case 1:		*to++ = *from++;
			} while (--loops > 0);
	    }
	}
    }

#endif


/*  strsave(s)
    return a new malloc()ed copy of s -- same as V.3's strdup().
*/
char *strsave(s)
    char *s;
    {
	register int n = strlen(s)+1;
	char *p = malloc(n);

	if (p) memcpy(p, s, n);
	return p;
    }


/*  indx(s1, s2)
    if s1 can be decomposed as alpha || s2 || omega, return the length
    of the shortest such alpha, otherwise return -1.
*/
int indx(s1, s2)
    char *s1;
    char *s2;
    {
d61 1
a62 1
	register char *p;
d65 3
a67 2
	    for (t = p, m = s2; *m && *m == *t; m++, t++);
	    if (!*m) return p-s1;
d69 24
a92 31
	return -1;
    }


char pbmsg[] = "m4: too many characters pushed back";

/*  Xputback(c)
    push character c back onto the input stream.
    This is now macro putback() in misc.h
*/
void Xputback(c)
    char c;
    {
	if (bp < endpbb) *bp++ = c; else error(pbmsg);
    }


/*  pbstr(s)
    push string s back onto the input stream.
    putback() has been unfolded here to improve performance.
    Example:
	s = <ABC>
	bp = <more stuff>
    After the call:
	bp = <more stuffCBA>
    It would be more efficient if we ran the pushback buffer in the
    opposite direction
*/
void pbstr(s)
    register char *s;
    {
d96 1
a97 17
	for (es = s; *es; ) es++;	/* now es points to terminating NUL */
	bp += es-s;			/* advance bp as far as it should go */
	if (bp >= endpbb) error("m4: too many characters to push back");
	while (es > s) *zp++ = *--es;
    }


/*  pbqtd(s)
    pushes string s back "quoted", doing whatever has to be done to it to
    make sure that the result will evaluate to the original value.  As it
    happens, we have only to add lquote and rquote.
*/
void pbqtd(s)
    register char *s;
    {
	register char *es;
	register char *zp;
d99 17
a115 17
	zp = bp;
	for (es = s; *es; ) es++;	/* now es points to terminating NUL */
	bp += 2+es-s;			/* advance bp as far as it should go */
	if (bp >= endpbb) error("m4: too many characters to push back");
	*zp++ = rquote;
	while (es > s) *zp++ = *--es;
	*zp++ = lquote;
    }


/*  pbnum(n)
    convert a number to a (decimal) string and push it back.
    The original definition did not work for MININT; this does.
*/
void pbnum(n)
    int n;
    {
d118 1
a118 1
	num = n > 0 ? -n : n;	/* MININT <= num <= 0 */
d120 1
a120 45
	    putback('0' - (num % 10));
	} while ((num /= 10) < 0);
	if (n < 0) putback('-');
    }


/*  pbrad(n, r, m)
    converts a number n to base r ([-36..-2] U [2..36]), with at least
    m digits.  If r == 10 and m == 1, this is exactly the same as pbnum.
    However, this uses the function int2str() from R.A.O'Keefe's public
    domain string library, and puts the results of that back.
    The Unix System V Release 3 version of m4 accepts radix 1;
    THIS VERSION OF M4 DOES NOT ACCEPT RADIX 1 OR -1,
    nor do we accept radix < -36 or radix > 36.  At the moment such bad
    radices quietly produce nothing.  The V.3 treatment of radix 1 is
	push back abs(n) "1"s, then
	if n < 0, push back one "-".
    Until I come across something which uses it, I can't bring myself to
    implement this.

    I have, however, found a use for radix 0.  Unsurprisingly, it is
    related to radix 0 in Edinburgh Prolog.
	eval('c1c2...cn', 0, m)
    pushes back max(m-n,0) blanks and the characters c1...cn.  This can
    adjust to any byte size as long as UCHAR_MAX = (1 << CHAR_BIT) - 1.
    In particular, eval(c, 0) where 0 < c <= UCHAR_MAX, pushes back the
    character with code c.  Note that this has to agree with eval(); so
    both of them have to use the same byte ordering.
*/
void pbrad(n, r, m)
    long int n;
    int r, m;
    {
	char buffer[34];
	char *p;
	int L;

	if (r == 0) {
	    unsigned long int x = (unsigned long)n;
	    int n;

	    for (n = 0; x; x >>= CHAR_BIT, n++) buffer[n] = x & UCHAR_MAX;
	    for (L = n; --L >= 0; ) putback(buffer[L]);
	    for (L = m-n; --L >= 0; ) putback(' ');
	    return;
d122 1
a122 13
	L = m - (int2str(p = buffer, -r, n)-buffer);
	if (buffer[0] == '-') L++, p++;
	if (L > 0) {
	    pbstr(p);
	    while (--L >= 0) putback('0');
	    if (p != buffer) putback('-');
	} else {
	    pbstr(buffer);
	}
    }


char csmsg[] = "m4: string space overflow";
d124 24
a147 20
/*  chrsave(c)
    put the character c in the string space.
*/
void Xchrsave(c)
    char c;
    {
#if 0
	if (sp < 0) putc(c, active); else
#endif
	if (ep < endest) *ep++ = c; else
	error(csmsg);
    }


/*  getdiv(ind)
    read in a diversion file and then delete it.
*/
void getdiv(ind)
    int ind;
    {
a149 1
	register FILE *afil;
d151 5
a155 6
	afil = active;
	if (outfile[ind] == afil)
	    error("m4: undivert: diversion still active.");
	(void) fclose(outfile[ind]);
	outfile[ind] = NULL;
	m4temp[UNIQUE] = '0' + ind;
d157 4
a160 2
	    error("m4: cannot undivert.");
	while ((c = getc(dfil)) != EOF) putc(c, afil);
d163 2
a164 2
#if vms
	if (remove(m4temp)) error("m4: cannot unlink.");
d166 1
a166 1
	if (unlink(m4temp) == -1) error("m4: cannot unlink.");
d168 2
a169 1
    }
d171 13
a183 6

/*  killdiv()
    delete all the diversion files which have been created.
*/
void killdiv()
    {
d186 6
a191 6
	for (n = 0; n < MAXOUT; n++) {
	    if (outfile[n] != NULL) {
		(void) fclose(outfile[n]);
		m4temp[UNIQUE] = '0' + n;
#if unix
		(void) unlink(m4temp);
d193 1
a193 1
		(void) remove(m4temp);
d195 2
a196 3
	    }
	}
    }
d198 5
d204 4
a207 10
/*  error(s)
    close all files, report a fatal error, and quit, letting the caller know.
*/
void error(s)
    char *s;
    {
	killdiv();
	fprintf(stderr, "%s\n", s);
	exit(1);
    }
d209 9
d219 6
a224 3
/*  Interrupt handling
*/
static char *msg = "\ninterrupted.";
d226 2
a227 8
#ifdef	__STDC__
void onintr(int signo)
#else
onintr()
#endif
    {
	error(msg);
    }
d229 2
d232 4
a235 3
void usage()
    {
	fprintf(stderr, "Usage: m4 [-e] [-[BHST]int] [-Dname[=val]] [-Uname]\n");
d237 1
a237 1
    }
d239 5
a243 2
#ifdef GETOPT
/* Henry Spencer's getopt() - get option letter from argv */
d245 7
a251 8
char *optarg;			/* Global argument pointer. */
int optind = 0;			/* Global argv index. */

static char *scan = NULL;	/* Private scan pointer. */

#ifndef	__STDC__
extern	char *index();
#define strchr index
d253 6
a258 43

int getopt(argc, argv, optstring)
    int argc;
    char **argv;
    char *optstring;
    {
	register char c;
	register char *place;

	optarg = NULL;

	if (scan == NULL || *scan == '\0') {
	    if (optind == 0) optind++;
	    if (optind >= argc
	     || argv[optind][0] != '-'
	     || argv[optind][1] == '\0')
		return EOF;
	    if (strcmp(argv[optind], "--") == 0) {
		optind++;
		return EOF;
	    }
	    scan = argv[optind]+1;
	    optind++;
	}
	c = *scan++;
	place = strchr(optstring, c);

	if (place == NULL || c == ':') {
	    fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
	    return '?';
	}
	place++;
	if (*place == ':') {
	    if (*scan != '\0') {
		optarg = scan;
		scan = NULL;
	    } else {
		optarg = argv[optind];
		optind++;
	    }
	}
	return c;
    }
d260 7
a266 1

@


1.3
log
@Add RCS identifiers, remove some completely useless RCS logs and patchkit
headers, and a few other insignificant changes.
@
text
@a6 4
#ifndef lint
static char rcsid[] = "$Id: $";
#endif /* not lint */

@


1.2
log
@baseline of new version of m4 supplied by Ozan Yigit, original author
of the broken m4 we had.  This is his stuff virgin + our Makefile.
@
text
@d7 4
@


1.1
log
@Initial revision
@
text
@d1 6
a6 50
/*
 * Copyright (c) 1989 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Ozan Yigit.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static char sccsid[] = "@@(#)misc.c	5.6 (Berkeley) 2/26/91";
#endif /* not lint */

/*
 * misc.c
 * Facility: m4 macro processor
 * by: oz
 */
 
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
d8 114
a121 47
#include "extr.h" 
#include "pathnames.h"
 
/*
 * indx - find the index of second str in the
 *        first str.
 */
indx(s1, s2)
char *s1;
char *s2;
{
        register char *t;
        register char *p;
        register char *m;
 
        for (p = s1; *p; p++) {
                for (t = p, m = s2; *m && *m == *t; m++, t++)
                        ;
                if (!*m)
                        return(p - s1);
        }
        return (-1);
}
 
/*
 *  putback - push character back onto input
 *
 */
putback (c)
char c;
{
        if (bp < endpbb)
                *bp++ = c;
        else
                error("m4: too many characters pushed back");
}
 
/*
 *  pbstr - push string back onto input
 *          putback is replicated to improve
 *          performance.
 *
 */
pbstr(s)
register char *s;
{
        register char *es;
a123 1
	es = s;
d125 56
d182 101
a282 82
        while (*es)
                es++;
        es--;
        while (es >= s)
                if (zp < endpbb)
                        *zp++ = *es--;
        if ((bp = zp) == endpbb)
                error("m4: too many characters pushed back");
}
 
/*
 *  pbnum - convert number to string, push back on input.
 *
 */
pbnum (n)
int n;
{
        register int num;
 
        num = (n < 0) ? -n : n;
        do {
                putback(num % 10 + '0');
        }
        while ((num /= 10) > 0);

        if (n < 0) putback('-');
}
 
/*
 *  chrsave - put single char on string space
 *
 */
chrsave (c)
char c;
{
/***        if (sp < 0)
                putc(c, active);
        else ***/ if (ep < endest)
                *ep++ = c;
        else
                error("m4: string space overflow");
}
 
/*
 * getdiv - read in a diversion file, and
 *          trash it.
 */
getdiv(ind) {
        register int c;
        register FILE *dfil;
 
        if (active == outfile[ind])
                error("m4: undivert: diversion still active.");
        (void) fclose(outfile[ind]);
        outfile[ind] = NULL;
        m4temp[UNIQUE] = ind + '0';
        if ((dfil = fopen(m4temp, "r")) == NULL)
                error("m4: cannot undivert.");
        else
                while((c = getc(dfil)) != EOF)
                        putc(c, active);
        (void) fclose(dfil);

	if (unlink(m4temp) == -1)
                error("m4: cannot unlink.");
}
 
/*
 * Very fatal error. Close all files
 * and die hard.
 */
error(s)
char *s;
{
        killdiv();
        fprintf(stderr,"%s\n",s);
        exit(1);
}
 
/*
 * Interrupt handling
 */
d284 74
a357 25
 
void
onintr() {
        error(msg);
}
 
/*
 * killdiv - get rid of the diversion files
 *
 */
killdiv() {
        register int n;
 
        for (n = 0; n < MAXOUT; n++)
                if (outfile[n] != NULL) {
                        (void) fclose (outfile[n]);
                        m4temp[UNIQUE] = n + '0';
                        (void) unlink (m4temp);
                }
}
 
usage() {
        fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n");
        exit(1);
}
@


1.1.1.1
log
@initial import of 386bsd-0.1 sources
@
text
@@


1.1.1.2
log
@imported from 44lite2
@
text
@d2 2
a3 2
 * Copyright (c) 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
d6 1
a6 1
 * Ozan Yigit at York University.
d38 1
a38 1
static char sccsid[] = "@@(#)misc.c	8.1 (Berkeley) 6/6/93";
d41 6
a46 2
#include <sys/types.h>
#include <errno.h>
d52 1
a52 2
#include "stdd.h"
#include "extern.h"
d54 1
a54 1

d56 2
a57 1
 * find the index of second str in the first str.
a58 1
int
d63 11
a73 10
	register char *t;
	register char *p;
	register char *m;

	for (p = s1; *p; p++) {
		for (t = p, m = s2; *m && *m == *t; m++, t++);
		if (!*m)
			return (p - s1);
	}
	return (-1);
d75 1
d78 1
d80 1
a80 2
void
putback(c)
d83 4
a86 4
	if (bp < endpbb)
		*bp++ = c;
	else
		oops("too many characters pushed back");
d88 1
a88 1

d93 1
a94 1
void
d98 1
a98 1
	register char *es;
d104 8
a111 8
	while (*es)
		es++;
	es--;
	while (es >= s)
		if (zp < endpbb)
			*zp++ = *es--;
	if ((bp = zp) == endpbb)
		oops("too many characters pushed back");
d113 1
a113 1

d116 1
d118 1
a118 2
void
pbnum(n)
d121 7
a127 7
	register int num;

	num = (n < 0) ? -n : n;
	do {
		putback(num % 10 + '0');
	}
	while ((num /= 10) > 0);
d129 1
a129 2
	if (n < 0)
		putback('-');
d131 1
a131 1

d134 1
d136 1
a136 2
void
chrsave(c)
d139 30
a168 4
	if (ep < endest)
		*ep++ = c;
	else
		oops("string space overflow");
d170 1
a170 1

d172 2
a173 1
 * read in a diversion file, and dispose it.
d175 2
a176 3
void
getdiv(n)
int n;
d178 3
a180 21
	register int c;
	register FILE *dfil;

	if (active == outfile[n])
		oops("%s: diversion still active.", "undivert");
	(void) fclose(outfile[n]);
	outfile[n] = NULL;
	m4temp[UNIQUE] = n + '0';
	if ((dfil = fopen(m4temp, "r")) == NULL)
		oops("%s: cannot undivert.", m4temp);
	else
		while ((c = getc(dfil)) != EOF)
			putc(c, active);
	(void) fclose(dfil);

#ifdef vms
	if (remove(m4temp))
#else
	if (unlink(m4temp) == -1)
#endif
		oops("%s: cannot unlink.", m4temp);
d182 6
a187 1

d189 2
a190 4
onintr(signo)
	int signo;
{
	oops("interrupted.");
d192 1
a192 1

d195 1
d197 14
a210 85
void
killdiv()
{
	register int n;

	for (n = 0; n < MAXOUT; n++)
		if (outfile[n] != NULL) {
			(void) fclose(outfile[n]);
			m4temp[UNIQUE] = n + '0';
#ifdef vms
			(void) remove(m4temp);
#else
			(void) unlink(m4temp);
#endif
		}
}

char *
xalloc(n)
unsigned long n;
{
	register char *p = malloc(n);

	if (p == NULL)
		oops("malloc: %s", strerror(errno));
	return p;
}

char *
xstrdup(s)
const char *s;
{
	register char *p = strdup(s);
	if (p == NULL)
		oops("strdup: %s", strerror(errno));
	return p;
}

char *
basename(s)
register char *s;
{
	register char *p;
	extern char *strrchr();

	if ((p = strrchr(s, '/')) == NULL)
		return s;

	return ++p;
}

void
usage()
{
	fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n");
	exit(1);
}

#if __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif

void
#if __STDC__
oops(const char *fmt, ...)
#else
oops(fmt, va_alist)
	char *fmt;
	va_dcl
#endif
{
	va_list ap;
#if __STDC__
	va_start(ap, fmt);
#else
	va_start(ap);
#endif
	(void)fprintf(stderr, "%s: ", progname);
	(void)vfprintf(stderr, fmt, ap);
	va_end(ap);
	(void)fprintf(stderr, "\n");
	exit(1);
	/* NOTREACHED */
@


1.1.1.3
log
@Import new m4 from OpenBSD.
@
text
@a0 3
/*	$OpenBSD: misc.c,v 1.41 2009/10/14 17:19:47 sthen Exp $	*/
/*	$NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $	*/

d16 5
a20 1
 * 3. Neither the name of the University nor the names of its contributors
d37 4
a43 1
#include <stdarg.h>
a45 1
#include <stddef.h>
a46 1
#include <err.h>
a51 14

char *ep;		/* first free char in strspace */
static char *strspace;	/* string space for evaluation */
char *endest;		/* end of string space	       */
static size_t strsize = STRSPMAX;
static size_t bufsize = BUFSIZE;

unsigned char *buf;			/* push-back buffer	       */
unsigned char *bufbase;			/* the base for current ilevel */
unsigned char *bbase[MAXINP];		/* the base for each ilevel    */
unsigned char *bp; 			/* first available character   */
unsigned char *endpbb;			/* end of push-back buffer     */


d55 15
a69 10
ptrdiff_t
indx(const char *s1, const char *s2)
{
	char *t;

	t = strstr(s1, s2);
	if (t == NULL)
		return (-1);
	else
		return (t - s1);
d72 1
a72 1
 *  pushback - push character back onto input
d75 2
a76 1
pushback(int c)
d78 4
a81 5
	if (c == EOF)
		return;
	if (bp >= endpbb)
		enlarge_bufspace();
	*bp++ = c;
d86 1
a86 1
 *          pushback is replicated to improve
d90 2
a91 1
pbstr(const char *s)
d93 2
a94 1
	size_t n;
d96 11
a106 5
	n = strlen(s);
	while (endpbb - bp <= n)
		enlarge_bufspace();
	while (n > 0)
		*bp++ = s[--n];
d113 2
a114 7
pbnum(int n)
{
	pbnumbase(n, 10, 0);
}

void
pbnumbase(int n, int base, int d)
d116 1
a116 9
	static char digits[36] = "0123456789abcdefghijklmnopqrstuvwxyz";
	int num;
	int printed = 0;

	if (base > 36)
		m4errx(1, "base %d > 36: not supported.", base);

	if (base < 2)
		m4errx(1, "bad base %d for conversion.", base);
d120 1
a120 2
		pushback(digits[num % base]);
		printed++;
d122 1
a122 6
	while ((num /= base) > 0);

	if (n < 0)
		printed++;
	while (printed++ < d)
		pushback('0');
d125 1
a125 66
		pushback('-');
}

/*
 *  pbunsigned - convert unsigned long to string, push back on input.
 */
void
pbunsigned(unsigned long n)
{
	do {
		pushback(n % 10 + '0');
	}
	while ((n /= 10) > 0);
}

void 
initspaces()
{
	int i;

	strspace = xalloc(strsize+1, NULL);
	ep = strspace;
	endest = strspace+strsize;
	buf = (unsigned char *)xalloc(bufsize, NULL);
	bufbase = buf;
	bp = buf;
	endpbb = buf + bufsize;
	for (i = 0; i < MAXINP; i++)
		bbase[i] = buf;
}

void 
enlarge_strspace()
{
	char *newstrspace;
	int i;

	strsize *= 2;
	newstrspace = malloc(strsize + 1);
	if (!newstrspace)
		errx(1, "string space overflow");
	memcpy(newstrspace, strspace, strsize/2);
	for (i = 0; i <= sp; i++) 
		if (sstack[i])
			mstack[i].sstr = (mstack[i].sstr - strspace) 
			    + newstrspace;
	ep = (ep-strspace) + newstrspace;
	free(strspace);
	strspace = newstrspace;
	endest = strspace + strsize;
}

void
enlarge_bufspace()
{
	unsigned char *newbuf;
	int i;

	bufsize += bufsize/2;
	newbuf = xrealloc(buf, bufsize, "too many characters pushed back");
	for (i = 0; i < MAXINP; i++)
		bbase[i] = (bbase[i]-buf)+newbuf;
	bp = (bp-buf)+newbuf;
	bufbase = (bufbase-buf)+newbuf;
	buf = newbuf;
	endpbb = buf+bufsize;
d132 2
a133 1
chrsave(int c)
d135 4
a138 3
	if (ep >= endest) 
		enlarge_strspace();
	*ep++ = c;
d145 2
a146 1
getdiv(int n)
d148 2
a149 1
	int c;
d152 1
a152 4
		m4errx(1, "undivert: diversion still active.");
	rewind(outfile[n]);
	while ((c = getc(outfile[n])) != EOF)
		putc(c, active);
d155 14
d172 2
a173 1
onintr(int signo)
d175 1
a175 3
#define intrmessage	"m4: interrupted.\n"
	write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
	_exit(1);
d184 1
a184 1
	int n;
d186 1
a186 1
	for (n = 0; n < maxout; n++)
d189 6
d198 3
a200 22
extern char *__progname;

void
m4errx(int eval, const char *fmt, ...)
{
	fprintf(stderr, "%s: ", __progname);
	fprintf(stderr, "%s at line %lu: ", CURRENT_NAME, CURRENT_LINE);
	if (fmt != NULL) {
		va_list ap;

		va_start(ap, fmt);
		vfprintf(stderr, fmt, ap);
		va_end(ap);
	}
	fprintf(stderr, "\n");
	exit(eval);
}

/*
 * resizedivs: allocate more diversion files */
void
resizedivs(int n)
d202 1
a202 1
	int i;
d204 2
a205 43
	outfile = (FILE **)xrealloc(outfile, sizeof(FILE *) * n, 
	    "too many diverts %d", n);
	for (i = maxout; i < n; i++)
		outfile[i] = NULL;
	maxout = n;
}

void *
xalloc(size_t n, const char *fmt, ...)
{
	void *p = malloc(n);

	if (p == NULL) {
		if (fmt == NULL)
			err(1, "malloc");
		else {
			va_list va;
			
			va_start(va, fmt);
			verr(1, fmt, va);
			va_end(va);
		}
	}
	return p;
}

void *
xrealloc(void *old, size_t n, const char *fmt, ...)
{
	char *p = realloc(old, n);

	if (p == NULL) {
		free(old);
		if (fmt == NULL)
			err(1, "realloc");
		else {
			va_list va;

			va_start(va, fmt);
			verr(1, fmt, va);
			va_end(va);
	    	}
	}
d210 2
a211 1
xstrdup(const char *s)
d213 1
a213 1
	char *p = strdup(s);
d215 1
a215 1
		err(1, "strdup");
d219 3
a221 24
void
usage()
{
	fprintf(stderr, "usage: m4 [-gPs] [-Dname[=value]] [-d flags] "
			"[-I dirname] [-o filename]\n"
			"\t[-t macro] [-Uname] [file ...]\n");
	exit(1);
}

int 
obtain_char(struct input_file *f)
{
	if (f->c == EOF)
		return EOF;

	f->c = fgetc(f->file);
	if (f->c == '\n')
		f->lineno++;

	return f->c;
}

void 
set_input(struct input_file *f, FILE *real, const char *name)
d223 2
a224 6
	f->file = real;
	f->lineno = 1;
	f->c = 0;
	f->name = xstrdup(name);
	emit_synchline();
}
d226 2
a227 19
void
do_emit_synchline()
{
	fprintf(active, "#line %lu \"%s\"\n",
	    infile[ilevel].lineno, infile[ilevel].name);
	infile[ilevel].synch_lineno = infile[ilevel].lineno;
}

void 
release_input(struct input_file *f)
{
	if (f->file != stdin)
	    fclose(f->file);
	f->c = EOF;
	/*
	 * XXX can't free filename, as there might still be 
	 * error information pointing to it.
	 */
}
d229 1
a229 4
void
doprintlineno(struct input_file *f)
{
	pbunsigned(f->lineno);
d233 1
a233 1
doprintfilename(struct input_file *f)
d235 2
a236 3
	pbstr(rquote);
	pbstr(f->name);
	pbstr(lquote);
d239 27
a265 18
/* 
 * buffer_mark/dump_buffer: allows one to save a mark in a buffer,
 * and later dump everything that was added since then to a file.
 */
size_t
buffer_mark()
{
	return bp - buf;
}


void
dump_buffer(FILE *f, size_t m)
{
	unsigned char *s;

	for (s = bp; s-buf > m;)
		fputc(*--s, f);
@

