#include <stdio.h>
#include <exec/types.h>
#include <exec/exec.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <exec/memory.h>
#include <exec/execbase.h>
#include <exec/tasks.h>
#include <exec/ports.h>
#include <exec/io.h>

/*
    Program:  stp.c  (Set Task Priority)
    Purpose:  Change a currently existing task's priority.
    Author:   Jeffrey Bailey
    Date:     3/15/88
    Usage:    stp <hex TCB address> <dec. priority>
    Compiler: Lattice C 4.0
    Porting:  It should be relatively easy to modify this to work
              with Aztec C.  The only special routine I used was
              strtol(), which may or may not exist in Aztec.

    I release this program into the public domain.  You may use all or 
    part of the source code for any purpose whatsoever.  All I ask is 
    that you give credit where credit is due.  

    I do not guarantee this program in any way, shape, or form.  Note
    that it does work for me, and I have attempted to test it
    thoroughly.  Use it at your own risk.  You can report
    any bugs, problems, features, suggestions, etc. to the address below.
    I do not promise to change or fix anything, but I will try to 
    respond to any mail messages I receive - please include a good 
    return path in your message!

    UUCP:...{ihnp4!codas, ucf-cs, allegra}!novavax!jeff
    UUCP:...{ihnp4!codas, ucf-cs, allegra}!novavax!regulus!jeff  (Private)
        (routing through codas preferred)
*/

struct ExecBase *ExecBase;

void main(argc, argv)
    int  argc;
    char *argv [];
    {
    char *badchar;
    struct Task *tad;
    long tpri;
    int  saneaddr;

/*
    If started from WorkBench, exit.  Don't bother printing an error msg.
*/
    if(argc == 0) exit(20);

    if(argc < 3)
        {
        puts("STP:  Missing args.  Supply a task address and a priority.");
        exit(20);
        }

    if(argc > 3)
        {
        puts("STP:  Too many args.  Supply a task address and a priority.");
        }

/*
    Convert hex string to a pointer value.  If strtol() sets badchar to point
    to a NULL, then conversion went ok.  Otherwise, badchar points at char in
    error.
*/
    tad = (struct Task *) strtol(argv [1], &badchar, 16);

    if(*badchar != NULL)
        {
        puts("STP:  Bad hex string conversion on task address.");
        exit(20);
        }

/*
    Convert decimal string to an integer value.  If strtol() sets badchar to 
    point to a NULL, then conversion went ok.  Otherwise, badchar points at 
    char in error.
*/
    tpri = strtol(argv [2], &badchar, 10);

    if(*badchar != NULL)
        {
        puts("STP:  Bad decimal string conversion on task priority.");
        exit(20);
        }

/*
    Check priority to make sure it's within proper bounds.
*/
    if(tpri < -128)
        {
        puts("STP:  Priority specified is too low.  Must be no less than -128.");
        exit(20);
        }
    if(tpri > 127)
        {
        puts("STP:  Priority specified is too high.  Must be no greater than 127.");
        exit(20);
        }

/*
    Get ExecBase pointer.  We need this to do sanity checking on the TCB
    address supplied.
*/
    if( (ExecBase = (struct ExecBase *) OpenLibrary("exec.library",0)) == 0 )
        {
        puts("STP:  Couldn't open Exec Library.");
        exit(20);
        }

/*
    Disable interrupts.  The RKM says this is necessary when scanning exec
    lists.  Apparently Forbid() isn't good enough in this instance.
*/
    Disable();

/*
    Do our TCB address sanity checking.  Set the new task priority if the
    address is valid.
*/
    if( (saneaddr = sane_address(tad)) )
        {
        SetTaskPri(tad, tpri);
        }

/*
    Enable interrupts again.
*/
    Enable();

/*
    If we couldn't find the TCB, complain.  Can't do this inside the Disable()
    Enable() pair, 'cause we can't keep interrupts disabled for very long, plus
    the RKM warns against printing when you disable interrupts.
*/
    if(!saneaddr)
        {
        puts("STP:  Can't find TCB at specified address.");
        }

    exit(0);
    }

sane_address(taskaddr)
    register struct Task *taskaddr;
    {
    register struct Task *task;

/*
    Check to see if we're trying to modify the current task's priority.
*/
    if(taskaddr == ExecBase->ThisTask) return(1);

/*
    Check the ready queue for our TCB.
*/
    for(task = (struct Task *)ExecBase->TaskReady.lh_Head; task->tc_Node.ln_Succ;
        task = (struct Task *)task->tc_Node.ln_Succ)
        {
        if(task == taskaddr) return(1);
        }

/*
    Check the wait queue for our TCB.
*/
    for(task = (struct Task *)ExecBase->TaskWait.lh_Head; task->tc_Node.ln_Succ;
        task = (struct Task *)task->tc_Node.ln_Succ)
        {
        if(task == taskaddr) return(1);
        }

/*
    If we got to this point, we haven't found our TCB.  Return failure.
*/
    return(0);
    }
