2010-10-31 15:28:06 -04:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
|
|
|
/*
|
|
|
|
* Based on gjs/console.c from GJS
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 litl, LLC
|
|
|
|
* Copyright (c) 2010 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2011-04-30 10:27:36 -04:00
|
|
|
#include "config.h"
|
|
|
|
|
2010-10-31 15:28:06 -04:00
|
|
|
#include <locale.h>
|
2011-04-30 10:27:36 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-10-31 15:28:06 -04:00
|
|
|
|
2011-04-30 10:27:36 -04:00
|
|
|
#include <clutter/x11/clutter-x11.h>
|
|
|
|
#include <gdk/gdkx.h>
|
2011-05-18 14:52:27 -04:00
|
|
|
#include <girepository.h>
|
2010-10-31 15:28:06 -04:00
|
|
|
#include <gjs/gjs.h>
|
2011-04-30 10:27:36 -04:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
#include "shell-global.h"
|
|
|
|
#include "shell-global-private.h"
|
2010-10-31 15:28:06 -04:00
|
|
|
|
|
|
|
static char *command = NULL;
|
|
|
|
|
|
|
|
static GOptionEntry entries[] = {
|
|
|
|
{ "command", 'c', 0, G_OPTION_ARG_STRING, &command, "Program passed in as a string", "COMMAND" },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
GOptionContext *context;
|
|
|
|
GError *error = NULL;
|
2011-04-30 10:27:36 -04:00
|
|
|
ShellGlobal *global;
|
2010-10-31 15:28:06 -04:00
|
|
|
GjsContext *js_context;
|
|
|
|
char *script;
|
|
|
|
const char *filename;
|
|
|
|
char *title;
|
|
|
|
gsize len;
|
|
|
|
int code;
|
|
|
|
|
2015-10-27 15:51:13 -04:00
|
|
|
gdk_set_allowed_backends("x11");
|
|
|
|
|
2010-10-31 15:28:06 -04:00
|
|
|
gtk_init (&argc, &argv);
|
|
|
|
|
|
|
|
clutter_x11_set_display (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()));
|
|
|
|
|
2011-03-13 14:16:54 -04:00
|
|
|
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
|
2011-03-13 14:26:01 -04:00
|
|
|
return 1;
|
2010-10-31 15:28:06 -04:00
|
|
|
|
2015-10-27 15:51:13 -04:00
|
|
|
gdk_x11_display_set_window_scale (gdk_display_get_default (), 1);
|
|
|
|
|
2010-10-31 15:28:06 -04:00
|
|
|
context = g_option_context_new (NULL);
|
|
|
|
|
|
|
|
/* pass unknown through to the JS script */
|
|
|
|
g_option_context_set_ignore_unknown_options (context, TRUE);
|
|
|
|
|
|
|
|
g_option_context_add_main_entries (context, entries, NULL);
|
|
|
|
if (!g_option_context_parse (context, &argc, &argv, &error))
|
|
|
|
g_error ("option parsing failed: %s", error->message);
|
|
|
|
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
2011-09-02 12:53:36 -04:00
|
|
|
_shell_global_init (NULL);
|
2011-04-30 10:27:36 -04:00
|
|
|
global = shell_global_get ();
|
|
|
|
js_context = _shell_global_get_gjs_context (global);
|
2010-10-31 15:28:06 -04:00
|
|
|
|
|
|
|
/* prepare command line arguments */
|
|
|
|
if (!gjs_context_define_string_array (js_context, "ARGV",
|
|
|
|
argc - 2, (const char**)argv + 2,
|
|
|
|
&error)) {
|
|
|
|
g_printerr ("Failed to defined ARGV: %s", error->message);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (command != NULL) {
|
|
|
|
script = command;
|
|
|
|
len = strlen (script);
|
|
|
|
filename = "<command line>";
|
|
|
|
} else if (argc <= 1) {
|
|
|
|
script = g_strdup ("const Console = imports.console; Console.interact();");
|
|
|
|
len = strlen (script);
|
|
|
|
filename = "<stdin>";
|
|
|
|
} else /*if (argc >= 2)*/ {
|
|
|
|
error = NULL;
|
|
|
|
if (!g_file_get_contents (argv[1], &script, &len, &error)) {
|
|
|
|
g_printerr ("%s\n", error->message);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
filename = argv[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
title = g_filename_display_basename (filename);
|
2011-12-15 18:14:15 -05:00
|
|
|
g_set_prgname (title);
|
2010-10-31 15:28:06 -04:00
|
|
|
g_free (title);
|
|
|
|
|
|
|
|
/* evaluate the script */
|
|
|
|
error = NULL;
|
|
|
|
if (!gjs_context_eval (js_context, script, len,
|
|
|
|
filename, &code, &error)) {
|
|
|
|
g_free (script);
|
|
|
|
g_printerr ("%s\n", error->message);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2012-06-24 02:25:38 -04:00
|
|
|
gjs_context_gc (js_context);
|
|
|
|
gjs_context_gc (js_context);
|
|
|
|
|
2016-12-09 21:50:39 -05:00
|
|
|
g_object_unref (js_context);
|
2010-10-31 15:28:06 -04:00
|
|
|
g_free (script);
|
|
|
|
exit (code);
|
|
|
|
}
|