"deprecated" warning fix
[opsoft.git] / silentbob / sblib / Sblib.cxx
blobcaa749f38a9363f57b689278b153d285b03787f2
1 /*
2 * (c) Oleg Puchinin 2006,2007
3 * graycardinalster@gmail.com
5 */
7 #include <gclib/gclib.h>
8 #include <env.h>
9 #include <bob_flags.h>
10 #include <head.h>
11 #include <wit.h>
12 #include <dbg.h>
13 #include <string.h>
14 #include <sys/wait.h>
15 #include <errno.h>
16 #include <the_tt.h>
18 struct env_t *ENV;
20 char * __bice_get (char *optname)
22 char * value;
23 value = ENV->settings->get (optname);
24 if (! value)
25 value = getenv (optname);
26 return value;
29 EHash * bob_init_compile_env ()
31 EHash * compile_env;
32 char *cc;
33 char *cxx;
34 char *cflags;
35 char *cxxflags;
36 char *opts;
37 char *include;
38 char *libs;
39 char *ldflags;
41 compile_env = new EHash;
43 cc = __bice_get ((char *) "CC");
44 cxx = __bice_get ((char *) "CXX");
45 cflags = __bice_get ((char *) "CFLAGS");
46 cxxflags = __bice_get ((char *) "CXXFLAGS");
47 opts = __bice_get ((char *) "OPTS");
48 include = __bice_get ((char *) "INCLUDE");
49 libs = __bice_get ((char *) "LIBS");
50 ldflags = __bice_get ((char *) "LDFLAGS");
52 if (! cc)
53 cc = (char *) "gcc";
54 if (! cxx)
55 cxx = (char *) "g++";
56 if (! cflags)
57 cflags = (char *) "-O3 -Wall -pipe";
58 if (! cxxflags)
59 cxxflags = cflags;
60 if (! opts)
61 opts = (char *) "";
62 if (! include)
63 include = (char *) "";
64 if (! libs)
65 libs = (char *) "";
66 if (! ldflags)
67 ldflags = (char *) "";
69 if (SB_FLGET (SB_FLVERBOSE)) {
70 printf ("C compiler: %s\n", cc);
71 printf ("C++ compiler: %s\n", cxx);
72 printf ("C flags: %s\n", cflags);
73 printf ("C++ flags: %s\n", cxxflags);
74 printf ("OPTS: %s\n", opts);
75 printf ("INCLUDE: %s\n", include);
76 printf ("LIBS: %s\n", libs);
77 printf ("LDFLAGS: %s\n", ldflags);
80 compile_env->set ((char *) "CC", strdup (cc));
81 compile_env->set ((char *) "CXX", strdup (cxx));
82 compile_env->set ((char *) "CFLAGS", strdup (cflags));
83 compile_env->set ((char *) "CXXFLAGS", strdup (cxxflags));
84 compile_env->set ((char *) "OPTS", strdup (opts));
85 compile_env->set ((char *) "INCLUDE", strdup (include));
86 compile_env->set ((char *) "LIBS", strdup (libs));
87 compile_env->set ((char *) "LDFLAGS", strdup (ldflags));
89 return compile_env;
92 int sb_prname (char *arg)
94 int i_cmd = 0;
95 char * d_ptr;
96 d_ptr = arg + strlen (arg);
97 d_ptr--;
99 while (*d_ptr != '\\' &&
100 d_ptr > arg)
101 d_ptr--;
103 if (*d_ptr == '\\')
104 d_ptr++;
106 if (EQ (d_ptr, "tags"))
107 i_cmd = cmd_tags;
108 else if (EQ (d_ptr, "the_tt"))
109 i_cmd = cmd_the_tt;
110 else if (EQ (d_ptr, "gc_indent"))
111 i_cmd = cmd_indent;
112 else if (EQ (d_ptr, "structs"))
113 i_cmd = cmd_give_structs;
115 return i_cmd;
118 // SilentBob --tags
119 void tags (DArray * d_names, char * d_file_output)
121 struct fdump_param_t d_param;
122 EArray * d_tags;
123 struct d_tag_t * d_tag;
124 int a, i, n_names;
126 if (! d_names)
127 return;
129 n_names = d_names->get_size ();
131 for (i = 0; i < n_names; i++) {
132 d_tags = got_tag (d_names->get (i));
133 if (d_tags == NULL) {
134 if (d_tags->get_size () == 0) {
135 fprintf (ENV->d_stream_dbg, "Tag not found : %s\n",
136 d_names->get (i));
137 fflush (ENV->d_stream_dbg);
138 delete d_tags;
139 continue;
143 if (! d_tags)
144 continue;
146 if(! d_tags->get_size ()) {
147 delete d_tags;
148 d_tags = NULL;
149 continue;
152 fault (! d_tags);
154 for (a = 0; a < d_tags->get_size (); a++) {
155 d_tag = (d_tag_t *) d_tags->get (a);
157 fault (! d_tag);
159 if (! d_file_output)
160 printf ("// file %s line %i\n",
161 d_tag->d_file, d_tag->d_line);
163 memset (&d_param, 0, sizeof (struct fdump_param_t));
164 d_param.n_trip = 0;
165 d_param.d_file_name = d_tag->d_file;
166 d_param.d_line = d_tag->d_line;
167 d_param.linear = true;
168 d_param.d_file_output = d_file_output;
169 if (d_tag->d_type == OT::Function)
170 d_param.b_force_block = true;
172 nogui_fdump (&d_param);
173 if (! d_file_output)
174 fputc ('\n', stdout);
176 DROP (d_tag);
179 if (d_tags) {
180 d_tags->drop ();
181 delete d_tags;
185 fflush (stdout);
188 void bug_longmacro ()
190 printf ("Too big macro."
191 "If your macro have more than 300 lines, please "
192 "contact <graycardinal@pisem.net>\n"
193 "Program stopped.\n");
195 exit (0);
198 void bug_nosuch_tag (char * f_name)
200 printf ("Tag \"%s\" not found. Broken \"tags\" file ? "
201 "Try \"silent-bob --make-ctags\".\n", f_name);
204 void bug_nocalltags ()
206 printf ("File \"call_tags\" not found. "
207 "Try \"silent-bob --call-tags [-L] <files>\"\n");
208 exit (1);
211 void bug_system ()
213 printf ("Can't make tags file. Maybe you do not have write permissions ?\n");
214 exit (1);
217 void bug_fork ()
219 perror ("fork");
220 exit (1);
223 void bug_plugin (char *name)
225 fprintf (stderr, "Can't load plugin (%s)\n", name);
228 void bug_notsupported ()
230 printf ("SilentBob (or language plugin)"
231 " don't support this feature !\n");
234 bool bob_tag (char *d_str, char * d_name, d_tag_t * d_tag)
236 char m_buf[256];
237 char *d_file;
238 char *S;
240 strcpy (m_buf, d_str);
241 d_str = m_buf;
243 strncpy (d_tag->d_name, d_name, 255);
244 d_tag->d_name[255] = 0;
245 S = strchr (d_str, '\t');
246 if (! S)
247 return false;
249 S++;
250 d_file = S;
251 S = strchr (d_file, '\t');
252 if (! S)
253 return false;
255 *S = 0;
256 strncpy (d_tag->d_file, d_file, 255);
257 d_tag->d_file[255] = 0;
259 S++;
261 if (if_digit (S))
262 d_tag->d_line = atoi (S);
263 else
264 return false;
266 return true;
269 char * cts (struct c_word * d_word)
271 char * S;
273 if (d_word == NULL)
274 return NULL;
276 S = d_word->S;
277 if (!strncmp (S, "else ", 5))
278 S += 5;
280 if (d_word->ch != '(')
281 return NULL;
283 while (!strncmp (S, "do ", 3))
284 S += 3;
286 if (!strncmp (S, "return ", 7))
287 S += 7;
289 if (ENV->d_cops->sfind (S) != -1)
290 return NULL;
292 if (words_count (S) != 1)
293 return NULL;
295 return S;
298 bool def_test (char * d_op)
300 char * S = d_op;
301 ++S;
302 while (*S == ' ' || *S == '\t')
303 ++S;
305 if (! strncmp (S, "define", 6))
306 return true;
307 return false;
310 int find_cfiles ()
312 find_one_ext ((char *) "*.h");
313 find_one_ext ("*.hpp");
314 find_one_ext ("*.cpp");
315 find_one_ext ("*.c");
316 find_one_ext ("*.cc");
317 find_one_ext ("*.cxx");
318 return 0;
321 int sblib_find (const char * path, const char * name, const char * f_outname)
323 int devnull;
324 int fd;
325 int pid;
326 int status = -1;
328 pid = fork ();
329 if (pid == 0) {
330 devnull = open ("/dev/null", O_WRONLY, 0600);
331 fd = open (f_outname, O_WRONLY, 0600);
332 if (fd == -1) {
333 fd = open (f_outname, O_WRONLY | O_CREAT, 0600);
334 if (fd == -1) {
335 close (devnull);
336 return -1;
338 } else
339 lseek (fd, 0, SEEK_END);
340 dup2 (devnull, 2);
341 dup2 (fd, 1);
342 execlp ("find", path, "-name", name, NULL);
343 } else if (pid > 0) {
344 waitpid (pid, &status, 0);
345 return status;
348 return status;
351 # define TMP_FILE_NAME (char *) "./silent_bob.tmp"
352 struct tt_state_t * get_onett_tag (char * f_name, char ** d_tt_buf)
354 DArray d_array;
355 tt_state_t * Ret = NULL;
356 char * S;
358 if (d_tt_buf)
359 *d_tt_buf = NULL;
361 d_array.add (f_name);
362 Ret = CNEW (tt_state_t, 1);
364 unlink (TMP_FILE_NAME);
365 tags (&d_array, TMP_FILE_NAME);
367 while( 1 ) {
368 if (access (TMP_FILE_NAME, R_OK) != 0)
369 break;
371 Ret->fileName = strdup (TMP_FILE_NAME);
372 S = THE_TT::do_tt_file (Ret);
374 if (S == NULL)
375 break;
377 if (Ret->mmaped)
378 munmap (Ret->fileData, Ret->fileDataSize);
380 if (Ret->fd)
381 close (Ret->fd);
383 if (d_tt_buf)
384 *d_tt_buf = S;
386 unlink (TMP_FILE_NAME);
387 return Ret;
390 DROP (Ret);
391 unlink (TMP_FILE_NAME);
392 return NULL;
395 void globalsPrint (tt_state_t * tt, char * d_out, int d_found_type)
397 int t_line;
398 char * S;
400 if (SB_FLGET (SB_FLSIMULATE))
401 return;
403 if (d_found_type & OT::Other)
404 return;
406 t_line = tt->attachment[ENV->t_op_no].pair_line+1;
407 t_line += ww_begin_line (tt, d_out,
408 tt->attachment[ENV->t_op_no].offset);
410 if (SB_FLGET(SB_FLTAGSTYLE))
411 mk_tag (d_out, tt->fileName,
412 t_line, d_found_type);
413 else {
414 if (d_found_type & OT::Class) {
415 S = strchr (d_out, ':');
416 if (S) {
417 *S = '\0';
418 if (S[-1] == ' ')
419 S[-1] = '\0';
422 printf ("%s\t\t//file %s //line %i\n", d_out,
423 tt->fileName, t_line);
427 #define NOT_VALID 0
428 #define CALL_BACK 1
429 #define VALID 2
431 EArray * got_tag (char * d_tag)
433 struct d_tag_t *d_new_tag;
434 char d_buf[256];
435 EArray * d_ret;
436 int d_len;
437 char *S;
438 int i;
440 d_ret = new EArray;
442 if (ENV->d_tags_file == NULL) {
443 ENV->d_tags_file = new EArray;
444 ENV->d_tags_file->from_file ((char *) "./tags");
447 snprintf (d_buf, 255, "%s\t", d_tag);
448 d_len = strlen (d_buf);
450 if (ENV->d_tags_file->get_size () == 0)
451 return d_ret;
453 i = ENV->d_tags_file->snfind_fast (d_buf, strlen (d_buf));
455 if (i == -1) {
456 fprintf (ENV->d_stream_dbg, "\tENV->d_tags_file->snfind_fast == -1"); LN;
457 return d_ret;
460 d_len = strlen (d_buf);
461 do {
462 i++;
463 S = ENV->d_tags_file->get (i);
464 if (! S)
465 break;
466 } while (! strncmp (S, d_buf, d_len));
467 --i;
469 while (true) {
470 S = ENV->d_tags_file->get (i);
471 fprintf (ENV->d_stream_dbg, "\ttag : %s\n", S); fflush (ENV->d_stream_dbg);
473 d_new_tag = CNEW (d_tag_t, 1);
474 memset (d_new_tag, 0, sizeof (d_tag_t));
475 if (strstr (S, ";\tf"))
476 d_new_tag->d_type = OT::Function;
478 if (bob_tag (S, d_tag, d_new_tag) == false) {
479 DROP (d_new_tag);
480 fprintf (ENV->d_stream_dbg, "\tBAD tag : %s\n", S);
481 return d_ret;
484 d_ret->add ((long) d_new_tag);
486 i--;
487 if (i < 0)
488 break;
490 S = ENV->d_tags_file->get (i);
491 if (strncmp (S, d_tag, strlen (d_tag)))
492 break;
495 return d_ret;
498 char * macro_name (char * d_op, char * d_macro_name)
500 char *d_begin;
501 char *S;
502 char m_buf[256];
504 strncpy (m_buf, d_op, 255);
505 m_buf[255] = 0;
507 S = strstr (m_buf, "define");
509 if (! S)
510 return NULL;
512 S = strchr (S, ' ');
513 if (! S)
514 return NULL;
516 while (*S == ' ')
517 S++;
519 d_begin = S;
520 S = strchr (d_begin, ' ');
521 if (S)
522 *S = 0;
524 S = strchr (d_begin, '(');
525 if (S)
526 *S = 0;
528 strcpy (d_macro_name, d_begin);
529 return d_macro_name;
532 void mk_tag_macro (char * d_op, char * d_file, int t_line)
534 char S[256];
536 if (! macro_name (d_op, S))
537 return;
539 printf ("%s\t%s\t%i\n", S, d_file, t_line);
542 void mk_tag (char * d_op, char * d_file, int line, int d_found_type)
544 char * d_format = (char *) "%s\t%s\t%i\n";
545 char * S;
547 if (d_found_type & OT::Define) {
548 mk_tag_macro (d_op, d_file, line);
549 return;
552 if (d_found_type & OT::Class) {
553 S = strchr (d_op, ':');
554 if (S)
555 *S = 0;
558 S = ww_last_word (d_op);
559 if (! S || !(*S) || (*S == ' '))
560 return;
562 if (*S == 's') {
563 if (! strncmp (S, "static", 6))
564 return;
565 if (! strncmp (S, "struct", 6))
566 return;
569 if (*S == 'u' && !strncmp (S, "union", 5))
570 return;
571 if (*S == 'e' && !strncmp (S, "enum", 4))
572 return;
574 if (d_found_type & OT::Function)
575 d_format = (char *) "%s\t%s\t%i\t;\tf\n";
576 else if (d_found_type & OT::Class)
577 d_format = (char *) "%s\t%s\t%i\t;\tc\n";
578 else if (d_found_type & OT::Struct)
579 (char *) "%s\t%s\t%i\t;\ts\n";
581 printf (d_format, S, d_file, line);
584 DArray * mk_tag_structtail_split (char *S)
586 bool b_macro = false;
587 char * d_old = NULL;
588 int brace_depth = 0;
589 DArray * d_array;
591 d_array = new DArray (128);
592 if (! d_array)
593 return NULL;
595 d_old = S;
597 while (true) {
598 if (*S == '\"' || *S == '\'') {
599 S = sstrend (S);
600 if (S == NULL || *S == '\0')
601 break;
604 if (*S == '(')
605 brace_depth++;
607 if (*S == ')') {
608 brace_depth--;
609 if (! brace_depth) {
610 S++;
611 break;
615 if (*S == 0) {
616 if (! b_macro)
617 d_array->add (d_old);
618 break;
621 if (brace_depth) {
622 S++;
623 continue;
626 if (S[1] == '(') {
627 b_macro = true;
628 S++;
629 continue;
632 if (*S == ' ' || *S == ',') {
633 *S = 0;
634 S++;
635 b_macro = false;
636 while (*S == ' ' || *S == '*')
637 S++;
638 if (! b_macro)
639 d_array->add (d_old);
640 d_old = S;
642 continue;
644 S++;
647 return d_array;
650 void mk_tag_structtail (char * S, char * d_file, int t_line)
652 char *w;
653 DArray * d_array;
654 int i;
656 d_array = mk_tag_structtail_split (S);
657 if (! d_array)
658 return;
660 for (i = 0; i < d_array->get_size (); i++) {
661 w = ww_last_word (d_array->get (i));
662 if (! w || !(*w) || *w == ' ')
663 continue;
664 printf ("%s\t%s\t%i\n", w, d_file, t_line);
667 delete d_array;
670 void mk_tags (char *f_name, DArray *d_in)
672 DHeapSort * heap;
673 DArray * d_array = NULL;
674 char *S;
675 FILE * my_file;
676 int d_size;
677 int i;
679 if (d_in == NULL) {
680 d_array = new DArray (1024);
681 d_array->from_file (ENV->tmp_tags);
682 } else
683 d_array = d_in;
685 d_size = d_array->get_size ();
686 heap = new DHeapSort (d_size);
688 for (i = 0; i < d_size; ++i)
689 heap->add (d_array->get (i));
691 my_file = fopen (f_name, "w");
692 if (my_file == NULL) {
693 fprintf (stderr, "file %s:\n", f_name);
694 perror ("fopen");
695 return;
698 fprintf (my_file, "!_TAG_FILE_FORMAT\t2\n");
699 fprintf (my_file, "!_TAG_FILE_SORTED\t1\n");
700 fprintf (my_file, "!_TAG_PROGRAM_AUTHOR\tOleg Puchinin (graycardinalster@gmail.com)\n");
701 fprintf (my_file, "!_TAG_PROGRAM_NAME\tSilent Bob\n");
702 fprintf (my_file, "!_TAG_PROGRAM_URL\thttp://sf.net/projects/silentbob\n");
703 fprintf (my_file, "!_TAG_PROGRAM_VERSION\t1.6\n");
705 while ((S = heap->extract_min ()) && S)
706 fprintf (my_file, "%s", S);
708 if (d_in == NULL) {
709 d_array->foreach (free);
710 delete d_array;
713 fclose (my_file);
714 delete heap;
717 char * name2obj (char * name)
719 char *S;
720 char m_buf[512];
721 if (! name)
722 return NULL;
723 strcpy (m_buf, name);
724 S = rindex (m_buf, '.');
725 if (! S)
726 return NULL;
727 strcpy (S, ".o");
728 return strdup (m_buf);
730 bool b_in_comment;
732 bool brace_count (char * d_str, int * d_count, bool b_force_block) // "nice"
734 bool Ret = false;
736 if (! d_str || ! d_count)
737 return false;
739 while (*d_str != 0) {
740 if (!strncmp (d_str, "/*", 2)) {
741 b_in_comment = true;
742 d_str += 2;
743 continue;
746 if (b_in_comment) {
747 if (strncmp (d_str, "*/", 2)) {
748 d_str ++;
749 continue;
750 } else {
751 d_str += 2;
752 b_in_comment = false;
753 continue;
757 if (!strncmp (d_str, "//", 2))
758 break;
760 if (*d_str == '\"' || *d_str == '\'') {
761 d_str = sstrend (d_str);
762 if (d_str == NULL || *d_str == 0) {
763 assert (true, "HimTeh 4");
764 break;
768 if (*d_str == '{') {
769 Ret = true;
770 (*d_count)++;
773 if (*d_str == '}') {
774 Ret = true;
775 (*d_count)--;
778 if (*d_str == ';' && *d_count == 0 && !b_force_block) {
779 Ret = true;
780 break;
782 d_str++;
785 return Ret;
788 void nogui_fdump (struct fdump_param_t * d_param)
790 int d_count = 0;
791 DArray d_array;
792 FILE * d_file;
793 int d_size;
794 char * S;
795 int i,a;
797 if (!d_array.from_file (d_param->d_file_name))
798 return;
800 if (d_param->d_file_output == NULL)
801 d_file = stdout;
802 else
803 d_file = fopen (d_param->d_file_output, "w");
805 if (! d_file)
806 return;
808 if (! d_param->linear) {
809 for (a = 0; a < d_param->n_trip; a++)
810 fprintf (d_file, "\t");
811 fprintf (d_file, "//<***>\n");
814 i = d_param->d_line-1;
815 d_size = d_array.get_size ();
816 b_in_comment = false;
818 if (d_array.get (i)[0] != '#') {
819 while (i < d_size) {
820 if (!d_param->linear) {
821 for (a = 0; a < d_param->n_trip; a++)
822 fprintf (d_file, "\t");
824 fprintf (d_file, "%s", d_array.get(i));
826 if (brace_count (d_array.get(i), &d_count, d_param->b_force_block) && !d_count)
827 break;
829 if (!d_count && ((i - d_param->d_line) > 2) && !d_param->b_force_block)
830 break;
832 i++;
834 } else {
835 do {
836 S = d_array.get (i);
837 fprintf (d_file, "%s", S);
838 S = &S[strlen (S)-2];
839 while ((*S == ' ') || (*S == '\t'))
840 S--;
842 if (*S != '\\')
843 break;
844 i++;
845 } while (i < d_size);
848 if (!d_param->linear) {
849 for (a = 0; a < d_param->n_trip; a++)
850 fprintf (d_file, "\t");
851 fprintf (d_file, "//</***>\n");
854 if (d_param->d_file_output != NULL)
855 fclose (d_file);
857 d_array.foreach (free);
861 /* code for "linear" functionality, */
862 void nogui_tagsdump (char * f_name, int n_trip) {
863 DArray * d_tags;
864 d_tag_t * d_tag;
865 struct fdump_param_t d_param;
866 int a,i;
868 d_tags = got_tag (f_name);
870 assert (d_tags->get_size () == 0, "HimTeh 1");
871 for (i = 0; i < d_tags->get_size (); i++) {
872 d_tag = (d_tag_t *) d_tags->get (i);
873 if (i != 0)
874 fputc ('\n', stdout);
876 if (!SB_FLGET(SB_FLLINEAR)) {
877 for (a = 0; a < n_trip; a++)
878 fputc ('\t', stdout);
881 printf ("// file %s line %i\n", d_tag->d_file, d_tag->d_line);
883 memset (&d_param, 0, sizeof (struct fdump_param_t));
884 d_param.n_trip = n_trip;
885 d_param.d_file_name = d_tag->d_file;
886 d_param.d_line = d_tag->d_line;
887 d_param.linear = SB_FLGET (SB_FLLINEAR);
888 if (d_tag->d_type & OT::Function)
889 d_param.b_force_block = true;
890 nogui_fdump (&d_param);
893 d_tags->foreach (free);
894 delete d_tags;
895 printf ("\n");
898 void opMacro (char ** d_ptr, char ** d_out, char ch)
900 char *macro_start;
901 char * d_my;
902 int n = 0;
904 d_my = *d_out;
905 macro_start = *d_out;
906 while (true) {
907 if (ch == '\n')
908 n++;
910 if ((ch == '\n') && (d_my[strlen (d_my) - 1] != '\\'))
911 break;
913 // if (n > 300)
914 // break;
916 ch = t_op (d_ptr, d_out);
917 ENV->t_op_no++;
918 if (ch == 0)
919 break;
920 d_my = *d_out;
924 int remove_tmp_files ()
926 char m_buf[512];
927 int i = 0;
929 for (i = 0; i < ENV->max_proc; ++i) {
930 sprintf (m_buf, "%s%i", ENV->tmp_files, i);
931 unlink (m_buf);
934 unlink (ENV->tmp_files);
935 unlink (ENV->tmp_tags);
936 return ENV->max_proc;
939 void setParam (char *opt)
941 char m_buf[512];
942 char *S;
944 if (! opt)
945 return;
946 strcpy (m_buf, opt);
947 S = index (m_buf, '=');
948 if (! S)
949 return;
951 *S = '\0';
952 ++S;
953 strip2 (m_buf);
954 strip (S);
955 ENV->settings->set (m_buf, strdup (S));
958 int split_tmp_files ()
960 FILE * f_tmpfiles;
961 FILE ** proc_files;
962 char m_buf[512];
963 int i = 0;
965 f_tmpfiles = fopen (ENV->tmp_files, "r");
966 if (! f_tmpfiles)
967 return -1;
969 proc_files = CNEW (FILE *, ENV->max_proc);
970 memset (proc_files, 0, sizeof (FILE *) * ENV->max_proc);
971 errno = 0;
972 for (i = 0; i < ENV->max_proc; ++i) {
973 sprintf (m_buf, "%s%i", ENV->tmp_files, i);
974 unlink (m_buf);
975 proc_files[i] = fopen (m_buf, "w");
976 if (! proc_files[i]) {
977 perror ("fopen");
978 return -1;
982 i = 0;
983 while (fgets (m_buf, 512, f_tmpfiles)) {
984 fprintf (proc_files[i], "%s", m_buf);
985 if (++i >= ENV->max_proc)
986 i = 0;
989 for (i = 0; i < ENV->max_proc; ++i)
990 fclose (proc_files[i]);
992 return ENV->max_proc;
995 DArray * split_to_words (char * d_op)
997 DArray * d_Ret = new DArray (16);
998 char * d_old = strdup (d_op);
999 bool b_done = false;
1000 char * S = d_old;
1001 char * d_end;
1002 char ch;
1004 if (d_Ret == NULL || d_old == NULL)
1005 return NULL;
1007 while (true) {
1008 b_done = false;
1009 d_end = S;
1011 if (*d_end == ' ')
1012 d_end++;
1014 while (*d_end) {
1015 if (!(if_abc(d_end) ||
1016 if_digit (d_end) ||
1017 *d_end == '_' ||
1018 *d_end == ' ') )
1019 break;
1020 d_end ++;
1023 if (! *d_end) {
1024 ch = 0;
1025 b_done = true;
1026 goto split_to_words_L1;
1029 ch = *d_end;
1030 if (d_end[-1] == ' ')
1031 d_end[-1] = 0;
1032 else
1033 *d_end = 0;
1035 while (*S && *S == ' ')
1036 S++;
1038 split_to_words_L1:
1039 d_Ret->add (LPCHAR(new_cword (S, ch)));
1041 if (b_done)
1042 break;
1044 if (ch == '\"' || ch == '\'') {
1045 *d_end = ch;
1046 d_end = sstrend (d_end);
1047 assert (d_end == NULL, "Lena 1");
1048 if (*d_end == '\0' || *(++d_end) == '\0')
1049 break;
1052 S = d_end + 1;
1055 DROP (d_old);
1056 return d_Ret;
1059 char * sstrend (char * d_ptr)
1061 bool t_instring = false;
1062 int d_slash_count;
1063 char ch_last;
1064 char *d_old;
1065 unsigned limit = 1024;
1067 if (! d_ptr)
1068 return (char *) 0;
1070 if (!(*d_ptr))
1071 return (char *) 0;
1073 ch_last = *d_ptr;
1074 d_old = d_ptr;
1075 limit--;
1076 while (*d_ptr && (limit > 0)) {
1077 if (*d_ptr == '\'' || *d_ptr == '\"') {
1078 if (t_instring && *d_ptr != ch_last) {
1079 d_ptr++;
1080 continue; // Mmm...
1083 if (t_instring) {
1084 if (d_ptr[-1] == '\\') {
1085 d_slash_count = 1;
1086 while (d_ptr [-(d_slash_count)] == '\\')
1087 d_slash_count++;
1089 if (d_slash_count & 1)
1090 t_instring = false;
1091 } else {
1092 d_ptr++;
1093 t_instring = false;
1094 continue;
1096 } else {
1097 ch_last = *d_ptr;
1098 t_instring = true;
1102 if (t_instring) {
1103 d_ptr++;
1104 continue;
1105 } else
1106 break;
1109 d_ptr --;
1111 if (*d_ptr == 0)
1112 return 0;
1114 return d_ptr;
1117 char * sstrkill (char *OP)
1119 char *S;
1120 char *tail;
1122 if (! OP)
1123 return NULL;
1125 S = OP;
1126 while (*S) {
1127 if (*S == '\"' || *S == '\'') {
1128 tail = sstrend (S);
1129 if (! tail)
1130 break;
1132 if (*tail == '\0' ||*(tail+1) == '\0') {
1133 *S = '\0';
1134 break;
1137 ++S;
1138 strcpy (S, tail);
1140 ++S;
1143 return OP;
1146 int words_count (char *S)
1148 bool b_begin = true;
1149 int d_ret = 0;
1151 if (S == 0)
1152 return 0;
1154 while (*S) {
1155 if (*S == ' ') {
1156 b_begin = true;
1157 S++;
1158 continue;
1161 if (b_begin) {
1162 if (if_abc (S) ||
1163 (*S == '_') ||
1164 (*S == '*') ||
1165 (*S == '&')) {
1166 S++;
1167 d_ret ++;
1168 b_begin = false;
1169 continue;
1170 } else
1171 break;
1172 } else {
1173 if (!(if_abc (S) || (*S == '_')
1174 || (*S == '*') || (if_digit (S))))
1175 break;
1178 S++;
1179 b_begin = false;
1182 return d_ret;
1185 int ww_begin_line (struct tt_state_t * d_tt_state, char *d_out, int d_offset)
1187 char *S = &d_tt_state->fileData [d_offset] - 1;
1188 char * d_end = &d_out[strlen (d_out)] - 1;
1189 int Ret = 0;
1191 while (d_end > d_out) {
1192 if (*d_end == ' ' || *d_end == '\t') {
1193 while ((S >= d_tt_state->fileData) && (*S == ' ' || *S == '\t'))
1194 --S;
1196 if (S < d_tt_state->fileData)
1197 return Ret;
1199 while ((d_end >= d_out) && (*d_end == ' ' || *d_end == '\t'))
1200 --d_end;
1202 if (d_end <= d_out)
1203 return Ret;
1205 continue;
1208 if (*S == '\n')
1209 --Ret;
1211 if (*S == *d_end)
1212 --d_end;
1214 --S;
1217 return Ret;
1220 char * ww_begin_offset (struct tt_state_t * d_tt_state, char *d_out, int d_offset)
1222 char *S = &d_tt_state->fileData [d_offset] - 1;
1223 char * d_real = &d_out[strlen (d_out)] - 1;
1225 while (d_real != d_out) {
1226 if (*d_real == ' ' || *d_real == '\t') {
1227 while (*S == ' ' || *S == '\t')
1228 S--;
1230 while ((*d_real == ' ' || *d_real == '\t')
1231 && (d_real != d_out))
1232 --d_real;
1234 continue;
1237 if (*S == *d_real)
1238 --d_real;
1240 --S;
1243 return S;
1246 char * ww_last_word (char *d_op)
1248 char * S = d_op;
1249 char * d_word;
1251 while (*S) {
1252 if (*S == '(' || *S == '=' || *S == '[')
1253 break;
1254 S++;
1257 if (S[-1] == ' ')
1258 S--;
1260 *S = 0;
1261 d_word = d_op;
1262 while (true) {
1263 S = strchr (d_word, ' ');
1264 if (S == NULL)
1265 break;
1266 d_word = S+1;
1269 while (*d_word == '*' ||
1270 *d_word == '&' ||
1271 *d_word == ' ')
1272 d_word++;
1274 return d_word;
1277 bool validOption (char *Name)
1279 char *S;
1280 if (! Name)
1281 return false;
1283 S = ENV->listOptions->first ();
1284 while (S) {
1285 if (EQ (Name, S))
1286 return true;
1287 S = ENV->listOptions->next ();
1289 return false;
1292 int lastPluginID = 0;
1293 int newPluginID ()
1295 return ++lastPluginID;