-- init_compile_env
[silentbob2.git] / src / grep.cxx
blob1437ad618b2b0332f21d5ed4a81334928b5b48ba
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <gclib/gclib.h>
12 #include <gclib/gclib_c.h>
13 #include <mod.h>
14 #include <head.h>
15 #include <dbg.h>
17 extern "C" DArray * plugin_init (struct env_t *env);
19 struct grep_env_t {
20 bool ignore_case;
21 char * f_name;
23 struct grep_env_t ENV_grep;
25 void grepBuf (char * buf, int size, char * search, int s_len)
27 int nlines;
28 char *s_line;
29 char *ptr;
30 char *end;
31 char *end2;
32 char * line_ptr;
34 if (size < s_len)
35 return;
37 nlines = 1;
38 ptr = buf;
39 end = &buf[size - s_len - 1];
40 end2 = &buf[size - 1];
41 line_ptr = ptr;
43 while (ptr <= end) {
44 if (*ptr == '\n') {
45 line_ptr = ptr+1;
46 ++nlines;
49 if (! memcmp (ptr, search, s_len)) {
50 s_line = Dmid_getstr (line_ptr, end2);
51 if (! s_line)
52 break;
53 chomp (s_line);
54 if (SB_FLGET (SB_FLTAGSTYLE))
55 printf ("%s\t%s\t%i\n", search, ENV_grep.f_name, nlines);
56 else
57 printf ("%s:%i: %s\n", ENV_grep.f_name, nlines, s_line);
58 fflush (stdout);
59 free (s_line);
60 ptr += s_len;
61 continue;
63 ++ptr;
67 int grepFile (char * fileName, char * search)
69 char * ptr;
70 int size;
72 ptr = DFILE (fileName, &size);
73 if (! ptr) {
74 perror ("DFILE");
75 return -1;
77 ENV_grep.f_name = fileName;
78 grepBuf (ptr, size, search, strlen (search));
79 DROP (ptr);
80 return 0;
83 void grep_loop (DArray * files, char * search)
85 int s_len;
86 int nfiles;
87 int i;
89 if (! files || ! search)
90 return;
92 s_len = strlen (search);
93 nfiles = files->get_size ();
94 for (i = 0; i < nfiles; ++i)
95 grepFile (files->get (i), search);
98 void grep_thread (int N, char * search)
100 FILE * m_file;
101 char m_buf[512];
103 sprintf (m_buf, "%s%i", ENV->tmp_files, N);
104 m_file = fopen (m_buf, "r");
105 if (! m_file)
106 return;
107 while (fgets (m_buf, 512, m_file)) {
108 chomp (m_buf);
109 grepFile (m_buf, search);
112 fclose (m_file);
115 char grep_opt (DArray * d_opts, int * pos)
117 int count;
118 char * S;
120 if (! d_opts || ! pos)
121 return 0;
123 count = d_opts->get_size ();
124 S = d_opts->get (*pos);
125 if (EQ (S, "--grep")) {
126 ++(*pos);
127 return 1;
130 return 0;
133 char grep_opt2 (DArray * d_opts, int * pos)
135 char * S;
136 __djob_t * j;
137 int i;
139 if (! d_opts || ! pos)
140 return 0;
142 S = d_opts->get (*pos);
143 if (NE (S, "--grep"))
144 return 0;
146 ++(*pos);
147 S = d_opts->get (*pos);
148 if (! S) {
149 printf ("SilentBob grep require parameter.\n");
150 exit (1);
153 if (ENV->max_proc == 1) {
154 grep_loop (ENV->d_files, S);
155 exit (0);
158 ENV->d_files->strings_to_file (ENV->tmp_files);
159 split_tmp_files ();
160 for (i = 0; i < ENV->max_proc; ++i) {
161 j = ENV->proc_list->fork ();
162 if (j->child) {
163 grep_thread (i, S);
164 exit (0);
168 while ((j = ENV->proc_list->wait_all ()) && j)
169 Dexec_done (j);
171 remove_tmp_files ();
172 exit (0);
173 return 0;
176 void grep_short_info ()
178 printf ("Search of text.");
181 void grep_long_info ()
183 printf ("Search of text.\n");
184 printf ("Version: 1.1\n");
185 printf ("options: --grep\n");
188 int grep_init ()
190 mod_t * plug;
192 plug = CNEW (mod_t, 1);
193 memset (plug, 0, sizeof (mod_t));
194 plug->Version = strdup ("1.0");
195 plug->short_info = grep_short_info;
196 plug->long_info = grep_long_info;
197 plug->opt = grep_opt;
198 plug->opt2 = grep_opt2;
199 plug->internal = true;
200 ENV->listOptions->add ("--grep");
201 ENV->modding->add (LPCHAR (plug));
203 return 0;