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