emit.c revision 1.23 1 /* $NetBSD: emit.c,v 1.23 2023/08/12 17:13:27 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.23 2023/08/12 17:13:27 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 static bool in_line;
51
52 void
53 outopen(const char *name)
54 {
55
56 output_name = name;
57 if ((output_file = fopen(name, "w")) == NULL)
58 err(1, "cannot open '%s'", name);
59 }
60
61 void
62 outclose(void)
63 {
64
65 outclr();
66 if (fclose(output_file) == EOF)
67 err(1, "cannot close '%s'", output_name);
68 }
69
70 void
71 outclr(void)
72 {
73
74 if (in_line)
75 outchar('\n');
76 }
77
78 void
79 outchar(char c)
80 {
81
82 fputc(c, output_file);
83 in_line = c != '\n';
84 }
85
86 /*
87 * write a string to the output file
88 * the string must not contain any characters which should be quoted
89 */
90 void
91 outstrg(const char *s)
92 {
93
94 while (*s != '\0')
95 outchar(*s++);
96 }
97
98 /* write an integer value to the output file */
99 void
100 outint(int i)
101 {
102 char buf[1 + 3 * sizeof(int)];
103
104 snprintf(buf, sizeof(buf), "%d", i);
105 outstrg(buf);
106 }
107
108 /* write a name to the output file, preceded by its length */
109 void
110 outname(const char *name)
111 {
112
113 outint((int)strlen(name));
114 outstrg(name);
115 }
116
117 /* write the name of the .c source */
118 void
119 outsrc(const char *name)
120 {
121
122 outclr();
123 outchar('S');
124 outstrg(name);
125 }
126