/*
 * front/generic.c
 *   Generic frontend - NOT a real frontend, just set of common
 * frontend's tools - especially parsing
 *
 * $Id: generic.c,v 1.1.1.1 2002/01/24 20:19:48 sembera Exp $
 */
/*
 * IIRC - IIRC Is a Real Client (Modular IRC Client)
 * Copyright (C) 2001  Jan Sembera <fis@ji.cz>
 * Copyright (C) 2001  Petr Baudis <pasky@ji.cz>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdlib.h>
#include <string.h>

#include "generic.h"

int
generic_front_split(char *str, parsed_t *pcmd) {
  char *space,
       *buf,
       *ostr;

  if ((str[0] != '/') || (! pcmd))
    return 0;

  ostr = str + 1;
  str = malloc(strlen(ostr));
  strcpy(str, ostr);
  
  pcmd->argv = 0;
  pcmd->argc = 0;
  
  space = strchr(str, ' ');
  
  if (! space) {
    
    pcmd->command = malloc(strlen(str));
    strcpy(pcmd->command, str);
    
  } else {
    
    (*space) = 0;
    pcmd->command = malloc(strlen(str));
    strcpy(pcmd->command, str);
    
    (*space) = ' ';
    while ((*space) == ' ')
      space++;
    
    buf = space;
    
    for (space = strchr(buf, ' ');
	buf && *buf;
	(buf = space + (space? 1: 0)),
	(space = buf? strchr(buf, ' '): space)) {
      
      if ((*buf) == ' ')
	continue;
      
      if (space)
	(*space) = 0;
      
      pcmd->argc++;
      pcmd->argv = realloc(pcmd->argv, pcmd->argc * sizeof(char *));
      
      pcmd->argv[pcmd->argc - 1] = malloc(strlen(buf));
      strcpy(pcmd->argv[pcmd->argc - 1], buf);
      
      if (space)
	(*space)=' ';
      
    }
    
  }
  
  return pcmd->argc + 1;
}

int
generic_front_join(char **str, parsed_t *pcmd) {
  int vc,
      len = 0;
  
  if ((! pcmd) || (! pcmd->command) || (pcmd->argc < 0) ||
      ((pcmd->argc > 0) && (! pcmd->argv)))
    return 0;
  
  len = strlen(pcmd->command);
  for (vc = 0; vc < pcmd->argc; vc++)
    len += strlen(pcmd->argv[vc]) + 1;

  *str = malloc(len + 1);
  strcpy(*str, "/");
  strcat(*str, pcmd->command);
  if (pcmd->argc)
    strcat(*str, " "); 
  
  for (vc = 0; vc < pcmd->argc; vc++) {
    strcat(*str, pcmd->argv[vc]);
    if (vc < pcmd->argc - 1)
      strcat(*str, " ");
  }

  return len;
}

int
generic_front_parjoin(char **str, int from, int count, parsed_t *pcmd) {
  int vc,
      len = 0;
  
  if ((! pcmd) || ((! count) && (pcmd->argc < from+count)) ||
      (! pcmd->argc) || (! pcmd->argv))
    return 0;

  if (! count)
    count = pcmd->argc - from;

  for (vc = from; vc < from + count; vc++)
    len += strlen(pcmd->argv[vc]) + 1;
  len--;

  *str = malloc(len+1);
  str[0][0] = 0;
  
  for (vc = from; vc < from + count; vc++) {
    strcat(*str, pcmd->argv[vc]);
    if (vc < from + count - 1)
      strcat(*str, " ");
  }

  return len;
}

int
generic_front_lookup_in(hook_t *hooks, parsed_t *pcmd) {
  hook_t *hook = hooks;
  int idx = 0;

  while (hook->handler) {
    if (hook->name)
      if (! strcasecmp(hook->name, pcmd->command))
	return idx;
    
    hook++;
    idx++;
  }

  return -1;
}

int
generic_front_destroy(parsed_t *pcmd) {
  
  if (! pcmd)
    return 0;
  
  if (pcmd->command)
    free(pcmd->command);

  for (; pcmd->argc; pcmd->argc--)
    free(pcmd->argv[pcmd->argc - 1]);
  
  return 1;
}
