paul@11 | 1 | /* |
paul@11 | 2 | * Main program. |
paul@28 | 3 | * |
paul@28 | 4 | * Copyright (C) 2019 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@56 | 29 | #include "program.h" |
paul@118 | 30 | #include "templates.h" |
paul@118 | 31 | #include "version.h" |
paul@33 | 32 | |
paul@33 | 33 | /* Parser functions and settings. */ |
paul@33 | 34 | |
paul@11 | 35 | extern void yyparse(void); |
paul@33 | 36 | extern void yyrestart(FILE *); |
paul@11 | 37 | extern int yydebug; |
paul@11 | 38 | |
paul@33 | 39 | /* Specialised parser functions. */ |
paul@33 | 40 | |
paul@33 | 41 | extern void reset(void); |
paul@33 | 42 | |
paul@33 | 43 | |
paul@33 | 44 | |
paul@33 | 45 | /* Return a copy of the given string without any extension. */ |
paul@33 | 46 | |
paul@97 | 47 | static char *make_prefix(const char *s) |
paul@33 | 48 | { |
paul@33 | 49 | char *copy = strdup(s); |
paul@33 | 50 | char *ext; |
paul@33 | 51 | |
paul@33 | 52 | if (copy == NULL) |
paul@33 | 53 | return NULL; |
paul@33 | 54 | |
paul@33 | 55 | ext = strrchr(copy, '.'); |
paul@33 | 56 | |
paul@33 | 57 | if (ext != NULL) |
paul@33 | 58 | *ext = '\0'; |
paul@33 | 59 | |
paul@33 | 60 | return copy; |
paul@33 | 61 | } |
paul@33 | 62 | |
paul@97 | 63 | static int get_compound_prefix_conflict(int argc, char *argv[]) |
paul@97 | 64 | { |
paul@97 | 65 | char *arg, *prefix, *name; |
paul@97 | 66 | int index, result; |
paul@97 | 67 | |
paul@97 | 68 | for (index = optind; index < argc; index++) |
paul@97 | 69 | { |
paul@97 | 70 | arg = argv[index]; |
paul@97 | 71 | prefix = make_prefix(arg); |
paul@97 | 72 | result = 0; |
paul@97 | 73 | |
paul@97 | 74 | if (prefix != NULL) |
paul@97 | 75 | { |
paul@97 | 76 | name = make_basename(prefix); |
paul@97 | 77 | |
paul@97 | 78 | if (name != NULL) |
paul@97 | 79 | { |
paul@126 | 80 | result = strcasecmp(name, conf.compound); |
paul@97 | 81 | free(name); |
paul@97 | 82 | } |
paul@97 | 83 | |
paul@97 | 84 | free(prefix); |
paul@97 | 85 | } |
paul@97 | 86 | |
paul@97 | 87 | if (!result) |
paul@97 | 88 | return index; |
paul@97 | 89 | } |
paul@97 | 90 | |
paul@97 | 91 | return 0; |
paul@97 | 92 | } |
paul@40 | 93 | |
paul@40 | 94 | |
paul@40 | 95 | /* Option definitions. */ |
paul@39 | 96 | |
paul@40 | 97 | static struct option long_options[] = { |
paul@78 | 98 | /* long opt following var pointer short opt */ |
paul@78 | 99 | {"all", no_argument, NULL, 'a' }, |
paul@78 | 100 | {"client", no_argument, NULL, 'c' }, |
paul@101 | 101 | {"comp", required_argument, NULL, 'C' }, |
paul@78 | 102 | {"dir", required_argument, NULL, 'd' }, |
paul@78 | 103 | {"headers", no_argument, NULL, 'h' }, |
paul@118 | 104 | {"help", no_argument, NULL, '?' }, |
paul@86 | 105 | {"interfaces", no_argument, NULL, 'i' }, |
paul@78 | 106 | {"language", required_argument, NULL, 'l' }, |
paul@101 | 107 | {"comp-name", required_argument, NULL, 'N' }, |
paul@78 | 108 | {"routines", no_argument, NULL, 'r' }, |
paul@78 | 109 | {"server", no_argument, NULL, 's' }, |
paul@78 | 110 | {"verbose", no_argument, NULL, 'v' }, |
paul@118 | 111 | {"version", no_argument, NULL, 'V' }, |
paul@78 | 112 | {0, 0, 0, 0 }, |
paul@40 | 113 | }; |
paul@33 | 114 | |
paul@33 | 115 | |
paul@33 | 116 | /* Main program, parsing indicated files or standard input. */ |
paul@33 | 117 | |
paul@11 | 118 | int main(int argc, char *argv[]) |
paul@11 | 119 | { |
paul@33 | 120 | FILE *fp; |
paul@118 | 121 | char *arg, *progname; |
paul@126 | 122 | int option, index, selected_content = 0, selected_role = 0; |
paul@33 | 123 | |
paul@11 | 124 | #if YYDEBUG == 1 |
paul@11 | 125 | yydebug = 0; |
paul@11 | 126 | #endif |
paul@11 | 127 | |
paul@40 | 128 | /* Accept various options. */ |
paul@40 | 129 | |
paul@40 | 130 | while (1) |
paul@40 | 131 | { |
paul@118 | 132 | option = getopt_long(argc, argv, "acC:d:hil:N:rsvV?", long_options, NULL); |
paul@40 | 133 | if (option == -1) |
paul@40 | 134 | break; |
paul@40 | 135 | |
paul@40 | 136 | switch (option) |
paul@40 | 137 | { |
paul@45 | 138 | /* Generate everything. */ |
paul@42 | 139 | case 'a': |
paul@126 | 140 | conf.client = 1; conf.server = 1; |
paul@126 | 141 | conf.headers = 1; conf.interfaces = 1; conf.routines = 1; |
paul@126 | 142 | selected_content = 1; |
paul@126 | 143 | selected_role = 1; |
paul@42 | 144 | break; |
paul@45 | 145 | |
paul@45 | 146 | /* Select client output. */ |
paul@42 | 147 | case 'c': |
paul@126 | 148 | conf.client = 1; |
paul@126 | 149 | selected_role = 1; |
paul@42 | 150 | break; |
paul@45 | 151 | |
paul@101 | 152 | /* Select compound interface filename prefix and output. */ |
paul@91 | 153 | case 'C': |
paul@126 | 154 | conf.compound = optarg; |
paul@126 | 155 | if (conf.compound_name == NULL) |
paul@126 | 156 | conf.compound_name = conf.compound; |
paul@126 | 157 | selected_role = 1; |
paul@91 | 158 | break; |
paul@91 | 159 | |
paul@45 | 160 | /* Set output directory. */ |
paul@42 | 161 | case 'd': |
paul@126 | 162 | conf.output_dir = optarg; |
paul@42 | 163 | break; |
paul@45 | 164 | |
paul@45 | 165 | /* Generate headers. */ |
paul@42 | 166 | case 'h': |
paul@126 | 167 | conf.headers = 1; |
paul@126 | 168 | selected_content = 1; |
paul@45 | 169 | break; |
paul@45 | 170 | |
paul@86 | 171 | /* Generate interface headers. */ |
paul@86 | 172 | case 'i': |
paul@126 | 173 | conf.interfaces = 1; |
paul@126 | 174 | selected_content = 1; |
paul@86 | 175 | break; |
paul@86 | 176 | |
paul@72 | 177 | /* Select output language. */ |
paul@72 | 178 | case 'l': |
paul@72 | 179 | if (!strcasecmp(optarg, "c")) |
paul@126 | 180 | conf.language = C_LANGUAGE; |
paul@72 | 181 | else if (!strcasecmp(optarg, "c++")) |
paul@126 | 182 | conf.language = CPP_LANGUAGE; |
paul@72 | 183 | else |
paul@72 | 184 | { |
paul@72 | 185 | fprintf(stderr, "Output language not recognised: %s\n", optarg); |
paul@72 | 186 | return 1; |
paul@72 | 187 | } |
paul@72 | 188 | break; |
paul@72 | 189 | |
paul@101 | 190 | /* Select compound interface name and output. */ |
paul@101 | 191 | case 'N': |
paul@126 | 192 | conf.compound_name = optarg; |
paul@126 | 193 | if (conf.compound == NULL) |
paul@126 | 194 | conf.compound = conf.compound_name; |
paul@126 | 195 | selected_role = 1; |
paul@101 | 196 | break; |
paul@101 | 197 | |
paul@45 | 198 | /* Generate routines (definitions). */ |
paul@45 | 199 | case 'r': |
paul@126 | 200 | conf.routines = 1; |
paul@126 | 201 | selected_content = 1; |
paul@42 | 202 | break; |
paul@45 | 203 | |
paul@45 | 204 | /* Select server output. */ |
paul@45 | 205 | case 's': |
paul@126 | 206 | conf.server = 1; |
paul@126 | 207 | selected_role = 1; |
paul@45 | 208 | break; |
paul@45 | 209 | |
paul@45 | 210 | /* Verbose reporting. */ |
paul@42 | 211 | case 'v': |
paul@126 | 212 | conf.verbose = 1; |
paul@42 | 213 | break; |
paul@45 | 214 | |
paul@118 | 215 | /* Show version (defined in the Makefile) and exit. */ |
paul@118 | 216 | case 'V': |
paul@118 | 217 | puts(VERSION_INFORMATION); |
paul@118 | 218 | return 0; |
paul@118 | 219 | |
paul@118 | 220 | /* Show help and exit. */ |
paul@118 | 221 | case '?': |
paul@118 | 222 | progname = make_basename(argv[0]); |
paul@118 | 223 | printf(help_text, progname); |
paul@118 | 224 | free(progname); |
paul@118 | 225 | return 0; |
paul@118 | 226 | |
paul@42 | 227 | default: |
paul@42 | 228 | return 1; |
paul@40 | 229 | } |
paul@40 | 230 | } |
paul@40 | 231 | |
paul@45 | 232 | /* Without any output selection, enable all output of the given kind. */ |
paul@42 | 233 | |
paul@126 | 234 | if (!selected_content) |
paul@42 | 235 | { |
paul@126 | 236 | conf.headers = 1; conf.interfaces = 1; conf.routines = 1; |
paul@45 | 237 | } |
paul@45 | 238 | |
paul@126 | 239 | if (!selected_role) |
paul@45 | 240 | { |
paul@126 | 241 | conf.client = 1; conf.server = 1; |
paul@42 | 242 | } |
paul@42 | 243 | |
paul@97 | 244 | /* Check any compound interface prefix against named files. */ |
paul@97 | 245 | |
paul@126 | 246 | if (conf.compound != NULL) |
paul@97 | 247 | { |
paul@97 | 248 | if ((index = get_compound_prefix_conflict(argc, argv))) |
paul@97 | 249 | { |
paul@97 | 250 | fprintf(stderr, "Compound interface prefix %s conflicts with filename %s.\n", |
paul@126 | 251 | conf.compound, argv[index]); |
paul@97 | 252 | return 1; |
paul@97 | 253 | } |
paul@97 | 254 | } |
paul@97 | 255 | |
paul@118 | 256 | /* Produce an error without any input files. */ |
paul@118 | 257 | |
paul@118 | 258 | if (optind >= argc) |
paul@118 | 259 | { |
paul@118 | 260 | fputs("No input files.\n", stderr); |
paul@118 | 261 | return 1; |
paul@118 | 262 | } |
paul@118 | 263 | |
paul@91 | 264 | /* Begin generating any compound interface code. */ |
paul@56 | 265 | |
paul@157 | 266 | if (!begin_compound_output()) |
paul@157 | 267 | { |
paul@157 | 268 | end_compound_output(); |
paul@157 | 269 | return 1; |
paul@157 | 270 | } |
paul@56 | 271 | |
paul@33 | 272 | /* Process named files. */ |
paul@33 | 273 | |
paul@40 | 274 | for (index = optind; index < argc; index++) |
paul@33 | 275 | { |
paul@39 | 276 | arg = argv[index]; |
paul@39 | 277 | |
paul@39 | 278 | /* Handle actual files. */ |
paul@39 | 279 | |
paul@126 | 280 | conf.output_prefix = make_prefix(arg); |
paul@33 | 281 | |
paul@126 | 282 | if (conf.output_prefix != NULL) |
paul@33 | 283 | { |
paul@39 | 284 | fp = fopen(arg, "r"); |
paul@33 | 285 | |
paul@33 | 286 | if (fp != NULL) |
paul@33 | 287 | { |
paul@33 | 288 | yyrestart(fp); |
paul@33 | 289 | yyparse(); |
paul@33 | 290 | fclose(fp); |
paul@33 | 291 | |
paul@33 | 292 | /* Reset any specialised parser state. */ |
paul@33 | 293 | |
paul@33 | 294 | reset(); |
paul@33 | 295 | } |
paul@33 | 296 | |
paul@126 | 297 | free(conf.output_prefix); |
paul@105 | 298 | |
paul@105 | 299 | if (fp == NULL) |
paul@105 | 300 | { |
paul@105 | 301 | fprintf(stderr, "Could not open file: %s\n", arg); |
paul@105 | 302 | return 1; |
paul@105 | 303 | } |
paul@33 | 304 | } |
paul@33 | 305 | } |
paul@33 | 306 | |
paul@91 | 307 | /* End generating any compound interface code. */ |
paul@56 | 308 | |
paul@91 | 309 | end_compound_output(); |
paul@56 | 310 | |
paul@11 | 311 | return 0; |
paul@11 | 312 | } |