mid-15c. "of or pertaining to the law," from Middle French légal or directly from Latin legalis "legal, pertaining to the law," from lex (genitive legis) "law," possibly related to legere "to gather," on notion of "a collection of rules" (see lecture (n.)).
Sense of "permitted by law" is from 1640s. Related: Legally. The Old French form was leial, loial (see leal, loyal). Legal tender is from 1740.
Loosely used to mean "in accordance with all the relevant rules", especially in connection with some set of constraints defined by software. "The older =+ alternate for += is no longer legal syntax in ANSI C." "This parser processes each line of legal input the moment it sees the trailing linefeed." Hackers often model their work as a sort of game played with the environment in which the objective is to maneuver through the thicket of "natural laws" to achieve a desired objective. Their use of "legal" is flavoured as much by this game-playing sense as by the more conventional one having to do with courts and lawyers. Compare language lawyer, legalese.
[Jargon File]