Example #1
0
//------------------------------------------------------------------------------
// Find first user of input instruction in its parent block.
// Return nullptr if no use is found.
Instruction *findFirstUser(Instruction *inst) {
  InstVector users = findUsers(inst);
  Instruction *firstUser = nullptr;
  int minDistance = inst->getParent()->size();

  BasicBlock::iterator begin(inst);
  for (auto inst : users) {
    BasicBlock::iterator blockIter(inst);
    int currentDist = std::distance(begin, blockIter);
    if (currentDist < minDistance) {
      minDistance = currentDist;
      firstUser = inst;
    }
  }

  return firstUser;
}
Example #2
0
//------------------------------------------------------------------------------
// Find the last user of input instruction in its parent block.
// Return nullptr if no use is found.
Instruction *findLastUser(Instruction *inst) {
  InstVector users = findUsers(inst);
  users = filterUsers(inst, users);
  Instruction *lastUser = nullptr;
  int maxDistance = 0;

  BasicBlock::iterator begin(inst);
  for (auto inst : users) {
    BasicBlock::iterator blockIter(inst);
    int currentDist = std::distance(begin, blockIter);
    if (currentDist > maxDistance) {
      maxDistance = currentDist;
      lastUser = inst;
    }
  }

  return lastUser;
}
Example #3
0
void UI::handleCommand(const std::string &command) {

	std::pair<std::string::size_type, std::string::size_type> pos
		= nextToken(command);
	std::string::size_type len = pos.second - pos.first;

	if (!len) {
		return;
	}

	std::string data(command, pos.first, len);

	if (len == 1 && data == "/" && command.length() > 2) {
		pos.first += 2;
		goto chatmsg;
	}

	if (command[pos.first] != '/') {
	chatmsg:
		if (! chatNetwork.length()) {
			messageW->printf("Message not sent, use /chat or /msg command\n");
			messageW->refresh();
			return;
		}

		if (! userExists(chatUser, chatNetwork)) {
			messageW->printf("Message not sent, user not found\n");
			messageW->refresh();
			return;
		}

		sendSignal("/net/msg/send", chatNetwork,
				   new sig::MessageData(chatUser,
									std::string(command, pos.first), 0));

	}

	if (len == 5 && (data == "/quit" || data == "/exit")) {
		sendSignal("/core/module/exits", Core::coreName);
		return;
	}

	if ((len == 2 && (data == "/n" || data == ":n"))
	|| (len == 6 && data == "/names")) {
		/* /names command */
		if (networkUsers.empty()) {
			messageW->printf("There are no connected networks\n");
		} else {
			NetworkUsers::iterator nuit;
			sig::UsersListData::Users::iterator uit;
			for(nuit=networkUsers.begin(); nuit!=networkUsers.end(); ++nuit) {
				if (nuit->second->users.empty()) {
					messageW->printf("Network %s has no connected users\n",
					                 nuit->first.c_str());
				} else {
					messageW->printf("Connected users in network %s:\n",
					                 nuit->first.c_str());
					for(uit=nuit->second->users.begin();
					    uit!=nuit->second->users.end();
					    ++uit) {
						messageW->printf("%s (%s) (%s)\n",
						          uit->second->name.c_str(),
						          uit->first.toString().c_str(),
						          uit->second->status.toString().c_str()
						);
					}
				}
			}
		}
	}

	if (len == 5 && data == "/chat") {

		/* trim */
		pos = nextToken(command, pos.second);
		if (pos.first == std::string::npos) {
			/* end chat */
			messageW->printf("Chat with %s (network %s) finished\n",
			                chatUser.toString().c_str(),
							chatNetwork.c_str());
			chatNetwork.clear();
			statusW->printf("\n");
			statusW->refresh();
			return;
		}

		std::string userString(command, pos.first, pos.second-pos.first);

		std::multimap< std::string, User* >::iterator it;
		findUsers(userString);

		if(usersFound.size() == 0) {
			messageW->printf("No users match to '%s', try /names command.\n", userString.c_str());
		} else if(usersFound.size() > 1) {
			int iii;
			messageW->printf("Ambiguous user '%s', possible matches:\n",
			                 userString.c_str());
			for(iii=1, it=usersFound.begin(); it!=usersFound.end(); ++iii, ++it) {
				messageW->printf("[#%d] %s\n", iii, it->second->id.toString().c_str());
			}
			completionModeEnter();
		} else {

			it = usersFound.begin();
			chatUser = it->second->id;
			chatNetwork = it->first;
			messageW->printf("Chatting with %s (network %s)\n",
			                 chatUser.toString().c_str(),
							 chatNetwork.c_str());
			statusW->printf("Chatting with %s (network %s)\n",
			                 chatUser.toString().c_str(),
							 chatNetwork.c_str());
			statusW->refresh();
		}
		messageW->refresh();
	}

	if ((len == 4 && data == "/msg") ||
	    (len == 3 && data == "/me")) {
		/* if len == 0 then data == "/" */

		/* trim */
		pos = nextToken(command, pos.second);
		if (pos.first == std::string::npos) {
			/* refuse to send empty line */
			return;
		}

		std::string userString(command, pos.first, pos.second-pos.first);
		pos = nextToken(command, pos.second);
		if (pos.first == std::string::npos) {
			/* no message? */
			return;
		}
		std::string msgString(command, pos.first, pos.second-pos.first);

		std::multimap< std::string, User* >::iterator it;
		findUsers(userString);

		if(usersFound.size() == 0) {
			messageW->printf("No users match to '%s', try /names command.\n", userString.c_str());
		} else if(usersFound.size() > 1) {
			int iii;
			messageW->printf("Ambiguous user '%s', possible matches:\n",
			                 userString.c_str());
			for(iii=1, it=usersFound.begin(); it!=usersFound.end(); ++iii, ++it) {
				messageW->printf("[#%d] %s\n", iii, it->second->id.toString().c_str());
			}
			completionModeEnter();
		} else {

			it = usersFound.begin();
			std::string net = it->first;
			sendSignal("/net/msg/send", net,
			           new sig::MessageData(it->second->id,
		                                std::string(command, pos.first),
		                                len==3?sig::MessageData::ACTION:0));
		}

	} else if (len == 3 && data == "/aw") {
		/* dirty trick */
		pos.second = pos.first + 1;
		goto status;

	} else if (len == 7 && data == "/status") {
	status:
		enum User::State state;
		bool valid;

		pos = nextToken(command, pos.second);
		if (pos.first == std::string::npos) {
			return;
		}

		data.assign(command, pos.first, pos.second - pos.first);
		state = User::getState(data, valid);
		if (!valid) {
			return;
		}

		/* send to all networks */
		std::string net = "/net/";
		pos = nextToken(command, pos.second);
		if (pos.first == std::string::npos) {
			data.clear();
		} else {
			data.assign(command, pos.first, std::string::npos);
		}

		/*
		 * in UserData we must supply valid data only for these
		 * information which has changed (status and/or displayname)
		 */
		sendSignal("/net/status/change", net,
		           new sig::UserData(User("dummy", Address(),
		                                  User::Status(state, data)),
		                             sig::UserData::STATE |
		                             sig::UserData::MESSAGE));
	} else if((len == 3 && data == "/dn")
	|| (len = 12 && data == "/displayname")) {



	} else if(len == 8 && data == "/history") {
		std::list<std::string>::iterator hi;
		int i;

		messageW->printf("Command history:\n");
		for(hi=history.begin(), ++hi, i=1; hi!=history.end(); ++hi, ++i) {
			messageW->printf("[%2d] %s\n", i, hi->c_str());
		}
		messageW->refresh();


	/* ... and so it may go with other commands */
	} else {
		/* print error message -- unkonw command */
	}
}