Codebase list tack / fresh-snapshots/main control.c
fresh-snapshots/main

Tree @fresh-snapshots/main (Download .tar.gz)

control.c @fresh-snapshots/mainraw · history · blame

  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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
** Copyright (C) 1991, 1997-2015,2017 Free Software Foundation, Inc.
** 
** This file is part of TACK.
** 
** TACK is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2, or (at your option)
** any later version.
** 
** TACK is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
** 
** You should have received a copy of the GNU General Public License
** along with TACK; see the file COPYING.  If not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
** Boston, MA 02110-1301, USA
*/

#include <tack.h>

#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif

MODULE_ID("$Id: control.c,v 1.17 2017/07/23 16:08:23 tom Exp $")

/* terminfo test program control subroutines */

#if HAVE_GETTIMEOFDAY
#define MY_TIMER struct timeval
#else
#define MY_TIMER time_t
#endif

/* globals */
int test_complete;		/* counts number of tests completed */

char txt_longer_test_time[80];	/* +) use longer time */
char txt_shorter_test_time[80];	/* -) use shorter time */
static int pad_test_duration = 1;	/* number of seconds for a pad test */
int auto_pad_mode;		/* run the time tests */
SIG_ATOMIC_T no_alarm_event;	/* TRUE if the alarm has not gone off yet */
unsigned long usec_run_time;	/* length of last test in microseconds */
static MY_TIMER stop_watch[MAX_TIMERS];		/* Hold the start timers */

char txt_longer_augment[80];	/* >) use bigger augment */
char txt_shorter_augment[80];	/* <) use smaller augment */

/* caps under test data base */
int tt_delay_max;		/* max number of milliseconds we can delay */
int tt_delay_used;		/* number of milliseconds consumed in delay */
const char *tt_cap[TT_MAX];	/* value of string */
int tt_affected[TT_MAX];	/* lines or columns effected (repetition factor) */
int tt_count[TT_MAX];		/* Number of times sent */
int tt_delay[TT_MAX];		/* Number of milliseconds delay */
int ttp;			/* number of entries used */

/* Saved value of the above data base */
const char *tx_cap[TT_MAX];	/* value of string */
int tx_affected[TT_MAX];	/* lines or columns effected (repetition factor) */
int tx_count[TT_MAX];		/* Number of times sent */
int tx_index[TT_MAX];		/* String index */
int tx_delay[TT_MAX];		/* Number of milliseconds delay */
int txp;			/* number of entries used */
int tx_characters;		/* printing characters sent by test */
unsigned long tx_cps;		/* characters per second */
static TestList *tx_source;	/* The test that generated this data */

#define RESULT_BLOCK		1024
static int blocks;		/* number of result blocks available */
static TestResults *results;	/* pointer to next available */
static TestResults **pads;	/* save pad results here */

static void
alloc_arrays(void)
{
    if (pads == 0) {
	pads = (TestResults **) calloc((size_t) MAX_STRINGS,
				       sizeof(TestResults
					      *));
    }
}

/*
**	event_start(number)
**
**	Begin the stopwatch at the current time-of-day.
*/
void
event_start(int n)
{
#if HAVE_GETTIMEOFDAY
    (void) gettimeofday(&stop_watch[n], (struct timezone *) 0);
#else
    stop_watch[n] = time((time_t *) 0);
#endif
}

/*
**	event_time(number)
**
**	Return the number of milliseconds since this stop watch began.
*/
long
event_time(int n)
{
#if HAVE_GETTIMEOFDAY
    MY_TIMER current_time;

    (void) gettimeofday(&current_time, (struct timezone *) 0);
    return ((current_time.tv_sec - stop_watch[n].tv_sec) * 1000000)
	+ current_time.tv_usec - stop_watch[n].tv_usec;
#else
    return (time((time_t *) 0) - stop_watch[n]) * 1000;
#endif
}

/*****************************************************************************
 *
 * Execution control for string capability tests
 *
 *****************************************************************************/

/*
**	get_next_block()
**
**	Get a results block for pad test data.
*/
static TestResults *
get_next_block(void)
{
    if (blocks <= 0) {
	results = (TestResults *)
	    malloc(sizeof(TestResults) * RESULT_BLOCK);
	if (!results) {
	    ptextln("Malloc failed");
	    return (TestResults *) 0;
	}
	blocks = RESULT_BLOCK;
    }
    blocks--;
    return results++;
}

/*
**	set_augment_txt()
**
**	Initialize the augment menu selections
*/
void
set_augment_txt(void)
{
    sprintf(txt_longer_augment,
	    ">) Change lines/characters effected to %d", augment << 1);
    sprintf(txt_shorter_augment,
	    "<) Change lines/characters effected to %d", augment >> 1);
}

void
control_init(void)
{
    sprintf(txt_longer_test_time, "+) Change test time to %d seconds",
	    pad_test_duration + 1);
    sprintf(txt_shorter_test_time, "-) Change test time to %d seconds",
	    pad_test_duration - 1);
    set_augment_txt();
}

/*
**	msec_cost(cap, affected-count)
**
**	Return the number of milliseconds delay needed by the cap.
*/
int
msec_cost(
	     const char *const cap,
	     int affcnt)
{
    int dec, value, total, star, ch;
    const char *cp;

    if (!cap) {
	return 0;
    }
    total = 0;
    for (cp = cap; *cp; cp++) {
	if (*cp == '$' && cp[1] == '<') {
	    star = 1;
	    value = dec = 0;
	    for (cp += 2; (ch = *cp); cp++) {
		if (ch >= '0' && ch <= '9') {
		    value = value * 10 + (ch - '0');
		    dec *= 10;
		} else if (ch == '.') {
		    dec = 1;
		} else if (ch == '*') {
		    star = affcnt;
		} else if (ch == '>') {
		    break;
		}
	    }
	    if (dec > 1) {
		total += (value * star) / dec;
	    } else {
		total += (value * star);
	    }
	}
    }
    return total;
}

/*
**	liberated(cap)
**
**	Return the cap without padding
*/
char *
liberated(const char *cap)
{
    static char cb[1024];
    char *ts, *ls;

    cb[0] = '\0';
    ls = NULL;
    if (cap) {
	for (ts = cb; (*ts = *cap); ++cap) {
	    if (*cap == '$' && cap[1] == '<') {
		ls = ts;
	    }
	    ++ts;
	    if (*cap == '>') {
		if (ls) {
		    ts = ls;
		    ls = NULL;
		}
	    }
	}
    }
    return cb;
}

/*
**	page_loop()
**
**	send CR/LF or go home and bump letter
*/
void
page_loop(void)
{
    if (line_count + 2 >= lines) {
	NEXT_LETTER;
	go_home();
    } else {
	put_crlf();
    }
}

/*
**	skip_pad_test(test-list-entry, state, ch, text)
**
**	Print the start test line.  Handle start up commands.
**	Return TRUE if a return is requested.
*/
int
skip_pad_test(
		 TestList * test,
		 int *state,
		 int *ch,
		 const char *text)
{
    char rep_text[16];

    while (1) {
	if (text) {
	    ptext(text);
	}
	if ((test->flags & MENU_LC_MASK)) {
	    sprintf(rep_text, " *%d", augment);
	    ptext(rep_text);
	}
	ptext(" [n] > ");
	*ch = wait_here();
	if (*ch == 's') {
	    /* Skip is converted to next */
	    *ch = 'n';
	    return TRUE;
	}
	if (*ch == 'q') {
	    /* Quit is converted to help */
	    *ch = '?';
	    return TRUE;
	}
	if (*ch == '\r' || *ch == '\n' || *ch == 'n' || *ch == 'r') {
	    /* this is the only response that allows the test to run */
	    *ch = 0;
	}
	if (subtest_menu(pad_test_list, state, ch)) {
	    continue;
	}
	return (*ch != 0);
    }
}

/*
**	pad_done_message(test_list)
**
**	Print the Done message and request input.
*/
void
pad_done_message(
		    TestList * test,
		    int *state,
		    int *ch)
{
    int default_action = 0;
    char done_message[128];
    char rep_text[16];

    while (1) {
	if ((test->flags & MENU_LC_MASK)) {
	    sprintf(rep_text, "*%d", augment);
	} else {
	    rep_text[0] = '\0';
	}
	if (test->caps_done) {
	    sprintf(done_message, "(%s)%s Done ", test->caps_done,
		    rep_text);
	    ptext(done_message);
	} else {
	    if (rep_text[0]) {
		ptext(rep_text);
		ptext(" ");
	    }
	    ptext("Done ");
	}
	if (debug_level & 2) {
	    dump_test_stats(test, state, ch);
	} else {
	    *ch = wait_here();
	}
	if (*ch == '\r' || *ch == '\n') {
	    *ch = default_action;
	    return;
	}
	if (*ch == 's' || *ch == 'n') {
	    *ch = 0;
	    return;
	}
	if ((strchr) (pad_repeat_test, *ch)) {
	    /* default action is now repeat */
	    default_action = 'r';
	}
	if (subtest_menu(pad_test_list, state, ch)) {
	    continue;
	}
	return;
    }
}

/*
**	sliding_scale(dividend, factor, divisor)
**
**	Return (dividend * factor) / divisor
*/
int
sliding_scale(
		 int dividend,
		 int factor,
		 unsigned long divisor)
{
    double d = dividend;

    if (divisor) {
	d = (d * (double) factor) / (double) divisor;
	return (int) (d + 0.5);
    }
    return 0;
}

/*
**	pad_test_startup()
**
**	Do the stuff needed to begin a test.
*/
void
pad_test_startup(
		    int do_clear)
{
    if (do_clear) {
	put_clear();
    }
    repeats = augment;
    raw_characters_sent = 0;
    test_complete = ttp = char_count = tt_delay_used = 0;
    letter = letters[letter_number = 0];
    if (pad_test_duration <= 0) {
	pad_test_duration = 1;
    }
    tt_delay_max = pad_test_duration * 1000;
    set_alarm_clock(pad_test_duration);
    event_start(TIME_TEST);
}

/*
**	still_testing()
**
**	This function is called to see if the test loop should be terminated.
*/
int
still_testing(void)
{
    fflush(stdout);
    test_complete++;
    return EXIT_CONDITION;
}

/*
**	pad_test_shutdown()
**
**	Do the stuff needed to end a test.
*/
void
pad_test_shutdown(
		     TestList * t,
		     int crlf)
{
    int i;
    int counts;			/* total counts */
    int ss;			/* Save string index */
    int cpo;			/* characters per operation */
    long delta;			/* difference in characters */
    int bogus;			/* Time is inaccurate */
    TestResults *r;		/* Results of current test */
    int ss_index[TT_MAX];	/* String index */

    alloc_arrays();
    if (tty_can_sync == SYNC_TESTED) {
	bogus = tty_sync_error();
    } else {
	bogus = 1;
    }
    usec_run_time = (unsigned long) event_time(TIME_TEST);
    tx_source = t;
    tx_characters = raw_characters_sent;
    tx_cps = (unsigned long) sliding_scale(tx_characters, 1000000, usec_run_time);

    /* save the data base */
    for (txp = ss = counts = 0; txp < ttp; txp++) {
	tx_cap[txp] = tt_cap[txp];
	tx_count[txp] = tt_count[txp];
	tx_delay[txp] = tt_delay[txp];
	tx_affected[txp] = tt_affected[txp];
	tx_index[txp] = get_string_cap_byvalue(tt_cap[txp]);
	if (tx_index[txp] >= 0) {
	    if (cap_match(t->caps_done, strnames[tx_index[txp]])) {
		ss_index[ss++] = txp;
		counts += tx_count[txp];
	    }
	}
    }

    if (crlf) {
	put_crlf();
    }
    if (counts == 0 || tty_cps == 0 || bogus) {
	/* nothing to do */
	return;
    }
    /* calculate the suggested pad times */
    delta = (long) (usec_run_time
		    - (unsigned long) sliding_scale(tx_characters,
						    1000000,
						    tty_cps));
    if (delta < 0) {
	/* probably should bump tx_characters */
	delta = 0;
    }
    cpo = (int) (delta / counts);
    for (i = 0; i < ss; i++) {
	if (!(r = get_next_block())) {
	    return;
	}
	r->next = pads[tx_index[ss_index[i]]];
	pads[tx_index[ss_index[i]]] = r;
	r->test = t;
	r->reps = tx_affected[ss_index[i]];
	r->delay = cpo;
    }
}

/*
**	show_cap_results(index)
**
**	Display the previous results
*/
static void
show_cap_results(
		    int x)
{
    TestResults *r;		/* a result */
    int delay;

    alloc_arrays();
    if ((r = pads[x])) {
	sprintf(temp, "(%s)", strnames[x]);
	ptext(temp);
	while (r) {
	    sprintf(temp, "$<%d>", r->delay / 1000);
	    put_columns(temp, (int) strlen(temp), 10);
	    r = r->next;
	}
	r = pads[x];
	while (r) {
	    if (r->reps > 1) {
		delay = r->delay / (r->reps * 100);
		sprintf(temp, "$<%d.%d*>", delay / 10, delay % 10);
		put_columns(temp, (int) strlen(temp), 10);
	    }
	    r = r->next;
	}
	put_crlf();
    }
}

/*
**	dump_test_stats(test_list, status, ch)
**
**	Dump the statistics about the last test
*/
void
dump_test_stats(
		   TestList * t,
		   int *state,
		   int *ch)
{
    int i, j;
    char tbuf[32];
    int x[32];

    put_crlf();
    if (tx_source && tx_source->caps_done) {
	cap_index(tx_source->caps_done, x);
	if (x[0] >= 0) {
	    sprintf(temp, "Caps summary for (%s)",
		    tx_source->caps_done);
	    ptextln(temp);
	    for (i = 0; x[i] >= 0; i++) {
		show_cap_results(x[i]);
	    }
	    put_crlf();
	}
    }
    sprintf(tbuf, "%011lu", usec_run_time);
    sprintf(temp,
	    "Test time: %lu.%s, characters per second %lu, characters %d",
	    usec_run_time / 1000000UL, &tbuf[5], tx_cps, tx_characters);
    ptextln(temp);
    for (i = 0; i < txp; i++) {
	if ((j = get_string_cap_byvalue(tx_cap[i])) >= 0) {
	    sprintf(tbuf, "(%s)", strnames[j]);
	} else {
	    strcpy(tbuf, "(?)");
	}
	sprintf(temp, "%8d  %3d  $<%3d>  %8s %s",
		tx_count[i], tx_affected[i], tx_delay[i],
		tbuf, expand(tx_cap[i]));
	putln(temp);
    }
    generic_done_message(t, state, ch);
}

/*
**	longer_test_time(test_list, status, ch)
**
**	Extend the number of seconds for each test.
*/
void
longer_test_time(
		    TestList * t GCC_UNUSED,
		    int *state GCC_UNUSED,
		    int *ch)
{
    pad_test_duration += 1;
    sprintf(txt_longer_test_time, "+) Change test time to %d seconds",
	    pad_test_duration + 1);
    sprintf(txt_shorter_test_time, "-) Change test time to %d seconds",
	    pad_test_duration - 1);
    sprintf(temp, "Tests will run for %d seconds", pad_test_duration);
    ptext(temp);
    *ch = REQUEST_PROMPT;
}

/*
**	shorter_test_time(test_list, status, ch)
**
**	Shorten the number of seconds for each test.
*/
void
shorter_test_time(
		     TestList * t GCC_UNUSED,
		     int *state GCC_UNUSED,
		     int *ch)
{
    if (pad_test_duration > 1) {
	pad_test_duration -= 1;
	sprintf(txt_longer_test_time, "+) Change test time to %d seconds",
		pad_test_duration + 1);
	sprintf(txt_shorter_test_time, "-) Change test time to %d seconds",
		pad_test_duration - 1);
    }
    sprintf(temp, "Tests will run for %d second%s", pad_test_duration,
	    pad_test_duration > 1 ? "s" : "");
    ptext(temp);
    *ch = REQUEST_PROMPT;
}

/*
**	longer_augment(test_list, status, ch)
**
**	Lengthen the number of lines/characters effected
*/
void
longer_augment(
		  TestList * t,
		  int *state GCC_UNUSED,
		  int *ch)
{
    augment <<= 1;
    set_augment_txt();
    if (augment_test) {
	t = augment_test;
    }
    sprintf(temp, "The pad tests will effect %d %s.", augment,
	    ((t->flags & MENU_LC_MASK) == MENU_lines) ?
	    "lines" : "characters");
    ptextln(temp);
    *ch = REQUEST_PROMPT;
}

/*
**	shorter_augment(test_list, status, ch)
**
**	Shorten the number of lines/characters effected
*/
void
shorter_augment(
		   TestList * t,
		   int *state GCC_UNUSED,
		   int *ch)
{
    if (augment > 1) {
	/* don't let the augment go to zero */
	augment >>= 1;
    }
    set_augment_txt();
    if (augment_test) {
	t = augment_test;
    }
    sprintf(temp, "The pad tests will effect %d %s.", augment,
	    ((t->flags & MENU_LC_MASK) == MENU_lines) ?
	    "lines" : "characters");
    ptextln(temp);
    *ch = REQUEST_PROMPT;
}