options.cpp revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1
2#include "options.h"
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7
8static int
9usage()
10{
11    fprintf(stderr,
12            "usage: aidl OPTIONS INPUT [OUTPUT]\n"
13            "       aidl --preprocess OUTPUT INPUT...\n"
14            "\n"
15            "OPTIONS:\n"
16            "   -I<DIR>  search path for import statements.\n"
17            "   -d<FILE> generate dependency file.\n"
18            "   -p<FILE> file created by --preprocess to import.\n"
19            "   -b       fail when trying to compile a parcelable.\n"
20            "\n"
21            "INPUT:\n"
22            "   An aidl interface file.\n"
23            "\n"
24            "OUTPUT:\n"
25            "   The generated interface files. If omitted, the input filename is used, with the .aidl extension changed to a .java extension.\n"
26           );
27    return 1;
28}
29
30int
31parse_options(int argc, const char* const* argv, Options *options)
32{
33    int i = 1;
34
35    if (argc >= 2 && 0 == strcmp(argv[1], "--preprocess")) {
36        if (argc < 4) {
37            return usage();
38        }
39        options->outputFileName = argv[2];
40        for (int i=3; i<argc; i++) {
41            options->filesToPreprocess.push_back(argv[i]);
42        }
43        options->task = PREPROCESS_AIDL;
44        return 0;
45    }
46
47    options->task = COMPILE_AIDL;
48    options->failOnParcelable = false;
49
50    // OPTIONS
51    while (i < argc) {
52        const char* s = argv[i];
53        int len = strlen(s);
54        if (s[0] == '-') {
55            if (len > 1) {
56                // -I<system-import-path>
57                if (s[1] == 'I') {
58                    if (len > 2) {
59                        options->importPaths.push_back(s+2);
60                    } else {
61                        fprintf(stderr, "-I option (%d) requires a path.\n", i);
62                        return usage();
63                    }
64                }
65                else if (s[1] == 'd') {
66                    if (len > 2) {
67                        options->depFileName = s+2;
68                    } else {
69                        fprintf(stderr, "-d option (%d) requires a file.\n", i);
70                        return usage();
71                    }
72                }
73                else if (s[1] == 'p') {
74                    if (len > 2) {
75                        options->preprocessedFiles.push_back(s+2);
76                    } else {
77                        fprintf(stderr, "-p option (%d) requires a file.\n", i);
78                        return usage();
79                    }
80                }
81                else if (len == 2 && s[1] == 'b') {
82                    options->failOnParcelable = true;
83                }
84                else {
85                    // s[1] is not known
86                    fprintf(stderr, "unknown option (%d): %s\n", i, s);
87                    return usage();
88                }
89            } else {
90                // len <= 1
91                fprintf(stderr, "unknown option (%d): %s\n", i, s);
92                return usage();
93            }
94        } else {
95            // s[0] != '-'
96            break;
97        }
98        i++;
99    }
100
101    // INPUT
102    if (i < argc) {
103        options->inputFileName = argv[i];
104        i++;
105    } else {
106        fprintf(stderr, "INPUT required\n");
107        return usage();
108    }
109
110    // OUTPUT
111    if (i < argc) {
112        options->outputFileName = argv[i];
113        i++;
114    } else {
115        // copy input into output and change the extension from .aidl to .java
116        options->outputFileName = options->inputFileName;
117        string::size_type pos = options->outputFileName.size()-5;
118        if (options->outputFileName.compare(pos, 5, ".aidl") == 0) {  // 5 = strlen(".aidl")
119            options->outputFileName.replace(pos, 5, ".java"); // 5 = strlen(".aidl")
120        } else {
121            fprintf(stderr, "INPUT is not an .aidl file.\n");
122            return usage();
123        }
124     }
125
126    // anything remaining?
127    if (i != argc) {
128        fprintf(stderr, "unknown option%s:", (i==argc-1?(const char*)"":(const char*)"s"));
129        for (; i<argc-1; i++) {
130            fprintf(stderr, " %s", argv[i]);
131        }
132        fprintf(stderr, "\n");
133        return usage();
134    }
135
136    return 0;
137}
138
139