File: main.cpp

package info (click to toggle)
pico-sdk 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 26,552 kB
  • sloc: ansic: 146,841; asm: 13,423; python: 2,417; cpp: 2,171; yacc: 381; lex: 270; makefile: 32; sh: 13; javascript: 13
file content (113 lines) | stat: -rw-r--r-- 4,179 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <iostream>
#include "pio_assembler.h"

#define DEFAULT_OUTPUT_FORMAT "c-sdk"

void usage() {
    std::cerr << "usage: pioasm <options> <input> (<output>)\n\n";
    std::cerr << "Assemble file of PIO program(s) for use in applications.\n";
    std::cerr << "   <input>             the input filename\n";
    std::cerr << "   <output>            the output filename (or filename prefix if the output format produces multiple outputs).\n";
    std::cerr << "                       if not specified, a single output will be written to stdout\n";
    std::cerr << "\n";
    std::cerr << "options:\n";
    std::cerr << "  -o <output_format>   select output_format (default '" << DEFAULT_OUTPUT_FORMAT << "'); available options are:\n";
    for(const auto& f : output_format::all()) {
        std::cerr << "                           " << f->name << std::endl;
        std::cerr << "                               " << f->get_description() << std::endl;
    }
    std::cerr << "  -p <output_param>    add a parameter to be passed to the output format generator" << std::endl;
    std::cerr << "  -v <version>         specify the default PIO version (0 or 1)" << std::endl;
    std::cerr << "  -?, --help           print this help and exit\n";
}

int main(int argc, char *argv[]) {
    int res = 0;
    pio_assembler pioasm;
    std::string format(DEFAULT_OUTPUT_FORMAT);
    const char *input = nullptr;
    const char *output = nullptr;
    std::vector<std::string> options;
    int i = 1;
    for (; !res && i < argc; i++) {
        if (argv[i][0] != '-') break;
        if (argv[i] == std::string("-o")) {
            if (++i < argc) {
                format = argv[i];
            } else {
                std::cerr << "error: -o requires format value" << std::endl;
                res = 1;
            }
        } else if (argv[i] == std::string("-p")) {
            if (++i < argc) {
                options.emplace_back(argv[i]);
            } else {
                std::cerr << "error: -p requires parameter value" << std::endl;
                res = 1;
            }
        } else if (argv[i] == std::string("-v")) {
            if (++i < argc) {
                if (argv[i] == std::string("0")) pioasm.default_pio_version = 0;
                else if (argv[i] == std::string("1")) pioasm.default_pio_version = 1;
                else {
                    std::cerr << "error: unsupported PIO version '" <<  argv[i] << "'" << std::endl;
                    res = 1;
                }
            } else {
                std::cerr << "error: -v requires version number" << std::endl;
                res = 1;
            }
        } else if (argv[i] == std::string("-?") || argv[i] == std::string("--help")) {
            usage();
            return 1;
        } else {
            std::cerr << "error: unknown option " << argv[i] << std::endl;
            res = 1;
        }
    }
    if (!res) {
        if (i != argc) {
            input = argv[i++];
        } else {
            std::cerr << "error: expected input filename\n";
            res = 1;
        }
    }
    if (!res) {
        if (i != argc) {
            output = argv[i++];
        } else {
            output = "-";
        }
    }
    if (!res && i != argc) {
        std::cerr << "unexpected command line argument " << argv[i] << std::endl;
        res = 1;
    }
    std::shared_ptr<output_format> oformat;
    if (!res) {
        const auto& e = std::find_if(output_format::all().begin(), output_format::all().end(),
                                     [&](const std::shared_ptr<output_format> &f) {
                                         return f->name == format;
                                     });
        if (e == output_format::all().end()) {
            std::cerr << "error: unknown output format '" << format << "'" << std::endl;
            res = 1;
        } else {
            oformat = *e;
        }
    }
    if (res) {
        std::cerr << std::endl;
        usage();
    } else {
        res = pioasm.generate(oformat, input, output, options);
    }
    return res;
}