aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 42ebe6e069963ed387428346b2733d6a340851c3 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "arg.h"
#include "file.h"
#include "process.h"
#include "types.h"
#include "util.h"

#define INVERT_T "\x1b[7m"
#define UINVERT_T "\x1b[27m"
#define GREY "\x1b[90m"
#define RESET "\x1b[0m"

FILE *st;
FILE *err;

void run(FILE *fp, char *filename, bool tty) {
  const char *invert_t = conf.color ? INVERT_T : "";
  const char *grey = conf.color ? GREY : "";
  const char *reset = conf.color ? RESET : "";

  struct filedata f;
  f = readfile(fp, conf.stdin);

  if (conf.pager) {
    st = popen("less", "w");
    err = st;

    if (st == NULL)
      die("popen 'less'");
  }

  if (conf.force_binary > 0) {
    f.binary = true;
  } else if (conf.force_binary == 0) {
    f.binary = false;
  }

  if (conf.headers) {
    char *addon = f.binary ? "<binary>" : "";
    if (conf.pager)
      fprintf(err, "%s%s%s%s\r\n", invert_t, basename(filename), addon, reset);
    else
      fprintf(err, "\x1b[2K\r%s%s%s%s\r\n", invert_t, basename(filename), addon,
              reset);
  }

  conf.process = (tty && !f.binary);
  if (conf.process) { // file display processing
    loadlines(&f);
  }

  if (conf.process) {
    int linecount = 1;
    for (int i = 0; i < f.lc; i++) {
      if (conf.lines) {
        char *padding = linepad(linecount, f.lc);
        fprintf(st, "%s%s%d│%s ", grey, padding, i + 1, reset);
        fwrite(f.lines[i].buf, 1, f.lines[i].len, st);
        fprintf(st, "\n");
        free(padding);
        linecount++;
      } else {
        fprintf(st, "%s\n", f.lines[i].buf);
      }
      free(f.lines[i].buf);
    }
  } else {
    fwrite(f.buf, 1, f.buflen, st);
    fflush(st);
    fwrite("\n", 1, 1, err);
  }
  free(f.buf);

  fflush(st); // prevent timing inconsistencies between st and err
  fflush(err);

  if (conf.headers) {
    float rounded;
    char *format = formatbytes(f.buflen, &rounded);

    fprintf(err, "%s%.2f %s%s\r\n", invert_t, rounded, format, reset);
  }

  if (conf.pager) {
    pclose(st); // err is already the same as st

    st = stdout;
    err = stderr;
  }
}

void initconf(void) {
  conf.force_binary = -1;
  conf.stdin = false;
  conf.has_read_stdin = false;
  conf.pager = false;
  conf.process = true;
  conf.headers = true;
  conf.color = true;
  conf.lines = true;
}

void clearstdin(void) {
  // from
  // https://stackoverflow.com/questions/7898215/how-to-clear-input-buffer-in-c
  fseek(stdin, 0, SEEK_END);
}

int main(int argc, char *argv[]) {
  initconf();

  st = stdout;
  err = stderr;

  // init no_color first so that args take priority
  char *no_color = getenv("NO_COLOR");

  if (no_color != NULL && no_color[0] != '\0') {
    conf.color = false;
  }

  bool tty = isatty(STDOUT_FILENO);
  if (argc > 1) {
    int offset = parseargs(argc, argv);
    conf.headers = conf.headers && tty; // tty still overrides user
    conf.pager = conf.pager && tty;

    for (int i = offset; i < argc; i++) {
      if (*argv[i] == '-') {
        if (conf.has_read_stdin)
          clearstdin();
        conf.has_read_stdin = true;
        conf.stdin = true;
        run(stdin, "stdin", tty);
        continue;
      }

      conf.stdin = false;
      FILE *fp = fopen(argv[i], "rb");
      if (fp == NULL)
        die(argv[i]);
      run(fp, argv[i], tty);
      fclose(fp);
      if (tty && (i + 1 != argc)) {
        printf("offset: %d argc: %d\n", i, argc);
        fprintf(err, "\r\n"); // separate concurrent files in tty
      }
    }

    if (offset == argc) {
      conf.stdin = true;
      run(stdin, "stdin", tty);
    }
  } else {
    conf.stdin = true;
    run(stdin, "stdin", tty); // for piped-input or repl-like behavior
  }

  return EXIT_SUCCESS;
}