Add support for "yosys -E"
[yosys.git] / passes / cmds / write_file.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2014 Clifford Wolf <clifford@clifford.at>
5 * Copyright (C) 2014 Johann Glaser <Johann.Glaser@gmx.at>
6 *
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.
10 *
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.
18 *
19 */
20
21 #include "kernel/yosys.h"
22
23 USING_YOSYS_NAMESPACE
24 PRIVATE_NAMESPACE_BEGIN
25
26 struct WriteFileFrontend : public Frontend {
27 WriteFileFrontend() : Frontend("=write_file", "write a text to a file") { }
28 virtual void help()
29 {
30 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
31 log("\n");
32 log(" write_file [options] output_file [input_file]\n");
33 log("\n");
34 log("Write the text from the input file to the output file.\n");
35 log("\n");
36 log(" -a\n");
37 log(" Append to output file (instead of overwriting)\n");
38 log("\n");
39 log("\n");
40 log("Inside a script the input file can also can a here-document:\n");
41 log("\n");
42 log(" write_file hello.txt <<EOT\n");
43 log(" Hello World!\n");
44 log(" EOT\n");
45 log("\n");
46 }
47 virtual void execute(std::istream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design*)
48 {
49 bool append_mode = false;
50 std::string output_filename;
51
52 size_t argidx;
53 for (argidx = 1; argidx < args.size(); argidx++)
54 {
55 if (args[argidx] == "-a") {
56 append_mode = true;
57 continue;
58 }
59 break;
60 }
61
62 if (argidx < args.size() && args[argidx].rfind("-", 0) != 0)
63 output_filename = args[argidx++];
64 else
65 log_cmd_error("Missing putput filename.\n");
66
67 extra_args(f, filename, args, argidx);
68
69 FILE *of = fopen(output_filename.c_str(), append_mode ? "a" : "w");
70 yosys_output_files.insert(output_filename);
71 char buffer[64 * 1024];
72 int bytes;
73
74 while (0 < (bytes = readsome(*f, buffer, sizeof(buffer))))
75 fwrite(buffer, bytes, 1, of);
76
77 fclose(of);
78 }
79 } WriteFileFrontend;
80
81 PRIVATE_NAMESPACE_END