rc.html 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  1. <html>
  2. <title>
  3. data
  4. </title>
  5. <body BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#330088" ALINK="#FF0044">
  6. <H1>Rc &#173; The Plan 9 Shell
  7. </H1>
  8. <DL><DD><I>Tom Duff<br>
  9. td@plan9.bell-labs.com<br>
  10. </I></DL>
  11. <DL><DD><H4>ABSTRACT</H4>
  12. <I>Rc</I>
  13. is a command interpreter for Plan 9 that
  14. provides similar facilities to UNIX's
  15. Bourne shell,
  16. with some small additions and less idiosyncratic syntax.
  17. This paper uses numerous examples to describe
  18. <I>rc</I>'s
  19. features, and contrasts
  20. <I>rc</I>
  21. with the Bourne shell, a model that many readers will be familiar with.
  22. </DL>
  23. <H4>1 Introduction
  24. </H4>
  25. <P>
  26. <I>Rc</I>
  27. is similar in spirit but different in detail from UNIX's
  28. Bourne shell. This paper describes
  29. <I>rc</I>'s
  30. principal features with many small examples and a few larger ones.
  31. It assumes familiarity with the Bourne shell.
  32. </P>
  33. <H4>2 Simple commands
  34. </H4>
  35. <P>
  36. For the simplest uses
  37. <I>rc</I>
  38. has syntax familiar to Bourne-shell users.
  39. All of the following behave as expected:
  40. <DL><DT><DD><TT><PRE>
  41. date
  42. cat /lib/news/build
  43. who &#62;user.names
  44. who &#62;&#62;user.names
  45. wc &#60;file
  46. echo [a-f]*.c
  47. who | wc
  48. who; date
  49. vc *.c &amp;
  50. mk &amp;&amp; v.out /*/bin/fb/*
  51. rm -r junk || echo rm failed!
  52. </PRE></TT></DL>
  53. </P>
  54. <H4>3 Quotation
  55. </H4>
  56. <P>
  57. An argument that contains a space or one of
  58. <I>rc</I>'s
  59. other syntax characters must be enclosed in apostrophes
  60. (<TT>'</TT>):
  61. <DL><DT><DD><TT><PRE>
  62. rm 'odd file name'
  63. </PRE></TT></DL>
  64. An apostrophe in a quoted argument must be doubled:
  65. <DL><DT><DD><TT><PRE>
  66. echo 'How''s your father?'
  67. </PRE></TT></DL>
  68. </P>
  69. <H4>4 Patterns
  70. </H4>
  71. <P>
  72. An unquoted argument that contains any of the characters
  73. <TT>*</TT>
  74. <TT>?</TT>
  75. <TT>[</TT>
  76. is a pattern to be matched against file names.
  77. A
  78. <TT>*</TT>
  79. character matches any sequence of characters,
  80. <TT>?</TT>
  81. matches any single character, and
  82. <TT>[<I>class</I>]</TT>
  83. matches any character in the
  84. <TT>class</TT>,
  85. unless the first character of
  86. <I>class</I>
  87. is
  88. <TT>~</TT>,
  89. in which case the class is complemented.
  90. The
  91. <I>class</I>
  92. may also contain pairs of characters separated by
  93. <TT>-</TT>,
  94. standing for all characters lexically between the two.
  95. The character
  96. <TT>/</TT>
  97. must appear explicitly in a pattern, as must the path name components
  98. <TT>.</TT>
  99. and
  100. <TT>..</TT>.
  101. A pattern is replaced by a list of arguments, one for each path name matched,
  102. except that a pattern matching no names is not replaced by the empty list;
  103. rather it stands for itself.
  104. </P>
  105. <H4>5 Variables
  106. </H4>
  107. <P>
  108. UNIX's Bourne shell offers string-valued variables.
  109. <I>Rc</I>
  110. provides variables whose values are lists of arguments &#173;
  111. that is, arrays of strings. This is the principal difference
  112. between
  113. <I>rc</I>
  114. and traditional UNIX command interpreters.
  115. Variables may be given values by typing, for example:
  116. <DL><DT><DD><TT><PRE>
  117. path=(. /bin)
  118. user=td
  119. font=/lib/font/bit/pelm/ascii.9.font
  120. </PRE></TT></DL>
  121. The parentheses indicate that the value assigned to
  122. <TT>path</TT>
  123. is a list of two strings. The variables
  124. <TT>user</TT>
  125. and
  126. <TT>font</TT>
  127. are assigned lists containing a single string.
  128. </P>
  129. <P>
  130. The value of a variable can be substituted into a command by
  131. preceding its name with a
  132. <TT>$</TT>,
  133. like this:
  134. <DL><DT><DD><TT><PRE>
  135. echo $path
  136. </PRE></TT></DL>
  137. If
  138. <TT>path</TT>
  139. had been set as above, this would be equivalent to
  140. <DL><DT><DD><TT><PRE>
  141. echo . /bin
  142. </PRE></TT></DL>
  143. Variables may be subscripted by numbers or lists of numbers,
  144. like this:
  145. <DL><DT><DD><TT><PRE>
  146. echo $path(2)
  147. echo $path(2 1 2)
  148. </PRE></TT></DL>
  149. These are equivalent to
  150. <DL><DT><DD><TT><PRE>
  151. echo /bin
  152. echo /bin . /bin
  153. </PRE></TT></DL>
  154. There can be no space separating the variable's name from the
  155. left parenthesis; otherwise, the subscript would be considered
  156. a separate parenthesized list.
  157. </P>
  158. <P>
  159. The number of strings in a variable can be determined by the
  160. <TT>$#</TT>
  161. operator. For example,
  162. <DL><DT><DD><TT><PRE>
  163. echo $#path
  164. </PRE></TT></DL>
  165. would print 2 for this example.
  166. </P>
  167. <P>
  168. The following two assignments are subtly different:
  169. <DL><DT><DD><TT><PRE>
  170. empty=()
  171. null=''
  172. </PRE></TT></DL>
  173. The first sets
  174. <TT>empty</TT>
  175. to a list containing no strings.
  176. The second sets
  177. <TT>null</TT>
  178. to a list containing a single string,
  179. but the string contains no characters.
  180. </P>
  181. <P>
  182. Although these may seem like more or less
  183. the same thing (in Bourne's shell, they are
  184. indistinguishable), they behave differently
  185. in almost all circumstances.
  186. Among other things
  187. <DL><DT><DD><TT><PRE>
  188. echo $#empty
  189. </PRE></TT></DL>
  190. prints 0, whereas
  191. <DL><DT><DD><TT><PRE>
  192. echo $#null
  193. </PRE></TT></DL>
  194. prints 1.
  195. </P>
  196. <P>
  197. All variables that have never been set have the value
  198. <TT>()</TT>.
  199. </P>
  200. <P>
  201. Occasionally, it is convenient to treat a variable's value
  202. as a single string. The elements of a string are concatenated
  203. into a single string, with spaces between the elements, by
  204. the
  205. <TT>$"</TT>
  206. operator.
  207. Thus, if we set
  208. <DL><DT><DD><TT><PRE>
  209. list=(How now brown cow)
  210. string=$"list
  211. </PRE></TT></DL>
  212. then both
  213. <DL><DT><DD><TT><PRE>
  214. echo $list
  215. </PRE></TT></DL>
  216. and
  217. <DL><DT><DD><TT><PRE>
  218. echo $string
  219. </PRE></TT></DL>
  220. cause the same output, viz:
  221. <DL><DT><DD><TT><PRE>
  222. How now brown cow
  223. </PRE></TT></DL>
  224. but
  225. <DL><DT><DD><TT><PRE>
  226. echo $#list $#string
  227. </PRE></TT></DL>
  228. will output
  229. <DL><DT><DD><TT><PRE>
  230. 4 1
  231. </PRE></TT></DL>
  232. because
  233. <TT>$list</TT>
  234. has four members, but
  235. <TT>$string</TT>
  236. has a single member, with three spaces separating its words.
  237. </P>
  238. <H4>6 Arguments
  239. </H4>
  240. <P>
  241. When
  242. <I>rc</I>
  243. is reading its input from a file, the file has access
  244. to the arguments supplied on
  245. <I>rc</I>'s
  246. command line. The variable
  247. <TT>$*</TT>
  248. initially has the list of arguments assigned to it.
  249. The names
  250. <TT>$1</TT>,
  251. <TT>$2</TT>,
  252. etc. are synonyms for
  253. <TT>$*(1)</TT>,
  254. <TT>$*(2)</TT>,
  255. etc.
  256. In addition,
  257. <TT>$0</TT>
  258. is the name of the file from which
  259. <I>rc</I>'s
  260. input is being read.
  261. </P>
  262. <H4>7 Concatenation
  263. </H4>
  264. <P>
  265. <I>Rc</I>
  266. has a string concatenation operator, the caret
  267. <TT>^</TT>,
  268. to build arguments out of pieces.
  269. <DL><DT><DD><TT><PRE>
  270. echo hully^gully
  271. </PRE></TT></DL>
  272. is exactly equivalent to
  273. <DL><DT><DD><TT><PRE>
  274. echo hullygully
  275. </PRE></TT></DL>
  276. Suppose variable
  277. <TT>i</TT>
  278. contains the name of a command.
  279. Then
  280. <DL><DT><DD><TT><PRE>
  281. vc $i^.c
  282. vl -o $1 $i^.v
  283. </PRE></TT></DL>
  284. might compile the command's source code, leaving the
  285. result in the appropriate file.
  286. </P>
  287. <P>
  288. Concatenation distributes over lists. The following
  289. <DL><DT><DD><TT><PRE>
  290. echo (a b c)^(1 2 3)
  291. src=(main subr io)
  292. cc $src^.c
  293. </PRE></TT></DL>
  294. are equivalent to
  295. <DL><DT><DD><TT><PRE>
  296. echo a1 b2 c3
  297. cc main.c subr.c io.c
  298. </PRE></TT></DL>
  299. In detail, the rule is: if both operands of
  300. <TT>^</TT>
  301. are lists of the same non-zero number of strings, they are concatenated
  302. pairwise. Otherwise, if one of the operands is a single string,
  303. it is concatenated with each member of the other operand in turn.
  304. Any other combination of operands is an error.
  305. </P>
  306. <H4>8 Free carets
  307. </H4>
  308. <P>
  309. User demand has dictated that
  310. <I>rc</I>
  311. insert carets in certain places, to make the syntax
  312. look more like the Bourne shell. For example, this:
  313. <DL><DT><DD><TT><PRE>
  314. cc -$flags $stems.c
  315. </PRE></TT></DL>
  316. is equivalent to
  317. <DL><DT><DD><TT><PRE>
  318. cc -^$flags $stems^.c
  319. </PRE></TT></DL>
  320. In general,
  321. <I>rc</I>
  322. will insert
  323. <TT>^</TT>
  324. between two arguments that are not separated by white space.
  325. Specifically, whenever one of
  326. <TT>$'`</TT>
  327. follows a quoted or unquoted word, or an unquoted word follows
  328. a quoted word with no intervening blanks or tabs, an implicit
  329. <TT>^</TT>
  330. is inserted between the two. If an unquoted word immediately following a
  331. <TT>$</TT>
  332. contains a character other than an alphanumeric, underscore or
  333. <TT>*</TT>,
  334. a
  335. <TT>^</TT>
  336. is inserted before the first such character.
  337. </P>
  338. <H4>9 Command substitution
  339. </H4>
  340. <P>
  341. It is often useful to build an argument list from the output of a command.
  342. <I>Rc</I>
  343. allows a command, enclosed in braces and preceded by a left quote,
  344. <TT>`{...}</TT>,
  345. anywhere that an argument is required. The command is executed and its
  346. standard output captured.
  347. The characters stored in the variable
  348. <TT>ifs</TT>
  349. are used to split the output into arguments.
  350. For example,
  351. <DL><DT><DD><TT><PRE>
  352. cat `{ls -tr|sed 10q}
  353. </PRE></TT></DL>
  354. will concatenate the ten oldest files in the current directory in temporal order, given the
  355. default
  356. <TT>ifs</TT>
  357. setting of space, tab, and newline.
  358. </P>
  359. <H4>10 Pipeline branching
  360. </H4>
  361. <P>
  362. The normal pipeline notation is general enough for almost all cases.
  363. Very occasionally it is useful to have pipelines that are not linear.
  364. Pipeline topologies more general than trees can require arbitrarily large pipe buffers,
  365. or worse, can cause deadlock.
  366. <I>Rc</I>
  367. has syntax for some kinds of non-linear but treelike pipelines.
  368. For example,
  369. <DL><DT><DD><TT><PRE>
  370. cmp &#60;{old} &#60;{new}
  371. </PRE></TT></DL>
  372. will regression-test a new version of a command.
  373. <TT><</TT>
  374. or
  375. <TT>></TT>
  376. followed by a command in braces causes the command to be run with
  377. its standard output or input attached to a pipe. The parent command
  378. (<TT>cmp</TT>
  379. in the example)
  380. is started with the other end of the pipe attached to some file descriptor
  381. or other, and with an argument that will connect to the pipe when opened
  382. (e.g.,
  383. <TT>/dev/fd/6</TT>).
  384. Some commands are unprepared to deal with input files that turn out not to be seekable.
  385. For example
  386. <TT>diff</TT>
  387. needs to read its input twice.
  388. </P>
  389. <H4>11 Exit status
  390. </H4>
  391. <P>
  392. When a command exits it returns status to the program that executed it.
  393. On Plan 9 status is a character string describing an error condition.
  394. On normal termination it is empty.
  395. </P>
  396. <P>
  397. <I>Rc</I>
  398. captures command exit status in the variable
  399. <TT>$status</TT>.
  400. For a simple command the value of
  401. <TT>$status</TT>
  402. is just as described above. For a pipeline
  403. <TT>$status</TT>
  404. is set to the concatenation of the statuses of the pipeline components with
  405. <TT>|</TT>
  406. characters for separators.
  407. </P>
  408. <P>
  409. <I>Rc</I>
  410. has a several kinds of control flow,
  411. many of them conditioned by the status returned from previously
  412. executed commands. Any
  413. <TT>$status</TT>
  414. containing only
  415. <TT>0</TT>'s
  416. and
  417. <TT>|</TT>'s
  418. has boolean value
  419. <I>true</I>.
  420. Any other status is
  421. <I>false</I>.
  422. </P>
  423. <H4>12 Command grouping
  424. </H4>
  425. <P>
  426. A sequence of commands enclosed in
  427. <TT>{}</TT>
  428. may be used anywhere a command is required.
  429. For example:
  430. <DL><DT><DD><TT><PRE>
  431. {sleep 3600;echo 'Time''s up!'}&amp;
  432. </PRE></TT></DL>
  433. will wait an hour in the background, then print a message.
  434. Without the braces,
  435. <DL><DT><DD><TT><PRE>
  436. sleep 3600;echo 'Time''s up!'&amp;
  437. </PRE></TT></DL>
  438. would lock up the terminal for an hour,
  439. then print the message in the background.
  440. </P>
  441. <H4>13 Control flow &#173; <TT>for</TT>
  442. </H4>
  443. <P>
  444. A command may be executed once for each member of a list
  445. by typing, for example:
  446. <DL><DT><DD><TT><PRE>
  447. for(i in printf scanf putchar) look $i /usr/td/lib/dw.dat
  448. </PRE></TT></DL>
  449. This looks for each of the words
  450. <TT>printf</TT>,
  451. <TT>scanf</TT>
  452. and
  453. <TT>putchar</TT>
  454. in the given file.
  455. The general form is
  456. <DL><DT><DD><TT><PRE>
  457. for(<I>name</I> in <I>list</I>) <I>command</I>
  458. </PRE></TT></DL>
  459. or
  460. <DL><DT><DD><TT><PRE>
  461. for(<I>name</I>) <I>command</I>
  462. </PRE></TT></DL>
  463. In the first case
  464. <I>command</I>
  465. is executed once for each member of
  466. <I>list</I>
  467. with that member assigned to variable
  468. <I>name</I>.
  469. If the clause
  470. ``<TT>in</TT>
  471. <I>list</I>''
  472. is missing,
  473. ``<TT>in</TT>
  474. <TT>$*</TT>''
  475. is assumed.
  476. </P>
  477. <H4>14 Conditional execution &#173; <TT>if</TT>
  478. </H4>
  479. <P>
  480. <I>Rc</I>
  481. also provides a general if-statement. For example:
  482. <DL><DT><DD><TT><PRE>
  483. for(i in *.c) if(cpp $i &#62;/tmp/$i) vc /tmp/$i
  484. </PRE></TT></DL>
  485. runs the C compiler on each C source program that
  486. cpp processes without error.
  487. An `if not' statement provides a two-tailed conditional.
  488. For example:
  489. <DL><DT><DD><TT><PRE>
  490. for(i){
  491. if(test -f /tmp/$i) echo $i already in /tmp
  492. if not cp $i /tmp
  493. }
  494. </PRE></TT></DL>
  495. This loops over each file in
  496. <TT>$*</TT>,
  497. copying to
  498. <TT>/tmp</TT>
  499. those that do not already appear there, and
  500. printing a message for those that do.
  501. </P>
  502. <H4>15 Control flow &#173; <TT>while</TT>
  503. </H4>
  504. <P>
  505. <I>Rc</I>'s
  506. while statement looks like this:
  507. <DL><DT><DD><TT><PRE>
  508. while(newer subr.v subr.c) sleep 5
  509. </PRE></TT></DL>
  510. This waits until
  511. <TT>subr.v</TT>
  512. is newer than
  513. <TT>subr.c</TT>,
  514. presumably because the C compiler finished with it.
  515. </P>
  516. <P>
  517. If the controlling command is empty, the loop will not terminate.
  518. Thus,
  519. <DL><DT><DD><TT><PRE>
  520. while() echo y
  521. </PRE></TT></DL>
  522. emulates the
  523. <I>yes</I>
  524. command.
  525. </P>
  526. <H4>16 Control flow &#173; <TT>switch</TT>
  527. </H4>
  528. <P>
  529. <I>Rc</I>
  530. provides a switch statement to do pattern-matching on
  531. arbitrary strings. Its general form is
  532. <DL><DT><DD><TT><PRE>
  533. switch(<I>word</I>){
  534. case <I>pattern ...</I>
  535. <I>commands</I>
  536. case <I>pattern ...</I>
  537. <I>commands</I>
  538. ...
  539. }
  540. </PRE></TT></DL>
  541. <I>Rc</I>
  542. attempts to match the word against the patterns in each case statement in turn.
  543. Patterns are the same as for filename matching, except that
  544. <TT>/</TT>
  545. and
  546. <TT>.</TT>
  547. and
  548. <TT>..</TT>
  549. need not be matched explicitly.
  550. </P>
  551. <P>
  552. If any pattern matches, the
  553. commands following that case up to
  554. the next case (or the end of the switch)
  555. are executed, and execution of the switch
  556. is complete. For example,
  557. <DL><DT><DD><TT><PRE>
  558. switch($#*){
  559. case 1
  560. cat &#62;&#62;$1
  561. case 2
  562. cat &#62;&#62;$2 &#60;$1
  563. case *
  564. echo 'Usage: append [from] to'
  565. }
  566. </PRE></TT></DL>
  567. is an append command. Called with one file argument,
  568. it appends its standard input to the named file. With two, the
  569. first is appended to the second. Any other number
  570. elicits an error message.
  571. </P>
  572. <P>
  573. The built-in
  574. <TT>~</TT>
  575. command also matches patterns, and is often more concise than a switch.
  576. Its arguments are a string and a list of patterns. It sets
  577. <TT>$status</TT>
  578. to true if and only if any of the patterns matches the string.
  579. The following example processes option arguments for the
  580. <A href="/magic/man2html/1/man"><I>man</I>(1)
  581. </A>command:
  582. <DL><DT><DD><TT><PRE>
  583. opt=()
  584. while(~ $1 -* [1-9] 10){
  585. switch($1){
  586. case [1-9] 10
  587. sec=$1 secn=$1
  588. case -f
  589. c=f s=f
  590. case -[qwnt]
  591. cmd=$1
  592. case -T*
  593. T=$1
  594. case -*
  595. opt=($opt $1)
  596. }
  597. shift
  598. }
  599. </PRE></TT></DL>
  600. </P>
  601. <H4>17 Functions
  602. </H4>
  603. <P>
  604. Functions may be defined by typing
  605. <DL><DT><DD><TT><PRE>
  606. fn <I>name</I> { <I>commands</I> }
  607. </PRE></TT></DL>
  608. Subsequently, whenever a command named
  609. <I>name</I>
  610. is encountered, the remainder of the command's
  611. argument list will assigned to
  612. <TT>$*</TT>
  613. and
  614. <I>rc</I>
  615. will execute the
  616. <I>commands</I>.
  617. The value of
  618. <TT>$*</TT>
  619. will be restored on completion.
  620. For example:
  621. <DL><DT><DD><TT><PRE>
  622. fn g {
  623. grep $1 *.[hcyl]
  624. }
  625. </PRE></TT></DL>
  626. defines
  627. <TT>g</TT><I> pattern</I>n(Sf
  628. to look for occurrences of
  629. <I>pattern</I>
  630. in all program source files in the current directory.
  631. </P>
  632. <P>
  633. Function definitions are deleted by writing
  634. <DL><DT><DD><TT><PRE>
  635. fn <I>name</I>
  636. </PRE></TT></DL>
  637. with no function body.
  638. </P>
  639. <H4>18 Command execution
  640. </H4>
  641. <P>
  642. <I>Rc</I>
  643. does one of several things to execute a simple command.
  644. If the command name is the name of a function defined using
  645. <TT>fn</TT>,
  646. the function is executed.
  647. Otherwise, if it is the name of a built-in command, the
  648. built-in is executed directly by
  649. <I>rc</I>.
  650. Otherwise, directories mentioned in the variable
  651. <TT>$path</TT>
  652. are searched until an executable file is found.
  653. Extensive use of the
  654. <TT>$path</TT>
  655. variable is discouraged in Plan 9. Instead, use the default
  656. <TT>(.</TT>
  657. <TT>/bin)</TT>
  658. and bind what you need into
  659. <TT>/bin</TT>.
  660. </P>
  661. <H4>19 Built-in commands
  662. </H4>
  663. <P>
  664. Several commands are executed internally by
  665. <I>rc</I>
  666. because they are difficult to implement otherwise.
  667. </P>
  668. <DL COMPACT>
  669. <DT></TT><TT>. [-i] <I>file ...</I><TT></TT><DD>
  670. Execute commands from
  671. </TT><I>file</I><TT>.
  672. </TT><TT>$*</TT><TT>
  673. is set for the duration to the reminder of the argument list following
  674. </TT><I>file</I><TT>.
  675. </TT><TT>$path</TT><TT>
  676. is used to search for
  677. </TT><I>file</I><TT>.
  678. Option
  679. </TT><TT>-i</TT><TT>
  680. indicates interactive input &#173; a prompt
  681. (found in
  682. </TT><TT>$prompt</TT><TT>)
  683. is printed before each command is read.
  684. <DT></TT><TT>builtin </TT><I>command ...</I><TT></TT><DD>
  685. Execute
  686. </TT><I>command</I><TT>
  687. as usual except that any function named
  688. </TT><I>command</I><TT>
  689. is ignored.
  690. For example,
  691. <DL><DT><DD><TT><PRE>
  692. fn cd{
  693. builtin cd $* &amp;&amp; pwd
  694. }
  695. </PRE></TT></DL>
  696. defines a replacement for the
  697. </TT><TT>cd</TT><TT>
  698. built-in (see below) that announces the full name of the new directory.
  699. <DT></TT><TT>cd [</TT><I>dir</I><TT>]</TT><DD>
  700. Change the current directory to
  701. </TT><I>dir</I><TT>.
  702. The default argument is
  703. </TT><TT>$home</TT><TT>.
  704. </TT><TT>$cdpath</TT><TT>
  705. is a list of places in which to search for
  706. </TT><I>dir</I><TT>.
  707. <DT></TT><TT>eval [</TT><I>arg ...</I><TT>]</TT><DD>
  708. The arguments are concatenated (separated by spaces) into a string, read as input to
  709. </TT><I>rc</I><TT>,
  710. and executed. For example,
  711. <DL><DT><DD><TT><PRE>
  712. x='$y'
  713. y=Doody
  714. eval echo Howdy, $x
  715. </PRE></TT></DL>
  716. would echo
  717. <DL><DT><DD><TT><PRE>
  718. Howdy, Doody
  719. </PRE></TT></DL>
  720. since the arguments of
  721. </TT><TT>eval</TT><TT>
  722. would be
  723. <DL><DT><DD><TT><PRE>
  724. echo Howdy, $y
  725. </PRE></TT></DL>
  726. after substituting for
  727. </TT><TT>$x</TT><TT>.
  728. <DT></TT><TT>exec [</TT><I>command ...</I><TT>]</TT><DD>
  729. </TT><I>Rc</I><TT>
  730. replaces itself with the given
  731. </TT><I>command</I><TT>.
  732. This is like a
  733. </TT><I>goto</I><TT>
  734. &#173;
  735. </TT><I>rc</I><TT>
  736. does not wait for the command to exit, and does not return to read any more commands.
  737. <DT></TT><TT>exit [</TT><I>status</I><TT>]</TT><DD>
  738. </TT><I>Rc</I><TT>
  739. exits immediately with the given status. If none is given, the current value of
  740. </TT><TT>$status</TT><TT>
  741. is used.
  742. <DT></TT><TT>flag </TT><I>f</I><TT> [+-]</TT><DD>
  743. This command manipulates and tests the command line flags (described below).
  744. <DL><DT><DD><TT><PRE>
  745. flag </TT><I>f</I><TT> +
  746. </PRE></TT></DL>
  747. sets flag
  748. </TT><I>f</I><TT>.
  749. <DL><DT><DD><TT><PRE>
  750. flag </TT><I>f</I><TT> -
  751. </PRE></TT></DL>
  752. clears flag
  753. </TT><I>f</I><TT>.
  754. <DL><DT><DD><TT><PRE>
  755. flag </TT><I>f</I><TT>
  756. </PRE></TT></DL>
  757. tests flag
  758. </TT><I>f</I><TT>,
  759. setting
  760. </TT><TT>$status</TT><TT>
  761. appropriately.
  762. Thus
  763. <DL><DT><DD><TT><PRE>
  764. if(flag x) flag v +
  765. </PRE></TT></DL>
  766. sets the
  767. </TT><TT>-v</TT><TT>
  768. flag if the
  769. </TT><TT>-x</TT><TT>
  770. flag is already set.
  771. <DT></TT><TT>rfork [nNeEsfF]</TT><DD>
  772. This uses the Plan 9
  773. </TT><I>rfork</I><TT>
  774. system entry to put
  775. </TT><I>rc</I><TT>
  776. into a new process group with the following attributes:
  777. <br><img src="data.11230.gif"><br>
  778. Section
  779. <A href="/magic/man2html/2/fork"></TT><I>fork</I><TT>(2)
  780. </A>of the Programmer's Manual describes these attributes in more detail.
  781. <DT></TT><TT>shift [</TT><I>n</I><TT>]</TT><DD>
  782. Delete the first
  783. </TT><I>n</I><TT>
  784. (default 1) elements of
  785. </TT><TT>$*</TT><TT>.
  786. <DT></TT><TT>wait [</TT><I>pid</I><TT>]</TT><DD>
  787. Wait for the process with the given
  788. </TT><I>pid</I><TT>
  789. to exit. If no
  790. </TT><I>pid</I><TT>
  791. is given, all outstanding processes are waited for.
  792. <DT></TT><TT>whatis </TT><I>name ...</I><TT></TT><DD>
  793. Print the value of each
  794. </TT><I>name</I><TT>
  795. in a form suitable for input to
  796. </TT><I>rc</I><TT>.
  797. The output is an assignment to a variable, the definition of a function,
  798. a call to
  799. </TT><TT>builtin</TT><TT>
  800. for a built-in command, or the path name of a binary program.
  801. For example,
  802. <DL><DT><DD><TT><PRE>
  803. whatis path g cd who
  804. </PRE></TT></DL>
  805. might print
  806. <DL><DT><DD><TT><PRE>
  807. path=(. /bin)
  808. fn g {gre -e $1 *.[hycl]}
  809. builtin cd
  810. /bin/who
  811. </PRE></TT></DL>
  812. <DT></I><TT>~ </TT><I>subject pattern ...</TT><DD>
  813. The
  814. </TT><I>subject</I><TT>
  815. is matched against each
  816. </TT><I>pattern</I><TT>
  817. in turn. On a match,
  818. </TT><TT>$status</TT><TT>
  819. is set to true.
  820. Otherwise, it is set to
  821. </TT><TT>'no match'</TT><TT>.
  822. Patterns are the same as for filename matching.
  823. The
  824. </TT><I>patterns</I><TT>
  825. are not subjected to filename replacement before the
  826. </TT><TT>~</TT><TT>
  827. command is executed, so they need not be enclosed in
  828. quotation marks, unless of course, a literal match for
  829. </TT><TT>*</TT><TT>
  830. </TT><TT>[</TT><TT>
  831. or
  832. </TT><TT>?</TT><TT>
  833. is required.
  834. For example
  835. <DL><DT><DD><TT><PRE>
  836. ~ $1 ?
  837. </PRE></TT></DL>
  838. matches any single character, whereas
  839. <DL><DT><DD><TT><PRE>
  840. ~ $1 '?'
  841. </PRE></TT></DL>
  842. only matches a literal question mark.
  843. </dl>
  844. </TT><H4>20 Advanced I/O Redirection
  845. </H4>
  846. <P>
  847. <I>Rc</I>
  848. allows redirection of file descriptors other than 0 and 1
  849. (standard input and output) by specifying the file descriptor
  850. in square brackets
  851. <TT>[ ]</TT>
  852. after the
  853. <TT><</TT>
  854. or
  855. <TT>></TT>.
  856. For example,
  857. <DL><DT><DD><TT><PRE>
  858. vc junk.c &#62;[2]junk.diag
  859. </PRE></TT></DL>
  860. saves the compiler's diagnostics from standard error in
  861. <TT>junk.diag</TT>.
  862. </P>
  863. <P>
  864. File descriptors may be replaced by a copy, in the sense of
  865. <A href="/magic/man2html/2/dup"><I>dup</I>(2),
  866. </A>of an already-open file by typing, for example
  867. <DL><DT><DD><TT><PRE>
  868. vc junk.c &#62;[2=1]
  869. </PRE></TT></DL>
  870. This replaces file descriptor 2 with a copy of file descriptor 1.
  871. It is more useful in conjunction with other redirections, like this
  872. <DL><DT><DD><TT><PRE>
  873. vc junk.c &#62;junk.out &#62;[2=1]
  874. </PRE></TT></DL>
  875. Redirections are evaluated from left to right, so this redirects
  876. file descriptor 1 to
  877. <TT>junk.out</TT>,
  878. then points file descriptor 2 at the same file.
  879. By contrast,
  880. <DL><DT><DD><TT><PRE>
  881. vc junk.c &#62;[2=1] &#62;junk.out
  882. </PRE></TT></DL>
  883. redirects file descriptor 2 to a copy of file descriptor 1
  884. (presumably the terminal), and then directs file descriptor 1
  885. to a file. In the first case, standard and diagnostic output
  886. will be intermixed in
  887. <TT>junk.out</TT>.
  888. In the second, diagnostic output will appear on the terminal,
  889. and standard output will be sent to the file.
  890. </P>
  891. <P>
  892. File descriptors may be closed by using the duplication notation
  893. with an empty right-hand side.
  894. For example,
  895. <DL><DT><DD><TT><PRE>
  896. vc junk.c &#62;[2=]
  897. </PRE></TT></DL>
  898. will discard diagnostics from the compilation.
  899. </P>
  900. <P>
  901. Arbitrary file descriptors may be sent through
  902. a pipe by typing, for example,
  903. <DL><DT><DD><TT><PRE>
  904. vc junk.c |[2] grep -v '^$'
  905. </PRE></TT></DL>
  906. This deletes blank lines
  907. from the C compiler's error output. Note that the output
  908. of
  909. <TT>grep</TT>
  910. still appears on file descriptor 1.
  911. </P>
  912. <P>
  913. Occasionally you may wish to connect the input side of
  914. a pipe to some file descriptor other than zero.
  915. The notation
  916. <DL><DT><DD><TT><PRE>
  917. cmd1 |[5=19] cmd2
  918. </PRE></TT></DL>
  919. creates a pipeline with
  920. <TT>cmd1</TT>'s
  921. file descriptor 5 connected through a pipe to
  922. <TT>cmd2</TT>'s
  923. file descriptor 19.
  924. </P>
  925. <H4>21 Here documents
  926. </H4>
  927. <P>
  928. <I>Rc</I>
  929. procedures may include data, called ``here documents'',
  930. to be provided as input to commands, as in this version of the
  931. <I>tel</I>
  932. command
  933. <DL><DT><DD><TT><PRE>
  934. for(i) grep $i &#60;&#60;!
  935. ...
  936. tor 2T-402 2912
  937. kevin 2C-514 2842
  938. bill 2C-562 7214
  939. ...
  940. !
  941. </PRE></TT></DL>
  942. A here document is introduced by the redirection symbol
  943. <TT><<</TT>,
  944. followed by an arbitrary EOF marker
  945. (<TT>!</TT>
  946. in the example). Lines following the command,
  947. up to a line containing only the EOF marker are saved
  948. in a temporary file that is connected to the command's
  949. standard input when it is run.
  950. </P>
  951. <P>
  952. <I>Rc</I>
  953. does variable substitution in here documents. The following command:
  954. <DL><DT><DD><TT><PRE>
  955. ed $3 &#60;&#60;EOF
  956. g/$1/s//$2/g
  957. w
  958. EOF
  959. </PRE></TT></DL>
  960. changes all occurrences of
  961. <TT>$1</TT>
  962. to
  963. <TT>$2</TT>
  964. in file
  965. <TT>$3</TT>.
  966. To include a literal
  967. <TT>$</TT>
  968. in a here document, type
  969. <TT>$$</TT>.
  970. If the name of a variable is followed immediately by
  971. <TT>^</TT>,
  972. the caret is deleted.
  973. </P>
  974. <P>
  975. Variable substitution can be entirely suppressed by enclosing
  976. the EOF marker following
  977. <TT><<</TT>
  978. in quotation marks, as in
  979. <TT><<'EOF'</TT>.
  980. </P>
  981. <P>
  982. Here documents may be provided on file descriptors other than 0 by typing, for example,
  983. <DL><DT><DD><TT><PRE>
  984. cmd &#60;&#60;[4]End
  985. ...
  986. End
  987. </PRE></TT></DL>
  988. </P>
  989. <P>
  990. If a here document appears within a compound block, the contents of the document
  991. must be after the whole block:
  992. <DL><DT><DD><TT><PRE>
  993. for(i in $*){
  994. mail $i &#60;&#60;EOF
  995. }
  996. words to live by
  997. EOF
  998. </PRE></TT></DL>
  999. </P>
  1000. <H4>22 Catching Notes
  1001. </H4>
  1002. <P>
  1003. <I>Rc</I>
  1004. scripts normally terminate when an interrupt is received from the terminal.
  1005. A function with the name of a UNIX signal, in lower case, is defined in the usual way,
  1006. but called when
  1007. <I>rc</I>
  1008. receives the corresponding note.
  1009. The
  1010. <A href="/magic/man2html/2/notify"><I>notify</I>(2)
  1011. </A>section of the Programmer's Manual discusses notes in some detail.
  1012. Notes of interest are:
  1013. </P>
  1014. <DL COMPACT>
  1015. <DT><TT>sighup</TT><DD>
  1016. The note was `hangup'.
  1017. Plan 9 sends this when the terminal has disconnected from
  1018. <I>rc</I>.
  1019. <DT><TT>sigint</TT><DD>
  1020. The note was `interrupt', usually sent when
  1021. the interrupt character (ASCII DEL) is typed on the terminal.
  1022. <DT><TT>sigterm</TT><DD>
  1023. The note was `kill', normally sent by
  1024. <A href="/magic/man2html/1/kill"><I>kill</I>(1).
  1025. </A><DT><TT>sigexit</TT><DD>
  1026. An artificial note sent when
  1027. <I>rc</I>
  1028. is about to exit.
  1029. </dl>
  1030. <P>
  1031. As an example,
  1032. <DL><DT><DD><TT><PRE>
  1033. fn sigint{
  1034. rm /tmp/junk
  1035. exit
  1036. }
  1037. </PRE></TT></DL>
  1038. sets a trap for the keyboard interrupt that
  1039. removes a temporary file before exiting.
  1040. </P>
  1041. <P>
  1042. Notes will be ignored if the note routine is set to
  1043. <TT>{}</TT>.
  1044. Signals revert to their default behavior when their handlers'
  1045. definitions are deleted.
  1046. </P>
  1047. <H4>23 Environment
  1048. </H4>
  1049. <P>
  1050. The environment is a list of name-value pairs made available to
  1051. executing binaries.
  1052. On Plan 9, the environment is stored in a file system named
  1053. <TT>#e</TT>,
  1054. normally mounted on
  1055. <TT>/env</TT>.
  1056. The value of each variable is stored in a separate file, with components
  1057. terminated by zero bytes.
  1058. (The file system is
  1059. maintained entirely in core, so no disk or network access is involved.)
  1060. The contents of
  1061. <TT>/env</TT>
  1062. are shared on a per-process group basis - when a new process group is
  1063. created it effectively attaches
  1064. <TT>/env</TT>
  1065. to a new file system initialized with a copy of the old one.
  1066. A consequence of this organization is that commands can change environment
  1067. entries and see the changes reflected in
  1068. <I>rc</I>.
  1069. </P>
  1070. <P>
  1071. Functions also appear in the environment, named by prefixing
  1072. <TT>fn#</TT>
  1073. to their names, like
  1074. <TT>/env/fn#roff</TT>.
  1075. </P>
  1076. <H4>24 Local Variables
  1077. </H4>
  1078. <P>
  1079. It is often useful to set a variable for the duration
  1080. of a single command. An assignment followed by a command
  1081. has this effect. For example
  1082. <DL><DT><DD><TT><PRE>
  1083. a=global
  1084. a=local echo $a
  1085. echo $a
  1086. </PRE></TT></DL>
  1087. will print
  1088. <DL><DT><DD><TT><PRE>
  1089. local
  1090. global
  1091. </PRE></TT></DL>
  1092. This works even for compound commands, like
  1093. <DL><DT><DD><TT><PRE>
  1094. f=/fairly/long/file/name {
  1095. { wc $f; spell $f; diff $f.old $f } |
  1096. pr -h 'Facts about '$f | lp -dfn
  1097. }
  1098. </PRE></TT></DL>
  1099. </P>
  1100. <H4>25 Examples &#173; <I>cd, pwd</I>
  1101. </H4>
  1102. <P>
  1103. Here is a pair of functions that provide
  1104. enhanced versions of the standard
  1105. <TT>cd</TT>
  1106. and
  1107. <TT>pwd</TT>
  1108. commands. (Thanks to Rob Pike for these.)
  1109. <DL><DT><DD><TT><PRE>
  1110. ps1='% ' # default prompt
  1111. tab=' ' # a tab character
  1112. fn cd{
  1113. builtin cd $1 &amp;&amp;
  1114. switch($#*){
  1115. case 0
  1116. dir=$home
  1117. prompt=($ps1 $tab)
  1118. case *
  1119. switch($1)
  1120. case /*
  1121. dir=$1
  1122. prompt=(`{basename `{pwd}}^$ps1 $tab)
  1123. case */* ..*
  1124. dir=()
  1125. prompt=(`{basename `{pwd}}^$ps1 $tab)
  1126. case *
  1127. dir=()
  1128. prompt=($1^$ps1 $tab)
  1129. }
  1130. }
  1131. }
  1132. fn pwd{
  1133. if(~ $#dir 0)
  1134. dir=`{/bin/pwd}
  1135. echo $dir
  1136. }
  1137. </PRE></TT></DL>
  1138. Function
  1139. <TT>pwd</TT>
  1140. is a version of the standard
  1141. <TT>pwd</TT>
  1142. that caches its value in variable
  1143. <TT>$dir</TT>,
  1144. because the genuine
  1145. <TT>pwd</TT>
  1146. can be quite slow to execute.
  1147. (Recent versions of Plan 9 have very fast implementations of
  1148. <TT>pwd</TT>,
  1149. reducing the advantage of the
  1150. <TT>pwd</TT>
  1151. function.)
  1152. </P>
  1153. <P>
  1154. Function
  1155. <TT>cd</TT>
  1156. calls the
  1157. <TT>cd</TT>
  1158. built-in, and checks that it was successful.
  1159. If so, it sets
  1160. <TT>$dir</TT>
  1161. and
  1162. <TT>$prompt</TT>.
  1163. The prompt will include the last component of the
  1164. current directory (except in the home directory,
  1165. where it will be null), and
  1166. <TT>$dir</TT>
  1167. will be reset either to the correct value or to
  1168. <TT>()</TT>,
  1169. so that the
  1170. <TT>pwd</TT>
  1171. function will work correctly.
  1172. </P>
  1173. <H4>26 Examples &#173; <I>man</I>
  1174. </H4>
  1175. <P>
  1176. The
  1177. <I>man</I>
  1178. command prints pages of the Programmer's Manual.
  1179. It is called, for example, as
  1180. <DL><DT><DD><TT><PRE>
  1181. man 2 sinh
  1182. man rc
  1183. man -t cat
  1184. </PRE></TT></DL>
  1185. In the first case, the page for
  1186. <I>sinh</I>
  1187. in section 2 is printed.
  1188. In the second case, the manual page for
  1189. <I>rc</I>
  1190. is printed. Since no manual section is specified,
  1191. all sections are searched for the page, and it is found
  1192. in section 1.
  1193. In the third case, the page for
  1194. <I>cat</I>
  1195. is typeset (the
  1196. <TT>-t</TT>
  1197. option).
  1198. <DL><DT><DD><TT><PRE>
  1199. cd /sys/man || {
  1200. echo $0: No manual! &#62;[1=2]
  1201. exit 1
  1202. }
  1203. NT=n # default nroff
  1204. s='*' # section, default try all
  1205. for(i) switch($i){
  1206. case -t
  1207. NT=t
  1208. case -n
  1209. NT=n
  1210. case -*
  1211. echo Usage: $0 '[-nt] [section] page ...' &#62;[1=2]
  1212. exit 1
  1213. case [1-9] 10
  1214. s=$i
  1215. case *
  1216. eval 'pages='$s/$i
  1217. for(page in $pages){
  1218. if(test -f $page)
  1219. $NT^roff -man $page
  1220. if not
  1221. echo $0: $i not found &#62;[1=2]
  1222. }
  1223. }
  1224. </PRE></TT></DL>
  1225. Note the use of
  1226. <TT>eval</TT>
  1227. to make a list of candidate manual pages.
  1228. Without
  1229. <TT>eval</TT>,
  1230. the
  1231. <TT>*</TT>
  1232. stored in
  1233. <TT>$s</TT>
  1234. would not trigger filename matching
  1235. &#173; it's enclosed in quotation marks,
  1236. and even if it weren't, it would be expanded
  1237. when assigned to
  1238. <TT>$s</TT>.
  1239. Eval causes its arguments
  1240. to be re-processed by
  1241. <I>rc</I>'s
  1242. parser and interpreter, effectively delaying
  1243. evaluation of the
  1244. <TT>*</TT>
  1245. until the assignment to
  1246. <TT>$pages</TT>.
  1247. </P>
  1248. <H4>27 Examples &#173; <I>holmdel</I>
  1249. </H4>
  1250. <P>
  1251. The following
  1252. <I>rc</I>
  1253. script plays the deceptively simple game
  1254. <I>holmdel</I>,
  1255. in which the players alternately name Bell Labs locations,
  1256. the winner being the first to mention Holmdel.
  1257. <DL><DT><DD><TT><PRE>
  1258. t=/tmp/holmdel$pid
  1259. fn read{
  1260. $1=`{awk '{print;exit}'}
  1261. }
  1262. ifs='
  1263. ' # just a newline
  1264. fn sigexit sigint sigquit sighup{
  1265. rm -f $t
  1266. exit
  1267. }
  1268. cat &#60;&#60;'!' &#62;$t
  1269. Allentown
  1270. Atlanta
  1271. Cedar Crest
  1272. Chester
  1273. Columbus
  1274. Elmhurst
  1275. Fullerton
  1276. Holmdel
  1277. Indian Hill
  1278. Merrimack Valley
  1279. Morristown
  1280. Neptune
  1281. Piscataway
  1282. Reading
  1283. Short Hills
  1284. South Plainfield
  1285. Summit
  1286. Whippany
  1287. West Long Branch
  1288. !
  1289. while(){
  1290. lab=`{fortune $t}
  1291. echo $lab
  1292. if(~ $lab Holmdel){
  1293. echo You lose.
  1294. exit
  1295. }
  1296. while(read lab; ! grep -i -s $lab $t) echo No such location.
  1297. if(~ $lab [hH]olmdel){
  1298. echo You win.
  1299. exit
  1300. }
  1301. }
  1302. </PRE></TT></DL>
  1303. </P>
  1304. <P>
  1305. This script is worth describing in detail
  1306. (rather, it would be if it weren't so silly.)
  1307. </P>
  1308. <P>
  1309. Variable
  1310. <TT>$t</TT>
  1311. is an abbreviation for the name of a temporary file.
  1312. Including
  1313. <TT>$pid</TT>,
  1314. initialized by
  1315. <I>rc</I>
  1316. to its process-id,
  1317. in the names of temporary files insures that their
  1318. names won't collide, in case more than one instance
  1319. of the script is running at a time.
  1320. </P>
  1321. <P>
  1322. Function
  1323. <TT>read</TT>'s
  1324. argument is the name of a variable into which a
  1325. line gathered from standard input is read.
  1326. <TT>$ifs</TT>
  1327. is set to just a newline. Thus
  1328. <TT>read</TT>'s
  1329. input is not split apart at spaces, but the terminating
  1330. newline is deleted.
  1331. </P>
  1332. <P>
  1333. A handler is set to catch
  1334. <TT>sigint</TT>,
  1335. <TT>sigquit</TT>,
  1336. and
  1337. <TT>sighup,</TT>
  1338. and the artificial
  1339. <TT>sigexit</TT>
  1340. signal. It just removes the temporary file and exits.
  1341. </P>
  1342. <P>
  1343. The temporary file is initialized from a here
  1344. document containing a list of Bell Labs locations, and
  1345. the main loop starts.
  1346. </P>
  1347. <P>
  1348. First, the program guesses a location (in
  1349. <TT>$lab</TT>)
  1350. using the
  1351. <TT>fortune</TT>
  1352. program to pick a random line from the location list.
  1353. It prints the location, and if it guessed Holmdel, prints
  1354. a message and exits.
  1355. </P>
  1356. <P>
  1357. Then it uses the
  1358. <TT>read</TT>
  1359. function to get lines from standard input and validity-check
  1360. them until it gets a legal name.
  1361. Note that the condition part of a
  1362. <TT>while</TT>
  1363. can be a compound command. Only the exit status of the
  1364. last command in the sequence is checked.
  1365. </P>
  1366. <P>
  1367. Again, if the result
  1368. is Holmdel, it prints a message and exits.
  1369. Otherwise it goes back to the top of the loop.
  1370. </P>
  1371. <H4>28 Design Principles
  1372. </H4>
  1373. <P>
  1374. <I>Rc</I>
  1375. draws heavily from Steve Bourne's
  1376. <TT>/bin/sh</TT>.
  1377. Any successor of the Bourne shell is bound to
  1378. suffer in comparison. I have tried to fix its
  1379. best-acknowledged shortcomings and to simplify things
  1380. wherever possible, usually by omitting inessential features.
  1381. Only when irresistibly tempted have I introduced novel ideas.
  1382. Obviously I have tinkered extensively with Bourne's syntax.
  1383. </P>
  1384. <P>
  1385. The most important principle in
  1386. <I>rc</I>'s
  1387. design is that it's not a macro processor. Input is never
  1388. scanned more than once by the lexical and syntactic analysis
  1389. code (except, of course, by the
  1390. <TT>eval</TT>
  1391. command, whose
  1392. <I>raison d'&ecirc;tre</I>
  1393. is to break the rule).
  1394. </P>
  1395. <P>
  1396. Bourne shell scripts can often be made
  1397. to run wild by passing them arguments containing spaces.
  1398. These will be split into multiple arguments using
  1399. <TT>IFS</TT>,
  1400. often at inopportune times.
  1401. In
  1402. <I>rc</I>,
  1403. values of variables, including command line arguments, are not re-read
  1404. when substituted into a command.
  1405. Arguments have presumably been scanned in the parent process, and ought
  1406. not to be re-read.
  1407. </P>
  1408. <P>
  1409. Why does Bourne re-scan commands after variable substitution?
  1410. He needs to be able to store lists of arguments in variables whose values are
  1411. character strings.
  1412. If we eliminate re-scanning, we must change the type of variables, so that
  1413. they can explicitly carry lists of strings.
  1414. </P>
  1415. <P>
  1416. This introduces some
  1417. conceptual complications. We need a notation for lists of words.
  1418. There are two different kinds of concatenation, for strings &#173;
  1419. <TT>$a^$b</TT>,
  1420. and lists &#173;
  1421. <TT>($a $b)</TT>.
  1422. The difference between
  1423. <TT>()</TT>
  1424. and
  1425. <TT>''</TT>
  1426. is confusing to novices,
  1427. although the distinction is arguably sensible &#173;
  1428. a null argument is not the same as no argument.
  1429. </P>
  1430. <P>
  1431. Bourne also rescans input when doing command substitution.
  1432. This is because the text enclosed in back-quotes is not
  1433. a string, but a command. Properly, it ought to
  1434. be parsed when the enclosing command is, but this makes
  1435. it difficult to
  1436. handle nested command substitutions, like this:
  1437. <DL><DT><DD><TT><PRE>
  1438. size=`wc -l \`ls -t|sed 1q\``
  1439. </PRE></TT></DL>
  1440. The inner back-quotes must be escaped
  1441. to avoid terminating the outer command.
  1442. This can get much worse than the above example;
  1443. the number of
  1444. <TT>r</TT>'s
  1445. equired is exponential in the nesting depth.
  1446. <I>Rc</I>
  1447. fixes this by making the backquote a unary operator
  1448. whose argument is a command, like this:
  1449. <DL><DT><DD><TT><PRE>
  1450. size=`{wc -l `{ls -t|sed 1q}}
  1451. </PRE></TT></DL>
  1452. No escapes are ever required, and the whole thing
  1453. is parsed in one pass.
  1454. </P>
  1455. <P>
  1456. For similar reasons
  1457. <I>rc</I>
  1458. defines signal handlers as though they were functions,
  1459. instead of associating a string with each signal, as Bourne does,
  1460. with the attendant possibility of getting a syntax error message
  1461. in response to typing the interrupt character. Since
  1462. <I>rc</I>
  1463. parses input when typed, it reports errors when you make them.
  1464. </P>
  1465. <P>
  1466. For all this trouble, we gain substantial semantic simplifications.
  1467. There is no need for the distinction between
  1468. <TT>$*</TT>
  1469. and
  1470. <TT>$@</TT>.
  1471. There is no need for four types of quotation, nor the
  1472. extremely complicated rules that govern them. In
  1473. <I>rc</I>
  1474. you use quotation marks when you want a syntax character
  1475. to appear in an argument, or an argument that is the empty string,
  1476. and at no other time.
  1477. <TT>IFS</TT>
  1478. is no longer used, except in the one case where it was indispensable:
  1479. converting command output into argument lists during command substitution.
  1480. </P>
  1481. <P>
  1482. This also avoids an important UNIX security hole.
  1483. In UNIX, the
  1484. <I>system</I>
  1485. and
  1486. <I>popen</I>
  1487. functions call
  1488. <TT>/bin/sh</TT>
  1489. to execute a command. It is impossible to use either
  1490. of these routines with any assurance that the specified command will
  1491. be executed, even if the caller of
  1492. <I>system</I>
  1493. or
  1494. <I>popen</I>
  1495. specifies a full path name for the command. This can be devastating
  1496. if it occurs in a set-userid program.
  1497. The problem is that
  1498. <TT>IFS</TT>
  1499. is used to split the command into words, so an attacker can just
  1500. set
  1501. <TT>IFS=/</TT>
  1502. in his environment and leave a Trojan horse
  1503. named
  1504. <TT>usr</TT>
  1505. or
  1506. <TT>bin</TT>
  1507. in the current working directory before running the privileged program.
  1508. <I>Rc</I>
  1509. fixes this by never rescanning input for any reason.
  1510. </P>
  1511. <P>
  1512. Most of the other differences between
  1513. <I>rc</I>
  1514. and the Bourne shell are not so serious. I eliminated Bourne's
  1515. peculiar forms of variable substitution, like
  1516. <DL><DT><DD><TT><PRE>
  1517. echo ${a=b} ${c-d} ${e?error}
  1518. </PRE></TT></DL>
  1519. because they are little used, redundant and easily
  1520. expressed in less abstruse terms.
  1521. I deleted the builtins
  1522. <TT>export</TT>,
  1523. <TT>readonly</TT>,
  1524. <TT>break</TT>,
  1525. <TT>continue</TT>,
  1526. <TT>read</TT>,
  1527. <TT>return</TT>,
  1528. <TT>set</TT>,
  1529. <TT>times</TT>
  1530. and
  1531. <TT>unset</TT>
  1532. because they seem redundant or
  1533. only marginally useful.
  1534. </P>
  1535. <P>
  1536. Where Bourne's syntax draws from Algol 68,
  1537. <I>rc</I>'s
  1538. is based on C or Awk. This is harder to defend.
  1539. I believe that, for example
  1540. <DL><DT><DD><TT><PRE>
  1541. if(test -f junk) rm junk
  1542. </PRE></TT></DL>
  1543. is better syntax than
  1544. <DL><DT><DD><TT><PRE>
  1545. if test -f junk; then rm junk; fi
  1546. </PRE></TT></DL>
  1547. because it is less cluttered with keywords,
  1548. it avoids the semicolons that Bourne requires
  1549. in odd places,
  1550. and the syntax characters better set off the
  1551. active parts of the command.
  1552. </P>
  1553. <P>
  1554. The one bit of large-scale syntax that Bourne
  1555. unquestionably does better than
  1556. <I>rc</I>
  1557. is the
  1558. <TT>if</TT>
  1559. statement with
  1560. <TT>else</TT>
  1561. clause.
  1562. <I>Rc</I>'s
  1563. <TT>if</TT>
  1564. has no terminating
  1565. <TT>fi</TT>-like
  1566. bracket. As a result, the parser cannot
  1567. tell whether or not to expect an
  1568. <TT>else</TT>
  1569. clause without looking ahead in its input.
  1570. The problem is that after reading, for example
  1571. <DL><DT><DD><TT><PRE>
  1572. if(test -f junk) echo junk found
  1573. </PRE></TT></DL>
  1574. in interactive mode,
  1575. <I>rc</I>
  1576. cannot decide whether to execute it immediately and print
  1577. <TT>$prompt(1)</TT>,
  1578. or to print
  1579. <TT>$prompt(2)</TT>
  1580. and wait for the
  1581. <TT>else</TT>
  1582. to be typed.
  1583. In the Bourne shell, this is not a problem, because the
  1584. <TT>if</TT>
  1585. command must end with
  1586. <TT>fi</TT>,
  1587. regardless of whether it contains an
  1588. <TT>else</TT>
  1589. or not.
  1590. </P>
  1591. <P>
  1592. <I>Rc</I>'s
  1593. admittedly feeble solution is to declare that the
  1594. <TT>else</TT>
  1595. clause is a separate statement, with the semantic
  1596. proviso that it must immediately follow an
  1597. <TT>if</TT>,
  1598. and to call it
  1599. <TT>if not</TT>
  1600. rather than
  1601. <TT>else</TT>,
  1602. as a reminder that something odd is going on.
  1603. The only noticeable consequence of this is that
  1604. the braces are required in the construction
  1605. <DL><DT><DD><TT><PRE>
  1606. for(i){
  1607. if(test -f $i) echo $i found
  1608. if not echo $i not found
  1609. }
  1610. </PRE></TT></DL>
  1611. and that
  1612. <I>rc</I>
  1613. resolves the ``dangling else'' ambiguity in opposition
  1614. to most people's expectations.
  1615. </P>
  1616. <P>
  1617. It is remarkable that in the four most recent editions of the UNIX system
  1618. programmer's manual the Bourne shell grammar described in the manual page
  1619. does not admit the command
  1620. <TT>who|wc</TT>.
  1621. This is surely an oversight, but it suggests something darker:
  1622. nobody really knows what the Bourne shell's grammar is. Even examination
  1623. of the source code is little help. The parser is implemented by recursive
  1624. descent, but the routines corresponding to the syntactic categories all
  1625. have a flag argument that subtly changes their operation depending on the
  1626. context.
  1627. <I>Rc</I>'s
  1628. parser is implemented using
  1629. <I>yacc</I>,
  1630. so I can say precisely what the grammar is.
  1631. </P>
  1632. <H4>29 Acknowledgements
  1633. </H4>
  1634. <P>
  1635. Rob Pike, Howard Trickey and other Plan 9 users have been insistent, incessant
  1636. sources of good ideas and criticism. Some examples in this document are plagiarized
  1637. from [Bourne],
  1638. as are most of
  1639. <I>rc</I>'s
  1640. good features.
  1641. </P>
  1642. <H4>30 Reference
  1643. </H4>
  1644. <br>&#32;<br>
  1645. S. R. Bourne,
  1646. UNIX Time-Sharing System: The UNIX Shell,
  1647. Bell System Technical Journal, Volume 57 number 6, July-August 1978
  1648. <br>&#32;<br>
  1649. <A href=http://www.lucent.com/copyright.html>
  1650. Copyright</A> &#169; 2000 Lucent Technologies Inc. All rights reserved.
  1651. </body></html>