2 * beep: make a beeping noise, by any means necessary
19 const char usagemsg
[] =
20 "usage: beep [ -v ] ["
27 "-X beep using the X display or not at all\n "
29 "-T beep using /dev/tty or not at all\n"
30 " -S beep using standard output or not at all\n"
31 " -v log failed and successful beep attempts\n"
32 " also: beep --version report version number\n"
33 " beep --help display this help text\n"
34 " beep --licence display the (MIT) licence text\n"
38 fputs(usagemsg
, stdout
);
41 const char licencemsg
[] =
42 "beep is copyright 2006 Simon Tatham.\n"
44 "Permission is hereby granted, free of charge, to any person\n"
45 "obtaining a copy of this software and associated documentation files\n"
46 "(the \"Software\"), to deal in the Software without restriction,\n"
47 "including without limitation the rights to use, copy, modify, merge,\n"
48 "publish, distribute, sublicense, and/or sell copies of the Software,\n"
49 "and to permit persons to whom the Software is furnished to do so,\n"
50 "subject to the following conditions:\n"
52 "The above copyright notice and this permission notice shall be\n"
53 "included in all copies or substantial portions of the Software.\n"
55 "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n"
56 "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n"
57 "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n"
58 "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n"
59 "BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n"
60 "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n"
61 "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n"
66 fputs(licencemsg
, stdout
);
70 #define SVN_REV "$Revision$"
71 char rev
[sizeof(SVN_REV
)];
76 for (p
= rev
; *p
&& *p
!= ':'; p
++);
79 while (*p
&& isspace((unsigned char)*p
)) p
++;
80 for (q
= p
; *q
&& !isspace((unsigned char)*q
) && *q
!= '$'; q
++);
82 printf("beep revision %s", p
);
84 printf("beep: unknown version");
87 printf(", compiled without X11");
92 int main(int argc
, char **argv
) {
97 enum { X11
= 1, TTY
= 2, STDOUT
= 4 } mode
= X11
| TTY
| STDOUT
;
101 char *pname
= argv
[0];
103 /* parse the command line arguments */
108 if (!strcmp(p
, "-display") || !strcmp(p
, "-disp")) {
110 fprintf(stderr
, "%s: option `%s' expects a parameter\n",
114 display
= *++argv
, --argc
;
115 } else if (!strcmp(p
, "-X")) {
119 if (!strcmp(p
, "-T")) {
121 } else if (!strcmp(p
, "-S")) {
123 } else if (!strcmp(p
, "-v")) {
125 } else if (!strcmp(p
, "--help")) {
128 } else if (!strcmp(p
, "--version")) {
131 } else if (!strcmp(p
, "--licence") || !strcmp(p
, "--license")) {
134 } else if (*p
=='-') {
135 fprintf(stderr
, "%s: unrecognised option `%s'\n", pname
, p
);
138 fprintf(stderr
, "%s: parameter `%s' unexpected\n", pname
, p
);
144 if (!done
&& (mode
& X11
)) {
145 Display
*disp
= XOpenDisplay(display
);
147 errlen
+= sprintf(errbuf
+errlen
,
148 "%s: unable to open X display\n", pname
);
153 errlen
+= sprintf(errbuf
+errlen
,
154 "%s: successfully beeped via X11\n", pname
);
161 if (!done
&& (mode
& TTY
)) {
162 int fd
= open("/dev/tty", O_WRONLY
);
164 errlen
+= sprintf(errbuf
+errlen
,
165 "%s: unable to open /dev/tty: %s\n",
166 pname
, strerror(errno
));
168 if (write(fd
, "\007", 1) < 0) {
169 errlen
+= sprintf(errbuf
+errlen
, "%s: unable to write to"
170 " /dev/tty: %s\n", pname
, strerror(errno
));
173 errlen
+= sprintf(errbuf
+errlen
, "%s: successfully beeped"
174 " via /dev/tty\n", pname
);
182 if (!done
&& (mode
& STDOUT
)) {
183 if (write(1, "\007", 1) < 0) {
184 errlen
+= sprintf(errbuf
+errlen
, "%s: unable to write to standard "
185 "output: %s\n", pname
, strerror(errno
));
188 errlen
+= sprintf(errbuf
+errlen
, "%s: successfully beeped"
189 " via standard output\n", pname
);
195 if (verbose
|| !done
) {
196 errbuf
[errlen
] = '\0';
197 fputs(errbuf
, stderr
);
200 return (done ? EXIT_SUCCESS
: EXIT_FAILURE
);