Added --pid option

master
Yevgen Muntyan 2006-08-18 02:24:18 -05:00
parent c781cd9635
commit 7828103b84
9 changed files with 246 additions and 100 deletions

View File

@ -26,7 +26,7 @@
</ignoreparts>
<projectdirectory>.</projectdirectory>
<absoluteprojectpath>false</absoluteprojectpath>
<description/>
<description></description>
<secondaryLanguages>
<language>C</language>
</secondaryLanguages>
@ -41,7 +41,7 @@
<mainprogram>tests/medit</mainprogram>
<directoryradio>executable</directoryradio>
<customdirectory>/</customdirectory>
<programargs>-n --mode=project</programargs>
<programargs>--pidpid=1000</programargs>
<terminal>false</terminal>
<autocompile>false</autocompile>
<envvars>
@ -216,7 +216,7 @@
<abortonerror>true</abortonerror>
<numberofjobs>3</numberofjobs>
<dontact>false</dontact>
<makebin/>
<makebin></makebin>
<prio>0</prio>
<runmultiplejobs>false</runmultiplejobs>
</make>
@ -224,14 +224,14 @@
<kdevdebugger>
<general>
<dbgshell>libtool</dbgshell>
<programargs>--g-fatal-warnings -n</programargs>
<programargs>--pidpid=1000</programargs>
<breakonloadinglibs>true</breakonloadinglibs>
<separatetty>false</separatetty>
<floatingtoolbar>true</floatingtoolbar>
<gdbpath/>
<configGdbScript/>
<runShellScript/>
<runGdbScript/>
<gdbpath></gdbpath>
<configGdbScript></configGdbScript>
<runShellScript></runShellScript>
<runGdbScript></runGdbScript>
</general>
<display>
<staticmembers>true</staticmembers>
@ -293,16 +293,16 @@
</kdevdoctreeview>
<kdevfilecreate>
<filetypes>
<type icon="source" ext="g" create="template" name="GAP source" >
<type icon="source" ext="g" name="GAP source" create="template" >
<descr>A new empty GAP source file</descr>
</type>
<type icon="source_cpp" ext="cpp" create="template" name="C++ Source" >
<type icon="source_cpp" ext="cpp" name="C++ Source" create="template" >
<descr>A new empty C++ file.</descr>
</type>
<type icon="source_h" ext="h" create="template" name="C/C++ Header" >
<type icon="source_h" ext="h" name="C/C++ Header" create="template" >
<descr>A new empty header file for C/C++.</descr>
</type>
<type icon="source_c" ext="c" create="template" name="C Source" >
<type icon="source_c" ext="c" name="C Source" create="template" >
<descr>A new empty C file.</descr>
</type>
</filetypes>
@ -329,7 +329,7 @@
</codecompletion>
<references/>
<creategettersetter>
<prefixGet/>
<prefixGet></prefixGet>
<prefixSet>set</prefixSet>
<prefixVariable>m_,_</prefixVariable>
<parameterName>theValue</parameterName>

View File

@ -897,19 +897,21 @@ start_input (MooApp *app)
gboolean
moo_app_send_msg (MooApp *app,
const char *pid,
const char *data,
int len)
{
g_return_val_if_fail (MOO_IS_APP (app), FALSE);
g_return_val_if_fail (data != NULL, FALSE);
return _moo_app_input_send_msg (app->priv->info->short_name, data, len);
return _moo_app_input_send_msg (app->priv->info->short_name, pid, data, len);
}
gboolean
moo_app_send_files (MooApp *app,
char **files,
guint32 stamp)
moo_app_send_files (MooApp *app,
char **files,
guint32 stamp,
const char *pid)
{
gboolean result;
GString *msg;
@ -951,7 +953,7 @@ moo_app_send_files (MooApp *app,
g_free (uri);
}
result = moo_app_send_msg (app, msg->str, msg->len + 1);
result = moo_app_send_msg (app, pid, msg->str, msg->len + 1);
g_string_free (msg, TRUE);
return result;

View File

@ -106,11 +106,13 @@ void moo_app_set_ui_xml (MooApp *app,
char *moo_app_tempnam (MooApp *app);
gboolean moo_app_send_msg (MooApp *app,
const char *pid,
const char *data,
int len);
gboolean moo_app_send_files (MooApp *app,
char **files,
guint32 stamp);
guint32 stamp,
const char *pid);
void moo_app_reload_python_plugins (void);

View File

@ -273,6 +273,7 @@ _moo_app_input_start (MooAppInput *ch)
gboolean
_moo_app_input_send_msg (G_GNUC_UNUSED const char *pipe_basename,
G_GNUC_UNUSED const char *pid,
G_GNUC_UNUSED const char *data,
G_GNUC_UNUSED gssize len)
{
@ -512,8 +513,64 @@ _moo_app_input_start (MooAppInput *ch)
}
static gboolean
try_send (const char *tmpdir_name,
const char *prefix,
const char *pid_string,
const char *data,
gssize data_len)
{
GPid pid;
char *filename = NULL;
GIOChannel *chan = NULL;
GIOStatus status;
gboolean result = FALSE;
if (!pid_string[0])
goto out;
errno = 0;
pid = strtol (pid_string, NULL, 10);
if (errno)
goto out;
filename = g_strdup_printf ("%s/%s%s", tmpdir_name, prefix, pid_string);
if (kill (pid, 0) != 0)
{
unlink (filename);
goto out;
}
chan = g_io_channel_new_file (filename, "w", NULL);
if (!chan)
goto out;
g_io_channel_set_encoding (chan, NULL, NULL);
status = g_io_channel_set_flags (chan, G_IO_FLAG_NONBLOCK, NULL);
if (status != G_IO_STATUS_NORMAL)
goto out;
status = g_io_channel_write_chars (chan, data, data_len, NULL, NULL);
if (status != G_IO_STATUS_NORMAL)
goto out;
result = TRUE;
out:
if (chan)
g_io_channel_unref (chan);
g_free (filename);
return result;
}
gboolean
_moo_app_input_send_msg (const char *pipe_basename,
const char *pid,
const char *data,
gssize data_len)
{
@ -526,75 +583,33 @@ _moo_app_input_send_msg (const char *pipe_basename,
g_return_val_if_fail (pipe_basename != NULL, FALSE);
g_return_val_if_fail (data != NULL, FALSE);
prefix = g_strdup_printf (NAME_PREFIX, pipe_basename);
prefix_len = strlen (prefix);
tmpdir_name = g_get_tmp_dir ();
if (pid)
{
success = try_send (tmpdir_name, prefix, pid, data, data_len);
goto out;
}
tmpdir = g_dir_open (tmpdir_name, 0, NULL);
if (!tmpdir)
return FALSE;
prefix = g_strdup_printf (NAME_PREFIX, pipe_basename);
prefix_len = strlen (prefix);
goto out;
while ((entry = g_dir_read_name (tmpdir)))
{
const char *pid_string;
GPid pid;
char *filename = NULL;
GIOChannel *chan;
GIOStatus status;
if (strncmp (entry, prefix, prefix_len))
goto cont;
pid_string = entry + prefix_len;
if (!pid_string[0])
goto cont;
errno = 0;
pid = strtol (entry + prefix_len, NULL, 10);
if (errno)
goto cont;
filename = g_build_filename (tmpdir_name, entry, NULL);
if (kill (pid, 0))
if (!strncmp (entry, prefix, prefix_len))
{
/* XXX unlink should file if it's not our file, but still.. */
unlink (filename);
goto cont;
const char *pid_string = entry + prefix_len;
if (try_send (tmpdir_name, prefix, pid_string, data, data_len))
{
success = TRUE;
goto out;
}
}
chan = g_io_channel_new_file (filename, "w", NULL);
if (!chan)
goto cont;
g_io_channel_set_encoding (chan, NULL, NULL);
status = g_io_channel_set_flags (chan, G_IO_FLAG_NONBLOCK, NULL);
if (status != G_IO_STATUS_NORMAL)
{
g_io_channel_unref (chan);
goto cont;
}
status = g_io_channel_write_chars (chan, data, data_len, NULL, NULL);
if (status != G_IO_STATUS_NORMAL)
{
g_io_channel_unref (chan);
goto cont;
}
g_free (filename);
g_io_channel_unref (chan);
success = TRUE;
goto out;
cont:
g_free (filename);
}
out:

View File

@ -80,6 +80,7 @@ void _moo_app_input_shutdown (MooAppInput *ch);
const char *_moo_app_input_get_name (MooAppInput *ch);
gboolean _moo_app_input_send_msg (const char *pipe_basename,
const char *pid,
const char *data,
gssize len);

View File

@ -131,6 +131,7 @@
(c-name "moo_app_send_msg")
(return-type "gboolean")
(parameters
'("const-char*" "pid" (null-ok))
'("const-char*" "data")
'("int" "len")
)
@ -143,5 +144,6 @@
(parameters
'("strv" "files")
'("guint32" "stamp" (default "0"))
'("const-char*" "pid" (null-ok) (default "NULL"))
)
)

View File

@ -5,10 +5,12 @@ moo/mooapp/mooappabout.c
moo/mooapp/mooapp.c
moo/mooapp/glade/mooappabout.glade
moo/mooedit/moocommand-script.c
moo/mooedit/mooeditwindow.c
moo/mooedit/moousertools.c
moo/mooedit/moousertools-prefs.c
moo/mooedit/context.xml
moo/mooedit/menu.xml
moo/mooedit/tools.xml
moo/mooedit/glade/mooeditprefs.glade
moo/mooedit/glade/mooeditprogress.glade
moo/mooedit/glade/mooeditsavemult.glade
@ -18,6 +20,7 @@ moo/mooedit/glade/mootextfind.glade
moo/mooedit/glade/mootextgotoline.glade
moo/mooedit/glade/quicksearch.glade
moo/mooedit/glade/statusbar.glade
moo/mooedit/glade/mooedittools.glade
moo/mooedit/plugins/completion/completion-plugin.c
moo/mooedit/plugins/fileselector/moofileselector-prefs.glade
moo/mooedit/plugins/fileselector/moofileselector.glade

View File

@ -69,6 +69,9 @@ int _medit_parse_options (const char *const program_name,
#define STR_HELP_NEW_APP "\
-n, --new-app Run new instance of application\n"
#define STR_HELP_PID "\
--pid=PID Use existing instance with process id PID\n"
#define STR_HELP_MODE "\
-m, --mode=[simple|project] Use specified mode\n"
@ -89,6 +92,7 @@ int _medit_parse_options (const char *const program_name,
#define STR_HELP "\
-n, --new-app Run new instance of application\n\
--pid=PID Use existing instance with process id PID\n\
-m, --mode=[simple|project] Use specified mode\n\
-p, --project=PROJECT Open project file PROJECT\n\
-l, --line=LINE Open file and position cursor on line LINE\n\
@ -99,6 +103,9 @@ int _medit_parse_options (const char *const program_name,
/* Set to 1 if option --new-app (-n) has been specified. */
char _medit_opt_new_app;
/* Set to 1 if option --pid has been specified. */
char _medit_opt_pid;
/* Set to 1 if option --mode (-m) has been specified. */
char _medit_opt_mode;
@ -117,10 +124,13 @@ char _medit_opt_version;
/* Set to 1 if option --help (-h) has been specified. */
char _medit_opt_help;
/* Argument to option --pid. */
const char *_medit_arg_pid;
/* Argument to option --mode (-m). */
const char *_medit_arg_mode;
/* Argument to option --project (-p), or a null pointer if no argument. */
/* Argument to option --project (-p). */
const char *_medit_arg_project;
/* Argument to option --line (-l). */
@ -134,18 +144,22 @@ const char *_medit_arg_log;
int _medit_parse_options (const char *const program_name, const int argc, char **const argv)
{
static const char *const optstr__new_app = "new-app";
static const char *const optstr__pid = "pid";
static const char *const optstr__mode = "mode";
static const char *const optstr__project = "project";
static const char *const optstr__line = "line";
static const char *const optstr__version = "version";
static const char *const optstr__help = "help";
int i = 0;
_medit_opt_new_app = 0;
_medit_opt_pid = 0;
_medit_opt_mode = 0;
_medit_opt_project = 0;
_medit_opt_line = 0;
_medit_opt_log = 0;
_medit_opt_version = 0;
_medit_opt_help = 0;
_medit_arg_pid = 0;
_medit_arg_mode = 0;
_medit_arg_project = 0;
_medit_arg_line = 0;
@ -239,9 +253,35 @@ int _medit_parse_options (const char *const program_name, const int argc, char *
}
goto error_unknown_long_opt;
case 'p':
if (strncmp (option + 1, "roject", option_len - 1) == 0)
if (strncmp (option + 1, optstr__pid + 1, option_len - 1) == 0)
{
_medit_arg_project = argument;
if (option_len <= 1)
goto error_long_opt_ambiguous;
if (argument != 0)
_medit_arg_pid = argument;
else if (++i < argc)
_medit_arg_pid = argv [i];
else
{
option = optstr__pid;
goto error_missing_arg_long;
}
_medit_opt_pid = 1;
break;
}
if (strncmp (option + 1, optstr__project + 1, option_len - 1) == 0)
{
if (option_len <= 1)
goto error_long_opt_ambiguous;
if (argument != 0)
_medit_arg_project = argument;
else if (++i < argc)
_medit_arg_project = argv [i];
else
{
option = optstr__project;
goto error_missing_arg_long;
}
_medit_opt_project = 1;
break;
}
@ -305,12 +345,12 @@ int _medit_parse_options (const char *const program_name, const int argc, char *
break;
case 'p':
if (option [1] != '\0')
{
_medit_arg_project = option + 1;
option = "\0";
}
else if (++i < argc)
_medit_arg_project = argv [i];
else
_medit_arg_project = 0;
goto error_missing_arg_short;
option = "\0";
_medit_opt_project = 1;
break;
default:
@ -357,6 +397,33 @@ check_args (int opt_remain)
exit (1);
}
if (_medit_opt_pid)
{
if (_medit_opt_mode)
{
g_print ("--mode can't be used together with --pid\n");
exit (1);
}
if (_medit_opt_project)
{
g_print ("--project can't be used together with --pid\n");
exit (1);
}
if (_medit_opt_new_app)
{
g_print ("--new-app can't be used together with --pid\n");
exit (1);
}
if (!_medit_arg_pid || !_medit_arg_pid[0])
{
usage ();
exit (1);
}
}
if (_medit_opt_mode)
{
if (!_medit_arg_mode ||
@ -396,7 +463,7 @@ main (int argc, char *argv[])
gboolean run_input = TRUE;
AppMode mode = MODE_SIMPLE;
guint32 stamp;
int line = 0;
int line = -1;
gtk_init (&argc, &argv);
stamp = TIMESTAMP;
@ -443,7 +510,23 @@ main (int argc, char *argv[])
"credits", THANKS,
NULL);
if ((!new_instance && moo_app_send_files (app, files, stamp)) ||
if (_medit_arg_line)
line = strtol (_medit_arg_line, NULL, 10);
if (_medit_opt_pid)
{
if (moo_app_send_files (app, files, stamp, _medit_arg_pid))
{
exit (0);
}
else
{
g_print ("Could not send files to pid %s\n", _medit_arg_pid);
exit (1);
}
}
if ((!new_instance && moo_app_send_files (app, files, stamp, NULL)) ||
!moo_app_init (app))
{
gdk_notify_startup_complete ();
@ -477,16 +560,13 @@ main (int argc, char *argv[])
editor = moo_app_get_editor (app);
window = moo_editor_new_window (editor);
if (_medit_arg_line)
line = strtol (_medit_arg_line, NULL, 10);
if (files && *files)
{
char **p;
for (p = files; p && *p; ++p)
{
if (p == files && _medit_arg_line)
if (p == files && line >= 0)
moo_editor_open_file_line (editor, *p, line, window);
else
moo_editor_new_file (editor, window, NULL, *p, NULL);

View File

@ -43,8 +43,9 @@ int _medit_parse_options (const char *const program_name,
*/
%%
n new-app "Run new instance of application"
pid "=PID Use existing instance with process id PID" reqarg
m mode "=[simple|project] Use specified mode" reqarg
p project "=PROJECT Open project file PROJECT" optarg
p project "=PROJECT Open project file PROJECT" reqarg
l line "=LINE Open file and position cursor on line LINE" reqarg
log "[=FILE] Show debug output or write it to FILE" optarg
version "Display version information and exit" return
@ -83,6 +84,33 @@ check_args (int opt_remain)
exit (1);
}
if (_medit_opt_pid)
{
if (_medit_opt_mode)
{
g_print ("--mode can't be used together with --pid\n");
exit (1);
}
if (_medit_opt_project)
{
g_print ("--project can't be used together with --pid\n");
exit (1);
}
if (_medit_opt_new_app)
{
g_print ("--new-app can't be used together with --pid\n");
exit (1);
}
if (!_medit_arg_pid || !_medit_arg_pid[0])
{
usage ();
exit (1);
}
}
if (_medit_opt_mode)
{
if (!_medit_arg_mode ||
@ -122,7 +150,7 @@ main (int argc, char *argv[])
gboolean run_input = TRUE;
AppMode mode = MODE_SIMPLE;
guint32 stamp;
int line = 0;
int line = -1;
gtk_init (&argc, &argv);
stamp = TIMESTAMP;
@ -169,7 +197,23 @@ main (int argc, char *argv[])
"credits", THANKS,
NULL);
if ((!new_instance && moo_app_send_files (app, files, stamp)) ||
if (_medit_arg_line)
line = strtol (_medit_arg_line, NULL, 10);
if (_medit_opt_pid)
{
if (moo_app_send_files (app, files, stamp, _medit_arg_pid))
{
exit (0);
}
else
{
g_print ("Could not send files to pid %s\n", _medit_arg_pid);
exit (1);
}
}
if ((!new_instance && moo_app_send_files (app, files, stamp, NULL)) ||
!moo_app_init (app))
{
gdk_notify_startup_complete ();
@ -203,16 +247,13 @@ main (int argc, char *argv[])
editor = moo_app_get_editor (app);
window = moo_editor_new_window (editor);
if (_medit_arg_line)
line = strtol (_medit_arg_line, NULL, 10);
if (files && *files)
{
char **p;
for (p = files; p && *p; ++p)
{
if (p == files && _medit_arg_line)
if (p == files && line >= 0)
moo_editor_open_file_line (editor, *p, line, window);
else
moo_editor_new_file (editor, window, NULL, *p, NULL);