1 /* 2 * Copyright 2012, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "CliStopCommand.h" 8 9 #include <stdio.h> 10 11 #include <AutoLocker.h> 12 13 #include "CliContext.h" 14 #include "MessageCodes.h" 15 #include "Team.h" 16 #include "UserInterface.h" 17 18 CliStopCommand::CliStopCommand() 19 : 20 CliCommand("stop a thread", 21 "%s [ <thread ID> ]\n" 22 "Stops the thread specified by <thread ID>, if supplied. Otherwise " 23 "stops\n" 24 "the current thread.") 25 { 26 } 27 28 29 void 30 CliStopCommand::Execute(int argc, const char* const* argv, 31 CliContext& context) 32 { 33 if (argc > 2) { 34 PrintUsage(argv[0]); 35 return; 36 } 37 38 39 AutoLocker<Team> teamLocker(context.GetTeam()); 40 Thread* thread = NULL; 41 if (argc < 2) { 42 thread = context.CurrentThread(); 43 if (thread == NULL) { 44 printf("Error: No current thread.\n"); 45 return; 46 } 47 } else if (argc == 2) { 48 // parse the argument 49 char* endPointer; 50 long threadID = strtol(argv[1], &endPointer, 0); 51 if (*endPointer != '\0' || threadID < 0) { 52 printf("Error: Invalid parameter \"%s\"\n", argv[1]); 53 return; 54 } 55 56 // get the thread and change the current thread 57 Team* team = context.GetTeam(); 58 thread = team->ThreadByID(threadID); 59 if (thread == NULL) { 60 printf("Error: No thread with ID %ld\n", threadID); 61 return; 62 } 63 } 64 65 if (thread->State() == THREAD_STATE_STOPPED) { 66 printf("Error: thread %" B_PRId32 " is already stopped.\n", 67 thread->ID()); 68 return; 69 } 70 71 context.GetUserInterfaceListener()->ThreadActionRequested(thread->ID(), 72 MSG_THREAD_STOP); 73 } 74