1 |
/* |
2 |
* main.cpp - Wrapper program for standalone cxmon |
3 |
* |
4 |
* cxmon (C) Christian Bauer, Marc Hellwig |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
20 |
|
21 |
#include "sysdeps.h" |
22 |
|
23 |
#include "mon.h" |
24 |
|
25 |
|
26 |
#ifdef __BEOS__ |
27 |
#include <AppKit.h> |
28 |
#include <KernelKit.h> |
29 |
#include <StorageKit.h> |
30 |
#include <stdlib.h> |
31 |
|
32 |
// Detect if program was launched from Shell or Tracker |
33 |
static bool launched_from_tracker(void) |
34 |
{ |
35 |
const char *cmd = getenv("_"); |
36 |
if (cmd == NULL || strlen(cmd) < 7) |
37 |
return false; |
38 |
return !strcmp(cmd + strlen(cmd) - 7 , "Tracker"); |
39 |
} |
40 |
|
41 |
// Open Terminal window with given title for stdio, returns false on error |
42 |
static bool open_stdio(const char *title) |
43 |
{ |
44 |
// Create key |
45 |
char key_name[64]; |
46 |
bigtime_t t = system_time(); |
47 |
sprintf(key_name, "%Ld", t); |
48 |
|
49 |
// Make pipe names |
50 |
char out_pipe_name[64], in_pipe_name[64]; |
51 |
sprintf(out_pipe_name, "/pipe/debug_out_%s", key_name); |
52 |
sprintf(in_pipe_name, "/pipe/debug_in_%s", key_name); |
53 |
|
54 |
// Create semaphore |
55 |
char sem_name[B_OS_NAME_LENGTH], sem_id_str[B_OS_NAME_LENGTH]; |
56 |
sprintf(sem_name, "debug_glue_%s", key_name); |
57 |
sem_id glue_sem = create_sem(0, sem_name); |
58 |
sprintf(sem_id_str, "%d", glue_sem); |
59 |
|
60 |
// Make path for "Terminal" app |
61 |
char term_path[B_PATH_NAME_LENGTH]; |
62 |
find_directory(B_BEOS_APPS_DIRECTORY, -1, false, term_path, 1024); |
63 |
strcat(term_path, "/Terminal"); |
64 |
|
65 |
// Load "Terminal" |
66 |
const char *t_argv[6]; |
67 |
t_argv[0] = term_path; |
68 |
t_argv[1] = "-t"; |
69 |
t_argv[2] = (char *)title; |
70 |
t_argv[3] = "/bin/debug_glue"; |
71 |
t_argv[4] = key_name; |
72 |
t_argv[5] = sem_id_str; |
73 |
thread_id th = load_image(6, t_argv, (const char **)environ); |
74 |
if (th < 0) { |
75 |
delete_sem(glue_sem); |
76 |
return false; |
77 |
} |
78 |
|
79 |
// Start "Terminal" |
80 |
resume_thread(th); |
81 |
status_t err = acquire_sem_etc(glue_sem, 1, B_TIMEOUT, 5000000); |
82 |
delete_sem(glue_sem); |
83 |
if (err) |
84 |
return false; |
85 |
|
86 |
// Open input/output pipes |
87 |
FILE *in = freopen(in_pipe_name, "rb", stdin); |
88 |
if (in == NULL) |
89 |
return false; |
90 |
FILE *out = freopen(out_pipe_name, "wb", stdout); |
91 |
if (out == NULL) { |
92 |
fclose(in); |
93 |
return false; |
94 |
} |
95 |
|
96 |
// Set buffer modes |
97 |
setvbuf(stdout, NULL, _IOLBF, 0); |
98 |
return true; |
99 |
} |
100 |
#endif |
101 |
|
102 |
// Main program |
103 |
int main(int argc, const char **argv) |
104 |
{ |
105 |
#ifdef __BEOS__ |
106 |
// Launched from Tracker? Then open terminal window |
107 |
if (launched_from_tracker()) { |
108 |
if (!open_stdio("mon")) |
109 |
return 1; |
110 |
} |
111 |
#endif |
112 |
|
113 |
// Execute mon |
114 |
mon_init(); |
115 |
mon(argc, argv); |
116 |
mon_exit(); |
117 |
return 0; |
118 |
} |