ISLAMABAD, Pakistan — Pakistan's Supreme Court, stacked with appointees loyal to Gen. Pervez Musharraf, dismissed the last legal challenge to his re-election as president on Thursday, paving the way for his swearing-in to a second five-year term.

Presidential aides said Musharraf would abide by his pledge to step down as head of the army and become a civilian president when he takes the oath of office, which is expected in coming days.

The court decision was the final step in a series of maneuvers devised by Musharraf, including the imposition of emergency rule nearly three weeks ago, to ensure his continuation in power.

It underscored the quandary for those in the opposition, which must decide whether to take part in parliamentary elections Musharraf has announced for Jan. 8, even though they consider his re-election illegitimate. The general scrapped the constitution on Nov. 3 and dismissed a Supreme Court that seemed poised to judge a new term for him illegal.

As the new, more pliant court gave the general the ruling he wanted, aides to one of the country's leading opposition figures, former Prime Minister Nawaz Sharif, said he appeared ready to leave his exile in Saudi Arabia and return home to take part in the parliamentary elections. Sharif, potentially Musharraf's most potent opponent, flew from Jiddah to Riyadh on Thursday to see the Saudi royal family in what appeared to be the final preparations for his homecoming.

Just two days ago, Musharraf flew to Saudi Arabia for a short trip that diplomats described as an effort to persuade the Saudi authorities to keep Sharif from leaving the country.

The two men are bitter enemies. Musharraf deposed Sharif in a bloodless coup in October 1999. Sharif left Pakistan in 2000 under an agreement in which a prison sentence for corruption and hijacking was dropped in exchange for 10 years in exile.

But as political events have unfolded in Pakistan, including the return of another former prime minister, Benazir Bhutto, Sharif has been pushing for the right to make a political comeback. Sharif, who like Bhutto served as prime minister twice and like Bhutto was dismissed twice, returned to Pakistan in September. After four hours on the ground, he was threatened with jail and then deported.