int main(int argc, char *argv[]){ if (argc < 2){ printf("No pattern\n"); return -1; } struct RL* rl = rl_open(0,MAX_LEN); int i; char** patterns = malloc((argc-1)*sizeof(char*)); char** repls = malloc((argc-1)*sizeof(char*)); int* greeds = malloc((argc-1)*sizeof(int)); char* str; for (i = 1; i < argc; i++){ //get pattern strtok(argv[i], "/"); char* pattern_tmp = strtok(NULL, "/");//skip s/ patterns[i-1] = malloc(strlen(pattern_tmp)+1); strcpy(patterns[i-1], pattern_tmp); pattern_tmp = strtok(NULL, "/"); repls[i-1] = malloc(strlen(pattern_tmp)+1); strcpy(repls[i-1], pattern_tmp); greeds[i-1] = (strtok(NULL, "/") != NULL); //printf("%s\n%s\n%d\n", patterns[i-1], repls[i-1], greeds[i-1]); } //test int res = rl_readline(rl, rl->buf, rl->buf_size); while (res!=0){ if (res > 0){ //do shit and write //printf("%s",rl->buf); str = malloc((res+1)*sizeof(char)); strcpy(str,rl->buf); //printf("%s+%s\n", str,repls[i]); for (i = 0; i < argc-1; i++){ //if (parse_pattern(str,patterns[i],repls[i],greeds[i]) == NULL){ // printf("NO F**K YOU"); //} str = parse_pattern(str,patterns[i],repls[i],greeds[i]); } write(1,str,strlen(str)); write(1,"\n",1); free(str); } else { break; } res = rl_readline(rl, rl->buf,rl->buf_size); } for (i = 0; i < argc -1; i++){ free(patterns[i]); free(repls[i]); } free(patterns); free(repls); free(greeds); return 0; }
int main(int argc, char *argv[]) { int len = atoi(argv[1]); RL *rl = rl_open(0, len); char *buf = (char *) malloc(len + 1); while (1) { int rc = rl_readline(rl, buf, len); if (rc == 0) break; else if (rc > 0) { write_line(buf, rc); } } free(buf); rl_close(rl); return 0; }
int main(int argc, char** argv) { if (argc != 2) { printf("Usage: readlines-main <max_size>\n"); return 0; } size_t max_size = atoi(argv[1]); RL *rl = rl_open(0, max_size); char *buf = malloc(max_size + 1); ssize_t read = 0; while ((read = rl_readline(rl, buf, max_size + 1)) != 0) { if (read == -1) { fprintf(stderr, "Error!\n"); break; } if (read == -2) { fprintf(stderr, "The line does not fit in the buffer\n"); continue; } if (read == -3) { fprintf(stderr, "Line is too long\n"); continue; } size_t written = 0; while (written < read) written += write(1, &buf[written], read - written); } rl_close(rl); return 0; }
void protocol_run(KFile *fd) { /** * \todo to be removed, we could probably access the serial FIFO * directly */ static char linebuf[80]; if (!interactive) { kfile_gets(fd, linebuf, sizeof(linebuf)); // reset serial port error anyway kfile_clearerr(fd); // check message minimum length if (linebuf[0]) { /* If we enter lines beginning with sharp(#) they are stripped out from commands */ if(linebuf[0] != '#') { if (linebuf[0] == 0x1B && linebuf[1] == 0x1B) // ESC { interactive = true; kfile_printf(fd, "Entering interactive mode\r\n"); } else { protocol_parse(fd, linebuf); } } } } else { const char *buf; /* * Read a line from serial. We use a temporary buffer * because otherwise we would have to extract a message * from the port immediately: there might not be any * available, and one might get free while we read * the line. We also add a fake ID at the start to * fool the parser. */ buf = rl_readline(&rl_ctx); /* If we enter lines beginning with sharp(#) they are stripped out from commands */ if(buf && buf[0] != '#') { if (buf[0] != '\0') { // exit special case to immediately change serial input if (!strcmp(buf, "exit") || !strcmp(buf, "quit")) { rl_clear_history(&rl_ctx); kfile_printf(fd, "Leaving interactive mode...\r\n"); interactive = FORCE_INTERACTIVE; } else { //TODO: remove sequence numbers linebuf[0] = '0'; linebuf[1] = ' '; strncpy(linebuf + 2, buf, sizeof(linebuf) - 3); linebuf[sizeof(linebuf) - 1] = '\0'; protocol_parse(fd, linebuf); } } } } }