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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/* $NetBSD: bellctrl.c,v 1.13 2011/08/25 17:00:55 christos Exp $ */
/*
* bellctrl - OPM bell controller (for NetBSD/X680x0)
* Copyright (c)1995 ussy.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: bellctrl.c,v 1.13 2011/08/25 17:00:55 christos Exp $");
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <machine/opmbellio.h>
#define DEFAULT -1
#define nextarg(i, argv) \
argv[i]; \
if (i >= argc) \
break; \
static int bell_setting;
static struct opm_voice voice;
static struct opm_voice bell_voice = DEFAULT_BELL_VOICE;
static struct bell_info values = {
DEFAULT, DEFAULT, DEFAULT
};
/* function prototype */
static int is_number(const char *, int);
static void set_bell_vol(int);
static void set_bell_pitch(int);
static void set_bell_dur(int);
static void set_voice_param(const char *, int);
static void set_bell_param(void);
static void usage(void) __dead;
int
main(int argc, char **argv)
{
const char *arg;
int percent;
int i;
bell_setting = 0;
if (argc < 2)
usage();
for (i = 1; i < argc; ) {
arg = argv[i++];
if (strcmp(arg, "-b") == 0) {
/* turn off bell */
set_bell_vol(0);
} else if (strcmp(arg, "b") == 0) {
/* set bell to default */
percent = DEFAULT;
if (i >= argc) {
/* set bell to default */
set_bell_vol(percent);
/* set pitch to default */
set_bell_pitch(percent);
/* set duration to default */
set_bell_dur(percent);
break;
}
arg = nextarg(i, argv);
if (strcmp(arg, "on") == 0) {
/*
* let it stay that way
*/
/* set bell on */
set_bell_vol(BELL_VOLUME);
/* set pitch to default */
set_bell_pitch(BELL_PITCH);
/* set duration to default */
set_bell_dur(BELL_DURATION);
i++;
} else if (strcmp(arg, "off") == 0) {
/* turn the bell off */
percent = 0;
set_bell_vol(percent);
i++;
} else if (is_number(arg, MAXBVOLUME)) {
/*
* If volume is given
*/
/* set bell appropriately */
percent = atoi(arg);
set_bell_vol(percent);
i++;
arg = nextarg(i, argv);
/* if pitch is given */
if (is_number(arg, MAXBPITCH)) {
/* set the bell */
set_bell_pitch(atoi(arg));
i++;
arg = nextarg(i, argv);
/* If duration is given */
if (is_number(arg, MAXBTIME)) {
/* set the bell */
set_bell_dur(atoi(arg));
i++;
}
}
} else {
/* set bell to default */
set_bell_vol(BELL_VOLUME);
}
} else if (strcmp(arg, "v") == 0) {
/*
* set voice parameter
*/
if (i >= argc) {
arg = "default";
} else {
arg = nextarg(i, argv);
}
set_voice_param(arg, 1);
i++;
} else if (strcmp(arg, "-v") == 0) {
/*
* set voice parameter
*/
if (i >= argc) {
warnx("Missing -v argument");
usage();
}
arg = nextarg(i, argv);
set_voice_param(arg, 0);
i++;
} else {
warnx("Unknown option %s", arg);
usage();
}
}
if (bell_setting)
set_bell_param();
exit(0);
}
static int
is_number(const char *arg, int maximum)
{
const char *p;
if (arg[0] == '-' && arg[1] == '1' && arg[2] == '\0')
return 1;
for (p = arg; isdigit((unsigned char)*p); p++)
;
if (*p || atoi(arg) > maximum)
return 0;
return 1;
}
static void
set_bell_vol(int percent)
{
values.volume = percent;
bell_setting++;
}
static void
set_bell_pitch(int pitch)
{
values.pitch = pitch;
bell_setting++;
}
static void
set_bell_dur(int duration)
{
values.msec = duration;
bell_setting++;
}
static void
set_voice_param(const char *path, int flag)
{
int fd;
if (flag) {
memcpy(&voice, &bell_voice, sizeof(bell_voice));
} else {
if ((fd = open(path, 0)) >= 0) {
if (read(fd, &voice, sizeof(voice)) != sizeof(voice))
err(1, "cannot read voice parameter");
close(fd);
} else {
err(1, "cannot open voice parameter");
}
}
if ((fd = open("/dev/bell", O_RDWR)) < 0)
err(1, "cannot open /dev/bell");
if (ioctl(fd, BELLIOCSVOICE, &voice))
err(1, "ioctl BELLIOCSVOICE failed");
close(fd);
}
static void
set_bell_param(void)
{
int fd;
struct bell_info param;
if ((fd = open("/dev/bell", O_RDWR)) < 0)
err(1, "cannot open /dev/bell");
if (ioctl(fd, BELLIOCGPARAM, ¶m))
err(1, "ioctl BELLIOCGPARAM failed");
if (values.volume == DEFAULT)
values.volume = param.volume;
if (values.pitch == DEFAULT)
values.pitch = param.pitch;
if (values.msec == DEFAULT)
values.msec = param.msec;
if (ioctl(fd, BELLIOCSPARAM, &values))
err(1, "ioctl BELLIOCSPARAM failed");
close(fd);
}
static void
usage(void)
{
fprintf(stderr, "Usage: %s option ...\n", getprogname());
fprintf(stderr, " To turn bell off:\n");
fprintf(stderr, "\t-b b off"
" b 0\n");
fprintf(stderr, " To set bell volume, pitch and duration:\n");
fprintf(stderr, "\t b [vol [pitch [dur]]] b on\n");
fprintf(stderr, " To restore default voice parameter:\n");
fprintf(stderr, "\t v default\n");
fprintf(stderr, " To set voice parameter:\n");
fprintf(stderr, "\t-v voicefile\n");
exit(0);
}
|