1 /* $NetBSD: emit.c,v 1.24 2023/08/12 20:48:24 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)
40 __RCSID("$NetBSD: emit.c,v 1.24 2023/08/12 20:48:24 rillig Exp $");
41 #endif
42
43 #include <stdio.h>
44 #include <string.h>
45
46 #include "lint.h"
47
48 static const char *output_name;
49 static FILE *output_file;
50
51 void
outopen(const char * name)52 outopen(const char *name)
53 {
54
55 output_name = name;
56 if ((output_file = fopen(name, "w")) == NULL)
57 err(1, "cannot open '%s'", name);
58 }
59
60 void
outclose(void)61 outclose(void)
62 {
63
64 if (fclose(output_file) == EOF)
65 err(1, "cannot close '%s'", output_name);
66 }
67
68 void
outchar(char c)69 outchar(char c)
70 {
71
72 fputc(c, output_file);
73 }
74
75 /*
76 * write a string to the output file
77 * the string must not contain any characters which should be quoted
78 */
79 void
outstrg(const char * s)80 outstrg(const char *s)
81 {
82
83 while (*s != '\0')
84 outchar(*s++);
85 }
86
87 /* write an integer value to the output file */
88 void
outint(int i)89 outint(int i)
90 {
91 char buf[1 + 3 * sizeof(int)];
92
93 snprintf(buf, sizeof(buf), "%d", i);
94 outstrg(buf);
95 }
96
97 /* write a name to the output file, preceded by its length */
98 void
outname(const char * name)99 outname(const char *name)
100 {
101
102 outint((int)strlen(name));
103 outstrg(name);
104 }
105
106 /* write the name of the .c source */
107 void
outsrc(const char * name)108 outsrc(const char *name)
109 {
110
111 outchar('S');
112 outstrg(name);
113 outchar('\n');
114 }
115