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 |
Asc_PrintPushVTable(&g_Asc_PrintVTable_Tcl); |
66 |
return 0; |
67 |
} |
68 |
|
69 |
int Asc_PrintInit_Tcl(void) |
70 |
{ |
71 |
|
72 |
/* |
73 |
* Initialize ASCEND's ideas of Tcl's idea of stdout and stderr. |
74 |
*/ |
75 |
g_tclout = Tcl_GetStdChannel( TCL_STDOUT ); |
76 |
g_tclerr = Tcl_GetStdChannel( TCL_STDERR ); |
77 |
|
78 |
if(( g_tclout == NULL ) || ( g_tclerr == NULL )) { |
79 |
return 1; |
80 |
} |
81 |
return 0; |
82 |
} |
83 |
|
84 |
void Asc_PrintFinalize_Tcl(void) |
85 |
{ |
86 |
Asc_PrintRemoveVTable(g_AscTclPrintName); |
87 |
} |
88 |
|
89 |
|
90 |
/* |
91 |
* int AscPrint(fp, format, args) |
92 |
* FILE *fp; |
93 |
* CONST char *format; |
94 |
* va_list args; |
95 |
* |
96 |
* Using the sprintf-style format string `format', print the arguments in |
97 |
* the va_list `args' to the file pointer `fp'. Return the number of |
98 |
* bytes printed. |
99 |
* |
100 |
* NOTE: The last argument to this function is a VA_LIST, NOT a variable |
101 |
* number of arguments. You must initialize the va_list before calling |
102 |
* this function, and cleanup the va_list afterwards. |
103 |
*/ |
104 |
static |
105 |
int Asc_PrintTcl(FILE *fp, CONST char *format, va_list args) |
106 |
{ |
107 |
static char buf[PRINT_BUFFER_SIZE]; /* the buffer that holds the output */ |
108 |
|
109 |
if(( fp == stdout ) && ( g_tclout != NULL )) { |
110 |
vsprintf( buf, format, args ); |
111 |
return Tcl_Write( g_tclout, buf, -1 ); |
112 |
} |
113 |
else if(( fp == stderr ) && ( g_tclerr != NULL )) { |
114 |
vsprintf( buf, format, args ); |
115 |
return Tcl_Write( g_tclerr, buf, -1 ); |
116 |
} |
117 |
/* TODO: should this fail loudly when a NULL fp is passed in? */ |
118 |
else if (fp != NULL) { |
119 |
return vfprintf( fp, format, args ); |
120 |
} |
121 |
else { |
122 |
return 0; |
123 |
} |
124 |
} |
125 |
|
126 |
|
127 |
/* |
128 |
* int Asc_FFlushTcl(fileptr) |
129 |
* FILE *fileptr; |
130 |
* |
131 |
* Flush output to the file pointed to by the file pointer `fileptr'; |
132 |
* return 0 for success and EOF for failure. |
133 |
* |
134 |
* This is needed for consistency with Asc_FPrintf() and Asc_Printf(). |
135 |
*/ |
136 |
extern |
137 |
int Asc_FFlushTcl( FILE *fileptr ) |
138 |
{ |
139 |
if(( fileptr == stdout ) && ( g_tclout != NULL )) { |
140 |
if( Tcl_Flush( g_tclout ) != TCL_OK ) { |
141 |
return EOF; |
142 |
} |
143 |
return 0; |
144 |
} |
145 |
else if(( fileptr == stdout ) && ( g_tclerr )) { |
146 |
if( Tcl_Flush( g_tclerr ) != TCL_OK ) { |
147 |
return EOF; |
148 |
} |
149 |
return 0; |
150 |
} |
151 |
else { |
152 |
return fflush(fileptr); |
153 |
} |
154 |
} |
155 |
|
156 |
#endif /* PRINTF == Asc_Printf */ |