forgot about avn add... :(
[oweals/busybox.git] / miscutils / less.c
index 7de1836568798f19a4df8e179d86e259c9a088da..de97ba6b93fc3bf894db7701e18dd8efe629154e 100644 (file)
  *      redirected input has been read from stdin
 */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <termios.h>
-#include <unistd.h>
-#include <ctype.h>
-
 #include "busybox.h"
 
 #ifdef CONFIG_FEATURE_LESS_REGEXP
@@ -83,7 +76,7 @@ static int num_files = 1;
 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)
@@ -192,12 +185,12 @@ static void clear_line(void)
 /* 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] = bb_xasprintf("%5d %s", i + 1, current_line);
+               char *new = xasprintf("%5d %s", i + 1, flines[i]);
+               free(flines[i]);
+               flines[i] = new;
        }
 }
 
@@ -207,15 +200,15 @@ static void data_readlines(void)
        char current_line[256];
        FILE *fp;
 
-       fp = (inp_stdin) ? stdin : bb_xfopen(filename, "r");
+       fp = (inp_stdin) ? stdin : xfopen(filename, "r");
        flines = NULL;
        for (i = 0; (feof(fp)==0) && (i <= MAXLINES); i++) {
                strcpy(current_line, "");
                fgets(current_line, 256, fp);
                if (fp != stdin)
-                       bb_xferror(fp, filename);
+                       die_if_ferror(fp, filename);
                flines = xrealloc(flines, (i+1) * sizeof(char *));
-               flines[i] = bb_xstrdup(current_line);
+               flines[i] = xstrdup(current_line);
        }
        num_flines = i - 2;
 
@@ -227,7 +220,7 @@ static void data_readlines(void)
        fclose(fp);
 
        if (inp == NULL)
-               inp = (inp_stdin) ? bb_xfopen(CURRENT_TTY, "r") : stdin;
+               inp = (inp_stdin) ? xfopen(CURRENT_TTY, "r") : stdin;
 
        if (flags & FLAG_N)
                add_linenumbers();
@@ -250,13 +243,18 @@ static void m_status_print(void)
        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) {
@@ -270,7 +268,8 @@ static void m_status_print(void)
                }
        }
        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);
@@ -307,7 +306,8 @@ static void status_print(void)
                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);
@@ -358,12 +358,12 @@ static void buffer_init(void)
        /* Fill the buffer until the end of the file or the
           end of the buffer is reached */
        for (i = 0; (i < (height - 1)) && (i <= num_flines); i++) {
-               buffer[i] = bb_xstrdup(flines[i]);
+               buffer[i] = xstrdup(flines[i]);
        }
 
        /* If the buffer still isn't full, fill it with blank lines */
        for (; i < (height - 1); i++) {
-               buffer[i] = bb_xstrdup("");
+               buffer[i] = xstrdup("");
        }
 }
 
@@ -377,7 +377,7 @@ static void buffer_down(int nlines)
                        line_pos += nlines;
                        for (i = 0; i < (height - 1); i++) {
                                free(buffer[i]);
-                               buffer[i] = bb_xstrdup(flines[line_pos + i]);
+                               buffer[i] = xstrdup(flines[line_pos + i]);
                        }
                }
                else {
@@ -387,7 +387,7 @@ static void buffer_down(int nlines)
                                line_pos += 1;
                                for (i = 0; i < (height - 1); i++) {
                                        free(buffer[i]);
-                                       buffer[i] = bb_xstrdup(flines[line_pos + i]);
+                                       buffer[i] = xstrdup(flines[line_pos + i]);
                                }
                        }
                }
@@ -408,7 +408,7 @@ static void buffer_up(int nlines)
                        line_pos -= nlines;
                        for (i = 0; i < (height - 1); i++) {
                                free(buffer[i]);
-                               buffer[i] = bb_xstrdup(flines[line_pos + i]);
+                               buffer[i] = xstrdup(flines[line_pos + i]);
                        }
                }
                else {
@@ -418,7 +418,7 @@ static void buffer_up(int nlines)
                                line_pos -= 1;
                                for (i = 0; i < (height - 1); i++) {
                                        free(buffer[i]);
-                                       buffer[i] = bb_xstrdup(flines[line_pos + i]);
+                                       buffer[i] = xstrdup(flines[line_pos + i]);
                                }
                        }
                }
@@ -440,10 +440,10 @@ static void buffer_up(int nlines)
                        for (i = 0; i < (height - 1); i++) {
                                free(buffer[i]);
                                if (i < tilde_line - nlines + 1)
-                                       buffer[i] = bb_xstrdup(flines[line_pos + i]);
+                                       buffer[i] = xstrdup(flines[line_pos + i]);
                                else {
                                        if (line_pos >= num_flines - height + 2)
-                                               buffer[i] = bb_xstrdup("~\n");
+                                               buffer[i] = xstrdup("~\n");
                                }
                        }
                }
@@ -462,7 +462,7 @@ static void buffer_line(int linenum)
        else if (linenum < (num_flines - height - 2)) {
                for (i = 0; i < (height - 1); i++) {
                        free(buffer[i]);
-                       buffer[i] = bb_xstrdup(flines[linenum + i]);
+                       buffer[i] = xstrdup(flines[linenum + i]);
                }
                line_pos = linenum;
                buffer_print();
@@ -471,9 +471,9 @@ static void buffer_line(int linenum)
                for (i = 0; i < (height - 1); i++) {
                        free(buffer[i]);
                        if (linenum + i < num_flines + 2)
-                               buffer[i] = bb_xstrdup(flines[linenum + i]);
+                               buffer[i] = xstrdup(flines[linenum + i]);
                        else
-                               buffer[i] = bb_xstrdup((flags & FLAG_TILDE) ? "\n" : "~\n");
+                               buffer[i] = xstrdup((flags & FLAG_TILDE) ? "\n" : "~\n");
                }
                line_pos = linenum;
                /* Set past_eof so buffer_down and buffer_up act differently */
@@ -509,7 +509,7 @@ static void examine_file(void)
        newline_offset = strlen(filename) - 1;
        filename[newline_offset] = '\0';
 
-       files[num_files] = bb_xstrdup(filename);
+       files[num_files] = xstrdup(filename);
        current_file = num_files + 1;
        num_files++;
 
@@ -609,34 +609,40 @@ static char *process_regex_on_line(char *line, regex_t *pattern, int action)
           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;
 
-       line2 = bb_xstrdup(line);
+       line2 = xstrdup(line);
 
        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 = bb_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 = bb_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 = bb_xasprintf("%s%s", growline, line2);
-       
+
+       growline = xasprintf("%s%s", growline, line2);
+
        return (match_found ? growline : line);
-       
+
        free(growline);
        free(line2);
 }
@@ -663,7 +669,7 @@ static void regex_process(void)
        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);
@@ -672,7 +678,7 @@ static void regex_process(void)
                return;
        }
        uncomp_regex[strlen(uncomp_regex) - 1] = '\0';
-       
+
        /* Compile the regex and check for errors */
        xregcomp(&pattern, uncomp_regex, 0);
 
@@ -680,11 +686,11 @@ static void regex_process(void)
                /* 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);
+                       flines[i] = xstrdup(current_line);
                }
        }
        old_pattern = pattern;
-       
+
        /* Reset variables */
        match_lines = xrealloc(match_lines, sizeof(int));
        match_lines[0] = -1;
@@ -694,14 +700,14 @@ static void regex_process(void)
        /* 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);
+               flines[i] = 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) {
@@ -865,7 +871,7 @@ static void save_input_to_file(void)
        fgets(current_line, 256, inp);
        current_line[strlen(current_line) - 1] = '\0';
        if (strlen(current_line) > 1) {
-               fp = bb_xfopen(current_line, "w");
+               fp = xfopen(current_line, "w");
                for (i = 0; i < num_flines; i++)
                        fprintf(fp, "%s", flines[i]);
                fclose(fp);
@@ -879,14 +885,12 @@ static void save_input_to_file(void)
 static void add_mark(void)
 {
        int letter;
-       int mark_line;
 
        clear_line();
        printf("Mark: ");
        letter = tless_getch();
 
        if (isalpha(letter)) {
-               mark_line = line_pos;
 
                /* If we exceed 15 marks, start overwriting previous ones */
                if (num_marks == 14)
@@ -934,19 +938,14 @@ static char opp_bracket(char bracket)
        switch (bracket) {
                case '{': case '[':
                        return bracket + 2;
-                       break;
                case '(':
                        return ')';
-                       break;
                case '}': case ']':
                        return bracket - 2;
-                       break;
                case ')':
                        return '(';
-                       break;
                default:
                        return 0;
-                       break;
        }
 }
 
@@ -1116,7 +1115,7 @@ int less_main(int argc, char **argv) {
 
        int keypress;
 
-       flags = bb_getopt_ulflags(argc, argv, "EMmN~");
+       flags = getopt32(argc, argv, "EMmN~");
 
        argc -= optind;
        argv += optind;
@@ -1127,13 +1126,13 @@ int less_main(int argc, char **argv) {
                if (ttyname(STDIN_FILENO) == NULL)
                        inp_stdin = 1;
                else {
-                       bb_error_msg("Missing filename");
+                       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)
+       get_terminal_width_height(0, &width, &height);
        data_readlines();
        tcgetattr(fileno(inp), &term_orig);
        term_vi = term_orig;