tbamud-3.52/cnf/
tbamud-3.52/lib/
tbamud-3.52/lib/etc/
tbamud-3.52/lib/house/
tbamud-3.52/lib/misc/
tbamud-3.52/lib/plralias/A-E/
tbamud-3.52/lib/plralias/F-J/
tbamud-3.52/lib/plralias/K-O/
tbamud-3.52/lib/plralias/P-T/
tbamud-3.52/lib/plralias/U-Z/
tbamud-3.52/lib/plralias/ZZZ/
tbamud-3.52/lib/plrfiles/A-E/
tbamud-3.52/lib/plrfiles/F-J/
tbamud-3.52/lib/plrfiles/K-O/
tbamud-3.52/lib/plrfiles/P-T/
tbamud-3.52/lib/plrfiles/U-Z/
tbamud-3.52/lib/plrfiles/ZZZ/
tbamud-3.52/lib/plrobjs/
tbamud-3.52/lib/plrobjs/A-E/
tbamud-3.52/lib/plrobjs/F-J/
tbamud-3.52/lib/plrobjs/K-O/
tbamud-3.52/lib/plrobjs/P-T/
tbamud-3.52/lib/plrobjs/U-Z/
tbamud-3.52/lib/plrobjs/ZZZ/
tbamud-3.52/lib/plrvars/A-E/
tbamud-3.52/lib/plrvars/F-J/
tbamud-3.52/lib/plrvars/K-O/
tbamud-3.52/lib/plrvars/P-T/
tbamud-3.52/lib/plrvars/U-Z/
tbamud-3.52/lib/plrvars/ZZZ/
tbamud-3.52/lib/text/help/
tbamud-3.52/lib/text/help/oldhelp/
tbamud-3.52/log/
/*************************************************************************
* File: alias.c	                                          Part of tbaMUD *
* Usage: Writing/reading player's aliases.				 *
*									 *
* Copyright (C) 1993, 94 by the Trustees of the Johns Hopkins University *
* CircleMUD is based on DikuMUD, Copyright (C) 1990, 1991.		 *
* by Jeremy Hess, Chad Thompson, and George Greer                        *
**************************************************************************/

#include "conf.h"
#include "sysdep.h"
#include "structs.h"
#include "utils.h"
#include "interpreter.h"
#include "db.h"

void write_aliases(struct char_data *ch);
void read_aliases(struct char_data *ch);
void delete_aliases(const char *charname);

void write_aliases(struct char_data *ch)
{
  FILE *file;
  char fn[MAX_STRING_LENGTH];
  struct alias_data *temp;

  get_filename(fn, sizeof(fn), ALIAS_FILE, GET_NAME(ch));
  remove(fn);

  if (GET_ALIASES(ch) == NULL)
    return;

  if ((file = fopen(fn, "w")) == NULL) {
    log("SYSERR: Couldn't save aliases for %s in '%s': %s", GET_NAME(ch), fn, strerror(errno));
    /* SYSERR_DESC: This error occurs when the server fails to open the relevant
     * alias file for writing.  The text at the end of the error should give a
     * valid reason why. */
    return;
  }

  for (temp = GET_ALIASES(ch); temp; temp = temp->next) {
    int aliaslen = strlen(temp->alias);
    int repllen = strlen(temp->replacement) - 1;

    fprintf(file, "%d\n%s\n"	/* Alias */
		  "%d\n%s\n"	/* Replacement */
		  "%d\n",	/* Type */
		aliaslen, temp->alias,
		repllen, temp->replacement + 1,
		temp->type);
  }

  fclose(file);
}

void read_aliases(struct char_data *ch)
{
  FILE *file;
  char xbuf[MAX_STRING_LENGTH];
  struct alias_data *t2, *prev = NULL;
  int length;

  get_filename(xbuf, sizeof(xbuf), ALIAS_FILE, GET_NAME(ch));

  if ((file = fopen(xbuf, "r")) == NULL) {
    if (errno != ENOENT) {
      log("SYSERR: Couldn't open alias file '%s' for %s: %s", xbuf, GET_NAME(ch), strerror(errno));
      /* SYSERR_DESC: This error occurs when the server fails to open the 
       * relevant alias file for reading.  The text at the end version should 
       * give a valid reason why. */
    }
    return;
  }

  CREATE(GET_ALIASES(ch), struct alias_data, 1);
  t2 = GET_ALIASES(ch);

  for (;;) {
    /* Read the aliased command. */
    if (fscanf(file, "%d\n", &length) != 1)
      goto read_alias_error;

    fgets(xbuf, length + 1, file);
    t2->alias = strdup(xbuf);

    /* Build the replacement. */
    if (fscanf(file, "%d\n", &length) != 1)
       goto read_alias_error;

    *xbuf = ' ';		/* Doesn't need terminated, fgets() will. */
    fgets(xbuf + 1, length + 1, file);
    t2->replacement = strdup(xbuf);

    /* Figure out the alias type. */
    if (fscanf(file, "%d\n", &length) != 1)
      goto read_alias_error;

    t2->type = length;

    if (feof(file))
      break;

    CREATE(t2->next, struct alias_data, 1);
    prev = t2;
    t2 = t2->next;
  };

  fclose(file);
  return;

read_alias_error:
  if (t2->alias)
    free(t2->alias);
  free(t2);
  if (prev)
    prev->next = NULL;
  fclose(file);
}

void delete_aliases(const char *charname)
{
  char filename[PATH_MAX];

  if (!get_filename(filename, sizeof(filename), ALIAS_FILE, charname))
    return;

  if (remove(filename) < 0 && errno != ENOENT)
    log("SYSERR: deleting alias file %s: %s", filename, strerror(errno));
    /* SYSERR_DESC: When an alias file cannot be removed, this error will occur,
     * and the reason why will be the tail end of the error. */
}

/* until further notice, the alias->pfiles save and load functions will 
 * function along side the old seperate alias file load, for compatibility. */
void write_aliases_ascii(FILE *file, struct char_data *ch)
{
  struct alias_data *temp;
  int count = 0;

  if (GET_ALIASES(ch) == NULL)
    return;

  for (temp = GET_ALIASES(ch); temp; temp = temp->next)
    count++;

  fprintf(file, "Alis: %d\n", count);
  /* the +1 thing below is due to alias replacements having a space prepended 
   * in memory. The reason for this escapes me. Welcor 27/12/06 */
  for (temp = GET_ALIASES(ch); temp; temp = temp->next) {
    fprintf(file, "%s\n"	/* Alias */
		  "%s\n"	/* Replacement */
		  "%d\n",	/* Type */
		temp->alias,
		temp->replacement+1,
		temp->type);
  }
}

void read_aliases_ascii(FILE *file, struct char_data *ch, int count)
{
  int i;
  struct alias_data *temp;
  char abuf[MAX_INPUT_LENGTH], rbuf[MAX_INPUT_LENGTH+1], tbuf[MAX_INPUT_LENGTH];

  if (count == 0) {
    GET_ALIASES(ch) = NULL;
    return; /* No aliases in the list. */
  }

  for (i = 0; i < count; i++) {
    /* Read the aliased command. */
    get_line(file, abuf);

    /* Read the replacement. */
    get_line(file, tbuf);
    strcpy(rbuf, " ");
    strcat(rbuf, tbuf); /* strcat: OK */

    /* read the type */
    get_line(file, tbuf);

    if (abuf && *abuf && tbuf && *tbuf && rbuf && *rbuf) {
      CREATE(temp, struct alias_data, 1);
      temp->alias = strdup(abuf);
      temp->replacement = strdup(rbuf);
      temp->type = atoi(tbuf);
      temp->next = GET_ALIASES(ch);
      GET_ALIASES(ch) = temp;
    }
  }
}