2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
11 #include <gclib/gclib.h>
12 #include <gclib/gclib_c.h>
17 extern "C" DArray
* plugin_init (struct env_t
*env
);
23 struct grep_env_t ENV_grep
;
25 void grepBuf (char * buf
, int size
, char * search
, int s_len
)
39 end
= &buf
[size
- s_len
- 1];
40 end2
= &buf
[size
- 1];
49 if (! memcmp (ptr
, search
, s_len
)) {
50 s_line
= Dmid_getstr (line_ptr
, end2
);
54 if (SB_FLGET (SB_FLTAGSTYLE
))
55 printf ("%s\t%s\t%i\n", search
, ENV_grep
.f_name
, nlines
);
57 printf ("%s:%i: %s\n", ENV_grep
.f_name
, nlines
, s_line
);
67 int grepFile (char * fileName
, char * search
)
72 ptr
= DFILE (fileName
, &size
);
77 ENV_grep
.f_name
= fileName
;
78 grepBuf (ptr
, size
, search
, strlen (search
));
83 void grep_loop (DArray
* files
, char * search
)
89 if (! files
|| ! search
)
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
)
103 sprintf (m_buf
, "%s%i", ENV
->tmp_files
, N
);
104 m_file
= fopen (m_buf
, "r");
107 while (fgets (m_buf
, 512, m_file
)) {
109 grepFile (m_buf
, search
);
115 char grep_opt (DArray
* d_opts
, int * pos
)
120 if (! d_opts
|| ! pos
)
123 count
= d_opts
->get_size ();
124 S
= d_opts
->get (*pos
);
125 if (EQ (S
, "--grep")) {
133 char grep_opt2 (DArray
* d_opts
, int * pos
)
139 if (! d_opts
|| ! pos
)
142 S
= d_opts
->get (*pos
);
143 if (NE (S
, "--grep"))
147 S
= d_opts
->get (*pos
);
149 printf ("SilentBob grep require parameter.\n");
153 if (ENV
->max_proc
== 1) {
154 grep_loop (ENV
->d_files
, S
);
158 ENV
->d_files
->strings_to_file (ENV
->tmp_files
);
160 for (i
= 0; i
< ENV
->max_proc
; ++i
) {
161 j
= ENV
->proc_list
->fork ();
168 while ((j
= ENV
->proc_list
->wait_all ()) && j
)
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");
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
));