/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 *
 */

extern char *gettxt();
/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"$Header: request.c 1.3 89/12/01 $@(#)nlp:cmd/lpstat/request.c	1.5"

#include "stdio.h"
#include "pwd.h"
#include "sys/types.h"

#include "lp.h"
#include "msgs.h"
#include "requests.h"

#define	WHO_AM_I	I_AM_LPSTAT
#include "oam.h"

#include "lpstat.h"


/**
 ** do_request()
 **/

void			do_request (list)
	char			**list;
{
	while (*list) {
		if (STREQU(NAME_ALL, *list)) {
			send_message (S_INQUIRE_REQUEST, "", "", "", "", "");
			(void)output (R_INQUIRE_REQUEST);

		} else if (isrequest(*list)) {
			send_message (S_INQUIRE_REQUEST, "", "", *list, "", "");
			switch (output(R_INQUIRE_REQUEST)) {
			case MNOINFO:
				LP_ERRMSG1 (ERROR, E_STAT_DONE, *list);
				exit_rc = 1;
				break;
			}

		} else {
			send_message (S_INQUIRE_REQUEST, "", *list, "", "", "");
			switch (output(R_INQUIRE_REQUEST)) {
			case MNOINFO:
				if (!isprinter(*list) && !isclass(*list)) {
					LP_ERRMSG1 (ERROR, E_STAT_BADSTAT, *list);
					exit_rc = 1;
				}
				break;
			}

		}
		list++;
	}
	return;
}

/**
 ** do_user()
 **/

void			do_user (list)
	char			**list;
{
	while (*list) {
		if (STREQU(NAME_ALL, *list)) {
			send_message (S_INQUIRE_REQUEST, "", "", "", "", "");
			(void)output (R_INQUIRE_REQUEST);

		} else {
			send_message (S_INQUIRE_REQUEST, "", "", "", *list, "");
			switch (output(R_INQUIRE_REQUEST)) {
			case MNOINFO:
				if (!getpwnam(*list))
					LP_ERRMSG1 (WARNING, E_STAT_USER, *list);
				break;
			}
		}
		list++;
	}
	return;
}

/**
 ** putoline()
 **/

void			putoline (
				request_id,
				user,
				size,
				date,
				state,
				printer,
				form,
				character_set
			)
	char			*request_id,
				*user,
				*date,
				*printer,
				*form,
				*character_set;
	long			size;
	int			state;
{
	PRINTF (
		"%-*s %-*s %*ld   %.12s",
		IDSIZE,
		request_id,
		LOGMAX-1,
		user,
		OSIZE,
		size,
		date + 4
	);


	if (!verbose) {

		/*
		 * Unless the -l option is given, we show the CURRENT
		 * status. Check the status bits in reverse order of
		 * chronology, i.e. go with the bit that would have been
		 * set last. Old bits don't get cleared by the Spooler.
		 * We only have space for 21 characters!
		 */

		if (state & RS_NOTIFYING)
			PRINTF (gettxt("lpstat:79", " notifying user"));

		else if (state & RS_CANCELLED)
			PRINTF (gettxt("lpstat:80", " canceled"));

		else if (state & RS_PRINTED)
			PRINTF (gettxt("lpstat:81", " finished printing"));

		else if (state & RS_PRINTING)
			PRINTF (gettxt("lpstat:82", " on %s"), printer);

		else if (state & RS_ADMINHELD)
			PRINTF (gettxt("lpstat:83", " held by admin"));

		else if (state & RS_HELD)
			PRINTF (gettxt("lpstat:84", " being held"));

		else if (state & RS_FILTERED)
			PRINTF (gettxt("lpstat:85", " filtered"));

		else if (state & RS_FILTERING)
			PRINTF (gettxt("lpstat:86", " being filtered"));

		else if (state & RS_CHANGING)
			PRINTF (gettxt("lpstat:87", " held for change"));

	} else {
		register char		*sep	= "\n	";

		/*
		 * Here we show all the interesting states the job
		 * has gone through. Left to right they are in
		 * chronological order.
		 */

		if (state & RS_PRINTING) {
			PRINTF (gettxt("lpstat:88", "%son %s"), sep, printer);
			sep = ", ";

		} else if (state & RS_CANCELLED) {
			PRINTF (gettxt("lpstat:89", "%scanceled"), sep);
			sep = ", ";

		} else if (state & RS_FAILED) {
			PRINTF (gettxt("lpstat:90", "%sfailed"), sep);
			sep = ", ";

		} else if (state & RS_PRINTED) {
			PRINTF (gettxt("lpstat:91", "%sfinished on %s"), sep, printer);
			sep = ", ";

		/*
		 * WATCH IT! We make the request ID unusable after
		 * the next line.
		 */
		} else if (!STREQU(strtok(request_id, "-"), printer)) {
			PRINTF (gettxt("lpstat:92", "%sassigned %s"), sep, printer);
			sep = ", ";

		} else {
			PRINTF (gettxt("lpstat:93", "%squeued for %s"), sep, printer);
			sep = ", ";
		}

		if (!(state & RS_DONE)) {
			if (form && *form) {
				PRINTF (gettxt("lpstat:94", "%sform %s"), sep, form);
				sep = ", ";
			}
			if (character_set && *character_set) {
				PRINTF (gettxt("lpstat:95", "%scharset %s"), sep, character_set);
				sep = ", ";
			}
		}

		if (state & RS_NOTIFYING) {
			PRINTF (gettxt("lpstat:96", "%snotifying user"), sep);
			sep = ", ";

		} else if (state & RS_CHANGING) {
			PRINTF (gettxt("lpstat:97", "%sheld for change"), sep);
			sep = ", ";

		} else if (state & RS_ADMINHELD) {
			PRINTF (gettxt("lpstat:98", "%sheld by admin"), sep);
			sep = ", ";

		} else if (state & RS_HELD) {
			PRINTF (gettxt("lpstat:99", "%sbeing held"), sep);
			sep = ", ";

		}

		if (state & RS_FILTERED) {
			PRINTF (gettxt("lpstat:100", "%sfiltered"), sep);
			sep = ", ";

		} else if (state & RS_FILTERING) {
			PRINTF (gettxt("lpstat:101", "%sbeing filtered"), sep);
			sep = ", ";
		}

	}
	PRINTF ("\n");
	return;
}
