Annotation of OpenXM_contrib2/asir2000/parse/stdio.c, Revision 1.7
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.7 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/stdio.c,v 1.6 2001/10/09 01:36:25 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;
1.7 ! noro 64: extern int emergency;
1.1 noro 65:
66: #define INBUF_EMPTY (W_BUF.inpos >= W_BUF.inlen)
1.4 noro 67:
68: void w_purge_stdin(void)
69: {
70: W_BUF.inpos = 0;
71: W_BUF.inlen = 0;
72: }
1.1 noro 73:
74: int w_filbuf(void)
75: {
76: int status = get_line(W_BUF.inbuf);
1.7 ! noro 77:
! 78: /* XXX */
! 79: if ( emergency ) return 0;
! 80:
1.1 noro 81: W_BUF.inpos = 0;
82: W_BUF.inlen = strlen(W_BUF.inbuf);
83: return status;
84: }
85:
86: int w_flushbuf(void) {
1.7 ! noro 87: /* XXX */
! 88: if ( emergency ) return 0;
! 89:
1.1 noro 90: W_BUF.outbuf[W_BUF.outpos] = 0;
91: W_BUF.outpos = 0;
92: put_line(W_BUF.outbuf);
93: return 0;
94: }
95:
96: int w_fflush(FILE *file)
97: {
98: if ( file == stdout || file == stderr )
99: return w_flushbuf();
100: else
101: return fflush(file);
102: }
103:
104: void w_noflush_stderr(int flag)
105: {
106: noflush = flag;
107: }
108:
109: int get_char(void)
110: {
111: int status = 0;
112:
113: if ( INBUF_EMPTY )
114: status = w_filbuf();
115: if ( status )
116: return EOF;
117: else
118: return (int)W_BUF.inbuf[W_BUF.inpos++];
119: }
120:
121: char *get_string(char *s, int n)
122: {
123: int i;
124: char *p;
125: int status = 0;
126:
127: if ( INBUF_EMPTY )
128: status = w_filbuf();
129: if ( status )
130: return NULL;
131: for ( i = 0, p = s; i < n && !INBUF_EMPTY && W_BUF.inbuf[i]; i++ )
132: *p++ = W_BUF.inbuf[W_BUF.inpos++];
133: if ( i < n )
134: *p = 0;
135: return s;
136: }
137:
138: int w_fgetc(FILE *file)
139: {
140: if ( file == stdin )
141: return get_char();
142: else
143: return fgetc(file);
144: }
145:
146: int unget_char(int c)
147: {
148: W_BUF.inbuf[--W_BUF.inpos] = (char )c;
149: return c;
150: }
151:
1.6 noro 152: int win_put_string(char *s,FILE *file);
153:
1.5 noro 154: int win_put_char(int c,FILE *file)
1.1 noro 155: {
156: static char s[2];
157:
158: s[0] = (char)c;
1.5 noro 159: win_put_string(s,file);
1.1 noro 160: return c;
161: }
162:
1.5 noro 163: int win_put_string(char *s,FILE *file)
1.1 noro 164: {
165: char c;
166:
167: while ( 1 ) {
168: for ( ; (c = *s) && W_BUF.outpos < BUFSIZ; s++ ) {
169: W_BUF.outbuf[W_BUF.outpos++] = c;
170: if ( c == '\n' ) {
171: W_BUF.outbuf[W_BUF.outpos-1] = '\r';
172: W_BUF.outbuf[W_BUF.outpos++] = '\n';
173: w_flushbuf();
174: }
175: }
176: if ( !c )
177: break;
178: else if ( W_BUF.outpos == BUFSIZ )
179: w_flushbuf();
180: }
181: if (file == stderr && !noflush)
182: w_flushbuf();
183: return 0;
184: }
185:
186: char *w_fgets(char *s,int n, FILE *file)
187: {
188: if ( file == stdin )
189: return get_string(s,n);
190: else
191: return fgets(s,n,file);
192: }
193:
194: int w_ungetc(int c, FILE *file)
195: {
196: if ( file == stdin )
197: return unget_char(c);
198: else
199: return ungetc(c,file);
200: }
201:
202: int w_fputc(int c, FILE *file)
203: {
204: if ( file == stdout || file == stderr )
1.5 noro 205: return win_put_char(c,file);
1.1 noro 206: else
207: return fputc(c,file);
208: }
209:
210: int w_fputs(char *s, FILE *file)
211: {
212: if ( file == stdout || file == stderr )
1.5 noro 213: return win_put_string(s,file);
1.1 noro 214: else
215: return fputs(s,file);
216: }
217:
218: int w_printf(char *format, ...)
219: {
220: va_list ap;
221: int status;
222: char buf[BUFSIZ];
223:
224: va_start(ap,format);
225: status = vsprintf(buf,format,ap);
1.5 noro 226: win_put_string(buf,stdout);
1.1 noro 227: return status;
228: }
229:
230: int w_fprintf(FILE *file, char *format, ...)
231: {
232: va_list ap;
233: int status;
234: char buf[BUFSIZ];
235:
236: va_start(ap,format);
237: if ( file == stdout || file == stderr ) {
238: status = vsprintf(buf,format,ap);
1.5 noro 239: win_put_string(buf,file);
1.1 noro 240: } else {
241: status = vfprintf(file,format,ap);
242: }
243: va_end(ap);
244: return status;
245: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>