/* * Copyright (C) 1997-2000 SpellCaster Telecommunications Inc. * $Id: bdial.c,v 1.2 2004/08/16 01:33:48 bcrl Exp $ * Released under the GNU Public License. See LICENSE file for details. */ #include #include #include #include #include #include #include #include #define CONTROL_PATH "/tmp/.bab_control" static void error(const char *huh) { perror(huh); exit(1); } int parse_in(const char *s, int *retp) { int status, i=0; unsigned int callid; int matched; matched = sscanf(s, "[%u] %d:%n", &callid, &status, &i); if (s[0] != '[' || !matched) { printf("%s\n", s+i); fflush(stdout); return 0; } if (s[i]) i++; if (!status) { printf("%s\n", s+i); fflush(stdout); } else if (status == -1) { if (s[i]) printf("%s\n", s+i); *retp = 0; return 1; } else if (status < -1) { if (s[i]) printf("Failed: %s\n", s+i); *retp = -status - 1; return 1; } else { printf("Failed with cause 0x%02x: %s\n", status, s+i); *retp = 1; return 1; } return 0; } int s_copy(int dest_fd, int s_fd, fd_set *fds, int *retp) { static char current_line[4096]; static unsigned current_pos; int ret = 0; if (FD_ISSET(s_fd, fds)) { char *bp; int len, i, begin; bp = current_line + current_pos; len = read(s_fd, bp, sizeof(current_line) - current_pos - 1); if (len < 0) error("read"); if (len == 0) { fprintf(stderr, "[quit -- read]\n"); *retp = 3; return 1; } current_pos += len; begin = 0; for (i=0; i 2 && !strncmp(argv[1], "-C", 2)) { control_path = argv[2]; skip_args += 2; } if (argc > (skip_args + 1) && !strncmp(argv[skip_args + 1], "--repeat=", 9)) { repeats = atoi(argv[skip_args + 1] + 9); fprintf(stderr, "repeats: %d\n", repeats); skip_args ++; } again: fd = socket(AF_UNIX, SOCK_STREAM, 0); if (-1 == fd) error("socket"); addr.sun_family = AF_UNIX; strcpy(addr.sun_path, control_path); if (-1 == connect(fd, (struct sockaddr *)&addr, sizeof(addr.sun_family)+strlen(addr.sun_path))) error("Unable to connect to server(" CONTROL_PATH ")"); for (i=0; i= 1 && i <= skip_args) continue; if (i && write(fd, " ", 1) <= 0) error("write[' ']"); while (len > 0) { int ret = write(fd, argv[i] + pos, len); if (ret < 0) error("write[main]"); len -= ret; pos += ret; } } if (i && write(fd, "\n", 1) <= 0) error("write['\n']"); FD_ZERO(&rfds); for (;;) { /*FD_SET(0, &rfds);*/ FD_SET(fd, &rfds); if (-1 == select(fd+1, &rfds, NULL, NULL, NULL)) error("select"); if (s_copy(0, fd, &rfds, &ret)) break; if (s_copy(fd, 0, &rfds, &ret)) break; } close(fd); if (--repeats > 0) goto again; return ret; }