/*
 * Copyright (C) 1994-2021 Altair Engineering, Inc.
 * For more information, contact Altair at www.altair.com.
 *
 * This file is part of both the OpenPBS software ("OpenPBS")
 * and the PBS Professional ("PBS Pro") software.
 *
 * Open Source License Information:
 *
 * OpenPBS 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.
 *
 * OpenPBS 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:
 *
 * PBS Pro is commercially licensed software that shares a common core with
 * the OpenPBS software.  For a copy of the commercial license terms and
 * conditions, go to: (http://www.pbspro.com/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 OpenPBS 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™",
 * "OpenPBS®", "PBS Professional®", and "PBS Pro™" and Altair's logos is
 * subject to Altair's trademark licensing policies.
 */

/**
 * @file	parse_jobid.c
 * @brief
 * takes a job_id string as input, parses it to separate the
 * 'current_server[:port]' part, and returns this in the return
 * argument 'server'; if things go OK, the function value is
 * set to 0, if errors, it is set to 1.   The 'current_server[:port]'
 * part of 'job_id' is removed, if it was present.
 *
 * Full legal syntax is:
 *  seq_number[.parent_server[:port]][@current_server[:port]]
 */

#include <pbs_config.h> /* the master config generated by configure */
#include "cmds.h"

#define ISNAMECHAR(x) ((isgraph(x)) && ((x) != '#') && ((x) != '@'))

/* static vars removed to make this a thread_safe function */

/**
 * @brief
 *	parses the job id for current server[:port] part
 *
 * @param[in] job_id - job id
 * @param[out] arg_seq_number - sequence number
 * @param[out] arg_parent_server - parent server
 * @param[out] arg_current_server - current server
 *
 * @return	int
 * @retval	0	success
 * @retval	1	error
 *
 */
int
parse_jobid(char *job_id, char **arg_seq_number, char **arg_parent_server, char **arg_current_server)
{
	int is_resv = 0;
	char *c;
	char *seq_number = NULL;
	int s_pos = 0;
	char *parent_server = NULL;
	int p_pos = 0;
	char *current_server = NULL;
	int c_pos = 0;
	int ret = 0;

	seq_number = calloc(PBS_MAXCLTJOBID + 1, 1);
	if (seq_number == NULL) {
		ret = 1;
		goto err;
	}
	parent_server = calloc(MAXSERVERNAME, 1);
	if (parent_server == NULL) {
		ret = 1;
		goto err;
	}
	current_server = calloc(MAXSERVERNAME, 1);
	if (current_server == NULL) {
		ret = 1;
		goto err;
	}

	/* Begin the parse */
	c = job_id;
	while (isspace(*c))
		c++;

	/* skip past initial char if reservation */
	if (*c == PBS_RESV_ID_CHAR || *c == PBS_STDNG_RESV_ID_CHAR || *c == PBS_MNTNC_RESV_ID_CHAR) {
		is_resv = 1;
		seq_number[s_pos++] = *c;
		c++;
	}

	/* Looking for a seq_number */
	while (*c != '\0') {
		if (isdigit(*c)) {
			if ((s_pos >= PBS_MAXSEQNUM && !is_resv) ||
			    (s_pos >= PBS_MAXSEQNUM + 1 && is_resv)) {
				ret = 3;
				goto err;
			}
			seq_number[s_pos++] = *c;
		} else
			break;
		c++;
	}
	if (s_pos == 0) {
		ret = 1;
		goto err;
	}

	/* Is this an ArrayJob identifier or a Array subjob id? */
	if (*c == '[') {
		if (is_resv) {
			ret = 1;
			goto err; /* cannot be both Array... and reservation */
		}

		if (s_pos >= PBS_MAXCLTJOBID) {
			ret = 3;
			goto err;
		}
		seq_number[s_pos++] = *c++; /* copy over opening brace */
		while (*c != ']') {
			while (isdigit((int) *c)) {
				if (s_pos >= PBS_MAXCLTJOBID) {
					ret = 3;
					goto err;
				}
				seq_number[s_pos++] = *c++;
			}
			if (*c == '-') {
				if (s_pos >= PBS_MAXCLTJOBID) {
					ret = 3;
					goto err;
				}
				seq_number[s_pos++] = *c++;
			} else if (*c == ':') {
				if (s_pos >= PBS_MAXCLTJOBID) {
					ret = 3;
					goto err;
				}
				seq_number[s_pos++] = *c++;
			} else if (*c != ']') {
				ret = 1;
				goto err;
			}
		}
		if (s_pos >= PBS_MAXCLTJOBID) {
			ret = 3;
			goto err;
		}
		seq_number[s_pos++] = *c++; /* copy in closing brace */
	}

	/* Looking for a parent_server */
	if (*c == '.') {
		c++;
		while (*c != '\0') {
			if (ISNAMECHAR(*c)) {
				if (p_pos >= MAXSERVERNAME) {
					ret = 3;
					goto err;
				}
				parent_server[p_pos++] = *c;
			} else
				break;
			c++;
		}
		if (p_pos == 0) {
			ret = 1;
			goto err;
		}
	}

	/* Looking for a current_server */
	if (*c == '@') {
		c++;
		while (*c != '\0') {
			if (ISNAMECHAR(*c)) {
				if (c_pos >= MAXSERVERNAME) {
					ret = 3;
					goto err;
				}
				current_server[c_pos++] = *c;
			} else
				break;
			c++;
		}
		if (c_pos == 0) {
			ret = 1;
			goto err;
		}
	}

	if (*c != '\0') {
		ret = 2;
		goto err;
	}

	if ((s_pos + p_pos + 2) > PBS_MAXCLTJOBID) {
		ret = 3;
		goto err;
	}

	/* set char * pointers to static data, to arguments */
	if (arg_seq_number != NULL)
		*arg_seq_number = seq_number;
	else
		free(seq_number);

	if (arg_parent_server != NULL)
		*arg_parent_server = parent_server;
	else
		free(parent_server);

	if (arg_current_server != NULL)
		*arg_current_server = current_server;
	else
		free(current_server);

	return 0;

err:
	if (seq_number)
		free(seq_number);
	if (current_server)
		free(current_server);
	if (parent_server)
		free(parent_server);
	return ret;
}
