1 |
/* |
2 |
* ASCEND Printf Substitutes |
3 |
* by Mark Thomas |
4 |
* Created: 27.May.1997 |
5 |
* Version: $Revision: 1.6 $ |
6 |
* Version control file: $RCSfile: ascPrint.c,v $ |
7 |
* Date last modified: $Date: 1997/10/29 13:08:49 $ |
8 |
* Last modified by: $Author: mthomas $ |
9 |
* |
10 |
* This file is part of the ASCEND utilities. |
11 |
* |
12 |
* Copyright 1997, Carnegie Mellon University |
13 |
* |
14 |
* The ASCEND utilities is free software; you can redistribute |
15 |
* it and/or modify it under the terms of the GNU General Public License as |
16 |
* published by the Free Software Foundation; either version 2 of the |
17 |
* License, or (at your option) any later version. |
18 |
* |
19 |
* The ASCEND utilities is distributed in hope that it will be |
20 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
22 |
* General Public License for more details. |
23 |
* |
24 |
* You should have received a copy of the GNU General Public License |
25 |
* along with the program; if not, write to the Free Software Foundation, |
26 |
* Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named |
27 |
* COPYING. COPYING is found in ../compiler. |
28 |
*/ |
29 |
|
30 |
#include <stdarg.h> |
31 |
#include "tcl.h" |
32 |
#include "utilities/ascConfig.h" |
33 |
#include "utilities/ascPrint.h" |
34 |
#include "utilities/ascPrintType.h" |
35 |
|
36 |
/* |
37 |
* Only compile this file if we are using Asc_Printf() |
38 |
*/ |
39 |
#ifdef USE_ASC_PRINTF |
40 |
|
41 |
#define PRINT_BUFFER_SIZE 16380 |
42 |
|
43 |
/* |
44 |
* The Tcl channels for stdout and stderr. |
45 |
* |
46 |
* These need to be initialized by calling Asc_PrintInit() before |
47 |
* calls to Asc_Printf() will write to the Tcl Channels. |
48 |
*/ |
49 |
static Tcl_Channel g_tclout = NULL; /* the Tcl channel for stdout */ |
50 |
static Tcl_Channel g_tclerr = NULL; /* the Tcl channel for stderr */ |
51 |
|
52 |
static struct Asc_PrintVTable g_Asc_PrintVTable_Tcl; |
53 |
static int Asc_PrintTcl(FILE *fp, CONST char *format, va_list args); |
54 |
static int Asc_FFlushTcl(FILE *fp); |
55 |
static char *g_AscTclPrintName = "tclPrintChannels"; |
56 |
|
57 |
int Asc_PrintInit_TclVtable(void) |
58 |
{ |
59 |
g_Asc_PrintVTable_Tcl.name = g_AscTclPrintName; |
60 |
g_Asc_PrintVTable_Tcl.print = Asc_PrintTcl; |
61 |
g_Asc_PrintVTable_Tcl.fflush = Asc_FFlushTcl; |
62 |
g_Asc_PrintVTable_Tcl.next = 0; |
63 |
return Asc_PrintPushVTable(&g_Asc_PrintVTable_Tcl); |
64 |
} |
65 |
|
66 |
int Asc_PrintInit_Tcl(void) |
67 |
{ |
68 |
|
69 |
/* |
70 |
* Initialize ASCEND's ideas of Tcl's idea of stdout and stderr. |
71 |
*/ |
72 |
g_tclout = Tcl_GetStdChannel( TCL_STDOUT ); |
73 |
g_tclerr = Tcl_GetStdChannel( TCL_STDERR ); |
74 |
|
75 |
if(( g_tclout == NULL ) || ( g_tclerr == NULL )) { |
76 |
return 1; |
77 |
} |
78 |
return 0; |
79 |
} |
80 |
|
81 |
void Asc_PrintFinalize_Tcl(void) |
82 |
{ |
83 |
Asc_PrintRemoveVTable(g_AscTclPrintName); |
84 |
} |
85 |
|
86 |
|
87 |
/* |
88 |
* int AscPrint(fp, format, args) |
89 |
* FILE *fp; |
90 |
* CONST char *format; |
91 |
* va_list args; |
92 |
* |
93 |
* Using the sprintf-style format string `format', print the arguments in |
94 |
* the va_list `args' to the file pointer `fp'. Return the number of |
95 |
* bytes printed. |
96 |
* |
97 |
* NOTE: The last argument to this function is a VA_LIST, NOT a variable |
98 |
* number of arguments. You must initialize the va_list before calling |
99 |
* this function, and cleanup the va_list afterwards. |
100 |
*/ |
101 |
static |
102 |
int Asc_PrintTcl(FILE *fp, CONST char *format, va_list args) |
103 |
{ |
104 |
static char buf[PRINT_BUFFER_SIZE]; /* the buffer that holds the output */ |
105 |
|
106 |
if(( fp == stdout ) && ( g_tclout != NULL )) { |
107 |
vsprintf( buf, format, args ); |
108 |
return Tcl_Write( g_tclout, buf, -1 ); |
109 |
} |
110 |
else if(( fp == stderr ) && ( g_tclerr != NULL )) { |
111 |
vsprintf( buf, format, args ); |
112 |
return Tcl_Write( g_tclerr, buf, -1 ); |
113 |
} |
114 |
/* TODO: should this fail loudly when a NULL fp is passed in? */ |
115 |
else if (fp != NULL) { |
116 |
return vfprintf( fp, format, args ); |
117 |
} |
118 |
else { |
119 |
return 0; |
120 |
} |
121 |
} |
122 |
|
123 |
|
124 |
/* |
125 |
* int Asc_FFlushTcl(fileptr) |
126 |
* FILE *fileptr; |
127 |
* |
128 |
* Flush output to the file pointed to by the file pointer `fileptr'; |
129 |
* return 0 for success and EOF for failure. |
130 |
* |
131 |
* This is needed for consistency with Asc_FPrintf() and Asc_Printf(). |
132 |
*/ |
133 |
extern |
134 |
int Asc_FFlushTcl( FILE *fileptr ) |
135 |
{ |
136 |
if(( fileptr == stdout ) && ( g_tclout != NULL )) { |
137 |
if( Tcl_Flush( g_tclout ) != TCL_OK ) { |
138 |
return EOF; |
139 |
} |
140 |
return 0; |
141 |
} |
142 |
else if(( fileptr == stdout ) && ( g_tclerr )) { |
143 |
if( Tcl_Flush( g_tclerr ) != TCL_OK ) { |
144 |
return EOF; |
145 |
} |
146 |
return 0; |
147 |
} |
148 |
else { |
149 |
return fflush(fileptr); |
150 |
} |
151 |
} |
152 |
|
153 |
#endif /* USE_ASC_PRINTF */ |