2023-05-12 16:29:41 +03:00
|
|
|
/* $NetBSD: worms.c,v 1.31 2023/05/12 13:29:41 kre Exp $ */
|
1995-04-22 12:00:54 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1995-04-22 12:00:54 +04:00
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 13:36:50 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
2008-07-20 04:52:39 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1980, 1993\
|
|
|
|
The Regents of the University of California. All rights reserved.");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-04-22 12:00:54 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)worms.c 8.1 (Berkeley) 5/31/93";
|
|
|
|
#else
|
2023-05-12 16:29:41 +03:00
|
|
|
__RCSID("$NetBSD: worms.c,v 1.31 2023/05/12 13:29:41 kre Exp $");
|
1995-04-22 12:00:54 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
|
|
|
|
* @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
|
|
|
|
* @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
|
|
|
|
* @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
|
|
|
|
* @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
|
|
|
|
* @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
|
|
|
|
* @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
|
|
|
|
* @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
|
|
|
|
* @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
|
|
|
|
*
|
|
|
|
* Eric P. Scott
|
|
|
|
* Caltech High Energy Physics
|
|
|
|
* October, 1980
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <sys/types.h>
|
1995-04-22 12:00:54 +04:00
|
|
|
|
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
2023-04-15 18:21:56 +03:00
|
|
|
#include <ctype.h>
|
1999-07-30 06:23:27 +04:00
|
|
|
#include <curses.h>
|
|
|
|
#include <err.h>
|
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
2023-04-15 18:21:56 +03:00
|
|
|
#include <limits.h>
|
1995-04-22 12:00:54 +04:00
|
|
|
#include <signal.h>
|
2023-04-18 18:02:22 +03:00
|
|
|
#include <stdbool.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdio.h>
|
1993-04-19 01:52:18 +04:00
|
|
|
#include <stdlib.h>
|
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
2023-04-15 18:21:56 +03:00
|
|
|
#include <strings.h>
|
2023-04-27 01:58:09 +03:00
|
|
|
#include <term.h>
|
1995-04-22 12:00:54 +04:00
|
|
|
#include <unistd.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1999-07-30 06:23:27 +04:00
|
|
|
static const struct options {
|
1993-03-21 12:45:37 +03:00
|
|
|
int nopts;
|
|
|
|
int opts[3];
|
|
|
|
}
|
|
|
|
normal[8] = {
|
|
|
|
{ 3, { 7, 0, 1 } },
|
|
|
|
{ 3, { 0, 1, 2 } },
|
|
|
|
{ 3, { 1, 2, 3 } },
|
|
|
|
{ 3, { 2, 3, 4 } },
|
|
|
|
{ 3, { 3, 4, 5 } },
|
|
|
|
{ 3, { 4, 5, 6 } },
|
|
|
|
{ 3, { 5, 6, 7 } },
|
|
|
|
{ 3, { 6, 7, 0 } }
|
2023-04-27 01:58:09 +03:00
|
|
|
},
|
|
|
|
upper[8] = {
|
1993-03-21 12:45:37 +03:00
|
|
|
{ 1, { 1, 0, 0 } },
|
|
|
|
{ 2, { 1, 2, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 2, { 4, 5, 0 } },
|
|
|
|
{ 1, { 5, 0, 0 } },
|
|
|
|
{ 2, { 1, 5, 0 } }
|
|
|
|
},
|
|
|
|
left[8] = {
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 2, { 2, 3, 0 } },
|
|
|
|
{ 1, { 3, 0, 0 } },
|
|
|
|
{ 2, { 3, 7, 0 } },
|
|
|
|
{ 1, { 7, 0, 0 } },
|
|
|
|
{ 2, { 7, 0, 0 } }
|
|
|
|
},
|
|
|
|
right[8] = {
|
|
|
|
{ 1, { 7, 0, 0 } },
|
|
|
|
{ 2, { 3, 7, 0 } },
|
|
|
|
{ 1, { 3, 0, 0 } },
|
|
|
|
{ 2, { 3, 4, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 2, { 6, 7, 0 } }
|
|
|
|
},
|
|
|
|
lower[8] = {
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 2, { 0, 1, 0 } },
|
|
|
|
{ 1, { 1, 0, 0 } },
|
|
|
|
{ 2, { 1, 5, 0 } },
|
|
|
|
{ 1, { 5, 0, 0 } },
|
|
|
|
{ 2, { 5, 6, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } }
|
|
|
|
},
|
|
|
|
upleft[8] = {
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 1, { 3, 0, 0 } },
|
|
|
|
{ 2, { 1, 3, 0 } },
|
|
|
|
{ 1, { 1, 0, 0 } }
|
|
|
|
},
|
|
|
|
upright[8] = {
|
|
|
|
{ 2, { 3, 5, 0 } },
|
|
|
|
{ 1, { 3, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 1, { 5, 0, 0 } }
|
|
|
|
},
|
|
|
|
lowleft[8] = {
|
|
|
|
{ 3, { 7, 0, 1 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 1, { 1, 0, 0 } },
|
|
|
|
{ 2, { 1, 7, 0 } },
|
|
|
|
{ 1, { 7, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } }
|
|
|
|
},
|
|
|
|
lowright[8] = {
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 1, { 7, 0, 0 } },
|
|
|
|
{ 2, { 5, 7, 0 } },
|
|
|
|
{ 1, { 5, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } }
|
|
|
|
};
|
1999-07-30 06:23:27 +04:00
|
|
|
static const char flavor[] = {
|
2023-04-27 01:58:09 +03:00
|
|
|
'O', '*', '#', '$', '%', '0', 'o', '~',
|
|
|
|
'+', 'x', ':', '^', '_', '&', '@', 'w'
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
2023-04-27 01:58:09 +03:00
|
|
|
static const int flavors = __arraycount(flavor);
|
2023-05-12 16:29:41 +03:00
|
|
|
static const char eyeball[] = {
|
|
|
|
'0', 'X', '@', 'S', 'X', '@', 'O', '=',
|
|
|
|
'#', '*', '=', 'v', 'L', '@', 'o', 'm'
|
|
|
|
};
|
|
|
|
__CTASSERT(sizeof(flavor) == sizeof(eyeball));
|
2023-04-27 01:58:09 +03:00
|
|
|
|
1999-07-30 06:23:27 +04:00
|
|
|
static const short xinc[] = {
|
1993-03-21 12:45:37 +03:00
|
|
|
1, 1, 1, 0, -1, -1, -1, 0
|
|
|
|
}, yinc[] = {
|
|
|
|
-1, 0, 1, 1, 1, 0, -1, -1
|
|
|
|
};
|
|
|
|
static struct worm {
|
2023-04-27 01:58:09 +03:00
|
|
|
int orientation, head, len;
|
1993-03-21 12:45:37 +03:00
|
|
|
short *xpos, *ypos;
|
2023-05-12 16:29:41 +03:00
|
|
|
chtype ch, eye, attr;
|
1993-03-21 12:45:37 +03:00
|
|
|
} *worm;
|
|
|
|
|
2023-04-27 01:58:09 +03:00
|
|
|
static volatile sig_atomic_t sig_caught;
|
1999-07-30 06:23:27 +04:00
|
|
|
|
2023-04-27 01:58:09 +03:00
|
|
|
static int initclr(int**);
|
2009-08-12 12:54:54 +04:00
|
|
|
static void nomem(void) __dead;
|
|
|
|
static void onsig(int);
|
2023-04-27 01:58:09 +03:00
|
|
|
static int worm_length(int, int);
|
1993-04-19 01:52:18 +04:00
|
|
|
|
1995-04-22 12:00:54 +04:00
|
|
|
int
|
2012-06-19 09:46:08 +04:00
|
|
|
main(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2023-04-27 01:58:09 +03:00
|
|
|
int CO, LI, last, bottom, ch, number, trail;
|
|
|
|
int x, y, h, n, nc;
|
|
|
|
int maxlength, minlength;
|
|
|
|
unsigned int seed, delay;
|
1999-07-30 06:23:27 +04:00
|
|
|
const struct options *op;
|
2023-04-27 01:58:09 +03:00
|
|
|
struct worm *w;
|
1993-03-21 12:45:37 +03:00
|
|
|
short **ref;
|
2023-04-27 01:58:09 +03:00
|
|
|
short *ip;
|
1999-07-30 06:23:27 +04:00
|
|
|
const char *field;
|
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
2023-04-15 18:21:56 +03:00
|
|
|
char *ep;
|
2023-04-27 01:58:09 +03:00
|
|
|
unsigned long ul, up;
|
2023-04-18 18:02:22 +03:00
|
|
|
bool argerror = false;
|
2023-04-27 01:58:09 +03:00
|
|
|
bool docolour = false; /* -C, use coloured worms */
|
|
|
|
bool docaput = false; /* -H, show which end of worm is head */
|
|
|
|
int *ctab = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2023-04-27 01:58:09 +03:00
|
|
|
delay = 20000;
|
|
|
|
maxlength = minlength = 16;
|
1993-03-21 12:45:37 +03:00
|
|
|
number = 3;
|
2023-04-27 01:58:09 +03:00
|
|
|
seed = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
trail = ' ';
|
|
|
|
field = NULL;
|
2023-04-27 01:58:09 +03:00
|
|
|
|
|
|
|
if ((ep = getenv("WORMS")) != NULL) {
|
|
|
|
ul = up = 0;
|
|
|
|
while ((ch = *ep++) != '\0') {
|
|
|
|
switch (ch) {
|
|
|
|
case 'C':
|
|
|
|
docolour = !docolour;
|
|
|
|
continue;
|
|
|
|
case 'f':
|
|
|
|
if (field)
|
|
|
|
field = NULL;
|
|
|
|
else
|
|
|
|
field = "WORM";
|
|
|
|
continue;
|
|
|
|
case 'H':
|
|
|
|
docaput = !docaput;
|
|
|
|
continue;
|
|
|
|
case 'r':
|
|
|
|
minlength = 5;
|
|
|
|
maxlength = 0;
|
|
|
|
continue;
|
|
|
|
case 't':
|
|
|
|
if (trail == ' ')
|
|
|
|
trail = '.';
|
|
|
|
else
|
|
|
|
trail = ' ';
|
|
|
|
continue;
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
if (up > 1)
|
|
|
|
continue;
|
|
|
|
if (ul >= 100000) /* 1/10 second, in us */
|
|
|
|
continue;
|
|
|
|
ul *= 10;
|
|
|
|
ul += (ch - '0');
|
|
|
|
up = 1;
|
|
|
|
continue;
|
|
|
|
case 'm':
|
|
|
|
if (up == 1 && ul <= 1000)
|
|
|
|
ul *= 1000;
|
|
|
|
up += 2;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((up & 1) != 0) /* up == 1 || up == 3 */
|
|
|
|
delay = ul;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "Cd:fHl:n:rS:t")) != -1) {
|
1993-03-21 12:45:37 +03:00
|
|
|
switch(ch) {
|
2023-04-27 01:58:09 +03:00
|
|
|
case 'C':
|
|
|
|
docolour = !docolour;
|
|
|
|
continue;
|
1999-07-30 06:23:27 +04:00
|
|
|
case 'd':
|
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
2023-04-15 18:21:56 +03:00
|
|
|
ul = strtoul(optarg, &ep, 10);
|
|
|
|
if (ep != optarg) {
|
|
|
|
while (isspace(*(unsigned char *)ep))
|
|
|
|
ep++;
|
|
|
|
}
|
|
|
|
if (ep == optarg ||
|
|
|
|
(*ep != '\0' &&
|
|
|
|
( ep[1] == '\0' ? (*ep != 'm' && *ep != 'u') :
|
|
|
|
( strcasecmp(ep, "ms") != 0 &&
|
|
|
|
strcasecmp(ep, "us") != 0 )) )) {
|
|
|
|
errx(1, "-d: invalid delay (%s)", optarg);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* if ul >= INT_MAX/1000 we don't need the *1000,
|
|
|
|
* as even without that it will exceed the limit
|
|
|
|
* just below and be treated as an error.
|
|
|
|
* (This does assume >=32 bit int, but so does POSIX)
|
|
|
|
*/
|
|
|
|
if (*ep != 'u' && ul < INT_MAX / 1000)
|
|
|
|
ul *= 1000; /* ms -> us */
|
|
|
|
if (ul > 1000*1000) {
|
|
|
|
errx(1,
|
2023-04-19 10:40:49 +03:00
|
|
|
"-d: delay (%s) out of range [0 - 1000]",
|
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
2023-04-15 18:21:56 +03:00
|
|
|
optarg);
|
|
|
|
}
|
|
|
|
delay = (unsigned int)ul;
|
2023-04-18 18:02:22 +03:00
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'f':
|
2023-04-27 01:58:09 +03:00
|
|
|
if (field == NULL)
|
|
|
|
field = "WORM";
|
|
|
|
else
|
|
|
|
field = NULL;
|
|
|
|
continue;
|
|
|
|
case 'H':
|
|
|
|
docaput = !docaput;
|
2023-04-18 18:02:22 +03:00
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'l':
|
2023-04-27 01:58:09 +03:00
|
|
|
up = ul = strtoul(optarg, &ep, 10);
|
|
|
|
if (ep != optarg) {
|
|
|
|
while (isspace(*(unsigned char *)ep))
|
|
|
|
ep++;
|
|
|
|
if (*ep == '-')
|
|
|
|
up = strtoul(++ep, &ep, 10);
|
|
|
|
}
|
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
2023-04-15 18:21:56 +03:00
|
|
|
if (ep == optarg || *ep != '\0' ||
|
2023-04-27 01:58:09 +03:00
|
|
|
ul < 2 || up < ul || up > 1024) {
|
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
2023-04-15 18:21:56 +03:00
|
|
|
errx(1, "-l: invalid length (%s) [%d - %d].",
|
|
|
|
optarg, 2, 1024);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2023-04-27 01:58:09 +03:00
|
|
|
minlength = (int)ul;
|
|
|
|
maxlength = (int)up;
|
2023-04-18 18:02:22 +03:00
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'n':
|
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
2023-04-15 18:21:56 +03:00
|
|
|
ul = strtoul(optarg, &ep, 10);
|
|
|
|
if (ep == optarg || *ep != '\0' ||
|
|
|
|
ul < 1 || ul > INT_MAX / 10 ) {
|
|
|
|
errx(1, "-n: invalid number of worms (%s).",
|
|
|
|
optarg);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
2023-04-15 18:21:56 +03:00
|
|
|
/* upper bound is further limited later */
|
|
|
|
number = (int)ul;
|
2023-04-18 18:02:22 +03:00
|
|
|
continue;
|
2023-04-27 01:58:09 +03:00
|
|
|
case 'r':
|
|
|
|
minlength = 5;
|
|
|
|
maxlength = 0;
|
|
|
|
continue;
|
2023-04-18 18:02:22 +03:00
|
|
|
case 'S':
|
|
|
|
ul = strtoul(optarg, &ep, 0);
|
|
|
|
if (ep == optarg || *ep != '\0' ||
|
|
|
|
ul > UINT_MAX ) {
|
|
|
|
errx(1, "-S: invalid seed (%s).", optarg);
|
|
|
|
}
|
|
|
|
seed = (unsigned int)ul;
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 't':
|
2023-04-27 01:58:09 +03:00
|
|
|
if (trail == ' ')
|
|
|
|
trail = '.';
|
|
|
|
else
|
|
|
|
trail = ' ';
|
2023-04-18 18:02:22 +03:00
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
case '?':
|
|
|
|
default:
|
2023-04-18 18:02:22 +03:00
|
|
|
argerror = true;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2023-04-18 18:02:22 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argerror || argc > optind)
|
|
|
|
errx(1,
|
2023-04-27 01:58:09 +03:00
|
|
|
"Usage: worms [-CfHrt] [-d delay] [-l length] [-n number]");
|
2023-04-18 18:02:22 +03:00
|
|
|
/* -S omitted deliberately, not useful often enough */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2008-08-08 20:10:47 +04:00
|
|
|
if (!initscr())
|
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
2023-04-15 18:21:56 +03:00
|
|
|
errx(1, "couldn't initialize screen");
|
2004-02-09 01:21:57 +03:00
|
|
|
curs_set(0);
|
2023-04-27 01:58:09 +03:00
|
|
|
nc = docolour ? initclr(&ctab) : 0;
|
1999-07-30 06:23:27 +04:00
|
|
|
CO = COLS;
|
|
|
|
LI = LINES;
|
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
2023-04-15 18:21:56 +03:00
|
|
|
|
2023-04-18 18:02:22 +03:00
|
|
|
if (CO == 0 || LI == 0) {
|
|
|
|
endwin();
|
|
|
|
errx(1, "screen must be a rectangle, not (%dx%d)", CO, LI);
|
|
|
|
}
|
2023-04-27 01:58:09 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The "sizeof(short *)" noise is to abslutely guarantee
|
|
|
|
* that a LI * CO * sizeof(short *) cannot overflow an int
|
|
|
|
*/
|
|
|
|
if (CO >= (int)(INT_MAX / (2 * sizeof(short *)) / LI)) {
|
2023-04-18 18:02:22 +03:00
|
|
|
endwin();
|
|
|
|
errx(1, "screen (%dx%d) too large for worms", CO, LI);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now known that LI*CO cannot overflow an int => also not a long */
|
|
|
|
|
|
|
|
if (LI < 3 || CO < 3 || LI * CO < 40) {
|
|
|
|
/*
|
|
|
|
* The placement algorithm is too weak for dimensions < 3.
|
|
|
|
* Need at least 40 spaces so we can have (n > 1) worms
|
|
|
|
* of a reasonable length, and still leave empty space.
|
|
|
|
*/
|
|
|
|
endwin();
|
|
|
|
errx(1, "screen (%dx%d) too small for worms", CO, LI);
|
|
|
|
}
|
|
|
|
|
2023-04-27 01:58:09 +03:00
|
|
|
if (maxlength == 0)
|
|
|
|
maxlength = minlength + (CO * LI / 40);
|
|
|
|
|
2023-04-18 18:02:22 +03:00
|
|
|
ul = (unsigned long)CO * LI;
|
2023-04-27 01:58:09 +03:00
|
|
|
if ((unsigned long)maxlength > ul / 20) {
|
2023-04-18 18:02:22 +03:00
|
|
|
endwin();
|
2023-04-19 12:39:29 +03:00
|
|
|
errx(1, "-l: worms too long (%d) for screen; max: %lu",
|
2023-04-27 01:58:09 +03:00
|
|
|
maxlength, ul / 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
ul /= maxlength * 3; /* no more than 33% arena occupancy */
|
|
|
|
|
|
|
|
if ((unsigned long)(unsigned)number > ul && maxlength > minlength) {
|
|
|
|
maxlength = CO * LI / 3 / number;
|
|
|
|
if (maxlength < minlength)
|
|
|
|
maxlength = minlength;
|
|
|
|
ul = (CO * LI) / ((minlength + maxlength)/2 * 3);;
|
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
2023-04-15 18:21:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((unsigned long)(unsigned)number > ul) {
|
|
|
|
endwin();
|
|
|
|
errx(1, "-n: too many worms (%d) max: %lu", number, ul);
|
|
|
|
}
|
|
|
|
if (!(worm = calloc((size_t)number, sizeof(struct worm))))
|
|
|
|
nomem();
|
|
|
|
|
2023-04-27 01:58:09 +03:00
|
|
|
srandom(seed ? seed : arc4random());
|
|
|
|
|
1994-04-05 04:21:20 +04:00
|
|
|
last = CO - 1;
|
|
|
|
bottom = LI - 1;
|
2023-04-27 01:58:09 +03:00
|
|
|
|
|
|
|
if (!(ip = calloc(LI * CO, sizeof(short))))
|
1993-03-21 12:45:37 +03:00
|
|
|
nomem();
|
2023-04-27 01:58:09 +03:00
|
|
|
if (!(ref = malloc((size_t)LI * sizeof(short *))))
|
1993-03-21 12:45:37 +03:00
|
|
|
nomem();
|
|
|
|
for (n = 0; n < LI; ++n) {
|
|
|
|
ref[n] = ip;
|
|
|
|
ip += CO;
|
|
|
|
}
|
2023-04-27 01:58:09 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
for (n = number, w = &worm[0]; --n >= 0; w++) {
|
2023-04-27 01:58:09 +03:00
|
|
|
int i;
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
w->orientation = w->head = 0;
|
2023-04-27 01:58:09 +03:00
|
|
|
w->len = worm_length(minlength, maxlength);
|
|
|
|
w->attr = nc ? ctab[n % nc] : 0;
|
|
|
|
i = (nc && number > flavors ? n / nc : n) % flavors;
|
|
|
|
w->ch = flavor[i];
|
2023-05-12 16:29:41 +03:00
|
|
|
w->eye = eyeball[i];
|
2023-04-27 01:58:09 +03:00
|
|
|
|
|
|
|
if (!(ip = malloc((size_t)(w->len * sizeof(short)))))
|
1993-03-21 12:45:37 +03:00
|
|
|
nomem();
|
|
|
|
w->xpos = ip;
|
2023-04-27 01:58:09 +03:00
|
|
|
for (x = w->len; --x >= 0;)
|
1993-03-21 12:45:37 +03:00
|
|
|
*ip++ = -1;
|
2023-04-27 01:58:09 +03:00
|
|
|
if (!(ip = malloc((size_t)(w->len * sizeof(short)))))
|
1993-03-21 12:45:37 +03:00
|
|
|
nomem();
|
|
|
|
w->ypos = ip;
|
2023-04-27 01:58:09 +03:00
|
|
|
for (y = w->len; --y >= 0;)
|
1993-03-21 12:45:37 +03:00
|
|
|
*ip++ = -1;
|
|
|
|
}
|
2023-04-27 01:58:09 +03:00
|
|
|
free(ctab); /* not needed any more */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
(void)signal(SIGHUP, onsig);
|
|
|
|
(void)signal(SIGINT, onsig);
|
|
|
|
(void)signal(SIGQUIT, onsig);
|
|
|
|
(void)signal(SIGTERM, onsig);
|
2023-04-27 01:58:09 +03:00
|
|
|
(void)signal(SIGTSTP, onsig);
|
|
|
|
(void)signal(SIGWINCH, onsig);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (field) {
|
1999-07-30 06:23:27 +04:00
|
|
|
const char *p = field;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1999-07-30 06:23:27 +04:00
|
|
|
for (y = LI; --y >= 0;) {
|
1993-03-21 12:45:37 +03:00
|
|
|
for (x = CO; --x >= 0;) {
|
1999-07-30 06:23:27 +04:00
|
|
|
addch(*p++);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!*p)
|
|
|
|
p = field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 01:58:09 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
for (;;) {
|
1999-07-30 06:23:27 +04:00
|
|
|
refresh();
|
|
|
|
if (sig_caught) {
|
|
|
|
endwin();
|
|
|
|
exit(0);
|
|
|
|
}
|
2004-03-30 00:30:03 +04:00
|
|
|
if (delay) {
|
|
|
|
if (delay % 1000000 != 0)
|
|
|
|
usleep(delay % 1000000);
|
|
|
|
if (delay >= 1000000)
|
|
|
|
sleep(delay / 1000000);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
for (n = 0, w = &worm[0]; n < number; n++, w++) {
|
2023-05-12 16:29:41 +03:00
|
|
|
chtype c = docaput ? w->eye : w->ch;
|
2023-04-27 01:58:09 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if ((x = w->xpos[h = w->head]) < 0) {
|
1999-07-30 06:23:27 +04:00
|
|
|
mvaddch(y = w->ypos[h] = bottom,
|
2023-04-27 01:58:09 +03:00
|
|
|
x = w->xpos[h] = 0, c | w->attr);
|
1993-03-21 12:45:37 +03:00
|
|
|
ref[y][x]++;
|
2023-04-27 01:58:09 +03:00
|
|
|
} else
|
1993-03-21 12:45:37 +03:00
|
|
|
y = w->ypos[h];
|
2023-04-27 01:58:09 +03:00
|
|
|
if (++h == w->len)
|
1993-03-21 12:45:37 +03:00
|
|
|
h = 0;
|
|
|
|
if (w->xpos[w->head = h] >= 0) {
|
1997-10-12 06:12:45 +04:00
|
|
|
int x1, y1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
x1 = w->xpos[h];
|
|
|
|
y1 = w->ypos[h];
|
2023-04-27 01:58:09 +03:00
|
|
|
if (--ref[y1][x1] == 0)
|
1999-07-30 06:23:27 +04:00
|
|
|
mvaddch(y1, x1, trail);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2023-04-15 16:40:23 +03:00
|
|
|
|
|
|
|
op = &(!x
|
|
|
|
? (!y
|
|
|
|
? upleft
|
|
|
|
: (y == bottom ? lowleft : left))
|
|
|
|
: (x == last
|
|
|
|
? (!y ? upright
|
|
|
|
: (y == bottom ? lowright : right))
|
|
|
|
: (!y ? upper
|
|
|
|
: (y == bottom ? lower : normal)))
|
|
|
|
)[w->orientation];
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
switch (op->nopts) {
|
|
|
|
case 0:
|
2023-04-18 18:02:22 +03:00
|
|
|
endwin();
|
1993-03-21 12:45:37 +03:00
|
|
|
abort();
|
2023-04-18 18:02:22 +03:00
|
|
|
/* NOTREACHED */
|
1993-03-21 12:45:37 +03:00
|
|
|
case 1:
|
|
|
|
w->orientation = op->opts[0];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
w->orientation =
|
|
|
|
op->opts[(int)random() % op->nopts];
|
|
|
|
}
|
1999-07-30 06:23:27 +04:00
|
|
|
mvaddch(y += yinc[w->orientation],
|
|
|
|
x += xinc[w->orientation],
|
2023-04-27 01:58:09 +03:00
|
|
|
c | w->attr);
|
1993-03-21 12:45:37 +03:00
|
|
|
ref[w->ypos[h] = y][w->xpos[h] = x]++;
|
2023-04-27 01:58:09 +03:00
|
|
|
if (docaput && w->len > 1) {
|
|
|
|
int prev = (h ? h : w->len) - 1;
|
|
|
|
|
|
|
|
mvaddch(w->ypos[prev], w->xpos[prev],
|
|
|
|
w->ch | w->attr);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 01:58:09 +03:00
|
|
|
static int
|
|
|
|
initclr(int** ctab)
|
|
|
|
{
|
|
|
|
int *ip, clr[] = {
|
|
|
|
COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW,
|
|
|
|
COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE
|
|
|
|
}, attr[] = {
|
|
|
|
A_NORMAL, A_BOLD, A_DIM
|
|
|
|
};
|
|
|
|
int nattr = __arraycount(attr);
|
|
|
|
int nclr = __arraycount(clr);
|
|
|
|
int nc = 0;
|
|
|
|
|
|
|
|
/* terminfo first */
|
|
|
|
char* s;
|
|
|
|
bool canbold = (s = tigetstr("bold")) != (char* )-1 && s != NULL;
|
|
|
|
bool candim = (s = tigetstr("dim")) != (char* )-1 && s != NULL;
|
|
|
|
|
|
|
|
#ifdef DO_TERMCAP
|
|
|
|
/* termcap if terminfo fails */
|
|
|
|
canbold = canbold || (s = tgetstr("md", NULL)) != NULL;
|
|
|
|
candim = candim || (s = tgetstr("mh", NULL)) != NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (has_colors() == FALSE)
|
|
|
|
return 0;
|
|
|
|
use_default_colors();
|
|
|
|
if (start_color() == ERR)
|
|
|
|
return 0;
|
|
|
|
if ((*ctab = calloc(COLOR_PAIRS, sizeof(int))) == NULL)
|
|
|
|
nomem();
|
|
|
|
ip = *ctab;
|
|
|
|
|
|
|
|
for (int i = 0; i < nattr; i++) {
|
|
|
|
if (!canbold && attr[i] == A_BOLD)
|
|
|
|
continue;
|
|
|
|
if (!candim && attr[i] == A_DIM)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int j = 0; j < nclr; j++) {
|
|
|
|
if (clr[j] == COLOR_BLACK && attr[i] != A_BOLD)
|
|
|
|
continue; /* invisible */
|
|
|
|
if (nc + 1 >= COLOR_PAIRS)
|
|
|
|
break;
|
|
|
|
if (init_pair(nc + 1, clr[j], -1) == ERR)
|
|
|
|
break;
|
|
|
|
*ip++ = COLOR_PAIR(nc + 1) | attr[i];
|
|
|
|
nc++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
worm_length(int low, int high)
|
|
|
|
{
|
|
|
|
if (low >= high)
|
|
|
|
return low;
|
|
|
|
|
|
|
|
return low + (random() % (high - low + 1));
|
|
|
|
}
|
|
|
|
|
2009-08-12 12:54:54 +04:00
|
|
|
static void
|
2012-06-19 09:46:08 +04:00
|
|
|
onsig(int signo __unused)
|
1995-04-22 12:00:54 +04:00
|
|
|
{
|
1999-07-30 06:23:27 +04:00
|
|
|
sig_caught = 1;
|
1995-04-22 12:00:54 +04:00
|
|
|
}
|
|
|
|
|
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
2023-04-15 18:21:56 +03:00
|
|
|
/* This is never called before curses is initialised */
|
2009-08-12 12:54:54 +04:00
|
|
|
static void
|
2012-06-19 09:46:08 +04:00
|
|
|
nomem(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
2023-04-15 18:21:56 +03:00
|
|
|
endwin();
|
1999-07-30 06:23:27 +04:00
|
|
|
errx(1, "not enough memory.");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|