glpenv03.c 6.5 KB

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