ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/mon/src/main.cpp
Revision: 1.2
Committed: 1999-10-04T21:16:02Z (24 years, 7 months ago) by cebix
Branch: MAIN
Changes since 1.1: +18 -6 lines
Log Message:
- GPL'ified sources
- added provisions for autoconf stuff

File Contents

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