Add -e option to repl.
[gnome.seed] / src / main.c
1 /* -*- mode: C; indent-tabs-mode: t; tab-width: 8; c-basic-offset: 2; -*- */
2
3 /*
4  * This file is part of Seed, the GObject Introspection<->Javascript bindings.
5  *
6  * Seed is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation, either version 3 of
9  * the License, or (at your option) any later version.
10  * Seed 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 Lesser General Public License for more details.
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Seed.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Copyright (C) Robert Carr 2009 <carrr@rpi.edu>
18  */
19
20 #include <stdlib.h>
21 #include <glib.h>
22 #include <glib-object.h>
23 #include "../libseed/seed.h"
24 #include "../libseed/seed-debug.h"
25 #include <girepository.h>
26 #include "config.h"
27
28 #define DEFAULT_PATH "."
29
30 SeedEngine *eng;
31 gboolean seed_interpreter_arg_print_version;
32 gchar *seed_interpreter_arg_exec_string;
33
34 gboolean seed_interpreter_parse_args (int *argc, char ***argv);
35
36 static void
37 seed_repl ()
38 {
39   SeedScript *script;
40   SeedException e;
41
42   script = seed_make_script (eng->context, "repl = imports.repl", NULL, 0);
43
44   if ((e = seed_script_exception (script)))
45     {
46       g_critical ("%s", seed_exception_to_string (eng->context, e));
47       exit (EXIT_FAILURE);
48     }
49
50   seed_evaluate (eng->context, script, 0);
51
52   if ((e = seed_script_exception (script)))
53     {
54       g_critical ("%s", seed_exception_to_string (eng->context, e));
55       exit (EXIT_FAILURE);
56     }
57
58   g_free (script);
59 }
60
61 static void
62 seed_exec (gchar * filename)
63 {
64   SeedObject global;
65   SeedScript *script;
66   SeedException e;
67   gchar *buffer;
68
69   g_file_get_contents (filename, &buffer, 0, 0);
70
71   if (!buffer)
72     {
73       g_critical ("File %s not found!", filename);
74       exit (EXIT_FAILURE);
75     }
76
77   if (*buffer == '#')
78     {
79       while (*buffer != '\n')
80         buffer++;
81       buffer++;
82     }
83
84   script = seed_make_script (eng->context, buffer, filename, 1);
85
86   if ((e = seed_script_exception (script)))
87     {
88       g_critical ("%s", seed_exception_to_string (eng->context, e));
89       exit (EXIT_FAILURE);
90     }
91
92   global = seed_context_get_global_object (eng->context);
93   seed_importer_add_global (global, filename);
94
95   seed_evaluate (eng->context, script, 0);
96   if ((e = seed_script_exception (script)))
97     {
98       g_critical ("%s", seed_exception_to_string (eng->context, e));
99       exit (EXIT_FAILURE);
100     }
101
102   g_free (script);
103 }
104
105 static void
106 seed_exec_str ()
107 {
108   SeedException e = NULL;
109   SeedValue val;
110   gchar *val_str;
111
112   val =
113     seed_simple_evaluate (eng->context, seed_interpreter_arg_exec_string, &e);
114
115   if (e)
116     {
117       g_critical ("%s", seed_exception_to_string (eng->context, e));
118       exit (EXIT_FAILURE);
119     }
120   else
121     {
122       val_str = seed_value_to_string (eng->context, val, &e);
123       if (e)
124         {
125           g_critical ("%s", seed_exception_to_string (eng->context, e));
126           exit (EXIT_FAILURE);
127         }
128
129       g_print ("%s\n", val_str);
130       g_free (seed_interpreter_arg_exec_string);
131       g_free (val_str);
132
133       exit (EXIT_SUCCESS);
134     }
135
136 }
137
138 gint
139 main (gint argc, gchar ** argv)
140 {
141   g_set_prgname ("seed");
142   g_thread_init (NULL);
143
144   seed_interpreter_parse_args (&argc, &argv);
145
146   if (seed_interpreter_arg_print_version)
147     {
148       g_print ("%s\n", "Seed " VERSION);
149       exit (EXIT_SUCCESS);
150     }
151
152   eng = seed_init (&argc, &argv);
153
154   seed_engine_set_search_path (eng, DEFAULT_PATH);
155
156   if (seed_interpreter_arg_exec_string)
157     seed_exec_str ();
158   else if (argc == 1)
159     seed_repl ();
160   else
161     seed_exec (argv[1]);
162
163   return EXIT_SUCCESS;
164 }