File: spage.c

package info (click to toggle)
trn4 4.0-test77-12
  • links: PTS, VCS
  • area: non-free
  • in suites: buster
  • size: 3,644 kB
  • sloc: ansic: 48,331; sh: 6,817; tcl: 1,696; yacc: 660; perl: 108; makefile: 24
file content (433 lines) | stat: -rw-r--r-- 12,487 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* This file Copyright 1992 by Clifford A. Adams */
/* spage.c
 *
 */

#include "EXTERN.h"
#include "common.h"
#ifdef SCAN
#include "list.h"
#include "hash.h"
#include "cache.h"
#include "final.h"	/* assert() */
#include "ngdata.h"
#include "rt-util.h"	/* spinner */
#include "scan.h"
#include "sdisp.h"	/* for display dimension sizes */
#include "smisc.h"
#include "sorder.h"
#ifdef SCAN_ART
#include "scanart.h"
#include "samain.h"
#include "sathread.h"	/* sa_mode_fold */
#endif
#include "term.h"
#include "INTERN.h"
#include "spage.h"

/* returns TRUE if sucessful */
bool
s_fillpage_backward(end)
long end;		/* entry number to be last on page */
{
    int min_page_ents;	/* current minimum */
    int i,j;
    long a;		/* misc entry number */
    int page_lines;	/* lines available on page */
    int line_on;	/* line # currently on: 0=line after top status bar */

/* Debug */
#if 0
    printf("entry: s_fillpage_backward(%d)\n",end) FLUSH;
#endif

    page_lines = scr_height - s_top_lines - s_bot_lines;
    min_page_ents = MAX_PAGE_SIZE-1;
    s_bot_ent = -1;	/* none yet */
    line_on = 0;

    /* whatever happens, the entry display will need a full refresh... */
    s_ref_status = s_ref_desc = 0;	/* refresh from top entry */

    if (end < 1)		/* start from end */
	end = s_last();
    if (end == 0)		/* no entries */
	return FALSE;
    if (s_eligible(end))
	a = end;
    else
	a = s_prev_elig(end);
    if (!a)	/* no eligible entries */
	return FALSE;
    /* at this point we *know* that there is at least one eligible */

/* what if the "first" one for the page has a description too long? */
/* later make it shorten the descript. */

/* CONSIDER: make setspin conditional on context? */
    setspin(SPIN_BACKGROUND);	/* turn on spin on cache misses */
    /* uncertain what next comment means now */
    /* later do sheer paranoia check for min_page_ents */
    while ((line_on+s_ent_lines(a)) <= page_lines) {
	page_ents[min_page_ents].entnum = a;
	i = s_ent_lines(a);
	page_ents[min_page_ents].lines = i;
	min_page_ents--;
	s_bot_ent += 1;
	line_on = line_on+i;
	a = s_prev_elig(a);
	if (!a)		/* no more eligible */
	    break;	/* get out of loop and finish up... */
    }
/* what if none on page? (desc. too long) Fix later */
    setspin(SPIN_POP);	/* turn off spin on cache misses */
    /* replace the entries at the front of the page_ents array */
    /* also set start_line entries */
    j = 0;
    line_on = 0;
    for (i = min_page_ents+1; i < MAX_PAGE_SIZE; i++) {
	page_ents[j].entnum = page_ents[i].entnum;
	page_ents[j].pageflags = (char)0;
	page_ents[j].lines = page_ents[i].lines;
	page_ents[j].start_line = line_on;
	line_on = line_on + page_ents[j].lines;
	j++;
    }
    /* set new s_top_ent */
    s_top_ent = page_ents[0].entnum;
    /* Now, suppose that the pointer position is off the page.  That would
     * be bad, so lets make sure it doesn't happen.
     */
    if (s_ptr_page_line > s_bot_ent)
	s_ptr_page_line = s_bot_ent;
#ifdef SCAN_ART
    if (s_cur_type != S_ART)
	return TRUE;
    /* temporary fix.  Under some conditions ineligible entries will
     * not be found until they are in the page.  In this case just
     * refill the page.
     */
    for (i = 0; i <= s_bot_ent; i++)
	if (is_unavailable(sa_ents[page_ents[i].entnum].artnum))
	    break;
    if (i <= s_bot_ent)
	return s_fillpage_backward(end);
/* next time the unavail won't be chosen */
#endif
    return TRUE;	/* we have a page... */
}

/* fills the page array */
/* returns TRUE on success */
bool
s_fillpage_forward(start)
long start;			/* entry to start filling with */
{
    int i;
    long a;
    int page_lines;	/* lines available on page */
    int line_on;	/* line # currently on (0: line after top status bar */

/* Debug */
#if 0
    printf("entry: s_fillpage_forward(%d)\n",start) FLUSH;
#endif

    page_lines = scr_height - s_top_lines - s_bot_lines;
    s_bot_ent = -1;
    line_on = 0;

    /* whatever happens, the entry zone will need a full refresh... */
    s_ref_status = s_ref_desc = 0;

    if (start < 0)	/* fill from top */
	start = s_first();
    if (start == 0)	/* no entries */
	return FALSE;

    if (s_eligible(start))
	a = start;
    else
	a = s_next_elig(start);
    if (!a)	/* no eligible entries */
	return FALSE;
    /* at this point we *know* that there is at least one eligible */

/* what if the first entry for the page has a description too long? */
/* later make it shorten the descript. */

    setspin(SPIN_BACKGROUND);	/* turn on spin on cache misses */
/* ?  later do paranoia check for s_bot_ent */
    while ((line_on+s_ent_lines(a)) <= page_lines) {
	s_bot_ent += 1;
	page_ents[s_bot_ent].entnum = a;
	page_ents[s_bot_ent].start_line = line_on;
	i = s_ent_lines(a);
	page_ents[s_bot_ent].lines = i;
	page_ents[s_bot_ent].pageflags = (char)0;
	line_on = line_on+i;
	a = s_next_elig(a);
	if (!a)		/* no more eligible */
	    break;	/* get out of loop and finish up... */
    }
    setspin(SPIN_POP);	/* turn off spin on cache misses */
    /* Now, suppose that the pointer position is off the page.  That would
     * be bad, so lets make sure it doesn't happen.
     */
    /* set new s_top_ent */
    s_top_ent = page_ents[0].entnum;
    if (s_ptr_page_line > s_bot_ent)
	s_ptr_page_line = s_bot_ent;
#ifdef SCAN_ART
    if (s_cur_type != S_ART)
	return TRUE;
    /* temporary fix.  Under some conditions ineligible entries will
     * not be found until they are in the page.  In this case just
     * refill the page.
     */
    for (i = 0; i <= s_bot_ent; i++)
	if (is_unavailable(sa_ents[page_ents[i].entnum].artnum))
	    break;
    if (i <= s_bot_ent)
	return s_fillpage_forward(start);
    /* next time the unavail won't be chosen */
#endif
    return TRUE;	/* we have a page... */
}

/* Given possible changes to which entries should be on the page,
 * fills the page with more/less entries.  Tries to minimize refreshing
 * (the last entry on the page will be refreshed whether it needs it
 *  or not.)
 */
/* returns TRUE on success */
bool
s_refillpage()
{
    int i,j;
    long a;
    int page_lines;	/* lines available on page */
    int line_on;	/* line # currently on: 0=line after top status bar */

/* Debug */
#if 0
    printf("entry: s_refillpage\n") FLUSH;
#endif

    page_lines = scr_height - s_top_lines - s_bot_lines;
    /* if the top entry is not the s_top_ent,
     *    or the top entry is not eligible,
     *    or the top entry is not on the first line,
     *    or the top entry has a different # of lines now,
     * just refill the whole page.
     */
    if (s_bot_ent < 1 || s_top_ent < 1
     ||	s_top_ent != page_ents[0].entnum || !s_eligible(page_ents[0].entnum)
     || page_ents[0].start_line != 0
     || page_ents[0].lines != s_ent_lines(page_ents[0].entnum))
	return s_fillpage_forward(s_top_ent);

    i = 1;
    /* CAA misc note: I used to have
     * a = page_ents[1];
     * ...at this point.  This caused a truly difficult to track bug...
     * (briefly, occasionally the entry in page_ents[1] would be
     *  *before* (by display order) the entry in page_ents[0].  In
     *  this case the start_line entry of page_ents[0] could be overwritten
     *  causing big problems...)
     */
    a = s_next_elig(page_ents[0].entnum);

    /* similar to the tests in the last loop... */
    while (i <= s_bot_ent && s_eligible(page_ents[i].entnum)
      && page_ents[i].entnum == a
      && page_ents[i].lines == s_ent_lines(page_ents[i].entnum)) {
	i++;
	a = s_next_elig(a);
    }
    j = i-1;	/* j is the last "good" entry */

    s_bot_ent = j;
    line_on = page_ents[j].start_line + s_ent_lines(page_ents[j].entnum);
    a = s_next_elig(page_ents[j].entnum);

    setspin(SPIN_BACKGROUND);
    while (a && line_on+s_ent_lines(a) <= page_lines) {
	i = s_ent_lines(a);
	s_bot_ent += 1;
	page_ents[s_bot_ent].entnum = a;
	page_ents[s_bot_ent].lines = i;
	page_ents[s_bot_ent].start_line = line_on;
	page_ents[s_bot_ent].pageflags = (char)0;
	line_on = line_on+i;
	a = s_next_elig(a);
    }
    setspin(SPIN_POP);

    /* there are fairly good reasons to refresh the last good entry, such
     * as clearing the rest of the screen...
     */
    s_ref_status = s_ref_desc = j;

    /* Now, suppose that the pointer position is off the page.  That would
     * be bad, so lets make sure it doesn't happen.
     */
    if (s_ptr_page_line > s_bot_ent)
	s_ptr_page_line = s_bot_ent;
#ifdef SCAN_ART
    if (s_cur_type != S_ART)
	return TRUE;
    /* temporary fix.  Under some conditions ineligible entries will
     * not be found until they are in the page.  In this case just
     * refill the page.
     */
    for (i = 0; i <= s_bot_ent; i++)
	if (is_unavailable(sa_ents[page_ents[i].entnum].artnum))
	    break;
    if (i <= s_bot_ent)
	return s_refillpage();	/* next time the unavail won't be chosen */
#endif
    return TRUE;	/* we have a page... */
}

/* fills a page from current position.
 *   if that fails, backs up for a page.
 *     if that fails, downgrade eligibility requirements and try again
 * returns positive # on normal fill,
 *         negative # on special condition
 *         0 on failure
 */
int
s_fillpage()
{
    int i;

    s_refill = FALSE;	/* we don't need one now */
    if (s_top_ent < 1)	/* set top to first entry */
	s_top_ent = s_first();
    if (s_top_ent == 0)	/* no entries */
	return 0;	/* failure */
    if (!s_refillpage())	/* try for efficient refill */
	if (!s_fillpage_backward(s_top_ent)) {
	    /* downgrade eligibility standards */
	    switch (s_cur_type) {
#ifdef SCAN_ART
	      case S_ART:		/* article context */
		if (sa_mode_zoom) {		/* we were zoomed in */
		    s_ref_top = TRUE;	/* for "FOLD" display */
		    sa_mode_zoom = FALSE;	/* zoom out */
		    if (sa_unzoomrefold)
			sa_mode_fold = TRUE;
		    (void)s_go_top_ents();	/* go to top (ents and page) */
		    return s_fillpage();
		}
		return -1;		/* there just aren't entries! */
#endif
	      default:
		return -1;		/* there just aren't entries! */
	    } /* switch */
	} /* if */
    /* temporary fix.  Under some conditions ineligible entries will
     * not be found until they are in the page.  In this case just
     * refill the page.
     */
    for (i = 0; i <= s_bot_ent; i++)
	if (!s_eligible(page_ents[i].entnum))
	    return s_fillpage();  /* ineligible won't be chosen again */
#ifdef SCAN_ART
    if (s_cur_type != S_ART)
	return 1;
    /* be extra cautious about the article scan pages */
    for (i = 0; i <= s_bot_ent; i++)
	if (is_unavailable(sa_ents[page_ents[i].entnum].artnum))
	    break;
    if (i <= s_bot_ent)
	return s_fillpage();	/* next time the unavail won't be chosen */
#endif
    return 1;	/* I guess everything worked :-) */
}

void
s_cleanpage()
{
}

void
s_go_top_page()
{
    s_ptr_page_line = 0;
}

void
s_go_bot_page()
{
    s_ptr_page_line = s_bot_ent;
}

bool
s_go_top_ents()
{
    s_top_ent = s_first();
    if (!s_top_ent)
	printf("s_go_top_ents(): no first entry\n") FLUSH;
    assert(s_top_ent);	/* be nicer later */
    if (!s_eligible(s_top_ent))	/* this may save a redraw...*/
	s_top_ent = s_next_elig(s_top_ent);
    if (!s_top_ent) {	/* none eligible */
	/* just go to the top of all the entries */
	if (s_cur_type == S_ART)
	    s_top_ent = absfirst;
	else
	    s_top_ent = 1;	/* not very nice coding */
    }
    s_refill = TRUE;
    s_go_top_page();
    return TRUE;	/* successful */
}

bool
s_go_bot_ents()
{
    bool flag;

    flag = s_fillpage_backward(s_last());	/* fill backwards */
    if (!flag)
	return FALSE;
    s_go_bot_page();
    return TRUE;
}

void
s_go_next_page()
{
    long a;
    bool flag;

    a = s_next_elig(page_ents[s_bot_ent].entnum);
    if (!a)
	return;		/* no next page (we shouldn't have been called) */
    /* the fill-page will set the refresh for the screen */
    flag = s_fillpage_forward(a);
    assert(flag);		/* I *must* be able to fill a page */
    s_ptr_page_line = 0;	/* top of page */
}

void
s_go_prev_page()
{
    long a;
    bool flag;

    a = s_prev_elig(page_ents[0].entnum);
    if (!a)
	return;		/* no prev. page (we shouldn't have been called) */
    /* the fill-page will set the refresh for the screen */
    flag = s_fillpage_backward(a);	/* fill backward */
    assert(flag);		/* be nicer later... */
    /* take care of partially filled previous pages */
    flag = s_refillpage();
    assert(flag);		/* be nicer later... */
    s_ref_status = s_ref_desc = 0;	/* refresh from top */
    s_ptr_page_line = 0;	/* top of page */
}
#endif /* SCAN */