void term_update_search() { int update_type = term.results.update_type; if (term.results.update_type == NO_UPDATE) return; term.results.update_type = NO_UPDATE; if (term.results.query_length == 0) return; circbuf cb; // Allocate room for the circular buffer of termlines. int lcurr = 0; if (update_type == PARTIAL_UPDATE) { // How much of the backscroll we need to update on a partial update? // Do a ceil: (x + y - 1) / y // On query_length - 1 int pstart = -((term.results.query_length + term.cols - 2) / term.cols) + term.sblines; lcurr = lcurr > pstart ? lcurr:pstart; results_partial_clear(lcurr); } else { term_clear_results(); } int llen = term.results.query_length / term.cols + 1; if (llen < 2) llen = 2; circbuf_init(&cb, llen); // Fill in our starting set of termlines. for (int i = lcurr; i < term.rows + term.sblines && cb.length < cb.capacity; ++i) { circbuf_push(&cb, fetch_line(i - term.sblines)); } int cpos = term.cols * lcurr; /* the number of matched chars in the current run */ int npos = 0; /* the number of matched cells in the current run (anpos >= npos) */ int anpos = 0; int end = term.cols * (term.rows + term.sblines); // Loop over every character and search for query. while (cpos < end) { // Determine the current position. int x = (cpos % term.cols); int y = (cpos / term.cols); // If our current position isn't in the buffer, add it in. if (y - lcurr >= llen) { circbuf_push(&cb, fetch_line(lcurr + llen - term.sblines)); ++lcurr; } termline * lll = circbuf_get(&cb, y - lcurr); termchar * chr = lll->chars + x; if (npos == 0 && cpos + term.results.query_length >= end) break; if (chr->chr != term.results.query[npos]) { // Skip the second cell of any wide characters if (chr->chr == UCSWIDE) { ++anpos; ++cpos; continue; } cpos -= npos - 1; npos = 0; anpos = 0; continue; } ++anpos; ++npos; if (term.results.query_length == npos) { int start = cpos - anpos + 1; result run = { .x = start % term.cols, .y = start / term.cols, .len = anpos }; #ifdef debug_search printf("%d, %d, %d\n", run.x, run.y, run.len); #endif results_add(run); npos = 0; anpos = 0; } ++cpos; } circbuf_destroy(&cb); }
int main(int argc, char *argv[]) { key_t cle; int idshm; char * memoire; circbuf bufferCirculaire; cle = ftok("/etc/passwd", 1); idshm = shmget(cle, 0, 0); memoire = shmat(idshm, NULL, SHM_RND); bufferCirculaire = circbuf_init(memoire, sizeof(char[8192])); if(argc > 1) { if((argv[1])[0] == '-') { if((argv[1])[1] == 'a') { for(int i = 0; i < circbuf_count(bufferCirculaire); i++) coller(circbuf_get(bufferCirculaire, i)); } else if((argv[1])[1] == 'l') { for(int i = 0; i < circbuf_count(bufferCirculaire); i++) printf("%d\t%s\n", i, strrchr(circbuf_get(bufferCirculaire, i), '/') + 1); } else if((argv[1])[1] == 'h') { afficher_aide(); } else { coller(circbuf_get(bufferCirculaire, atoi(&(argv[1])[1]))); } } else{ if(fopen(argv[1], "r")) { char c = (argv[1])[0]; if(c != '/') { char path[100]; getcwd(path, sizeof(path)); argv[1] = strcat(strcat(path, "/"), argv[1]); } printf("%s\n", argv[1]); circbuf_append(bufferCirculaire, argv[1]); } else { printf("Le fichier '%s' n'existe pas\n", argv[1]); } } } else { printf("Veuillez entrez un paramètre ou un nom de fichier!\n"); printf("Pour plus d'informations, utilisez l'aide en saisissant 'mycp -h'\n"); } circbuf_shut(bufferCirculaire); shmdt(memoire); return 0; }