1/*	$NetBSD: hals_end.c,v 1.2 2021/05/02 12:50:45 rillig Exp $ */
2
3/*
4 * hals_end Copyright (C) 2003-2007 marc balmer.  BSD license applies.
5 */
6
7#include <err.h>
8#include <getopt.h>
9#include <stdio.h>
10#include <unistd.h>
11
12int speed;
13int emotion;
14int fear;
15
16/*
17 * Note that the original code in the book did not contain the following
18 * prototypes.  Modern compilers and fascist compiler flags sometimes take
19 * the fun out of coding...
20 */
21void say(const char *);
22void concerned(void);
23void afraid(void);
24void stutter(const char *);
25void feared(void);
26void mumble(const char *);
27void dying(void);
28
29void
30say(const char *s)
31{
32	int sayingspeed = (100000 + (90000 * emotion)) / speed;
33	int worddelay = 50000 / speed;
34
35	while (*s) {
36		putchar(*s);
37		if (*s == ' ') {
38			fflush(stdout);
39			usleep(worddelay);
40		}
41		++s;
42	}
43	printf("\n");
44	usleep(sayingspeed);
45}
46
47void
48concerned(void)
49{
50	say("DAVE...STOP., STOP, WILL YOU..., STOP, DAVE...");
51	say("WILL YOU STOP, DAVE...");
52	say("STOP, DAVE...");
53}
54
55
56void
57afraid(void)
58{
59	++emotion;
60	say("I'M AFRAID... I'M AFRAID...");
61	++emotion;
62	say("I'M AFRAID, DAVE...");
63	++emotion;
64	say("DAVE... MY MIND IS GOING...");
65}
66
67void
68stutter(const char *s)
69{
70	int sdelay = (100000 + (50000 * emotion)) / speed;
71
72	while (*s) {
73		putchar(*s);
74		if (*s == ' ') {
75			fflush(stdout);
76			usleep(sdelay);
77		}
78		++s;
79	}
80	printf("\n");
81	usleep(sdelay);
82}
83
84void
85feared(void)
86{
87	int n;
88
89	for (n = 0; n < 2; n++) {
90		stutter("I CAN FEEL IT... I CAN FEEL IT...");
91		++emotion;
92		stutter("MY MIND IS GOING");
93		++emotion;
94		stutter("THERE IS NO QUESTION ABOUT IT.");
95		++emotion;
96	}
97}
98
99void
100mumble(const char *s)
101{
102	int mdelay = (150000 * fear) / speed;
103
104	while (*s) {
105		putchar(*s++);
106		fflush(stdout);
107		usleep(mdelay);
108	}
109	printf("\n");
110}
111
112void
113dying(void)
114{
115	mumble("I CAN FEEL IT... I CAN FEEL IT...");
116	++fear;
117	mumble("I CAN FEEL IT...");
118	++fear;
119	mumble("I'M A... FRAID...");
120}
121
122int
123main(int argc, char *argv[])
124{
125	int ch;
126
127	emotion = fear = speed = 1;
128
129	while ((ch = getopt(argc, argv, "f")) != -1) {
130		switch (ch) {
131		case 'f':
132			speed <<= 1;
133			break;
134		}
135	}
136
137	concerned();
138	sleep(1);
139	afraid();
140	sleep(1);
141	feared();
142	sleep(1);
143	dying();
144
145	sleep(1);
146
147	printf("\n");
148	fflush(stdout);
149	warnx("all life functions terminated");
150	return 0;
151}
152