Exemplo n.º 1
0
void math_command(wchar_t *line)
{
  wchar_t left[BUFFER_SIZE], right[BUFFER_SIZE], 
       temp[BUFFER_SIZE], result[BUFFER_SIZE];
  int i; 
  line=get_arg_in_braces(line,left,STOP_SPACES,sizeof(left)/sizeof(wchar_t)-1);
  line=get_arg_in_braces(line,right,WITH_SPACES,sizeof(right)/sizeof(wchar_t)-1);
  substitute_vars(right, result, sizeof(result)/sizeof(wchar_t));
  substitute_myvars(result, right, sizeof(right)/sizeof(wchar_t));
  i=eval_expression(right);
  swprintf(temp,L"%d",i);
  

  
    VAR_INDEX ind = VarList.find(left);
    VAR* pvar;
    if ( ind != VarList.end() ) {
        pvar = ind->second;
        pvar->m_strVal = temp;
    }
    else {
        pvar = new VAR(temp);
        VarList[left] = pvar;
    }
}
Exemplo n.º 2
0
void strcmp_command(wchar_t *arg) 
{
	wchar_t arg1[BUFFER_SIZE], 
		 arg2[BUFFER_SIZE], 
		 if_then[BUFFER_SIZE], 
		 if_else[BUFFER_SIZE],
		 temp[BUFFER_SIZE];

	arg = get_arg_in_braces(arg,arg1,STOP_SPACES,sizeof(arg1)/sizeof(wchar_t)-1);
	substitute_vars(arg1,temp, sizeof(temp)/sizeof(wchar_t));
	substitute_myvars(temp,arg1, sizeof(arg1)/sizeof(wchar_t));

	arg = get_arg_in_braces(arg,arg2,STOP_SPACES,sizeof(arg2)/sizeof(wchar_t)-1);
	substitute_vars(arg2,temp, sizeof(temp)/sizeof(wchar_t));
	substitute_myvars(temp,arg2, sizeof(arg2)/sizeof(wchar_t));
 
	arg = get_arg_in_braces(arg,if_then,WITH_SPACES,sizeof(if_then)/sizeof(wchar_t)-1);
    substitute_vars(if_then,temp, sizeof(temp)/sizeof(wchar_t));
	substitute_myvars(temp,if_then, sizeof(if_then)/sizeof(wchar_t));

	arg = get_arg_in_braces(arg,if_else,WITH_SPACES,sizeof(if_else)/sizeof(wchar_t)-1);
    substitute_vars(if_else,temp, sizeof(temp)/sizeof(wchar_t));
	substitute_myvars(temp,if_else, sizeof(if_else)/sizeof(wchar_t));

	wchar_t *to_parse = !wcscmp(arg1, arg2) ? if_then : if_else;
	if( to_parse && wcslen(to_parse) ) {
		parse_input(to_parse); 
	}
}
Exemplo n.º 3
0
void deathlog_command(const char *arg, struct session *ses)
{
    FILE *fh;
    char fname[BUFFER_SIZE], text[BUFFER_SIZE], temp[BUFFER_SIZE], lfname[BUFFER_SIZE];

    if (*arg)
    {
        arg = get_arg_in_braces(arg, temp, 0);
        arg = get_arg_in_braces(arg, text, 1);
        substitute_vars(temp, fname);
        substitute_myvars(fname, temp, ses);
        expand_filename(temp, fname, lfname);
        substitute_vars(text, temp);
        substitute_myvars(temp, text, ses);
        if ((fh = fopen(lfname, "a")))
        {
            cfprintf(fh, "%s\n", text);
            fclose(fh);
        }
        else
            tintin_eprintf(ses, "#ERROR: COULDN'T OPEN FILE {%s}.", fname);
    }
    else
        tintin_eprintf(ses, "#ERROR: valid syntax is: #deathlog <file> <text>");
}
Exemplo n.º 4
0
void alias_command(const char *arg, struct session *ses)
{
    char left[BUFFER_SIZE], right[BUFFER_SIZE], *ch;

    arg = get_arg_in_braces(arg, left, 0);
    arg = get_arg_in_braces(arg, right, 1);

    if (*left && *right)
    {
        if ((ch=strchr(left, ' ')))
        {
            tintin_eprintf(ses, "#ERROR: aliases cannot contain spaces! Bad alias: {%s}", left);
            if (ch==left)
                return;
            *ch=0;
            tintin_printf(ses, "#Converted offending alias to {%s}.", left);
        }
        set_hash(ses->aliases, left, right);
        if (ses->mesvar[MSG_ALIAS])
            tintin_printf(ses, "#Ok. {%s} aliases {%s}.", left, right);
        alnum++;
        return;
    }
    show_hashlist(ses, ses->aliases, left,
        "#Defined aliases:",
        "#No match(es) found for {%s}.");
}
Exemplo n.º 5
0
int check_all_aliases(struct session *ses, char *input) {
  struct listnode *node;
  struct listroot *root;
  char line[BUFFER_SIZE], tmp[BUFFER_SIZE], *arg;
  int i;

  root = ses->list[LIST_ALIAS];

  if (HAS_BIT(root->flags, LIST_FLAG_IGNORE)) {
    return FALSE;
  }

  for (i = 1; i < 100; i++) {
    if (*gtd->vars[i]) {
      RESTRING(gtd->vars[i], "");
    }
  }

  substitute(ses, input, line, SUB_VAR | SUB_FUN);

  for (root->update = 0; root->update < root->used; root->update++) {
    if (check_one_regexp(ses, root->list[root->update], line, line, PCRE_ANCHORED)) {
      node = root->list[root->update];

      i = strlen(node->left);

      if (!strncmp(node->left, line, i)) {
        if (line[i] && line[i] != ' ') {
          continue;
        }

        arg = get_arg_in_braces(ses, line, tmp, FALSE);

        RESTRING(gtd->vars[0], arg) for (i = 1; i < 100 && *arg; i++) {
          arg = get_arg_in_braces(ses, arg, tmp, FALSE);

          RESTRING(gtd->vars[i], tmp);
        }
      }

      substitute(ses, node->right, tmp, SUB_ARG);

      if (!strncmp(node->left, line, strlen(node->left))
          && !strcmp(node->right, tmp) && *gtd->vars[0]) {
        sprintf(input, "%s %s", tmp, gtd->vars[0]);
      } else {
        sprintf(input, "%s", tmp);
      }

      show_debug(ses, LIST_ALIAS, "#DEBUG ALIAS {%s} {%s}", node->left, gtd->vars[0]);

      return TRUE;
    }
  }
Exemplo n.º 6
0
void unsubstitute_command(char *arg)
{
  char left[BUFFER_SIZE] ,result[BUFFER_SIZE];
  struct listnode *mysubs, *ln;
  struct listnode TempPointerHolder;
  int flag;
  flag=FALSE;
  mysubs=common_subs;
  arg=get_arg_in_braces(arg,left,WITH_SPACES);
  TempPointerHolder.next = common_subs->next;
  while ((ln=search_node_with_wild(&TempPointerHolder, left))!=NULL) {
    if (mesvar[MSG_SUB]) {
      if (*(ln->right)=='.' && !*(ln->right+1))
        sprintf(result, rs::rs(1170), ln->left);
      else
        sprintf(result, rs::rs(1171), ln->left);
      tintin_puts2(result);
    }
    TempPointerHolder.next=ln->next;
    deletenode_list(mysubs, ln);
    subnum--;
    flag=TRUE;
    if ( TempPointerHolder.next == NULL ) 
        break;
  }
  if (!flag && mesvar[MSG_SUB])
    tintin_puts2(rs::rs(1172));
}
Exemplo n.º 7
0
Arquivo: Text.cpp Projeto: Liscar/jmc
void read_file(char *arg)
{
  FILE *myfile;
  char buffer[BUFFER_SIZE], *cptr;
  get_arg_in_braces(arg, arg, WITH_SPACES);

  if ( !arg || !*arg ) {
	  tintin_puts2(rs::rs(1173));
	  return;
  }


  if((myfile=fopen(arg, "r"))==NULL) {
    char buff[MAX_PATH];
    sprintf (buff, rs::rs(1174), arg);
    tintin_puts2(buff);
    return;
  }
  while(fgets(buffer, sizeof(buffer), myfile)) {
    for(cptr=buffer; *cptr && *cptr!='\n'; cptr++);
    *cptr='\0';
    write_line_mud(buffer);
  }
  fclose(myfile);
  tintin_puts2(rs::rs(1175));
}
Exemplo n.º 8
0
struct session *parse_tintin_command(struct session *ses, char *input)
{
	char line[BUFFER_SIZE];
	struct session *sesptr;

	input = get_arg_stop_spaces(ses, input, line, 0);

	substitute(ses, line, line, SUB_VAR|SUB_FUN);

	if (is_number(line))
	{
		int cnt = atoi(line);

		input = get_arg_in_braces(ses, input, line, TRUE);

		while (cnt-- > 0)
		{
			ses = script_driver(ses, -1, line);
		}
		return ses;
	}

	for (sesptr = gts ; sesptr ; sesptr = sesptr->next)
	{
		if (!strcmp(sesptr->name, line))
		{
			if (*input)
			{
				input = get_arg_in_braces(ses, input, line, TRUE);

				substitute(ses, line, line, SUB_VAR|SUB_FUN);

				script_driver(sesptr, -1, line);

				return ses;
			}
			else
			{
				return activate_session(sesptr);
			}
		}
	}

	tintin_printf(ses, "#ERROR: #UNKNOWN TINTIN-COMMAND '%s'.", line);

	return ses;
}
Exemplo n.º 9
0
void output_command(wchar_t* arg)
{ 
    wchar_t left[BUFFER_SIZE], right[BUFFER_SIZE];
    wchar_t result[BUFFER_SIZE], strng[BUFFER_SIZE];
    
    arg=get_arg_in_braces(arg,left,WITH_SPACES,sizeof(left)/sizeof(wchar_t)-1);
    arg=get_arg_in_braces(arg,right,WITH_SPACES,sizeof(right)/sizeof(wchar_t)-1);

    if ( !right[0] ) {  // no colors
        prepare_actionalias(left,strng, sizeof(strng)/sizeof(wchar_t)); 
    } else {
		prepare_actionalias(right,result, sizeof(result)/sizeof(wchar_t)); 
        add_codes(result, strng, left);
    }

    tintin_puts3(strng, 0);
}
Exemplo n.º 10
0
void unalias_command(const char *arg, struct session *ses)
{
    char left[BUFFER_SIZE];

    arg = get_arg_in_braces(arg, left, 1);
    delete_hashlist(ses, ses->aliases, left,
        ses->mesvar[MSG_ALIAS]? "#Ok. {%s} is no longer an alias." : 0,
        ses->mesvar[MSG_ALIAS]? "#No match(es) found for {%s}" : 0);
}
Exemplo n.º 11
0
char *sub_arg_in_braces(struct session *ses, char *string, char *result, int flag, int sub)
{
	char buffer[BUFFER_SIZE];

	string = get_arg_in_braces(ses, string, buffer, flag);

	substitute(ses, buffer, result, sub);

	return string;
}
Exemplo n.º 12
0
void if_command(wchar_t *line)
{
  wchar_t if_expr[BUFFER_SIZE], 
	   if_then[BUFFER_SIZE], 
       if_else[BUFFER_SIZE], 
       temp[BUFFER_SIZE];

  line=get_arg_in_braces(line,if_expr,STOP_SPACES,sizeof(if_expr)/sizeof(wchar_t)-1);
      substitute_vars(if_expr,temp, sizeof(temp)/sizeof(wchar_t));substitute_myvars(temp,if_expr, sizeof(if_expr)/sizeof(wchar_t));
  line=get_arg_in_braces(line,if_then,WITH_SPACES,sizeof(if_then)/sizeof(wchar_t)-1);
      substitute_vars(if_then,temp, sizeof(temp)/sizeof(wchar_t));substitute_myvars(temp,if_then, sizeof(if_then)/sizeof(wchar_t));
  line=get_arg_in_braces(line,if_else,WITH_SPACES,sizeof(if_else)/sizeof(wchar_t)-1);
      substitute_vars(if_else,temp, sizeof(temp)/sizeof(wchar_t));substitute_myvars(temp,if_else, sizeof(if_else)/sizeof(wchar_t));


	wchar_t *to_parse = eval_expression(if_expr) ? if_then : if_else;
	if( to_parse && wcslen(to_parse) ) {
		parse_input(to_parse); 
	}
}
Exemplo n.º 13
0
char *sub_arg_in_braces(struct session *ses, char *string, char *result, int flag, int sub)
{
	char *buffer = str_alloc(UMAX(strlen(string), BUFFER_SIZE));

	string = get_arg_in_braces(ses, string, buffer, flag);

	substitute(ses, buffer, result, sub);

	str_free(buffer);

	return string;
}
Exemplo n.º 14
0
void parse_sub(char *arg)
{
  char left[BUFFER_SIZE], right[BUFFER_SIZE], result[BUFFER_SIZE];
  struct listnode *mysubs, *ln;
  mysubs=common_subs;
  arg=get_arg_in_braces(arg, left,STOP_SPACES);
  arg=get_arg_in_braces(arg, right,WITH_SPACES);

  if(!*left) {
    tintin_puts2(rs::rs(1166));
    show_list(mysubs);
  }

  else if(*left && !*right) {
    if ((ln=search_node_with_wild(mysubs,left))!=NULL) {
      while((mysubs=search_node_with_wild(mysubs, left))!=NULL) {
        shownode_list(mysubs);
      }
    }
    else
      if (mesvar[MSG_SUB])
        tintin_puts2(rs::rs(1167));
  }

  else {
    if((ln=searchnode_list(mysubs, left))!=NULL) {
      deletenode_list(mysubs, ln);
      subnum--;
    }
    insertnode_list(mysubs, left, right, "0", ALPHA);
    subnum++;
    if (strcmp(right,".")!=0)
      sprintf(result, rs::rs(1168),right,left);
    else
      sprintf(result, rs::rs(1169), left);
    if (mesvar[MSG_SUB])
      tintin_puts2(result);
  }
}
Exemplo n.º 15
0
void unlink_command(const char *arg, struct session *ses)
{
    char file[BUFFER_SIZE], temp[BUFFER_SIZE], lstr[BUFFER_SIZE];

    if (*arg)
    {
        arg = get_arg_in_braces(arg, temp, 1);
        substitute_vars(temp, file);
        substitute_myvars(file, temp, ses);
        expand_filename(temp, file, lstr);
        unlink(lstr);
    }
    else
        tintin_eprintf(ses, "#ERROR: valid syntax is: #unlink <filename>");

}
Exemplo n.º 16
0
void systemkill_command(wchar_t *arg)
{
    DWORD pid = 0;
    wchar_t word[BUFFER_SIZE];
//* en
//* due to an error in tintin.h (messed up STOP_SPACES and WITH_SPACES)
//    get_arg_in_braces(arg,word,WITH_SPACES,sizeof(word)/sizeof(wchar_t)-1);
    get_arg_in_braces(arg,word,STOP_SPACES,sizeof(word)/sizeof(wchar_t)-1);
//* /en
    if ( !*word ) {
systemkill_error:
        tintin_puts2(rs::rs(1225));
        return;
    }

    if (!wcscmp(word, L"all")) {
        // pid = 0;
    } else if (!wcscmp(word, L"last")) {
        // pid = PsList.size() > 0 ? (*(PsList.begin()))->m_dwPID : 0;
        if (PsList.size() > 0) pid = (*(PsList.begin()))->m_dwPID;
    } else {
        pid = _wtoi(word);
        if (pid == 0) goto systemkill_error;
    }

    BOOL bFound = FALSE;

    EnterCriticalSection(&secSystemList);
    PS_INDEX ind = PsList.begin();
    while (ind != PsList.end()) {
        PS_ITEM* psi = *ind;
        if (pid == 0 || psi->m_dwPID == pid) {
            bFound = TRUE;
            HANDLE hp = OpenProcess(PROCESS_TERMINATE, FALSE, psi->m_dwPID);
            if (hp) TerminateProcess(hp, 0);
            delete psi;
            ind = PsList.erase(ind);
        } else {
            ind++;
        }
    }
    LeaveCriticalSection(&secSystemList);
    if (!bFound && pid != 0) {
        tintin_puts2(rs::rs(1228));
    }
}
Exemplo n.º 17
0
void unantisubstitute_command(const char *arg, struct session *ses)
{
    char left[BUFFER_SIZE];
    struct listnode *myantisubs, *ln, *temp;
    bool flag = false;

    myantisubs = ses->antisubs;
    temp = myantisubs;
    arg = get_arg_in_braces(arg, left, 1);
    while ((ln = search_node_with_wild(temp, left)) != NULL)
    {
        if (ses->mesvar[MSG_SUBSTITUTE])
            tintin_printf(ses, "#Ok. Lines with {%s} will now be subbed.", ln->left);
        deletenode_list(myantisubs, ln);
        flag = true;
    }
    if (!flag && ses->mesvar[MSG_SUBSTITUTE])
        tintin_printf(ses, "#THAT ANTISUBSTITUTE (%s) IS NOT DEFINED.", left);
}
Exemplo n.º 18
0
Arquivo: Parse.cpp Projeto: Liscar/jmc
bool parse_tintin_command(char *command, char *arg)
{
  /*
   this lines almost totally rewrote to put all command functions
   into a struct array. Size of array defined in tintin.h;
   Array itself defined in cmds.h.
   Struct:
    char alias    - with it you can easily add aliases with same function name
	void function_name - main stuff
	char hlpfile  - this field is for help_command. do not forget to link 
	                aliases to one help file.
	would rely it would not crash... En.
  */
  CharLower(command);

  if(isdigit(*command)) {
	  int a1=0;
	  int a2=0;
  	  sscanf(command,"%d:%d",&a1,&a2);
	  get_arg_in_braces(arg, arg, WITH_SPACES);

    do_cycle(      1,     a1,    1,         a2,  arg    );
 /* do_cycle( bound1, bound2, step,      delay,  command); */
    return false;
  }

  if(*command == cCommandChar) return false;

  for(int i=0;i<JMC_CMDS_NUM;i++)
	  if((command[0]==jmc_cmds[i].alias[0])
		  &&(is_abrev(command,jmc_cmds[i].alias)))
	  {
		  (*(jmc_cmds[i].jmcfn))(arg);
		  return true;
	  }
  
  tintin_puts2(rs::rs(1145));
  return false;
}
Exemplo n.º 19
0
void antisubstitute_command(const char *arg, struct session *ses)
{
    char left[BUFFER_SIZE];
    struct listnode *myantisubs, *ln;

    myantisubs = ses->antisubs;
    arg = get_arg_in_braces(arg, left, 1);

    if (!*left)
    {
        tintin_puts("#THESE ANTISUBSTITUTES HAS BEEN DEFINED:", ses);
        show_list(myantisubs);
    }
    else
    {
        if ((ln = searchnode_list(myantisubs, left)) != NULL)
            deletenode_list(myantisubs, ln);
        insertnode_list(myantisubs, left, left, 0, ALPHA);
        antisubnum++;
        if (ses->mesvar[MSG_SUBSTITUTE])
            tintin_printf(ses, "Ok. Any line with {%s} will not be subbed.", left);
    }
}
Exemplo n.º 20
0
Arquivo: Text.cpp Projeto: Liscar/jmc
//* en
void spit_command(char *arg)
{
  char 
      filename[BUFFER_SIZE],
      spformat[BUFFER_SIZE],
      soptions[BUFFER_SIZE],
        buffer[BUFFER_SIZE],
       *cptr1/*,*cptr2,*cptr3*/;
  FILE 
	  *myfile;
  BOOL
      bPassSpace = FALSE;  
  BOOL
	  bSilent    = FALSE;

  
  arg=get_arg_in_braces(arg, filename, STOP_SPACES);
  arg=get_arg_in_braces(arg, spformat, WITH_SPACES);
  arg=get_arg_in_braces(arg, soptions, STOP_SPACES);

  if ( !filename || !*filename ) {
	  tintin_puts2(rs::rs(1173));
	  return;
  }

  if ( !spformat || !*spformat ) {
	  strcpy(spformat,"%0");
  }
  BOOL hasD = FALSE;
  for(cptr1=spformat;*cptr1&&*cptr1!='\0';cptr1++)
    if(*cptr1=='%'&&*(cptr1+1)=='0')
	  hasD = TRUE;
  if(!hasD) 
	  strcpy(cptr1," %0\0");
  
  for(cptr1=soptions;*cptr1&&*cptr1!='\0';cptr1++)
  {
    if(*cptr1=='n')
	  bPassSpace = TRUE;
	if(*cptr1=='s')
		bSilent = TRUE;
  }	 

  if((myfile=fopen(filename, "r"))==NULL) {
    char buff[MAX_PATH];
    sprintf(buff, rs::rs(1174), filename);
    tintin_puts2(buff);
    return;
  }
  while(fgets(buffer, sizeof(buffer), myfile)) 
  {
	char cbuffer[BUFFER_SIZE];
    
    if((*buffer=='\n'||strlen(buffer)<2)&&bPassSpace)
	  continue;

/*
    for(cptr1=spformat,cptr3=cbuffer; *cptr1 && *cptr1!='\0'; cptr1++,cptr3++)
      if(*cptr1=='@')
      {
		 for(cptr2=buffer;*cptr2&&*cptr2!='\0'&&*cptr2!='\n';cptr2++,cptr3++)
			 *cptr3=*cptr2;
		 cptr3--;
	  }
	  else
        *cptr3=*cptr1;
	*cptr3='\0';
*/
		    for(cptr1=buffer;*cptr1&&*cptr1!='\n';cptr1++);
			*cptr1='\0';
            sprintf(vars[0], "%s", buffer);
            substitute_vars(spformat,cbuffer);
    parse_input(cbuffer);
  }
  fclose(myfile);
  if(!bSilent)
	  tintin_puts2(rs::rs(1175));
}
Exemplo n.º 21
0
pcre *tintin_regexp_compile(struct session * ses, struct listnode * node, char *exp, int option) {
  char out[BUFFER_SIZE], *pti, *pto;

  pti = exp;
  pto = out;

  if (*pti == '~') {
    pti++;
    SET_BIT(node->flags, NODE_FLAG_META);
  }

  while (*pti == '^') {
    *pto++ = *pti++;
  }

  while (*pti) {
    if (HAS_BIT(ses->flags, SES_FLAG_BIG5) && *pti & 128 && pti[1] != 0) {
      *pto++ = *pti++;
      *pto++ = *pti++;
      continue;
    }
    switch (pti[0]) {
      case '\\':
        *pto++ = *pti++;
        *pto++ = *pti++;
        break;

      case '{':
        *pto++ = '(';
        pti = get_arg_in_braces(ses, pti, pto, TRUE);
        while (*pto) {
          if (pto[0] == '$' || pto[0] == '@') {
            if (pto[1]) {
              return NULL;
            }
          }
          pto++;
        }
        *pto++ = ')';
        break;

      case '&':
        if (pti[1] == DEFAULT_OPEN || isalnum((int)pti[1])
            || pti[1] == '&') {
          return NULL;
        }
        *pto++ = *pti++;
        break;

      case '@':
        if (pti[1] == DEFAULT_OPEN || isalnum((int)pti[1])
            || pti[1] == '@') {
          return NULL;
        }
        *pto++ = *pti++;
        break;

      case '$':
        if (pti[1] == DEFAULT_OPEN || isalnum((int)pti[1])) {
          return NULL;
        }
        {
          int i = 0;

          while (pti[++i] == '$') {
            continue;
          }

          if (pti[i]) {
            *pto++ = '\\';
          }
        }
        *pto++ = *pti++;
        break;

      case '[':
      case ']':
      case '(':
      case ')':
      case '|':
      case '.':
      case '?':
      case '+':
      case '*':
      case '^':
        *pto++ = '\\';
        *pto++ = *pti++;
        break;

      case '%':
        switch (pti[1]) {
          case '0':
          case '1':
          case '2':
          case '3':
          case '4':
          case '5':
          case '6':
          case '7':
          case '8':
          case '9':
            pti += isdigit((int)pti[2]) ? 3 : 2;
            strcpy(pto, *pti == 0 ? "(.*)" : "(.*?)");
            pto += strlen(pto);
            break;

          case 'd':
            pti += 2;
            strcpy(pto, *pti == 0 ? "([0-9]*)" : "([0-9]*?)");
            pto += strlen(pto);
            break;

          case 'D':
            pti += 2;
            strcpy(pto, *pti == 0 ? "([^0-9]*)" : "([^0-9]*?)");
            pto += strlen(pto);
            break;

          case 'i':
            pti += 2;
            strcpy(pto, "(?i)");
            pto += strlen(pto);
            break;

          case 'I':
            pti += 2;
            strcpy(pto, "(?-i)");
            pto += strlen(pto);
            break;

          case 's':
            pti += 2;
            strcpy(pto, *pti == 0 ? "(\\s*)" : "(\\s*?)");
            pto += strlen(pto);
            break;

          case 'S':
            pti += 2;
            strcpy(pto, *pti == 0 ? "(\\S*)" : "(\\S*?)");
            pto += strlen(pto);
            break;

          case 'w':
            pti += 2;
            strcpy(pto, *pti == 0 ? "([a-zA-Z]*)" : "([a-zA-Z]*?)");
            pto += strlen(pto);
            break;

          case 'W':
            pti += 2;
            strcpy(pto, *pti == 0 ? "([^a-zA-Z]*)" : "([^a-zA-Z]*?)");
            pto += strlen(pto);
            break;

          case '?':
            pti += 2;
            strcpy(pto, *pti == 0 ? "(.?)" : "(.?" "?)");
            pto += strlen(pto);
            break;

          case '*':
            pti += 2;
            strcpy(pto, *pti == 0 ? "(.*)" : "(.*?)");
            pto += strlen(pto);
            break;

          case '+':
            pti += 2;
            strcpy(pto, *pti == 0 ? "(.+)" : "(.+?)");
            pto += strlen(pto);
            break;

          case '.':
            pti += 2;
            strcpy(pto, "(.)");
            pto += strlen(pto);
            break;

          case '%':
            *pto++ = *pti++;
            pti++;
            break;

          default:
            *pto++ = *pti++;
            break;
        }
        break;

      default:
        *pto++ = *pti++;
        break;
    }
  }
  *pto = 0;

  return regexp_compile(out, option);
}
Exemplo n.º 22
0
int tintin_regexp(struct session *ses, pcre * nodepcre, char *str, char *exp, int option, int flag) {
  char out[BUFFER_SIZE], *pti, *pto;
  int arg = 1, var = 1, fix = 0;

  pti = exp;
  pto = out;

  while (*pti == '^') {
    *pto++ = *pti++;
  }

  while (*pti) {
    if (HAS_BIT(ses->flags, SES_FLAG_BIG5) && *pti & 128 && pti[1] != 0) {
      *pto++ = *pti++;
      *pto++ = *pti++;
      continue;
    }

    switch (pti[0]) {
      case '\\':
        *pto++ = *pti++;
        *pto++ = *pti++;
        break;

      case '{':
        gtd->args[up(var)] = up(arg);
        *pto++ = '(';
        pti = get_arg_in_braces(ses, pti, pto, TRUE);
        pto += strlen(pto);
        *pto++ = ')';
        break;

      case '[':
      case ']':
      case '(':
      case ')':
      case '|':
      case '.':
      case '?':
      case '+':
      case '*':
      case '^':
        *pto++ = '\\';
        *pto++ = *pti++;
        break;

      case '$':
        if (pti[1] != DEFAULT_OPEN && !isalnum((int)pti[1])) {
          int i = 0;

          while (pti[++i] == '$') {
            continue;
          }

          if (pti[i]) {
            *pto++ = '\\';
          }
        }
        *pto++ = *pti++;
        break;

      case '%':
        switch (pti[1]) {
          case '0':
          case '1':
          case '2':
          case '3':
          case '4':
          case '5':
          case '6':
          case '7':
          case '8':
          case '9':
            fix = SUB_FIX;
            arg = isdigit((int)pti[2]) ? (pti[1] - '0') * 10 + (pti[2] - '0') : pti[1]
             - '0';
            gtd->args[up(var)] = up(arg);
            pti += isdigit((int)pti[2]) ? 3 : 2;
            strcpy(pto, *pti == 0 ? "(.*)" : "(.*?)");
            pto += strlen(pto);
            break;

          case 'd':
            gtd->args[up(var)] = up(arg);
            pti += 2;
            strcpy(pto, *pti == 0 ? "([0-9]*)" : "([0-9]*?)");
            pto += strlen(pto);
            break;

          case 'D':
            gtd->args[up(var)] = up(arg);
            pti += 2;
            strcpy(pto, *pti == 0 ? "([^0-9]*)" : "([^0-9]*?)");
            pto += strlen(pto);
            break;

          case 'i':
            pti += 2;
            strcpy(pto, "(?i)");
            pto += strlen(pto);
            break;

          case 'I':
            pti += 2;
            strcpy(pto, "(?-i)");
            pto += strlen(pto);
            break;

          case 's':
            gtd->args[up(var)] = up(arg);
            pti += 2;
            strcpy(pto, *pti == 0 ? "(\\s*)" : "(\\s*?)");
            pto += strlen(pto);
            break;

          case 'S':
            gtd->args[up(var)] = up(arg);
            pti += 2;
            strcpy(pto, *pti == 0 ? "(\\S*)" : "(\\S*?)");
            pto += strlen(pto);
            break;

          case 'w':
            gtd->args[up(var)] = up(arg);
            pti += 2;
            strcpy(pto, *pti == 0 ? "([a-zA-Z]*)" : "([a-zA-Z]*?)");
            pto += strlen(pto);
            break;

          case 'W':
            gtd->args[up(var)] = up(arg);
            pti += 2;
            strcpy(pto, *pti == 0 ? "([^a-zA-Z]*)" : "([^a-zA-Z]*?)");
            pto += strlen(pto);
            break;

          case '?':
            gtd->args[up(var)] = up(arg);
            pti += 2;
            strcpy(pto, *pti == 0 ? "(.?)" : "(.?" "?)");
            pto += strlen(pto);
            break;

          case '*':
            gtd->args[up(var)] = up(arg);
            pti += 2;
            strcpy(pto, *pti == 0 ? "(.*)" : "(.*?)");
            pto += strlen(pto);
            break;

          case '+':
            gtd->args[up(var)] = up(arg);
            pti += 2;
            strcpy(pto, *pti == 0 ? "(.+)" : "(.+?)");
            pto += strlen(pto);
            break;

          case '.':
            gtd->args[up(var)] = up(arg);
            pti += 2;
            strcpy(pto, "(.)");
            pto += strlen(pto);
            break;

          case '%':
            *pto++ = *pti++;
            pti++;
            break;

          default:
            *pto++ = *pti++;
            break;
        }
        break;

      default:
        *pto++ = *pti++;
        break;
    }
  }
  *pto = 0;

  return regexp_compare(nodepcre, str, out, option, flag + fix);
}
Exemplo n.º 23
0
int substitute(struct session *ses, char *string, char *result, int flags) {
  struct listnode *node;
  char temp[BUFFER_SIZE], buf[BUFFER_SIZE], buffer[BUFFER_SIZE], *pti, *pto, *ptt;
  char *pte, old[6] = { 0 };
  int i, cnt, escape = FALSE, flags_neol = flags;

  push_call("substitute(%p,%p,%p,%d)", ses, string, result, flags);

  pti = string;
  pto = (string == result) ? buffer : result;

  DEL_BIT(flags_neol, SUB_EOL | SUB_LNF);

  while (TRUE) {
    if (HAS_BIT(ses->flags, SES_FLAG_BIG5) && *pti & 128 && pti[1] != 0) {
      *pto++ = *pti++;
      *pto++ = *pti++;
      continue;
    }

    switch (*pti) {
      case '\0':
        if (HAS_BIT(flags, SUB_EOL)) {
          if (HAS_BIT(ses->flags, SES_FLAG_RUN)) {
            *pto++ = '\r';
          } else {
            *pto++ = '\r';
            *pto++ = '\n';
          }
        }

        if (HAS_BIT(flags, SUB_LNF)) {
          *pto++ = '\n';
        }

        *pto = 0;

        pop_call();

        if (string == result) {
          strcpy(result, buffer);

          return pto - buffer;
        } else {
          return pto - result;
        }
        break;

      case '$':
        if (HAS_BIT(flags, SUB_VAR)
            && (pti[1] == DEFAULT_OPEN || isalpha((int)pti[1])
                || pti[1] == '$')) {
          int def = FALSE;

          if (pti[1] == '$') {
            while (pti[1] == '$') {
              *pto++ = *pti++;
            }

            if (pti[1] == DEFAULT_OPEN || isalnum((int)pti[1])) {
              pti++;
            } else {
              *pto++ = *pti++;
            }
            continue;
          }

          pti++;

          if (*pti == DEFAULT_OPEN) {
            def = TRUE;

            pti = get_arg_in_braces(ses, pti, buf, TRUE);

            substitute(ses, buf, temp, flags_neol);
          } else {
            ptt = temp;

            while (isalnum((int)*pti) || *pti == '_') {
              *ptt++ = *pti++;
            }
            *ptt = 0;
          }

          pti = get_arg_at_brackets(ses, pti, temp + strlen(temp));

          substitute(ses, temp, buf, flags_neol);

          get_nest_node(ses->list[LIST_VARIABLE], buf, temp, def);

          substitute(ses, temp, pto, flags_neol - SUB_VAR);

          pto += strlen(pto);
        } else {
          *pto++ = *pti++;
        }
        break;

      case '<':
        if (HAS_BIT(flags, SUB_COL)) {
          if (HAS_BIT(flags, SUB_CMP) && !strncmp(old, pti, 5)) {
            pti += 5;
          } else if (isdigit((int)pti[1]) && isdigit((int)pti[2])
                     && isdigit((int)pti[3]) && pti[4] == '>') {
            if (pti[1] != '8' || pti[2] != '8' || pti[3] != '8') {
              *pto++ = ESCAPE;
              *pto++ = '[';

              switch (pti[1]) {
                case '2':
                  *pto++ = '2';
                  *pto++ = '2';
                  *pto++ = ';';
                  break;
                case '8':
                  break;
                default:
                  *pto++ = pti[1];
                  *pto++ = ';';
              }
              switch (pti[2]) {
                case '8':
                  break;
                default:
                  *pto++ = '3';
                  *pto++ = pti[2];
                  *pto++ = ';';
                  break;
              }
              switch (pti[3]) {
                case '8':
                  break;
                default:
                  *pto++ = '4';
                  *pto++ = pti[3];
                  *pto++ = ';';
                  break;
              }
              pto--;
              *pto++ = 'm';
            }
            pti += sprintf(old, "<%c%c%c>", pti[1], pti[2], pti[3]);
          } else if (pti[1] >= 'a' && pti[1] <= 'f' && pti[2] >= 'a' && pti[2] <= 'f' && pti[3] >= 'a' && pti[3] <= 'f' && pti[4] == '>') {
            *pto++ = ESCAPE;
            *pto++ = '[';
            *pto++ = '3';
            *pto++ = '8';
            *pto++ = ';';
            *pto++ = '5';
            *pto++ = ';';
            cnt = 16 + (pti[1] - 'a') * 36 + (pti[2] - 'a') * 6 + (pti[3] - 'a');
            *pto++ = '0' + cnt / 100;
            *pto++ = '0' + cnt % 100 / 10;
            *pto++ = '0' + cnt % 10;
            *pto++ = 'm';
            pti += sprintf(old, "<%c%c%c>", pti[1], pti[2], pti[3]);
          } else if (pti[1] >= 'A' && pti[1] <= 'F' && pti[2] >= 'A' && pti[2] <= 'F' && pti[3] >= 'A' && pti[3] <= 'F' && pti[4] == '>') {
            *pto++ = ESCAPE;
            *pto++ = '[';
            *pto++ = '4';
            *pto++ = '8';
            *pto++ = ';';
            *pto++ = '5';
            *pto++ = ';';
            cnt = 16 + (pti[1] - 'A') * 36 + (pti[2] - 'A') * 6 + (pti[3] - 'A');
            *pto++ = '0' + cnt / 100;
            *pto++ = '0' + cnt % 100 / 10;
            *pto++ = '0' + cnt % 10;
            *pto++ = 'm';
            pti += sprintf(old, "<%c%c%c>", pti[1], pti[2], pti[3]);
          } else if (pti[1] == 'g' && isdigit((int)pti[2])
                     && isdigit((int)pti[3]) && pti[4] == '>') {
            *pto++ = ESCAPE;
            *pto++ = '[';
            *pto++ = '3';
            *pto++ = '8';
            *pto++ = ';';
            *pto++ = '5';
            *pto++ = ';';
            cnt = 232 + (pti[2] - '0') * 10 + (pti[3] - '0');
            *pto++ = '0' + cnt / 100;
            *pto++ = '0' + cnt % 100 / 10;
            *pto++ = '0' + cnt % 10;
            *pto++ = 'm';
            pti += sprintf(old, "<%c%c%c>", pti[1], pti[2], pti[3]);
          } else if (pti[1] == 'G' && isdigit((int)pti[2])
                     && isdigit((int)pti[3]) && pti[4] == '>') {
            *pto++ = ESCAPE;
            *pto++ = '[';
            *pto++ = '4';
            *pto++ = '8';
            *pto++ = ';';
            *pto++ = '5';
            *pto++ = ';';
            cnt = 232 + (pti[2] - '0') * 10 + (pti[3] - '0');
            *pto++ = '0' + cnt / 100;
            *pto++ = '0' + cnt % 100 / 10;
            *pto++ = '0' + cnt % 10;
            *pto++ = 'm';
            pti += sprintf(old, "<%c%c%c>", pti[1], pti[2], pti[3]);
          } else {
            *pto++ = *pti++;
          }
        } else {
          *pto++ = *pti++;
        }
        break;

      case '@':
        if (HAS_BIT(flags, SUB_FUN)) {
          if (pti[1] == '@') {
            escape = TRUE;
            *pto++ = *pti++;

            continue;
          }

          for (ptt = temp, i = 1; isalnum((int)pti[i]) || pti[i] == '_'; i++) {
            *ptt++ = pti[i];
          }
          *ptt = 0;

          node = search_node_list(ses->list[LIST_FUNCTION], temp);

          if (node == NULL || pti[i] != DEFAULT_OPEN) {
            escape = FALSE;
            *pto++ = *pti++;
            continue;
          }

          if (escape) {
            pti++;
            continue;
          }

          pti = get_arg_in_braces(ses, &pti[i], temp, FALSE);

          substitute(ses, temp, buf, flags_neol);

          show_debug(ses, LIST_FUNCTION, "#DEBUG FUNCTION {%s}", node->left);

          RESTRING(gtd->vars[0], buf);

          pte = buf;

          for (i = 1; i < 100; i++) {
            pte = get_arg_in_braces(ses, pte, temp, TRUE);

            RESTRING(gtd->vars[i], temp);

            if (*pte == 0) {
              break;
            }

            if (*pte == COMMAND_SEPARATOR) {
              pte++;
            }

          }

          substitute(ses, node->right, buf, SUB_ARG);

          script_driver(ses, LIST_FUNCTION, buf);

          substitute(ses, "$result", pto, flags_neol | SUB_VAR);

          pto += strlen(pto);
        } else {
          *pto++ = *pti++;
        }
        break;

      case '%':
        if (HAS_BIT(flags, SUB_ARG)
            && (isdigit((int)pti[1]) || pti[1] == '%')) {
          if (pti[1] == '%') {
            while (pti[1] == '%') {
              *pto++ = *pti++;
            }
            pti++;
          } else {
            i = isdigit((int)pti[2]) ? (pti[1] - '0') * 10 + pti[2] - '0' : pti[1] - '0';

            ptt = gtd->vars[i];

            while (*ptt) {
              if (HAS_BIT(ses->flags, SES_FLAG_BIG5) && *ptt & 128 && ptt[1] != 0) {
                *pto++ = *ptt++;
                *pto++ = *ptt++;
                continue;
              }

              if (HAS_BIT(flags, SUB_SEC)) {
                switch (*ptt) {
                  case '\\':
                    *pto++ = '\\';
                    *pto++ = '\\';
                    break;

                  case '{':
                    *pto++ = '\\';
                    *pto++ = 'x';
                    *pto++ = '7';
                    *pto++ = 'B';
                    break;

                  case '}':
                    *pto++ = '\\';
                    *pto++ = 'x';
                    *pto++ = '7';
                    *pto++ = 'D';
                    break;

                  case COMMAND_SEPARATOR:
                    *pto++ = '\\';
                    *pto++ = COMMAND_SEPARATOR;
                    break;

                  default:
                    *pto++ = *ptt;
                    break;
                }
                ptt++;
              } else {
                *pto++ = *ptt++;
              }
            }
            pti += isdigit((int)pti[2]) ? 3 : 2;
          }
        } else {
          *pto++ = *pti++;
        }
        break;

      case '&':
        if (HAS_BIT(flags, SUB_CMD)
            && (isdigit((int)pti[1]) || pti[1] == '&')) {
          if (pti[1] == '&') {
            while (pti[1] == '&') {
              *pto++ = *pti++;
            }
            if (isdigit((int)pti[1])) {
              pti++;
            } else {
              *pto++ = *pti++;
            }
          } else {
            i = isdigit((int)pti[2]) ? (pti[1] - '0') * 10 + pti[2] - '0' : pti[1] - '0';

            for (cnt = 0; gtd->cmds[i][cnt]; cnt++) {
              *pto++ = gtd->cmds[i][cnt];
            }
            pti += isdigit((int)pti[2]) ? 3 : 2;
          }
        } else if (HAS_BIT(flags, SUB_VAR)
                   && (pti[1] == DEFAULT_OPEN || isalpha((int)pti[1])
                       || pti[1] == '&')) {
          int def = 0;

          if (pti[1] == '&') {
            while (pti[1] == '&') {
              *pto++ = *pti++;
            }

            if (pti[1] == DEFAULT_OPEN || isalnum((int)pti[1])) {
              pti++;
            } else {
              *pto++ = *pti++;
            }
            continue;
          }

          pti++;

          if (*pti == DEFAULT_OPEN) {
            def = TRUE;

            pti = get_arg_in_braces(ses, pti, buf, TRUE);

            substitute(ses, buf, temp, flags_neol);
          } else {
            for (ptt = temp; isalnum((int)*pti) || *pti == '_'; i++) {
              *ptt++ = *pti++;
            }
            *ptt = 0;
          }

          pti = get_arg_at_brackets(ses, pti, temp + strlen(temp));

          substitute(ses, temp, buf, flags_neol);

          get_nest_index(ses->list[LIST_VARIABLE], buf, temp, def);

          substitute(ses, temp, pto, flags_neol - SUB_VAR);

          pto += strlen(pto);
        } else {
          *pto++ = *pti++;
        }
        break;

      case '\\':
        if (HAS_BIT(flags, SUB_ESC)) {
          pti++;

          switch (*pti) {
            case 'a':
              *pto++ = '\a';
              break;
            case 'b':
              *pto++ = '\b';
              break;
            case 'c':
              if (pti[1]) {
                pti++;
                *pto++ = *pti % 32;
              }
              break;
            case 'e':
              *pto++ = '\033';
              break;
            case 'n':
              *pto++ = '\n';
              break;
            case 'r':
              *pto++ = '\r';
              break;
            case 't':
              *pto++ = '\t';
              break;
            case 'x':
              if (pti[1] && pti[2]) {
                pti++;
                *pto++ = hex_number(pti);
                pti++;
              }
              break;
            case '0':
              if (pti[1] && pti[2]) {
                pti++;
                *pto++ = oct_number(pti);
                pti++;
              }
              break;
            case '\0':
              DEL_BIT(flags, SUB_EOL);
              DEL_BIT(flags, SUB_LNF);
              continue;
            default:
              *pto++ = *pti;
              break;
          }
          pti++;
        } else {
          *pto++ = *pti++;
        }
        break;

      case ESCAPE:
        *pto++ = *pti++;
        break;

      default:
        if (HAS_BIT(flags, SUB_SEC) && !HAS_BIT(flags, SUB_ARG)) {
          switch (*pti) {
            case '\\':
              *pto++ = '\\';
              *pto++ = '\\';
              break;

            case '{':
              *pto++ = '\\';
              *pto++ = 'x';
              *pto++ = '7';
              *pto++ = 'B';
              break;

            case '}':
              *pto++ = '\\';
              *pto++ = 'x';
              *pto++ = '7';
              *pto++ = 'D';
              break;

            case COMMAND_SEPARATOR:
              *pto++ = '\\';
              *pto++ = COMMAND_SEPARATOR;
              break;

            default:
              *pto++ = *pti;
              break;
          }
          pti++;
        } else {
          *pto++ = *pti++;
        }
        break;
    }
  }
}
Exemplo n.º 24
0
void match_command(wchar_t *arg) 
{
	wchar_t pattern[BUFFER_SIZE], 
		 strng[BUFFER_SIZE], 
		 if_then[BUFFER_SIZE], 
		 if_else[BUFFER_SIZE],
		 temp[BUFFER_SIZE];
	
	CPCRE re;

	arg = get_arg_in_braces(arg,temp,STOP_SPACES,sizeof(temp)/sizeof(wchar_t)-1);
	prepare_actionalias(temp, pattern, sizeof(pattern)/sizeof(wchar_t)-1);

    BOOL i_flag = FALSE, m_flag = FALSE, g_flag = FALSE;
	std::wstring regexp = pattern;

    if ( *pattern == L'/' ) {
		regexp = (wchar_t*)pattern + 1;
        
        int size = regexp.size();
		
		for (int i = size - 1; i >= 0; i--) {
			if (regexp[i] == L'i') {
				size--;
				i_flag = TRUE;
			} else if (regexp[i] == L'm') {
				size--;
				m_flag = TRUE;
			} else if (regexp[i] == L'g') {
				size--;
				g_flag = TRUE;
			} else if (regexp[i] == L'/') {
				size--;
				break;
			} else {
				size = regexp.size();
				i_flag = m_flag = g_flag = FALSE;
				break;
			}
		}
		regexp.resize(size);
    }

	if (!re.SetSource(regexp, m_flag, i_flag)) {
		return;
	}

	arg = get_arg_in_braces(arg,temp,STOP_SPACES,sizeof(temp)/sizeof(wchar_t)-1);
	prepare_actionalias(temp, strng, sizeof(strng)/sizeof(wchar_t)-1);
 
	arg = get_arg_in_braces(arg,if_then,WITH_SPACES,sizeof(if_then)/sizeof(wchar_t)-1);

	arg = get_arg_in_braces(arg,if_else,WITH_SPACES,sizeof(if_else)/sizeof(wchar_t)-1);

	int offset = 0;
    int offsets[33];

	bool no_match = true;
	wchar_t *test = (wchar_t*)strng;
	int test_len = wcslen(test);

	for (;;) {
		int captured = pcre16_exec(re.m_pPcre, re.m_pExtra, test, test_len, offset, 0, offsets, 33);
		if (captured <= 0)
			break;

		no_match = false;
		
		int i;
		for (i = 0; i < 10; i++) 
			vars[i][0] = 0;
		for (i = 1; i < captured; i++) {
			if (offsets[i*2] >= 0) {
				int size = offsets[i*2 + 1] - offsets[i*2];
				wcsncpy(vars[i-1], test + offsets[i*2], size);
				*(vars[i-1]+size) = L'\0'; 
			}
		}
		prepare_actionalias(if_then, temp, sizeof(temp)/sizeof(wchar_t)-1);
		parse_input(temp, g_flag);

		offset = offsets[1];

		if (!g_flag)
			break;
	}

	if (no_match)
		parse_input(if_else);
}
Exemplo n.º 25
0
Arquivo: Text.cpp Projeto: Liscar/jmc
void grab_command(char *arg)
{
  char 
      arg1[BUFFER_SIZE],
      arg2[BUFFER_SIZE],
      arg3[BUFFER_SIZE],
    buffer[BUFFER_SIZE],
    result[BUFFER_SIZE],
   *cptr;
  FILE 
	  *myfile;
  int needLine = 0;

  BOOL wn = FALSE;
  
  arg=get_arg_in_braces(arg, arg1, STOP_SPACES);
  arg=get_arg_in_braces(arg, arg2, WITH_SPACES);
  arg=get_arg_in_braces(arg, arg3, WITH_SPACES);

  if(*arg1 && isdigit(*arg1))
  {
	  wn = TRUE;
	  needLine = atoi(arg1);
  }
  else if(!strcmpi(arg1,lastGrab))
	  needLine = lastLine+1;
  else
  {
	  needLine = 1;
      maxLines = count_lines(arg1);
  }

  if (((!wn)&&(!arg1||!*arg1))||((wn)&&(!arg2||!*arg2))) 
  {
	  tintin_puts2(rs::rs(1173));
	  return;
  }

  if (((!wn)&&(!arg2||!*arg2))||((wn)&&(!arg3||!*arg3))) 
  {
	  strcpy(wn?arg3:arg2,"%0\0");
  }
  BOOL hasD = FALSE;
  cptr=wn?arg3:arg2;
  for(;*cptr&&*cptr!='\0';cptr++)
    if(*cptr=='%'&&*(cptr+1)=='0')
	  hasD = TRUE;
  if(!hasD) 
	  strcpy(cptr," %0\0");

  if((myfile=fopen(wn?arg2:arg1, "r"))==NULL) {
    char buff[MAX_PATH];
    sprintf(buff, rs::rs(1174), wn?arg2:arg1);
    tintin_puts2(buff);
    return;
  }
  int currLine = 0;
  while(fgets(buffer, sizeof(buffer), myfile)) 
  {
    currLine++;
	if(currLine == needLine)
	{
		    for(cptr=buffer;*cptr&&*cptr!='\n';cptr++);
			*cptr='\0';
            sprintf(vars[0], "%s", buffer);
            substitute_vars(wn?arg3:arg2,result);
			parse_input(result);
			break;
	}
  }
  lastLine = (needLine != maxLines)?needLine:0;
  strcpy(lastGrab,wn?arg2:arg1);
  fclose(myfile);
}
Exemplo n.º 26
0
void dogoto_command(const char *arg, struct session *ses)
{
    char A[BUFFER_SIZE], B[BUFFER_SIZE],
        distvar[BUFFER_SIZE], locvar[BUFFER_SIZE], pathvar[BUFFER_SIZE];
    char left[BUFFER_SIZE], right[BUFFER_SIZE], tmp[BUFFER_SIZE], cond[BUFFER_SIZE];
    int a, b, i, j, s;
    int d[MAX_LOCATIONS], ok[MAX_LOCATIONS], way[MAX_LOCATIONS];
    char path[BUFFER_SIZE], *pptr;

    arg=get_arg(arg, A, 0, ses);
    arg=get_arg(arg, B, 0, ses);
    arg=get_arg(arg, distvar, 0, ses);
    arg=get_arg(arg, locvar, 0, ses);
    arg=get_arg(arg, pathvar, 0, ses);

    if ((!*A)||(!*B))
    {
        tintin_eprintf(ses, "#SYNTAX: #dogoto <from> <to> [<distvar> [<locvar> [<pathvar>]]] [#else ...]");
        return;
    }
    bool flag=*distvar||*locvar||*pathvar;

    for (a=0;a<MAX_LOCATIONS;a++)
        if (ses->locations[a]&&!strcmp(ses->locations[a], A))
            break;
    if (a==MAX_LOCATIONS)
        goto not_found;
    for (b=0;b<MAX_LOCATIONS;b++)
        if (ses->locations[b]&&!strcmp(ses->locations[b], B))
            break;
    if (b==MAX_LOCATIONS)
        goto not_found;
    for (i=0;i<MAX_LOCATIONS;i++)
    {
        d[i]=INF;
        ok[i]=0;
    }
    d[a]=0;
    do
    {
        s=INF;
        for (j=0;j<MAX_LOCATIONS;j++)
            if (!ok[j]&&(d[j]<s))
                s=d[i=j];
        if (s==INF)
            goto not_found;
        ok[i]=1;
        for (struct routenode *r=ses->routes[i];r;r=r->next)
            if (d[r->dest]>s+r->distance)
            {
                if (!*(r->cond))
                    goto good;
                substitute_vars(r->cond, tmp);
                substitute_myvars(tmp, cond, ses);
                if (eval_expression(cond, ses))
                {
                good:
                    d[r->dest]=s+r->distance;
                    way[r->dest]=i;
                }
            }
    } while (!ok[b]);
    sprintf(tmp, "%d", d[b]);
    if (*distvar)
        set_variable(distvar, tmp, ses);
    j=0;
    for (i=b;i!=a;i=way[i])
        d[j++]=i;
    d[j]=a;
    pptr=path;
    for (i=j;i>=0;i--)
        pptr+=snprintf(pptr, path-pptr+BUFFER_SIZE, " %s", ses->locations[d[i]]);
    pptr=path+(pptr!=path);
    if (*locvar)
        set_variable(locvar, pptr, ses);
    pptr=path;
    for (i=j;i>0;i--)
    {
        for (struct routenode *r=ses->routes[d[i]];r;r=r->next)
            if (r->dest==d[i-1])
            {
                if (flag)
                    pptr+=snprintf(pptr,
                        path-pptr+BUFFER_SIZE,
                        " {%s}",
                        r->path);
                else
                {
                    tintin_printf(ses, "%-10s>%-10s {%s}",
                        ses->locations[d[i]],
                        ses->locations[d[i-1]],
                        r->path);
                }
            }
    }
    pptr=path+(pptr!=path);
    if (*pathvar)
        set_variable(pathvar, pptr, ses);
    return;

not_found:
    arg=get_arg_in_braces(arg, left, 0);
    if (*left == tintin_char)
    {
        if (is_abrev(left + 1, "else"))
        {
            get_arg_in_braces(arg, right, 1);
            parse_input(right, true, ses);
            return;
        }
        if (is_abrev(left + 1, "elif"))
        {
            if_command(arg, ses);
            return;
        }
    }
    if (*left)
        tintin_eprintf(ses, "#ERROR: cruft after #dogoto: {%s}", left);
    if (!flag)
        tintin_printf(ses, "No paths from %s to %s found.", A, B);
}
Exemplo n.º 27
0
void systemexec_command(wchar_t *arg)
{
    wchar_t cmd[BUFFER_SIZE];
//* en
//* due to an error in tintin.h (messed up STOP_SPACES and WITH_SPACES)
//    get_arg_in_braces(arg,cmd,STOP_SPACES,sizeof(cmd)/sizeof(wchar_t)-1);
    get_arg_in_braces(arg,cmd,WITH_SPACES,sizeof(cmd)/sizeof(wchar_t)-1);
//* /en
    if ( !*cmd ) {
        tintin_puts2(rs::rs(1224));
        return;
    }

    HANDLE hOutRead = NULL, hOutDup = NULL;
    HANDLE hOutWrite = NULL, hErrWrite = NULL;

    SECURITY_ATTRIBUTES sa;
    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
    sa.bInheritHandle = TRUE;
    sa.lpSecurityDescriptor = NULL;

    if (!CreatePipe(&hOutRead, &hOutWrite, &sa, BUFFER_SIZE)) {
systemexec_error:
        CloseHandle(hErrWrite);
        CloseHandle(hOutWrite);
        CloseHandle(hOutRead);
        tintin_puts2(rs::rs(1195));
        return;
    }

    if (!DuplicateHandle(
        GetCurrentProcess(), hOutWrite,
        GetCurrentProcess(), &hErrWrite , 0,
        TRUE, DUPLICATE_SAME_ACCESS))
        goto systemexec_error;

    if (!DuplicateHandle(
        GetCurrentProcess(), hOutRead,
        GetCurrentProcess(), &hOutDup , 0,
        FALSE, DUPLICATE_SAME_ACCESS))
        goto systemexec_error;

    CloseHandle(hOutRead);

    PROCESS_INFORMATION pi;
    STARTUPINFO         si;
    ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
    ZeroMemory(&si, sizeof(STARTUPINFO));
    si.cb = sizeof(STARTUPINFO);
    //si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    si.dwFlags = STARTF_USESTDHANDLES;
    si.wShowWindow = SW_HIDE;
    si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
    si.hStdError = hErrWrite;
    si.hStdOutput = hOutWrite;


    if (!CreateProcess(NULL, cmd, NULL, NULL, TRUE,
        CREATE_NO_WINDOW | DETACHED_PROCESS, NULL, NULL, &si, &pi)) {
		DWORD err = GetLastError();
		wchar_t buf[256];
		swprintf(buf, L"GetLastError() = %d", err);
		tintin_puts2(buf);
        goto systemexec_error;
	}

    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
    CloseHandle(hErrWrite);
    CloseHandle(hOutWrite);

    PS_ITEM* psi = new PS_ITEM(hOutDup, pi.dwProcessId, cmd);
    PS_ITEM* pParam = new PS_ITEM(psi);

    EnterCriticalSection(&secSystemList);
    PsList.push_front(psi);
    LeaveCriticalSection(&secSystemList);

    DWORD dwId;
    HANDLE hThread = CreateThread(NULL, 0,
        (LPTHREAD_START_ROUTINE)&systemexec_thread, (LPVOID)pParam, 0, &dwId);
    if (!hThread) {
        delete psi;
        systemlist_delete(pi.dwProcessId);
        return;
    }

    CloseHandle(hThread);
}
Exemplo n.º 28
0
void route_command(const char *arg, struct session *ses)
{
    char a[BUFFER_SIZE], b[BUFFER_SIZE], way[BUFFER_SIZE], dist[BUFFER_SIZE], cond[BUFFER_SIZE];
    int j, d;

    arg=get_arg(arg, a, 0, ses);
    arg=get_arg(arg, b, 0, ses);
    arg=get_arg_in_braces(arg, way, 0);
    arg=get_arg(arg, dist, 0, ses);
    arg=get_arg_in_braces(arg, cond, 1);
    if (!*a)
    {
        tintin_printf(ses, "#THESE ROUTES HAVE BEEN DEFINED:");
        for (int i=0;i<MAX_LOCATIONS;i++)
            for (struct routenode *r=ses->routes[i];r;r=r->next)
                show_route(ses, i, r);
        return;
    }
    if (!*way)
    {
        bool first=true;
        if (!*b)
            strcpy(b, "*");
        for (int i=0;i<MAX_LOCATIONS;i++)
            if (ses->locations[i]&&match(a, ses->locations[i]))
                for (struct routenode *r=ses->routes[i];r;r=r->next)
                    if (ses->locations[i]&&
                          match(b, ses->locations[r->dest]))
                    {
                        if (first)
                        {
                            tintin_printf(ses, "#THESE ROUTES HAVE BEEN DEFINED:");
                            first=false;
                        }
                        show_route(ses, i, r);
                    }
        if (first)
            tintin_printf(ses, "#THAT ROUTE (%s) IS NOT DEFINED.", b);
        return;
    }
    int i;
    for (i=0;i<MAX_LOCATIONS;i++)
        if (ses->locations[i]&&!strcmp(ses->locations[i], a))
            goto found_i;
    if (i==MAX_LOCATIONS)
    {
        for (i=0;i<MAX_LOCATIONS;i++)
            if (!ses->locations[i])
            {
                ses->locations[i]=mystrdup(a);
                goto found_i;
            }
        tintin_eprintf(ses, "#TOO MANY LOCATIONS!");
        return;
    }
found_i:
    for (j=0;j<MAX_LOCATIONS;j++)
        if (ses->locations[j]&&!strcmp(ses->locations[j], b))
            goto found_j;
    if (j==MAX_LOCATIONS)
    {
        for (j=0;j<MAX_LOCATIONS;j++)
            if (!ses->locations[j])
            {
                ses->locations[j]=mystrdup(b);
                goto found_j;
            }
        tintin_eprintf(ses, "#TOO MANY LOCATIONS!");
        kill_unused_locations(ses);
        return;
    }
found_j:
    if (*dist)
    {
        char *err;
        d=strtol(dist, &err, 0);
        if (*err)
        {
            tintin_eprintf(ses, "#Hey! Route length has to be a number! Got {%s}.", arg);
            kill_unused_locations(ses);
            return;
        }
        if ((d<0)&&(ses->mesvar[MSG_ROUTE]||ses->mesvar[MSG_ERROR]))
            tintin_eprintf(ses, "#Error: distance cannot be negative!");
    }
    else
        d=DEFAULT_ROUTE_DISTANCE;
    addroute(ses, i, j, way, d, cond);
    if (ses->mesvar[MSG_ROUTE])
    {
        if (*cond)
            tintin_printf(ses, "#Ok. Way from {%s} to {%s} is now set to {%s} (distance=%i) condition:{%s}",
                ses->locations[i],
                ses->locations[j],
                way,
                d,
                cond);
        else
            tintin_printf(ses, "#Ok. Way from {%s} to {%s} is now set to {%s} (distance=%i)",
                ses->locations[i],
                ses->locations[j],
                way,
                d);
    }
    routnum++;
}