comparison input/input.c @ 5571:124bfc43c044

Fix the bug pointed out by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz> wich affected command driven input (lirc and slave mode).
author albeu
date Fri, 12 Apr 2002 10:17:24 +0000
parents 39dae98304af
children 8346974080fe
comparison
equal deleted inserted replaced
5570:3a8d8c51355a 5571:124bfc43c044
507 static int 507 static int
508 mp_input_read_cmd(mp_input_fd_t* mp_fd, char** ret) { 508 mp_input_read_cmd(mp_input_fd_t* mp_fd, char** ret) {
509 char* end; 509 char* end;
510 (*ret) = NULL; 510 (*ret) = NULL;
511 511
512 // Allocate the buffer if it dont exist
512 if(!mp_fd->buffer) { 513 if(!mp_fd->buffer) {
513 mp_fd->buffer = (char*)malloc(MP_CMD_MAX_SIZE*sizeof(char)); 514 mp_fd->buffer = (char*)malloc(MP_CMD_MAX_SIZE*sizeof(char));
514 mp_fd->pos = 0; 515 mp_fd->pos = 0;
515 mp_fd->size = MP_CMD_MAX_SIZE; 516 mp_fd->size = MP_CMD_MAX_SIZE;
516 } 517 }
517 518
518 if(mp_fd->size - mp_fd->pos == 0) { 519 // Get some data if needed/possible
519 mp_msg(MSGT_INPUT,MSGL_ERR,"Cmd buffer of fd %d is full : dropping content\n",mp_fd->fd); 520 while( !(mp_fd->flags & MP_FD_GOT_CMD) && !(mp_fd->flags & MP_FD_EOF) && (mp_fd->size - mp_fd->pos > 1) ) {
520 mp_fd->pos = 0;
521 mp_fd->flags |= MP_FD_DROP;
522 }
523
524 while( !(mp_fd->flags & MP_FD_EOF) && (mp_fd->size - mp_fd->pos > 1) ) {
525 int r = ((mp_cmd_func_t)mp_fd->read_func)(mp_fd->fd,mp_fd->buffer+mp_fd->pos,mp_fd->size - 1 - mp_fd->pos); 521 int r = ((mp_cmd_func_t)mp_fd->read_func)(mp_fd->fd,mp_fd->buffer+mp_fd->pos,mp_fd->size - 1 - mp_fd->pos);
522 // Error ?
526 if(r < 0) { 523 if(r < 0) {
527 if(errno == EINTR) 524 if(errno == EINTR)
528 continue; 525 continue;
529 else if(errno == EAGAIN) 526 else if(errno == EAGAIN)
530 break; 527 break;
531 mp_msg(MSGT_INPUT,MSGL_ERR,"Error while reading cmd fd %d : %s\n",mp_fd->fd,strerror(errno)); 528 mp_msg(MSGT_INPUT,MSGL_ERR,"Error while reading cmd fd %d : %s\n",mp_fd->fd,strerror(errno));
532 return MP_INPUT_ERROR; 529 return MP_INPUT_ERROR;
530 // EOF ?
533 } else if(r == 0) { 531 } else if(r == 0) {
534 mp_fd->flags |= MP_FD_EOF; 532 mp_fd->flags |= MP_FD_EOF;
535 break; 533 break;
536 } 534 }
537 mp_fd->pos += r; 535 mp_fd->pos += r;
538 break; 536 break;
539 } 537 }
540 538
539 // Reset the got_cmd flag
540 mp_fd->flags &= ~MP_FD_GOT_CMD;
541 541
542 while(1) { 542 while(1) {
543 int l = 0; 543 int l = 0;
544 // Find the cmd end
544 mp_fd->buffer[mp_fd->pos] = '\0'; 545 mp_fd->buffer[mp_fd->pos] = '\0';
545 end = strchr(mp_fd->buffer,'\n'); 546 end = strchr(mp_fd->buffer,'\n');
546 if(!end) 547 // No cmd end ?
548 if(!end) {
549 // If buffer is full we must drop all until the next \n
550 if(mp_fd->size - mp_fd->pos <= 1) {
551 mp_msg(MSGT_INPUT,MSGL_ERR,"Cmd buffer of fd %d is full : dropping content\n",mp_fd->fd);
552 mp_fd->pos = 0;
553 mp_fd->flags |= MP_FD_DROP;
554 }
547 break; 555 break;
556 }
557 // We alredy have a cmd : set the got_cmd flag
548 else if((*ret)) { 558 else if((*ret)) {
549 mp_fd->flags |= MP_FD_GOT_CMD; 559 mp_fd->flags |= MP_FD_GOT_CMD;
550 break; 560 break;
551 } 561 }
552 562
553 l = end - mp_fd->buffer; 563 l = end - mp_fd->buffer;
554 564
565 // Not dropping : put the cmd in ret
555 if( ! (mp_fd->flags & MP_FD_DROP)) { 566 if( ! (mp_fd->flags & MP_FD_DROP)) {
556 (*ret) = (char*)malloc((l+1)*sizeof(char)); 567 (*ret) = (char*)malloc((l+1)*sizeof(char));
557 strncpy((*ret),mp_fd->buffer,l); 568 strncpy((*ret),mp_fd->buffer,l);
558 (*ret)[l] = '\0'; 569 (*ret)[l] = '\0';
559 } else { 570 } else { // Remove the dropping flag
560 mp_fd->flags &= ~MP_FD_DROP; 571 mp_fd->flags &= ~MP_FD_DROP;
561 } 572 }
562 if( mp_fd->pos - (l+1) > 0) 573 if( mp_fd->pos - (l+1) > 0)
563 memmove(mp_fd->buffer,end,mp_fd->pos-(l+1)); 574 memmove(mp_fd->buffer,end+1,mp_fd->pos-(l+1));
564 mp_fd->pos -= l+1; 575 mp_fd->pos -= l+1;
565 } 576 }
566 577
567 if(*ret) 578 if(*ret)
568 return 1; 579 return 1;
784 795
785 static mp_cmd_t* 796 static mp_cmd_t*
786 mp_input_read_cmds(int time) { 797 mp_input_read_cmds(int time) {
787 fd_set fds; 798 fd_set fds;
788 struct timeval tv,*time_val; 799 struct timeval tv,*time_val;
789 int i,n = 0,max_fd = 0; 800 int i,n = 0,max_fd = 0,got_cmd = 0;
790 mp_cmd_t* ret; 801 mp_cmd_t* ret;
791 static int last_loop = 0; 802 static int last_loop = 0;
792 803
793 if(num_cmd_fd == 0) 804 if(num_cmd_fd == 0)
794 return NULL; 805 return NULL;
799 mp_input_rm_cmd_fd(cmd_fds[i].fd); 810 mp_input_rm_cmd_fd(cmd_fds[i].fd);
800 i--; 811 i--;
801 continue; 812 continue;
802 } else if(cmd_fds[i].flags & MP_FD_NO_SELECT) 813 } else if(cmd_fds[i].flags & MP_FD_NO_SELECT)
803 continue; 814 continue;
815 if(cmd_fds[i].flags & MP_FD_GOT_CMD)
816 got_cmd = 1;
804 if(cmd_fds[i].fd > max_fd) 817 if(cmd_fds[i].fd > max_fd)
805 max_fd = cmd_fds[i].fd; 818 max_fd = cmd_fds[i].fd;
806 FD_SET(cmd_fds[i].fd,&fds); 819 FD_SET(cmd_fds[i].fd,&fds);
807 n++; 820 n++;
808 } 821 }
822 if(i < 0) { 835 if(i < 0) {
823 if(errno == EINTR) 836 if(errno == EINTR)
824 continue; 837 continue;
825 mp_msg(MSGT_INPUT,MSGL_ERR,"Select error : %s\n",strerror(errno)); 838 mp_msg(MSGT_INPUT,MSGL_ERR,"Select error : %s\n",strerror(errno));
826 } 839 }
827 return NULL; 840 if(!got_cmd)
841 return NULL;
828 } 842 }
829 break; 843 break;
830 } 844 }
831 845
832 for(i = last_loop + 1; i != last_loop ; i++) { 846 for(i = last_loop + 1; i != last_loop ; i++) {