paul@11 | 1 | /* |
paul@11 | 2 | * Main program. |
paul@28 | 3 | * |
paul@214 | 4 | * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@28 | 5 | * |
paul@28 | 6 | * This program is free software; you can redistribute it and/or |
paul@28 | 7 | * modify it under the terms of the GNU General Public License as |
paul@28 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@28 | 9 | * the License, or (at your option) any later version. |
paul@28 | 10 | * |
paul@28 | 11 | * This program is distributed in the hope that it will be useful, |
paul@28 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@28 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@28 | 14 | * GNU General Public License for more details. |
paul@28 | 15 | * |
paul@28 | 16 | * You should have received a copy of the GNU General Public License |
paul@28 | 17 | * along with this program; if not, write to the Free Software |
paul@28 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@28 | 19 | * Boston, MA 02110-1301, USA |
paul@11 | 20 | */ |
paul@11 | 21 | |
paul@40 | 22 | #include <getopt.h> |
paul@33 | 23 | #include <stdio.h> |
paul@33 | 24 | #include <stdlib.h> |
paul@33 | 25 | #include <string.h> |
paul@72 | 26 | #include <strings.h> |
paul@97 | 27 | #include "common.h" |
paul@126 | 28 | #include "config.h" |
paul@214 | 29 | #include "imports.h" |
paul@214 | 30 | #include "summary.h" |
paul@118 | 31 | #include "templates.h" |
paul@118 | 32 | #include "version.h" |
paul@33 | 33 | |
paul@40 | 34 | |
paul@40 | 35 | |
paul@40 | 36 | /* Option definitions. */ |
paul@39 | 37 | |
paul@40 | 38 | static struct option long_options[] = { |
paul@78 | 39 | /* long opt following var pointer short opt */ |
paul@78 | 40 | {"all", no_argument, NULL, 'a' }, |
paul@78 | 41 | {"client", no_argument, NULL, 'c' }, |
paul@78 | 42 | {"dir", required_argument, NULL, 'd' }, |
paul@214 | 43 | {"files", no_argument, NULL, 'f' }, |
paul@78 | 44 | {"headers", no_argument, NULL, 'h' }, |
paul@118 | 45 | {"help", no_argument, NULL, '?' }, |
paul@86 | 46 | {"interfaces", no_argument, NULL, 'i' }, |
paul@78 | 47 | {"language", required_argument, NULL, 'l' }, |
paul@214 | 48 | {"recursive", no_argument, NULL, 'R' }, |
paul@78 | 49 | {"routines", no_argument, NULL, 'r' }, |
paul@78 | 50 | {"server", no_argument, NULL, 's' }, |
paul@78 | 51 | {"verbose", no_argument, NULL, 'v' }, |
paul@118 | 52 | {"version", no_argument, NULL, 'V' }, |
paul@78 | 53 | {0, 0, 0, 0 }, |
paul@40 | 54 | }; |
paul@33 | 55 | |
paul@33 | 56 | |
paul@33 | 57 | /* Main program, parsing indicated files or standard input. */ |
paul@33 | 58 | |
paul@11 | 59 | int main(int argc, char *argv[]) |
paul@11 | 60 | { |
paul@118 | 61 | char *arg, *progname; |
paul@126 | 62 | int option, index, selected_content = 0, selected_role = 0; |
paul@214 | 63 | struct interface *iface; |
paul@33 | 64 | |
paul@11 | 65 | #if YYDEBUG == 1 |
paul@11 | 66 | yydebug = 0; |
paul@11 | 67 | #endif |
paul@11 | 68 | |
paul@214 | 69 | /* The configuration structure is already initialised in config.c. */ |
paul@214 | 70 | |
paul@40 | 71 | /* Accept various options. */ |
paul@40 | 72 | |
paul@40 | 73 | while (1) |
paul@40 | 74 | { |
paul@214 | 75 | option = getopt_long(argc, argv, "acd:fhil:rRsvV?", long_options, NULL); |
paul@40 | 76 | if (option == -1) |
paul@40 | 77 | break; |
paul@40 | 78 | |
paul@40 | 79 | switch (option) |
paul@40 | 80 | { |
paul@45 | 81 | /* Generate everything. */ |
paul@42 | 82 | case 'a': |
paul@126 | 83 | conf.client = 1; conf.server = 1; |
paul@126 | 84 | conf.headers = 1; conf.interfaces = 1; conf.routines = 1; |
paul@126 | 85 | selected_content = 1; |
paul@126 | 86 | selected_role = 1; |
paul@42 | 87 | break; |
paul@45 | 88 | |
paul@45 | 89 | /* Select client output. */ |
paul@42 | 90 | case 'c': |
paul@126 | 91 | conf.client = 1; |
paul@126 | 92 | selected_role = 1; |
paul@42 | 93 | break; |
paul@45 | 94 | |
paul@45 | 95 | /* Set output directory. */ |
paul@42 | 96 | case 'd': |
paul@126 | 97 | conf.output_dir = optarg; |
paul@42 | 98 | break; |
paul@45 | 99 | |
paul@214 | 100 | /* Only report processed file basenames. */ |
paul@214 | 101 | case 'f': |
paul@214 | 102 | conf.show_filenames = 1; |
paul@214 | 103 | break; |
paul@214 | 104 | |
paul@45 | 105 | /* Generate headers. */ |
paul@42 | 106 | case 'h': |
paul@126 | 107 | conf.headers = 1; |
paul@126 | 108 | selected_content = 1; |
paul@45 | 109 | break; |
paul@45 | 110 | |
paul@86 | 111 | /* Generate interface headers. */ |
paul@86 | 112 | case 'i': |
paul@126 | 113 | conf.interfaces = 1; |
paul@126 | 114 | selected_content = 1; |
paul@86 | 115 | break; |
paul@86 | 116 | |
paul@72 | 117 | /* Select output language. */ |
paul@72 | 118 | case 'l': |
paul@72 | 119 | if (!strcasecmp(optarg, "c")) |
paul@126 | 120 | conf.language = C_LANGUAGE; |
paul@72 | 121 | else if (!strcasecmp(optarg, "c++")) |
paul@126 | 122 | conf.language = CPP_LANGUAGE; |
paul@72 | 123 | else |
paul@72 | 124 | { |
paul@72 | 125 | fprintf(stderr, "Output language not recognised: %s\n", optarg); |
paul@72 | 126 | return 1; |
paul@72 | 127 | } |
paul@72 | 128 | break; |
paul@72 | 129 | |
paul@45 | 130 | /* Generate routines (definitions). */ |
paul@45 | 131 | case 'r': |
paul@126 | 132 | conf.routines = 1; |
paul@126 | 133 | selected_content = 1; |
paul@42 | 134 | break; |
paul@45 | 135 | |
paul@214 | 136 | /* Also generate files associated with base interfaces. */ |
paul@214 | 137 | case 'R': |
paul@214 | 138 | conf.generate_all = 1; |
paul@214 | 139 | break; |
paul@214 | 140 | |
paul@45 | 141 | /* Select server output. */ |
paul@45 | 142 | case 's': |
paul@126 | 143 | conf.server = 1; |
paul@126 | 144 | selected_role = 1; |
paul@45 | 145 | break; |
paul@45 | 146 | |
paul@45 | 147 | /* Verbose reporting. */ |
paul@42 | 148 | case 'v': |
paul@126 | 149 | conf.verbose = 1; |
paul@42 | 150 | break; |
paul@45 | 151 | |
paul@118 | 152 | /* Show version (defined in the Makefile) and exit. */ |
paul@118 | 153 | case 'V': |
paul@118 | 154 | puts(VERSION_INFORMATION); |
paul@118 | 155 | return 0; |
paul@118 | 156 | |
paul@118 | 157 | /* Show help and exit. */ |
paul@118 | 158 | case '?': |
paul@118 | 159 | progname = make_basename(argv[0]); |
paul@118 | 160 | printf(help_text, progname); |
paul@118 | 161 | free(progname); |
paul@118 | 162 | return 0; |
paul@118 | 163 | |
paul@42 | 164 | default: |
paul@42 | 165 | return 1; |
paul@40 | 166 | } |
paul@40 | 167 | } |
paul@40 | 168 | |
paul@45 | 169 | /* Without any output selection, enable all output of the given kind. */ |
paul@42 | 170 | |
paul@126 | 171 | if (!selected_content) |
paul@42 | 172 | { |
paul@126 | 173 | conf.headers = 1; conf.interfaces = 1; conf.routines = 1; |
paul@45 | 174 | } |
paul@45 | 175 | |
paul@126 | 176 | if (!selected_role) |
paul@45 | 177 | { |
paul@126 | 178 | conf.client = 1; conf.server = 1; |
paul@42 | 179 | } |
paul@42 | 180 | |
paul@118 | 181 | /* Produce an error without any input files. */ |
paul@118 | 182 | |
paul@118 | 183 | if (optind >= argc) |
paul@118 | 184 | { |
paul@118 | 185 | fputs("No input files.\n", stderr); |
paul@118 | 186 | return 1; |
paul@118 | 187 | } |
paul@118 | 188 | |
paul@33 | 189 | /* Process named files. */ |
paul@33 | 190 | |
paul@40 | 191 | for (index = optind; index < argc; index++) |
paul@33 | 192 | { |
paul@39 | 193 | arg = argv[index]; |
paul@39 | 194 | |
paul@214 | 195 | /* Import each file for processing. */ |
paul@33 | 196 | |
paul@214 | 197 | iface = import_file(arg, 1); |
paul@33 | 198 | |
paul@214 | 199 | if (iface == NULL) |
paul@214 | 200 | { |
paul@214 | 201 | fprintf(stderr, "Could not process file: %s\n", arg); |
paul@214 | 202 | return 1; |
paul@214 | 203 | } |
paul@105 | 204 | |
paul@214 | 205 | /* Summarise interfaces if requested. */ |
paul@214 | 206 | |
paul@214 | 207 | if (conf.verbose) |
paul@214 | 208 | show_interfaces(iface); |
paul@33 | 209 | } |
paul@33 | 210 | |
paul@11 | 211 | return 0; |
paul@11 | 212 | } |