comparison usr/src/cmd/fmli/qued/getfield.c @ 0:c9caec207d52 b86

Initial porting based on b86
author Koji Uno <koji.uno@sun.com>
date Tue, 02 Jun 2009 18:56:50 +0900
parents
children 1a15d5aaf794
comparison
equal deleted inserted replaced
-1:000000000000 0:c9caec207d52
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 1992 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29
30 #pragma ident "@(#)getfield.c 1.7 05/06/08 SMI"
31
32 #include <stdio.h>
33 #include <curses.h>
34 #include "token.h"
35 #include "wish.h"
36 #include "winp.h"
37 #include "fmacs.h"
38 #include "terror.h"
39
40 static char *getfixedval();
41 static char *getscrollval();
42
43 /*
44 * GETFIELD will return the contents of the current field. It is not
45 * stable as of yet and does not account for scrolling field.
46 */
47 char *
48 getfield(fld, buff)
49 ifield *fld;
50 char *buff;
51 {
52 register char *fbuf, *val;
53 ifield *savefield;
54
55 savefield = Cfld;
56 if (fld != NULL)
57 Cfld = fld;
58 if (Flags & I_INVISIBLE) {
59 if (Value == NULL)
60 val = nil;
61 else
62 val = Value;
63 }
64 else if (Flags & I_SCROLL)
65 val = getscrollval();
66 else
67 val = getfixedval();
68 /*
69 * getscrollval() and getfixedval() return NULL from
70 * time to time - causing calling functions to segfault.
71 */
72 if (val == NULL)
73 val = nil;
74 if (buff == NULL)
75 fbuf = val;
76 else {
77 strcpy(buff, val);
78 fbuf = buff;
79 }
80 Cfld = savefield;
81 return(fbuf);
82 }
83
84 static char *
85 getfixedval()
86 {
87 register int row;
88 register char *bptr;
89
90 if (!(Flags & I_CHANGED))
91 return(Value);
92 Flags &= ~(I_CHANGED);
93
94 /*
95 * If this field does not already have a value then
96 * allocate space equal to the size of the field dimensions
97 * (Buffer is guarenteed to be at least this size if there
98 * already is a field value)
99 */
100 if (!Value && (Value = malloc(FIELDBYTES +1)) == NULL) /* +1 abs k15 */
101 fatal(NOMEM, nil);
102
103 /*
104 * Read the field value from the window map and eat
105 * trailing new-line characters
106 */
107 for (bptr = Value, row = 0; row <= LASTROW; row++) {
108 bptr += freadline(row, bptr, TRUE);
109 *bptr++ = '\n';
110 }
111 while (--bptr >= Value && *bptr == '\n')
112 *bptr = '\0';
113 return(Value);
114 }
115
116 static char *
117 getscrollval()
118 {
119 register char *dptr;
120 register chtype *sptr, *lastptr, *saveptr;
121 unsigned buflength, lenvalptr;
122 char *dest;
123
124 if (!(Flags & I_CHANGED))
125 return(Value);
126 Flags &= ~(I_CHANGED);
127 /*
128 * HORIZONTAL SCROLL FIELD
129 *
130 * - syncronize the window map with the scroll buffer.
131 * - set Value to the result
132 *
133 */
134 if (Currtype == SINGLE) {
135 syncbuf(Buffoffset, 0, 0); /* syncronize buffer */
136 if ((dest = malloc(Buffsize + 1)) == NULL)
137 fatal(NOMEM, nil);
138 dptr = dest;
139 sptr = Scrollbuf;
140 while ((*dptr++ = (char)(*(sptr++) & A_CHARTEXT)) != '\0')
141 ;
142 if (Value)
143 free(Value);
144 Value = dest;
145 return(Value);
146 }
147
148 /*
149 * VERTICAL SCROLL FIELD
150 *
151 * - syncronize the window map with the scroll buffer.
152 * - "pack" the scoll buffer (remove trailing blanks).
153 * - append the remaining (unprocessed) text pointed to by Valptr.
154 * - eat trailing new-lines
155 * - set Value to the result.
156 *
157 */
158 syncbuf(Buffoffset, 0, Fieldrows - 1); /* syncronize buffer */
159 if ((dest = malloc(Buffsize + 1)) == NULL)
160 fatal(NOMEM, nil);
161 lastptr = Scrollbuf + Bufflast;
162 saveptr = sptr = Scrollbuf;
163 dptr = dest;
164 while (sptr < lastptr) { /* pack Scrollbuf */
165 if ((*dptr++ = (char)(*(sptr++) & A_CHARTEXT)) == '\0') {
166 saveptr += LINEBYTES;
167 sptr = saveptr;
168 *(dptr - 1) = '\n';
169 }
170 }
171 *dptr = '\0';
172
173 buflength = strlen(dest);
174 if (Valptr) { /* append unprocessed text */
175 lenvalptr = strlen(Valptr);
176 if ((dest = realloc(dest, buflength + lenvalptr + 1)) == NULL)
177 fatal(NOMEM, nil);
178 strcat(dest, Valptr);
179 Valptr = dest + buflength;
180 buflength += lenvalptr;
181 }
182 if (Value)
183 free(Value);
184 Value = dest;
185 for (dptr = dest + buflength - 1; --dptr >= dest && *dptr == '\n'; )
186 *dptr = '\0'; /* eat trailing new-lines */
187 return(Value);
188 }
189