grepの色の仕組み(その6)

前回はprint_line_middleにprintfを埋め込んで確認しました。
今回はprtextにprintfを埋め込みます。

takk@deb9:~/tmp/grep-3.1/src$ cat -n grep.c | sed -ne '1299,1374p'
  1299  static void
  1300  prtext (char *beg, char *lim)
  1301  {
  1302    static bool used;     /* Avoid printing SEP_STR_GROUP before any output.  */
  1303    char eol = eolbyte;
  1304
  1305  printf("[pr 1]");
  1306    if (!out_quiet && pending > 0)
  1307      prpending (beg);
  1308
  1309    char *p = beg;
  1310
  1311    if (!out_quiet)
  1312      {
  1313  printf("[pr 2]");
  1314        /* Deal with leading context.  */
  1315        char const *bp = lastout ? lastout : bufbeg;
  1316        intmax_t i;
  1317        for (i = 0; i < out_before; ++i)
  1318          if (p > bp)
  1319            do
  1320              --p;
  1321            while (p[-1] != eol);
  1322
  1323        /* Print the group separator unless the output is adjacent to
  1324           the previous output in the file.  */
  1325        if ((0 <= out_before || 0 <= out_after) && used
  1326            && p != lastout && group_separator)
  1327          {
  1328  printf("[pr 3]");
  1329            pr_sgr_start_if (sep_color);
  1330            fputs_errno (group_separator);
  1331            pr_sgr_end_if (sep_color);
  1332            putchar_errno ('\n');
  1333          }
  1334
  1335        while (p < beg)
  1336          {
  1337  printf("[pr 4]");
  1338            char *nl = memchr (p, eol, beg - p);
  1339            nl++;
  1340            prline (p, nl, SEP_CHAR_REJECTED);
  1341            p = nl;
  1342          }
  1343      }
  1344
  1345    intmax_t n;
  1346    if (out_invert)
  1347      {
  1348  printf("[pr 5]");
  1349        /* One or more lines are output.  */
  1350        for (n = 0; p < lim && n < outleft; n++)
  1351          {
  1352            char *nl = memchr (p, eol, lim - p);
  1353            nl++;
  1354            if (!out_quiet)
  1355              prline (p, nl, SEP_CHAR_SELECTED);
  1356            p = nl;
  1357          }
  1358      }
  1359    else
  1360      {
  1361  printf("[pr 6]");
  1362        /* Just one line is output.  */
  1363        if (!out_quiet)
  1364          prline (beg, lim, SEP_CHAR_SELECTED);
  1365        n = 1;
  1366        p = lim;
  1367      }
  1368  printf("[pr 7]");
  1369
  1370    after_last_match = bufoffset - (buflim - p);
  1371    pending = out_quiet ? 0 : MAX (0, out_after);
  1372    used = true;
  1373    outleft -= n;
  1374  }
takk@deb9:~/tmp/grep-3.1/src$

7つのprintf([pr 1]~パス[pr 7])を埋め込みました。

takk@deb9:~/tmp/grep-3.1/src$ make
  CC       grep.o
  CCLD     grep
takk@deb9:~/tmp/grep-3.1/src$

確認します。

[pr 1]、[pr 2]、[pr 6]、[pr 7]のパスを通るようです。
つまり、通らないパス[pr 3]、[pr 4]、[pr 5]が色をつける処理であると言えます。

つづく

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA