home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1437 < prev    next >
Internet Message Format  |  1990-12-28  |  48KB

  1. From: istvan@hhb.UUCP (Istvan Mohos)
  2. Newsgroups: alt.sources
  3. Subject: Subject: ILIB Unix Toolkit in C
  4. Message-ID: <552@hhb.UUCP>
  5. Date: 8 Jun 90 20:56:30 GMT
  6.  
  7.  
  8. ---- Cut Here and unpack ----
  9. #!/bin/sh
  10. # This is part 06 of a multipart archive
  11. if touch 2>&1 | fgrep '[-amc]' > /dev/null
  12.  then TOUCH=touch
  13.  else TOUCH=true
  14. fi
  15. # ============= iman/icue.tex ==============
  16. echo "x - extracting iman/icue.tex (Text)"
  17. sed 's/^X//' << 'SHAR_EOF' > iman/icue.tex &&
  18. X% XREF icue inl inull
  19. X
  20. X\def\name{ICUE}
  21. X\def\IT{{\bb icue()}}
  22. X
  23. X\S{NAME}
  24. X
  25. X{\bb icue} --- buffer management
  26. X
  27. X{\bb inl} --- return pointer to next {\myit newline\/} or NUL byte
  28. X
  29. X{\bb inull} --- return pointer to next NUL byte
  30. X
  31. X\S{SYNOPSIS}
  32. X{\obeylines \bb
  33. Xvoid
  34. Xicue (ptr)
  35. Xchar **ptr;
  36. X\L
  37. Xchar *
  38. Xinl (start)
  39. Xchar *start;
  40. X\L
  41. Xchar *
  42. Xinull (start)
  43. Xchar *start;
  44. X}
  45. X
  46. X\S{DESCRIPTION}
  47. XBSD's {\myit sprintf()} implementation results in
  48. X``one of those annoying gratuitous incompatibilities among UNIX
  49. Xvariants'' (quoting the eloquently restrained
  50. XUSENET {\myit wizard\/} Doug Gwyn).
  51. XUnder BSD, {\myit sprintf()} returns quite uselessly the
  52. Xsame buffer pointer that the caller passed in;
  53. Xline~51 of Berkeley's {\myit /usr/include/stdio.h\/} is not ashamed to
  54. Xdeclare:
  55. X\smallskip
  56. X\I{\mytt /* \ats(\#)stdio.h 1.2 86/10/07 SMI; from UCB 1.4 06/30/83 */}
  57. X\I{\mytt char *sprintf(); /* too painful to do right */}
  58. X\smallskip
  59. XSystem~V  meanwhile, returns the number of
  60. Xcharacters written so the user's pointer at which printing began, can
  61. Xeasily be incremented by the returned {\myit int\/} to
  62. Xcontinue printing at the end of the previous string.
  63. XTo achieve the same end
  64. Xunder BSD, the pointer would have to be incremented by the
  65. Xoffset gotten with an extra call to {\myit strlen()\/}.
  66. X\L
  67. XILIB's {\bb icue()} function presents an alternative method of resetting
  68. Xthe pointer to the end of NUL terminated strings.  {\bb icue()}
  69. Xbypasses the {\myit sprintf\/} system dependency, and
  70. Xalso works hand-in-hand with any
  71. Xstring function that leaves a NUL byte at the end of the
  72. Xcaller's buffer.
  73. X\L
  74. X{\bb icue()} is called following
  75. X{\myit sprintf()\/} or other string function calls, and
  76. Xautomatically advances the passed pointer {\myit ptr\/} in the
  77. Xbuffer to the next NUL byte.
  78. XIn order to alter the address that the pointer dereferences,
  79. Xthe pointer should be passed to
  80. X{\bb icue()} by its address.
  81. X\L
  82. X{\bb inl()} and {\bb inull()} are two other ILIB routines
  83. Xdedicated to search for the end of NUL terminated strings.
  84. XSimilarly to {\myit strlen()\/}, {\bb inl()} and {\bb inull()}
  85. Xboth report the length of a string, but by returning a pointer to the
  86. Xend of the string rather than by the count of bytes.  The pointer is
  87. Xoften more convenient than the offset.
  88. X{\bb inl()} finds the first {\myit unescaped\/}
  89. Xnewline or the first NUL (ASCII zero) byte
  90. Xfrom {\myit start\/}, and returns a pointer to it.
  91. X{\bb inull()} finds the first NUL byte
  92. Xfrom {\myit start\/}, and returns a pointer to it.
  93. XNeither {\bb inl()} or {\bb inull()} ``know when to stop'' unless they
  94. Xfound the delimiters they were searching for.  If the buffer does not
  95. Xcontain the delimiter, the search will continue into restricted
  96. Xspace and cause a {\myit core dump\/}.  {\bb inull()} is marginally
  97. Xfaster than the standard {\myit strlen()\/}.  {\bb inl} pays a speed
  98. Xpenalty for having to match
  99. Xeither delimiter against each byte of the string.
  100. X
  101. X\S{isdef EXAMPLE PROGRAM}
  102. XMost C compilers contain a number of unadvertised internal definitions.
  103. XInternal {\myit \#define\/}s implement system debugging parameters
  104. Xsuch as  {\myit \_\_LINE\_\_, \_\_FILE\_\_,\/} as well as
  105. Xglobal system or machine identifiers
  106. Xsuch as {\myit unix, pyr\/} (under the Pyramid 90x mini, for
  107. Xexample).
  108. XOnce in a while, identifiers of a user program will result in a
  109. Xcollision with an internal
  110. X{\myit \#define\/}.
  111. XTo find out beforehand whether a word belongs in the set of
  112. Xinternally defined tokens, one could examine the string space
  113. Xof the programs comprising the object set of {\myit cc\/} (a few
  114. Xelements of this list are
  115. X{\myit /lib/cpp, /lib/ccom, /lib/c2, /lib/crt0.o, /lib/libc.a\/});
  116. Xbut often it is simpler just to write a few-line {\myit main()\/}
  117. Xprogram that includes the word in question --- if the program
  118. Xcompiles, the word is already defined.
  119. X\L
  120. XThe {\bb isdef} program automates this process: it writes a
  121. Xtemporary C~program for testing the defined status
  122. Xof the command line parameters; it
  123. Xthen proceeds with compiling, executing, and finally deleting
  124. Xthe test program.  To ascertain whether a command line parameter is
  125. Xdefined by the C~compiler, the generated test code will contain
  126. X\smallskip
  127. X{\obeylines \mytt \parindent=40pt
  128. X\#ifdef\ \dx{token}
  129. X\ \ \ \ printf\ ("\dx{token}\ is\ defined\bsl n");
  130. X\#else
  131. X\ \ \ \ printf\ ("\dx{token}\ not\ defined\bsl n");
  132. X\#endif
  133. X\smallskip}
  134. Xfor each token supplied to {\bb isdef} as a command line parameter.
  135. XThe {\bb isdef} process pieces the test code together in an internal
  136. Xbuffer {\myit fbuf\/},
  137. Xand writes the entire buffer to a temporary file in one
  138. Xoperation.  While filling the buffer, the
  139. Xbuffer pointer {\myit ptr\/} is moved to the
  140. Xend of the most recently installed sub-string via {\bb icue()}, and
  141. Xthe next sub-string is printed from this point on.
  142. X
  143. X\S{isdef.c PROGRAM TEXT}
  144. X\listing{../iex/isdef.c}
  145. X
  146. X\S{excise EXAMPLE PROGRAM}
  147. XWhile it is ``cleaner'' to implement algorithms for in-place
  148. Xfile transformations by bypassing \stin\ and \stout\ (reading and
  149. Xwriting file data through specific file descriptors instead), the
  150. X{\bb excise} example program illustrates a method using standard
  151. Xterminal I/O.  The complaint against some
  152. X{\myit filter\/} programs in Unix is that a filter structure is
  153. Xoften inappropriate, and forces the user to redirect output to a
  154. Xtemporary file that must then be followed by manually moving the
  155. Xtemporary file to replace the source file.  {\bb excise} eliminates
  156. Xthe need for this ``manual post-processing'' by doing it within
  157. Xthe program: it creates the temporary file of the output, and
  158. Xmoves the temporary file to replace the source file with a call to the
  159. X{\myit system()\/} command of the standard C library.
  160. X\L
  161. X{\bb excise} searches
  162. Xfor a user-specified string (a mandatory command line
  163. Xparameter), in the text of each file given on its command line,
  164. XHaving found the string nearest to the beginning of the
  165. Xfile, {\bb excise} removes the {\myit newline\/} terminated
  166. Xline that contained the string, then goes on to
  167. Xthe next file.  The ``solo minus'' option on the command line
  168. Xforces {\bb excise} to search each file in its entirety, and to remove
  169. Xall those lines of the file that contained the specified string.
  170. X\L
  171. X{\bb excise} implements the mechanics of ``removing a line'' via
  172. Xthe \stio\ discipline.  Each source file in the queue
  173. Xis recast as \stin\ in turn, and is read a line at a time.
  174. XEach line of input is immediately reprinted to \stout\ unless the
  175. Xline contains the string match and is therefore to be ``removed''.
  176. XRemoval
  177. Xconsists of suppressing the line from being output.  Similarly to
  178. Xrenaming source files \stin, \stout\ is attached to a
  179. Xtemporary file in {\myit /tmp\/} collecting the altered text.
  180. XBecause \stout\ is buffered, the system is not obliged to copy
  181. Xfragments of output to the temporary file unless it is forced to do
  182. Xthis by a
  183. Xcall to {\myit fflush()\/}.  After this, the temporary file is made
  184. Xto displace the source file with the {\myit mv\/} command; and then
  185. Xthe process repeats with the next source file in the queue.
  186. X\L
  187. XBecause of the line-by-line processing and because
  188. X{\myit strcmp()\/} is used for string matching, {\bb excise}
  189. Xwill not find the specified string in any text if the
  190. Xstring contained {\myit newline\/}
  191. Xor NUL (ASCII zero) characters, even though
  192. X{\myit ifonetic()\/} pre-processes the string and converts
  193. XILIB's phonetic control sequences to non-alphanumeric values.
  194. X
  195. X\S{excise.c PROGRAM TEXT}
  196. X\listing{../iex/excise.c}
  197. X\eject
  198. SHAR_EOF
  199. $TOUCH -am 0605083990 iman/icue.tex &&
  200. chmod 0644 iman/icue.tex ||
  201. echo "restore of iman/icue.tex failed"
  202. set `wc -c iman/icue.tex`;Wc_c=$1
  203. if test "$Wc_c" != "7399"; then
  204.     echo original size 7399, current size $Wc_c
  205. fi
  206. # ============= iman/idate.tex ==============
  207. echo "x - extracting iman/idate.tex (Text)"
  208. sed 's/^X//' << 'SHAR_EOF' > iman/idate.tex &&
  209. X% XREF idate imonth itoday itohour itomin itomonth itosec itoyear
  210. X
  211. X\def\name{IDATE}
  212. X\def\IT{{\bb idate()}}
  213. X
  214. X\S{NAME}
  215. X{\bb idate} --- print today's date in one of four formats
  216. X
  217. X{\bb imonth} --- convert month name to number 1 through 12
  218. X
  219. X{\bb itoday} --- integer value (1 $\ldots$\ 31) of current day of month
  220. X
  221. X{\bb itohour} --- integer value (0 $\ldots$\ 23) of current hour of day
  222. X
  223. X{\bb itomin} --- integer value (0 $\ldots$\ 59) of current minute
  224. X
  225. X{\bb itomonth} --- integer value (1 $\ldots$\ 12) of current month
  226. X
  227. X{\bb itosec} --- integer value (0 $\ldots$\ 59) of current second
  228. X
  229. X{\bb itoyear} --- four decimal-digit value of current year
  230. X
  231. X\S{SYNOPSIS}
  232. X\settabs\+{\bb mdefine SHORTUPMO} & mmm &\cr
  233. X\+{\bb\#define SHORTMO}&\hfill{\bb0}&\kern2em{\mytt
  234. X/* to get: Dec 23 1990 */}\cr
  235. X\+{\bb\#define SHORTUPMO}&\hfill{\bb1}&\kern2em{\mytt
  236. X/* to get: DEC 23 1990 */}\cr
  237. X\+{\bb\#define LONGMO}&\hfill{\bb2}&\kern2em{\mytt
  238. X/* to get: December 23, 1990 */}\cr
  239. X\+{\bb\#define LONGUPMO}&\hfill{\bb3}&\kern2em{\mytt
  240. X/* to get: DECEMBER 23, 1990 */}\cr
  241. X\L
  242. X{\obeylines \bb
  243. Xchar *
  244. Xidate (format)
  245. Xint format;
  246. X\L
  247. Xint
  248. Ximonth (ptr)
  249. Xchar *ptr;
  250. X\L
  251. Xint
  252. Xitoday (), itohour (), itomin (), itomonth (), itosec (), itoyear ();
  253. X}
  254. X
  255. X\S{DESCRIPTION}
  256. XThe \IT\ functions provide simple and convenient ``date stamping''
  257. Xin a variety of formats other than returned by the Unix
  258. X{\myit ctime()\/}
  259. Xand {\myit date()\/}.  All \IT\ functions except {\bb imonth()}
  260. Xrely on an internal, ``hidden'' function {\myit \_igetdate()\/}
  261. Xfor consulting the system clock and storing the fields of a {\myit tm\/}
  262. Xstructure.  The current time is saved at the first
  263. X{\myit \_igetdate()\/} call of the process; subsequent calls though
  264. Xlater in time, do not update the originally stored values.
  265. X\L
  266. X\IT\ returns a pointer to a null terminated ``today's date'' string in
  267. Xone of four {\myit format\/}s.  The returned string does not contain
  268. Xa {\myit newline\/}.  Error reporting is absent.
  269. X\L
  270. X{\bb imonth()} understands the string passed in via
  271. X{\myit ptr\/} to describe a month between January and December
  272. Xor between 1 and 12, and returns the {\myit int\/} value of
  273. Xthe month (1 through 12).
  274. XNames may contain arbitrary upper or lower case characters,
  275. Xbut must begin with the initial 3 characters of a month
  276. Xto be recognized.
  277. XIf the first character of {\myit ptr\/}
  278. Xis a digit, {\bb imonth()} uses {\myit atoi()\/} to extract the
  279. Xnumber.  If the first character is alphabetic, two more characters
  280. Xare read, and a hash value computed from the upcased three characters
  281. Xis used to decode the number of the month.  If the number from
  282. Xeither evaluation lies outside the 1--12 range, 0 is returned instead.
  283. XNegative {\myit sys\_nerr\/} is returned if
  284. X{\myit (char *)NULL\/} is passed as {\myit ptr\/},
  285. Xor if {\myit ptr\/} dereferences a zero length
  286. Xstring.
  287. X\L
  288. XThe time stamps
  289. X{\bb
  290. Xitoday(),
  291. Xitohour(),
  292. Xitomin(),
  293. Xitomonth(),
  294. Xitosec(),
  295. Xitoyear()}
  296. Xeach return the current {\myit int\/}
  297. Xvalue of the unit that follows {\myit ito\/} in the function name.
  298. XError reporting is absent.
  299. X
  300. X\S{SEE ALSO}
  301. X{\myit ihms\/},
  302. Xfor converting seconds to hours:minutes:seconds; and
  303. Xto access a continually updated time stamp for timing applications.
  304. X
  305. X\S{stamp EXAMPLE PROGRAM}
  306. XAlthough the date of creation of a Unix file is evident from the
  307. Xsystem time stamp displayed in a {\myit long listing\/} of the file,
  308. Xoccasionally it is desirable to include the date in
  309. Xthe file name itself.  A {\myit crontab entry\/} may take regular
  310. Xsnapshots of some system resource for example; the inclusion of
  311. Xthe date in the snapshot file names eliminates the worry of having
  312. Xto generate
  313. Xfile name variants, and provides a more obvious mark of the
  314. Xtime of creation of a file.  The {\bb stamp} program prints a
  315. Xfour-character digit string of the current month/day, easily included
  316. Xas a file name extension:
  317. X\smallskip
  318. X\I{\mytt df > diskspace.`stamp`}
  319. X\smallskip
  320. XIn this command line, {\myit back quotes\/} surround {\bb stamp};
  321. Xthe name of the newly written file will end in a four-digit
  322. Xextension generated by {\bb stamp}.  On January 23 for example,
  323. Xthe new file would be named {\myit diskspace.0123\/}.
  324. X
  325. X\S{stamp.c PROGRAM TEXT}
  326. X\listing{../iex/stamp.c}
  327. X\eject
  328. SHAR_EOF
  329. $TOUCH -am 0607093190 iman/idate.tex &&
  330. chmod 0644 iman/idate.tex ||
  331. echo "restore of iman/idate.tex failed"
  332. set `wc -c iman/idate.tex`;Wc_c=$1
  333. if test "$Wc_c" != "4167"; then
  334.     echo original size 4167, current size $Wc_c
  335. fi
  336. # ============= iman/idump.tex ==============
  337. echo "x - extracting iman/idump.tex (Text)"
  338. sed 's/^X//' << 'SHAR_EOF' > iman/idump.tex &&
  339. X% XREF idump
  340. X
  341. X\def\name{IDUMP}
  342. X\def\IT{{\bb idump()}}
  343. X
  344. X\S{NAME}
  345. X{\bb idump} --- write a buffer to \stout,
  346. Xtranslating NUL bytes to a different ASCII value
  347. X
  348. X\S{SYNOPSIS}
  349. X{\obeylines \bb
  350. Xvoid
  351. Xidump (start, end, ch)
  352. Xchar *start;
  353. Xchar *end;
  354. Xchar ch;
  355. X}
  356. X
  357. X\S{DESCRIPTION}
  358. XMaking repeated calls to {\myit write()\/}, \IT\
  359. Xprints the contents of an internal buffer
  360. Xbegininning at {\myit start\/} and ending one byte before
  361. X{\myit end\/}, to \stout.  The most useful feature of \IT\
  362. Xis the transformation of ASCII NUL (zero) bytes
  363. Xto a different ASCII value in the output,
  364. Xmaking this hard-to-handle character
  365. Xeasily visible and identifiable.
  366. XThe internal buffer meanwhile remains intact, and
  367. Xprocess development can continue undisturbed by the dump.
  368. X\L
  369. XThe target character value (to substitute for NUL bytes in the output)
  370. Xis passed via the {\myit char~ch\/}.
  371. XThis character itself may be NUL
  372. X(passed as two successive single quotes or as '\bsl0'), producing
  373. Xan exact copy of the buffer on \stout.
  374. X\IT\ is intended mainly as a {\myit write()\/} substitute
  375. Xduring program development,
  376. X
  377. X\S{nulcat EXAMPLE PROGRAM}
  378. XThe {\bb nulcat} program described here,
  379. Xuses \IT\ to print a file to \stout\ with NUL bytes of the
  380. Xfile translated to \key{\ats} characters.  Alternately,
  381. X{\bb nulcat} can be a filter, using \stin\ for input.
  382. X\L
  383. XThe {\bb nulcat}
  384. Xprocess terminates immediately after \IT, and so fails to take
  385. Xadvantage of the unchanged file image still in the internal
  386. Xbuffer.  The program aims to supplement the
  387. Xotherwise excellent non-printing character display capabilities of
  388. Xthe {\myit vi\/} editor.  {\myit vi\/} appears to have a
  389. X``blind spot'' when faced with NUL bytes; other solutions for displaying
  390. XNUL characters ({\myit od~-c\/}, etc.) are visually complex and
  391. Xhard to interpret.
  392. X\L
  393. XCommand line syntax is typical of filters.  The
  394. Xfollowing commands give identical results:
  395. X\smallskip
  396. X\I{\mytt cat sourcefile \pip \ nulcat}
  397. X\I{\mytt nulcat sourcefile}
  398. X
  399. X\S{nulcat.c PROGRAM TEXT}
  400. X\listing{../iex/nulcat.c}
  401. X\eject
  402. SHAR_EOF
  403. $TOUCH -am 0607094290 iman/idump.tex &&
  404. chmod 0644 iman/idump.tex ||
  405. echo "restore of iman/idump.tex failed"
  406. set `wc -c iman/idump.tex`;Wc_c=$1
  407. if test "$Wc_c" != "2013"; then
  408.     echo original size 2013, current size $Wc_c
  409. fi
  410. # ============= iman/iego.tex ==============
  411. echo "x - extracting iman/iego.tex (Text)"
  412. sed 's/^X//' << 'SHAR_EOF' > iman/iego.tex &&
  413. X% XREF iego
  414. X
  415. X\def\name{IEGO}
  416. X\def\IT{{\bb iego()}}
  417. X
  418. X\S{NAME}
  419. X{\bb iego} --- put path-less, extension-less base name into word buffer
  420. X
  421. X\S{SYNOPSIS}
  422. X{\obeylines \bb
  423. Xint
  424. Xiego (ptr, wbuf, delim, ext)
  425. Xchar *ptr;
  426. Xchar *wbuf;
  427. Xchar delim;
  428. Xchar ext;
  429. X}
  430. X
  431. X\S{DESCRIPTION}
  432. X\IT\ examines the null terminated
  433. X{\myit path/filename\/} pointed to by {\myit ptr\/},
  434. Xand removes from it
  435. Xeither the {\myit basename.extension\/} substring
  436. X(leaving only the {\myit path\/} component
  437. Xin the buffer passed in via {\myit wbuf\/}),
  438. Xor removes the {\myit path\/} and {\myit extension\/} substrings
  439. X(leaving only the {\myit basename\/} component
  440. Xin {\myit wbuf\/}).  The original string at {\myit ptr\/} is not
  441. Xdisturbed.
  442. X\L
  443. XThe {\myit path\/} substring is computed as all leading bytes to
  444. Xand including the last {\myit delim\/} character of the name;
  445. X{\myit delim\/} is typically --- but not necessarily --- the
  446. Xslash \key{/} character.
  447. XThe {\myit extension\/} substring is computed as all trailing bytes
  448. Xincluding the first occurrence of the {\myit ext\/} character,
  449. Xfollowing the base name.  The {\myit ext\/} parameter
  450. Xis typically the dot \key{.} character.
  451. XThe {\myit ext\/} parameter also serves to convey the choice
  452. Xbetween generating either the {\myit path\/} or the
  453. X{\myit basename.extension\/} targets:
  454. XIf {\myit ext\/} is NUL (passed as {\myit (int)0\/},
  455. Xadjacent single quotes \dx{\mytt''}\
  456. Xor {\mytt'\bsl0'}),
  457. Xthe null terminated {\myit path\/} is left in {\myit wbuf\/}.
  458. XOtherwise, the null terminated {\myit basename\/} is
  459. Xleft in {\myit wbuf\/}.
  460. X\L
  461. X\IT\ returns the byte length of the target string in {\myit wbuf\/}.
  462. XNegative {\myit sys\_nerr\/} is returned if
  463. Xaddress~0 is passed as {\myit ptr\/},
  464. Xor if {\myit ptr\/} dereferences a zero length string.
  465. X
  466. X\S{ego EXAMPLE PROGRAM}
  467. XThe {\bb ego} program provides an
  468. Xalternative to the Unix {\myit basename\/}
  469. Xcommand, with
  470. Xa shorter name and an easier syntax.  Three other names {\bb path},
  471. X{\bb ext}
  472. Xand {\bb egoext} are {\myit links\/} of {\bb ego}, meaning that
  473. Xthe four command names are equivalent and refer to the same object.
  474. XExecution of the object begins with an internal examination of the
  475. Xname by which the command was invoked;
  476. Xthe intention of the user is inferred from the selection of the command
  477. Xname.
  478. X\smallskip
  479. X\I{\mytt ego file ...}
  480. X\smallskip
  481. Xprints the {\myit basename\/} of the command line
  482. Xarguments.  Each name is terminated with a {\myit newline\/}.
  483. X\smallskip
  484. X\I{\mytt ext file ...}
  485. X\smallskip
  486. Xprints the {\myit extension\/} part of the file names
  487. Xgiven as command line
  488. Xarguments, without the period character that separates an extension
  489. Xfrom the base name.  The extension itself may contain embedded or
  490. Xtrailing periods.  Each name is terminated with a {\myit newline\/}.
  491. XSimilarly,
  492. X\smallskip
  493. X\I{\mytt path file ...}
  494. X\smallskip
  495. Xreprints the {\myit path\/}s of
  496. Xcommand line arguments, retaining the trailing slash \key{/}.
  497. X\smallskip
  498. X\I{\mytt egoext file ...}
  499. X\smallskip
  500. Xprints the {\myit basename.extension\/} substring of each file name
  501. Xsupplied on the command line.
  502. X
  503. X\S{ego.c PROGRAM TEXT}
  504. X\listing{../iex/ego.c}
  505. X\eject
  506. SHAR_EOF
  507. $TOUCH -am 0531085290 iman/iego.tex &&
  508. chmod 0644 iman/iego.tex ||
  509. echo "restore of iman/iego.tex failed"
  510. set `wc -c iman/iego.tex`;Wc_c=$1
  511. if test "$Wc_c" != "3085"; then
  512.     echo original size 3085, current size $Wc_c
  513. fi
  514. # ============= iman/ierror.tex ==============
  515. echo "x - extracting iman/ierror.tex (Text)"
  516. sed 's/^X//' << 'SHAR_EOF' > iman/ierror.tex &&
  517. X% XREF ierror idamage ierflag ierbuf
  518. X
  519. X\def\name{IERROR}
  520. X\def\IT{{\bb ierror()}}
  521. X
  522. X\S{NAME}
  523. X{\bb ierror} --- report an ILIB error or a system error
  524. X
  525. X{\bb idamage} --- return TRUE on possible file damage
  526. X
  527. X\S{SYNOPSIS}
  528. X{\obeylines \bb
  529. X\#define IHALFK 512
  530. X\L
  531. Xchar ierbuf[IHALFK];
  532. Xint ierflag;
  533. X\L
  534. Xint
  535. Xierror (ustr)
  536. Xchar *ustr;
  537. X\L
  538. Xint
  539. Xidamage (error)
  540. Xint error;
  541. X}
  542. X
  543. X\S{DESCRIPTION}
  544. XThe \IT\ function is similar to the standard
  545. X{\myit perror()\/}.
  546. XILIB functions report internal failures exclusively through \IT.
  547. XIn addition to errors during
  548. Xsystem calls encountered within ILIB function blocks,
  549. XILIB functions also use \IT\ to flag improper
  550. Xparameters passed to ILIB functions that prevent normal
  551. Xfunction execution.
  552. X\L
  553. XImproper parameters are typically zero-length strings, or
  554. X{\myit start\/} pointers set to NULL instead of to some accessible
  555. Xbuffer.  Exhaustive testing of parameters would impose prohibitive
  556. Xruntime performance penalties;
  557. XILIB functions with {\myit start, end\/}
  558. Xbuffer parameters are usually content to read the buffer from
  559. X{\myit start\/} if {\myit (char *)NULL\/} is passed as
  560. X{\myit end\/}, and assume that in these cases the caller was good
  561. Xenough to pass
  562. Xa null (byte) terminated string beginning at {\myit start\/}.
  563. X\L
  564. XIf an ILIB function makes a call to \IT\ not because of
  565. Xsystem call failure
  566. Xbut because the user's call to the
  567. Xthe ILIB function
  568. Xcontained a parameter error,
  569. X{\myit errno\/} will be zero.  In this case,
  570. X\IT\ copies {\myit ustr\/} (or ``{\mytt Error\/}'' if
  571. X{\myit ustr\/} is NULL)
  572. Xinto {\bb ierbuf\/},
  573. Xsets {\bb ierflag\/} to the negative value of the system constant
  574. X{\myit sys\_nerr\/} (the number of error messages in
  575. X{\myit sys\_errlist\/}), and returns this value.
  576. X\L
  577. XAll system calls executed by ILIB functions are tested
  578. Xfor failing returns within the individual function blocks.
  579. XFailing system calls
  580. Xcause the caller ILIB function to issue a call to \IT\
  581. Xand then to return without further processing.
  582. XWhen called, \IT\ first examines the global
  583. X{\myit errno\/}.
  584. XIf {\myit errno\/} is set, an error has occurred during a previous
  585. Xsystem call.  In this case,
  586. X\IT\ copies the {\myit sys\_errlist\/}
  587. Xmessage indexed by {\myit errno\/}, into {\bb ierbuf\/}; appends
  588. Xthe string ``{\mytt~---~}'' and then {\myit ustr\/}.
  589. XIf {\myit ustr\/} is NULL, only
  590. X{\myit sys\_errlist[errno]\/} is copied.
  591. XNewline or white space is not added after {\myit ustr\/}.  The
  592. Xfinal {\bb ierbuf\/} message is null terminated.
  593. XThe ILIB error flag
  594. X{\bb ierflag\/} is assigned
  595. Xthe negative value of {\myit errno\/} before
  596. X\IT\ resets {\myit errno\/} to zero.
  597. XThe {\bb ierflag\/} value is returned from the call.
  598. X\L
  599. XPrograms linking with ILIB may utilize \IT\ as completely distinct
  600. Xfrom ILIB usage, for their own internal error reporting.
  601. XThe principal benefit from this is
  602. Xan orthogonal interpretation of errors across
  603. Xsystem calls and user functions.
  604. XThe \IT\ code defines 
  605. Xthe global {\bb char ierbuf[IHALFK]\/} and
  606. X{\bb int ierflag\/} variables.  The {\myit ilib.h\/} file
  607. X{\myit \#include\/}d in the user code, declares these
  608. Xas {\myit extern\/}.
  609. X\L
  610. XThe string sent to
  611. X\IT\ from the calling ILIB function in {\myit ustr\/}
  612. Xis a brief message noting the name of the function that recognized
  613. Xthe error and the specific test that failed.  No restrictions are
  614. Xset for functions that call \IT\ externally to ILIB, except that
  615. Xexcessively long
  616. Xsystem-message/user-strings are truncated beginning at their 512th
  617. Xcharacter in order that \IT\ can null terminate {\bb ierbuf\/}.
  618. XBecause {\myit errno\/} is not automatically cleared by the system
  619. Xbetween system calls, any user code that makes system calls on its
  620. Xown (not via ILIB) and then keeps the process alive in spite of
  621. Xa failing system call, should reset
  622. X{\myit errno\/} on its own before subsequent calls to ILIB.  This
  623. Xwill prevent misleading error reports.
  624. X\L
  625. XThe {\bb idamage()\/} function makes an informed guess as to
  626. Xwhether an error value returned by ILIB functions could have
  627. Xbeen accompanied by file damage.  A number of failing system calls
  628. Xare benign, preventing further processing but not destroying
  629. Xalready computed results.  For example a {\myit malloc\/} error
  630. Xsimply means that no more memory is available; existing buffer
  631. Xcontents remain unchanged.  An ``out of disk space'' error on the
  632. Xother hand, during a {\myit write\/} could occur after a partial
  633. Xflush of the processed buffers,
  634. Xleaving a destroyed file image on disk.  Programs that write to
  635. Xa series of files, may be able to ignore benign errors by
  636. Xsimply bypassing the file under whose processing the error
  637. Xoccurred, but would
  638. Xterminate the process when noticing apparent file damage.
  639. X\L
  640. XInterpreting the passed number as the negative version of a
  641. Xsystem error constant defined in {\myit errno.h\/},
  642. X{\bb idamage()\/} returns TRUE if the failing system call
  643. Xhad the potential of damaging a file; and returns FALSE
  644. Xotherwise.  A {\myit -sys\_nerr\/}
  645. Xvalue passed is assumed to be the error return from an ILIB
  646. Xfunction failing not because of a system call error, and is
  647. Xevaluated as having no potential for file damage.
  648. XMore negative values, and all non-negative values are considered
  649. Xnot in the domain of error numbers, causing FALSE returns.
  650. X
  651. X\S{mung EXAMPLE PROGRAM}
  652. XThe interactive Unix interface is noted for buffering user input
  653. Xline-by-line.  Most standard tools (such as {\myit sed, grep\/})
  654. Xthwart casual attempts at
  655. Xstring transformations with embedded
  656. Xline-separating {\myit newline\/} characters.  The main advantage of
  657. Xoperating on a line
  658. Xof input at a time is that the entire input
  659. Xcan be of unlimited length; programs aiming to transform
  660. Xa single string can
  661. Xallocate just enough work space to contain the
  662. Xmaximum expected line length.
  663. X\L
  664. XBecause of the {\myit stream\/}
  665. Xnature of I/O, standard transformation tools never directly
  666. Xchange the input data.  Instead, the output is generally collected
  667. Xin a new file.  If the original data is to be discarded, the user
  668. Xwill take the extra step of moving the target file over the
  669. Xsource file; in effect unlinking the source file and renaming the
  670. Xtarget file with the name of the source file.  This need for manually
  671. Xreorganizing the directory tends to inhibit multiple file names
  672. X(each requiring the same transformation) supplied as command parameters.
  673. X\L
  674. XThe {\bb mung\/} program finds all occurrences of the
  675. Xstring given as the first
  676. Xcommand line parameter, and transforms each occurrence
  677. Xinto the string given as the second
  678. Xcommand line parameter, in each file listed on the rest of
  679. Xthe command line.  Breaking with tradition, the files themselves
  680. Xare transformed by the changes; each original file will be replaced
  681. Xby its changed version without further user intervention.  Both
  682. Xthe source string and the target string may span lines; {\bb mung\/}
  683. Xuses the easy and obvious {\myit ifonetic()\/} syntax to recognize
  684. Xnon-printing characters specified
  685. Xin the exchange strings.
  686. X\L
  687. XTrue to its name (derived from {\myit mungo\/} --- the waste of
  688. Xmilled wool used for making cheap cloth),
  689. X{\bb mung\/} operates in an inelegant,
  690. Xbrute force fashion; ignoring traditional I/O blocking.
  691. XNevertheless, its easy syntax and
  692. Xthe convenience of arbitrary characters in the exchange strings
  693. Xmake {\bb mung} a useful tool.
  694. X\L
  695. XThe following command example
  696. Xtransforms each carriage-return/linefeed
  697. Xline termination (endemic in files created under DOS)
  698. Xof file1, file2 and file3, to the Unix
  699. X``newline only'' line termination:
  700. X\smallskip
  701. X\I{\mytt mung \ XretXnew \ Xnew \ file1 \ file2 \ file3}
  702. X\smallskip
  703. XAnother example restores words broken up across consecutive lines by
  704. Xhyphenation,
  705. Xto whole words (at the expense of doubling the length of an
  706. Xoccasional line), by deleting hyphen/newline pairs from
  707. Xall files in the directory with names ending in ``text'':
  708. X\smallskip
  709. X\I{\mytt mung \ -Xnew \ '' \ *text}
  710. X\smallskip
  711. XThe two adjacent single quotes in this example indicate a zero-length
  712. Xtarget string: occurrences of the source string are eliminated.
  713. XZero-length strings are different from strings containing ASCII 0
  714. X(NUL) bytes:
  715. X\smallskip
  716. X\I{\mytt mung \ Xnul \ '' \ *text}
  717. X\smallskip
  718. Xdeletes each NUL byte from *text.
  719. X\L
  720. XAfter updating the file, the count of
  721. Xupdated string instances is reported to \stout, in the form
  722. X\smallskip
  723. X\I{\mytt file2 - 75}
  724. X\smallskip
  725. Xshowing that 75 instances of the source string were changed to
  726. Xthe target string in file2.
  727. XEach examined file is rewritten (touched) only if the source string
  728. Xdid occur in the file; zero occurrences of the source string are
  729. Xnot reported.
  730. X\L
  731. XSpecifying zero-length strings for both exchange strings, results
  732. Xin an error message printed to \sterr.  Supplying no file name
  733. Xon the command line triggers a ``usage'' message printed to
  734. X\sterr, summarizing command line syntax. Specifying a zero-length
  735. Xsource string and a non-zero-length target string is interpreted
  736. Xas a ``count~only'' option, instructing {\bb mung\/} to report
  737. Xthe number of occurrences of the second string in each of the
  738. Xfiles, without altering the files in any way.
  739. XUnder this option, the count of
  740. Xtarget string instances is reported to \stout, in the form
  741. X\smallskip
  742. X\I{\mytt file2: 75}
  743. X\smallskip
  744. Xshowing that 75 instances of the target string were encountered in
  745. Xfile2, but file2 was not altered.
  746. X\L
  747. XThe main concern of designs effecting in-place file transformation
  748. Xis the potential for damaging the file during interrupts or
  749. Xoperating system failures.  While
  750. Xinterrupting a transform process acting on a stream
  751. Xcannot damage the source text and may
  752. Xat most be an inconvenience for having to restart the process,
  753. Xa program interrupted in the midst of altering source text itself,
  754. Xwill not be able to
  755. Xcomplete the changes, nor could it restore the source to its original
  756. Xcondition.
  757. XBecause of this, the {\bb mung} process reads source text
  758. Xinto a dynamically allocated buffer, and applies transformations
  759. Xto the buffer image only.  Afterwards,
  760. Xthe source text is overwritten with the altered buffer text in a single
  761. X{\myit write()\/} function call, using the maximally {\myit atomic\/}
  762. Xoperation allowed for a user process by
  763. Xthe multiprocessing kernel.  Interrupts are guaranteed
  764. Xto kill the process either before {\myit write()\/} or after
  765. X{\myit write()\/} but never during {\myit write()\/}, so
  766. Xthe file image conforms to the source text only, or to the target
  767. Xtext only.
  768. XBecause the system employs delayed
  769. X{\myit write\/} operations, the buffer image will linger in
  770. Xdynamic memory, and the disk image of the file may still get
  771. Xcorrupted if the system crashes with the dynamic memory only
  772. Xpartially flushed.  However, unflushed crashes occur rarely.
  773. X\L
  774. XReading an entire file into an internal buffer has the obvious
  775. Xadvantage of making any line boundary meaningless.  The disadvantage
  776. Xis that the file size that the program can handle is limited to
  777. Xthe available dynamic memory.  To maximize available space, the
  778. X{\bb mung\/} program is frugal in assigning memory to anything
  779. Xelse.  The {\myit ifonetic()\/} call reuses {\myit argv[1]\/} and
  780. X{\myit argv[2]\/} space in converting phonetic
  781. Xcontrol sequences to true {\myit char\/}s;
  782. Xand {\bb mung} uses just the one buffer allocated for the source text,
  783. Xfor transforming and containing the target image as well.
  784. XDuring the conversion, the target image
  785. Xwill expand if the the target string is longer than the
  786. Xsource string, but the program doesn't know the extent of the
  787. Xexpansion unless it counts the occurrences of the source string
  788. Xin the source text first.
  789. XTherefore, if the target string is longer than the source string,
  790. Xan extra parsing of the source text is necessary.
  791. X\L
  792. XTo be able to utilize the same transformation alogrithm regardless of
  793. Xthe length of the exchange strings, the ``find-and-replace''
  794. Xblock of the code proceeds from the end of the text toward the
  795. Xbeginning.  When a match to the
  796. Xsource string is found, the source text pointer is
  797. Xdecremented by the length of the source string, preventing
  798. Xoverlapping pattern matching.  Given the source text ``rococo''
  799. Xfor example, the source string ``oco'' will be seen to occur only
  800. Xonce, and if replaced with ``k'' would yield the target text
  801. X``rock''.
  802. X\L
  803. XA generally overlooked
  804. Xbenefit of in-place file transformation is that the output
  805. Xleaves \stout\ available for status reporting, which is normally
  806. Xdifferent from error reporting.  {\bb mung} is able to flag
  807. Xunreadable files or attempted directory writes as true errors, to
  808. X\sterr; while summarizing the conversion count on a ``per file''
  809. Xbasis, to \stout.  It is easy to capture the summary in a pipe
  810. Xto generate a ``total'', for example:
  811. X\smallskip
  812. X{\obeylines \mytt
  813. X\ \ \ \ mung\ ''\ '\bsl listing\lcu '\ *.tex\ |\ fcat\ 2\ |\ add
  814. X\smallskip}
  815. XIn this command {\bb mung} counts the ``per-file''
  816. Xoccurrences of the \dx{\mytt\bsl listing\lcu}\ token
  817. Xin all files with the {\myit .tex\/} extension.
  818. X(The \dx{\mytt\bsl listing\lcu}\ token is
  819. Xdefined as a \TeX\ macro for printing this manual; and in the present
  820. Xcontext it illustrates that the use of the phonetic control sequences
  821. Xin specifying the {\bb mung} exchange strings is not mandatory.)
  822. XThe second fields of the report (the actual count produced by
  823. X{\bb mung})
  824. Xis stripped out from the
  825. Xoutput by {\myit fcat\/}, and is summed by {\myit add\/}.
  826. X\L
  827. XThe overall structure of the {\bb mung\/} program is simple.
  828. XAfter checking and translating the command line,
  829. Xa transform is attempted on each file of the
  830. Xcommand line in turn: the file name is
  831. Xpassed to the {\bb mung()\/} subroutine, and {\myit main()\/}
  832. Xexpectantly awaits the return value.  If the return is zero, the
  833. Xfile did not contain the source string, the file has not been
  834. Xaltered, and no reporting is done.  If the return is positive, the
  835. Xreturn
  836. Xvalue is the number of converted strings in the file, also
  837. Xreported to \stout.
  838. X\L
  839. XNegative returns signal that some error occurred, and {\myit main()\/}
  840. Xmust now decide whether the error means potential file damage
  841. X(in which case to notify the user and exit), or less serious trouble
  842. X(print a warning and proceed with the next file).  The {\bb mung()\/}
  843. Xsubroutine itself relies on ILIB functions and passes
  844. X{\bb ierflag\/} to {\myit main()\/}; but in one case it calls
  845. X{\myit realloc()\/} directly, referring the task of building the
  846. Xerror message after a {\myit realloc()\/} error, to \IT.
  847. X
  848. X\S{mung.c PROGRAM TEXT}
  849. X\listing{../iex/mung.c}
  850. X\eject
  851. SHAR_EOF
  852. $TOUCH -am 0607095490 iman/ierror.tex &&
  853. chmod 0644 iman/ierror.tex ||
  854. echo "restore of iman/ierror.tex failed"
  855. set `wc -c iman/ierror.tex`;Wc_c=$1
  856. if test "$Wc_c" != "14309"; then
  857.     echo original size 14309, current size $Wc_c
  858. fi
  859. # ============= iman/iexamplist.tex ==============
  860. echo "x - extracting iman/iexamplist.tex (Text)"
  861. sed 's/^X//' << 'SHAR_EOF' > iman/iexamplist.tex &&
  862. X\def\name{TABLE 3}
  863. X
  864. X\S{Summary of Program Examples}
  865. X{
  866. X\settabs\+{mmmmmmmmm} &
  867. X manage stacks of /tmp files for stdout segments, report filenames &\cr
  868. X
  869. X\+{\bb add}&
  870. Xadd leading number of each line of input, print total
  871. X&\hfill IROUND\cr
  872. X
  873. X\+{\bb area}&
  874. Xarea code data base lookup
  875. X&\hfill ISEARCH\cr
  876. X
  877. X\+{\bb banner}&
  878. Xoutput command line strings in large letters
  879. X&\hfill IFROMBIT\cr
  880. X
  881. X\+{\bb behead}&
  882. Xin place, delete leading lines of files
  883. X&\hfill IEXPECT\cr
  884. X
  885. X\+{\bb char}&
  886. Xoutput space separated character lists
  887. X&\hfill IFONETIC\cr
  888. X
  889. X\+{\bb cnum}&
  890. Xcustom line numbering via command line options
  891. X&\hfill IOPT\cr
  892. X
  893. X\+{\bb cweed}&
  894. Xlist minimally spaced tokens of C text, garbage removed
  895. X&\hfill ISTRIPCOM\cr
  896. X
  897. X\+{\bb defilter}&
  898. Xreplace input file text of filter commands with their output
  899. X&\hfill IFONETIC\cr
  900. X
  901. X\+{\bb distrib}&
  902. Xcount strings in lines
  903. X&\hfill ICOUNT\cr
  904. X
  905. X\+{down}&
  906. Xtolower entire files, in place
  907. X&\hfill ILOWER\cr
  908. X
  909. X\+{\bb ego}&
  910. Xextract basename of file names
  911. X&\hfill IEGO\cr
  912. X
  913. X\+{egoext}&
  914. Xextract name.extension of file names
  915. X&\hfill IEGO\cr
  916. X
  917. X\+{ext}&
  918. Xextract extension substring of file names
  919. X&\hfill IEGO\cr
  920. X
  921. X\+{\bb excise}&
  922. Xdelete line(s) of files that contain a specified string
  923. X&\hfill ICUE\cr
  924. X
  925. X\+{\bb fcat}&
  926. Xprint fields from lines of input, concatenated by spaces
  927. X&\hfill ITOK\cr
  928. X
  929. X\+{fpath}&
  930. Xlist all files visible from \dol PATH or from given paths
  931. X&\hfill ITOK\cr
  932. X
  933. X\+{\bb group}&
  934. Xwrite alphanumerically sorted lines to \stout, in groups
  935. X&\hfill IFAMILY\cr
  936. X
  937. X\+{\bb inum}&
  938. Xinteractively specify custom line numbering sequence
  939. X&\hfill IINPUT\cr
  940. X
  941. X\+{\bb isdef}&
  942. Xfind token in list of definitions recognized by C compiler
  943. X&\hfill ICUE\cr
  944. X
  945. X\+{\bb lcat}&
  946. Xcat line {\myit N\/} or lines {\myit N\/} through {\myit N+M\/} of input to \stout
  947. X&\hfill ILIST\cr
  948. X
  949. X\+{leftrunc}&
  950. Xskipping left {\myit N\/} columns, output remaining columns of input
  951. X&\hfill IHASH\cr
  952. X
  953. X\+{\bb lhash}&
  954. Xoutput formatted sum of bytes of each line of input
  955. X&\hfill IHASH\cr
  956. X
  957. X\+{\bb mung}&
  958. Xtransform every {\myit string1\/} within files into {\myit string2\/}
  959. X&\hfill IERROR\cr
  960. X
  961. X\+{\bb ncat}&
  962. Xline number, append spooler string before each file
  963. X&\hfill ILINE\cr
  964. X
  965. X\+{\bb nest}&
  966. Xprint line-numbered list of delimiter pair contained in file
  967. X&\hfill INEST\cr
  968. X
  969. X\+{nestinfo}&
  970. Xprint nesting information for delimiter pair contained in file
  971. X&\hfill INEST\cr
  972. X
  973. X\+{now}&
  974. Xcurrent time anywhere around the world
  975. X&\hfill ISEARCH\cr
  976. X
  977. X\+{\bb nulcat}&
  978. Xwrite input to \stout, changing NUL bytes to `@'
  979. X&\hfill IDUMP\cr
  980. X
  981. X\+{\bb objed}&
  982. Xbinary file editor
  983. X&\hfill IWRITE\cr
  984. X
  985. X\+{path}&
  986. Xextract path (directory) component of file names
  987. X&\hfill IEGO\cr
  988. X
  989. X\+{\bb pathfiles}&
  990. Xprint instructions on using {\myit fpath, rpath, wpath, xpath\/}
  991. X&\hfill ITOK\cr
  992. X
  993. X\+{\bb q}&
  994. Xstack {\mytt /tmp} files for \stout\ segments, report filename
  995. X&\hfill INTRO\cr
  996. X
  997. X\+{\bb rename}&
  998. Xchange string {\myit pattern1\/} in filenames to {\myit pattern2\/}
  999. X&\hfill IMATCH\cr
  1000. X
  1001. X\+{\bb rewrap}&
  1002. Xin place, re-list tokens of file
  1003. X&\hfill IALNTOK\cr
  1004. X
  1005. X\+{\bb rot}&
  1006. Xprint instructions on using {\myit rotl, rotol, rotor, rotr\/}
  1007. X&\hfill IROTATE\cr
  1008. X
  1009. X\+{rotl}&
  1010. Xrotate input text 90 degrees to the left (counterclockwise)
  1011. X&\hfill IROTATE\cr
  1012. X
  1013. X\+{rotol}&
  1014. Xrotate text 90 degrees to the left, and over (upside down)
  1015. X&\hfill IROTATE\cr
  1016. X
  1017. X\+{rotor}&
  1018. Xrotate text 90 degrees to the right, and over
  1019. X&\hfill IROTATE\cr
  1020. X
  1021. X\+{rotr}&
  1022. Xrotate input text 90 degrees to the right
  1023. X&\hfill IROTATE\cr
  1024. X
  1025. X\+{rpath}&
  1026. Xlist all readable files of \dol PATH or of given paths
  1027. X&\hfill ITOK\cr
  1028. X
  1029. X\+{\bb scat}&
  1030. Xreprint input from line containing {\myit string1\/}, stop at line of {\myit string2\/}
  1031. X&\hfill ILIST\cr
  1032. X
  1033. X\+{\bb stamp}&
  1034. Xprint four-digit stamp of current month/day
  1035. X&\hfill IDATE\cr
  1036. X
  1037. X\+{\bb trunc}&
  1038. Xreprint lines of input only until given column number N
  1039. X&\hfill IHASH\cr
  1040. X
  1041. X\+{uhash}&
  1042. Xoutput formatted sum of upcased bytes of each line of input
  1043. X&\hfill IHASH\cr
  1044. X
  1045. X\+{\bb unlink}&
  1046. Xbrute force file remove
  1047. X&\hfill IMODE\cr
  1048. X
  1049. X\+{\bb untab}&
  1050. Xfor each tab, substitute spaces until next {\myit tabstop\/}
  1051. X&\hfill IREAD\cr
  1052. X
  1053. X\+{\bb up}&
  1054. Xtoupper entire files, in place
  1055. X&\hfill ILOWER\cr
  1056. X
  1057. X\+{\bb which}&
  1058. Xlocate a file
  1059. X&\hfill IWHICH\cr
  1060. X
  1061. X\+{wpath}&
  1062. Xlist all writable files of \dol PATH or of given paths
  1063. X&\hfill ITOK\cr
  1064. X
  1065. X\+{\bb xec}&
  1066. Xexec a command
  1067. X&\hfill IFONETIC\cr
  1068. X
  1069. X\+{xpath}&
  1070. Xlist all executable files of \dol PATH or of given paths
  1071. X&\hfill ITOK\cr
  1072. X}
  1073. X\eject
  1074. SHAR_EOF
  1075. $TOUCH -am 0601145590 iman/iexamplist.tex &&
  1076. chmod 0644 iman/iexamplist.tex ||
  1077. echo "restore of iman/iexamplist.tex failed"
  1078. set `wc -c iman/iexamplist.tex`;Wc_c=$1
  1079. if test "$Wc_c" != "4338"; then
  1080.     echo original size 4338, current size $Wc_c
  1081. fi
  1082. # ============= iman/iexpect.tex ==============
  1083. echo "x - extracting iman/iexpect.tex (Text)"
  1084. sed 's/^X//' << 'SHAR_EOF' > iman/iexpect.tex &&
  1085. X% XREF iexpect inextl istartl
  1086. X
  1087. X\def\name{IEXPECT}
  1088. X\def\IT{{\bb iexpect()}}
  1089. X
  1090. X\S{NAME}
  1091. X\hangindent=.75in \hangafter=1
  1092. X{\bb iexpect} --- confirm or deny that the first token in the nearest
  1093. Xline that does not begin with {\myit skiptok\/}, is a match for the
  1094. Xexpected {\myit word\/}
  1095. X\smallskip
  1096. X{\bb inextl} --- return location of first token in the line after the
  1097. Xcurrent line
  1098. X
  1099. X{\bb istartl} --- find nearest line that starts with a given
  1100. X{\myit word\/}
  1101. X
  1102. X\S{SYNOPSIS}
  1103. X\settabs\+{\bb mdefine SHORTUPMO} &\cr
  1104. X\+{\bb\#define IANYTOK}&\hfill{\bb0}\cr
  1105. X\+{\bb\#define IALNTOK}&\hfill{\bb1}\cr
  1106. X\+{\bb\#define ICTOK}&\hfill{\bb2}\cr
  1107. X\L
  1108. X{\obeylines \bb
  1109. Xint
  1110. Xiexpect (start, end, word, skiptok, toktype)
  1111. Xchar *start;
  1112. Xchar *end;
  1113. Xchar *word;
  1114. Xchar *skiptok;
  1115. Xint toktype;
  1116. X\L
  1117. Xchar *
  1118. Xinextl (start, end, skiptok)
  1119. Xchar *start;
  1120. Xchar *end;
  1121. Xchar *skiptok;
  1122. X\L
  1123. Xchar *
  1124. Xistartl (start, end, word)
  1125. Xchar *start;
  1126. Xchar *end;
  1127. Xchar *word;
  1128. X}
  1129. X
  1130. X\S{DESCRIPTION}
  1131. XBoth \IT\ and {\bb istartl()} are
  1132. Xused to match a chosen {\myit word\/} against the first token in
  1133. Xsuccessive lines in an internal buffer.
  1134. XDuring \IT, the search ends at the first token of the buffer
  1135. Xwhether or not the token matches the expected
  1136. X{\myit word\/}, except when the token matches {\myit skiptok\/}.
  1137. XIn this case, the entire line that began with {\myit skiptok\/} is
  1138. Xbypassed, and token-recognition continues from the next line.
  1139. X\L
  1140. XDuring {\bb istartl()}
  1141. Xthe beginning of each successive line is examined for a
  1142. Xmatch to {\myit word\/};
  1143. Xif there are no tokens in the currently examined
  1144. Xline or if the first token in the
  1145. Xcurrent line does not match the specified {\myit word\/},
  1146. Xthe line is skipped
  1147. Xand the search resumes at the start of the next line.
  1148. X\L
  1149. X{\bb inextl()} simply advances a pointer
  1150. Xpast the first {\myit newline\/},
  1151. Xand then parks it at the next printing (non-white)
  1152. Xcharacter.  If this printing character leads a string that exactly
  1153. Xmatches {\myit skiptok\/}, the pointer is again advanced past the line,
  1154. Xand the process repeats in the next line, until a line is found that
  1155. Xdoesn't begin with
  1156. Xthe {\myit skiptok\/} substring.
  1157. X\L
  1158. XThe searched buffer begins at
  1159. X{\myit start\/} and ends one byte before {\myit end\/}.
  1160. XThe passed reference {\myit word\/}
  1161. Xand {\myit skiptok\/}
  1162. Xare both character strings each terminated by a NUL byte.
  1163. XIf {\myit word\/} is {\myit (char~*)NULL\/}
  1164. Xor if it is a zero-length string,
  1165. X\IT\ does no processing and returns~0.
  1166. X{\bb istartl()\/} on the other hand returns
  1167. X{\myit (char~*)NULL\/} and reports the incident via {\myit ierror()\/}.
  1168. XA {\myit (char~*)NULL\/} or a
  1169. Xzero-length string for
  1170. X{\myit skiptok\/} may be passed to \IT\ or to {\bb inextl()}
  1171. Xin a normal manner.
  1172. X\L
  1173. XThe {\myit toktype\/} parameter passed to \IT\ specifies token-forming
  1174. Xrules and character sets for tokens.
  1175. XThe ILIB {\myit include file ''ilib.h''\/} defines high level
  1176. Xconstants IANYTOK, IALNTOK, and ICTOK as possible values for
  1177. X{\myit toktype\/}.
  1178. XOther values default to IANYTOK.
  1179. XDepending on the value chosen, \IT\ calls on one of the ILIB
  1180. Xfunctions {\myit ianytok(), ialntok()\/}, or
  1181. X{\myit ictok()\/} for constructing the token.
  1182. X{\myit ianytok()\/} considers any successive
  1183. Xprinting character a legitimate member
  1184. Xof the token byte string.
  1185. X{\myit ialntok()\/} creates multi-byte tokens from
  1186. Xalphanumeric characters, or single-byte tokens from non-alphanumeric
  1187. Xprinting characters.
  1188. X{\myit ictok()\/} expands the alphanumeric character set used
  1189. Xby
  1190. X{\myit ialntok()\/} with the underscore \key{\und} and dollar
  1191. X\key{\$} characters, and also recognizes C operators and
  1192. Xcharacter constants as separate tokens.
  1193. X\L
  1194. XInteger values returned by \IT\ may be positive, negative, or zero.
  1195. XA returned zero informs the caller that the buffer contained no
  1196. Xtokens whatsoever (or that \IT\ has received an illegal {\myit word\/}
  1197. Xparameter).
  1198. XA positive return indicates that the first valid token was a match
  1199. Xfor {\myit word\/}; the integer value is the
  1200. Xoffset from {\myit start\/},
  1201. Xof the {\myit newline\/} character at the end of the line
  1202. Xin which the expected {\myit word\/} was found.
  1203. XA negative return indicates that the first valid token did not match
  1204. X{\myit word\/};
  1205. Xthe absolute value of the return is the offset from {\myit start\/},
  1206. Xof the {\myit newline\/} character at the end of the line
  1207. Xin which the valid non-matching token was found.
  1208. X\L
  1209. X\IT\ is most useful for parsing past ``comment lines'' beginning
  1210. Xwith a single-byte {\myit comment delimiter\/} and ending with
  1211. X{\myit newline\/}, or for bypassing line-starting
  1212. X``format specifications'' (for example the leading periods of
  1213. X{\myit nroff\/}).
  1214. XTo skip records with fixed {\myit header\/} keywords in various
  1215. Xfixed format text data files, {\bb istartl()} is more convenient.
  1216. X\L
  1217. X{\bb istartl()} uses a simpler algorithm for matching tokens against
  1218. Xthe specified {\myit word\/}.  The byte count of {\myit word\/} is
  1219. Xstored in a temporary variable {\myit length\/}.
  1220. XThe parser is advanced to the first
  1221. Xprinting character of the examined line; a comparison
  1222. X(using the standard {\myit strncmp()\/} function)
  1223. Xis performed at this point
  1224. Xof {\myit word\/} against a {\myit length\/} number of successive
  1225. Xbytes of the line.
  1226. XThis implies that {\myit word\/} can be an arbitrary aggregate of
  1227. Xnon-NUL ASCII bytes, potentially a cluster of words; but must begin
  1228. Xwith a {\myit printing character\/}.
  1229. XAlso, a line in the buffer will match {\myit word\/} even if
  1230. X{\myit word\/} is a substring of a longer line-starting token:
  1231. Xa line beginning with ``plaintiff'' will match the {\myit word\/}
  1232. X``plain'', for example.
  1233. X\L
  1234. X{\bb istartl()} does not perform a final
  1235. Xcomparison if the length of {\myit word\/} would mean
  1236. Xcomparing past the end of the buffer.
  1237. X{\bb istartl()} returns a pointer dereferencing the leading token of the
  1238. Xfirst line that matched {\myit word\/}, or a pointer to NULL
  1239. Xif no match was found.
  1240. X\L
  1241. XThe returned pointer from the {\bb inextl()} function
  1242. Xsimply points to {\myit end\/} if a legal token
  1243. X(leading a {\myit printing\/} string in
  1244. Xthe next line) could not be found, but it will point
  1245. Xto NULL if the line does not end with a {\myit newline\/}
  1246. Xas {\myit end\/} is encountered.
  1247. X
  1248. X\S{SEE ALSO}
  1249. X{\myit imatch, ialntok.}
  1250. X
  1251. X\S{behead PROGRAM EXAMPLE}
  1252. XThe stark name of the {\bb behead} program aptly describes the
  1253. Xradical and irreversable changes it inflicts on the file in its
  1254. Xcommand line parameter list.  {\bb behead} reads each file in turn,
  1255. Xinto an internal buffer; advances a pointer past an integral number of
  1256. Xbeginning text lines; then writes all text from the pointer until the
  1257. Xend of the buffer, back to the file (on disk).  The net effect is to
  1258. Xdiscard of a number of leading text lines from each file.
  1259. X\L
  1260. XThe command name must be followed by a decimal
  1261. X{\myit number\/} or a {\myit string\/},
  1262. Xpreceding the list of files subjected to the operation.  When a
  1263. X{\myit number\/} is given, it is taken to be the number of lines
  1264. Xthat should
  1265. Xbe removed from the beginning of files.  The assumed line delimiter
  1266. Xis the {\myit newline\/} character.  If the selected file has less
  1267. Xlines than (or has an equal number of lines to)
  1268. Xthe number of lines to be removed, the file is
  1269. Xtruncated to zero length.
  1270. X\L
  1271. XIf the command name is followed by a
  1272. X{\myit string\/} instead of a {\myit number\/},
  1273. Xthis {\myit string\/}
  1274. Xcorresponds to the {\myit word\/} parameter of a call to
  1275. X{\bb istartl()} described above.  The text of each file in turn,
  1276. Xis searched to find
  1277. Xthe first line that begins (following optional {\myit white spaces\/})
  1278. Xwith an exact match to the specified {\myit string\/}.  Lines of the
  1279. Xexamined file that preceded the line in which the match was found,
  1280. Xare removed.  If lines of a file do not produce a match, the file
  1281. Xis left intact, and the incident triggers a warning message
  1282. Xprinted to \sterr.
  1283. X\L
  1284. XA minor drawback of the command line syntax is that a positive
  1285. Xinteger cannot be taken as a request to delete lines ending at
  1286. Xthe line that begins with the
  1287. Xdigit string of the integer:
  1288. X\smallskip
  1289. X\I{\mytt behead 15 foo}
  1290. X\smallskip
  1291. Xremoves the first 15 lines of {\myit foo\/} instead of searching
  1292. Xfor a line in {\myit foo\/} that begin with the bytes ``15''.
  1293. XA similar drawback inherent in the {\bb istartl()} process is that
  1294. Xa specified {\myit string\/} cannot begin with {\myit white space\/}.
  1295. X
  1296. X\S{behead.c PROGRAM TEXT}
  1297. X\listing{../iex/behead.c}
  1298. X\eject
  1299. SHAR_EOF
  1300. $TOUCH -am 0607103590 iman/iexpect.tex &&
  1301. chmod 0644 iman/iexpect.tex ||
  1302. echo "restore of iman/iexpect.tex failed"
  1303. set `wc -c iman/iexpect.tex`;Wc_c=$1
  1304. if test "$Wc_c" != "8261"; then
  1305.     echo original size 8261, current size $Wc_c
  1306. fi
  1307. echo "End of part 6, continue with part 7"
  1308. exit 0
  1309. -- 
  1310.         Istvan Mohos
  1311.         ...uunet!pyrdc!pyrnj!hhb!istvan
  1312.         RACAL-REDAC/HHB 1000 Wyckoff Ave. Mahwah NJ 07430 201-848-8000
  1313. ======================================================================
  1314.