summaryrefslogtreecommitdiff
path: root/mpn/tests/spinner.c
blob: 26e2ecd02e49b492feb7dc01490792944997e081 (plain)
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
/* A stupid little spinning wheel designed to make it look like useful work
   is being done.  */

/*
Copyright (C) 1999, 2000 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The GNU MP Library 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 Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
*/

#include "config.h"

#include <signal.h>
#include <stdio.h>
#if HAVE_UNISTD_H
#include <unistd.h>     /* for isatty */
#endif

#include "gmp.h"
#include "gmp-impl.h"

#include "try.h"


/* An application can update this to get a count printed with the spinner.
   If left at 0, no count is printed. */

unsigned long  spinner_count = 0;


static int  spinner_wanted = -1;  /* -1 uninitialized, 1 wanted, 0 not */
static int  spinner_tick = 1;     /* 1 ready to print, 0 not */

#define numberof(x)   (sizeof (x) / sizeof ((x)[0]))


/*ARGSUSED*/
void
spinner_signal (int signum)
{
  spinner_tick = 1;

  if (signal (SIGALRM, spinner_signal) == SIG_ERR)  abort ();
  alarm (1);
}


/* Initialize the spinner.

   This is done the first time spinner() is called, so an application
   doesn't need to call this directly.
  
   The spinner is only wanted if the output is a tty.  */

#define SPINNER_WANTED_INIT() \
  if (spinner_wanted < 0) spinner_init ()

void
spinner_init (void)
{
  spinner_wanted = isatty (fileno (stdout));  
  if (spinner_wanted == -1)                   
    abort ();                                  

  if (!spinner_wanted)
    return;

  if (signal (SIGALRM, spinner_signal) == SIG_ERR)  abort ();
  alarm (1);
}


void
spinner (void)
{
  static const char  data[] = { '|', '/', '-', '\\' };
  static int         pos = 0;

  char  buf[128];

  SPINNER_WANTED_INIT ();

  if (spinner_tick)
    {
      buf[0] = data[pos];
      pos = (pos + 1) % numberof (data);
      spinner_tick = 0;

      if (spinner_count != 0)
        {
          sprintf (buf+1, " %lu\r", spinner_count);
        }
      else
        {
          buf[1] = '\r';
          buf[2] = '\0';
        }
      fputs (buf, stdout);
    }
}