joystick related code is delt with in user_routines.c. Each button has it's own variable. Joystick positions have two variables, 1 for the x directions and one for the y directions. The values for these positions are mapped like pwm values: 0 is full one direction, 127 means the joystick is in it's neutral position, and 255 means the joystick is being pushed as far as it goes in the other direction. Because of the nature of the values, putting the line of code:
pwm01 = p1_y;
... would be legit, because when the joystick is just sitting there, it doesn't move, and when the joystick is pushed forward/backward all the way, the motor goes as fast as it goes in the appropriate direction. This setup is a bit twitchy, because slight movements in the joystick gets responses from the motor.
Variable names are in the format *joystick_port_name*_*button/axis*.
This means p2_x is the variable dealing with the joystick plugged into the second control port, and the has to do with the x axis directions.
As for unplugging the joystick in the middle of a competion; don't. It confuses the robot, and bad things happen

.