/*
 * misc.c - miscellaneous functions
 *
 * Copyright (C) 2003   Rodolfo Giometti <giometti@linux.com>
 *
 *   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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <argz.h>

#include "c6term.h"

/* --- Misc functins -------------------------------------------------------- */

/* This function reset __only__ global variables */
void reset_status(void)
{
   status = OFFLINE;
   user[0] = passwd[0] = tonick[0] = '\0';
   server_count = client_count = 1;

   /* Watch out! We also close the main socket! */
   if (sock >= 0) {
      close(sock);
      sock = -1;
   }
}

int readn(int fd, void *vptr, int n)
{
   int nleft = n, nread;
   char *ptr = vptr;

   while (nleft > 0) {
      if ((nread = read(fd, ptr, nleft)) < 0) {
         if (errno != EAGAIN)
            return -1;
         nread = 0;
      }
      else if (nread == 0)
         break;

      nleft -= nread;
      ptr += nread;
   }

   return n-nleft;
}

int writen(int fd, void *vptr, int n)
{
   int nleft = n, nwritten;
   char *ptr = vptr;

   while (nleft > 0) {
      if ((nwritten = write(fd, ptr, nleft)) < 0) {
         if (errno != EAGAIN)
            return -1;
         nwritten = 0;
      }
      else if (nwritten == 0)
         break;

      nleft -= nwritten;
      ptr += nwritten;
   }

   return n-nleft;
}

int unix_style_conv(char *string, int *argc, char ***argv)
{
   static char *argz = NULL;
   static int lenz = 0;
   int n;

   n = strlen(string);
   if (n == 0) {
      *argc = 0;
      *argv = NULL;

      free(*argv);
      return 0;
   }

   /* Remove spaces from the end */
   for (n--; n > 0 && string[n] == ' '; n--)
      ;
   string[n+1] = '\0';

   if (argz_create_sep(string, ' ', &argz, &lenz) < 0)
      return -1;

   *argc = argz_count(argz, lenz);;
   *argv = realloc(*argv, (*argc+1)*sizeof(char *));
   if (*argv == NULL)
      return -1;
   argz_extract(argz, lenz, *argv);

   return 0;
}

char *escape(char *str)
{
   int i, j;
   /* Is not the best but it's simple... :) */
   char *enc = malloc(strlen(str)*2);
   if (enc == NULL)
      return NULL;

   for (i = j = 0; str[i]; i++, j++) {
      if (str[i] == '\n') {
         enc[j++] = '\\';
         enc[j] = 'n';
      }
      else
         enc[j] = str[i];
   }
   enc[j] = 0;

   return enc;
}

char *unescape(char *str)
{
   int i, j;
   char *unenc = malloc(strlen(str));
   if (unenc == NULL)
      return NULL;

   for (i = j = 0; str[i]; i++, j++) {
      if (str[i] == '\\' && str[i+1] == 'n') {
         i++;
         unenc[j] = '\n'; 
      }
      else
         unenc[j] = str[i];
   }
   unenc[j] = 0;

   return unenc;
}

