1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-25 11:37:56 +00:00
Use ANSI functions rather than old K&R style.
This commit is contained in:
Warner Losh 2002-06-12 04:18:35 +00:00
parent d2c462cec8
commit 30322d95ac
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=98139

View File

@ -69,95 +69,98 @@ char *X10cmdnames[] = {
}; };
int find(char *, char *[]); int find(char *, char *[]);
static void usage(void); static void usage(void) __dead2;
int int
main(argc, argv) main(int argc, char *argv[])
int argc;
char *argv[];
{ {
int c, tmp, h, k, sock, error; int c, tmp, h, k, sock, error;
FILE *daemon; FILE *daemon;
struct sockaddr_un sa; struct sockaddr_un sa;
char *sockpath = SOCKPATH; char *sockpath = SOCKPATH;
char reply[CMDLEN], cmd[CMDLEN], *cp; char reply[CMDLEN], cmd[CMDLEN], *cp;
int interactive = 0; int interactive = 0;
if(argc == 2 && !strcmp(argv[1], "-")) interactive++; if (argc == 2 && !strcmp(argv[1], "-"))
else if(argc < 3) interactive++;
usage(); else if(argc < 3)
if((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) usage();
errx(1, "can't create socket"); if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
strcpy(sa.sun_path, sockpath); errx(1, "can't create socket");
sa.sun_family = AF_UNIX; strcpy(sa.sun_path, sockpath);
if(connect(sock, (struct sockaddr *)(&sa), strlen(sa.sun_path) + 2) < 0) sa.sun_family = AF_UNIX;
errx(1, "can't connect to X-10 daemon"); if (connect(sock, (struct sockaddr *)&sa, strlen(sa.sun_path) + 2) < 0)
if((daemon = fdopen(sock, "w+")) == NULL) errx(1, "can't connect to X-10 daemon");
errx(1, "can't attach stream to socket"); if ((daemon = fdopen(sock, "w+")) == NULL)
/* errx(1, "can't attach stream to socket");
* If interactive, copy standard input to daemon and report results /*
* on standard output. * If interactive, copy standard input to daemon and report results
*/ * on standard output.
if(interactive) { */
while(!feof(stdin)) { if (interactive) {
if(fgets(cmd, CMDLEN, stdin) != NULL) { while (!feof(stdin)) {
fprintf(daemon, "%s", cmd); if (fgets(cmd, CMDLEN, stdin) != NULL) {
fflush(daemon); fprintf(daemon, "%s", cmd);
if(fgets(reply, CMDLEN, daemon) != NULL) { fflush(daemon);
fprintf(stdout, "%s", reply); if (fgets(reply, CMDLEN, daemon) != NULL) {
fflush(stdout); fprintf(stdout, "%s", reply);
fflush(stdout);
}
}
}
exit(0);
} }
} /*
} * Otherwise, interpret arguments and issue commands to daemon,
exit(0); * handling retries in case of errors.
} */
/* if ((h = find(argv[1], X10housenames)) < 0)
* Otherwise, interpret arguments and issue commands to daemon, errx(1, "invalid house code: %s", argv[1]);
* handling retries in case of errors. argv++;
*/ argv++;
if((h = find(argv[1], X10housenames)) < 0) while (argc >= 3) {
errx(1, "invalid house code: %s", argv[1]); cp = argv[0];
argv++; if((tmp = find(cp, X10housenames)) >= 0) {
argv++; h = tmp;
while(argc >= 3) { argv++;
cp = argv[0]; argc--;
if((tmp = find(cp, X10housenames)) >= 0) { continue;
h = tmp; }
argv++; while (*cp != '\0' && *cp != ':')
argc--; cp++;
continue; if (*cp == ':')
} c = atoi(cp+1);
while(*cp != '\0' && *cp != ':') cp++; else
if(*cp == ':') c = atoi(cp+1); c = 2;
else c = 2; *cp = '\0';
*cp = '\0'; if ((k = find(argv[0], X10cmdnames)) < 0) {
if((k = find(argv[0], X10cmdnames)) < 0) { warnx("invalid key/unit code: %s", argv[0]);
warnx("invalid key/unit code: %s", argv[0]); error++;
error++; }
} error = 0;
error = 0; while (error < RETRIES) {
while(error < RETRIES) { fprintf(daemon, "send %s %s %d\n", X10housenames[h],
fprintf(daemon, "send %s %s %d\n", X10housenames[h], X10cmdnames[k], c); X10cmdnames[k], c);
fflush(daemon); fflush(daemon);
fgets(reply, CMDLEN, daemon); fgets(reply, CMDLEN, daemon);
if(strncmp(reply, "ERROR", 5)) break; if(strncmp(reply, "ERROR", 5)) break;
error++; error++;
usleep(200000); usleep(200000);
} }
if(error == RETRIES) { if (error == RETRIES) {
warnx("command failed: send %s %s %d", warnx("command failed: send %s %s %d",
X10housenames[h], X10cmdnames[k], c); X10housenames[h], X10cmdnames[k], c);
} }
argc--; argc--;
argv++; argv++;
} }
fprintf(daemon, "done\n"); fprintf(daemon, "done\n");
fgets(reply, CMDLEN, daemon); fgets(reply, CMDLEN, daemon);
exit(0); exit(0);
} }
static void static void
usage() usage(void)
{ {
fprintf(stderr, fprintf(stderr,
"usage: xten house key[:cnt] [[house] key[:cnt] ...]\n"); "usage: xten house key[:cnt] [[house] key[:cnt] ...]\n");
@ -165,14 +168,13 @@ usage()
} }
int int
find(s, tab) find(char *s, char *tab[])
char *s;
char *tab[];
{ {
int i; int i;
for(i = 0; tab[i] != NULL; i++) { for (i = 0; tab[i] != NULL; i++) {
if(strcasecmp(s, tab[i]) == 0) return(i); if (strcasecmp(s, tab[i]) == 0)
return (i);
} }
return(-1); return (-1);
} }