grep.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* $NetBSD: grep.h,v 1.8 2012/05/06 22:27:00 joerg Exp $ */
  2. /* $OpenBSD: grep.h,v 1.15 2010/04/05 03:03:55 tedu Exp $ */
  3. /* $FreeBSD: head/usr.bin/grep/grep.h 211496 2010-08-19 09:28:59Z des $ */
  4. /*-
  5. * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
  6. * Copyright (c) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <limits.h>
  31. #include <regex.h>
  32. #include <stdbool.h>
  33. #include <stdio.h>
  34. #define getstr(n) errstr[n]
  35. extern const char *errstr[];
  36. #define VERSION "2.5.1-FreeBSD"
  37. #define GREP_FIXED 0
  38. #define GREP_BASIC 1
  39. #define GREP_EXTENDED 2
  40. #define BINFILE_BIN 0
  41. #define BINFILE_SKIP 1
  42. #define BINFILE_TEXT 2
  43. #define FILE_STDIO 0
  44. #define FILE_GZIP 1
  45. #define FILE_BZIP 2
  46. #define DIR_READ 0
  47. #define DIR_SKIP 1
  48. #define DIR_RECURSE 2
  49. #define DEV_READ 0
  50. #define DEV_SKIP 1
  51. #define LINK_READ 0
  52. #define LINK_EXPLICIT 1
  53. #define LINK_SKIP 2
  54. #define EXCL_PAT 0
  55. #define INCL_PAT 1
  56. #define MAX_LINE_MATCHES 32
  57. struct file {
  58. int fd;
  59. bool binary;
  60. };
  61. struct str {
  62. off_t off;
  63. size_t len;
  64. char *dat;
  65. char *file;
  66. int line_no;
  67. };
  68. struct epat {
  69. char *pat;
  70. int mode;
  71. };
  72. typedef struct {
  73. size_t len;
  74. unsigned char *pattern;
  75. int qsBc[UCHAR_MAX + 1];
  76. /* flags */
  77. bool bol;
  78. bool eol;
  79. bool reversed;
  80. bool word;
  81. } fastgrep_t;
  82. /* Flags passed to regcomp() and regexec() */
  83. extern int cflags, eflags;
  84. /* Command line flags */
  85. extern bool Eflag, Fflag, Gflag, Hflag, Lflag,
  86. bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag,
  87. qflag, sflag, vflag, wflag, xflag;
  88. extern bool dexclude, dinclude, fexclude, finclude, lbflag, nullflag, nulldataflag;
  89. extern unsigned char line_sep;
  90. extern unsigned long long Aflag, Bflag, mcount;
  91. extern char *label;
  92. extern const char *color;
  93. extern int binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave;
  94. extern bool notfound;
  95. extern int tail;
  96. extern unsigned int dpatterns, fpatterns, patterns;
  97. extern char **pattern;
  98. extern struct epat *dpattern, *fpattern;
  99. extern regex_t *er_pattern, *r_pattern;
  100. extern fastgrep_t *fg_pattern;
  101. /* For regex errors */
  102. #define RE_ERROR_BUF 512
  103. extern char re_error[RE_ERROR_BUF + 1]; /* Seems big enough */
  104. /* util.c */
  105. bool file_matching(const char *fname);
  106. int procfile(const char *fn);
  107. int grep_tree(char **argv);
  108. void *grep_malloc(size_t size);
  109. void *grep_calloc(size_t nmemb, size_t size);
  110. void *grep_realloc(void *ptr, size_t size);
  111. char *grep_strdup(const char *str);
  112. void printline(struct str *line, int sep, regmatch_t *matches, int m);
  113. /* queue.c */
  114. void enqueue(struct str *x);
  115. void printqueue(void);
  116. void clearqueue(void);
  117. /* file.c */
  118. void grep_close(struct file *f);
  119. struct file *grep_open(const char *path);
  120. char *grep_fgetln(struct file *f, size_t *len);
  121. /* fastgrep.c */
  122. int fastcomp(fastgrep_t *, const char *);
  123. void fgrepcomp(fastgrep_t *, const char *);
  124. int grep_search(fastgrep_t *, const unsigned char *, size_t, regmatch_t *);