1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <iostream>
8 #include "pio_assembler.h"
9 
10 #define DEFAULT_OUTPUT_FORMAT "c-sdk"
11 
usage()12 void usage() {
13     std::cerr << "usage: pioasm <options> <input> (<output>)\n\n";
14     std::cerr << "Assemble file of PIO program(s) for use in applications.\n";
15     std::cerr << "   <input>             the input filename\n";
16     std::cerr << "   <output>            the output filename (or filename prefix if the output format produces multiple outputs).\n";
17     std::cerr << "                       if not specified, a single output will be written to stdout\n";
18     std::cerr << "\n";
19     std::cerr << "options:\n";
20     std::cerr << "  -o <output_format>   select output_format (default '" << DEFAULT_OUTPUT_FORMAT << "'); available options are:\n";
21     for(const auto& f : output_format::all()) {
22         std::cerr << "                           " << f->name << std::endl;
23         std::cerr << "                               " << f->get_description() << std::endl;
24     }
25     std::cerr << "  -p <output_param>    add a parameter to be passed to the output format generator" << std::endl;
26     std::cerr << "  -?, --help           print this help and exit\n";
27 }
28 
main(int argc,char * argv[])29 int main(int argc, char *argv[]) {
30     int res = 0;
31     pio_assembler pioasm;
32     std::string format(DEFAULT_OUTPUT_FORMAT);
33     const char *input = nullptr;
34     const char *output = nullptr;
35     std::vector<std::string> options;
36     int i = 1;
37     for (; !res && i < argc; i++) {
38         if (argv[i][0] != '-') break;
39         if (argv[i] == std::string("-o")) {
40             if (++i < argc) {
41                 format = argv[i];
42             } else {
43                 std::cerr << "error: -o requires format value" << std::endl;
44                 res = 1;
45             }
46         } else if (argv[i] == std::string("-p")) {
47             if (++i < argc) {
48                 options.emplace_back(argv[i]);
49             } else {
50                 std::cerr << "error: -p requires parameter value" << std::endl;
51                 res = 1;
52             }
53         } else if (argv[i] == std::string("-?") || argv[i] == std::string("--help")) {
54             usage();
55             return 1;
56         } else {
57             std::cerr << "error: unknown option " << argv[i] << std::endl;
58             res = 1;
59         }
60     }
61     if (!res) {
62         if (i != argc) {
63             input = argv[i++];
64         } else {
65             std::cerr << "error: expected input filename\n";
66             res = 1;
67         }
68     }
69     if (!res) {
70         if (i != argc) {
71             output = argv[i++];
72         } else {
73             output = "-";
74         }
75     }
76     if (!res && i != argc) {
77         std::cerr << "unexpected command line argument " << argv[i] << std::endl;
78         res = 1;
79     }
80     std::shared_ptr<output_format> oformat;
81     if (!res) {
82         const auto& e = std::find_if(output_format::all().begin(), output_format::all().end(),
83                                      [&](const std::shared_ptr<output_format> &f) {
84                                          return f->name == format;
85                                      });
86         if (e == output_format::all().end()) {
87             std::cerr << "error: unknown output format '" << format << "'" << std::endl;
88             res = 1;
89         } else {
90             oformat = *e;
91         }
92     }
93     if (res) {
94         std::cerr << std::endl;
95         usage();
96     } else {
97         res = pioasm.generate(oformat, input, output, options);
98     }
99     return res;
100 }