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