svn update: 48958 (latest:48959)
[framework/uifw/ecore.git] / src / lib / ecore / ecore_app.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4
5 #ifdef HAVE_CONFIG_H
6 # include <config.h>
7 #endif
8
9 #include <stdlib.h>
10
11 #ifndef _MSC_VER
12 # include <unistd.h>
13 #else
14 # include <process.h>
15 #endif
16
17 #ifdef HAVE_EVIL
18 # include <Evil.h>
19 #endif
20
21 #include "Ecore.h"
22 #include "ecore_private.h"
23
24 static int app_argc = 0;
25 static char **app_argv = NULL;
26
27 /**
28  * Set up the programs command-line arguments.
29  * @param argc The same as passed as argc to the programs main() function
30  * @param argv The same as passed as argv to the programs main() function
31  *
32  * A call to this function will store the programs command-line arguments
33  * for later use by ecore_app_restart() or ecore_app_args_get().
34  */
35 EAPI void
36 ecore_app_args_set(int argc, const char **argv)
37 {
38    if ((argc < 1) ||
39        (!argv)) return;
40    app_argc = argc;
41    app_argv = (char **)argv;
42 }
43
44 /**
45  * Return the programs stored command-line arguments.
46  * @param argc A pointer to the return value to hold argc
47  * @param argv A pointer to the return value to hold argv
48  *
49  * When called, this funciton returns the arguments for the program stored by
50  * ecore_app_args_set(). The integer pointed to by @p argc will be filled, if
51  * the pointer is not NULL, and the string array pointer @p argv will be filled
52  * also if the pointer is not NULL. The values they are filled with will be the
53  * same set by ecore_app_args_set().
54  */
55 EAPI void
56 ecore_app_args_get(int *argc, char ***argv)
57 {
58    if (argc) *argc = app_argc;
59    if (argv) *argv = app_argv;
60 }
61
62 /**
63  * Restart the program executable with the command-line arguments stored.
64  *
65  * This function will restart & re-execute this program in place of itself
66  * using the command-line arguments stored by ecore_app_args_set(). This is
67  * an easy way for a program to restart itself for cleanup purposes,
68  * configuration reasons or in the event of a crash.
69  */
70 EAPI void
71 ecore_app_restart(void)
72 {
73    char *args[4096];
74    int i;
75
76    if ((app_argc < 1) || (!app_argv)) return;
77    if (app_argc >= 4096) return;
78    for (i = 0; i < app_argc; i++) args[i] = app_argv[i];
79    args[i] = NULL;
80    execvp(app_argv[0], args);
81 }