Allow building with older library versions
[geda-gaf.git] / gaf / shell.c
blob043da49d68cce5b866bad89e9fb4d1600d924748
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
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <version.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <getopt.h>
29 /* Gettext translation */
30 #include "gettext.h"
32 #include <libgeda/libgeda.h>
33 #include <libgeda/libgedaguile.h>
35 #define shell_short_options "s:c:L:l:h"
37 static struct option shell_long_options[] =
39 {"help", 0, NULL, 'h'},
40 {NULL, 0, NULL, 0},
43 static void
44 shell_usage (void)
46 printf (_("Usage: gaf shell [OPTION ...]\n"
47 "\n"
48 "Shell for interactive processing of gEDA data using Scheme.\n"
49 "\n"
50 " -s FILE load Scheme source code from FILE, and exit\n"
51 " -c EXPR evaluate Scheme expression EXPR, and exit\n"
52 " -- stop scanning arguments; run interactively\n"
53 "\n"
54 "The above switches stop argument processing, and pass all\n"
55 "remaining arguments as the value of (command-line).\n"
56 "\n"
57 " -L DIRECTORY add DIRECTORY to the front of the Scheme load path\n"
58 " -l FILE load Scheme source code from FILE\n"
59 " -h, --help display usage information and exit\n"
60 "\n"
61 "Please report bugs to %s.\n"),
62 PACKAGE_BUGREPORT);
63 exit (0);
66 /* Some symbols we need */
67 SCM_SYMBOL (sym_begin, "begin");
68 SCM_SYMBOL (sym_use_modules, "use-modules");
69 SCM_SYMBOL (sym_ice_9, "ice-9");
70 SCM_SYMBOL (sym_readline, "readline");
71 SCM_SYMBOL (sym_activate_readline, "activate-readline");
73 static void
74 cmd_shell_impl (void *data, int argc, char **argv)
76 int c, interactive = 1;
77 TOPLEVEL *toplevel;
79 #include "shell.x"
81 /* Parse command-line arguments */
82 while ((c = getopt_long (argc, argv, shell_short_options,
83 shell_long_options, NULL)) != -1) {
84 switch (c) {
85 case 0:
86 /* This is a long-form-only flag option, and has already been
87 * dealt with by getopt_long(). */
88 break;
89 case 's':
90 case 'c':
91 interactive = 0;
92 /* Intentionally falls through */
93 case 'l':
94 case 'L':
95 /* Do nothing, scm_shell() will deal with these. */
96 break;
97 case 'h':
98 shell_usage ();
99 break;
100 case '?':
101 /* getopt_long already printed an error message */
102 fprintf (stderr, _("\nRun `gaf shell --help' for more information.\n"));
103 exit (1);
104 break;
105 default:
106 g_assert_not_reached ();
110 libgeda_init ();
111 scm_dynwind_begin (0);
112 toplevel = s_toplevel_new ();
113 edascm_dynwind_toplevel (toplevel);
115 /* Interactive, so enable readline support and print an abbreviated
116 * version message. */
117 if (interactive) {
118 fprintf (stderr, "gEDA %s (g%.7s)\n", PACKAGE_DOTTED_VERSION, PACKAGE_GIT_COMMIT);
119 /* readline is not supported for MinGW builds yet */
120 #ifndef __MINGW32__
121 SCM expr = scm_list_3 (sym_begin,
122 scm_list_2 (sym_use_modules,
123 scm_list_2 (sym_ice_9, sym_readline)),
124 scm_list_1 (sym_activate_readline));
126 scm_eval_x (expr, scm_current_module ());
127 #endif /* __MINGW32__ */
130 /* Now load rc files, if necessary */
131 if (getenv ("GAF_INHIBIT_RCFILES") == NULL) {
132 g_rc_parse (toplevel, "gaf shell", NULL, NULL);
134 i_vars_libgeda_set (toplevel); /* Ugh */
136 scm_shell (argc, argv); /* Doesn't return */
138 scm_dynwind_end ();
142 cmd_shell (int argc, char **argv)
144 scm_boot_guile (argc, argv, cmd_shell_impl, NULL); /* Doesn't return */
145 return 0;