main (int argc, char **argv) { char *s = r_file_slurp (argv[1], NULL); r_cons_new (); r_cons_less (s); }
R_API int r_cons_grepbuf(char *buf, int len) { RCons *cons = r_cons_singleton (); char *tline, *tbuf, *p, *out, *in = buf; int ret, buffer_len = 0, l = 0, tl = 0; if (cons->grep.json) { char *out = sdb_json_indent (buf); free (cons->buffer); cons->buffer = out; cons->buffer_len = strlen (out); cons->buffer_sz = cons->buffer_len +1; cons->grep.json = 0; if (cons->grep.less) { cons->grep.less = 0; r_cons_less (cons->buffer); } return 3; } if (cons->grep.less) { cons->grep.less = 0; r_cons_less (buf); buf[0] = 0; cons->buffer_len = 0; if (cons->buffer) cons->buffer[0] = 0; free (cons->buffer); cons->buffer = NULL; return 0; } if (!cons->buffer) { cons->buffer_len = len+20; cons->buffer = malloc (cons->buffer_len); cons->buffer[0] = 0; } out = tbuf = calloc (1, len); tline = malloc (len); cons->lines = 0; while ((int)(size_t)(in-buf)<len) { p = strchr (in, '\n'); if (!p) { free (tbuf); free (tline); return 0; } l = p-in; if (l > 0) { memcpy (tline, in, l); tl = r_str_ansi_filter (tline, l); if (tl < 0) ret = -1; else ret = r_cons_grep_line (tline, tl); if (ret > 0) { if (cons->grep.line == -1 || (cons->grep.line != -1 && cons->grep.line == cons->lines)) { memcpy (out, tline, ret); memcpy (out+ret, "\n", 1); out += ret+1; buffer_len += ret+1; } cons->lines++; } else if (ret < 0) { free (tbuf); free (tline); return 0; } in += l+1; } else in++; } memcpy (buf, tbuf, len); cons->buffer_len = buffer_len; free (tbuf); free (tline); if (cons->grep.counter) { if (cons->buffer_len<10) cons->buffer_len = 10; // HACK snprintf (cons->buffer, cons->buffer_len, "%d\n", cons->lines); cons->buffer_len = strlen (cons->buffer); } return cons->lines; }