summaryrefslogtreecommitdiff
path: root/Userland/Utilities/nl.cpp
blob: ae4d856551f07b58527e4c9e935d61f147897120 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/DeprecatedString.h>
#include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
#include <LibMain/Main.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

enum NumberStyle {
    NumberAllLines,
    NumberNonEmptyLines,
    NumberNoLines,
};

ErrorOr<int> serenity_main(Main::Arguments arguments)
{
    NumberStyle number_style = NumberNonEmptyLines;
    int increment = 1;
    char const* separator = "  ";
    int start_number = 1;
    int number_width = 6;
    Vector<DeprecatedString> files;

    Core::ArgsParser args_parser;

    Core::ArgsParser::Option number_style_option {
        Core::ArgsParser::OptionArgumentMode::Required,
        "Line numbering style: 't' for non-empty lines, 'a' for all lines, 'n' for no lines",
        "body-numbering",
        'b',
        "style",
        [&number_style](char const* s) {
            if (!strcmp(s, "t"))
                number_style = NumberNonEmptyLines;
            else if (!strcmp(s, "a"))
                number_style = NumberAllLines;
            else if (!strcmp(s, "n"))
                number_style = NumberNoLines;
            else
                return false;

            return true;
        }
    };

    args_parser.add_option(move(number_style_option));
    args_parser.add_option(increment, "Line count increment", "increment", 'i', "number");
    args_parser.add_option(separator, "Separator between line numbers and lines", "separator", 's', "string");
    args_parser.add_option(start_number, "Initial line number", "startnum", 'v', "number");
    args_parser.add_option(number_width, "Number width", "width", 'w', "number");
    args_parser.add_positional_argument(files, "Files to process", "file", Core::ArgsParser::Required::No);
    args_parser.parse(arguments);

    Vector<FILE*> file_pointers;
    if (!files.is_empty()) {
        for (auto& file : files) {
            FILE* file_pointer = fopen(file.characters(), "r");
            if (!file_pointer) {
                warnln("Failed to open {}: {}", file, strerror(errno));
                continue;
            }
            file_pointers.append(file_pointer);
        }
    } else {
        file_pointers.append(stdin);
    }

    for (auto& file_pointer : file_pointers) {
        int line_number = start_number - increment; // so the line number can start at 1 when added below
        int previous_character = 0;
        int next_character = 0;
        while ((next_character = fgetc(file_pointer)) != EOF) {
            if (previous_character == 0 || previous_character == '\n') {
                if (next_character == '\n' && number_style != NumberAllLines) {
                    // Skip printing line count on empty lines.
                    outln();
                    continue;
                }
                if (number_style != NumberNoLines)
                    out("{1:{0}}{2}", number_width, (line_number += increment), separator);
                else
                    out("{1:{0}}", number_width, "");
            }
            putchar(next_character);
            previous_character = next_character;
        }
        fclose(file_pointer);
        if (previous_character != '\n')
            outln(); // for cases where files have no trailing newline
    }
    return 0;
}