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