1*525a267eSThomas Cort /* $NetBSD: util.c,v 1.5 2012/03/06 16:55:18 mbalmer Exp $ */
2*525a267eSThomas Cort
3*525a267eSThomas Cort /*
4*525a267eSThomas Cort * Copyright 1997 Piermont Information Systems Inc.
5*525a267eSThomas Cort * All rights reserved.
6*525a267eSThomas Cort *
7*525a267eSThomas Cort * Written by Philip A. Nelson for Piermont Information Systems Inc.
8*525a267eSThomas Cort *
9*525a267eSThomas Cort * Redistribution and use in source and binary forms, with or without
10*525a267eSThomas Cort * modification, are permitted provided that the following conditions
11*525a267eSThomas Cort * are met:
12*525a267eSThomas Cort * 1. Redistributions of source code must retain the above copyright
13*525a267eSThomas Cort * notice, this list of conditions and the following disclaimer.
14*525a267eSThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
15*525a267eSThomas Cort * notice, this list of conditions and the following disclaimer in the
16*525a267eSThomas Cort * documentation and/or other materials provided with the distribution.
17*525a267eSThomas Cort * 3. The name of Piermont Information Systems Inc. may not be used to endorse
18*525a267eSThomas Cort * or promote products derived from this software without specific prior
19*525a267eSThomas Cort * written permission.
20*525a267eSThomas Cort *
21*525a267eSThomas Cort * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
22*525a267eSThomas Cort * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*525a267eSThomas Cort * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*525a267eSThomas Cort * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
25*525a267eSThomas Cort * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26*525a267eSThomas Cort * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27*525a267eSThomas Cort * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*525a267eSThomas Cort * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29*525a267eSThomas Cort * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30*525a267eSThomas Cort * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31*525a267eSThomas Cort * THE POSSIBILITY OF SUCH DAMAGE.
32*525a267eSThomas Cort *
33*525a267eSThomas Cort */
34*525a267eSThomas Cort
35*525a267eSThomas Cort /* util.c - utility routines. */
36*525a267eSThomas Cort
37*525a267eSThomas Cort #if HAVE_NBTOOL_CONFIG_H
38*525a267eSThomas Cort #include "nbtool_config.h"
39*525a267eSThomas Cort #endif
40*525a267eSThomas Cort
41*525a267eSThomas Cort #include <sys/cdefs.h>
42*525a267eSThomas Cort
43*525a267eSThomas Cort #if defined(__RCSID) && !defined(lint)
44*525a267eSThomas Cort __RCSID("$NetBSD: util.c,v 1.5 2012/03/06 16:55:18 mbalmer Exp $");
45*525a267eSThomas Cort #endif
46*525a267eSThomas Cort
47*525a267eSThomas Cort #include <stdio.h>
48*525a267eSThomas Cort #include <stdlib.h>
49*525a267eSThomas Cort #include <string.h>
50*525a267eSThomas Cort #include <stdarg.h>
51*525a267eSThomas Cort #include "defs.h"
52*525a267eSThomas Cort
53*525a267eSThomas Cort /* Error routine */
54*525a267eSThomas Cort void
yyerror(const char * fmt,...)55*525a267eSThomas Cort yyerror(const char *fmt, ...)
56*525a267eSThomas Cort {
57*525a267eSThomas Cort va_list args;
58*525a267eSThomas Cort
59*525a267eSThomas Cort va_start(args, fmt);
60*525a267eSThomas Cort printf("%s:%d: ", src_name, line_no);
61*525a267eSThomas Cort vfprintf(stdout, fmt, args);
62*525a267eSThomas Cort printf("\n");
63*525a267eSThomas Cort va_end(args);
64*525a267eSThomas Cort had_errors = TRUE;
65*525a267eSThomas Cort }
66*525a267eSThomas Cort
67*525a267eSThomas Cort /* Buffer routines */
68*525a267eSThomas Cort static char *mc_buff = NULL;
69*525a267eSThomas Cort static int mc_size = 0;
70*525a267eSThomas Cort static int mc_loc = 0;
71*525a267eSThomas Cort
72*525a267eSThomas Cort void
buff_add_ch(char ch)73*525a267eSThomas Cort buff_add_ch(char ch)
74*525a267eSThomas Cort {
75*525a267eSThomas Cort char *t;
76*525a267eSThomas Cort
77*525a267eSThomas Cort if (mc_loc >= mc_size-1) {
78*525a267eSThomas Cort if (mc_size == 0)
79*525a267eSThomas Cort mc_size = 80;
80*525a267eSThomas Cort else
81*525a267eSThomas Cort mc_size *= 2;
82*525a267eSThomas Cort t = (char *)malloc(mc_size);
83*525a267eSThomas Cort if (t == NULL) {
84*525a267eSThomas Cort (void)fprintf(stderr, "%s:%d: Malloc error\n",
85*525a267eSThomas Cort src_name, line_no);
86*525a267eSThomas Cort exit(1);
87*525a267eSThomas Cort }
88*525a267eSThomas Cort if (mc_buff != NULL) {
89*525a267eSThomas Cort strcpy(t, mc_buff);
90*525a267eSThomas Cort free(mc_buff);
91*525a267eSThomas Cort }
92*525a267eSThomas Cort mc_buff = t;
93*525a267eSThomas Cort }
94*525a267eSThomas Cort mc_buff[mc_loc++] = ch;
95*525a267eSThomas Cort mc_buff[mc_loc] = '\0';
96*525a267eSThomas Cort }
97*525a267eSThomas Cort
98*525a267eSThomas Cort /* get a copy of the string ! */
99*525a267eSThomas Cort char *
buff_copy(void)100*525a267eSThomas Cort buff_copy(void)
101*525a267eSThomas Cort {
102*525a267eSThomas Cort char *res = strdup(mc_buff);
103*525a267eSThomas Cort
104*525a267eSThomas Cort mc_loc = 0;
105*525a267eSThomas Cort mc_buff[0] = '\0';
106*525a267eSThomas Cort return res;
107*525a267eSThomas Cort }
108