OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [expect/] [exp_log.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/* exp_log.c - logging routines and other things common to both Expect
2
   program and library.  Note that this file must NOT have any
3
   references to Tcl except for including tclInt.h
4
*/
5
 
6
#include "expect_cf.h"
7
#include <stdio.h>
8
/*#include <varargs.h>          tclInt.h drags in varargs.h.  Since Pyramid */
9
/*                              objects to including varargs.h twice, just */
10
/*                              omit this one. */
11
#include "tclInt.h"
12
#include "expect_comm.h"
13
#include "exp_int.h"
14
#include "exp_rename.h"
15
#include "exp_log.h"
16
 
17
int loguser = TRUE;             /* if TRUE, expect/spawn may write to stdout */
18
int logfile_all = FALSE;        /* if TRUE, write log of all interactions */
19
                                /* despite value of loguser. */
20
FILE *logfile = 0;
21
FILE *debugfile = 0;
22
int exp_is_debugging = FALSE;
23
 
24
/* Following this are several functions that log the conversation. */
25
/* Most of them have multiple calls to printf-style functions.  */
26
/* At first glance, it seems stupid to reformat the same arguments again */
27
/* but we have no way of telling how long the formatted output will be */
28
/* and hence cannot allocate a buffer to do so. */
29
/* Fortunately, in production code, most of the duplicate reformatting */
30
/* will be skipped, since it is due to handling errors and debugging. */
31
 
32
/* send to log if open */
33
/* send to stderr if debugging enabled */
34
/* use this for logging everything but the parent/child conversation */
35
/* (this turns out to be almost nothing) */
36
/* uppercase L differentiates if from math function of same name */
37
#define LOGUSER         (loguser || force_stdout)
38
/*VARARGS*/
39
void
40
exp_log TCL_VARARGS_DEF(int,arg1)
41
/*exp_log(va_alist)*/
42
/*va_dcl*/
43
{
44
        int force_stdout;
45
        char *fmt;
46
        va_list args;
47
 
48
        force_stdout = TCL_VARARGS_START(int,arg1,args);
49
        /*va_start(args);*/
50
        /*force_stdout = va_arg(args,int);*/
51
        fmt = va_arg(args,char *);
52
        if (debugfile) vfprintf(debugfile,fmt,args);
53
        if (logfile_all || (LOGUSER && logfile)) vfprintf(logfile,fmt,args);
54
        if (LOGUSER) vfprintf(stdout,fmt,args);
55
        va_end(args);
56
}
57
 
58
/* just like log but does no formatting */
59
/* send to log if open */
60
/* use this function for logging the parent/child conversation */
61
void
62
exp_nflog(buf,force_stdout)
63
char *buf;
64
int force_stdout;       /* override value of loguser */
65
{
66
        int length = strlen(buf);
67
 
68
        if (debugfile) fwrite(buf,1,length,debugfile);
69
        if (logfile_all || (LOGUSER && logfile)) fwrite(buf,1,length,logfile);
70
        if (LOGUSER) fwrite(buf,1,length,stdout);
71
#if 0
72
        if (logfile_all || (LOGUSER && logfile)) {
73
                int newlength = exp_copy_out(length);
74
                fwrite(exp_out_buffer,1,newlength,logfile);
75
        }
76
#endif
77
}
78
#undef LOGUSER
79
 
80
/* send to log if open and debugging enabled */
81
/* send to stderr if debugging enabled */
82
/* use this function for recording unusual things in the log */
83
/*VARARGS*/
84
void
85
debuglog TCL_VARARGS_DEF(char *,arg1)
86
/*debuglog(va_alist)*/
87
/*va_dcl*/
88
{
89
        char *fmt;
90
        va_list args;
91
 
92
        fmt = TCL_VARARGS_START(char *,arg1,args);
93
        /*va_start(args);*/
94
        /*fmt = va_arg(args,char *);*/
95
        if (debugfile) vfprintf(debugfile,fmt,args);
96
        if (is_debugging) {
97
                vfprintf(stderr,fmt,args);
98
                if (logfile) vfprintf(logfile,fmt,args);
99
        }
100
 
101
        va_end(args);
102
}
103
 
104
/* send to log if open */
105
/* send to stderr */
106
/* use this function for error conditions */
107
/*VARARGS*/
108
void
109
exp_errorlog TCL_VARARGS_DEF(char *,arg1)
110
/*exp_errorlog(va_alist)*/
111
/*va_dcl*/
112
{
113
        char *fmt;
114
        va_list args;
115
 
116
        fmt = TCL_VARARGS_START(char *,arg1,args);
117
        /*va_start(args);*/
118
        /*fmt = va_arg(args,char *);*/
119
        vfprintf(stderr,fmt,args);
120
        if (debugfile) vfprintf(debugfile,fmt,args);
121
        if (logfile) vfprintf(logfile,fmt,args);
122
        va_end(args);
123
}
124
 
125
/* just like errorlog but does no formatting */
126
/* send to log if open */
127
/* use this function for logging the parent/child conversation */
128
/*ARGSUSED*/
129
void
130
exp_nferrorlog(buf,force_stdout)
131
char *buf;
132
int force_stdout;       /* not used, only declared here for compat with */
133
                        /* exp_nflog() */
134
{
135
        int length = strlen(buf);
136
        fwrite(buf,1,length,stderr);
137
        if (debugfile) fwrite(buf,1,length,debugfile);
138
        if (logfile) fwrite(buf,1,length,logfile);
139
}
140
 
141
#if 0
142
static int out_buffer_size;
143
static char *outp_last;
144
static char *out_buffer;
145
static char *outp;      /* pointer into out_buffer - static in order */
146
                        /* to update whenever out_buffer is enlarged */
147
 
148
 
149
void
150
exp_init_log()
151
{
152
        out_buffer = ckalloc(BUFSIZ);
153
        out_buffer_size = BUFSIZ;
154
        outp_last = out_buffer + BUFSIZ - 1;
155
}
156
 
157
char *
158
enlarge_out_buffer()
159
{
160
        int offset = outp - out_buffer;
161
 
162
        int new_out_buffer_size = out_buffer_size = BUFSIZ;
163
        realloc(out_buffer,new_out_buffer_size);
164
 
165
        out_buffer_size = new_out_buffer_size;
166
        outp = out_buffer + offset;
167
 
168
        outp_last = out_buffer + out_buffer_size - 1;
169
 
170
        return(out_buffer);
171
}
172
 
173
/* like sprintf, but uses a static buffer enlarged as necessary */
174
/* currently supported are %s, %d, and %#d where # is a single-digit */
175
void
176
exp_sprintf TCL_VARARGS_DEF(char *,arg1)
177
/* exp_sprintf(va_alist)*/
178
/*va_dcl*/
179
{
180
        char *fmt;
181
        va_list args;
182
        char int_literal[20];   /* big enough for an int literal? */
183
        char *int_litp;         /* pointer into int_literal */
184
        char *width;
185
        char *string_arg;
186
        int int_arg;
187
        char *int_fmt;
188
 
189
        fmt = TCL_VARARGS_START(char *,arg1,args);
190
        /*va_start(args);*/
191
        /*fmt = va_arg(args,char *);*/
192
 
193
        while (*fmt != '\0') {
194
                if (*fmt != '%') {
195
                        *outp++ = *fmt++;
196
                        continue;
197
                }
198
 
199
                /* currently, only single-digit widths are used */
200
                if (isdigit(*fmt)) {
201
                        width = fmt++;
202
                } else width = 0;
203
 
204
                switch (*fmt) {
205
                case 's':       /* interpolate string */
206
                        string_arg = va_arg(args,char *);
207
 
208
                        while (*string_arg) {
209
                                if (outp == outp_last) {
210
                                        if (enlarge_out_buffer() == 0) {
211
                                                /* FAIL */
212
                                                return;
213
                                        }
214
                                }
215
                                *outp++ = *string_arg++;
216
                        }
217
                        fmt++;
218
                        break;
219
                case 'd':       /* interpolate int */
220
                        int_arg = va_arg(args,int);
221
 
222
                        if (width) int_fmt = width;
223
                        else int_fmt = fmt;
224
 
225
                        sprintf(int_literal,int_fmt,int_arg);
226
 
227
                        int_litp = int_literal;
228
                        for (int_litp;*int_litp;) {
229
                                if (enlarge_out_buffer() == 0) return;
230
                                *outp++ = *int_litp++;
231
                        }
232
                        fmt++;
233
                        break;
234
                default:        /* anything else is literal */
235
                        if (enlarge_out_buffer() == 0) return;   /* FAIL */
236
                        *outp++ = *fmt++;
237
                        break;
238
                }
239
        }
240
}
241
 
242
/* copy input string to exp_output, replacing \r\n sequences by \n */
243
/* return length of new string */
244
int
245
exp_copy_out(char *s)
246
{
247
        outp = out_buffer;
248
        int count = 0;
249
 
250
        while (*s) {
251
                if ((*s == '\r') && (*(s+1) =='\n')) s++;
252
                if (enlarge_out_buffer() == 0) {
253
                        /* FAIL */
254
                        break;
255
                }
256
                *outp = *s;
257
                count++;
258
        }
259
        return count;
260
}
261
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.