I know that you can't cast spells that require somatic or
material components while you're being grappled. Suppose
I have the Still Spell feat, or I'm casting a spell that only
has a verbal component, and I cast a spell while being
grappled. Does the opponent who is grappling me get an
attack of opportunity against me when I cast the spell? Do I
provoke an attack of opportunity from a nongrappling
enemy in a square next to me (say the grappler's buddy,
who is standing by to prevent just such a thing)? How many
Concentration checks do I have to make to pull off this
action?
   Actually, you can cast a spell with a material component
while you're being grappled or pinned; you just have to have
the material component in hand before you cast the spell (see
page 137 in the Player's Handbook). Under normal
circumstances, you can get a material component out of a
pocket or a spell component pouch as part of your spellcasting
action. To have a component in your hand when being grappled
or pinned, you either must have previously told the DM that
you were holding the component, or you have to take an action
to retrieve the component. This is similar to retrieving a stored
item, but it is a full-round action when you're grappled. It
provokes an attack of opportunity (but see below), and it's not
possible if you're pinned.
   When you cast the spell (or retrieve the necessary
component), you provoke an attack of opportunity, but not
from the foe who is grappling you. When you're involved in a
grapple, you don't threaten any area (see page 138 in the
Player's Handbook). Any foes who threaten you while you're
being grappled (such as an enemy standing apart from the
grapple) make attacks of opportunity normally against you.
   In this situation, you need to make a Concentration check
with a DC of 20 + the spell level to cast a spell while grappled,
and possibly a second check with a DC of 10 + spell level + the
damage dealt if your grappler's ally hits you with an attack of
opportunity. Make one check and apply the roll to both DCs. If
you aren't successful with both checks, you lose the spell. You
can attempt to cast defensively in this situation; if you do, you
also make one check and compare the result to the defensive
casting (DC 15 + spell level) and the DC for casting while
grappling (DC of 20 + spell level).