xref: /netbsd-src/usr.bin/menuc/util.c (revision 48d8b2ae499feef2e768f20a23fd3036499e7879)
1*48d8b2aeSchristos /*	$NetBSD: util.c,v 1.6 2019/06/23 22:46:53 christos Exp $	*/
295f3bf39Sphil 
395f3bf39Sphil /*
495f3bf39Sphil  * Copyright 1997 Piermont Information Systems Inc.
595f3bf39Sphil  * All rights reserved.
695f3bf39Sphil  *
795f3bf39Sphil  * Written by Philip A. Nelson for Piermont Information Systems Inc.
895f3bf39Sphil  *
995f3bf39Sphil  * Redistribution and use in source and binary forms, with or without
1095f3bf39Sphil  * modification, are permitted provided that the following conditions
1195f3bf39Sphil  * are met:
1295f3bf39Sphil  * 1. Redistributions of source code must retain the above copyright
1395f3bf39Sphil  *    notice, this list of conditions and the following disclaimer.
1495f3bf39Sphil  * 2. Redistributions in binary form must reproduce the above copyright
1595f3bf39Sphil  *    notice, this list of conditions and the following disclaimer in the
1695f3bf39Sphil  *    documentation and/or other materials provided with the distribution.
175cd43bc6Smbalmer  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
1895f3bf39Sphil  *    or promote products derived from this software without specific prior
1995f3bf39Sphil  *    written permission.
2095f3bf39Sphil  *
2195f3bf39Sphil  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
2295f3bf39Sphil  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2395f3bf39Sphil  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2495f3bf39Sphil  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
2595f3bf39Sphil  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2695f3bf39Sphil  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2795f3bf39Sphil  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2895f3bf39Sphil  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2995f3bf39Sphil  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3095f3bf39Sphil  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3195f3bf39Sphil  * THE POSSIBILITY OF SUCH DAMAGE.
3295f3bf39Sphil  *
3395f3bf39Sphil  */
3495f3bf39Sphil 
3595f3bf39Sphil /* util.c - utility routines. */
3695f3bf39Sphil 
37b2f78261Sjmc #if HAVE_NBTOOL_CONFIG_H
38b2f78261Sjmc #include "nbtool_config.h"
39b2f78261Sjmc #endif
40b2f78261Sjmc 
41a8d6388eSagc #include <sys/cdefs.h>
42a8d6388eSagc 
43abcf838dSlukem #if defined(__RCSID) && !defined(lint)
44*48d8b2aeSchristos __RCSID("$NetBSD: util.c,v 1.6 2019/06/23 22:46:53 christos Exp $");
45a8d6388eSagc #endif
46a8d6388eSagc 
4795f3bf39Sphil #include <stdio.h>
4895f3bf39Sphil #include <stdlib.h>
4995f3bf39Sphil #include <string.h>
5095f3bf39Sphil #include <stdarg.h>
5195f3bf39Sphil #include "defs.h"
5295f3bf39Sphil 
5395f3bf39Sphil /* Error routine */
5495f3bf39Sphil void
yyerror(const char * fmt,...)5595f3bf39Sphil yyerror(const char *fmt, ...)
5695f3bf39Sphil {
5795f3bf39Sphil 	va_list args;
5895f3bf39Sphil 
5995f3bf39Sphil 	va_start(args, fmt);
6095f3bf39Sphil 	printf("%s:%d: ", src_name, line_no);
6195f3bf39Sphil 	vfprintf(stdout, fmt, args);
6295f3bf39Sphil 	printf("\n");
6395f3bf39Sphil 	va_end(args);
6495f3bf39Sphil 	had_errors = TRUE;
6595f3bf39Sphil }
6695f3bf39Sphil 
6795f3bf39Sphil /* Buffer routines */
6895f3bf39Sphil static char *mc_buff = NULL;
6995f3bf39Sphil static int mc_size = 0;
7095f3bf39Sphil static int mc_loc = 0;
7195f3bf39Sphil 
725cd43bc6Smbalmer void
buff_add_ch(char ch)735cd43bc6Smbalmer buff_add_ch(char ch)
7495f3bf39Sphil {
7595f3bf39Sphil 	char *t;
7695f3bf39Sphil 
7795f3bf39Sphil 	if (mc_loc >= mc_size-1) {
7895f3bf39Sphil 		if (mc_size == 0)
7995f3bf39Sphil 			mc_size = 80;
8095f3bf39Sphil 		else
8195f3bf39Sphil 			mc_size *= 2;
82*48d8b2aeSchristos 		t = malloc(mc_size);
8395f3bf39Sphil 		if (t == NULL) {
8495f3bf39Sphil 			(void)fprintf(stderr, "%s:%d: Malloc error\n",
8595f3bf39Sphil 					 src_name, line_no);
8695f3bf39Sphil 			exit(1);
8795f3bf39Sphil 		}
8895f3bf39Sphil 		if (mc_buff != NULL) {
8995f3bf39Sphil 			strcpy(t, mc_buff);
9095f3bf39Sphil 			free(mc_buff);
9195f3bf39Sphil 		}
9295f3bf39Sphil 		mc_buff = t;
9395f3bf39Sphil 	}
9495f3bf39Sphil 	mc_buff[mc_loc++] = ch;
9595f3bf39Sphil 	mc_buff[mc_loc] = '\0';
9695f3bf39Sphil }
9795f3bf39Sphil 
9895f3bf39Sphil /* get a copy of the string ! */
9995f3bf39Sphil char *
buff_copy(void)10095f3bf39Sphil buff_copy(void)
10195f3bf39Sphil {
10295f3bf39Sphil 	char *res = strdup(mc_buff);
1035cd43bc6Smbalmer 
10495f3bf39Sphil 	mc_loc = 0;
10595f3bf39Sphil 	mc_buff[0] = '\0';
10695f3bf39Sphil 	return res;
10795f3bf39Sphil }
108