static void buffer_line(int linenum) { int i; past_eof = 0; if (linenum < 0 || linenum > num_flines) { clear_line(); printf("%s%s%i%s", HIGHLIGHT, "Cannot seek to line number ", linenum + 1, NORMAL); } else if (linenum < (num_flines - height - 2)) { for (i = 0; i < (height - 1); i++) { free(buffer[i]); buffer[i] = bb_xstrdup(flines[linenum + i]); } line_pos = linenum; buffer_print(); } else { for (i = 0; i < (height - 1); i++) { free(buffer[i]); if (linenum + i < num_flines + 2) buffer[i] = bb_xstrdup(flines[linenum + i]); else buffer[i] = bb_xstrdup((flags & FLAG_TILDE) ? "\n" : "~\n"); } line_pos = linenum; /* Set past_eof so buffer_down and buffer_up act differently */ past_eof = 1; buffer_print(); } }
int main(int argc, char *argv[]) { int thr_id1, thr_id2; atomic_set(0,&flag); setup(); pass_criteria = THRESHOLD; rt_init("l:h", parse_args, argc, argv); /* we need the buffered print system */ printf("-------------------------------\n"); printf("pthread_kill Latency\n"); printf("-------------------------------\n\n"); printf("Iterations: %d\n", ITERATIONS); debug(DBG_DEBUG, "Main creating threads\n"); fflush(stdout); thr_id1 = create_fifo_thread(signal_receiving_thread, (void*)0, PRIO); thr_id2 = create_fifo_thread(signal_sending_thread, (void*)(intptr_t)thr_id1, PRIO-1); // thr_id2 = create_other_thread(signal_sending_thread, (void*)(intptr_t)thr_id1); debug(DBG_DEBUG, "Main joining threads debug\n"); join_thread(thr_id1); join_thread(thr_id2); buffer_print(); return fail; }
static void buffer_fill_and_print(void) { unsigned i; #if ENABLE_FEATURE_LESS_DASHCMD int fpos = cur_fline; if (option_mask32 & FLAG_S) { /* Go back to the beginning of this line */ while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1])) fpos--; } i = 0; while (i <= max_displayed_line && fpos <= max_fline) { int lineno = LINENO(flines[fpos]); buffer[i] = flines[fpos]; i++; do { fpos++; } while ((fpos <= max_fline) && (option_mask32 & FLAG_S) && lineno == LINENO(flines[fpos]) ); } #else for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) { buffer[i] = flines[cur_fline + i]; } #endif for (; i <= max_displayed_line; i++) { buffer[i] = empty_line_marker; } buffer_print(); }
static void remove_current_file(void) { int i; if (current_file != 1) { change_file(-1); for (i = 3; i <= num_files; i++) files[i - 2] = files[i - 1]; num_files--; buffer_print(); } else { change_file(1); for (i = 2; i <= num_files; i++) files[i - 2] = files[i - 1]; num_files--; current_file--; buffer_print(); } }
void* consumer(void* ptr) { Buffer* buf = (Buffer*)ptr; while (1) { int slot = read_start(buf); if (slot == -1) { printf(ANSI_RED"consumer: no data in queue...waiting"ANSI_NORMAL"\n"); usleep(consumer_delay); continue; } //printf(ANSI_RED"consumer: slot %d"ANSI_NORMAL"\n", slot); buffer_print(buf); usleep(consume_duration); read_done(buf); buffer_print(buf); } return 0; }
static void buffer_fill_and_print(void) { int i; for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) { buffer[i] = flines[cur_fline + i]; } for (; i <= max_displayed_line; i++) { buffer[i] = empty_line_marker; } buffer_print(); }
static void trie_node_format(struct f_trie_node *t, buffer *buf) { if (t == NULL) return; if (ipa_nonzero(t->accept)) buffer_print(buf, "%I/%d{%I}, ", t->addr, t->plen, t->accept); trie_node_format(t->c[0], buf); trie_node_format(t->c[1], buf); }
/* Reinitialise everything for a new file - free the memory and start over */ static void reinitialise(void) { int i; for (i = 0; i <= num_flines; i++) free(flines[i]); free(flines); data_readlines(); buffer_init(); buffer_print(); }
bool Zlib_decompress ( Zlib *self, void *data, size_t dataSize ) { z_stream stream; stream.next_in = data; stream.avail_in = dataSize; stream.avail_out = sizeof (self->buffer); stream.next_out = self->buffer; stream.total_in = 0; stream.total_out = 0; stream.zalloc = 0; stream.zfree = 0; if (inflateInit2_(&stream, -15, "1.2.8", 88)) { buffer_print (data, dataSize, "Buffer : "); error ("Can't init decompression."); return false; } if (inflate(&stream, Z_FINISH) != 1) { buffer_print (data, dataSize, "Buffer : "); error ("Can't decompress."); return false; } if (inflateEnd(&stream)) { buffer_print (data, dataSize, "Buffer : "); error ("Can't end decompression."); return false; } self->header.magic = ZLIB_MAGIC_HEADER; self->header.size = stream.total_out; return true; }
void* producer(void* ptr) { Buffer* buf = (Buffer*)ptr; while (1) { int slot = write_start(buf); if (slot == -1) { printf(ANSI_GREEN"producer: no free space left in queue...waiting"ANSI_NORMAL"\n"); buffer_print(buf); exit(-1); usleep(producer_delay); continue; } //printf(ANSI_GREEN"producer: slot %d"ANSI_NORMAL"\n", slot); buffer_print(buf); usleep(produce_duration); write_done(buf); buffer_print(buf); } return 0; }
static void regex_process(void) { char *uncomp_regex, *err; /* Reset variables */ free(match_lines); match_lines = NULL; match_pos = 0; num_matches = 0; if (pattern_valid) { regfree(&pattern); pattern_valid = 0; } /* Get the uncompiled regular expression from the user */ clear_line(); bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/'); uncomp_regex = less_gets(1); if (!uncomp_regex[0]) { free(uncomp_regex); buffer_print(); return; } /* Compile the regex and check for errors */ err = regcomp_or_errmsg(&pattern, uncomp_regex, (option_mask32 & FLAG_I) ? REG_ICASE : 0); free(uncomp_regex); if (err) { print_statusline(err); free(err); return; } pattern_valid = 1; match_pos = 0; fill_match_lines(0); while (match_pos < num_matches) { if ((int)match_lines[match_pos] > cur_fline) break; match_pos++; } if (option_mask32 & LESS_STATE_MATCH_BACKWARDS) match_pos--; /* It's possible that no matches are found yet. * goto_match() will read input looking for match, * if needed */ goto_match(match_pos); }
/** * trie_format * @t: trie to be formatted * @buf: destination buffer * * Prints the trie to the supplied buffer. */ void trie_format(struct f_trie *t, buffer *buf) { buffer_puts(buf, "["); if (t->zero) buffer_print(buf, "%I/%d", IPA_NONE, 0); trie_node_format(&t->root, buf); /* Undo last separator */ if (buf->pos[-1] != '[') buf->pos -= 2; buffer_puts(buf, "]"); }
static void save_input_to_file(void) { char current_line[256]; int i; FILE *fp; clear_line(); printf("Log file: "); fgets(current_line, 256, inp); current_line[strlen(current_line) - 1] = '\0'; if (strlen(current_line) > 1) { fp = bb_xfopen(current_line, "w"); for (i = 0; i < num_flines; i++) fprintf(fp, "%s", flines[i]); fclose(fp); buffer_print(); } else printf("%sNo log file%s", HIGHLIGHT, NORMAL); }
int less_main(int argc, char **argv) { int keypress; flags = bb_getopt_ulflags(argc, argv, "EMmN~"); argc -= optind; argv += optind; files = argv; num_files = argc; if (!num_files) { if (ttyname(STDIN_FILENO) == NULL) inp_stdin = 1; else { bb_error_msg("Missing filename"); bb_show_usage(); } } strcpy(filename, (inp_stdin) ? bb_msg_standard_input : files[0]); get_terminal_width_height(0, &width, &height); data_readlines(); tcgetattr(fileno(inp), &term_orig); term_vi = term_orig; term_vi.c_lflag &= (~ICANON & ~ECHO); term_vi.c_iflag &= (~IXON & ~ICRNL); term_vi.c_oflag &= (~ONLCR); term_vi.c_cc[VMIN] = 1; term_vi.c_cc[VTIME] = 0; buffer_init(); buffer_print(); while (1) { keypress = tless_getch(); keypress_process(keypress); } }
static void regex_process(void) { char uncomp_regex[100]; char *current_line; int i; int j = 0; regex_t pattern; /* Get the uncompiled regular expression from the user */ clear_line(); putchar((match_backwards) ? '?' : '/'); uncomp_regex[0] = 0; fgets(uncomp_regex, sizeof(uncomp_regex), inp); if (strlen(uncomp_regex) == 1) { if (num_matches) goto_match(match_backwards ? match_pos - 1 : match_pos + 1); else buffer_print(); return; } uncomp_regex[strlen(uncomp_regex) - 1] = '\0'; /* Compile the regex and check for errors */ xregcomp(&pattern, uncomp_regex, 0); if (num_matches) { /* Get rid of all the highlights we added previously */ for (i = 0; i <= num_flines; i++) { current_line = process_regex_on_line(flines[i], &old_pattern, 0); flines[i] = bb_xstrdup(current_line); } } old_pattern = pattern; /* Reset variables */ match_lines = xrealloc(match_lines, sizeof(int)); match_lines[0] = -1; match_pos = 0; num_matches = 0; match_found = 0; /* Run the regex on each line of the current file here */ for (i = 0; i <= num_flines; i++) { current_line = process_regex_on_line(flines[i], &pattern, 1); flines[i] = bb_xstrdup(current_line); if (match_found) { match_lines = xrealloc(match_lines, (j + 1) * sizeof(int)); match_lines[j] = i; j++; } } num_matches = j; if ((match_lines[0] != -1) && (num_flines > height - 2)) { if (match_backwards) { for (i = 0; i < num_matches; i++) { if (match_lines[i] > line_pos) { match_pos = i - 1; buffer_line(match_lines[match_pos]); break; } } } else buffer_line(match_lines[0]); } else buffer_init(); }
static void keypress_process(int keypress) { switch (keypress) { case KEYCODE_DOWN: case 'e': case 'j': case 0x0d: buffer_down(1); break; case KEYCODE_UP: case 'y': case 'k': buffer_up(1); break; case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f': buffer_down(max_displayed_line + 1); break; case KEYCODE_PAGEUP: case 'w': case 'b': buffer_up(max_displayed_line + 1); break; case 'd': buffer_down((max_displayed_line + 1) / 2); break; case 'u': buffer_up((max_displayed_line + 1) / 2); break; case KEYCODE_HOME: case 'g': case 'p': case '<': case '%': buffer_line(0); break; case KEYCODE_END: case 'G': case '>': cur_fline = MAXLINES; read_lines(); buffer_line(cur_fline); break; case 'q': case 'Q': less_exit(EXIT_SUCCESS); break; #if ENABLE_FEATURE_LESS_MARKS case 'm': add_mark(); buffer_print(); break; case '\'': goto_mark(); buffer_print(); break; #endif case 'r': case 'R': buffer_print(); break; /*case 'R': full_repaint(); break;*/ case 's': save_input_to_file(); break; case 'E': examine_file(); break; #if ENABLE_FEATURE_LESS_FLAGS case '=': m_status_print(); break; #endif #if ENABLE_FEATURE_LESS_REGEXP case '/': option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS; regex_process(); break; case 'n': goto_match(match_pos + 1); break; case 'N': goto_match(match_pos - 1); break; case '?': option_mask32 |= LESS_STATE_MATCH_BACKWARDS; regex_process(); break; #endif #if ENABLE_FEATURE_LESS_DASHCMD case '-': flag_change(); buffer_print(); break; #ifdef BLOAT case '_': show_flag_status(); break; #endif #endif #if ENABLE_FEATURE_LESS_BRACKETS case '{': case '(': case '[': match_right_bracket(keypress); break; case '}': case ')': case ']': match_left_bracket(keypress); break; #endif case ':': colon_process(); break; } if (isdigit(keypress)) number_process(keypress); }
static void number_process(int first_digit) { unsigned i; int num; int keypress; char num_input[sizeof(int)*4]; /* more than enough */ num_input[0] = first_digit; /* Clear the current line, print a prompt, and then print the digit */ clear_line(); printf(":%c", first_digit); /* Receive input until a letter is given */ i = 1; while (i < sizeof(num_input)-1) { keypress = less_getch(i + 1); if ((unsigned)keypress > 255 || !isdigit(num_input[i])) break; num_input[i] = keypress; bb_putchar(keypress); i++; } num_input[i] = '\0'; num = bb_strtou(num_input, NULL, 10); /* on format error, num == -1 */ if (num < 1 || num > MAXLINES) { buffer_print(); return; } /* We now know the number and the letter entered, so we process them */ switch (keypress) { case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015': buffer_down(num); break; case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u': buffer_up(num); break; case 'g': case '<': case 'G': case '>': cur_fline = num + max_displayed_line; read_lines(); buffer_line(num - 1); break; case 'p': case '%': num = num * (max_fline / 100); /* + max_fline / 2; */ cur_fline = num + max_displayed_line; read_lines(); buffer_line(num); break; #if ENABLE_FEATURE_LESS_REGEXP case 'n': goto_match(match_pos + num); break; case '/': option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS; regex_process(); break; case '?': option_mask32 |= LESS_STATE_MATCH_BACKWARDS; regex_process(); break; #endif } }
int main(int argc, char** argv) { // read all used flags while (1) { static struct option long_options[] = { {"invert-match", no_argument, 0, 'v'}, {"line-regexp", no_argument, 0, 'x'}, {"count", no_argument, 0, 'c'}, {"help", no_argument, &display_help, 1}, {"version", no_argument, 0, 'V'}, {"cache-limit", required_argument, 0, OPTION_CACHE_LIMIT}, {0, 0, 0, 0} }; int option_index = 0; int c = getopt_long(argc, argv, "vxcV", long_options, &option_index); if (c == -1) break; switch (c) { case 'v': invert_match = true; break; case 'x': whole_lines = true; break; case 'c': count_matches = true; break; case OPTION_CACHE_LIMIT: cache_mem_limit = parse_size_in_kb(optarg); if (cache_mem_limit == -1U) { print_usage(argv[0]); return 2; } break; case 'V': display_version = true; break; case 0: break; default: print_usage(argv[0]); return 2; } } if (display_help) { printf(help_message, argv[0]); return 0; } if (display_version) { printf(version_message); return 0; } // check if there is exactly one regular expression if (optind != argc-1) { print_usage(argv[0]); return 2; } char* regex = argv[optind]; // parse the regular expression to abstract syntax tree struct syntree* tree; tree = parse(regex, strlen(regex)); // build nfa for the regular expression struct nfa* nfa; nfa = build_nfa(tree, whole_lines); free_tree(tree); // initialize simulation state sim_init(&state, nfa, invert_match, cache_mem_limit); // initialize buffer uintptr_t buffer_size = 65536; buffer_init(&buffer, STDIN_FILENO, STDOUT_FILENO, buffer_size); // main matching loop uintmax_t match_count = 0; bool new_line = true; bool some_match = false; intptr_t res = buffer_next(&buffer); if (!count_matches) buffer_mark(&buffer); while (res == 1) { // save next input byte to ch uint_fast8_t ch = buffer_get(&buffer); if (ch == '\n') { // handle the end of line here new_line = true; // simulate the special line end character if (!state.dfa_state->accept) sim_step(&state, CHAR_INPUT_END); if (sim_is_match(&state)) { // the last line matched, either print it or count it some_match = true; if (count_matches) { ++match_count; } else { int_fast8_t res = buffer_print(&buffer, true); if (res != 1) die(2, (res == -1) ? errno : 0, "Error writing to stdout"); } } // reset simulation state state.dfa_state = state.after_begin; // read next character from input res = buffer_next(&buffer); if (!count_matches) buffer_mark(&buffer); } else { new_line = false; // simulate only if there was no match on the current line if (!state.dfa_state->accept) { // if the character is not valid ASCII, no transitions will take place if (ch > MAX_CHAR) state.dfa_state = state.before_begin; // otherwise do the simulation else sim_step(&state, ch); } // read next character from input res = buffer_next(&buffer); } } if (res == -1) die(2, errno, "Error reading from stdin"); // check if there is no \n at the end of the input // if it is the case, behave as if it was there if (!new_line) { if (!state.dfa_state->accept) sim_step(&state, CHAR_INPUT_END); if (sim_is_match(&state)) { some_match = true; if (count_matches) { ++match_count; } else { int_fast8_t res = buffer_print(&buffer, false); puts(""); if (res != 1) die(2, (res == -1) ? errno : 0, "Error writing to stdout"); } } } // if -c is used, print the count of matches if (count_matches) { printf("%" PRIuMAX "\n", match_count); } // clean up buffer_cleanup(&buffer); sim_cleanup(&state); free_nfa(nfa); // return status is based on whether there was a match or not return some_match ? 0 : 1; }
void *signal_receiving_thread(void *arg) { int i, ret, sig; long delta; long max, min; sigset_t set, oset; stats_container_t dat; stats_container_t hist; stats_quantiles_t quantiles; stats_record_t rec; stats_container_init(&dat, ITERATIONS); stats_container_init(&hist, HIST_BUCKETS); stats_quantiles_init(&quantiles, (int)log10(ITERATIONS)); debug(DBG_DEBUG, "Signal receiving thread running\n"); if ((sigaddset(&set, SIGNALNUMBER))) { perror("sigaddset:"); exit(1); } if ((ret = pthread_sigmask(SIG_BLOCK, &set, &oset))) { printf("pthread_sigmask returned %d\n", ret); exit(1); } /* Let the sending thread know that receiver is ready */ atomic_set(1, &flag); debug(DBG_DEBUG, "Signal receiving thread ready to receive\n"); if (latency_threshold) { latency_trace_enable(); latency_trace_start(); } /* Warm up */ for (i = 0; i < 5; i++) { sigwait(&set, &sig); atomic_set(1, &flag); } max = min = 0; fail = 0; debug(DBG_INFO, "\n\n"); for (i = 0; i < ITERATIONS; i++) { sigwait(&set, &sig); end = rt_gettime(); delta = (end - begin)/NS_PER_US; rec.x = i; rec.y = delta; stats_container_append(&dat, rec); if (i == 0 || delta < min) min = delta; if (delta > max) max = delta; if (delta > pass_criteria) fail++; debug(DBG_INFO, "Iteration %d: Took %ld us. Max = %ld us, " "Min = %ld us\n", i, delta, max, min); fflush(stdout); buffer_print(); if (latency_threshold && (delta > latency_threshold)) { atomic_set(2, &flag); break; } atomic_set(1, &flag); } if (latency_threshold) { latency_trace_stop(); if (i != ITERATIONS) { printf("Latency threshold (%luus) exceeded at iteration %d\n", latency_threshold, i); fflush(stdout); buffer_print(); latency_trace_print(); stats_container_resize(&dat, i + 1); } } stats_hist(&hist, &dat); stats_container_save("samples", "pthread_kill Latency Scatter Plot", "Iteration", "Latency (us)", &dat, "points"); stats_container_save("hist", "pthread_kill Latency Histogram", "Latency (us)", "Samples", &hist, "steps"); printf("\n"); printf("Min: %lu us\n", stats_min(&dat)); printf("Max: %lu us\n", stats_max(&dat)); printf("Avg: %.4f us\n", stats_avg(&dat)); printf("StdDev: %.4f us\n", stats_stddev(&dat)); printf("Quantiles:\n"); stats_quantiles_calc(&dat, &quantiles); stats_quantiles_print(&quantiles); printf("Failures: %d\n", fail); printf("Criteria: Time < %d us\n", (int)pass_criteria); printf("Result: %s", fail ? "FAIL" : "PASS"); printf("\n\n"); return NULL; }
static void number_process(int first_digit) { int i = 1; int num; char num_input[80]; char keypress; char *endptr; num_input[0] = first_digit; /* Clear the current line, print a prompt, and then print the digit */ clear_line(); printf(":%c", first_digit); /* Receive input until a letter is given (max 80 chars)*/ while((i < 80) && (num_input[i] = tless_getch()) && isdigit(num_input[i])) { putchar(num_input[i]); i++; } /* Take the final letter out of the digits string */ keypress = num_input[i]; num_input[i] = '\0'; num = strtol(num_input, &endptr, 10); if (endptr==num_input || *endptr!='\0' || num < 1 || num > MAXLINES) { buffer_print(); return; } /* We now know the number and the letter entered, so we process them */ switch (keypress) { case KEY_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015': buffer_down(num); break; case KEY_UP: case 'b': case 'w': case 'y': case 'u': buffer_up(num); break; case 'g': case '<': case 'G': case '>': if (num_flines >= height - 2) buffer_line(num - 1); break; case 'p': case '%': buffer_line(((num / 100) * num_flines) - 1); break; #ifdef CONFIG_FEATURE_LESS_REGEXP case 'n': goto_match(match_pos + num); break; case '/': match_backwards = 0; regex_process(); break; case '?': match_backwards = 1; regex_process(); break; #endif default: break; } }
static void keypress_process(int keypress) { switch (keypress) { case KEY_DOWN: case 'e': case 'j': case '\015': buffer_down(1); buffer_print(); break; case KEY_UP: case 'y': case 'k': buffer_up(1); buffer_print(); break; case PAGE_DOWN: case ' ': case 'z': buffer_down(height - 1); buffer_print(); break; case PAGE_UP: case 'w': case 'b': buffer_up(height - 1); buffer_print(); break; case 'd': buffer_down((height - 1) / 2); buffer_print(); break; case 'u': buffer_up((height - 1) / 2); buffer_print(); break; case 'g': case 'p': case '<': case '%': buffer_line(0); break; case 'G': case '>': buffer_line(num_flines - height + 2); break; case 'q': case 'Q': tless_exit(0); break; #ifdef CONFIG_FEATURE_LESS_MARKS case 'm': add_mark(); buffer_print(); break; case '\'': goto_mark(); buffer_print(); break; #endif case 'r': buffer_print(); break; case 'R': full_repaint(); break; case 's': if (inp_stdin) save_input_to_file(); break; case 'E': examine_file(); break; #ifdef CONFIG_FEATURE_LESS_FLAGS case '=': clear_line(); m_status_print(); break; #endif #ifdef CONFIG_FEATURE_LESS_REGEXP case '/': match_backwards = 0; regex_process(); break; case 'n': goto_match(match_pos + 1); break; case 'N': goto_match(match_pos - 1); break; case '?': match_backwards = 1; regex_process(); break; #endif #ifdef CONFIG_FEATURE_LESS_FLAGCS case '-': flag_change(); buffer_print(); break; case '_': show_flag_status(); break; #endif #ifdef CONFIG_FEATURE_LESS_BRACKETS case '{': case '(': case '[': match_right_bracket(keypress); break; case '}': case ')': case ']': match_left_bracket(keypress); break; #endif case ':': colon_process(); break; default: break; } if (isdigit(keypress)) number_process(keypress); }