/
conf.m
185 lines (157 loc) · 5.55 KB
/
conf.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 2001-2002, 2004-2008, 2010 The University of Melbourne.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%-----------------------------------------------------------------------------%
%
% Author: zs.
%
% This module contains primitives whose parameters are decided by
% ../configure.in. This module picks them up from the #defines put into
% runtime/mercury_conf.h by the configure script.
:- module conf.
:- interface.
:- import_module io.
%-----------------------------------------------------------------------------%
% Given a pathname, return a shell command that will create a named pipe
% with that pathname.
%
:- func make_pipe_cmd(string) = string.
% The name of the server and, optionally, the port on which mdprof is
% being run.
%
:- pred server_name_port(string::out, io::di, io::uo) is det.
% The virtual path under which this program is being executed, used for
% self-referencing URLs.
%
:- pred script_name(string::out, io::di, io::uo) is det.
:- func getpid = int.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module list.
:- import_module maybe.
:- import_module require.
:- import_module string.
%-----------------------------------------------------------------------------%
make_pipe_cmd(PipeName) = Cmd :-
mkfifo_cmd(CmdName),
( CmdName = "" ->
unexpected($module, $pred, "do not know what command to use")
;
string.format("%s %s", [s(CmdName), s(PipeName)], Cmd)
).
server_name_port(Machine, !IO) :-
server_name(ServerName, !IO),
maybe_server_port(MaybeServerPort, !IO),
(
MaybeServerPort = yes(Port),
Machine = ServerName ++ ":" ++ Port
;
MaybeServerPort = no,
Machine = ServerName
).
:- pred server_name(string::out, io::di, io::uo) is det.
server_name(ServerName, !IO) :-
io.get_environment_var("SERVER_NAME", MaybeServerName, !IO),
(
MaybeServerName = yes(ServerName)
;
MaybeServerName = no,
server_name_2(ServerName, !IO)
).
:- pred server_name_2(string::out, io::di, io::uo) is det.
server_name_2(ServerName, !IO) :-
io.make_temp(TmpFile, !IO),
hostname_cmd(HostnameCmd),
ServerRedirectCmd =
string.format("%s > %s", [s(HostnameCmd), s(TmpFile)]),
io.call_system(ServerRedirectCmd, Res1, !IO),
(
Res1 = ok(ResCode),
( ResCode = 0 ->
io.open_input(TmpFile, TmpRes, !IO),
(
TmpRes = ok(TmpStream),
io.read_file_as_string(TmpStream, TmpReadRes, !IO),
(
TmpReadRes = ok(ServerNameNl),
(
string.remove_suffix(ServerNameNl, "\n",
ServerNamePrime)
->
ServerName = ServerNamePrime
;
unexpected($module, $pred, "malformed server name")
)
;
TmpReadRes = error(_, _),
unexpected($module, $pred, "cannot read server's name")
),
io.close_input(TmpStream, !IO)
;
TmpRes = error(_),
unexpected($module, $pred,
"cannot open file to find the server's name")
),
io.remove_file(TmpFile, _, !IO)
;
unexpected($module, $pred,
"cannot execute cmd to find the server's name")
)
;
Res1 = error(_),
unexpected($module, $pred,
"cannot execute cmd to find the server's name")
).
:- pred maybe_server_port(maybe(string)::out, io::di, io::uo) is det.
maybe_server_port(MaybeServerPort, !IO) :-
io.get_environment_var("SERVER_PORT", MaybeServerPort, !IO).
script_name(ScriptName, !IO) :-
io.get_environment_var("SCRIPT_NAME", MaybeScriptName, !IO),
(
MaybeScriptName = yes(ScriptName)
;
MaybeScriptName = no,
% Should not happen if this predicate is called as a CGI program,
% but this predicate is also called by other tools.
ScriptName = "/cgi-bin/mdprof_cgi"
).
:- pred mkfifo_cmd(string::out) is det.
:- pragma foreign_proc("C",
mkfifo_cmd(Mkfifo::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
/* shut up warnings about casting away const */
Mkfifo = (MR_String) (MR_Integer) MR_MKFIFO;
").
:- pred hostname_cmd(string::out) is det.
:- pragma foreign_proc("C",
hostname_cmd(Hostname::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
/* shut up warnings about casting away const */
Hostname = (MR_String) (MR_Integer) MR_HOSTNAMECMD;
").
:- pragma foreign_decl("C",
"
#ifdef MR_DEEP_PROFILER_ENABLED
#include <sys/types.h>
#include <unistd.h>
#endif
").
:- pragma foreign_proc("C",
getpid = (Pid::out),
[will_not_call_mercury, promise_pure],
"
#ifdef MR_DEEP_PROFILER_ENABLED
Pid = getpid();
#else
MR_fatal_error(""the deep profiler is not supported"");
#endif
").
%-----------------------------------------------------------------------------%
:- end_module conf.
%-----------------------------------------------------------------------------%