1 |
/* |
2 |
* main.cpp - "mon" main program |
3 |
* |
4 |
* (C) 1997-1999 Christian Bauer |
5 |
*/ |
6 |
|
7 |
#include <stdlib.h> |
8 |
#include <stdio.h> |
9 |
#include <string.h> |
10 |
#include <unistd.h> |
11 |
|
12 |
#include "mon.h" |
13 |
|
14 |
#if __BEOS__ |
15 |
#include <AppKit.h> |
16 |
#include <KernelKit.h> |
17 |
#include <StorageKit.h> |
18 |
|
19 |
// Detect if program was launched from Shell or Tracker |
20 |
static bool launched_from_tracker(void) |
21 |
{ |
22 |
char *cmd = getenv("_"); |
23 |
if (cmd == NULL || strlen(cmd) < 7) |
24 |
return false; |
25 |
return !strcmp(cmd + strlen(cmd) - 7 , "Tracker"); |
26 |
} |
27 |
|
28 |
// Open Terminal window with given title for stdio, returns false on error |
29 |
static bool open_stdio(const char *title) |
30 |
{ |
31 |
// Create key |
32 |
char key_name[64]; |
33 |
bigtime_t t = system_time(); |
34 |
sprintf(key_name, "%Ld", t); |
35 |
|
36 |
// Make pipe names |
37 |
char out_pipe_name[64], in_pipe_name[64]; |
38 |
sprintf(out_pipe_name, "/pipe/debug_out_%s", key_name); |
39 |
sprintf(in_pipe_name, "/pipe/debug_in_%s", key_name); |
40 |
|
41 |
// Create semaphore |
42 |
char sem_name[B_OS_NAME_LENGTH], sem_id_str[B_OS_NAME_LENGTH]; |
43 |
sprintf(sem_name, "debug_glue_%s", key_name); |
44 |
sem_id glue_sem = create_sem(0, sem_name); |
45 |
sprintf(sem_id_str, "%d", glue_sem); |
46 |
|
47 |
// Make path for "Terminal" app |
48 |
char term_path[B_PATH_NAME_LENGTH]; |
49 |
find_directory(B_BEOS_APPS_DIRECTORY, -1, false, term_path, 1024); |
50 |
strcat(term_path, "/Terminal"); |
51 |
|
52 |
// Load "Terminal" |
53 |
char *t_argv[6]; |
54 |
t_argv[0] = term_path; |
55 |
t_argv[1] = "-t"; |
56 |
t_argv[2] = (char *)title; |
57 |
t_argv[3] = "/bin/debug_glue"; |
58 |
t_argv[4] = key_name; |
59 |
t_argv[5] = sem_id_str; |
60 |
thread_id th = load_image(6, t_argv, environ); |
61 |
if (th < 0) { |
62 |
delete_sem(glue_sem); |
63 |
return false; |
64 |
} |
65 |
|
66 |
// Start "Terminal" |
67 |
resume_thread(th); |
68 |
status_t err = acquire_sem_etc(glue_sem, 1, B_TIMEOUT, 5000000); |
69 |
delete_sem(glue_sem); |
70 |
if (err) |
71 |
return false; |
72 |
|
73 |
// Open input/output pipes |
74 |
FILE *in = freopen(in_pipe_name, "rb", stdin); |
75 |
if (in == NULL) |
76 |
return false; |
77 |
FILE *out = freopen(out_pipe_name, "wb", stdout); |
78 |
if (out == NULL) { |
79 |
fclose(in); |
80 |
return false; |
81 |
} |
82 |
|
83 |
// Set buffer modes |
84 |
setvbuf(stdout, NULL, _IOLBF, 0); |
85 |
return true; |
86 |
} |
87 |
#endif |
88 |
|
89 |
// Main program |
90 |
int main(int argc, char **argv) |
91 |
{ |
92 |
#if __BEOS__ |
93 |
// Launched from Tracker? Then open terminal window |
94 |
if (launched_from_tracker()) { |
95 |
if (!open_stdio("mon")) |
96 |
return 1; |
97 |
} |
98 |
#endif |
99 |
|
100 |
// Execute mon |
101 |
mon_init(); |
102 |
mon(argc, argv); |
103 |
mon_exit(); |
104 |
return 0; |
105 |
} |