summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 504a00ad2672d2cdc7a68cd8f65c59ef3ac09549 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* -*- mode: C; indent-tabs-mode: t; tab-width: 8; c-basic-offset: 2; -*- */

/*
 * This file is part of Seed, the GObject Introspection<->Javascript bindings.
 *
 * Seed is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2 of
 * the License, or (at your option) any later version.
 * Seed is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with Seed.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) Robert Carr 2009 <carrr@rpi.edu>
 */

#include <stdlib.h>
#include <glib.h>
#include <glib-object.h>
#include "../libseed/seed.h"
#include "../libseed/seed-debug.h"
#include <girepository.h>
#include "config.h"

#define DEFAULT_PATH "."

SeedEngine *eng;
gboolean seed_interpreter_arg_print_version;
gchar *seed_interpreter_arg_exec_string;

gboolean seed_interpreter_parse_args (int *argc, char ***argv);

static void
seed_repl ()
{
  SeedScript *script;
  SeedException e;

  script = seed_make_script (eng->context, "repl = imports.repl", NULL, 0);

  if ((e = seed_script_exception (script)))
    {
      g_critical ("%s", seed_exception_to_string (eng->context, e));
      exit (EXIT_FAILURE);
    }

  seed_evaluate (eng->context, script, 0);

  if ((e = seed_script_exception (script)))
    {
      g_critical ("%s", seed_exception_to_string (eng->context, e));
      exit (EXIT_FAILURE);
    }

  g_free (script);
}

static void
seed_exec (gchar * filename)
{
  SeedObject global;
  SeedScript *script;
  SeedException e;
  gchar *buffer;

  g_file_get_contents (filename, &buffer, 0, 0);

  if (!buffer)
    {
      g_critical ("File %s not found!", filename);
      exit (EXIT_FAILURE);
    }

  if (*buffer == '#')
    {
      while (*buffer != '\n')
	buffer++;
      buffer++;
    }

  script = seed_make_script (eng->context, buffer, filename, 1);

  if ((e = seed_script_exception (script)))
    {
      g_critical ("%s", seed_exception_to_string (eng->context, e));
      exit (EXIT_FAILURE);
    }

  global = seed_context_get_global_object (eng->context);
  seed_importer_add_global (global, filename);

  seed_evaluate (eng->context, script, 0);
  if ((e = seed_script_exception (script)))
    {
      g_critical ("%s", seed_exception_to_string (eng->context, e));
      exit (EXIT_FAILURE);
    }

  g_free (script);
}

static void
seed_exec_str ()
{
  SeedException e = NULL;
  SeedValue val;
  gchar *val_str;

  val =
    seed_simple_evaluate (eng->context, seed_interpreter_arg_exec_string, &e);

  if (e)
    {
      g_critical ("%s", seed_exception_to_string (eng->context, e));
      exit (EXIT_FAILURE);
    }
  else
    {
      val_str = seed_value_to_string (eng->context, val, &e);
      if (e)
	{
	  g_critical ("%s", seed_exception_to_string (eng->context, e));
	  exit (EXIT_FAILURE);
	}

      g_print ("%s\n", val_str);
      g_free (seed_interpreter_arg_exec_string);
      g_free (val_str);

      exit (EXIT_SUCCESS);
    }

}

gint
main (gint argc, gchar ** argv)
{
  g_set_prgname ("seed");
  g_thread_init (NULL);

  seed_interpreter_parse_args (&argc, &argv);

  if (seed_interpreter_arg_print_version)
    {
      g_print ("%s\n", "Seed " VERSION);
      exit (EXIT_SUCCESS);
    }

  eng = seed_init (&argc, &argv);

  seed_engine_set_search_path (eng, DEFAULT_PATH);

  if (seed_interpreter_arg_exec_string)
    seed_exec_str ();
  else if (argc == 1)
    seed_repl ();
  else
    seed_exec (argv[1]);

  return EXIT_SUCCESS;
}