gaf-config: Use new GFile config sys APIs where appropriate.
[geda-gaf/pcjc2.git] / gaf / gaf.c
blobcaf33d289bd577238a1c8cee096b329b2a1fbbe5
1 /*
2 * gEDA/gaf command-line utility
3 * Copyright (C) 2012 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 <version.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <getopt.h>
31 /* Gettext translation */
32 #include "gettext.h"
34 #include <glib.h>
36 #include "builtins.h"
38 #define short_options "+hV"
40 static struct option long_options[] =
42 {"help", 0, NULL, 'h'},
43 {"version", 0, NULL, 'V'},
44 {"no-rcfiles", 0, NULL, 2},
45 {NULL, 0, NULL, 0},
48 struct internal_command {
49 char *name;
50 int (*func)(int, char **);
53 static struct internal_command commands[] =
55 {"shell", cmd_shell},
56 {"config", cmd_config},
57 {"export", cmd_export},
58 {NULL, NULL},
61 /* Print help info and exit */
62 static void
63 usage (void)
65 printf (_("Usage: gaf [OPTION...] COMMAND [ARGS ...]\n"
66 "\n"
67 "gEDA/gaf command-line utility.\n"
68 "\n"
69 "General options:\n"
70 " --no-rcfiles inhibit loading of 'gafrc' files\n"
71 " -h, --help display usage information and exit\n"
72 " -V, --version display version information and exit\n"
73 "\n"
74 "Commonly-used commands (type `gaf <cmd> --help' for usage):\n"
75 " shell Scheme REPL for interactive gEDA data processing\n"
76 " config Edit gEDA configuration\n"
77 " export Export gEDA files in various image formats.\n"
78 "\n"
79 "Please report bugs to %s.\n"),
80 PACKAGE_BUGREPORT);
81 exit (0);
84 /* Print version info and exit */
85 static void
86 version (void)
88 printf(_("gEDA/gaf %s (g%.7s)\n"
89 "Copyright (C) 1998-2012 gEDA developers\n"
90 "This is free software, and you are welcome to redistribute it under\n"
91 "certain conditions. For details, see the file `COPYING', which is\n"
92 "included in the gEDA distribution.\n"
93 "There is NO WARRANTY, to the extent permitted by law.\n"),
94 PACKAGE_DOTTED_VERSION, PACKAGE_GIT_COMMIT);
95 exit (0);
98 int
99 main (int argc, char **argv)
101 int c;
102 char *cmd = NULL;
103 int cmd_argc = 0;
104 char **cmd_argv = NULL;
105 int (*cmd_func)(int, char **) = NULL;
107 /* Set up gettext */
108 #if ENABLE_NLS
109 setlocale (LC_ALL, "");
110 bindtextdomain ("geda-gaf", LOCALEDIR);
111 textdomain ("geda-gaf");
112 bind_textdomain_codeset ("geda-gschem", "UTF-8");
113 #endif
115 while (-1 != (c = getopt_long (argc, argv, short_options,
116 long_options, NULL))) {
117 switch (c) {
119 case 0:
120 /* This is a long-form-only flag option, and has already been
121 * dealt with by getopt_long(). */
122 break;
124 case 2: /* --no-rcfiles */
125 setenv ("GAF_INHIBIT_RCFILES", "1", 1);
126 break;
128 case 'V':
129 version ();
131 case 'h':
132 usage ();
134 case '?':
135 /* getopt_long already printed an error message */
136 fprintf (stderr, _("\nRun `gaf --help' for more information.\n"));
137 exit (1);
138 break;
140 default:
141 g_assert_not_reached ();
145 /* The next argument should be a command */
146 if (optind == argc) {
147 fprintf (stderr,
148 _("ERROR: You must specify a command to run.\n"
149 "\n"
150 "Run `gaf --help' for more information.\n"));
151 exit (1);
154 cmd = argv[optind];
156 /* Look up the command */
157 int i;
158 for (i = 0; commands[i].name != NULL; i++) {
159 if (strcmp (cmd, commands[i].name) == 0) {
160 cmd_func = commands[i].func;
161 break;
164 if (cmd_func == NULL) {
165 fprintf (stderr,
166 _("ERROR: Unrecognised command `%s'.\n"
167 "\n"
168 "Run `gaf --help' for more information.\n"),
169 cmd);
170 exit (1);
173 cmd_argc = argc - optind;
174 cmd_argv = argv + optind;
175 optind = 1;
177 return cmd_func (cmd_argc, cmd_argv);