1 /* -*-c-*- -------------- mixguile.c :
2  * Implementation of the functions declared in mixguile.h
3  * ------------------------------------------------------------------
4  * Copyright (C) 2001, 2002, 2006, 2007, 2009 Free Software Foundation, Inc.
5  *
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 3 of the License, or
9  * (at your option) any later version.
10  *
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.
15  *
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.
19  *
20  */
21 
22 #include <unistd.h>
23 
24 #include <mixlib/mix_config.h>
25 #include "mixguile_cmd_dispatcher.h"
26 #include "mixguile.h"
27 
28 static mixguile_cmd_dispatcher_t *dispatcher_ = NULL;
29 static mix_vm_cmd_dispatcher_t *vm_dispatcher_ = NULL;
30 static main_func_t main_fun_;
31 static gboolean init_file_;
32 
33 /* do local initialisation and enter the user provided main */
34 
35 static void
real_main_(void * closure,int argc,char * argv[])36 real_main_ (void *closure, int argc, char *argv[])
37 {
38   if (vm_dispatcher_)
39     {
40       mixguile_set_cmd_dispatcher (vm_dispatcher_);
41       mixguile_load_bootstrap (init_file_);
42     }
43   (*main_fun_)(NULL, argc, argv);
44 }
45 
46 /*
47   initialise the guile command dispatcher and enter the provided
48   main function.
49 */
50 void
mixguile_init(int argc,char * argv[],gboolean initfile,main_func_t main_fun,mix_vm_cmd_dispatcher_t * dis)51 mixguile_init (int argc, char *argv[], gboolean initfile,
52 	       main_func_t main_fun,
53 	       mix_vm_cmd_dispatcher_t *dis)
54 {
55   main_fun_ = main_fun;
56   vm_dispatcher_ = dis;
57   init_file_ = initfile;
58   scm_boot_guile (argc, argv, real_main_, 0);
59 }
60 
61 /* load bootstrap file */
62 void
mixguile_load_bootstrap(gboolean loadlocal)63 mixguile_load_bootstrap (gboolean loadlocal)
64 {
65   const gchar *scmfile = SCM_FILE;
66   gchar *lscmfile = g_strconcat (g_get_home_dir (), G_DIR_SEPARATOR_S,
67 				 MIX_CONFIG_DIR, G_DIR_SEPARATOR_S,
68 				 LOCAL_SCM_FILE, NULL);
69 
70   if (access (scmfile, R_OK) && access ((scmfile = LOCAL_SCM_FILE), R_OK))
71     {
72       g_warning (_("mixguile bootstrap file %s not found\n"), SCM_FILE);
73       scmfile = NULL;
74     }
75   else
76     mixguile_interpret_file (scmfile);
77 
78   if (loadlocal && !access (lscmfile, R_OK))
79     {
80       mixguile_interpret_file (lscmfile);
81     }
82 
83   g_free (lscmfile);
84 }
85 
86 /* enter the guile repl */
87 void
mixguile_enter_repl(void * closure,int argc,char * argv[])88 mixguile_enter_repl (void *closure, int argc, char *argv[])
89 {
90   scm_shell (argc, argv);
91 }
92 
93 /* set the command dispatcher */
94 void
mixguile_set_cmd_dispatcher(mix_vm_cmd_dispatcher_t * dis)95 mixguile_set_cmd_dispatcher (mix_vm_cmd_dispatcher_t *dis)
96 {
97   g_return_if_fail (dis != NULL);
98   if (dispatcher_) mixguile_cmd_dispatcher_delete (dispatcher_);
99   vm_dispatcher_ = dis;
100   dispatcher_ = mixguile_cmd_dispatcher_new (dis);
101   g_assert (dispatcher_);
102 }
103 
104 /* access the mixguile comand dispatcher */
105 mix_vm_cmd_dispatcher_t *
mixguile_get_cmd_dispatcher(void)106 mixguile_get_cmd_dispatcher (void)
107 {
108   return mixguile_cmd_dispatcher_get_vm_dispatcher (dispatcher_);
109 }
110 
111 /* execute a string or file using the guile interpreter */
112 void
mixguile_interpret_file(const gchar * path)113 mixguile_interpret_file (const gchar *path)
114 {
115   mixguile_cmd_dispatcher_interpret_file (dispatcher_, path);
116 }
117 
118 void
mixguile_interpret_command(const gchar * command)119 mixguile_interpret_command (const gchar *command)
120 {
121   mixguile_cmd_dispatcher_interpret_command (dispatcher_, command);
122 }
123