/*! \brief Load a Scheme file, catching and logging errors. * \par Function Description * Loads \a filename, catching any uncaught errors and logging them. * * \bug Most other functions in the libgeda API return TRUE on success * and FALSE on failure. g_read_file() shouldn't be an exception. * * \param toplevel The TOPLEVEL structure. * \param filename The file name of the Scheme file to load. * \param err Return location for errors, or NULL. * \return TRUE on success, FALSE on failure. */ gboolean g_read_file(TOPLEVEL *toplevel, const gchar *filename, GError **err) { struct g_read_file_data_t data; g_return_val_if_fail ((filename != NULL), FALSE); data.stack = SCM_BOOL_F; data.filename = scm_from_utf8_string (filename); data.err = NULL; scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE); edascm_dynwind_toplevel (toplevel); scm_c_catch (SCM_BOOL_T, (scm_t_catch_body) g_read_file__body, &data, (scm_t_catch_handler) g_read_file__post_handler, &data, (scm_t_catch_handler) g_read_file__pre_handler, &data); scm_dynwind_end (); /* If no error occurred, indicate success. */ if (data.err == NULL) return TRUE; g_propagate_error (err, data.err); return FALSE; }
/*! * \brief Set the #GschemToplevel fluid in the current dynamic context. * \par Function Description * * This function must be used inside a pair of calls to * scm_dynwind_begin() and scm_dynwind_end(). During the dynwind * context, the #GschemToplevel fluid is set to \a w_current. * * \param [in] w_current The new GschemToplevel pointer. */ void g_dynwind_window (GschemToplevel *w_current) { SCM window_s = g_scm_from_window (w_current); scm_dynwind_fluid (scheme_window_fluid, window_s); edascm_dynwind_toplevel (w_current->toplevel); }
static void cmd_export_impl (void *data, int argc, char **argv) { int i; GError *err = NULL; gchar *tmp; const gchar *out_suffix; struct ExportFormat *exporter = NULL; GArray *render_color_map = NULL; gchar *original_cwd = g_get_current_dir (); gtk_init_check (&argc, &argv); scm_init_guile (); libgeda_init (); scm_dynwind_begin (0); toplevel = s_toplevel_new (); edascm_dynwind_toplevel (toplevel); /* Now load rc files, if necessary */ if (getenv ("GAF_INHIBIT_RCFILES") == NULL) { g_rc_parse (toplevel, "gaf export", NULL, NULL); } i_vars_libgeda_set (toplevel); /* Ugh */ /* Parse configuration files */ export_config (); /* Parse command-line arguments */ export_command_line (argc, argv); /* If no format was specified, try and guess from output * filename. */ if (settings.format == NULL) { out_suffix = strrchr (settings.outfile, '.'); if (out_suffix != NULL) { out_suffix++; /* Skip '.' */ } else { fprintf (stderr, _("ERROR: Cannot infer output format from filename '%s'.\n"), settings.outfile); exit (1); } } /* Try and find an exporter function */ tmp = g_utf8_strdown ((settings.format == NULL) ? out_suffix : settings.format, -1); for (i = 0; formats[i].name != NULL; i++) { if (strcmp (tmp, formats[i].alias) == 0) { exporter = &formats[i]; break; } } if (exporter == NULL) { if (settings.format == NULL) { fprintf (stderr, _("ERROR: Cannot find supported format for filename '%s'.\n"), settings.outfile); exit (1); } else { fprintf (stderr, _("ERROR: Unsupported output format '%s'.\n"), settings.format); fprintf (stderr, see_help_msg); exit (1); } } g_free (tmp); /* If more than one schematic/symbol file was specified, check that * exporter supports multipage output. */ if ((settings.infilec > 1) && !(exporter->flags & OUTPUT_MULTIPAGE)) { fprintf (stderr, _("ERROR: Selected output format does not support multipage output\n")); exit (1); } /* Load schematic files */ while (optind < argc) { PAGE *page; tmp = argv[optind++]; page = s_page_new (toplevel, tmp); if (!f_open (toplevel, page, tmp, &err)) { fprintf (stderr, _("ERROR: Failed to load '%s': %s\n"), tmp, err->message); exit (1); } if (g_chdir (original_cwd) != 0) { fprintf (stderr, _("ERROR: Failed to change directory to '%s': %s\n"), original_cwd, g_strerror (errno)); exit (1); } } /* Create renderer */ renderer = eda_renderer_new (NULL, NULL); if (settings.font != NULL) { g_object_set (renderer, "font-name", settings.font, NULL); } /* Make sure libgeda knows how to calculate the bounds of text * taking into account font etc. */ o_text_set_rendered_bounds_func (toplevel, export_text_rendered_bounds, renderer); /* Create color map */ render_color_map = g_array_sized_new (FALSE, FALSE, sizeof(GedaColor), MAX_COLORS); render_color_map = g_array_append_vals (render_color_map, print_colors, MAX_COLORS); if (!settings.color) { /* Create a black and white color map. All non-background colors * are black. */ GedaColor white = {~0, ~0, ~0, ~0, TRUE}; GedaColor black = {0, 0, 0, ~0, TRUE}; for (i = 0; i < MAX_COLORS; i++) { GedaColor *c = &g_array_index (render_color_map, GedaColor, i); if (!c->enabled) continue; if (c->a == 0) { c->enabled = FALSE; continue; } if (i == OUTPUT_BACKGROUND_COLOR) { *c = white; } else { *c = black; } } } eda_renderer_set_color_map (renderer, render_color_map); /* Render */ exporter->func (); scm_dynwind_end (); exit (0); }
static void shell_main (void *data, int argc, char **argv) { SCM setup_lst = SCM_EOL; /* We reverse! this before using it. */ SCM run_lst = SCM_EOL; /* We reverse! this before using it. */ int c; int interactive = 1; int inhibit_rc = 0; int status; TOPLEVEL *toplevel; #include "shell.x" /* Parse command-line arguments */ opterr = 0; while ((c = getopt (argc, argv, GETOPT_OPTIONS)) != -1) { switch (c) { case 's': /* Construct an application of LOAD to the script name */ run_lst = scm_cons (scm_list_2 (sym_load, scm_from_locale_string (optarg)), run_lst); interactive = 0; goto endoptloop; case 'c': /* We need to evaluate an expression */ run_lst = scm_cons (scm_list_2 (sym_eval_string, scm_from_locale_string (optarg)), run_lst); interactive = 0; goto endoptloop; case 'L': /* Add argument to %load-path */ setup_lst = scm_cons (scm_list_3 (sym_set_x, sym_load_path, scm_list_3 (sym_cons, scm_from_locale_string (optarg), sym_load_path)), setup_lst); break; case 'l': /* Same as -s, pretty much */ run_lst = scm_cons (scm_list_2 (sym_load, scm_from_locale_string (optarg)), run_lst); break; case 'q': inhibit_rc = 1; break; case 'h': usage (0); case 'V': version(); case '?': if ((optopt != ':') && (strchr (GETOPT_OPTIONS, optopt) != NULL)) { fprintf (stderr, "ERROR: -%c option requires an argument.\n\n", optopt); usage (1); } else if (isprint (optopt)) { fprintf (stderr, "ERROR: Unknown option -%c\n\n", optopt); usage (1); } else { fprintf (stderr, "ERROR: Unknown option character `\\x%x'.\n\n", optopt); usage (1); } default: g_assert_not_reached (); } } endoptloop: /* Set program arguments visible from Guile */ scm_set_program_arguments (argc - optind, argv + optind, "geda-shell"); /* If interactive mode, load readline and run top REPL. */ if (interactive) { run_lst = scm_cons (scm_list_2 (sym_use_modules, scm_list_2 (sym_ice_9, sym_readline)), run_lst); run_lst = scm_cons (scm_list_1 (sym_activate_readline), run_lst); run_lst = scm_cons (scm_list_1 (sym_top_repl), run_lst); /* Print GPL bumf if necessary */ if (isatty (1) && isatty (0)) { printf ( "gEDA " PACKAGE_GIT_VERSION "\n" "Copyright (C) 1998-2010 gEDA developers\n" "This is free software, and you are welcome to redistribute it under\n" "certain conditions. For details, see the file `COPYING', which is\n" "included in the gEDA distribution.\n" "There is NO WARRANTY, to the extent permitted by law.\n" ); } } else { run_lst = scm_cons (scm_list_1 (sym_quit), run_lst); } /* Reverse lists */ setup_lst = scm_reverse_x (setup_lst, SCM_UNDEFINED); run_lst = scm_reverse_x (run_lst, SCM_UNDEFINED); /* Initialise libgeda */ libgeda_init (); scm_dynwind_begin (0); toplevel = s_toplevel_new (); edascm_dynwind_toplevel (toplevel); /* First run the setup list */ if (setup_lst != SCM_EOL) { setup_lst = scm_cons (sym_begin, setup_lst); scm_eval_x (setup_lst, scm_current_module ()); } /* Now load rc files, if necessary */ if (!inhibit_rc) g_rc_parse (toplevel, argv[0], NULL, NULL); i_vars_libgeda_set (toplevel); /* Ugh */ /* Finally evaluate run list */ run_lst = scm_cons (sym_begin, run_lst); status = scm_exit_status (scm_eval_x (run_lst, scm_current_module ())); exit (status); scm_dynwind_end (); scm_remember_upto_here_2 (setup_lst, run_lst); }