19b008d31SIngo Weinhold/*
29b008d31SIngo Weinhold * Copyright 2012, Ingo Weinhold, ingo_weinhold@gmx.de.
39b008d31SIngo Weinhold * Distributed under the terms of the MIT License.
49b008d31SIngo Weinhold */
59b008d31SIngo Weinhold
69b008d31SIngo Weinhold
79b008d31SIngo Weinhold#include "CliThreadCommand.h"
89b008d31SIngo Weinhold
99b008d31SIngo Weinhold#include <stdio.h>
109b008d31SIngo Weinhold
119b008d31SIngo Weinhold#include <AutoLocker.h>
129b008d31SIngo Weinhold
139b008d31SIngo Weinhold#include "CliContext.h"
149b008d31SIngo Weinhold#include "Team.h"
159b008d31SIngo Weinhold
169b008d31SIngo Weinhold
179b008d31SIngo WeinholdCliThreadCommand::CliThreadCommand()
189b008d31SIngo Weinhold	:
199b008d31SIngo Weinhold	CliCommand("set or print the current thread",
209b008d31SIngo Weinhold		"%s [ <thread ID> ]\n"
219b008d31SIngo Weinhold		"Sets the current thread to <thread ID>, if supplied. Otherwise prints "
229b008d31SIngo Weinhold			"the\n"
239b008d31SIngo Weinhold		"current thread.")
249b008d31SIngo Weinhold{
259b008d31SIngo Weinhold}
269b008d31SIngo Weinhold
279b008d31SIngo Weinhold
289b008d31SIngo Weinholdvoid
299b008d31SIngo WeinholdCliThreadCommand::Execute(int argc, const char* const* argv,
309b008d31SIngo Weinhold	CliContext& context)
319b008d31SIngo Weinhold{
329b008d31SIngo Weinhold	if (argc > 2) {
339b008d31SIngo Weinhold		PrintUsage(argv[0]);
349b008d31SIngo Weinhold		return;
359b008d31SIngo Weinhold	}
369b008d31SIngo Weinhold
379b008d31SIngo Weinhold	if (argc < 2) {
389b008d31SIngo Weinhold		// no arguments -- print the current thread
399b008d31SIngo Weinhold		context.PrintCurrentThread();
409b008d31SIngo Weinhold		return;
419b008d31SIngo Weinhold	}
429b008d31SIngo Weinhold
439b008d31SIngo Weinhold	// parse the argument
449b008d31SIngo Weinhold	char* endPointer;
459b008d31SIngo Weinhold	long threadID = strtol(argv[1], &endPointer, 0);
469b008d31SIngo Weinhold	if (*endPointer != '\0' || threadID < 0) {
479b008d31SIngo Weinhold		printf("Error: Invalid parameter \"%s\"\n", argv[1]);
489b008d31SIngo Weinhold		return;
499b008d31SIngo Weinhold	}
509b008d31SIngo Weinhold
519b008d31SIngo Weinhold	// get the thread and change the current thread
529b008d31SIngo Weinhold	Team* team = context.GetTeam();
539b008d31SIngo Weinhold	AutoLocker<Team> teamLocker(team);
549b008d31SIngo Weinhold	if (Thread* thread = team->ThreadByID(threadID))
559b008d31SIngo Weinhold		context.SetCurrentThread(thread);
569b008d31SIngo Weinhold	else
579b008d31SIngo Weinhold		printf("Error: No thread with ID %ld\n", threadID);
589b008d31SIngo Weinhold}
59