Clarify error message
[geda-gaf.git] / gaf / gaf.c
blobf0341a9f582e157f331d3c4c1ac095318b6fe7c1
1 /*
2 * gEDA/gaf command-line utility
3 * Copyright (C) 2012-2013 Peter Brett <peter@peter-b.co.uk>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23 #include <locale.h>
24 #include <version.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <getopt.h>
32 /* Gettext translation */
33 #include "gettext.h"
35 #include <glib.h>
37 #include "builtins.h"
39 #define short_options "+hV"
41 static struct option long_options[] =
43 {"help", 0, NULL, 'h'},
44 {"version", 0, NULL, 'V'},
45 {"no-rcfiles", 0, NULL, 2},
46 {NULL, 0, NULL, 0},
49 struct internal_command {
50 char *name;
51 int (*func)(int, char **);
54 static struct internal_command commands[] =
56 {"shell", cmd_shell},
57 {"config", cmd_config},
58 {"export", cmd_export},
59 {NULL, NULL},
62 /* Print help info and exit */
63 static void
64 usage (void)
66 printf (_("Usage: gaf [OPTION...] COMMAND [ARGS ...]\n"
67 "\n"
68 "gEDA/gaf command-line utility.\n"
69 "\n"
70 "General options:\n"
71 " --no-rcfiles inhibit loading of 'gafrc' files\n"
72 " -h, --help display usage information and exit\n"
73 " -V, --version display version information and exit\n"
74 "\n"
75 "Commonly-used commands (type `gaf <cmd> --help' for usage):\n"
76 " shell Scheme REPL for interactive gEDA data processing\n"
77 " config Edit gEDA configuration\n"
78 " export Export gEDA files in various image formats.\n"
79 "\n"
80 "Please report bugs to %s.\n"),
81 PACKAGE_BUGREPORT);
82 exit (0);
85 /* Print version info and exit */
86 static void
87 version (void)
89 printf(_("gEDA/gaf %s (g%.7s)\n"
90 "Copyright (C) 1998-2020 gEDA developers\n"
91 "This is free software, and you are welcome to redistribute it under\n"
92 "certain conditions. For details, see the file `COPYING', which is\n"
93 "included in the gEDA distribution.\n"
94 "There is NO WARRANTY, to the extent permitted by law.\n"),
95 PACKAGE_DOTTED_VERSION, PACKAGE_GIT_COMMIT);
96 exit (0);
99 int
100 main (int argc, char **argv)
102 int c;
103 char *cmd = NULL;
104 int cmd_argc = 0;
105 char **cmd_argv = NULL;
106 int (*cmd_func)(int, char **) = NULL;
108 /* Set up gettext */
109 #if ENABLE_NLS
110 setlocale (LC_ALL, "");
111 bindtextdomain ("geda-gaf", LOCALEDIR);
112 textdomain ("geda-gaf");
113 bind_textdomain_codeset ("geda-gaf", "UTF-8");
114 #endif
116 while (-1 != (c = getopt_long (argc, argv, short_options,
117 long_options, NULL))) {
118 switch (c) {
120 case 0:
121 /* This is a long-form-only flag option, and has already been
122 * dealt with by getopt_long(). */
123 break;
125 case 2: /* --no-rcfiles */
126 g_setenv ("GAF_INHIBIT_RCFILES", "1", 1);
127 break;
129 case 'V':
130 version ();
132 case 'h':
133 usage ();
135 case '?':
136 /* getopt_long already printed an error message */
137 fprintf (stderr, _("\nRun `gaf --help' for more information.\n"));
138 exit (1);
139 break;
141 default:
142 g_assert_not_reached ();
146 /* The next argument should be a command */
147 if (optind == argc) {
148 fprintf (stderr,
149 _("ERROR: You must specify a command to run.\n"
150 "\n"
151 "Run `gaf --help' for more information.\n"));
152 exit (1);
155 cmd = argv[optind];
157 /* Look up the command */
158 int i;
159 for (i = 0; commands[i].name != NULL; i++) {
160 if (strcmp (cmd, commands[i].name) == 0) {
161 cmd_func = commands[i].func;
162 break;
165 if (cmd_func == NULL) {
166 fprintf (stderr,
167 _("ERROR: Unrecognised command `%s'.\n"
168 "\n"
169 "Run `gaf --help' for more information.\n"),
170 cmd);
171 exit (1);
174 cmd_argc = argc - optind;
175 cmd_argv = argv + optind;
176 optind = 1;
178 return cmd_func (cmd_argc, cmd_argv);