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

続きです。

以下のように[middle *]の文字列を分岐に埋め込みます。

takk@deb9:~/tmp/grep-3.1/src$ cat -n grep.c | sed -ne '1137,1207p'
  1137  static char * print_line_middle (char *beg, char *lim, const char *line_color, const char *match_color)
  1138  {
  1139    size_t match_size;
  1140    size_t match_offset;
  1141    char *cur;
  1142    char *mid = NULL;
  1143    char *b;
  1144
  1145  printf("[middle 1]");
  1146    for (cur = beg;
  1147         (cur < lim
  1148          && ((match_offset = execute (compiled_pattern, beg, lim - beg,
  1149                                       &match_size, cur)) != (size_t) -1));
  1150         cur = b + match_size)
  1151      {
  1152        b = beg + match_offset;
  1153  printf("[middle 2]");
  1154        /* Avoid matching the empty line at the end of the buffer. */
  1155        if (b == lim)
  1156          break;
  1157  printf("[middle 3]");
  1158        /* Avoid hanging on grep --color "" foo */
  1159        if (match_size == 0)
  1160          {
  1161  printf("[middle 4]");
  1162            /* Make minimal progress; there may be further non-empty matches.  */
  1163            /* XXX - Could really advance by one whole multi-octet character.  */
  1164            match_size = 1;
  1165            if (!mid)
  1166              mid = cur;
  1167          }
  1168        else
  1169          {
  1170            /* This function is called on a matching line only,
  1171               but is it selected or rejected/context?  */
  1172            if (only_matching)
  1173              {
  1174  printf("[middle 5]");
  1175                char sep = out_invert ? SEP_CHAR_REJECTED : SEP_CHAR_SELECTED;
  1176                if (! print_line_head (b, match_size, lim, sep))
  1177                  return NULL;
  1178              }
  1179            else
  1180              {
  1181  printf("[middle 5]");
  1182                pr_sgr_start (line_color);
  1183                if (mid)
  1184                  {
  1185                    cur = mid;
  1186                    mid = NULL;
  1187                  }
  1188                fwrite_errno (cur, 1, b - cur);
  1189              }
  1190
  1191  printf("[middle 6]");
  1192            pr_sgr_start_if (match_color);
  1193            fwrite_errno (b, 1, match_size);
  1194            pr_sgr_end_if (match_color);
  1195            if (only_matching)
  1196              putchar_errno (eolbyte);
  1197          }
  1198      }
  1199
  1200  printf("[middle 8]");
  1201    if (only_matching)
  1202      cur = lim;
  1203    else if (mid)
  1204      cur = mid;
  1205
  1206    return cur;
  1207  }
takk@deb9:~/tmp/grep-3.1/src$

make。

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

確認します。


せっかく分岐を入れたのに、全く出ませんでした。
パイプへ渡すときは、print_line_middle は実行されないんですね。

Leave a Reply

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

CAPTCHA