static int past_eof;
/* Command line options */
-static unsigned long flags;
+static unsigned flags;
#define FLAG_E 1
#define FLAG_M (1<<1)
#define FLAG_m (1<<2)
/* This adds line numbers to every line, as the -N flag necessitates */
static void add_linenumbers(void)
{
- char current_line[256];
int i;
for (i = 0; i <= num_flines; i++) {
- safe_strncpy(current_line, flines[i], 256);
- flines[i] = xasprintf("%5d %s", i + 1, current_line);
+ char *new = xasprintf("%5d %s", i + 1, flines[i]);
+ free(flines[i]);
+ flines[i] = new;
}
}
strcpy(current_line, "");
fgets(current_line, 256, fp);
if (fp != stdin)
- xferror(fp, filename);
+ die_if_ferror(fp, filename);
flines = xrealloc(flines, (i+1) * sizeof(char *));
flines[i] = xstrdup(current_line);
}
if (!past_eof) {
if (!line_pos) {
if (num_files > 1)
- printf("%s%s %s%i%s%i%s%i-%i/%i ", HIGHLIGHT, filename, "(file ", current_file, " of ", num_files, ") lines ", line_pos + 1, line_pos + height - 1, num_flines + 1);
+ printf("%s%s %s%i%s%i%s%i-%i/%i ", HIGHLIGHT,
+ filename, "(file ", current_file, " of ", num_files, ") lines ",
+ line_pos + 1, line_pos + height - 1, num_flines + 1);
else {
- printf("%s%s lines %i-%i/%i ", HIGHLIGHT, filename, line_pos + 1, line_pos + height - 1, num_flines + 1);
+ printf("%s%s lines %i-%i/%i ", HIGHLIGHT,
+ filename, line_pos + 1, line_pos + height - 1,
+ num_flines + 1);
}
}
else {
- printf("%s %s lines %i-%i/%i ", HIGHLIGHT, filename, line_pos + 1, line_pos + height - 1, num_flines + 1);
+ printf("%s %s lines %i-%i/%i ", HIGHLIGHT, filename,
+ line_pos + 1, line_pos + height - 1, num_flines + 1);
}
if (line_pos == num_flines - height + 2) {
}
}
else {
- printf("%s%s lines %i-%i/%i (END) ", HIGHLIGHT, filename, line_pos + 1, num_flines + 1, num_flines + 1);
+ printf("%s%s lines %i-%i/%i (END) ", HIGHLIGHT, filename,
+ line_pos + 1, num_flines + 1, num_flines + 1);
if ((num_files > 1) && (current_file != num_files))
printf("- Next: %s", files[current_file]);
printf("%s", NORMAL);
if (!line_pos) {
printf("%s%s %s", HIGHLIGHT, filename, NORMAL);
if (num_files > 1)
- printf("%s%s%i%s%i%s%s", HIGHLIGHT, "(file ", current_file, " of ", num_files, ")", NORMAL);
+ printf("%s%s%i%s%i%s%s", HIGHLIGHT, "(file ",
+ current_file, " of ", num_files, ")", NORMAL);
}
else if (line_pos == num_flines - height + 2) {
printf("%s%s %s", HIGHLIGHT, "(END)", NORMAL);
insert_highlights if action = 1, or has the escape sequences
removed if action = 0, and then the line is returned. */
int match_status;
- char *line2 = (char *) xmalloc((sizeof(char) * (strlen(line) + 1)) + 64);
+ char *line2 = xmalloc((sizeof(char) * (strlen(line) + 1)) + 64);
char *growline = "";
regmatch_t match_structs;
match_found = 0;
match_status = regexec(pattern, line2, 1, &match_structs, 0);
-
+
while (match_status == 0) {
if (match_found == 0)
match_found = 1;
-
+
if (action) {
- growline = xasprintf("%s%.*s%s%.*s%s", growline, match_structs.rm_so, line2, HIGHLIGHT, match_structs.rm_eo - match_structs.rm_so, line2 + match_structs.rm_so, NORMAL);
+ growline = xasprintf("%s%.*s%s%.*s%s", growline,
+ match_structs.rm_so, line2, HIGHLIGHT,
+ match_structs.rm_eo - match_structs.rm_so,
+ line2 + match_structs.rm_so, NORMAL);
}
else {
- growline = xasprintf("%s%.*s%.*s", growline, match_structs.rm_so - 4, line2, match_structs.rm_eo - match_structs.rm_so, line2 + match_structs.rm_so);
+ growline = xasprintf("%s%.*s%.*s", growline,
+ match_structs.rm_so - 4, line2,
+ match_structs.rm_eo - match_structs.rm_so,
+ line2 + match_structs.rm_so);
}
-
+
line2 += match_structs.rm_eo;
match_status = regexec(pattern, line2, 1, &match_structs, REG_NOTBOL);
}
-
+
growline = xasprintf("%s%s", growline, line2);
-
+
return (match_found ? growline : line);
-
+
free(growline);
free(line2);
}
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);
return;
}
uncomp_regex[strlen(uncomp_regex) - 1] = '\0';
-
+
/* Compile the regex and check for errors */
xregcomp(&pattern, uncomp_regex, 0);
}
}
old_pattern = pattern;
-
+
/* Reset variables */
match_lines = xrealloc(match_lines, sizeof(int));
match_lines[0] = -1;
j++;
}
}
-
+
num_matches = j;
if ((match_lines[0] != -1) && (num_flines > height - 2)) {
if (match_backwards) {
int keypress;
- flags = bb_getopt_ulflags(argc, argv, "EMmN~");
+ flags = getopt32(argc, argv, "EMmN~");
argc -= optind;
argv += optind;
if (ttyname(STDIN_FILENO) == NULL)
inp_stdin = 1;
else {
- bb_error_msg("Missing filename");
+ bb_error_msg("missing filename");
bb_show_usage();
}
}