1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
23*0Sstevel@tonic-gate /* All Rights Reserved */
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate * Copyright (c) 1997, by Sun Microsystems, Inc.
28*0Sstevel@tonic-gate * All rights reserved.
29*0Sstevel@tonic-gate */
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.8 */
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate /*LINTLIBRARY*/
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate #include <stdlib.h>
37*0Sstevel@tonic-gate #include "utility.h"
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gate #define MAX_BUF 81
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate /* default form */
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate static FORM default_form =
44*0Sstevel@tonic-gate {
45*0Sstevel@tonic-gate 0, /* status */
46*0Sstevel@tonic-gate 0, /* rows */
47*0Sstevel@tonic-gate 0, /* cols */
48*0Sstevel@tonic-gate 0, /* currow */
49*0Sstevel@tonic-gate 0, /* curcol */
50*0Sstevel@tonic-gate 0, /* toprow */
51*0Sstevel@tonic-gate 0, /* begincol */
52*0Sstevel@tonic-gate -1, /* maxfield */
53*0Sstevel@tonic-gate -1, /* maxpage */
54*0Sstevel@tonic-gate -1, /* curpage */
55*0Sstevel@tonic-gate O_NL_OVERLOAD |
56*0Sstevel@tonic-gate O_BS_OVERLOAD, /* opts */
57*0Sstevel@tonic-gate (WINDOW *) 0, /* win */
58*0Sstevel@tonic-gate (WINDOW *) 0, /* sub */
59*0Sstevel@tonic-gate (WINDOW *) 0, /* w */
60*0Sstevel@tonic-gate (FIELD **) 0, /* field */
61*0Sstevel@tonic-gate (FIELD *) 0, /* current */
62*0Sstevel@tonic-gate (_PAGE *) 0, /* page */
63*0Sstevel@tonic-gate (char *) 0, /* usrptr */
64*0Sstevel@tonic-gate (PTF_void) 0, /* forminit */
65*0Sstevel@tonic-gate (PTF_void) 0, /* formterm */
66*0Sstevel@tonic-gate (PTF_void) 0, /* fieldinit */
67*0Sstevel@tonic-gate (PTF_void) 0, /* fieldterm */
68*0Sstevel@tonic-gate };
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate FORM * _DEFAULT_FORM = &default_form;
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate /*
73*0Sstevel@tonic-gate * insert - insert field f into sorted list pointed
74*0Sstevel@tonic-gate * to by head. return (possibly new) head of list.
75*0Sstevel@tonic-gate */
76*0Sstevel@tonic-gate static FIELD *
insert(FIELD * f,FIELD * head)77*0Sstevel@tonic-gate insert(FIELD *f, FIELD *head)
78*0Sstevel@tonic-gate {
79*0Sstevel@tonic-gate FIELD *p;
80*0Sstevel@tonic-gate FIELD *newhead;
81*0Sstevel@tonic-gate int frow, fcol;
82*0Sstevel@tonic-gate
83*0Sstevel@tonic-gate if (head) {
84*0Sstevel@tonic-gate p = newhead = head;
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate frow = f->frow;
87*0Sstevel@tonic-gate fcol = f->fcol;
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate while ((p->frow < frow) ||
90*0Sstevel@tonic-gate (p->frow == frow && p->fcol < fcol)) {
91*0Sstevel@tonic-gate p = p->snext;
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate if (p == head) {
94*0Sstevel@tonic-gate head = (FIELD *) 0;
95*0Sstevel@tonic-gate break;
96*0Sstevel@tonic-gate }
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate f->snext = p;
99*0Sstevel@tonic-gate f->sprev = p->sprev;
100*0Sstevel@tonic-gate f->snext->sprev = f;
101*0Sstevel@tonic-gate f->sprev->snext = f;
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate if (p == head)
104*0Sstevel@tonic-gate newhead = f; /* insert at head of list */
105*0Sstevel@tonic-gate } else
106*0Sstevel@tonic-gate newhead = f->sprev = f->snext = f; /* initialize new list */
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate return (newhead);
109*0Sstevel@tonic-gate }
110*0Sstevel@tonic-gate
111*0Sstevel@tonic-gate /* sort_form - sort fields on form(per page) */
112*0Sstevel@tonic-gate static void
sort_form(FORM * f)113*0Sstevel@tonic-gate sort_form(FORM *f)
114*0Sstevel@tonic-gate {
115*0Sstevel@tonic-gate FIELD **field;
116*0Sstevel@tonic-gate FIELD *p;
117*0Sstevel@tonic-gate int i, page, pmin, pmax;
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate field = f->field;
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate for (page = 0; page < f->maxpage; ++page) { /* for each page */
122*0Sstevel@tonic-gate p = (FIELD *) 0;
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate pmin = Pmin(f, page);
125*0Sstevel@tonic-gate pmax = Pmax(f, page);
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate for (i = pmin; i <= pmax; ++i) { /* for each field */
128*0Sstevel@tonic-gate field[i]->index = i;
129*0Sstevel@tonic-gate field[i]->page = page;
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate p = insert(field[i], p);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate Smin(f, page) = p->index; /* set sorted min */
134*0Sstevel@tonic-gate Smax(f, page) = p->sprev->index; /* set sorted max */
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate /* merge - xmax/ymax is the minimum window size to hold field f */
139*0Sstevel@tonic-gate static void
merge(FIELD * f,FORM * form)140*0Sstevel@tonic-gate merge(FIELD *f, FORM *form) /* adjust form dimensions to include field f */
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate int xmax = f->fcol + f->cols;
143*0Sstevel@tonic-gate int ymax = f->frow + f->rows;
144*0Sstevel@tonic-gate
145*0Sstevel@tonic-gate if (form->rows < ymax)
146*0Sstevel@tonic-gate form->rows = ymax;
147*0Sstevel@tonic-gate if (form->cols < xmax)
148*0Sstevel@tonic-gate form->cols = xmax;
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate /* disconnect_fields - disconnect fields from form */
152*0Sstevel@tonic-gate static void
disconnect_fields(FORM * form)153*0Sstevel@tonic-gate disconnect_fields(FORM *form)
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate FIELD **f = form->field;
156*0Sstevel@tonic-gate
157*0Sstevel@tonic-gate if (f)
158*0Sstevel@tonic-gate while (*f) {
159*0Sstevel@tonic-gate if ((*f)->form == form)
160*0Sstevel@tonic-gate (*f)->form = (FORM *) 0;
161*0Sstevel@tonic-gate ++f;
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gate form->rows = 0;
165*0Sstevel@tonic-gate form->cols = 0;
166*0Sstevel@tonic-gate form->maxfield = -1;
167*0Sstevel@tonic-gate form->maxpage = -1;
168*0Sstevel@tonic-gate form->field = (FIELD **) 0;
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate /* connect_fields - connect fields to form */
172*0Sstevel@tonic-gate static int
connect_fields(FORM * f,FIELD ** x)173*0Sstevel@tonic-gate connect_fields(FORM *f, FIELD **x)
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate _PAGE * page;
176*0Sstevel@tonic-gate
177*0Sstevel@tonic-gate int nf, /* number of fields */
178*0Sstevel@tonic-gate np; /* number of pages */
179*0Sstevel@tonic-gate int i;
180*0Sstevel@tonic-gate
181*0Sstevel@tonic-gate f->field = x;
182*0Sstevel@tonic-gate f->maxfield = 0;
183*0Sstevel@tonic-gate f->maxpage = 0;
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate if (!x)
186*0Sstevel@tonic-gate return (E_OK); /* null field array */
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate for (nf = 0, np = 0; x[nf]; ++nf) {
189*0Sstevel@tonic-gate if (nf == 0 || Status(x[nf], NEW_PAGE))
190*0Sstevel@tonic-gate ++np; /* count pages */
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gate if (x[nf]->form)
193*0Sstevel@tonic-gate return (E_CONNECTED);
194*0Sstevel@tonic-gate else
195*0Sstevel@tonic-gate x[nf]->form = f; /* connect field to form */
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate if (nf == 0)
198*0Sstevel@tonic-gate return (E_BAD_ARGUMENT); /* no fields */
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate if (arrayAlloc(f->page, np, _PAGE)) {
201*0Sstevel@tonic-gate page = f->page;
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gate for (i = 0; i < nf; ++i) {
204*0Sstevel@tonic-gate if (i == 0)
205*0Sstevel@tonic-gate page->pmin = i;
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate else if (Status(x[i], NEW_PAGE)) {
208*0Sstevel@tonic-gate page->pmax = i - 1;
209*0Sstevel@tonic-gate ++page;
210*0Sstevel@tonic-gate page->pmin = i;
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate merge(x[i], f);
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate page->pmax = nf - 1;
215*0Sstevel@tonic-gate f->maxfield = nf;
216*0Sstevel@tonic-gate f->maxpage = np;
217*0Sstevel@tonic-gate sort_form(f);
218*0Sstevel@tonic-gate return (E_OK);
219*0Sstevel@tonic-gate }
220*0Sstevel@tonic-gate return (E_SYSTEM_ERROR);
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate
223*0Sstevel@tonic-gate FORM *
new_form(FIELD ** field)224*0Sstevel@tonic-gate new_form(FIELD **field)
225*0Sstevel@tonic-gate {
226*0Sstevel@tonic-gate FORM *f;
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gate if (Alloc(f, FORM)) {
229*0Sstevel@tonic-gate *f = *_DEFAULT_FORM;
230*0Sstevel@tonic-gate
231*0Sstevel@tonic-gate if (connect_fields(f, field) == E_OK) {
232*0Sstevel@tonic-gate if (f->maxpage) {
233*0Sstevel@tonic-gate P(f) = 0;
234*0Sstevel@tonic-gate C(f) = _first_active(f);
235*0Sstevel@tonic-gate } else {
236*0Sstevel@tonic-gate P(f) = -1;
237*0Sstevel@tonic-gate C(f) = (FIELD *) 0;
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate return (f);
240*0Sstevel@tonic-gate }
241*0Sstevel@tonic-gate }
242*0Sstevel@tonic-gate (void) free_form(f);
243*0Sstevel@tonic-gate return ((FORM *) 0);
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate
246*0Sstevel@tonic-gate int
free_form(FORM * f)247*0Sstevel@tonic-gate free_form(FORM *f)
248*0Sstevel@tonic-gate {
249*0Sstevel@tonic-gate if (!f)
250*0Sstevel@tonic-gate return (E_BAD_ARGUMENT);
251*0Sstevel@tonic-gate
252*0Sstevel@tonic-gate if (Status(f, POSTED))
253*0Sstevel@tonic-gate return (E_POSTED);
254*0Sstevel@tonic-gate
255*0Sstevel@tonic-gate disconnect_fields(f);
256*0Sstevel@tonic-gate Free(f->page);
257*0Sstevel@tonic-gate Free(f);
258*0Sstevel@tonic-gate return (E_OK);
259*0Sstevel@tonic-gate }
260*0Sstevel@tonic-gate
261*0Sstevel@tonic-gate int
set_form_fields(FORM * f,FIELD ** fields)262*0Sstevel@tonic-gate set_form_fields(FORM *f, FIELD **fields)
263*0Sstevel@tonic-gate {
264*0Sstevel@tonic-gate FIELD **p;
265*0Sstevel@tonic-gate int v;
266*0Sstevel@tonic-gate
267*0Sstevel@tonic-gate if (!f)
268*0Sstevel@tonic-gate return (E_BAD_ARGUMENT);
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate if (Status(f, POSTED))
271*0Sstevel@tonic-gate return (E_POSTED);
272*0Sstevel@tonic-gate
273*0Sstevel@tonic-gate p = f->field;
274*0Sstevel@tonic-gate disconnect_fields(f);
275*0Sstevel@tonic-gate
276*0Sstevel@tonic-gate if ((v = connect_fields(f, fields)) == E_OK) {
277*0Sstevel@tonic-gate if (f->maxpage) {
278*0Sstevel@tonic-gate P(f) = 0;
279*0Sstevel@tonic-gate C(f) = _first_active(f);
280*0Sstevel@tonic-gate } else {
281*0Sstevel@tonic-gate P(f) = -1;
282*0Sstevel@tonic-gate C(f) = (FIELD *) 0;
283*0Sstevel@tonic-gate }
284*0Sstevel@tonic-gate } else
285*0Sstevel@tonic-gate (void) connect_fields(f, p); /* reconnect original fields */
286*0Sstevel@tonic-gate return (v);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate
289*0Sstevel@tonic-gate FIELD **
form_fields(FORM * f)290*0Sstevel@tonic-gate form_fields(FORM *f)
291*0Sstevel@tonic-gate {
292*0Sstevel@tonic-gate return (Form(f)->field);
293*0Sstevel@tonic-gate }
294*0Sstevel@tonic-gate
295*0Sstevel@tonic-gate int
field_count(FORM * f)296*0Sstevel@tonic-gate field_count(FORM *f)
297*0Sstevel@tonic-gate {
298*0Sstevel@tonic-gate return (Form(f)->maxfield);
299*0Sstevel@tonic-gate }
300*0Sstevel@tonic-gate
301*0Sstevel@tonic-gate int
scale_form(FORM * f,int * rows,int * cols)302*0Sstevel@tonic-gate scale_form(FORM *f, int *rows, int *cols)
303*0Sstevel@tonic-gate {
304*0Sstevel@tonic-gate if (!f)
305*0Sstevel@tonic-gate return (E_BAD_ARGUMENT);
306*0Sstevel@tonic-gate
307*0Sstevel@tonic-gate if (!f->field)
308*0Sstevel@tonic-gate return (E_NOT_CONNECTED);
309*0Sstevel@tonic-gate
310*0Sstevel@tonic-gate *rows = f->rows;
311*0Sstevel@tonic-gate *cols = f->cols;
312*0Sstevel@tonic-gate return (E_OK);
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate
315*0Sstevel@tonic-gate BOOLEAN
data_behind(FORM * f)316*0Sstevel@tonic-gate data_behind(FORM *f)
317*0Sstevel@tonic-gate {
318*0Sstevel@tonic-gate return (OneRow(C(f)) ? B(f) != 0 : T(f) != 0);
319*0Sstevel@tonic-gate }
320*0Sstevel@tonic-gate
321*0Sstevel@tonic-gate /* _data_ahead - return ptr to last non-pad char in v[n] (v on failure) */
322*0Sstevel@tonic-gate static char *
_data_ahead(char * v,int pad,int n)323*0Sstevel@tonic-gate _data_ahead(char *v, int pad, int n)
324*0Sstevel@tonic-gate {
325*0Sstevel@tonic-gate char *vend = v + n;
326*0Sstevel@tonic-gate while (vend > v && *(vend - 1) == pad) --vend;
327*0Sstevel@tonic-gate return (vend);
328*0Sstevel@tonic-gate }
329*0Sstevel@tonic-gate
330*0Sstevel@tonic-gate BOOLEAN
data_ahead(FORM * f)331*0Sstevel@tonic-gate data_ahead(FORM *f)
332*0Sstevel@tonic-gate {
333*0Sstevel@tonic-gate static char buf[ MAX_BUF ];
334*0Sstevel@tonic-gate char *bptr = buf;
335*0Sstevel@tonic-gate WINDOW *w = W(f);
336*0Sstevel@tonic-gate FIELD *c = C(f);
337*0Sstevel@tonic-gate int ret = FALSE;
338*0Sstevel@tonic-gate int pad = Pad(c);
339*0Sstevel@tonic-gate int cols = c->cols;
340*0Sstevel@tonic-gate int dcols;
341*0Sstevel@tonic-gate int drows;
342*0Sstevel@tonic-gate int flag = cols > MAX_BUF - 1;
343*0Sstevel@tonic-gate int start;
344*0Sstevel@tonic-gate int chunk;
345*0Sstevel@tonic-gate
346*0Sstevel@tonic-gate if (flag)
347*0Sstevel@tonic-gate bptr = malloc(cols + 1);
348*0Sstevel@tonic-gate
349*0Sstevel@tonic-gate if (OneRow(c)) {
350*0Sstevel@tonic-gate dcols = c->dcols;
351*0Sstevel@tonic-gate start = B(f) + cols;
352*0Sstevel@tonic-gate
353*0Sstevel@tonic-gate while (start < dcols) {
354*0Sstevel@tonic-gate chunk = MIN(cols, dcols - start);
355*0Sstevel@tonic-gate (void) wmove(w, 0, start);
356*0Sstevel@tonic-gate (void) winnstr(w, bptr, chunk);
357*0Sstevel@tonic-gate
358*0Sstevel@tonic-gate if (bptr != _data_ahead(bptr, pad, chunk)) {
359*0Sstevel@tonic-gate ret = (TRUE);
360*0Sstevel@tonic-gate break;
361*0Sstevel@tonic-gate }
362*0Sstevel@tonic-gate
363*0Sstevel@tonic-gate start += cols;
364*0Sstevel@tonic-gate }
365*0Sstevel@tonic-gate } else { /* else multi-line field */
366*0Sstevel@tonic-gate drows = c->drows;
367*0Sstevel@tonic-gate start = T(f) + c->rows;
368*0Sstevel@tonic-gate
369*0Sstevel@tonic-gate while (start < drows) {
370*0Sstevel@tonic-gate (void) wmove(w, start++, 0);
371*0Sstevel@tonic-gate (void) winnstr(w, bptr, cols);
372*0Sstevel@tonic-gate
373*0Sstevel@tonic-gate if (bptr != _data_ahead(bptr, pad, cols)) {
374*0Sstevel@tonic-gate ret = TRUE;
375*0Sstevel@tonic-gate break;
376*0Sstevel@tonic-gate }
377*0Sstevel@tonic-gate }
378*0Sstevel@tonic-gate }
379*0Sstevel@tonic-gate
380*0Sstevel@tonic-gate if (flag)
381*0Sstevel@tonic-gate (void) free(bptr);
382*0Sstevel@tonic-gate
383*0Sstevel@tonic-gate (void) wmove(w, Y(f), X(f));
384*0Sstevel@tonic-gate return (ret);
385*0Sstevel@tonic-gate }
386