#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include "extern.h"

exeseek()
      {   
          char param[100],fjunle[9],fhiscall[7],fflg[2],seeksnt[82];
          int i,flg;
          
          if ( ! strcmp(nowjle,mainjle) )
            { strcpy(ssent,"\n! ＭＡＩＮ からは検索できません。ボードを選んでください。\n");
              rsout();
              return(0);
            }
            
          fpw = fopen(seek_hdr,"r");
          if ( fpw )
            { fclose(fpw);
              strcpy(ynsent,"\n前回の検索から更に絞りこみますか？ ");
              if ( y_or_n() )  unlink(seek_hdr);
            }
          else
            { strcpy(param,nowjle);
              for ( i = 7 ; i > 0 ; i-- )
                  {  if ( *(param + i) != ' ' )
                        { i++; *(param + i) = NULL; break; }
                  }
                  
              sprintf(ssent,"\n%sのボードで文字列検索をおこないます。\n",param);
              rsout();
            }
          strcpy(ynsent,"\n詳しい使用方法をご覧になりますか？ ");
          if ( ! y_or_n() )
             {  strcpy(fname,SEEK_HLP);
                sendfile(0);
             }

          strcpy(ssent,"\n検索文字列を入力して下さい。( ｷｬﾝｾﾙ ﾊ cr )\n");
          strcat(ssent,"   ( , や ; で区切って複数指定可 そのときは OR 条件になる)\n");
          strcat(ssent,"文字列=>");
          rsout();
  
          
          rsin(0);
          if ( *(rsent) == '\n' )
            { strcpy(ssent,"\n--- キャンセルされました ---\n\n");
              rsout();
              return(0);
            }
          
          *(rsent + 80) = NULL;
          for ( i = 0 ; i < strlen(rsent) ; i++ )
            { if ( *(rsent + i) == ';' || *(rsent + i) == ',' )
                                             { *(rsent + i) =  ' '; }
              if ( *(rsent + i) == '\n' ) { *(rsent + i) = NULL;
                                            break; }
            }
             
          strcpy(seeksnt,rsent);
          
          strcpy(ssent,"\n見出しだけを検索しますか？\n");
          strcat(ssent,"  ( Y なら見出しのみ検索   N なら本文も検索 ) ");
          rsout();
          
          *(ynsent) = NULL;
          flg = y_or_n();

          strcpy(ssent,"\n*** 検索を始めました。しばらくお待ち下さい ***\n\n");
          rsout();
          
          fpw = fopen(seek_hdr,"r");
          if ( fpw == NULL )
             {  fpw = fopen(seek_hdr,"w");
                if ( mail ) { fp2 = fopen(MAIL_HDR,"r"); }
                     else   { fp2 = fopen(HDR_FIL,"r");  }
                while(1)
                   {  if ( fgets(fbuff,255,fp2) == NULL ) break;
		      if ( ! isdigit(*fbuff) ) continue;
                      substr(fbuff,4,1,fflg);
                      substr(fbuff,5,8,fjunle);
                      substr(fbuff,47,6,fhiscall);
                      if ( mail )
                         {  if ( strncmp(fjunle,hiscall,6) &&
                                 strcmp(fhiscall,hiscall) ) continue;
                            if ( ! strcmp(fflg,"X") &&
                                 ! strncmp(fjunle,hiscall,6) ) continue;
                         }
                      else
                         {  if ( strcmp(nowjle,"PLAZA   ") )
                               {  if ( strcmp(nowjle,mainjle) &&
                                       strcmp(nowjle,fjunle) ) continue;
                                  if ( ! strcmp(fjunle,"PLAZA   ") )
                                                               continue;
                               }
                            else
                               { if ( strcmp(nowjle,fjunle) ) continue; }
                         }
                      if ( ! mail ) { *(fbuff + 75) = 0x0d;
				      *(fbuff + 76) = 0x0a;
                                      *(fbuff + 77) = NULL;
                                    }
                      fputs(fbuff,fpw);
                   }
                fclose(fp2);
            }
          fclose(fpw); 
	  strcpy(fname,msgpass); strcat(fname,MSG_FIL);


	  sprintf(param,"SEEK %s %s %1d %s %s",fname,temp_fil,flg,seek_hdr,seeksnt);
	  i =  system(param);

          if ( i == -1 )
             {  strcpy(ssent,"\n=== ｼｽﾃﾑ の都合により現在検索は実行できません ===\n\n");
                rsout();
                return(0);
             }
      
          fpw = fopen(seek_hdr,"r");
          if ( fgets(fbuff,255,fpw) == NULL )
                { fclose(fpw);
                  strcpy(ssent,"\n=== 文字列に一致したファイルは見つかりません ===\n");
                  rsout();
                  unlink(seek_hdr);
                  return(0); }
          else  { fclose(fpw); }
          strcpy(cmdopt,"A");
          *(cmdnum) = NULL;
          seek_flg = 1;
          exefind();
          seek_flg = 0;
          return(0);
        }
