options.buf = malloc(sizeof(char) * MAX_LEN);
}
-static struct line *find_lines(void)
+static struct line *find_lines(int size)
{
/* printf("entering find_lines\n"); */
struct line *lines = malloc(sizeof(struct line));
char *ptr = options.buf;
lines->buf = options.buf;
lines->next = NULL;
-
+ int c = 0;
/* printf("starting loop.\n"); */
while(1){
+ if(c >= size){
+ /* printf("killing read at end of reported read.\n"); */
+ /* printf("(3) used [%d] of [%d]\n", c, size); */
+ /* maybe put a null at end of cur_line->buf */
+ return lines;
+ }
if(*ptr == '\n' || *ptr == '\0'){
/* printf("----\n"); */
/* printf("found a new line.\n"); */
/* printf("'%c' -> \\0\n", *ptr); */
/* printf("'next \%c': %c\n", *(ptr+1)); */
- if(*ptr == '\0'){
- /* end of buf */
- /* printf("found the end. (1)\n"); */
- return lines;
- }
+ /* if(*ptr == '\0'){ */
+ /* /\* end of buf *\/ */
+ /* /\* printf("found the end. (1)\n"); *\/ */
+ /* printf("(1) used [%d] of [%d]\n", c, size); */
+ /* return lines; */
+ /* } */
*ptr = '\0';
- ptr++;
-
- while(*ptr == '\n'){
+ ptr++; c++;
+
+ while(*ptr == '\n' || *ptr == '\0'){
+ if(c >= size){
+ /* printf("killing read at end of reported read.\n"); */
+ /* printf("(3) used [%d] of [%d]\n", c, size); */
+ return lines;
+ }
/* empty, discard */
/* printf("empty\n"); */
*ptr = '\0';
- ptr++;
+ ptr++; c++;
}
- if(*ptr == '\0'){
- /* end of buf */
- /* printf("found the end. (2)\n"); */
- return lines;
- }
+ /* if(*ptr == '\0'){ */
+ /* /\* end of buf *\/ */
+ /* /\* printf("found the end. (2)\n"); *\/ */
+ /* printf("(2) used [%d] of [%d]\n", c, size); */
+ /* return lines; */
+ /* } */
/* printf("hallo there.\n"); */
cur_line = malloc(sizeof(struct line));
old_line->next = cur_line;
old_line = cur_line;
cur_line->buf = ptr;
+ cur_line->next = NULL;
}
- printf("________|%c|\n", *ptr);
- ptr++;
- }
+ /* printf("________|%c|\n", *ptr); */
+ ptr++; c++;
+ }
}
void socket_read(void (*func)(char *line))
{
if(options.lines != NULL){
- printf("|||| already got some input. |||\n");
+ /* printf("|||| already got some input. |||\n"); */
goto call_func;
}
int size;
do_read:
- printf("||||| doing read. ||||\n");
+ /* printf("||||| doing read. ||||\n"); */
+ memset(options.buf, 0, MAX_LEN); \
size = read(options.sfd, options.buf, MAX_LEN);
- printf("||||| got read. ||||\n");
+ /* printf("read size: %d\n", size); */
+ /* printf("||||| got read. ||||\n"); */
if(size == 0){
- printf("||||| reading again. ||||\n");
+ /* printf("||||| reading again. ||||\n"); */
goto do_read;
}else{
- printf("||||| done read. ||||\n");
- options.lines = find_lines();
+ /* printf("||||| done read. ||||\n"); */
+ options.lines = find_lines(size);
+
+ /* struct line *current_line = options.lines; */
+
+ /* int count = 0; */
+ /* while(1){ */
+ /* printf("[%d] '%s'\n", count++, current_line->buf); */
+ /* if(current_line->next == NULL){ */
+ /* printf("found the end.\n"); */
+ /* break; */
+ /* } */
+ /* current_line = current_line->next; */
+ /* } */
+ }
+ call_func:{
struct line *cur_line = options.lines;
-
- int count = 0;
- while(1){
- printf("[%d] '%s'\n", count++, cur_line->buf);
- if(cur_line->next == NULL){
- printf("found the end.\n");
- break;
+ if(cur_line == NULL){
+ /* printf("bad options line.\n"); */
+ }else{
+ while(1){
+ /* printf("[%s]->", cur_line->buf); */
+ if(cur_line->next == NULL){
+ /* printf("(nil)\n"); */
+ break;
+ }else{
+ cur_line = cur_line->next;
+ }
}
- cur_line = cur_line->next;
}
}
- call_func:
- printf("calling function with %s\n", options.lines->buf);
+ /* printf("calling function with %s\n", options.lines->buf); */
func(options.lines->buf);
options.lines = options.lines->next;
return;
}
-
struct sockaddr_un addr;
addr.sun_family = AF_LOCAL;
sprintf(addr.sun_path, "/run/user/1000/tomd/socket");
-
+
if(connect(sfd, (struct sockaddr *) &addr, SUN_LEN(&addr)) != 0){
perror("connect");
exit(EXIT_FAILURE);
}
options.name = NULL;
-
+
for(int i = 1;
i < argc;
i++){
if(wrote != write_len){ \
perror("[tomc] write size mismatch"); \
exit(EXIT_FAILURE); \
- }}
-
+ }}
+
int last_read_size = 0;
static void do_read(char *line)
{
- printf("got %s as input.\n", line);
+ /* printf("got %s as input.\n", line); */
strcpy(socket_buf, line);
/* printf("socket_buf: %s\n", socket_buf); */
}
SOCK_WRITE(options.name); \
SOCK_ACK; \
}
-
+
static void write_client(void)
{
SOCK_WRITE("client:tomc");
}
while(1){
- printf("looking for that last ack.\n");
+ /* printf("looking for that last ack.\n"); */
SOCK_READ;
if(strcmp(socket_buf, "ACK") == 0){
- tomc_p("got the ending ack.");
+ /* tomc_p("got the ending ack."); */
+ write(sfd, "ACK", sizeof "ACK");
break;
}else{
- printf("in else.\n");
+ /* printf("in else.\n"); */
/* int len = strlen(socket_buf); */
/* int total = len; */
/* if(last_read_size == 0){ */
init();
write_requests();
- tomc_p("dying of our own accord.");
-
+ /* tomc_p("dying of our own accord."); */
+
return EXIT_SUCCESS;
}
static void socket_init(void)
{
gen_socket_filename();
-
+
/* listen on socket, respond to requests. */
/* perform all registered 'tick' operations */
sfd =
#define SEND_ACK { \
int size; \
- if(size = write(asfd, "ACK\n", sizeof "ACK\n") != sizeof "ACK\n") { \
- tomd_p("didn't send as much as we though (%d != %d)", size, sizeof "ACK\n"); \
+ char ACK[4];\
+ ACK[0] = 'A';\
+ ACK[1] = 'C';\
+ ACK[2] = 'K';\
+ ACK[3] = '\0';\
+if(size = write(asfd, ACK, sizeof(ACK)) != sizeof(ACK)) { \
+ tomd_p("didn't send as much as we though (%d != %d)",\
+ size, sizeof(ACK)); \
} \
}
}
#define READ_SOCKET {\
+ memset(buf, 0, sizeof(buf)); \
int size = read(asfd, buf, 100); \
buf[size] = '\0'; \
}
request = request_types[i].request;
break;
}
-
+
i++;
}
perror("[tomd] waitpid");
}else if(n_pid == 0){
DOUBLE_DUTY("waitpid == 0, check manual\n");
- }else{
+ }else{
DOUBLE_DUTY("running: %s\n",
!WIFEXITED(status) ? "yes"
: "no");
-
+
if(WIFEXITED(status)){
DOUBLE_DUTY(" status: %d\n", WEXITSTATUS(status));
jp->pid = -1;
DOUBLE_DUTY("running: no\n");
DOUBLE_DUTY(" status: %d\n", jp->last_status);
}
-
+
if(jp->pid != -1){
DOUBLE_DUTY(" pid: %d\n", jp->pid);
}
tomd_p("sending ack.");
SEND_ACK;
tomd_p("ack is sent.");
+ get_ack:
+ READ_SOCKET;
+ if(strcmp(buf, "ACK") != 0)
+ goto get_ack;
+ tomd_p("ack was ack'd.");
}
static void handle_connection(int asfd)
handle_request(asfd, buf);
shutdown(asfd, SHUT_WR);
+ close(asfd);
}
static void run(void)
perror("[tomd] accept socket");
exit(EXIT_FAILURE);
}
-
+
handle_connection(accept_sfd);
}
}
/* daemonize(); */
run();
cleanup();
-
+
return EXIT_SUCCESS;
}