xref: /original-bsd/lib/libcurses/scanw.c (revision 56c13d2e)
1 /*
2  * Copyright (c) 1981 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)scanw.c	5.6 (Berkeley) 03/12/91";
10 #endif /* not lint */
11 
12 /*
13  * scanw and friends
14  *
15  */
16 
17 # include	"curses.ext"
18 # include	"stdarg.h"
19 
20 /*
21  *	This routine implements a scanf on the standard screen.
22  */
23 scanw(fmt, args)
24 char	*fmt;
25 int	args; {
26 
27 	return _sscans(stdscr, fmt, &args);
28 }
29 
30 /*
31  *	This routine implements a scanf on the given window.
32  */
33 wscanw(win, fmt, args)
34 WINDOW	*win;
35 char	*fmt;
36 int	args; {
37 
38 	return _sscans(win, fmt, &args);
39 }
40 
41 /*
42  *	Internal routine to read from a string, and its data structure.
43  */
44 struct strinfo {
45 	char	*addr;		/* address */
46 	int	len;		/* remaining bytes */
47 };
48 
49 static int
50 _winread(cookie, buf, n)
51 char	*cookie, *buf;
52 reg int	n; {
53 
54 	reg struct strinfo *s = (struct strinfo *)cookie;
55 
56 	if (n > s->len)
57 		n = s->len;
58 	bcopy(s->addr, buf, n);
59 	s->len -= n;
60 	s->addr += n;
61 	return n;
62 }
63 
64 /*
65  *	This routine actually executes the scanf from the window.
66  *	SHOULD IMPLEMENT VSSCANF
67  */
68 _sscans(win, fmt)
69 WINDOW	*win;
70 char	*fmt; {
71 
72 	va_list ap;
73 	int	ret;
74 	FILE	*f;
75 	struct	strinfo s;
76 	char	buf[100];
77 
78 	if ((f = fropen((char *)&s, _winread)) == NULL)
79 		return ERR;
80 	if (wgetstr(win, buf) == ERR) {
81 		(void) fclose(f);
82 		return ERR;
83 	}
84 	s.addr = buf;
85 	s.len = strlen(buf);
86 	va_start(ap, fmt);
87 	ret = __svfscanf(f, fmt, ap);
88 	va_end(ap);
89 	(void) fclose(f);
90 	return ret;
91 }
92