2 * beep: quickly pipe text data into, or out of, the primary X
20 const char usagemsg
[] =
21 "usage: beep [ -v ] ["
28 "-X beep using the X display or not at all\n "
30 "-T beep using /dev/tty or not at all\n"
31 " -S beep using standard output or not at all\n"
32 " -v log failed and successful beep attempts\n"
33 " also: beep --version report version number\n"
34 " beep --help display this help text\n"
35 " beep --licence display the (MIT) licence text\n"
39 fputs(usagemsg
, stdout
);
42 const char licencemsg
[] =
43 "beep is copyright 2006 Simon Tatham.\n"
45 "Permission is hereby granted, free of charge, to any person\n"
46 "obtaining a copy of this software and associated documentation files\n"
47 "(the \"Software\"), to deal in the Software without restriction,\n"
48 "including without limitation the rights to use, copy, modify, merge,\n"
49 "publish, distribute, sublicense, and/or sell copies of the Software,\n"
50 "and to permit persons to whom the Software is furnished to do so,\n"
51 "subject to the following conditions:\n"
53 "The above copyright notice and this permission notice shall be\n"
54 "included in all copies or substantial portions of the Software.\n"
56 "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n"
57 "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n"
58 "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n"
59 "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n"
60 "BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n"
61 "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n"
62 "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n"
67 fputs(licencemsg
, stdout
);
71 #define SVN_REV "$Revision$"
72 char rev
[sizeof(SVN_REV
)];
77 for (p
= rev
; *p
&& *p
!= ':'; p
++);
80 while (*p
&& isspace((unsigned char)*p
)) p
++;
81 for (q
= p
; *q
&& !isspace((unsigned char)*q
) && *q
!= '$'; q
++);
83 printf("beep revision %s", p
);
85 printf("beep: unknown version");
88 printf(", compiled without X11");
93 int main(int argc
, char **argv
) {
98 enum { X11
= 1, TTY
= 2, STDOUT
= 4 } mode
= X11
| TTY
| STDOUT
;
102 char *pname
= argv
[0];
104 /* parse the command line arguments */
109 if (!strcmp(p
, "-display") || !strcmp(p
, "-disp")) {
111 fprintf(stderr
, "%s: option `%s' expects a parameter\n",
115 display
= *++argv
, --argc
;
116 } else if (!strcmp(p
, "-X")) {
120 if (!strcmp(p
, "-T")) {
122 } else if (!strcmp(p
, "-S")) {
124 } else if (!strcmp(p
, "-v")) {
126 } else if (!strcmp(p
, "--help")) {
129 } else if (!strcmp(p
, "--version")) {
132 } else if (!strcmp(p
, "--licence") || !strcmp(p
, "--license")) {
135 } else if (*p
=='-') {
136 fprintf(stderr
, "%s: unrecognised option `%s'\n", pname
, p
);
139 fprintf(stderr
, "%s: parameter `%s' unexpected\n", pname
, p
);
145 if (!done
&& (mode
& X11
)) {
146 Display
*disp
= XOpenDisplay(display
);
148 errlen
+= sprintf(errbuf
+errlen
,
149 "%s: unable to open X display\n", pname
);
154 errlen
+= sprintf(errbuf
+errlen
,
155 "%s: successfully beeped via X11\n", pname
);
162 if (!done
&& (mode
& TTY
)) {
163 int fd
= open("/dev/tty", O_WRONLY
);
165 errlen
+= sprintf(errbuf
+errlen
,
166 "%s: unable to open /dev/tty: %s\n",
167 pname
, strerror(errno
));
169 if (write(fd
, "\007", 1) < 0) {
170 errlen
+= sprintf(errbuf
+errlen
, "%s: unable to write to"
171 " /dev/tty: %s\n", pname
, strerror(errno
));
174 errlen
+= sprintf(errbuf
+errlen
, "%s: successfully beeped"
175 " via /dev/tty\n", pname
);
183 if (!done
&& (mode
& STDOUT
)) {
184 if (write(1, "\007", 1) < 0) {
185 errlen
+= sprintf(errbuf
+errlen
, "%s: unable to write to standard "
186 "output: %s\n", pname
, strerror(errno
));
189 errlen
+= sprintf(errbuf
+errlen
, "%s: successfully beeped"
190 " via standard output\n", pname
);
196 if (verbose
|| !done
) {
197 errbuf
[errlen
] = '\0';
198 fputs(errbuf
, stderr
);
201 return (done ? EXIT_SUCCESS
: EXIT_FAILURE
);