[BACK]Return to spinner.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / tests

Annotation of OpenXM_contrib/gmp/tests/spinner.c, Revision 1.1.1.1

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>