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 |
/* |
31 |
* Only compile this file if we are using Asc_Printf() |
32 |
*/ |
33 |
#if PRINTF == Asc_PrintF |
34 |
|
35 |
|
36 |
#include <stdarg.h> |
37 |
#include "tcl.h" |
38 |
#include "utilities/ascConfig.h" |
39 |
#include "utilities/ascPrint.h" |
40 |
|
41 |
|
42 |
#define PRINT_BUFFER_SIZE 16380 |
43 |
|
44 |
|
45 |
/* |
46 |
* The Tcl channels for stdout and stderr. |
47 |
* |
48 |
* These need to be initialized by calling Asc_PrintInit() before |
49 |
* calls to Asc_Printf() will write to the Tcl Channels. |
50 |
*/ |
51 |
static Tcl_Channel g_tclout = NULL; /* the Tcl channel for stdout */ |
52 |
static Tcl_Channel g_tclerr = NULL; /* the Tcl channel for stderr */ |
53 |
|
54 |
static struct Asc_PrintVTable g_Asc_PrintVTable_Tcl; |
55 |
static int Asc_PrintTcl(FILE *fp, CONST char *format, va_list args); |
56 |
static int Asc_FFlushTcl(FILE *fp); |
57 |
static char *g_AscTclPrintName = "tclPrintChannels"; |
58 |
|
59 |
int Asc_PrintInit_TclVtable(void) |
60 |
{ |
61 |
g_Asc_PrintVTable_Tcl.name = g_AscTclPrintName; |
62 |
g_Asc_PrintVTable_Tcl.print = Asc_PrintTcl; |
63 |
g_Asc_PrintVTable_Tcl.fflush = Asc_FFlushTcl; |
64 |
g_Asc_PrintVTable_Tcl.next = 0; |
65 |
return Asc_PrintPushVTable(&g_Asc_PrintVTable_Tcl); |
66 |
} |
67 |
|
68 |
int Asc_PrintInit_Tcl(void) |
69 |
{ |
70 |
|
71 |
/* |
72 |
* Initialize ASCEND's ideas of Tcl's idea of stdout and stderr. |
73 |
*/ |
74 |
g_tclout = Tcl_GetStdChannel( TCL_STDOUT ); |
75 |
g_tclerr = Tcl_GetStdChannel( TCL_STDERR ); |
76 |
|
77 |
if(( g_tclout == NULL ) || ( g_tclerr == NULL )) { |
78 |
return 1; |
79 |
} |
80 |
return 0; |
81 |
} |
82 |
|
83 |
void Asc_PrintFinalize_Tcl(void) |
84 |
{ |
85 |
Asc_PrintRemoveVTable(g_AscTclPrintName); |
86 |
} |
87 |
|
88 |
|
89 |
/* |
90 |
* int AscPrint(fp, format, args) |
91 |
* FILE *fp; |
92 |
* CONST char *format; |
93 |
* va_list args; |
94 |
* |
95 |
* Using the sprintf-style format string `format', print the arguments in |
96 |
* the va_list `args' to the file pointer `fp'. Return the number of |
97 |
* bytes printed. |
98 |
* |
99 |
* NOTE: The last argument to this function is a VA_LIST, NOT a variable |
100 |
* number of arguments. You must initialize the va_list before calling |
101 |
* this function, and cleanup the va_list afterwards. |
102 |
*/ |
103 |
static |
104 |
int Asc_PrintTcl(FILE *fp, CONST char *format, va_list args) |
105 |
{ |
106 |
static char buf[PRINT_BUFFER_SIZE]; /* the buffer that holds the output */ |
107 |
|
108 |
if(( fp == stdout ) && ( g_tclout != NULL )) { |
109 |
vsprintf( buf, format, args ); |
110 |
return Tcl_Write( g_tclout, buf, -1 ); |
111 |
} |
112 |
else if(( fp == stderr ) && ( g_tclerr != NULL )) { |
113 |
vsprintf( buf, format, args ); |
114 |
return Tcl_Write( g_tclerr, buf, -1 ); |
115 |
} |
116 |
/* TODO: should this fail loudly when a NULL fp is passed in? */ |
117 |
else if (fp != NULL) { |
118 |
return vfprintf( fp, format, args ); |
119 |
} |
120 |
else { |
121 |
return 0; |
122 |
} |
123 |
} |
124 |
|
125 |
|
126 |
/* |
127 |
* int Asc_FFlushTcl(fileptr) |
128 |
* FILE *fileptr; |
129 |
* |
130 |
* Flush output to the file pointed to by the file pointer `fileptr'; |
131 |
* return 0 for success and EOF for failure. |
132 |
* |
133 |
* This is needed for consistency with Asc_FPrintf() and Asc_Printf(). |
134 |
*/ |
135 |
extern |
136 |
int Asc_FFlushTcl( FILE *fileptr ) |
137 |
{ |
138 |
if(( fileptr == stdout ) && ( g_tclout != NULL )) { |
139 |
if( Tcl_Flush( g_tclout ) != TCL_OK ) { |
140 |
return EOF; |
141 |
} |
142 |
return 0; |
143 |
} |
144 |
else if(( fileptr == stdout ) && ( g_tclerr )) { |
145 |
if( Tcl_Flush( g_tclerr ) != TCL_OK ) { |
146 |
return EOF; |
147 |
} |
148 |
return 0; |
149 |
} |
150 |
else { |
151 |
return fflush(fileptr); |
152 |
} |
153 |
} |
154 |
|
155 |
#endif /* PRINTF == Asc_Printf */ |