1 /* A stupid little spinning wheel designed to make it look like useful work 2 is being done. 3 4 Copyright 1999-2001 Free Software Foundation, Inc. 5 6 This file is part of the GNU MP Library test suite. 7 8 The GNU MP Library test suite is free software; you can redistribute it 9 and/or modify it under the terms of the GNU General Public License as 10 published by the Free Software Foundation; either version 3 of the License, 11 or (at your option) any later version. 12 13 The GNU MP Library test suite is distributed in the hope that it will be 14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 16 Public License for more details. 17 18 You should have received a copy of the GNU General Public License along with 19 the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */ 20 21 #include "config.h" 22 23 #include <signal.h> 24 #include <stdio.h> 25 #include <stdlib.h> 26 #if HAVE_UNISTD_H 27 #include <unistd.h> /* for isatty */ 28 #endif 29 30 #include "gmp-impl.h" 31 32 #include "tests.h" 33 34 35 /* "alarm" is not available on mingw32, and the SIGALRM constant is not 36 defined. Don't bother with a spinner in this case. */ 37 #if ! HAVE_ALARM || ! defined (SIGALRM) 38 #define alarm(n) abort() 39 #define signal(sig,func) SIG_ERR 40 #endif 41 42 43 /* An application can update this to get a count printed with the spinner. 44 If left at 0, no count is printed. */ 45 46 unsigned long spinner_count = 0; 47 48 49 int spinner_wanted = -1; /* -1 uninitialized, 1 wanted, 0 not */ 50 int spinner_tick = 1; /* 1 ready to print, 0 not */ 51 52 53 /*ARGSUSED*/ 54 RETSIGTYPE 55 spinner_signal (int signum) 56 { 57 spinner_tick = 1; 58 59 if (signal (SIGALRM, spinner_signal) == SIG_ERR) 60 { 61 printf ("spinner_signal(): Oops, cannot reinstall SIGALRM\n"); 62 abort (); 63 } 64 alarm (1); 65 } 66 67 68 /* Initialize the spinner. 69 70 This is done the first time spinner() is called, so an application 71 doesn't need to call this directly. 72 73 The spinner is only wanted if the output is a tty. */ 74 75 #define SPINNER_WANTED_INIT() \ 76 if (spinner_wanted < 0) spinner_init () 77 78 void 79 spinner_init (void) 80 { 81 spinner_wanted = isatty (STDOUT_FILENO); 82 if (spinner_wanted == -1) 83 abort (); 84 85 if (!spinner_wanted) 86 return; 87 88 if (signal (SIGALRM, spinner_signal) == SIG_ERR) 89 { 90 printf ("(no spinner)\r"); 91 spinner_tick = 0; 92 return; 93 } 94 alarm (1); 95 96 /* unbuffered output so the spinner will show up */ 97 setbuf (stdout, NULL); 98 } 99 100 101 void 102 spinner (void) 103 { 104 static const char data[] = { '|', '/', '-', '\\' }; 105 static int pos = 0; 106 107 char buf[128]; 108 109 SPINNER_WANTED_INIT (); 110 111 if (spinner_tick) 112 { 113 buf[0] = data[pos]; 114 pos = (pos + 1) % numberof (data); 115 spinner_tick = 0; 116 117 if (spinner_count != 0) 118 { 119 sprintf (buf+1, " %lu\r", spinner_count); 120 } 121 else 122 { 123 buf[1] = '\r'; 124 buf[2] = '\0'; 125 } 126 fputs (buf, stdout); 127 } 128 } 129