aboutsummaryrefslogtreecommitdiffhomepage
path: root/libslang/modules/grep
blob: a8f97d693260d910df51551fd8f7128a1f87ec15 (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
166
167
168
169
170
171
#!/usr/bin/env slsh

% This is a simple-minded implementation of a highlighting grep program
% designed to test the pcre module.
import ("pcre");

static define usage ()
{
   ()=fprintf(stderr, "Usage: %s [-inl] pattern [files...]\n", __argv[0]);
   exit (1);
}

static variable MATCH = 1;
static variable LINENUM = 2;
static variable FILENAME = 4;
static variable Output_Format = 0;

static define grep (file, p)
{
   variable fp;
   
   if (file == NULL)
     fp = stdin;
   else
     fp = fopen (file, "r");
   
   if (fp == NULL)
     {
	fprintf (stderr, "*** Unable to open %s\n", file);
	return;
     }

   variable linenum = 0;
   foreach (fp) using ("line")
     {
	variable str = ();
	linenum++;

	if (pcre_exec (p, str, 0))
	  {
	     variable i0, i1, i;

	     switch (Output_Format)
	       {
		case FILENAME:
		  () = fprintf (stdout, "%s\n", file);
		  return;
	       }
	       {
		case LINENUM:
		  () = fprintf (stdout, "%d\n", linenum);
		  continue;
	       }
	       {
		case (MATCH|LINENUM):
		  
		  () = fprintf (stdout, "%d:", linenum);
	       }
	       {
		case (LINENUM|FILENAME):
		  () = fprintf (stdout, "%s:%d\n", file, linenum);
		  continue;
	       }
	       {
		case (LINENUM|FILENAME|MATCH):
		  () = fprintf (stdout, "%s:%d:", file, linenum);
	       }
	       {
		case (FILENAME|MATCH):
		  () = fprintf (stdout, "%s:", file);
	       }
	       
	     do
	       {
		  i = pcre_nth_match (p, 0);
		  i0 = i[0];
		  i1 = i[1];
		  if (i1 <= i0)
		    break;
		  if (i0 > 0)
		    () = fprintf (stdout, "%s", str[[0:i0-1]]);
		  () = fprintf (stdout, "\e[7m%s\e[m", str[[i0:i1-1]]);
		  str = str[[i1:]];
	       }
	     while (pcre_exec (p, str, 0));
	     () = fprintf (stdout, "%s", str);
	  }
     }
}

static define main (argc, argv)
{
   variable i;
   variable files, pattern;
   variable options;
   
   i = 1;
   files = NULL;
   pattern = NULL;
   options = 0;
   Output_Format = MATCH;

   while (i < argc)
     {
	variable arg = __argv[i];
	i++;

	if (arg[0] == '-')
	  {
	     foreach (arg[[1:]])
	       {
		  variable ch = ();
		  switch (ch)
		    {
		     case 'i':
		       options |= PCRE_CASELESS;
		    }
		    {
		     case 'l':
		       Output_Format &= ~MATCH;
		       Output_Format |= FILENAME;
		    }
		    {
		     case 'n':
		       Output_Format |= LINENUM;
		    }
		    {
		       () = fprintf (stderr, "Unsupported option: '%c'\n", ch);
		       exit (1);
		    }
	       }
	     continue;
	  }
		       
	pattern = arg;
	break;
     }

   if (pattern == NULL)
     usage ();

   variable p = pcre_compile (pattern, options);

   if (i == __argc)
     {
	if (isatty (stdin))
	  usage ();

	Output_Format &= ~FILENAME;
	grep (NULL, p);
	return;
     }
   
   files = argv[[i:]];

   if (length(files) > 1)
     Output_Format |= FILENAME;

   foreach (files)
     {
	variable f = ();
	grep (f, p);
     }
}

public define slsh_main ()
{
   main (__argc, __argv);
   exit (0);   
}