1/*****************************************************************************/
2/* */
3/* output.c */
4/* */
5/* Output file handling */
6/* */
7/* */
8/* */
9/* (C) 2009-2012, Ullrich von Bassewitz */
10/* Roemerstrasse 52 */
11/* D-70794 Filderstadt */
12/* EMail: uz@cc65.org */
13/* */
14/* */
15/* This software is provided 'as-is', without any expressed or implied */
16/* warranty. In no event will the authors be held liable for any damages */
17/* arising from the use of this software. */
18/* */
19/* Permission is granted to anyone to use this software for any purpose, */
20/* including commercial applications, and to alter it and redistribute it */
21/* freely, subject to the following restrictions: */
22/* */
23/* 1. The origin of this software must not be misrepresented; you must not */
24/* claim that you wrote the original software. If you use this software */
25/* in a product, an acknowledgment in the product documentation would be */
26/* appreciated but is not required. */
27/* 2. Altered source versions must be plainly marked as such, and must not */
28/* be misrepresented as being the original software. */
29/* 3. This notice may not be removed or altered from any source */
30/* distribution. */
31/* */
32/*****************************************************************************/
33
34
35
36#include <stdio.h>
37#include <stdarg.h>
38#include <string.h>
39#include <errno.h>
40
41/* common */
42#include "check.h"
43#include "fname.h"
44#include "print.h"
45#include "xmalloc.h"
46
47/* cc65 */
48#include "error.h"
49#include "global.h"
50#include "output.h"
51
52
53
54/*****************************************************************************/
55/* Data */
56/*****************************************************************************/
57
58
59
60/* Name of the output file. Dynamically allocated and read only. */
61const char* OutputFilename = 0;
62
63/* Output file handle */
64FILE* OutputFile = 0;
65
66
67
68/*****************************************************************************/
69/* Code */
70/*****************************************************************************/
71
72
73
74void SetOutputName (const char* Name)
75/* Sets the name of the output file. */
76{
77 OutputFilename = Name;
78}
79
80
81
82void MakeDefaultOutputName (const char* InputFilename)
83/* If the name of the output file is empty or NULL, the name of the output
84** file is derived from the input file by adjusting the file name extension.
85*/
86{
87 if (OutputFilename == 0 || *OutputFilename == '\0') {
88 /* We don't have an output file for now */
89 const char* Ext = PreprocessOnly? ".i" : ".s";
90 OutputFilename = MakeFilename (InputFilename, Ext);
91 }
92}
93
94
95
96void OpenOutputFile ()
97/* Open the output file. Will call Fatal() in case of failures. */
98{
99 /* Output file must not be open and we must have a name*/
100 PRECONDITION (OutputFile == 0 && OutputFilename != 0);
101
102 /* Open the file */
103 OutputFile = fopen (OutputFilename, "w");
104 if (OutputFile == 0) {
105 Fatal ("Cannot open output file '%s': %s", OutputFilename, strerror (errno));
106 }
107 Print (stdout, 1, "Opened output file '%s'\n", OutputFilename);
108}
109
110
111
112void OpenDebugOutputFile (const char* Name)
113/* Open an output file for debugging purposes. Will call Fatal() in case of
114** failures.
115*/
116{
117 /* Output file must not be open and we must have a name*/
118 PRECONDITION (OutputFile == 0);
119
120 /* Open the file */
121 OutputFile = fopen (Name, "w");
122 if (OutputFile == 0) {
123 Fatal ("Cannot open debug output file '%s': %s", Name, strerror (errno));
124 }
125 Print (stdout, 1, "Opened debug output file '%s'\n", Name);
126}
127
128
129
130void CloseOutputFile ()
131/* Close the output file. Will call Fatal() in case of failures. */
132{
133 /* Output file must be open */
134 PRECONDITION (OutputFile != 0);
135
136 /* Close the file, check for errors */
137 if (fclose (OutputFile) != 0) {
138 remove (OutputFilename);
139 Fatal ("Cannot write to output file (disk full?)");
140 }
141 Print (stdout, 1, "Closed output file '%s'\n", OutputFilename);
142
143 OutputFile = 0;
144}
145
146
147
148int WriteOutput (const char* Format, ...)
149/* Write to the output file using printf like formatting. Returns the number
150** of chars written.
151*/
152{
153 va_list ap;
154 int CharCount;
155
156 /* Must have an output file */
157 PRECONDITION (OutputFile != 0);
158
159 /* Output formatted */
160 va_start (ap, Format);
161 CharCount = vfprintf (OutputFile, Format, ap);
162 va_end (ap);
163
164 /* Return the number of chars written */
165 return CharCount;
166}
167