Home | History | Annotate | Line # | Download | only in common
emit.c revision 1.15
      1 /*	$NetBSD: emit.c,v 1.15 2021/09/04 14:42:30 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995 Jochen Pohl
      5  * All Rights Reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Jochen Pohl for
     18  *	The NetBSD Project.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #if HAVE_NBTOOL_CONFIG_H
     35 #include "nbtool_config.h"
     36 #endif
     37 
     38 #include <sys/cdefs.h>
     39 #if defined(__RCSID) && !defined(lint)
     40 __RCSID("$NetBSD: emit.c,v 1.15 2021/09/04 14:42:30 rillig Exp $");
     41 #endif
     42 
     43 #include <stdio.h>
     44 #include <string.h>
     45 
     46 #include "lint.h"
     47 
     48 /* name and handle of output file */
     49 static	const	char *loname;
     50 static	FILE	*lout;
     51 
     52 /* output buffer data */
     53 static	ob_t	ob;
     54 
     55 static	void	outxbuf(void);
     56 
     57 
     58 /*
     59  * initialize output
     60  */
     61 void
     62 outopen(const char *name)
     63 {
     64 
     65 	loname = name;
     66 
     67 	/* Open output file */
     68 	if ((lout = fopen(name, "w")) == NULL)
     69 		err(1, "cannot open '%s'", name);
     70 
     71 	/* Create output buffer */
     72 	ob.o_len = 1024;
     73 	ob.o_end = (ob.o_buf = ob.o_next = xmalloc(ob.o_len)) + ob.o_len;
     74 }
     75 
     76 /*
     77  * flush output buffer and close file
     78  */
     79 void
     80 outclose(void)
     81 {
     82 
     83 	outclr();
     84 	if (fclose(lout) == EOF)
     85 		err(1, "cannot close '%s'", loname);
     86 }
     87 
     88 /*
     89  * resize output buffer
     90  */
     91 static void
     92 outxbuf(void)
     93 {
     94 	ptrdiff_t coffs;
     95 
     96 	coffs = ob.o_next - ob.o_buf;
     97 	ob.o_len *= 2;
     98 	ob.o_end = (ob.o_buf = xrealloc(ob.o_buf, ob.o_len)) + ob.o_len;
     99 	ob.o_next = ob.o_buf + coffs;
    100 }
    101 
    102 /*
    103  * reset output buffer
    104  * if it is not empty, it is flushed
    105  */
    106 void
    107 outclr(void)
    108 {
    109 	size_t	sz;
    110 
    111 	if (ob.o_buf != ob.o_next) {
    112 		outchar('\n');
    113 		sz = ob.o_next - ob.o_buf;
    114 		if (sz > ob.o_len)
    115 			errx(1, "internal error: outclr() 1");
    116 		if (fwrite(ob.o_buf, sz, 1, lout) != 1)
    117 			err(1, "cannot write to %s", loname);
    118 		ob.o_next = ob.o_buf;
    119 	}
    120 }
    121 
    122 /*
    123  * write a character to the output buffer
    124  */
    125 void
    126 outchar(char c)
    127 {
    128 
    129 	if (ob.o_next == ob.o_end)
    130 		outxbuf();
    131 	*ob.o_next++ = c;
    132 }
    133 
    134 #if defined(IS_LINT1)
    135 /* write a character to the output buffer, quoted if necessary */
    136 void
    137 outqchar(char c)
    138 {
    139 
    140 	if (ch_isprint(c) && c != '\\' && c != '"' && c != '\'') {
    141 		outchar(c);
    142 	} else {
    143 		outchar('\\');
    144 		switch (c) {
    145 		case '\\':
    146 			outchar('\\');
    147 			break;
    148 		case '"':
    149 			outchar('"');
    150 			break;
    151 		case '\'':
    152 			outchar('\'');
    153 			break;
    154 		case '\b':
    155 			outchar('b');
    156 			break;
    157 		case '\t':
    158 			outchar('t');
    159 			break;
    160 		case '\n':
    161 			outchar('n');
    162 			break;
    163 		case '\f':
    164 			outchar('f');
    165 			break;
    166 		case '\r':
    167 			outchar('r');
    168 			break;
    169 		case '\v':
    170 			outchar('v');
    171 			break;
    172 		case '\a':
    173 			outchar('a');
    174 			break;
    175 		default:
    176 			outchar((char)((((unsigned char)c >> 6) & 07) + '0'));
    177 			outchar((char)((((unsigned char)c >> 3) & 07) + '0'));
    178 			outchar((char)((c & 07) + '0'));
    179 			break;
    180 		}
    181 	}
    182 }
    183 #endif
    184 
    185 /*
    186  * write a string to the output buffer
    187  * the string must not contain any characters which
    188  * should be quoted
    189  */
    190 void
    191 outstrg(const char *s)
    192 {
    193 
    194 	while (*s != '\0') {
    195 		if (ob.o_next == ob.o_end)
    196 			outxbuf();
    197 		*ob.o_next++ = *s++;
    198 	}
    199 }
    200 
    201 /* write an integer value to the output buffer */
    202 void
    203 outint(int i)
    204 {
    205 
    206 	if ((size_t)(ob.o_end - ob.o_next) < 3 * sizeof(int))
    207 		outxbuf();
    208 	ob.o_next += sprintf(ob.o_next, "%d", i);
    209 }
    210 
    211 /* write a name to the output buffer, preceded by its length */
    212 void
    213 outname(const char *name)
    214 {
    215 	outint((int)strlen(name));
    216 	outstrg(name);
    217 }
    218 
    219 /* write the name of the .c source */
    220 void
    221 outsrc(const char *name)
    222 {
    223 
    224 	outclr();
    225 	outchar('S');
    226 	outstrg(name);
    227 }
    228