Annotation of OpenXM_contrib2/asir2000/parse/stdio.c, Revision 1.5
1.2 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.3 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.5 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/stdio.c,v 1.4 2000/11/13 01:48:13 noro Exp $
1.2 noro 49: */
1.1 noro 50: #include <stdio.h>
51: #include <stdlib.h>
52: #include <string.h>
53: #include <stdarg.h>
54: #include "w_stdio.h"
55:
56: static struct w_buf {
57: int inlen, inpos;
58: int outpos;
59: char inbuf[2*BUFSIZ];
60: char outbuf[2*BUFSIZ];
61: } W_BUF;
62:
63: static int noflush;
64:
65: #define INBUF_EMPTY (W_BUF.inpos >= W_BUF.inlen)
1.4 noro 66:
67: void w_purge_stdin(void)
68: {
69: W_BUF.inpos = 0;
70: W_BUF.inlen = 0;
71: }
1.1 noro 72:
73: int w_filbuf(void)
74: {
75: int status = get_line(W_BUF.inbuf);
76: W_BUF.inpos = 0;
77: W_BUF.inlen = strlen(W_BUF.inbuf);
78: return status;
79: }
80:
81: int w_flushbuf(void) {
82: W_BUF.outbuf[W_BUF.outpos] = 0;
83: W_BUF.outpos = 0;
84: put_line(W_BUF.outbuf);
85: return 0;
86: }
87:
88: int w_fflush(FILE *file)
89: {
90: if ( file == stdout || file == stderr )
91: return w_flushbuf();
92: else
93: return fflush(file);
94: }
95:
96: void w_noflush_stderr(int flag)
97: {
98: noflush = flag;
99: }
100:
101: int get_char(void)
102: {
103: int status = 0;
104:
105: if ( INBUF_EMPTY )
106: status = w_filbuf();
107: if ( status )
108: return EOF;
109: else
110: return (int)W_BUF.inbuf[W_BUF.inpos++];
111: }
112:
113: char *get_string(char *s, int n)
114: {
115: int i;
116: char *p;
117: int status = 0;
118:
119: if ( INBUF_EMPTY )
120: status = w_filbuf();
121: if ( status )
122: return NULL;
123: for ( i = 0, p = s; i < n && !INBUF_EMPTY && W_BUF.inbuf[i]; i++ )
124: *p++ = W_BUF.inbuf[W_BUF.inpos++];
125: if ( i < n )
126: *p = 0;
127: return s;
128: }
129:
130: int w_fgetc(FILE *file)
131: {
132: if ( file == stdin )
133: return get_char();
134: else
135: return fgetc(file);
136: }
137:
138: int unget_char(int c)
139: {
140: W_BUF.inbuf[--W_BUF.inpos] = (char )c;
141: return c;
142: }
143:
1.5 ! noro 144: int win_put_char(int c,FILE *file)
1.1 noro 145: {
146: static char s[2];
147:
148: s[0] = (char)c;
1.5 ! noro 149: win_put_string(s,file);
1.1 noro 150: return c;
151: }
152:
1.5 ! noro 153: int win_put_string(char *s,FILE *file)
1.1 noro 154: {
155: char c;
156:
157: while ( 1 ) {
158: for ( ; (c = *s) && W_BUF.outpos < BUFSIZ; s++ ) {
159: W_BUF.outbuf[W_BUF.outpos++] = c;
160: if ( c == '\n' ) {
161: W_BUF.outbuf[W_BUF.outpos-1] = '\r';
162: W_BUF.outbuf[W_BUF.outpos++] = '\n';
163: w_flushbuf();
164: }
165: }
166: if ( !c )
167: break;
168: else if ( W_BUF.outpos == BUFSIZ )
169: w_flushbuf();
170: }
171: if (file == stderr && !noflush)
172: w_flushbuf();
173: return 0;
174: }
175:
176: char *w_fgets(char *s,int n, FILE *file)
177: {
178: if ( file == stdin )
179: return get_string(s,n);
180: else
181: return fgets(s,n,file);
182: }
183:
184: int w_ungetc(int c, FILE *file)
185: {
186: if ( file == stdin )
187: return unget_char(c);
188: else
189: return ungetc(c,file);
190: }
191:
192: int w_fputc(int c, FILE *file)
193: {
194: if ( file == stdout || file == stderr )
1.5 ! noro 195: return win_put_char(c,file);
1.1 noro 196: else
197: return fputc(c,file);
198: }
199:
200: int w_fputs(char *s, FILE *file)
201: {
202: int status;
203:
204: if ( file == stdout || file == stderr )
1.5 ! noro 205: return win_put_string(s,file);
1.1 noro 206: else
207: return fputs(s,file);
208: }
209:
210: int w_printf(char *format, ...)
211: {
212: va_list ap;
213: int status;
214: char buf[BUFSIZ];
215:
216: va_start(ap,format);
217: status = vsprintf(buf,format,ap);
1.5 ! noro 218: win_put_string(buf,stdout);
1.1 noro 219: return status;
220: }
221:
222: int w_fprintf(FILE *file, char *format, ...)
223: {
224: va_list ap;
225: int status;
226: char buf[BUFSIZ];
227:
228: va_start(ap,format);
229: if ( file == stdout || file == stderr ) {
230: status = vsprintf(buf,format,ap);
1.5 ! noro 231: win_put_string(buf,file);
1.1 noro 232: } else {
233: status = vfprintf(file,format,ap);
234: }
235: va_end(ap);
236: return status;
237: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>