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