2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2014 Clifford Wolf <clifford@clifford.at>
5 * Copyright (C) 2014 Johann Glaser <Johann.Glaser@gmx.at>
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include "kernel/yosys.h"
24 PRIVATE_NAMESPACE_BEGIN
26 struct WriteFileFrontend
: public Frontend
{
27 WriteFileFrontend() : Frontend("=write_file", "write a text to a file") { }
28 void help() YS_OVERRIDE
30 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
32 log(" write_file [options] output_file [input_file]\n");
34 log("Write the text from the input file to the output file.\n");
37 log(" Append to output file (instead of overwriting)\n");
40 log("Inside a script the input file can also can a here-document:\n");
42 log(" write_file hello.txt <<EOT\n");
43 log(" Hello World!\n");
47 void execute(std::istream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*) YS_OVERRIDE
49 bool append_mode
= false;
50 std::string output_filename
;
53 for (argidx
= 1; argidx
< args
.size(); argidx
++)
55 if (args
[argidx
] == "-a") {
62 if (argidx
< args
.size() && args
[argidx
].rfind("-", 0) != 0)
63 output_filename
= args
[argidx
++];
65 log_cmd_error("Missing output filename.\n");
67 extra_args(f
, filename
, args
, argidx
);
69 FILE *of
= fopen(output_filename
.c_str(), append_mode
? "a" : "w");
70 yosys_output_files
.insert(output_filename
);
71 char buffer
[64 * 1024];
74 while (0 < (bytes
= readsome(*f
, buffer
, sizeof(buffer
))))
75 fwrite(buffer
, bytes
, 1, of
);