Fix o_net_consolidate_segments() to consolidate with all joined lines
[geda-gaf/pcjc2.git] / gnetlist / src / parsecmd.c
blob2cd76c5dd34e0dfce137b511bc31b2619f9f4bf8
1 /* gEDA - GPL Electronic Design Automation
2 * gnetlist - gEDA Netlist
3 * Copyright (C) 1998-2010 Ales Hvezda
4 * Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <config.h>
22 #include <version.h>
23 #include <missing.h>
25 #include <stdio.h>
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
33 #ifdef HAVE_GETOPT_H
34 #include <getopt.h>
35 #endif
37 #include <libgeda/libgeda.h>
39 #include "../include/globals.h"
40 #include "../include/prototype.h"
41 #include "../include/gettext.h"
43 #ifdef HAVE_LIBDMALLOC
44 #include <dmalloc.h>
45 #endif
47 #define OPTIONS "c:g:hil:L:m:o:O:qvV"
49 #ifndef OPTARG_IN_UNISTD
50 extern char *optarg;
51 extern int optind;
52 #endif
55 /* Added by SDB 3.3.2006. */
56 #ifdef HAVE_GETOPT_LONG
57 struct option long_options[] =
59 {"help", 0, 0, 'h'},
60 {"list-backends", 0, &list_backends, TRUE},
61 {"verbose", 0, 0, 'v'},
62 {"version", 0, 0, 'V'},
63 {0, 0, 0, 0}
65 #endif
69 void usage(char *cmd)
71 printf (_(
72 "Usage: %s [OPTION ...] [-g BACKEND] [--] FILE ...\n"
73 "\n"
74 "Generate a netlist from one or more gEDA schematic FILEs.\n"
75 "\n"
76 "General options:\n"
77 " -q Quiet mode.\n"
78 " -v, --verbose Verbose mode.\n"
79 " -o FILE Filename for netlist data output.\n"
80 " -L DIR Add DIR to Scheme search path.\n"
81 " -g BACKEND Specify netlist backend to use.\n"
82 " -O STRING Pass an option string to backend.\n"
83 " -l FILE Load Scheme file before loading backend.\n"
84 " -m FILE Load Scheme file after loading backend.\n"
85 " -c EXPR Evaluate Scheme expression at startup.\n"
86 " -i Enter interactive Scheme REPL after loading.\n"
87 " --list-backends Print a list of available netlist backends.\n"
88 " -h, --help Help; this message.\n"
89 " -V, --version Show version information.\n"
90 " -- Treat all remaining arguments as filenames.\n"
91 "\n"
92 "Report bugs at <https://bugs.launchpad.net/geda>\n"
93 "gEDA/gaf homepage: <http://www.geda-project.org/>\n"),
94 cmd);
95 exit (0);
98 /*! \brief Print version info and exit.
99 * \par Function Description
100 * Print gEDA version, and copyright/warranty notices, and exit with
101 * exit status 0.
103 static void
104 version ()
106 printf(_(
107 "gEDA %s (g%.7s)\n"
108 "Copyright (C) 1998-2012 gEDA developers\n"
109 "This is free software, and you are welcome to redistribute it under\n"
110 "certain conditions. For details, see the file `COPYING', which is\n"
111 "included in the gEDA distribution.\n"
112 "There is NO WARRANTY, to the extent permitted by law.\n"),
113 PACKAGE_DOTTED_VERSION, PACKAGE_GIT_COMMIT);
114 exit (0);
117 /* from guile (libguile/gh_init.c) */
118 static SCM
119 catch_handler (void *data, SCM tag, SCM throw_args)
121 fprintf (stderr, _("\nJust got an error; tag is\n "));
122 scm_display (tag, scm_current_output_port ());
123 scm_newline (scm_current_output_port ());
124 scm_newline (scm_current_output_port ());
125 return SCM_BOOL_F;
130 parse_commandline (int argc, char *argv[])
132 int ch;
133 SCM sym_begin = scm_from_utf8_symbol ("begin");
134 SCM sym_cons = scm_from_utf8_symbol ("cons");
135 SCM sym_load = scm_from_utf8_symbol ("load");
136 SCM sym_set_x = scm_from_utf8_symbol ("set!");
137 SCM sym_load_path = scm_from_utf8_symbol ("%load-path");
139 #ifdef HAVE_GETOPT_LONG
140 /* int option_index = 0; */
142 while ((ch = getopt_long(argc, argv, OPTIONS, long_options, NULL /* &option_index */)) != -1) {
143 #else
144 while ((ch = getopt(argc, argv, OPTIONS)) != -1) {
145 #endif
146 switch (ch) {
148 case 0:
149 /* This is a long-form-only flag option, and has already been
150 * dealt with by getopt_long(). */
151 break;
153 case 'v':
154 verbose_mode = TRUE;
155 break;
157 case 'i':
158 interactive_mode = TRUE;
159 break;
161 case 'q':
162 quiet_mode = TRUE;
163 break;
165 case 'L':
166 /* Argument is a directory to add to the Scheme load path.
167 * Add the necessary expression to be evaluated before rc file
168 * loading. */
169 pre_rc_list =
170 scm_cons (scm_list_3 (sym_set_x,
171 sym_load_path,
172 scm_list_3 (sym_cons,
173 scm_from_locale_string (optarg),
174 sym_load_path)),
175 pre_rc_list);
176 break;
178 case 'g':
179 guile_proc = g_strdup(optarg);
180 break;
182 case 'l':
183 /* Argument is filename of a Scheme script to be run before
184 * loading gnetlist backend. */
185 pre_backend_list =
186 scm_cons (scm_list_2 (sym_load, scm_from_locale_string (optarg)),
187 pre_backend_list);
188 break;
190 case 'm':
191 /* Argument is filename of a Scheme script to be run after
192 * loading gnetlist backend. */
193 post_backend_list =
194 scm_cons (scm_list_2 (sym_load, scm_from_locale_string (optarg)),
195 post_backend_list);
196 break;
198 case 'o':
199 g_free(output_filename);
200 output_filename = g_strdup(optarg);
201 break;
203 case 'O':
204 backend_params = g_slist_append(backend_params, optarg);
205 break;
207 case 'c':
208 scm_internal_stack_catch (SCM_BOOL_T,
209 (scm_t_catch_body) scm_c_eval_string,
210 (void *) optarg,
211 (scm_t_catch_handler) catch_handler,
212 (void *) optarg);
213 break;
215 case 'h':
216 usage(argv[0]);
217 break;
219 case 'V':
220 version();
221 break;
223 case '?':
224 #ifndef HAVE_GETOPT_LONG
225 if ((optopt != ':') && (strchr (GETOPT_OPTIONS, optopt) != NULL)) {
226 fprintf (stderr,
227 _("ERROR: -%c option requires an argument.\n\n"),
228 optopt);
229 } else if (isprint (optopt)) {
230 fprintf (stderr, _("ERROR: Unknown option -%c.\n\n"), optopt);
231 } else {
232 fprintf (stderr, _("ERROR: Unknown option character `\\x%x'.\n\n"),
233 optopt);
235 #endif
236 fprintf (stderr, _("\nRun `%s --help' for more information.\n"), argv[0]);
237 exit (1);
238 break;
240 default:
241 g_assert_not_reached ();
245 if (quiet_mode) {
246 verbose_mode = FALSE;
249 /* Make sure Scheme expressions can be passed straight to eval */
250 pre_rc_list = scm_cons (sym_begin,
251 scm_reverse_x (pre_rc_list, SCM_UNDEFINED));
252 scm_gc_protect_object (pre_rc_list);
253 pre_backend_list = scm_cons (sym_begin,
254 scm_reverse_x (pre_backend_list, SCM_UNDEFINED));
255 scm_gc_protect_object (pre_backend_list);
256 post_backend_list = scm_cons (sym_begin,
257 scm_reverse_x (post_backend_list, SCM_UNDEFINED));
258 scm_gc_protect_object (post_backend_list);
260 return (optind);