alpar@9: /* glpenv03.c (terminal output) */ alpar@9: alpar@9: /*********************************************************************** alpar@9: * This code is part of GLPK (GNU Linear Programming Kit). alpar@9: * alpar@9: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@9: * 2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics, alpar@9: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@9: * E-mail: . alpar@9: * alpar@9: * GLPK is free software: you can redistribute it and/or modify it alpar@9: * under the terms of the GNU General Public License as published by alpar@9: * the Free Software Foundation, either version 3 of the License, or alpar@9: * (at your option) any later version. alpar@9: * alpar@9: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@9: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@9: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@9: * License for more details. alpar@9: * alpar@9: * You should have received a copy of the GNU General Public License alpar@9: * along with GLPK. If not, see . alpar@9: ***********************************************************************/ alpar@9: alpar@9: #include "glpapi.h" alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_printf - write formatted output to terminal alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_printf(const char *fmt, ...); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_printf uses the format control string fmt to format alpar@9: * its parameters and writes the formatted output to the terminal. */ alpar@9: alpar@9: void glp_printf(const char *fmt, ...) alpar@9: { va_list arg; alpar@9: va_start(arg, fmt); alpar@9: xvprintf(fmt, arg); alpar@9: va_end(arg); alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_vprintf - write formatted output to terminal alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_vprintf(const char *fmt, va_list arg); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_vprintf uses the format control string fmt to format alpar@9: * its parameters specified by the list arg and writes the formatted alpar@9: * output to the terminal. */ alpar@9: alpar@9: void glp_vprintf(const char *fmt, va_list arg) alpar@9: { ENV *env = get_env_ptr(); alpar@9: /* if terminal output is disabled, do nothing */ alpar@9: if (!env->term_out) goto skip; alpar@9: /* format the output */ alpar@9: vsprintf(env->term_buf, fmt, arg); alpar@9: /* pass the output to the user-defined routine */ alpar@9: if (env->term_hook != NULL) alpar@9: { if (env->term_hook(env->term_info, env->term_buf) != 0) alpar@9: goto skip; alpar@9: } alpar@9: /* send the output to the terminal */ alpar@9: fputs(env->term_buf, stdout); alpar@9: fflush(stdout); alpar@9: /* copy the output to the text file */ alpar@9: if (env->tee_file != NULL) alpar@9: { fputs(env->term_buf, env->tee_file); alpar@9: fflush(env->tee_file); alpar@9: } alpar@9: skip: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_term_out - enable/disable terminal output alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_term_out(int flag); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * Depending on the parameter flag the routine glp_term_out enables or alpar@9: * disables terminal output performed by glpk routines: alpar@9: * alpar@9: * GLP_ON - enable terminal output; alpar@9: * GLP_OFF - disable terminal output. alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The routine glp_term_out returns the previous value of the terminal alpar@9: * output flag. */ alpar@9: alpar@9: int glp_term_out(int flag) alpar@9: { ENV *env = get_env_ptr(); alpar@9: int old = env->term_out; alpar@9: if (!(flag == GLP_ON || flag == GLP_OFF)) alpar@9: xerror("glp_term_out: flag = %d; invalid value\n", flag); alpar@9: env->term_out = flag; alpar@9: return old; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_term_hook - install hook to intercept terminal output alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_term_hook(int (*func)(void *info, const char *s), alpar@9: * void *info); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_term_hook installs a user-defined hook routine to alpar@9: * intercept all terminal output performed by glpk routines. alpar@9: * alpar@9: * This feature can be used to redirect the terminal output to other alpar@9: * destination, for example to a file or a text window. alpar@9: * alpar@9: * The parameter func specifies the user-defined hook routine. It is alpar@9: * called from an internal printing routine, which passes to it two alpar@9: * parameters: info and s. The parameter info is a transit pointer, alpar@9: * specified in the corresponding call to the routine glp_term_hook; alpar@9: * it may be used to pass some information to the hook routine. The alpar@9: * parameter s is a pointer to the null terminated character string, alpar@9: * which is intended to be written to the terminal. If the hook routine alpar@9: * returns zero, the printing routine writes the string s to the alpar@9: * terminal in a usual way; otherwise, if the hook routine returns alpar@9: * non-zero, no terminal output is performed. alpar@9: * alpar@9: * To uninstall the hook routine the parameters func and info should be alpar@9: * specified as NULL. */ alpar@9: alpar@9: void glp_term_hook(int (*func)(void *info, const char *s), void *info) alpar@9: { ENV *env = get_env_ptr(); alpar@9: if (func == NULL) alpar@9: { env->term_hook = NULL; alpar@9: env->term_info = NULL; alpar@9: } alpar@9: else alpar@9: { env->term_hook = func; alpar@9: env->term_info = info; alpar@9: } alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_open_tee - start copying terminal output to text file alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_open_tee(const char *fname); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_open_tee starts copying all the terminal output to alpar@9: * an output text file, whose name is specified by the character string alpar@9: * fname. alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * 0 - operation successful alpar@9: * 1 - copying terminal output is already active alpar@9: * 2 - unable to create output file */ alpar@9: alpar@9: int glp_open_tee(const char *fname) alpar@9: { ENV *env = get_env_ptr(); alpar@9: if (env->tee_file != NULL) alpar@9: { /* copying terminal output is already active */ alpar@9: return 1; alpar@9: } alpar@9: env->tee_file = fopen(fname, "w"); alpar@9: if (env->tee_file == NULL) alpar@9: { /* unable to create output file */ alpar@9: return 2; alpar@9: } alpar@9: return 0; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_close_tee - stop copying terminal output to text file alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_close_tee(void); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_close_tee stops copying the terminal output to the alpar@9: * output text file previously open by the routine glp_open_tee closing alpar@9: * that file. alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * 0 - operation successful alpar@9: * 1 - copying terminal output was not started */ alpar@9: alpar@9: int glp_close_tee(void) alpar@9: { ENV *env = get_env_ptr(); alpar@9: if (env->tee_file == NULL) alpar@9: { /* copying terminal output was not started */ alpar@9: return 1; alpar@9: } alpar@9: fclose(env->tee_file); alpar@9: env->tee_file = NULL; alpar@9: return 0; alpar@9: } alpar@9: alpar@9: /* eof */