1 /* glpenv03.c (terminal output) */
3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7 * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
9 * E-mail: <mao@gnu.org>.
11 * GLPK is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
23 ***********************************************************************/
27 /***********************************************************************
30 * glp_printf - write formatted output to terminal
34 * void glp_printf(const char *fmt, ...);
38 * The routine glp_printf uses the format control string fmt to format
39 * its parameters and writes the formatted output to the terminal. */
41 void glp_printf(const char *fmt, ...)
49 /***********************************************************************
52 * glp_vprintf - write formatted output to terminal
56 * void glp_vprintf(const char *fmt, va_list arg);
60 * The routine glp_vprintf uses the format control string fmt to format
61 * its parameters specified by the list arg and writes the formatted
62 * output to the terminal. */
64 void glp_vprintf(const char *fmt, va_list arg)
65 { ENV *env = get_env_ptr();
66 /* if terminal output is disabled, do nothing */
67 if (!env->term_out) goto skip;
68 /* format the output */
69 vsprintf(env->term_buf, fmt, arg);
70 /* pass the output to the user-defined routine */
71 if (env->term_hook != NULL)
72 { if (env->term_hook(env->term_info, env->term_buf) != 0)
75 /* send the output to the terminal */
76 fputs(env->term_buf, stdout);
78 /* copy the output to the text file */
79 if (env->tee_file != NULL)
80 { fputs(env->term_buf, env->tee_file);
81 fflush(env->tee_file);
86 /***********************************************************************
89 * glp_term_out - enable/disable terminal output
93 * int glp_term_out(int flag);
97 * Depending on the parameter flag the routine glp_term_out enables or
98 * disables terminal output performed by glpk routines:
100 * GLP_ON - enable terminal output;
101 * GLP_OFF - disable terminal output.
105 * The routine glp_term_out returns the previous value of the terminal
108 int glp_term_out(int flag)
109 { ENV *env = get_env_ptr();
110 int old = env->term_out;
111 if (!(flag == GLP_ON || flag == GLP_OFF))
112 xerror("glp_term_out: flag = %d; invalid value\n", flag);
113 env->term_out = flag;
117 /***********************************************************************
120 * glp_term_hook - install hook to intercept terminal output
124 * void glp_term_hook(int (*func)(void *info, const char *s),
129 * The routine glp_term_hook installs a user-defined hook routine to
130 * intercept all terminal output performed by glpk routines.
132 * This feature can be used to redirect the terminal output to other
133 * destination, for example to a file or a text window.
135 * The parameter func specifies the user-defined hook routine. It is
136 * called from an internal printing routine, which passes to it two
137 * parameters: info and s. The parameter info is a transit pointer,
138 * specified in the corresponding call to the routine glp_term_hook;
139 * it may be used to pass some information to the hook routine. The
140 * parameter s is a pointer to the null terminated character string,
141 * which is intended to be written to the terminal. If the hook routine
142 * returns zero, the printing routine writes the string s to the
143 * terminal in a usual way; otherwise, if the hook routine returns
144 * non-zero, no terminal output is performed.
146 * To uninstall the hook routine the parameters func and info should be
147 * specified as NULL. */
149 void glp_term_hook(int (*func)(void *info, const char *s), void *info)
150 { ENV *env = get_env_ptr();
152 { env->term_hook = NULL;
153 env->term_info = NULL;
156 { env->term_hook = func;
157 env->term_info = info;
162 /***********************************************************************
165 * glp_open_tee - start copying terminal output to text file
169 * int glp_open_tee(const char *fname);
173 * The routine glp_open_tee starts copying all the terminal output to
174 * an output text file, whose name is specified by the character string
179 * 0 - operation successful
180 * 1 - copying terminal output is already active
181 * 2 - unable to create output file */
183 int glp_open_tee(const char *fname)
184 { ENV *env = get_env_ptr();
185 if (env->tee_file != NULL)
186 { /* copying terminal output is already active */
189 env->tee_file = fopen(fname, "w");
190 if (env->tee_file == NULL)
191 { /* unable to create output file */
197 /***********************************************************************
200 * glp_close_tee - stop copying terminal output to text file
204 * int glp_close_tee(void);
208 * The routine glp_close_tee stops copying the terminal output to the
209 * output text file previously open by the routine glp_open_tee closing
214 * 0 - operation successful
215 * 1 - copying terminal output was not started */
217 int glp_close_tee(void)
218 { ENV *env = get_env_ptr();
219 if (env->tee_file == NULL)
220 { /* copying terminal output was not started */
223 fclose(env->tee_file);
224 env->tee_file = NULL;