1 | /* glpenv03.c (terminal output) */ |
---|
2 | |
---|
3 | /*********************************************************************** |
---|
4 | * This code is part of GLPK (GNU Linear Programming Kit). |
---|
5 | * |
---|
6 | * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, |
---|
7 | * 2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics, |
---|
8 | * Moscow Aviation Institute, Moscow, Russia. All rights reserved. |
---|
9 | * E-mail: <mao@gnu.org>. |
---|
10 | * |
---|
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. |
---|
15 | * |
---|
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. |
---|
20 | * |
---|
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 | ***********************************************************************/ |
---|
24 | |
---|
25 | #include "glpapi.h" |
---|
26 | |
---|
27 | /*********************************************************************** |
---|
28 | * NAME |
---|
29 | * |
---|
30 | * glp_printf - write formatted output to terminal |
---|
31 | * |
---|
32 | * SYNOPSIS |
---|
33 | * |
---|
34 | * void glp_printf(const char *fmt, ...); |
---|
35 | * |
---|
36 | * DESCRIPTION |
---|
37 | * |
---|
38 | * The routine glp_printf uses the format control string fmt to format |
---|
39 | * its parameters and writes the formatted output to the terminal. */ |
---|
40 | |
---|
41 | void glp_printf(const char *fmt, ...) |
---|
42 | { va_list arg; |
---|
43 | va_start(arg, fmt); |
---|
44 | xvprintf(fmt, arg); |
---|
45 | va_end(arg); |
---|
46 | return; |
---|
47 | } |
---|
48 | |
---|
49 | /*********************************************************************** |
---|
50 | * NAME |
---|
51 | * |
---|
52 | * glp_vprintf - write formatted output to terminal |
---|
53 | * |
---|
54 | * SYNOPSIS |
---|
55 | * |
---|
56 | * void glp_vprintf(const char *fmt, va_list arg); |
---|
57 | * |
---|
58 | * DESCRIPTION |
---|
59 | * |
---|
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. */ |
---|
63 | |
---|
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) |
---|
73 | goto skip; |
---|
74 | } |
---|
75 | /* send the output to the terminal */ |
---|
76 | fputs(env->term_buf, stdout); |
---|
77 | fflush(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); |
---|
82 | } |
---|
83 | skip: return; |
---|
84 | } |
---|
85 | |
---|
86 | /*********************************************************************** |
---|
87 | * NAME |
---|
88 | * |
---|
89 | * glp_term_out - enable/disable terminal output |
---|
90 | * |
---|
91 | * SYNOPSIS |
---|
92 | * |
---|
93 | * int glp_term_out(int flag); |
---|
94 | * |
---|
95 | * DESCRIPTION |
---|
96 | * |
---|
97 | * Depending on the parameter flag the routine glp_term_out enables or |
---|
98 | * disables terminal output performed by glpk routines: |
---|
99 | * |
---|
100 | * GLP_ON - enable terminal output; |
---|
101 | * GLP_OFF - disable terminal output. |
---|
102 | * |
---|
103 | * RETURNS |
---|
104 | * |
---|
105 | * The routine glp_term_out returns the previous value of the terminal |
---|
106 | * output flag. */ |
---|
107 | |
---|
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; |
---|
114 | return old; |
---|
115 | } |
---|
116 | |
---|
117 | /*********************************************************************** |
---|
118 | * NAME |
---|
119 | * |
---|
120 | * glp_term_hook - install hook to intercept terminal output |
---|
121 | * |
---|
122 | * SYNOPSIS |
---|
123 | * |
---|
124 | * void glp_term_hook(int (*func)(void *info, const char *s), |
---|
125 | * void *info); |
---|
126 | * |
---|
127 | * DESCRIPTION |
---|
128 | * |
---|
129 | * The routine glp_term_hook installs a user-defined hook routine to |
---|
130 | * intercept all terminal output performed by glpk routines. |
---|
131 | * |
---|
132 | * This feature can be used to redirect the terminal output to other |
---|
133 | * destination, for example to a file or a text window. |
---|
134 | * |
---|
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. |
---|
145 | * |
---|
146 | * To uninstall the hook routine the parameters func and info should be |
---|
147 | * specified as NULL. */ |
---|
148 | |
---|
149 | void glp_term_hook(int (*func)(void *info, const char *s), void *info) |
---|
150 | { ENV *env = get_env_ptr(); |
---|
151 | if (func == NULL) |
---|
152 | { env->term_hook = NULL; |
---|
153 | env->term_info = NULL; |
---|
154 | } |
---|
155 | else |
---|
156 | { env->term_hook = func; |
---|
157 | env->term_info = info; |
---|
158 | } |
---|
159 | return; |
---|
160 | } |
---|
161 | |
---|
162 | /*********************************************************************** |
---|
163 | * NAME |
---|
164 | * |
---|
165 | * glp_open_tee - start copying terminal output to text file |
---|
166 | * |
---|
167 | * SYNOPSIS |
---|
168 | * |
---|
169 | * int glp_open_tee(const char *fname); |
---|
170 | * |
---|
171 | * DESCRIPTION |
---|
172 | * |
---|
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 |
---|
175 | * fname. |
---|
176 | * |
---|
177 | * RETURNS |
---|
178 | * |
---|
179 | * 0 - operation successful |
---|
180 | * 1 - copying terminal output is already active |
---|
181 | * 2 - unable to create output file */ |
---|
182 | |
---|
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 */ |
---|
187 | return 1; |
---|
188 | } |
---|
189 | env->tee_file = fopen(fname, "w"); |
---|
190 | if (env->tee_file == NULL) |
---|
191 | { /* unable to create output file */ |
---|
192 | return 2; |
---|
193 | } |
---|
194 | return 0; |
---|
195 | } |
---|
196 | |
---|
197 | /*********************************************************************** |
---|
198 | * NAME |
---|
199 | * |
---|
200 | * glp_close_tee - stop copying terminal output to text file |
---|
201 | * |
---|
202 | * SYNOPSIS |
---|
203 | * |
---|
204 | * int glp_close_tee(void); |
---|
205 | * |
---|
206 | * DESCRIPTION |
---|
207 | * |
---|
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 |
---|
210 | * that file. |
---|
211 | * |
---|
212 | * RETURNS |
---|
213 | * |
---|
214 | * 0 - operation successful |
---|
215 | * 1 - copying terminal output was not started */ |
---|
216 | |
---|
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 */ |
---|
221 | return 1; |
---|
222 | } |
---|
223 | fclose(env->tee_file); |
---|
224 | env->tee_file = NULL; |
---|
225 | return 0; |
---|
226 | } |
---|
227 | |
---|
228 | /* eof */ |
---|