alpar@1
|
1 |
/* glpenv03.c (terminal output) */
|
alpar@1
|
2 |
|
alpar@1
|
3 |
/***********************************************************************
|
alpar@1
|
4 |
* This code is part of GLPK (GNU Linear Programming Kit).
|
alpar@1
|
5 |
*
|
alpar@1
|
6 |
* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
|
alpar@1
|
7 |
* 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
|
alpar@1
|
8 |
* Moscow Aviation Institute, Moscow, Russia. All rights reserved.
|
alpar@1
|
9 |
* E-mail: <mao@gnu.org>.
|
alpar@1
|
10 |
*
|
alpar@1
|
11 |
* GLPK is free software: you can redistribute it and/or modify it
|
alpar@1
|
12 |
* under the terms of the GNU General Public License as published by
|
alpar@1
|
13 |
* the Free Software Foundation, either version 3 of the License, or
|
alpar@1
|
14 |
* (at your option) any later version.
|
alpar@1
|
15 |
*
|
alpar@1
|
16 |
* GLPK is distributed in the hope that it will be useful, but WITHOUT
|
alpar@1
|
17 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
alpar@1
|
18 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
alpar@1
|
19 |
* License for more details.
|
alpar@1
|
20 |
*
|
alpar@1
|
21 |
* You should have received a copy of the GNU General Public License
|
alpar@1
|
22 |
* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
|
alpar@1
|
23 |
***********************************************************************/
|
alpar@1
|
24 |
|
alpar@1
|
25 |
#include "glpapi.h"
|
alpar@1
|
26 |
|
alpar@1
|
27 |
/***********************************************************************
|
alpar@1
|
28 |
* NAME
|
alpar@1
|
29 |
*
|
alpar@1
|
30 |
* glp_printf - write formatted output to terminal
|
alpar@1
|
31 |
*
|
alpar@1
|
32 |
* SYNOPSIS
|
alpar@1
|
33 |
*
|
alpar@1
|
34 |
* void glp_printf(const char *fmt, ...);
|
alpar@1
|
35 |
*
|
alpar@1
|
36 |
* DESCRIPTION
|
alpar@1
|
37 |
*
|
alpar@1
|
38 |
* The routine glp_printf uses the format control string fmt to format
|
alpar@1
|
39 |
* its parameters and writes the formatted output to the terminal. */
|
alpar@1
|
40 |
|
alpar@1
|
41 |
void glp_printf(const char *fmt, ...)
|
alpar@1
|
42 |
{ va_list arg;
|
alpar@1
|
43 |
va_start(arg, fmt);
|
alpar@1
|
44 |
xvprintf(fmt, arg);
|
alpar@1
|
45 |
va_end(arg);
|
alpar@1
|
46 |
return;
|
alpar@1
|
47 |
}
|
alpar@1
|
48 |
|
alpar@1
|
49 |
/***********************************************************************
|
alpar@1
|
50 |
* NAME
|
alpar@1
|
51 |
*
|
alpar@1
|
52 |
* glp_vprintf - write formatted output to terminal
|
alpar@1
|
53 |
*
|
alpar@1
|
54 |
* SYNOPSIS
|
alpar@1
|
55 |
*
|
alpar@1
|
56 |
* void glp_vprintf(const char *fmt, va_list arg);
|
alpar@1
|
57 |
*
|
alpar@1
|
58 |
* DESCRIPTION
|
alpar@1
|
59 |
*
|
alpar@1
|
60 |
* The routine glp_vprintf uses the format control string fmt to format
|
alpar@1
|
61 |
* its parameters specified by the list arg and writes the formatted
|
alpar@1
|
62 |
* output to the terminal. */
|
alpar@1
|
63 |
|
alpar@1
|
64 |
void glp_vprintf(const char *fmt, va_list arg)
|
alpar@1
|
65 |
{ ENV *env = get_env_ptr();
|
alpar@1
|
66 |
/* if terminal output is disabled, do nothing */
|
alpar@1
|
67 |
if (!env->term_out) goto skip;
|
alpar@1
|
68 |
/* format the output */
|
alpar@1
|
69 |
vsprintf(env->term_buf, fmt, arg);
|
alpar@1
|
70 |
/* pass the output to the user-defined routine */
|
alpar@1
|
71 |
if (env->term_hook != NULL)
|
alpar@1
|
72 |
{ if (env->term_hook(env->term_info, env->term_buf) != 0)
|
alpar@1
|
73 |
goto skip;
|
alpar@1
|
74 |
}
|
alpar@1
|
75 |
/* send the output to the terminal */
|
alpar@1
|
76 |
fputs(env->term_buf, stdout);
|
alpar@1
|
77 |
fflush(stdout);
|
alpar@1
|
78 |
/* copy the output to the text file */
|
alpar@1
|
79 |
if (env->tee_file != NULL)
|
alpar@1
|
80 |
{ fputs(env->term_buf, env->tee_file);
|
alpar@1
|
81 |
fflush(env->tee_file);
|
alpar@1
|
82 |
}
|
alpar@1
|
83 |
skip: return;
|
alpar@1
|
84 |
}
|
alpar@1
|
85 |
|
alpar@1
|
86 |
/***********************************************************************
|
alpar@1
|
87 |
* NAME
|
alpar@1
|
88 |
*
|
alpar@1
|
89 |
* glp_term_out - enable/disable terminal output
|
alpar@1
|
90 |
*
|
alpar@1
|
91 |
* SYNOPSIS
|
alpar@1
|
92 |
*
|
alpar@1
|
93 |
* int glp_term_out(int flag);
|
alpar@1
|
94 |
*
|
alpar@1
|
95 |
* DESCRIPTION
|
alpar@1
|
96 |
*
|
alpar@1
|
97 |
* Depending on the parameter flag the routine glp_term_out enables or
|
alpar@1
|
98 |
* disables terminal output performed by glpk routines:
|
alpar@1
|
99 |
*
|
alpar@1
|
100 |
* GLP_ON - enable terminal output;
|
alpar@1
|
101 |
* GLP_OFF - disable terminal output.
|
alpar@1
|
102 |
*
|
alpar@1
|
103 |
* RETURNS
|
alpar@1
|
104 |
*
|
alpar@1
|
105 |
* The routine glp_term_out returns the previous value of the terminal
|
alpar@1
|
106 |
* output flag. */
|
alpar@1
|
107 |
|
alpar@1
|
108 |
int glp_term_out(int flag)
|
alpar@1
|
109 |
{ ENV *env = get_env_ptr();
|
alpar@1
|
110 |
int old = env->term_out;
|
alpar@1
|
111 |
if (!(flag == GLP_ON || flag == GLP_OFF))
|
alpar@1
|
112 |
xerror("glp_term_out: flag = %d; invalid value\n", flag);
|
alpar@1
|
113 |
env->term_out = flag;
|
alpar@1
|
114 |
return old;
|
alpar@1
|
115 |
}
|
alpar@1
|
116 |
|
alpar@1
|
117 |
/***********************************************************************
|
alpar@1
|
118 |
* NAME
|
alpar@1
|
119 |
*
|
alpar@1
|
120 |
* glp_term_hook - install hook to intercept terminal output
|
alpar@1
|
121 |
*
|
alpar@1
|
122 |
* SYNOPSIS
|
alpar@1
|
123 |
*
|
alpar@1
|
124 |
* void glp_term_hook(int (*func)(void *info, const char *s),
|
alpar@1
|
125 |
* void *info);
|
alpar@1
|
126 |
*
|
alpar@1
|
127 |
* DESCRIPTION
|
alpar@1
|
128 |
*
|
alpar@1
|
129 |
* The routine glp_term_hook installs a user-defined hook routine to
|
alpar@1
|
130 |
* intercept all terminal output performed by glpk routines.
|
alpar@1
|
131 |
*
|
alpar@1
|
132 |
* This feature can be used to redirect the terminal output to other
|
alpar@1
|
133 |
* destination, for example to a file or a text window.
|
alpar@1
|
134 |
*
|
alpar@1
|
135 |
* The parameter func specifies the user-defined hook routine. It is
|
alpar@1
|
136 |
* called from an internal printing routine, which passes to it two
|
alpar@1
|
137 |
* parameters: info and s. The parameter info is a transit pointer,
|
alpar@1
|
138 |
* specified in the corresponding call to the routine glp_term_hook;
|
alpar@1
|
139 |
* it may be used to pass some information to the hook routine. The
|
alpar@1
|
140 |
* parameter s is a pointer to the null terminated character string,
|
alpar@1
|
141 |
* which is intended to be written to the terminal. If the hook routine
|
alpar@1
|
142 |
* returns zero, the printing routine writes the string s to the
|
alpar@1
|
143 |
* terminal in a usual way; otherwise, if the hook routine returns
|
alpar@1
|
144 |
* non-zero, no terminal output is performed.
|
alpar@1
|
145 |
*
|
alpar@1
|
146 |
* To uninstall the hook routine the parameters func and info should be
|
alpar@1
|
147 |
* specified as NULL. */
|
alpar@1
|
148 |
|
alpar@1
|
149 |
void glp_term_hook(int (*func)(void *info, const char *s), void *info)
|
alpar@1
|
150 |
{ ENV *env = get_env_ptr();
|
alpar@1
|
151 |
if (func == NULL)
|
alpar@1
|
152 |
{ env->term_hook = NULL;
|
alpar@1
|
153 |
env->term_info = NULL;
|
alpar@1
|
154 |
}
|
alpar@1
|
155 |
else
|
alpar@1
|
156 |
{ env->term_hook = func;
|
alpar@1
|
157 |
env->term_info = info;
|
alpar@1
|
158 |
}
|
alpar@1
|
159 |
return;
|
alpar@1
|
160 |
}
|
alpar@1
|
161 |
|
alpar@1
|
162 |
/***********************************************************************
|
alpar@1
|
163 |
* NAME
|
alpar@1
|
164 |
*
|
alpar@1
|
165 |
* glp_open_tee - start copying terminal output to text file
|
alpar@1
|
166 |
*
|
alpar@1
|
167 |
* SYNOPSIS
|
alpar@1
|
168 |
*
|
alpar@1
|
169 |
* int glp_open_tee(const char *fname);
|
alpar@1
|
170 |
*
|
alpar@1
|
171 |
* DESCRIPTION
|
alpar@1
|
172 |
*
|
alpar@1
|
173 |
* The routine glp_open_tee starts copying all the terminal output to
|
alpar@1
|
174 |
* an output text file, whose name is specified by the character string
|
alpar@1
|
175 |
* fname.
|
alpar@1
|
176 |
*
|
alpar@1
|
177 |
* RETURNS
|
alpar@1
|
178 |
*
|
alpar@1
|
179 |
* 0 - operation successful
|
alpar@1
|
180 |
* 1 - copying terminal output is already active
|
alpar@1
|
181 |
* 2 - unable to create output file */
|
alpar@1
|
182 |
|
alpar@1
|
183 |
int glp_open_tee(const char *fname)
|
alpar@1
|
184 |
{ ENV *env = get_env_ptr();
|
alpar@1
|
185 |
if (env->tee_file != NULL)
|
alpar@1
|
186 |
{ /* copying terminal output is already active */
|
alpar@1
|
187 |
return 1;
|
alpar@1
|
188 |
}
|
alpar@1
|
189 |
env->tee_file = fopen(fname, "w");
|
alpar@1
|
190 |
if (env->tee_file == NULL)
|
alpar@1
|
191 |
{ /* unable to create output file */
|
alpar@1
|
192 |
return 2;
|
alpar@1
|
193 |
}
|
alpar@1
|
194 |
return 0;
|
alpar@1
|
195 |
}
|
alpar@1
|
196 |
|
alpar@1
|
197 |
/***********************************************************************
|
alpar@1
|
198 |
* NAME
|
alpar@1
|
199 |
*
|
alpar@1
|
200 |
* glp_close_tee - stop copying terminal output to text file
|
alpar@1
|
201 |
*
|
alpar@1
|
202 |
* SYNOPSIS
|
alpar@1
|
203 |
*
|
alpar@1
|
204 |
* int glp_close_tee(void);
|
alpar@1
|
205 |
*
|
alpar@1
|
206 |
* DESCRIPTION
|
alpar@1
|
207 |
*
|
alpar@1
|
208 |
* The routine glp_close_tee stops copying the terminal output to the
|
alpar@1
|
209 |
* output text file previously open by the routine glp_open_tee closing
|
alpar@1
|
210 |
* that file.
|
alpar@1
|
211 |
*
|
alpar@1
|
212 |
* RETURNS
|
alpar@1
|
213 |
*
|
alpar@1
|
214 |
* 0 - operation successful
|
alpar@1
|
215 |
* 1 - copying terminal output was not started */
|
alpar@1
|
216 |
|
alpar@1
|
217 |
int glp_close_tee(void)
|
alpar@1
|
218 |
{ ENV *env = get_env_ptr();
|
alpar@1
|
219 |
if (env->tee_file == NULL)
|
alpar@1
|
220 |
{ /* copying terminal output was not started */
|
alpar@1
|
221 |
return 1;
|
alpar@1
|
222 |
}
|
alpar@1
|
223 |
fclose(env->tee_file);
|
alpar@1
|
224 |
env->tee_file = NULL;
|
alpar@1
|
225 |
return 0;
|
alpar@1
|
226 |
}
|
alpar@1
|
227 |
|
alpar@1
|
228 |
/* eof */
|