gschem
|
00001 /* gEDA - GPL Electronic Design Automation 00002 * gschem - gEDA Schematic Capture 00003 * Copyright (C) 1998-2012 Ales Hvezda 00004 * Copyright (C) 1998-2012 gEDA Contributors (see ChangeLog for details) 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 #include <config.h> 00021 #include <version.h> 00022 #include <missing.h> 00023 00024 #include <stdio.h> 00025 #include <ctype.h> 00026 #ifdef HAVE_UNISTD_H 00027 #include <unistd.h> 00028 #endif 00029 00030 #include "gschem.h" 00031 00032 #ifdef HAVE_LIBDMALLOC 00033 #include <dmalloc.h> 00034 #endif 00035 00036 #define GETOPT_OPTIONS "c:hL:o:pqr:s:vV" 00037 00038 #ifndef OPTARG_IN_UNISTD 00039 extern char *optarg; 00040 extern int optind; 00041 #endif 00042 00043 #ifdef HAVE_GETOPT_H 00044 #include <getopt.h> 00045 #endif 00046 00047 #ifdef HAVE_GETOPT_LONG 00048 struct option long_options[] = 00049 { 00050 {"help", 0, 0, 'h'}, 00051 {"version", 0, 0, 'V'}, 00052 {"quiet", 0, 0, 'q'}, 00053 {"verbose", 0, 0, 'v'}, 00054 {"config-file", 0, 0, 'r'}, 00055 {"output", 0, 0, 'o'}, 00056 {0, 0, 0, 0} 00057 }; 00058 #endif 00059 00063 SCM s_pre_load_expr = SCM_EOL; 00064 00069 SCM s_post_load_expr = SCM_EOL; 00070 00078 static void 00079 usage(char *cmd) 00080 { 00081 printf(_( 00082 "Usage: %s [OPTION ...] [--] [FILE ...]\n" 00083 "\n" 00084 "Interactively edit gEDA schematics or symbols. If one or more FILEs\n" 00085 "are specified, open them for editing; otherwise, create a new, empty\n" 00086 "schematic.\n" 00087 "\n" 00088 "Options:\n" 00089 " -q, --quiet Quiet mode.\n" 00090 " -v, --verbose Verbose mode.\n" 00091 " -r, --config-file=FILE Additional configuration file to load.\n" 00092 " -L DIR Add DIR to Scheme search path.\n" 00093 " -c EXPR Scheme expression to run at startup.\n" 00094 " -s FILE Scheme script to run at startup.\n" 00095 " -o, --output=FILE Output filename (for printing).\n" 00096 " -p Automatically place the window.\n" 00097 " -V, --version Show version information.\n" 00098 " -h, --help Help; this message.\n" 00099 " -- Treat all remaining arguments as filenames.\n" 00100 "\n" 00101 "Report bugs at <https://bugs.launchpad.net/geda>\n" 00102 "gEDA/gaf homepage: <http://www.geda-project.org/>\n"), 00103 cmd); 00104 exit(0); 00105 } 00106 00112 static void 00113 version () 00114 { 00115 printf(_( 00116 "gEDA %s (g%.7s)\n" 00117 "Copyright (C) 1998-2012 gEDA developers\n" 00118 "This is free software, and you are welcome to redistribute it under\n" 00119 "certain conditions. For details, see the file `COPYING', which is\n" 00120 "included in the gEDA distribution.\n" 00121 "There is NO WARRANTY, to the extent permitted by law.\n"), 00122 PACKAGE_DOTTED_VERSION, PACKAGE_GIT_COMMIT); 00123 exit (0); 00124 } 00125 00135 int 00136 parse_commandline(int argc, char *argv[]) 00137 { 00138 int ch; 00139 SCM sym_cons = scm_from_utf8_symbol ("cons"); 00140 SCM sym_set_x = scm_from_utf8_symbol ("set!"); 00141 SCM sym_load_path = scm_from_utf8_symbol ("%load-path"); 00142 SCM sym_begin = scm_from_utf8_symbol ("begin"); 00143 SCM sym_load = scm_from_utf8_symbol ("load"); 00144 SCM sym_eval_string = scm_from_utf8_symbol ("eval-string"); 00145 00146 #ifdef HAVE_GETOPT_LONG 00147 while ((ch = getopt_long (argc, argv, GETOPT_OPTIONS, long_options, NULL)) != -1) { 00148 #else 00149 while ((ch = getopt (argc, argv, GETOPT_OPTIONS)) != -1) { 00150 #endif 00151 switch (ch) { 00152 case 'v': 00153 verbose_mode = TRUE; 00154 break; 00155 00156 case 'q': 00157 quiet_mode = TRUE; 00158 break; 00159 00160 case 'r': 00161 rc_filename = g_strdup (optarg); 00162 break; 00163 00164 case 's': 00165 /* Argument is filename of a Scheme script to be run on gschem 00166 * load. Add the necessary expression to be evaluated after 00167 * loading. */ 00168 s_post_load_expr = 00169 scm_cons (scm_list_2 (sym_load, 00170 scm_from_locale_string (optarg)), 00171 s_post_load_expr); 00172 break; 00173 00174 case 'c': 00175 /* Argument is a Scheme expression to be evaluated on gschem 00176 * load. Add the necessary expression to be evaluated after 00177 * loading. */ 00178 s_post_load_expr = 00179 scm_cons (scm_list_2 (sym_eval_string, 00180 scm_from_locale_string (optarg)), 00181 s_post_load_expr); 00182 break; 00183 00184 case 'o': 00185 output_filename = g_strdup (optarg); 00186 break; 00187 00188 case 'p': 00189 auto_place_mode = TRUE; 00190 break; 00191 00192 case 'L': 00193 /* Argument is a directory to add to the Scheme load path. 00194 * Add the necessary expression to be evaluated before rc file 00195 * loading. */ 00196 s_pre_load_expr = 00197 scm_cons (scm_list_3 (sym_set_x, 00198 sym_load_path, 00199 scm_list_3 (sym_cons, 00200 scm_from_locale_string (optarg), 00201 sym_load_path)), 00202 s_pre_load_expr); 00203 break; 00204 00205 case 'h': 00206 usage(argv[0]); 00207 break; 00208 00209 case 'V': 00210 version (); 00211 break; 00212 00213 case '?': 00214 #ifndef HAVE_GETOPT_LONG 00215 if ((optopt != ':') && (strchr (GETOPT_OPTIONS, optopt) != NULL)) { 00216 fprintf (stderr, 00217 "ERROR: -%c option requires an argument.\n\n", 00218 optopt); 00219 } else if (isprint (optopt)) { 00220 fprintf (stderr, "ERROR: Unknown option -%c.\n\n", optopt); 00221 } else { 00222 fprintf (stderr, "ERROR: Unknown option character `\\x%x'.\n\n", 00223 optopt); 00224 } 00225 #endif 00226 fprintf (stderr, "\nRun `%s --help' for more information.\n", argv[0]); 00227 exit (1); 00228 break; 00229 default: 00230 g_assert_not_reached (); 00231 } 00232 } 00233 00234 if (quiet_mode) { 00235 verbose_mode = FALSE; 00236 } 00237 00238 /* Make sure Scheme expressions can be passed straight to eval */ 00239 s_pre_load_expr = scm_cons (sym_begin, 00240 scm_reverse_x (s_pre_load_expr, SCM_UNDEFINED)); 00241 scm_gc_protect_object (s_pre_load_expr); 00242 s_post_load_expr = scm_cons (sym_begin, 00243 scm_reverse_x (s_post_load_expr, SCM_UNDEFINED)); 00244 scm_gc_protect_object (s_post_load_expr); 00245 return(optind); 00246 }