paul@11 | 1 | /* |
paul@11 | 2 | * Main program. |
paul@28 | 3 | * |
paul@222 | 4 | * Copyright (C) 2019, 2022, 2023 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@222 | 40 | {"alignment", required_argument, NULL, 'A' }, |
paul@78 | 41 | {"all", no_argument, NULL, 'a' }, |
paul@78 | 42 | {"client", no_argument, NULL, 'c' }, |
paul@78 | 43 | {"dir", required_argument, NULL, 'd' }, |
paul@214 | 44 | {"files", no_argument, NULL, 'f' }, |
paul@78 | 45 | {"headers", no_argument, NULL, 'h' }, |
paul@118 | 46 | {"help", no_argument, NULL, '?' }, |
paul@86 | 47 | {"interfaces", no_argument, NULL, 'i' }, |
paul@78 | 48 | {"language", required_argument, NULL, 'l' }, |
paul@214 | 49 | {"recursive", no_argument, NULL, 'R' }, |
paul@78 | 50 | {"routines", no_argument, NULL, 'r' }, |
paul@78 | 51 | {"server", no_argument, NULL, 's' }, |
paul@78 | 52 | {"verbose", no_argument, NULL, 'v' }, |
paul@118 | 53 | {"version", no_argument, NULL, 'V' }, |
paul@78 | 54 | {0, 0, 0, 0 }, |
paul@40 | 55 | }; |
paul@33 | 56 | |
paul@33 | 57 | |
paul@33 | 58 | /* Main program, parsing indicated files or standard input. */ |
paul@33 | 59 | |
paul@11 | 60 | int main(int argc, char *argv[]) |
paul@11 | 61 | { |
paul@118 | 62 | char *arg, *progname; |
paul@126 | 63 | int option, index, selected_content = 0, selected_role = 0; |
paul@214 | 64 | struct interface *iface; |
paul@33 | 65 | |
paul@11 | 66 | #if YYDEBUG == 1 |
paul@11 | 67 | yydebug = 0; |
paul@11 | 68 | #endif |
paul@11 | 69 | |
paul@214 | 70 | /* The configuration structure is already initialised in config.c. */ |
paul@214 | 71 | |
paul@40 | 72 | /* Accept various options. */ |
paul@40 | 73 | |
paul@40 | 74 | while (1) |
paul@40 | 75 | { |
paul@222 | 76 | option = getopt_long(argc, argv, "A:acd:fhil:rRsvV?", long_options, NULL); |
paul@40 | 77 | if (option == -1) |
paul@40 | 78 | break; |
paul@40 | 79 | |
paul@40 | 80 | switch (option) |
paul@40 | 81 | { |
paul@222 | 82 | /* Set alignment. */ |
paul@222 | 83 | case 'A': |
paul@222 | 84 | conf.alignment = atoi(optarg); |
paul@222 | 85 | break; |
paul@222 | 86 | |
paul@45 | 87 | /* Generate everything. */ |
paul@42 | 88 | case 'a': |
paul@126 | 89 | conf.client = 1; conf.server = 1; |
paul@126 | 90 | conf.headers = 1; conf.interfaces = 1; conf.routines = 1; |
paul@126 | 91 | selected_content = 1; |
paul@126 | 92 | selected_role = 1; |
paul@42 | 93 | break; |
paul@45 | 94 | |
paul@45 | 95 | /* Select client output. */ |
paul@42 | 96 | case 'c': |
paul@126 | 97 | conf.client = 1; |
paul@126 | 98 | selected_role = 1; |
paul@42 | 99 | break; |
paul@45 | 100 | |
paul@45 | 101 | /* Set output directory. */ |
paul@42 | 102 | case 'd': |
paul@126 | 103 | conf.output_dir = optarg; |
paul@42 | 104 | break; |
paul@45 | 105 | |
paul@214 | 106 | /* Only report processed file basenames. */ |
paul@214 | 107 | case 'f': |
paul@214 | 108 | conf.show_filenames = 1; |
paul@214 | 109 | break; |
paul@214 | 110 | |
paul@45 | 111 | /* Generate headers. */ |
paul@42 | 112 | case 'h': |
paul@126 | 113 | conf.headers = 1; |
paul@126 | 114 | selected_content = 1; |
paul@45 | 115 | break; |
paul@45 | 116 | |
paul@86 | 117 | /* Generate interface headers. */ |
paul@86 | 118 | case 'i': |
paul@126 | 119 | conf.interfaces = 1; |
paul@126 | 120 | selected_content = 1; |
paul@86 | 121 | break; |
paul@86 | 122 | |
paul@72 | 123 | /* Select output language. */ |
paul@72 | 124 | case 'l': |
paul@72 | 125 | if (!strcasecmp(optarg, "c")) |
paul@126 | 126 | conf.language = C_LANGUAGE; |
paul@72 | 127 | else if (!strcasecmp(optarg, "c++")) |
paul@126 | 128 | conf.language = CPP_LANGUAGE; |
paul@72 | 129 | else |
paul@72 | 130 | { |
paul@72 | 131 | fprintf(stderr, "Output language not recognised: %s\n", optarg); |
paul@72 | 132 | return 1; |
paul@72 | 133 | } |
paul@72 | 134 | break; |
paul@72 | 135 | |
paul@45 | 136 | /* Generate routines (definitions). */ |
paul@45 | 137 | case 'r': |
paul@126 | 138 | conf.routines = 1; |
paul@126 | 139 | selected_content = 1; |
paul@42 | 140 | break; |
paul@45 | 141 | |
paul@214 | 142 | /* Also generate files associated with base interfaces. */ |
paul@214 | 143 | case 'R': |
paul@214 | 144 | conf.generate_all = 1; |
paul@214 | 145 | break; |
paul@214 | 146 | |
paul@45 | 147 | /* Select server output. */ |
paul@45 | 148 | case 's': |
paul@126 | 149 | conf.server = 1; |
paul@126 | 150 | selected_role = 1; |
paul@45 | 151 | break; |
paul@45 | 152 | |
paul@45 | 153 | /* Verbose reporting. */ |
paul@42 | 154 | case 'v': |
paul@126 | 155 | conf.verbose = 1; |
paul@42 | 156 | break; |
paul@45 | 157 | |
paul@118 | 158 | /* Show version (defined in the Makefile) and exit. */ |
paul@118 | 159 | case 'V': |
paul@118 | 160 | puts(VERSION_INFORMATION); |
paul@118 | 161 | return 0; |
paul@118 | 162 | |
paul@118 | 163 | /* Show help and exit. */ |
paul@118 | 164 | case '?': |
paul@118 | 165 | progname = make_basename(argv[0]); |
paul@118 | 166 | printf(help_text, progname); |
paul@118 | 167 | free(progname); |
paul@118 | 168 | return 0; |
paul@118 | 169 | |
paul@42 | 170 | default: |
paul@42 | 171 | return 1; |
paul@40 | 172 | } |
paul@40 | 173 | } |
paul@40 | 174 | |
paul@45 | 175 | /* Without any output selection, enable all output of the given kind. */ |
paul@42 | 176 | |
paul@126 | 177 | if (!selected_content) |
paul@42 | 178 | { |
paul@126 | 179 | conf.headers = 1; conf.interfaces = 1; conf.routines = 1; |
paul@45 | 180 | } |
paul@45 | 181 | |
paul@126 | 182 | if (!selected_role) |
paul@45 | 183 | { |
paul@126 | 184 | conf.client = 1; conf.server = 1; |
paul@42 | 185 | } |
paul@42 | 186 | |
paul@118 | 187 | /* Produce an error without any input files. */ |
paul@118 | 188 | |
paul@118 | 189 | if (optind >= argc) |
paul@118 | 190 | { |
paul@118 | 191 | fputs("No input files.\n", stderr); |
paul@118 | 192 | return 1; |
paul@118 | 193 | } |
paul@118 | 194 | |
paul@33 | 195 | /* Process named files. */ |
paul@33 | 196 | |
paul@40 | 197 | for (index = optind; index < argc; index++) |
paul@33 | 198 | { |
paul@39 | 199 | arg = argv[index]; |
paul@39 | 200 | |
paul@214 | 201 | /* Import each file for processing. */ |
paul@33 | 202 | |
paul@214 | 203 | iface = import_file(arg, 1); |
paul@33 | 204 | |
paul@214 | 205 | if (iface == NULL) |
paul@214 | 206 | { |
paul@214 | 207 | fprintf(stderr, "Could not process file: %s\n", arg); |
paul@214 | 208 | return 1; |
paul@214 | 209 | } |
paul@105 | 210 | |
paul@214 | 211 | /* Summarise interfaces if requested. */ |
paul@214 | 212 | |
paul@214 | 213 | if (conf.verbose) |
paul@214 | 214 | show_interfaces(iface); |
paul@33 | 215 | } |
paul@33 | 216 | |
paul@11 | 217 | return 0; |
paul@11 | 218 | } |