/*
 * Copyright (C) 1994-2018 Altair Engineering, Inc.
 * For more information, contact Altair at www.altair.com.
 *
 * This file is part of the PBS Professional ("PBS Pro") software.
 *
 * Open Source License Information:
 *
 * PBS Pro is free software. You can redistribute it and/or modify it under the
 * terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * PBS Pro 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Commercial License Information:
 *
 * For a copy of the commercial license terms and conditions,
 * go to: (http://www.pbspro.com/UserArea/agreement.html)
 * or contact the Altair Legal Department.
 *
 * Altair’s dual-license business model allows companies, individuals, and
 * organizations to create proprietary derivative works of PBS Pro and
 * distribute them - whether embedded or bundled with other software -
 * under a commercial license agreement.
 *
 * Use of Altair’s trademarks, including but not limited to "PBS™",
 * "PBS Professional®", and "PBS Pro™" and Altair’s logos is subject to Altair's
 * trademark licensing policies.
 *
 */
#include <pbs_config.h>   /* the master config generated by configure */

#include <assert.h>
#include <ctype.h>
#include <memory.h>
#ifndef NDEBUG
#include <stdio.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <pbs_ifl.h>
#include "list_link.h"
#include "attribute.h"
#include "resource.h"
#include "pbs_error.h"


/**
 * @file	attr_fn_unkn.c
 * @brief
 * This file contains functions for manipulating attributes of an
 * unknown (unrecognized) name (and therefore unknown type).
 * It is a collection point for all "other" attributes, other than
 * the types with specific definition and meaning.
 *
 * Because the type is unknown, it cannot be decoded into a native
 * form.  Thus the attribute is maintained in the attrlist form.
 * Any attribute/value located here will be sent to the Scheduler and it
 * within its rules may do as it choses with them.
 *
 * The prototypes are declared in "attribute.h"
 *
 * ----------------------------------------------------------------------------
 * Attribute functions for attributes with value type "unknown"
 * ----------------------------------------------------------------------------
 */

/* External Global Items */


/* private functions */


/**
 * @brief
 * 	decode_unkn - decode a pair of strings (name and value) into the Unknown
 *	type attribute/resource which is maintained as a "svrattrl", a
 *	linked list of structures containing strings.
 *
 * @param[in] patr - ptr to attribute to decode
 * @param[in] name - attribute name
 * @param[in] rescn - resource name or null
 * @param[out] val - string holding values for attribute structure
 *
 * @retval      int
 * @retval      0       if ok
 * @retval      >0      error number1 if error,
 * @retval      *patr   members set
 *
 */

int
decode_unkn(struct attribute *patr, char *name, char *rescn, char *value)
{
	svrattrl *entry;
	size_t	     valln;


	if (patr == NULL)
		return (PBSE_INTERNAL);

	if (!(patr->at_flags & ATR_VFLAG_SET))
		CLEAR_HEAD(patr->at_val.at_list);

	if (name == NULL)
		return (PBSE_INTERNAL);

	if (value == NULL)
		valln = 0;
	else
		valln = strlen(value) + 1;

	entry = attrlist_create(name, rescn, valln);
	if (entry == NULL)
		return (PBSE_SYSTEM);

	if (valln)
		memcpy(entry->al_value, value, valln);

	append_link(&patr->at_val.at_list, &entry->al_link, entry);
	patr->at_flags |= ATR_VFLAG_SET | ATR_VFLAG_MODIFY | ATR_VFLAG_MODCACHE;
	return (0);
}


/**
 * @brief
 * 	encode_unkn - encode attr of unknown type into attrlist form
 *
 * Here things are different from the typical attribute.  Most have a
 * single value to be encoded.  But "the unknown" attribute may have a whole
 * list.
 *
 * This function does not use the parent attribute name, after all "_other_"
 * is rather meaningless.  In addition, each unknown already is in an
 * attrlist form.
 *
 * Thus for each entry in the list, encode_unkn duplicates the existing
 * attrlist struct and links the copy into the list.
 *
 * @param[in] attr - ptr to attribute to encode
 * @param[in] phead - ptr to head of attrlist list
 * @param[in] atname - attribute name
 * @param[in] rsname - resource name or null
 * @param[in] mode - encode mode
 * @param[out] rtnl - ptr to svrattrl
 *
 * @retval      int
 * @retval      >0      if ok, entry created and linked into list
 * @retval      =0      no value to encode, entry not created
 * @retval      -1      if error
 *
 */
/*ARGSUSED*/

int
encode_unkn(attribute *attr, pbs_list_head *phead, char *atname, char *rsname, int mode, svrattrl **rtnl)
{
	svrattrl *plist;
	svrattrl *pnew;
	svrattrl *xprior = NULL;
	int	  first = 1;

	if (!attr)
		return (-2);

	plist = (svrattrl *)GET_NEXT(attr->at_val.at_list);
	if (plist == NULL)
		return (0);

	while (plist != NULL) {
		pnew = (svrattrl *)malloc(plist->al_tsize);
		if (pnew == NULL)
			return (-1);
		CLEAR_LINK(pnew->al_link);
		pnew->al_sister = NULL;
		pnew->al_tsize  = plist->al_tsize;
		pnew->al_nameln = plist->al_nameln;
		pnew->al_rescln = plist->al_rescln;
		pnew->al_valln  = plist->al_valln;
		pnew->al_flags  = plist->al_flags;
		pnew->al_refct  = 1;

		pnew->al_name = (char *)pnew + sizeof(svrattrl);
		(void)memcpy(pnew->al_name, plist->al_name, plist->al_nameln);
		if (plist->al_rescln) {
			pnew->al_resc = pnew->al_name + pnew->al_nameln;
			(void)memcpy(pnew->al_resc, plist->al_resc,
				plist->al_rescln);
		} else {
			pnew->al_resc = NULL;
		}
		if (plist->al_valln) {
			pnew->al_value = pnew->al_name + pnew->al_nameln +
				pnew->al_rescln;
			(void)memcpy(pnew->al_value, plist->al_value,
				pnew->al_valln);
		}
		if (phead)
			append_link(phead, &pnew->al_link, pnew);
		if (first) {
			if (rtnl)
				*rtnl = pnew;
			first = 0;
		} else {
			if (xprior)
				xprior->al_sister = pnew;
		}
		xprior = pnew;

		plist = (svrattrl *)GET_NEXT(plist->al_link);
	}
	return (1);
}



/**
 * @brief
 * 	set_unkn - set value of attribute of unknown type  to another
 *
 *	Each entry in the list headed by the "new" attribute is appended
 *	to the list headed by "old".
 *
 *	All operations, set, incr, and decr, map to append.
 * @param[in]   attr - pointer to new attribute to be set (A)
 * @param[in]   new  - pointer to attribute (B)
 * @param[in]   op   - operator
 *
 * @return      int
 * @retval      0       if ok
 * @retval     >0       if error
 *
 */

/*ARGSUSED*/
int
set_unkn(struct attribute *old, struct attribute *new, enum batch_op op)
{
	svrattrl *plist;
	svrattrl *pnext;

	assert(old && new && (new->at_flags & ATR_VFLAG_SET));

	plist = (svrattrl *)GET_NEXT(new->at_val.at_list);
	while (plist != NULL) {
		pnext = (svrattrl *)GET_NEXT(plist->al_link);
		delete_link(&plist->al_link);
		append_link(&old->at_val.at_list, &plist->al_link, plist);
		plist = pnext;
	}
	old->at_flags |= ATR_VFLAG_SET | ATR_VFLAG_MODIFY | ATR_VFLAG_MODCACHE;
	return (0);
}

/**
 * @brief
 * 	comp_unkn - compare two attributes of type ATR_TYPE_RESR
 *
 *	How do you compare something when you don't know what it is...
 *	So, always returns +1
 *
 * @param[in] attr - pointer to attribute structure
 * @param[in] with - pointer to attribute structure
 *
 * @return      int
 * @retval      0       if the set of strings in "with" is a subset of "attr"
 * @retval      1       otherwise
 *
 */

int
comp_unkn(struct attribute *attr, struct attribute *with)
{
	return (1);
}

/**
 * @brief
 * 	free_unkn - free space associated with attribute value
 *
 *	For each entry in the list, it is delinked, and freed.
 *
 * @param[in] pattr - pointer to attribute structure
 *
 * @return	Void
 *
 */

void
free_unkn(attribute *pattr)
{
	svrattrl *plist;

	if (pattr->at_flags & ATR_VFLAG_SET) {
		while ((plist = (svrattrl *)GET_NEXT(pattr->at_val.at_list)) !=
			NULL) {
			delete_link(&plist->al_link);
			(void)free(plist);
		}
	}
	free_null(pattr);
	CLEAR_HEAD(pattr->at_val.at_list);
}
