1 /* $NetBSD: emit.c,v 1.2 1995/07/03 21:24:00 cgd Exp $ */
4 * Copyright (c) 1994, 1995 Jochen Pohl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
37 static char rcsid
[] = "$NetBSD: emit.c,v 1.2 1995/07/03 21:24:00 cgd Exp $";
47 /* name and handle of output file */
48 static const char *loname
;
51 /* output buffer data */
54 static void outxbuf(void);
66 /* Ausgabedatei oeffnen */
67 if ((lout
= fopen(name
, "w")) == NULL
)
68 err(1, "cannot open '%s'", name
);
70 /* Ausgabepuffer anlegen */
72 ob
.o_end
= (ob
.o_buf
= ob
.o_nxt
= xmalloc(ob
.o_len
)) + ob
.o_len
;
76 * flush output buffer and close file
82 if (fclose(lout
) == EOF
)
83 err(1, "cannot close '%s'", loname
);
87 * resize output buffer
94 coffs
= ob
.o_nxt
- ob
.o_buf
;
96 ob
.o_end
= (ob
.o_buf
= xrealloc(ob
.o_buf
, ob
.o_len
)) + ob
.o_len
;
97 ob
.o_nxt
= ob
.o_buf
+ coffs
;
101 * reset output buffer
102 * if it is not empty, it is flushed
109 if (ob
.o_buf
!= ob
.o_nxt
) {
111 sz
= ob
.o_nxt
- ob
.o_buf
;
113 errx(1, "internal error: outclr() 1");
114 if (fwrite(ob
.o_buf
, sz
, 1, lout
) != 1)
115 err(1, "cannot write to %s", loname
);
121 * write a character to the output buffer
127 if (ob
.o_nxt
== ob
.o_end
)
129 *ob
.o_nxt
++ = (char)c
;
133 * write a character to the output buffer, qouted if necessary
139 if (isprint(c
) && c
!= '\\' && c
!= '"' && c
!= '\'') {
183 outchar((((u_int
)c
>> 6) & 07) + '0');
184 outchar((((u_int
)c
>> 3) & 07) + '0');
185 outchar((c
& 07) + '0');
192 * write a strint to the output buffer
193 * the string must not contain any characters which
201 if (ob
.o_nxt
== ob
.o_end
)
208 * write an integer value to toe output buffer
214 if ((ob
.o_end
- ob
.o_nxt
) < 3 * sizeof (int))
216 ob
.o_nxt
+= sprintf(ob
.o_nxt
, "%d", i
);
220 * write the name of a symbol to the output buffer
221 * the name is preceded by its length
228 errx(1, "internal error: outname() 1");
229 outint((int)strlen(name
));
234 * write the name of the .c source