home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume2 / tools / part5 < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  47.1 KB

  1. Subject: Software Tools in Pascal (Part 4 of 6)
  2. From: ihnp4!mnetor!clewis (Chris Lewis)
  3. Newsgroups: mod.sources
  4. Approved: john@genrad.UUCP
  5.  
  6. Mod.sources:  Volume 2, Issue 10
  7. Submitted by: ihnp4!mnetor!clewis (Chris Lewis)
  8.  
  9. #!/bin/sh
  10. echo 'Start of pack.out, part 04 of 06:'
  11. echo 'x - ckglob.pascal'
  12. sed 's/^X//' > ckglob.pascal << '/'
  13. X{
  14. X    Copyright (c) 1981
  15. X    By:    Bell Telephone Laboratories, Inc. and
  16. X        Whitesmiths, Ltd.,
  17. X
  18. X    This software is derived from the book
  19. X        "Software Tools In Pascal", by
  20. X        Brian W. Kernighan and P.J. Plauger
  21. X        Addison-Wesley, 1981
  22. X        ISBN 0-201-10342-7
  23. X
  24. X    Right is hereby granted to freely distribute or duplicate this
  25. X    software, providing distribution or duplication is not for profit
  26. X    or other commerical gain and that this copyright notice remains 
  27. X    intact.
  28. X}
  29. X{ CkGlob -- if global prefix, mark lines to be affected }
  30. Xsegment CkGlob;
  31. X%include swtools
  32. X%include editcons
  33. X%include edittype
  34. X%include editproc
  35. X%include editref
  36. X%include matchdef
  37. Xfunction CkGlob;
  38. Xvar
  39. X    n: Integer;
  40. X    gFlag: Boolean;
  41. X    temp: StringType;
  42. Xbegin
  43. X    if (lin[i] <> GCMD) and (lin[i] <> XCMD) then
  44. X        status := ENDDATA
  45. X    else begin
  46. X        gFlag := (lin[i] = GCMD);
  47. X        i := i + 1;
  48. X        if (OptPat(lin, i) = ERR) then
  49. X            status := ERR
  50. X        else if (Default(1, lastLn, status) <> ERR) then begin
  51. X            i := i + 1;   { mark affected lines }
  52. X            for n := line1 to line2 do begin
  53. X                GetTxt(n, temp);
  54. X                PutMark(n, (Match(temp, pat) = gFlag))
  55. X            end;
  56. X            for n := 1 to line1-1 do { erase other marks }
  57. X                PutMark(n, false);
  58. X            for n := line2+1 to lastLn do
  59. X                PutMark(n, false);
  60. X            status := OK
  61. X        end
  62. X    end;
  63. X    CkGlob := status
  64. Xend;
  65. /
  66. echo 'x - define.pascal'
  67. sed 's/^X//' > define.pascal << '/'
  68. X{
  69. X    Copyright (c) 1981
  70. X    By:    Bell Telephone Laboratories, Inc. and
  71. X        Whitesmiths, Ltd.,
  72. X
  73. X    This software is derived from the book
  74. X        "Software Tools In Pascal", by
  75. X        Brian W. Kernighan and P.J. Plauger
  76. X        Addison-Wesley, 1981
  77. X        ISBN 0-201-10342-7
  78. X
  79. X    Right is hereby granted to freely distribute or duplicate this
  80. X    software, providing distribution or duplication is not for profit
  81. X    or other commerical gain and that this copyright notice remains 
  82. X    intact.
  83. X}
  84. X{ Define -- simple string replacement macro processor }
  85. Xprogram Define;
  86. X%include swtools
  87. X%include defdef
  88. X%include defvar
  89. X%include defproc
  90. X{ InitDef -- initialize variables for define }
  91. Xprocedure InitDef;
  92. Xbegin
  93. X    CvtSST('define', defName);
  94. X    bp := 0;        { push back buffer pointer }
  95. X    InitHash
  96. Xend;
  97. Xbegin
  98. X    ToolInit;
  99. X    null[1] := ENDSTR;
  100. X    InitDef;
  101. X    Install(defName, null, DEFTYPE);
  102. X    while (GetTok(token, MAXTOK) <> ENDFILE) do
  103. X        if (not IsLetter(token[1])) then
  104. X            PutStr(token, STDOUT)
  105. X        else if (not Lookup(token, defn, tokType)) then
  106. X            PutStr(token, STDOUT)   { undefined }
  107. X        else if (tokType = DEFTYPE) then begin { defn }
  108. X            GetDef(token, MAXTOK, defn, MAXDEF);
  109. X            Install(token, defn, MACTYPE)
  110. X        end
  111. X        else
  112. X            PBStr(defn)      { push back replacement string }
  113. Xend.
  114. /
  115. echo 'x - dodash.pascal'
  116. sed 's/^X//' > dodash.pascal << '/'
  117. X{
  118. X    Copyright (c) 1981
  119. X    By:    Bell Telephone Laboratories, Inc. and
  120. X        Whitesmiths, Ltd.,
  121. X
  122. X    This software is derived from the book
  123. X        "Software Tools In Pascal", by
  124. X        Brian W. Kernighan and P.J. Plauger
  125. X        Addison-Wesley, 1981
  126. X        ISBN 0-201-10342-7
  127. X
  128. X    Right is hereby granted to freely distribute or duplicate this
  129. X    software, providing distribution or duplication is not for profit
  130. X    or other commerical gain and that this copyright notice remains 
  131. X    intact.
  132. X}
  133. X{ DoDash -- expand set at src(i) into dest(j), stop at delim }
  134. Xsegment DoDash;
  135. X%include swtools
  136. X%include patdef
  137. Xprocedure DoDash;
  138. Xvar
  139. X    k: CharType;
  140. X    junk: Boolean;
  141. Xbegin
  142. X    while (src[i] <> delim) and (src[i] <> ENDSTR) do begin
  143. X        if (src[i] = ESCAPE) then
  144. X            junk := AddStr(Esc(src,i), dest, j, maxSet)
  145. X        else if (src[i] <> DASH) then
  146. X            junk := AddStr(src[i], dest, j, maxSet)
  147. X        else if (j <= 1) or (src[i+1] = ENDSTR) then
  148. X            junk := AddStr(DASH, dest, j, maxSet) { literal -}
  149. X        else if IsAlphaNum(src[i-1]) and
  150. X          IsAlphaNum(src[i+1]) and
  151. X          (src[i-1] <= src[i+1]) then begin
  152. X            for k := Succ(src[i-1]) to src[i+1] do
  153. X                { the following obscenity is due to EBCDIC "holes" }
  154. X                if IsAlphaNum(k) then begin
  155. X                    junk := AddStr(k, dest, j, maxSet);
  156. X                end;
  157. X            i := i + 1
  158. X        end
  159. X        else
  160. X            junk := AddStr(DASH, dest, j, maxSet);
  161. X        i := i + 1
  162. X    end
  163. Xend;
  164. /
  165. echo 'x - dooption.pascal'
  166. sed 's/^X//' > dooption.pascal << '/'
  167. X{
  168. X    Copyright (c) 1981
  169. X    By:    Bell Telephone Laboratories, Inc. and
  170. X        Whitesmiths, Ltd.,
  171. X
  172. X    This software is derived from the book
  173. X        "Software Tools In Pascal", by
  174. X        Brian W. Kernighan and P.J. Plauger
  175. X        Addison-Wesley, 1981
  176. X        ISBN 0-201-10342-7
  177. X
  178. X    Right is hereby granted to freely distribute or duplicate this
  179. X    software, providing distribution or duplication is not for profit
  180. X    or other commerical gain and that this copyright notice remains 
  181. X    intact.
  182. X}
  183. X{ DoOption -- build options for the swtools editor }
  184. Xsegment DoOption;
  185. X%include swtools
  186. X%include editcons
  187. X%include edittype
  188. X%include editproc
  189. Xdef
  190. X    optionFlags: set of promptFlag..numFlag;
  191. Xvalue
  192. X    optionFlags := [];
  193. Xfunction DoOption;
  194. Xvar
  195. X    optSel: promptFlag..numFlag;
  196. X    setting: Boolean;
  197. Xbegin
  198. X    DoOption := OK;   { error handling done here }
  199. X    i := i + 1;
  200. X    if (lin[i] = NEWLINE) or (lin[i+1] = NEWLINE) then
  201. X        Message('Bad option string')
  202. X    else begin
  203. X        if lin[i+1] in [LETS, BIGS] then      setting := true
  204. X        else if lin[i+1] in [LETC, BIGC] then setting := false
  205. X        else begin
  206. X            Message('You must [s]et or [c]lear the option');
  207. X            return
  208. X        end;
  209. X        case lin[i] of
  210. X            LETP, BIGP:
  211. X                optSel := promptFlag;
  212. X            LETM, BIGM:
  213. X                optSel := noMetaFlag;
  214. X            LETV, BIGV:
  215. X                optSel := verboseFlag;
  216. X            LETN, BIGN:
  217. X                optSel := numFlag
  218. X            otherwise
  219. X                begin
  220. X                     Message('You gave an illegal option');
  221. X                     Message('available options are:');
  222. X                     Message('ps/pc: turn on/off prompting');
  223. X                     Message('vs/vc: turn on/off verbose mode');
  224. X                     Message('ns/nc: turn on/off line numbers');
  225. X                     Message('ms/mc: turn on/off stupid matching');
  226. X                     return
  227. X                end
  228. X        end;
  229. X        if setting then
  230. X            optionFlags := optionFlags + [optSel]
  231. X        else
  232. X            optionFlags := optionFlags - [optSel]
  233. X    end
  234. Xend;
  235. Xfunction OptIsOn;
  236. Xbegin
  237. X    if flag in optionFlags then OptIsOn := true
  238. X                           else OptIsOn := false
  239. Xend;
  240. /
  241. echo 'x - doread.pascal'
  242. sed 's/^X//' > doread.pascal << '/'
  243. X{
  244. X    Copyright (c) 1981
  245. X    By:    Bell Telephone Laboratories, Inc. and
  246. X        Whitesmiths, Ltd.,
  247. X
  248. X    This software is derived from the book
  249. X        "Software Tools In Pascal", by
  250. X        Brian W. Kernighan and P.J. Plauger
  251. X        Addison-Wesley, 1981
  252. X        ISBN 0-201-10342-7
  253. X
  254. X    Right is hereby granted to freely distribute or duplicate this
  255. X    software, providing distribution or duplication is not for profit
  256. X    or other commerical gain and that this copyright notice remains 
  257. X    intact.
  258. X}
  259. X{ DoRead -- read "fil" after line n }
  260. Xsegment DoRead;
  261. X%include swtools
  262. X%include editcons
  263. X%include edittype
  264. X%include editproc
  265. X%include editref
  266. Xfunction DoRead;
  267. Xvar
  268. X    count: Integer;
  269. X    t: Boolean;
  270. X    stat: STCode;
  271. X    fd: FileDesc;
  272. X    inLine: StringType;
  273. Xbegin
  274. X    fd := FOpen(fil, IOREAD);
  275. X    if (fd = IOERROR) then
  276. X        stat := ERR
  277. X    else begin
  278. X        curLn := n;
  279. X        stat := OK;
  280. X        count := 0;
  281. X        repeat
  282. X            t := GetLine(inLine, fd, MAXSTR);
  283. X            if (t) then begin
  284. X                stat := PutTxt(inLine);
  285. X                if (stat <> ERR) then
  286. X                    count := count + 1
  287. X            end
  288. X        until (stat <> OK) or (t = false);
  289. X        FClose(fd);
  290. X        PutDec(count, 1);
  291. X        PutC(NEWLINE);
  292. X    end;
  293. X    DoRead := stat
  294. Xend;
  295. /
  296. echo 'x - dosub.pascal'
  297. sed 's/^X//' > dosub.pascal << '/'
  298. X{
  299. X    Copyright (c) 1981
  300. X    By:    Bell Telephone Laboratories, Inc. and
  301. X        Whitesmiths, Ltd.,
  302. X
  303. X    This software is derived from the book
  304. X        "Software Tools In Pascal", by
  305. X        Brian W. Kernighan and P.J. Plauger
  306. X        Addison-Wesley, 1981
  307. X        ISBN 0-201-10342-7
  308. X
  309. X    Right is hereby granted to freely distribute or duplicate this
  310. X    software, providing distribution or duplication is not for profit
  311. X    or other commerical gain and that this copyright notice remains 
  312. X    intact.
  313. X}
  314. X{ DoSub -- Select substring }
  315. Xsegment DoSub;
  316. X%include swtools
  317. X%include macdefs
  318. X%include macproc
  319. Xprocedure DoSub;
  320. Xvar
  321. X    ap, fc, k, nc: Integer;
  322. X    temp1, temp2: StringType;
  323. Xbegin
  324. X    if (j - i >= 3) then begin
  325. X        if (j - i < 4) then
  326. X            nc := MAXTOK
  327. X        else begin
  328. X            CsCopy(evalStk, argStk[i+4], temp1);
  329. X            k := 1;
  330. X            nc := Expr(temp1, k)
  331. X        end {if};
  332. X        CsCopy(evalStk, argStk[i+3], temp1); { origin }
  333. X        ap := argStk[i+2];   { target string }
  334. X        k := 1;
  335. X        fc := ap + Expr(temp1, k) - 1;  { first char }
  336. X        CsCopy(evalStk, ap, temp2);
  337. X        if (fc >= ap) and (fc < ap + StrLength(temp2)) then begin
  338. X            CsCopy(evalStk, fc, temp1);
  339. X            for k := fc + Min(nc, StrLength(temp1))-1 downto fc do
  340. X                PutBack(evalStk[k])
  341. X        end {if}
  342. X    end {if}
  343. Xend {DoSub};
  344. /
  345. echo 'x - expand.pascal'
  346. sed 's/^X//' > expand.pascal << '/'
  347. X{
  348. X    Copyright (c) 1982
  349. X    By:    Chris Lewis
  350. X
  351. X    Right is hereby granted to freely distribute or duplicate this
  352. X    software, providing distribution or duplication is not for profit
  353. X    or other commerical gain and that this copyright notice remains 
  354. X    intact.
  355. X}
  356. X{ Expand -- Expand a file by a specified factor }
  357. Xprogram Expand;
  358. X%include swtools
  359. Xconst maxWidth = 2000;
  360. Xvar
  361. X    arguments: StringType;
  362. X    outBuffer: array [1..maxWidth] of Char;
  363. X    inPtr: Integer;
  364. X    anchor: Integer;
  365. X    i: Integer;
  366. X    factor: Integer;
  367. X    index: Integer;
  368. X    j: Integer;
  369. Xbegin
  370. X    ToolInit;
  371. X    index := 1;
  372. X    if GetArg(1, arguments, MAXSTR) then begin
  373. X        factor := CToI(arguments, index);
  374. X        if factor = 0 then
  375. X            Error('Argument to Expand should be numeric, > 0');
  376. X    end
  377. X    else
  378. X        factor := 1;
  379. X    while true do begin
  380. X        inPtr := 1;
  381. X        { read an input line, expanding on the fly }
  382. X        while (GetC(outBuffer[inPtr]) <> ENDFILE) do begin
  383. X            if outBuffer[inPtr] = NEWLINE then leave;
  384. X            anchor := inPtr;
  385. X            for j := 1 to factor - 1 do begin
  386. X                inPtr := inPtr + 1;
  387. X                outBuffer[inPtr] := outBuffer[anchor];
  388. X            end; {for}
  389. X            inPtr := inPtr + 1;
  390. X        end; {while}
  391. X        if outBuffer[inPtr] = ENDFILE then leave;
  392. X        { output expanded array twice }
  393. X        for j := 1 to factor do
  394. X            for i := 1 to inPtr do
  395. X                PutC(outBuffer[i]);
  396. X    end; {while}
  397. Xend. {Expand}
  398. /
  399. echo 'x - fopen.pascal'
  400. sed 's/^X//' > fopen.pascal << '/'
  401. X{
  402. X    Copyright (c) 1982
  403. X    By:    Chris Lewis
  404. X
  405. X    Right is hereby granted to freely distribute or duplicate this
  406. X    software, providing distribution or duplication is not for profit
  407. X    or other commerical gain and that this copyright notice remains 
  408. X    intact.
  409. X}
  410. X{ FOpen -- open a file }
  411. Xsegment FOpen;
  412. X%include swtools
  413. X%include cms
  414. X%include ioref
  415. Xfunction FOpen;
  416. Xvar
  417. X    returnCode: Integer;
  418. X    cmsString: String(MAXSTR);
  419. X    sName: String(MAXSTR);
  420. X    f: FileDesc;
  421. X    i: 1..MAXSTR;
  422. X    fixedName: StringType;
  423. Xbegin
  424. X    if mode = IOREAD then begin
  425. X        cmsString := 'STATE ';
  426. X        for i := 1 TO StrLength(name) do
  427. X            if name[i] in [NEWLINE, PERIOD] then
  428. X                cmsString := cmsString || Str(' ')
  429. X            else
  430. X                cmsString := cmsString || Str(name[i]);
  431. X        Cms(cmsString, returnCode);
  432. X        if returnCode <> 0 then begin
  433. X            FOpen := IOERROR;
  434. X            return
  435. X        end;
  436. X    end;
  437. X    i := 1;
  438. X    if (not GetFid(Name, i, fixedName)) then
  439. X        Error('Bad file name');
  440. X    CvtSTS(fixedName, sName);
  441. X    f := FDAlloc;
  442. X    if f = IOERROR then
  443. X        Error('Out of file descriptors')
  444. X    else begin
  445. X        openList[f].mode := mode;
  446. X        if mode = IOREAD then
  447. X            Reset(openList[f].fileVar, 'name=' || sName)
  448. X        else begin
  449. X            Remove(fixedName);
  450. X            ReWrite(openList[f].fileVar, 'name=' || sName);
  451. X        end;
  452. X        if ERRORIO then begin
  453. X            openList[f].mode := IOAVAIL;
  454. X            f := IOERROR;
  455. X            ERRORIO := false;
  456. X        end
  457. X    end;
  458. X    FOpen := f
  459. Xend;
  460. /
  461. echo 'x - getdef.pascal'
  462. sed 's/^X//' > getdef.pascal << '/'
  463. X{
  464. X    Copyright (c) 1981
  465. X    By:    Bell Telephone Laboratories, Inc. and
  466. X        Whitesmiths, Ltd.,
  467. X
  468. X    This software is derived from the book
  469. X        "Software Tools In Pascal", by
  470. X        Brian W. Kernighan and P.J. Plauger
  471. X        Addison-Wesley, 1981
  472. X        ISBN 0-201-10342-7
  473. X
  474. X    Right is hereby granted to freely distribute or duplicate this
  475. X    software, providing distribution or duplication is not for profit
  476. X    or other commerical gain and that this copyright notice remains 
  477. X    intact.
  478. X}
  479. X{ GetDef -- get name and definition }
  480. Xsegment GetDef;
  481. X%include swtools
  482. X%include defdef
  483. X%include defref
  484. X%include defproc
  485. Xprocedure GetDef;
  486. Xvar
  487. X    i, nlPar: Integer;
  488. X    c: CharType;
  489. Xbegin
  490. X    token[1] := ENDSTR;     { in case of bad input }
  491. X    defn[1] := ENDSTR;
  492. X    if (GetPBC(c) <> LPAREN) then
  493. X        Message('define: missing left paren')
  494. X    else if (not IsLetter(GetTok(token, tokSize))) then
  495. X        Message('define: non-alphanumeric name')
  496. X    else if (GetPBC(c) <> COMMA) then
  497. X        Message('define: missing comma in define')
  498. X    else begin      { got '(name,' so far }
  499. X        while (GetPBC(c) = BLANK) do
  500. X            ; { skip leading blanks }
  501. X        PutBack(c);   { went one too far }
  502. X        nlPar := 0;
  503. X        i := 1;
  504. X        while (nlPar >= 0) do begin
  505. X            defn[i] := GetPBC(c);
  506. X            if (i >= defSize) then
  507. X                Error('define: definition too long')
  508. X            else if (c = ENDFILE) then
  509. X                Error('define: missing right paren')
  510. X            else if (c = LPAREN) then
  511. X                nlPar := nlPar + 1
  512. X            else if (c = RPAREN) then
  513. X                nlPar := nlPar - 1;
  514. X            { else normal char in defn[i] }
  515. X            i := i + 1
  516. X        end;
  517. X        defn[i-1] := ENDSTR
  518. X    end
  519. Xend;
  520. /
  521. echo 'x - getfid.pascal'
  522. sed 's/^X//' > getfid.pascal << '/'
  523. X{
  524. X    Copyright (c) 1982
  525. X    By:    Chris Lewis
  526. X
  527. X    Right is hereby granted to freely distribute or duplicate this
  528. X    software, providing distribution or duplication is not for profit
  529. X    or other commerical gain and that this copyright notice remains 
  530. X    intact.
  531. X}
  532. X{ GetFid -- convert a string into a file name }
  533. Xsegment GetFid;
  534. X%include swtools
  535. X%include ioref
  536. Xfunction GetFid;
  537. Xvar
  538. X    nameIndex: 1..MAXSTR;
  539. X    temp: StringType;
  540. X    fMode: StringType;
  541. X    fType: StringType;
  542. X    i: 0..MAXSTR;
  543. X    j: 0..MAXSTR;
  544. Xbegin
  545. X    SCopy(line, idx, temp, 1);
  546. X    for nameIndex := 1 to StrLength(temp) do
  547. X        if (not (line[nameIndex] in
  548. X           [DOLLAR, LETA..LETZ, BIGA..BIGZ, DIG0..DIG9, BLANK])) then
  549. X            temp[nameIndex] := BLANK;
  550. X    i := GetWord(temp, 1, fileName);
  551. X    if i = 0 then begin
  552. X        GetFid := false;
  553. X        return;
  554. X    end;
  555. X    j := GetWord(temp, i, fType);
  556. X    if j = 0 then begin
  557. X        CvtSST('TEMP', fType);
  558. X        CvtSST('*', fMode);
  559. X    end
  560. X    else begin
  561. X        j := GetWord(temp, j, fMode);
  562. X        if j = 0 then
  563. X            CvtSST('*', fMode);
  564. X    end;
  565. X    i := StrLength(fileName);
  566. X    fileName[i+1] := PERIOD;
  567. X    SCopy(fType, 1, fileName, i + 2);
  568. X    i := StrLength(fileName);
  569. X    fileName[i+1] := PERIOD;
  570. X    SCopy(fMode, 1, fileName, i + 2);
  571. X    getFid := true;
  572. Xend;
  573. /
  574. echo 'x - getfn.pascal'
  575. sed 's/^X//' > getfn.pascal << '/'
  576. X{
  577. X    Copyright (c) 1981
  578. X    By:    Bell Telephone Laboratories, Inc. and
  579. X        Whitesmiths, Ltd.,
  580. X
  581. X    This software is derived from the book
  582. X        "Software Tools In Pascal", by
  583. X        Brian W. Kernighan and P.J. Plauger
  584. X        Addison-Wesley, 1981
  585. X        ISBN 0-201-10342-7
  586. X
  587. X    Right is hereby granted to freely distribute or duplicate this
  588. X    software, providing distribution or duplication is not for profit
  589. X    or other commerical gain and that this copyright notice remains 
  590. X    intact.
  591. X}
  592. X{ GetFn -- get file name from lin[i] .... }
  593. Xsegment GetFn;
  594. X%include swtools
  595. X%include editcons
  596. X%include edittype
  597. X%include editproc
  598. X%include editref
  599. Xfunction GetFn;
  600. Xvar
  601. X    k: Integer;
  602. X    stat: STCode;
  603. Xbegin
  604. X    stat := ERR;
  605. X    if (lin[i+1] = BLANK) then begin
  606. X        Scopy(lin, i+2, fil, 1);
  607. X        if fil[StrLength(fil)] = NEWLINE then
  608. X            fil[StrLength(fil)] := ENDSTR;
  609. X        stat := OK
  610. X    end
  611. X    else if (lin[i+1] = NEWLINE) and (saveFile[1] <> ENDSTR) then begin
  612. X        Scopy(saveFile, 1, fil, 1);
  613. X        stat := OK
  614. X    end;
  615. X    if (stat = OK) and (saveFile[1] = ENDSTR) then
  616. X       Scopy(fil, 1, saveFile, 1);    { save if no old one }
  617. X    k := 1;
  618. X    if stat = Ok then
  619. X        if (not GetFid(saveFile, k, saveFile)) then
  620. X            stat := ERR;
  621. X    GetFn := stat
  622. Xend;
  623. /
  624. echo 'x - getline.pascal'
  625. sed 's/^X//' > getline.pascal << '/'
  626. X{
  627. X    Copyright (c) 1981
  628. X    By:    Bell Telephone Laboratories, Inc. and
  629. X        Whitesmiths, Ltd.,
  630. X
  631. X    This software is derived from the book
  632. X        "Software Tools In Pascal", by
  633. X        Brian W. Kernighan and P.J. Plauger
  634. X        Addison-Wesley, 1981
  635. X        ISBN 0-201-10342-7
  636. X
  637. X    Right is hereby granted to freely distribute or duplicate this
  638. X    software, providing distribution or duplication is not for profit
  639. X    or other commerical gain and that this copyright notice remains 
  640. X    intact.
  641. X}
  642. X{ GetLine-- put string out on file }
  643. Xsegment GetLine;
  644. X%include swtools
  645. X%include ioref
  646. Xref termInput: Boolean;
  647. Xfunction GetKeyBoard(var str: StringType; maxSize: Integer): Boolean;
  648. X    forward;
  649. Xfunction GetLine;
  650. Xvar
  651. X    i: Integer;
  652. Xbegin
  653. X    if (fd < STDIN) or (fd > MAXOPEN) or
  654. X      (openList[fd].mode <> IOREAD) then
  655. X        Error('Getline with unopen or bad fd')
  656. X    else if (fd = STDIN) and (termInput) then
  657. X        GetLine := GetKeyBoard(str, maxSize)
  658. X    else begin
  659. X        i := 1;
  660. X        GetLine := false;
  661. X        if Eof(openList[fd].fileVar) then begin
  662. X            str[1] := NEWLINE;
  663. X            str[2] := ENDSTR;
  664. X            return;
  665. X        end;
  666. X        Readln(openList[fd].fileVar, str);
  667. X        i := maxSize;
  668. X        while (i > 0) do begin
  669. X            if (str[i] <> BLANK) then leave;
  670. X            i := i - 1
  671. X        end;
  672. X        str[i+1] := NEWLINE;
  673. X        str[i+2] := ENDSTR;
  674. X        GetLine := true
  675. X    end
  676. Xend;
  677. Xfunction GetKeyBoard;
  678. Xvar
  679. X    i: Integer;
  680. Xbegin
  681. X    ReadLn(openList[STDIN].fileVar, str);
  682. X    if Eof(openList[STDIN].fileVar) then begin
  683. X        TermIn(openList[STDIN].fileVar);
  684. X        i := 0
  685. X    end
  686. X    else begin
  687. X        i := maxSize;
  688. X        while (i > 0) do begin
  689. X            if str[i] <> BLANK then leave;
  690. X            i := i - 1
  691. X        end
  692. X    end;
  693. X    str[i + 1] := NEWLINE;
  694. X    str[i + 2] := ENDSTR;
  695. X    if (str[1] = ATSIGN) and (str[2] = NEWLINE) then
  696. X        GetKeyBoard := false
  697. X    else
  698. X        GetKeyBoard := true
  699. Xend;
  700. /
  701. echo 'x - getlist.pascal'
  702. sed 's/^X//' > getlist.pascal << '/'
  703. X{
  704. X    Copyright (c) 1981
  705. X    By:    Bell Telephone Laboratories, Inc. and
  706. X        Whitesmiths, Ltd.,
  707. X
  708. X    This software is derived from the book
  709. X        "Software Tools In Pascal", by
  710. X        Brian W. Kernighan and P.J. Plauger
  711. X        Addison-Wesley, 1981
  712. X        ISBN 0-201-10342-7
  713. X
  714. X    Right is hereby granted to freely distribute or duplicate this
  715. X    software, providing distribution or duplication is not for profit
  716. X    or other commerical gain and that this copyright notice remains 
  717. X    intact.
  718. X}
  719. X{ GetList -- Get list of line numbers at lin[i], increment i }
  720. Xsegment GetList;
  721. X%include swtools
  722. X%include editcons
  723. X%include edittype
  724. X%include editproc
  725. X%include editref
  726. Xfunction GetList;
  727. Xvar
  728. X    num: Integer;
  729. X    done: Boolean;
  730. Xbegin
  731. X    line2 := 0;
  732. X    nLines := 0;
  733. X    done := (GetOne(lin, i, num, status) <> OK);
  734. X    if done and (lin[i] = COMMA) then begin
  735. X        done := false;
  736. X        num := 1
  737. X    end; {if}
  738. X    while (not done) do begin
  739. X        line1 := line2;
  740. X        line2 := num;
  741. X        nLines := nLines + 1;
  742. X        if (lin[i] = SEMICOL) then
  743. X            curLn := num;
  744. X        if (lin[i] = COMMA) or (lin[i] = SEMICOL) then begin
  745. X            i := i + 1;
  746. X            done := (GetOne(lin, i, num, status) <> OK);
  747. X            if done then begin
  748. X                num := lastLn;
  749. X                done := false
  750. X            end {if}
  751. X        end
  752. X        else
  753. X            done := true
  754. X    end;
  755. X    nLines := Min(nLines, 2);
  756. X    if (nLines = 0) then
  757. X        line2 := curLn;
  758. X    if (nLines <= 1) then
  759. X        line1 := line2;
  760. X    if (status <> ERR) then
  761. X        status := OK;
  762. X    GetList := status
  763. Xend;
  764. /
  765. echo 'x - getnum.pascal'
  766. sed 's/^X//' > getnum.pascal << '/'
  767. X{
  768. X    Copyright (c) 1981
  769. X    By:    Bell Telephone Laboratories, Inc. and
  770. X        Whitesmiths, Ltd.,
  771. X
  772. X    This software is derived from the book
  773. X        "Software Tools In Pascal", by
  774. X        Brian W. Kernighan and P.J. Plauger
  775. X        Addison-Wesley, 1981
  776. X        ISBN 0-201-10342-7
  777. X
  778. X    Right is hereby granted to freely distribute or duplicate this
  779. X    software, providing distribution or duplication is not for profit
  780. X    or other commerical gain and that this copyright notice remains 
  781. X    intact.
  782. X}
  783. X{ GetNum -- get single line number component }
  784. Xsegment GetNum;
  785. X%include swtools
  786. X%include editcons
  787. X%include edittype
  788. X%include editproc
  789. X%include editref
  790. Xfunction GetNum;
  791. Xbegin
  792. X    status := OK;
  793. X    SkipBl(lin, i);
  794. X    if (IsDigit(lin[i])) then begin
  795. X        num := CToI(lin, i);
  796. X        i := i - 1   { move back, to be advanced at end }
  797. X    end
  798. X    else if (lin[i] = PLUS) or (lin[i] = MINUS) then begin
  799. X        num := curLn;
  800. X        i := i - 1; {don't eat the plus or minus sign}
  801. X    end
  802. X    else if (lin[i] = CURLINE) then
  803. X        num := curLn
  804. X    else if (lin[i] = LASTLINE) then
  805. X        num := lastLn
  806. X    else if (lin[i] = SCAN) or (lin[i] = BACKSCAN) then begin
  807. X        if (OptPat(lin,i) = ERR) then { build pattern }
  808. X            status := ERR
  809. X        else
  810. X            status := PatScan(lin[i], num)
  811. X    end
  812. X    else
  813. X        status := ENDDATA;
  814. X    if (status = OK) then
  815. X        i := i + 1; { advance to next character }
  816. X    GetNum := status
  817. Xend;
  818. /
  819. echo 'x - getone.pascal'
  820. sed 's/^X//' > getone.pascal << '/'
  821. X{
  822. X    Copyright (c) 1981
  823. X    By:    Bell Telephone Laboratories, Inc. and
  824. X        Whitesmiths, Ltd.,
  825. X
  826. X    This software is derived from the book
  827. X        "Software Tools In Pascal", by
  828. X        Brian W. Kernighan and P.J. Plauger
  829. X        Addison-Wesley, 1981
  830. X        ISBN 0-201-10342-7
  831. X
  832. X    Right is hereby granted to freely distribute or duplicate this
  833. X    software, providing distribution or duplication is not for profit
  834. X    or other commerical gain and that this copyright notice remains 
  835. X    intact.
  836. X}
  837. X{ GetOne -- get one line number expression }
  838. Xsegment GetOne;
  839. X%include swtools
  840. X%include editcons
  841. X%include edittype
  842. X%include editref
  843. X%include editproc
  844. Xfunction GetOne;
  845. Xvar
  846. X    iStart, mul, pNum: Integer;
  847. Xbegin
  848. X    iStart := i;
  849. X    num := 0;
  850. X    if (GetNum(lin, i, num, status) = OK) then { 1st term }
  851. X        repeat { + or - terms }
  852. X            SkipBl(lin, i);
  853. X            if (lin[i] <> PLUS) and (lin[i] <> MINUS) then
  854. X                status := ENDDATA
  855. X            else begin
  856. X                if (lin[i] = PLUS) then
  857. X                    mul := 1
  858. X                else
  859. X                    mul := -1;
  860. X                i := i + 1;
  861. X                if (GetNum(lin, i, pNum, status) = OK) then
  862. X                    num := num + mul * pNum;
  863. X                if (status = ENDDATA) then
  864. X                    status := ERR
  865. X            end
  866. X        until (status <> OK);
  867. X    if (num < 0) or (num > lastLn) then
  868. X        status := ERR;
  869. X    if (status <> ERR) then begin
  870. X        if (i <= iStart) then
  871. X            status := ENDDATA
  872. X        else
  873. X            status := OK
  874. X    end;
  875. X    GetOne := status
  876. Xend;
  877. /
  878. echo 'x - getpat.pascal'
  879. sed 's/^X//' > getpat.pascal << '/'
  880. X{
  881. X    Copyright (c) 1981
  882. X    By:    Bell Telephone Laboratories, Inc. and
  883. X        Whitesmiths, Ltd.,
  884. X
  885. X    This software is derived from the book
  886. X        "Software Tools In Pascal", by
  887. X        Brian W. Kernighan and P.J. Plauger
  888. X        Addison-Wesley, 1981
  889. X        ISBN 0-201-10342-7
  890. X
  891. X    Right is hereby granted to freely distribute or duplicate this
  892. X    software, providing distribution or duplication is not for profit
  893. X    or other commerical gain and that this copyright notice remains 
  894. X    intact.
  895. X}
  896. X{ GetPat -- get pattern from lin, increment i }
  897. Xsegment GetPat;
  898. X%include swtools
  899. X%include patdef
  900. Xfunction GetPat;
  901. Xbegin
  902. X    GetPat := (MakePat(arg, 1, ENDSTR, pat) > 0)
  903. Xend;
  904. /
  905. echo 'x - install.pascal'
  906. sed 's/^X//' > install.pascal << '/'
  907. X{
  908. X    Copyright (c) 1981
  909. X    By:    Bell Telephone Laboratories, Inc. and
  910. X        Whitesmiths, Ltd.,
  911. X
  912. X    This software is derived from the book
  913. X        "Software Tools In Pascal", by
  914. X        Brian W. Kernighan and P.J. Plauger
  915. X        Addison-Wesley, 1981
  916. X        ISBN 0-201-10342-7
  917. X
  918. X    Right is hereby granted to freely distribute or duplicate this
  919. X    software, providing distribution or duplication is not for profit
  920. X    or other commerical gain and that this copyright notice remains 
  921. X    intact.
  922. X}
  923. X{ Install -- add name, definition and type to table }
  924. Xsegment Install;
  925. X%include swtools
  926. X%include defdef
  927. X%include defref
  928. X%include defproc
  929. Xprocedure Install;
  930. Xvar
  931. X    h, dlen, nlen: Integer;
  932. X    p: NDPtr;
  933. Xbegin
  934. X    nlen := StrLength(name) + 1;   { 1 for ENDSTR }
  935. X    dlen := StrLength(defn) + 1;
  936. X    if (nextTab + nlen + dlen > MAXCHARS) then begin
  937. X        PutStr(name, STDERR);
  938. X        Error(': too many definitions')
  939. X    end
  940. X    else begin
  941. X        h := Hash(name);
  942. X        new(p);
  943. X        p->.nextPtr := hashTab[h];
  944. X        hashTab[h] := p;
  945. X        p->.name := nextTab;
  946. X        SCCopy(name, ndTable, nextTab);
  947. X        nextTab := nextTab + nlen;
  948. X        p->.defn := nextTab;
  949. X        SCCopy(defn, ndTable, nextTab);
  950. X        nextTab := nextTab + dlen;
  951. X        p->.kind := t
  952. X    end
  953. Xend;
  954. /
  955. echo 'x - kopy.pascal'
  956. sed 's/^X//' > kopy.pascal << '/'
  957. X{
  958. X    Copyright (c) 1981
  959. X    By:    Bell Telephone Laboratories, Inc. and
  960. X        Whitesmiths, Ltd.,
  961. X
  962. X    This software is derived from the book
  963. X        "Software Tools In Pascal", by
  964. X        Brian W. Kernighan and P.J. Plauger
  965. X        Addison-Wesley, 1981
  966. X        ISBN 0-201-10342-7
  967. X
  968. X    Right is hereby granted to freely distribute or duplicate this
  969. X    software, providing distribution or duplication is not for profit
  970. X    or other commerical gain and that this copyright notice remains 
  971. X    intact.
  972. X}
  973. X{ Kopy -- move line1 thru line2 after line3 }
  974. Xsegment Kopy;
  975. X%include swtools
  976. X%include editcons
  977. X%include edittype
  978. X%include editproc
  979. X%include editref
  980. Xfunction Kopy;
  981. Xvar
  982. X    i: Integer;
  983. X    curSave, lastSave: Integer;
  984. X    tempLine: StringType;
  985. Xbegin
  986. X    if (line1 <= 0) or ((line3 >= line1) and (line3 < line2)) then
  987. X        Kopy := ERR
  988. X    else begin
  989. X        curSave := curLn;
  990. X        lastSave := lastLn;
  991. X        curLn := lastLn;
  992. X        for i := line1 to line2 do begin
  993. X            GetTxt(i, tempLine);
  994. X            if PutTxt(tempLine) = ERR then begin
  995. X                curLn := curSave;
  996. X                lastLn := lastSave;
  997. X                Kopy := ERR;
  998. X                return
  999. X           end
  1000. X       end; {if}
  1001. X        BlkMove(lastSave+1, lastSave+1+line2-line1, line3);
  1002. X       if (line3 > line1) then
  1003. X           curLn := line3
  1004. X       else
  1005. X           curLn := line3 + (line2 - line1 + 1);
  1006. X       Kopy := OK
  1007. X    end
  1008. Xend;
  1009. /
  1010. echo 'x - makesub.pascal'
  1011. sed 's/^X//' > makesub.pascal << '/'
  1012. X{
  1013. X    Copyright (c) 1981
  1014. X    By:    Bell Telephone Laboratories, Inc. and
  1015. X        Whitesmiths, Ltd.,
  1016. X
  1017. X    This software is derived from the book
  1018. X        "Software Tools In Pascal", by
  1019. X        Brian W. Kernighan and P.J. Plauger
  1020. X        Addison-Wesley, 1981
  1021. X        ISBN 0-201-10342-7
  1022. X
  1023. X    Right is hereby granted to freely distribute or duplicate this
  1024. X    software, providing distribution or duplication is not for profit
  1025. X    or other commerical gain and that this copyright notice remains 
  1026. X    intact.
  1027. X}
  1028. X{ MakeSub -- make substitution string from arg into sub }
  1029. Xsegment MakeSub;
  1030. X%include swtools
  1031. X%include patdef
  1032. X%include subdef
  1033. X%include metadef
  1034. Xvalue
  1035. X    nullMetaTable := MetaTableType(
  1036. X        MetaElementType(0,0),
  1037. X        MetaElementType(0,0),
  1038. X        MetaElementType(0,0),
  1039. X        MetaElementType(0,0),
  1040. X        MetaElementType(0,0),
  1041. X        MetaElementType(0,0),
  1042. X        MetaElementType(0,0),
  1043. X        MetaElementType(0,0),
  1044. X        MetaElementType(0,0),
  1045. X        MetaElementType(0,0));
  1046. Xfunction MakeSub;
  1047. Xvar
  1048. X    k: Integer;
  1049. X    i, j: Integer;
  1050. X    l: Integer;
  1051. X    junk: Boolean;
  1052. Xbegin
  1053. X    j := 1;
  1054. X    i := from;
  1055. X    k := from;
  1056. X    while (arg[k] <> delim) and (k <= (MAXSTR - 2)) do
  1057. X        if (arg[k] = NEWLINE) or (arg[k] = ENDSTR) then begin
  1058. X            arg[k] := delim;
  1059. X            arg[k+1] := NEWLINE;
  1060. X            arg[k+2] := ENDSTR;
  1061. X        end
  1062. X        else
  1063. X            k := k + 1;
  1064. X    while (arg[i] <> delim) and (arg[i] <> ENDSTR) do begin
  1065. X        if (arg[i] = AMPER) then begin
  1066. X            junk := AddStr(DITTO, sub, j, MAXPAT);
  1067. X            { &n handler for meta brackets }
  1068. X            if (arg[i+1] in [DIG0..DIG9]) then begin
  1069. X                i := i + 1;
  1070. X                junk := AddStr(Chr(Ord(arg[i]) - Ord(DIG0)),
  1071. X                    sub, j, MAXPAT)
  1072. X            end
  1073. X        end
  1074. X        else
  1075. X            junk := AddStr(Esc(arg,i), sub, j, MAXPAT);
  1076. X        i := i + 1
  1077. X    end;
  1078. X    if (arg[i] <> delim) then   { missing delim }
  1079. X        MakeSub := 0
  1080. X    else if (not AddStr(ENDSTR, sub, j, MAXPAT)) then
  1081. X        MakeSub := 0
  1082. X    else
  1083. X        MakeSub := i
  1084. Xend;
  1085. /
  1086. echo 'x - mputstr.pascal'
  1087. sed 's/^X//' > mputstr.pascal << '/'
  1088. X{
  1089. X    Copyright (c) 1981
  1090. X    By:    Bell Telephone Laboratories, Inc. and
  1091. X        Whitesmiths, Ltd.,
  1092. X
  1093. X    This software is derived from the book
  1094. X        "Software Tools In Pascal", by
  1095. X        Brian W. Kernighan and P.J. Plauger
  1096. X        Addison-Wesley, 1981
  1097. X        ISBN 0-201-10342-7
  1098. X
  1099. X    Right is hereby granted to freely distribute or duplicate this
  1100. X    software, providing distribution or duplication is not for profit
  1101. X    or other commerical gain and that this copyright notice remains 
  1102. X    intact.
  1103. X}
  1104. X{ MPutStr -- put meta'd string out on file }
  1105. Xsegment MPutStr;
  1106. X%include swtools
  1107. X%include ioref
  1108. Xprocedure MPutStr;
  1109. Xvar
  1110. X    i: Integer;
  1111. X    j: integer;
  1112. X    len: Integer;
  1113. X    outString: StringType;
  1114. Xbegin
  1115. X    i := 1;
  1116. X    j := 1;
  1117. X    len := StrLength(str);
  1118. X    while i <= len do begin
  1119. X        if str[i] = DOLLAR then begin
  1120. X            i := i + 1;
  1121. X            if (str[i] = BIGN) or (str[i] = LETN) then begin
  1122. X                if j = 1 then WriteLn(openList[fd].fileVar,' ')
  1123. X                         else WriteLn(openList[fd].fileVar,
  1124. X                              outString:j-1);
  1125. X                j := 1
  1126. X            end
  1127. X            else if (str[i] = BIGE) or (str[i] = LETE) then
  1128. X                return
  1129. X            else
  1130. X                i := i - 1
  1131. X        end else
  1132. X        if str[i] = NEWLINE then begin
  1133. X            if j = 1 then WriteLn(openList[fd].fileVar,' ')
  1134. X                     else WriteLn(openList[fd].fileVar, outString:j-1);
  1135. X            j := 1;
  1136. X        end {then}
  1137. X        else begin
  1138. X            outString[j] := str[i];
  1139. X            j := j + 1;
  1140. X        end; {if}
  1141. X        i := i + 1
  1142. X    end; {while}
  1143. X    if j <> 1 then write(openList[fd].fileVar, outString:j-1);
  1144. Xend; {MPutStr}
  1145. /
  1146. echo 'x - omatch.pascal'
  1147. sed 's/^X//' > omatch.pascal << '/'
  1148. X{
  1149. X    Copyright (c) 1981
  1150. X    By:    Bell Telephone Laboratories, Inc. and
  1151. X        Whitesmiths, Ltd.,
  1152. X
  1153. X    This software is derived from the book
  1154. X        "Software Tools In Pascal", by
  1155. X        Brian W. Kernighan and P.J. Plauger
  1156. X        Addison-Wesley, 1981
  1157. X        ISBN 0-201-10342-7
  1158. X
  1159. X    Right is hereby granted to freely distribute or duplicate this
  1160. X    software, providing distribution or duplication is not for profit
  1161. X    or other commerical gain and that this copyright notice remains 
  1162. X    intact.
  1163. X}
  1164. X{ OMatch -- match one pattern element at pat[j] }
  1165. Xsegment OMatch;
  1166. X%include swtools
  1167. X%include matchdef
  1168. X%include patdef
  1169. X%include metadef
  1170. Xfunction OMatch;
  1171. Xvar
  1172. X    advance: -1..1;
  1173. X    mIndex: Integer;
  1174. Xbegin
  1175. X    advance := -1;
  1176. X    if (lin[i] = ENDSTR) then
  1177. X        OMatch := false
  1178. X    else
  1179. X        case pat[j] of
  1180. X            LITCHAR:
  1181. X                if (lin[i] = pat[j+1]) then
  1182. X                    advance := 1;
  1183. X            BOM:
  1184. X                if (metaStackPointer <= 9) and
  1185. X                  (metaIndex <= 9) then begin
  1186. X                    metaStack[metaStackPointer] := metaIndex;
  1187. X                    metaTable[metaIndex].first := i;
  1188. X                    metaIndex := metaIndex + 1;
  1189. X                    metaStackPointer := metaStackPointer + 1;
  1190. X                    advance := 0
  1191. X                end
  1192. X                else
  1193. X                    Error('OMatch/meta: can''t happen');
  1194. X            EOM:
  1195. X                if (metaStackPointer >= 1) then begin
  1196. X                    metaStackPointer := metaStackPointer - 1;
  1197. X                    mIndex := metaStack[metaStackPointer];
  1198. X                    metaTable[mIndex].last := i;
  1199. X                    advance := 0
  1200. X                end
  1201. X                else
  1202. X                    Error('OMatch/meta/EOM can''t happen');
  1203. X            BOL:
  1204. X                if (i = 1) then
  1205. X                    advance := 0;
  1206. X            ANY:
  1207. X                if (lin[i] <> NEWLINE) then
  1208. X                    advance := 1;
  1209. X            EOL:
  1210. X                if (lin[i] = NEWLINE) then
  1211. X                    advance := 0;
  1212. X            CCL:
  1213. X                if (Locate(lin[i], pat, j+1)) then
  1214. X                    advance := 1;
  1215. X            NCCL:
  1216. X                if (lin[i] <> NEWLINE) and
  1217. X                  (not Locate(lin[i], pat, j+1)) then
  1218. X                    advance := 1
  1219. X            otherwise
  1220. X                Error('in omatch: can''t happen')
  1221. X        end;
  1222. X    if (advance >= 0) then begin
  1223. X        i := i + advance;
  1224. X        OMatch := true
  1225. X    end
  1226. X    else
  1227. X        OMatch := false
  1228. Xend;
  1229. /
  1230. echo 'x - onerror.pascal'
  1231. sed 's/^X//' > onerror.pascal << '/'
  1232. X{
  1233. X    Copyright (c) 1982
  1234. X    By:    Chris Lewis
  1235. X
  1236. X    Right is hereby granted to freely distribute or duplicate this
  1237. X    software, providing distribution or duplication is not for profit
  1238. X    or other commerical gain and that this copyright notice remains 
  1239. X    intact.
  1240. X}
  1241. X{ OnError -- intercept pascalvs run-time errors }
  1242. Xsegment OnError;
  1243. Xdef ERRORIO: Boolean;
  1244. Xdef ATTENTION: Boolean;
  1245. Xdef OUTOFSPACE: Boolean;
  1246. Xvalue
  1247. X    ERRORIO := false;
  1248. X    ATTENTION := false;
  1249. X    OUTOFSPACE := false;
  1250. X%include onerror
  1251. Xprocedure OnError;
  1252. Xvar
  1253. X    statementNumber: String(10);
  1254. X    procName: String(10);
  1255. X    errorNo: String(10);
  1256. Xbegin
  1257. X    if (FERROR in [41..53,75..78]) then begin
  1258. X        ERRORIO := true;
  1259. X        FACTION := [];
  1260. X    end
  1261. X    else if FERROR = 30 then begin
  1262. X        ATTENTION := true;
  1263. X        FACTION := [];
  1264. X    end
  1265. X    else if (FERROR = 64) and (not OUTOFSPACE) then begin
  1266. X        OUTOFSPACE := true;
  1267. X        FACTION := []
  1268. X    end
  1269. X    else if FERROR = 36 then begin
  1270. X        FACTION := [XUMSG,XTRACE,XHALT];
  1271. X        WriteStr(statementNumber, FSTMTNO:5);
  1272. X        WriteStr(procName, FPROCNAME:8);
  1273. X        WriteStr(errorNo, FERROR:5);
  1274. X        FRETMSG := 'SWTOOLS ASSERT FAILURE: RID=' || PROCNAME||
  1275. X                   '; S#=' || statementNumber ||
  1276. X                   '; EID' || errorNo || ';';
  1277. X    end
  1278. X    else begin
  1279. X        FACTION := [XUMSG,XTRACE];
  1280. X        WriteStr(statementNumber, FSTMTNO:5);
  1281. X        WriteStr(procName, FPROCNAME:8);
  1282. X        WriteStr(errorNo, FERROR: 5);
  1283. X        FRETMSG := '***SWTOOLS error: RID=' || procName
  1284. X                   || '; S#=' || statementNumber ||
  1285. X                   '; EID=' || errorNo || ';';
  1286. X    end
  1287. Xend;
  1288. /
  1289. echo 'x - rot.pascal'
  1290. sed 's/^X//' > rot.pascal << '/'
  1291. X{
  1292. X    Copyright (c) 1982
  1293. X    By:    Chris Lewis
  1294. X
  1295. X    Right is hereby granted to freely distribute or duplicate this
  1296. X    software, providing distribution or duplication is not for profit
  1297. X    or other commerical gain and that this copyright notice remains 
  1298. X    intact.
  1299. X}
  1300. X{ Rot -- Rotate a file 90 degrees clockwise }
  1301. Xprogram Rot;
  1302. X%include swtools
  1303. Xconst
  1304. X    maxWidth = 2000;
  1305. X    maxHeight = 130;
  1306. Xvar
  1307. X    buffers: array [1..maxHeight] of array
  1308. X       [1..maxWidth] of Char;
  1309. X    i: Integer;
  1310. X    j: Integer;
  1311. X    maxReadWidth: Integer;
  1312. X    maxReadHeight: Integer;
  1313. Xbegin
  1314. X    ToolInit;
  1315. X    i := 1;
  1316. X    j := 1;
  1317. X    maxReadWidth := 0;
  1318. X    while (GetC(buffers[i,j]) <> ENDFILE) do begin
  1319. X        if (buffers[i,j] = NEWLINE) then begin
  1320. X            maxReadWidth := Max(maxReadWidth,j);
  1321. X            for j := j to maxWidth do
  1322. X                buffers[i,j] := BLANK;
  1323. X            j := 1;
  1324. X            i := i + 1;
  1325. X        end
  1326. X        else
  1327. X            j := j + 1;
  1328. X        if (i > maxHeight) or (j > maxWidth) then begin
  1329. X            Message('input file too big');
  1330. X            leave
  1331. X        end
  1332. X    end;
  1333. X    maxReadHeight := i - 1;
  1334. X    for i := 1 to maxReadWidth do begin
  1335. X        for j := maxReadHeight downto 1 do
  1336. X             PutC (buffers[j,i]);
  1337. X        PutC (NEWLINE)
  1338. X    end;
  1339. Xend.
  1340. /
  1341. echo 'x - subst.pascal'
  1342. sed 's/^X//' > subst.pascal << '/'
  1343. X{
  1344. X    Copyright (c) 1981
  1345. X    By:    Bell Telephone Laboratories, Inc. and
  1346. X        Whitesmiths, Ltd.,
  1347. X
  1348. X    This software is derived from the book
  1349. X        "Software Tools In Pascal", by
  1350. X        Brian W. Kernighan and P.J. Plauger
  1351. X        Addison-Wesley, 1981
  1352. X        ISBN 0-201-10342-7
  1353. X
  1354. X    Right is hereby granted to freely distribute or duplicate this
  1355. X    software, providing distribution or duplication is not for profit
  1356. X    or other commerical gain and that this copyright notice remains 
  1357. X    intact.
  1358. X}
  1359. X{ SubSt -- substitute "sub" for occurrences of pattern }
  1360. Xsegment SubSt;
  1361. X%include swtools
  1362. X%include editcons
  1363. X%include edittype
  1364. X%include editproc
  1365. X%include editref
  1366. X%include matchdef
  1367. X%include subdef
  1368. Xfunction SubSt;
  1369. Xvar
  1370. X    new, old: StringType;
  1371. X    j, k, lastm, line, m: Integer;
  1372. X    stat: STCode;
  1373. X    done, subbed, junk: Boolean;
  1374. Xbegin
  1375. X    if (glob) then
  1376. X        stat := OK
  1377. X    else
  1378. X        stat := ERR;
  1379. X    done := (line1 <= 0);
  1380. X    line := line1;
  1381. X    while (not done) and (line <= line2) do begin
  1382. X        j := 1;
  1383. X        subbed := false;
  1384. X        GetTxt(line, old);
  1385. X        lastm := 0;
  1386. X        k := 1;
  1387. X        while (old[k] <> ENDSTR) do begin
  1388. X            if (gFlag) or (not subbed) then
  1389. X                m := AMatch(old, k, pat, 1)
  1390. X            else
  1391. X                m := 0;
  1392. X            if (m > 0) and (lastm <> m) then begin
  1393. X                { replace matched text }
  1394. X                subbed := true;
  1395. X                CatSub(old, k, m, sub, new, j, MAXSTR);
  1396. X                lastm := m
  1397. X            end;
  1398. X            if (m = 0) or (m = k) then begin
  1399. X                { no match or null match }
  1400. X                junk := AddStr(old[k], new, j, MAXSTR);
  1401. X                k := k + 1
  1402. X            end
  1403. X            else
  1404. X                { skip matched text }
  1405. X                k := m
  1406. X        end;
  1407. X        if (subbed) then begin
  1408. X            if (not AddStr(ENDSTR, new, j, MAXSTR)) then begin
  1409. X                stat := ERR;
  1410. X                done := true
  1411. X            end
  1412. X            else begin
  1413. X                stat := LnDelete(line, line, stat);
  1414. X                stat := PutTxt(new);
  1415. X                line2 := line2 + curLn - line;
  1416. X                line := curLn;
  1417. X                if (stat = ERR) then
  1418. X                    done := true
  1419. X                else
  1420. X                    stat := OK
  1421. X            end
  1422. X        end;
  1423. X        line := line + 1
  1424. X    end;
  1425. X    SubSt := stat
  1426. Xend;
  1427. /
  1428. echo 'x - sw.pascal'
  1429. sed 's/^X//' > sw.pascal << '/'
  1430. X{
  1431. X    Copyright (c) 1981
  1432. X    By:    Bell Telephone Laboratories, Inc. and
  1433. X        Whitesmiths, Ltd.,
  1434. X
  1435. X    This software is derived from the book
  1436. X        "Software Tools In Pascal", by
  1437. X        Brian W. Kernighan and P.J. Plauger
  1438. X        Addison-Wesley, 1981
  1439. X        ISBN 0-201-10342-7
  1440. X
  1441. X    Right is hereby granted to freely distribute or duplicate this
  1442. X    software, providing distribution or duplication is not for profit
  1443. X    or other commerical gain and that this copyright notice remains 
  1444. X    intact.
  1445. X}
  1446. X{ SW[edit] -- main routine for text editor }
  1447. Xprogram SW;
  1448. X%include swtools
  1449. X%include editcons
  1450. X%include edittype
  1451. X%include editproc
  1452. Xvar
  1453. X    curSave, i: Integer;
  1454. X    status: STCode;
  1455. X    more: Boolean;
  1456. X    argIndex: Integer;
  1457. Xdef line1: Integer;   { first line number }
  1458. Xdef line2: Integer;   { second line number }
  1459. Xdef nLines: Integer;  { # lines in buffer }
  1460. Xdef curLn: Integer;  { current line: value of dot }
  1461. Xdef lastLn: Integer; { last line: value of $ }
  1462. Xdef pat: StringType; { pattern }
  1463. Xdef lin: StringType; { input line }
  1464. Xdef saveFile: StringType; { file name }
  1465. Xvalue
  1466. X    line1 := 0;
  1467. X    line2 := 0;
  1468. X    nLines := 0;
  1469. Xbegin
  1470. X    ToolInit;
  1471. X    SetBuf;
  1472. X    pat[1] := ENDSTR;
  1473. X    saveFile[1] := ENDSTR;
  1474. X    i := 1;
  1475. X    for argIndex := 1 to Nargs do
  1476. X        if GetArg(argIndex, lin, MAXSTR) then begin
  1477. X            SCopy (lin, 1, saveFile, i);
  1478. X            i := StrLength(saveFile) + 2;
  1479. X            saveFile[i-1] := BLANK
  1480. X        end;
  1481. X    i := 1;
  1482. X    if saveFile[1] <> ENDSTR then
  1483. X        if (not GetFid(saveFile, i, saveFile)) then
  1484. X            saveFile[1] := ENDSTR;
  1485. X    if saveFile[1] <> ENDSTR then
  1486. X        if (DoRead(0, saveFile) = ERR) then
  1487. X            Message('Cannot open input file');
  1488. X    if (OptIsOn(promptFlag)) then begin
  1489. X        PutC(COLON);
  1490. X        PutC(NEWLINE)
  1491. X    end;
  1492. X    more := GetLine(lin, STDIN, MAXSTR);
  1493. X    while (more) do begin
  1494. X        i := 1;
  1495. X        curSave := curLn;
  1496. X        if (GetList(lin, i, Status) = OK) then begin
  1497. X            if (CKGlob(lin, i, status) = OK) then
  1498. X                status := DoGlob(lin, i, curSave, status)
  1499. X            else if (status <> ERR) then
  1500. X                status := DoCmd(lin, i, false, status)
  1501. X            { else error - do nothing }
  1502. X        end;
  1503. X        if (status = ERR) then begin
  1504. X            Message('eh?');
  1505. X            curLn := Min(curSave, lastLn)
  1506. X        end
  1507. X        else if (status = ENDDATA) then
  1508. X            more := false;
  1509. X        { else ok }
  1510. X        if (more) then begin
  1511. X            if OptIsOn(promptFlag) then begin
  1512. X                PutC(COLON);
  1513. X                PutC(NEWLINE)
  1514. X            end;
  1515. X            more := GetLine(lin, STDIN, MAXSTR)
  1516. X        end
  1517. X    end;
  1518. X    ClrBuf
  1519. Xend.
  1520. /
  1521. echo 'x - swtr.pascal'
  1522. sed 's/^X//' > swtr.pascal << '/'
  1523. X{
  1524. X    Copyright (c) 1981
  1525. X    By:    Bell Telephone Laboratories, Inc. and
  1526. X        Whitesmiths, Ltd.,
  1527. X
  1528. X    This software is derived from the book
  1529. X        "Software Tools In Pascal", by
  1530. X        Brian W. Kernighan and P.J. Plauger
  1531. X        Addison-Wesley, 1981
  1532. X        ISBN 0-201-10342-7
  1533. X
  1534. X    Right is hereby granted to freely distribute or duplicate this
  1535. X    software, providing distribution or duplication is not for profit
  1536. X    or other commerical gain and that this copyright notice remains 
  1537. X    intact.
  1538. X}
  1539. X{ Translit -- map characters }
  1540. Xprogram SWTr;
  1541. X%include swtools
  1542. X%include patdef
  1543. Xvar
  1544. X    arg, fromSet, toSet: StringType;
  1545. X    c: CharType;
  1546. X    i, lastTo: 0..MAXSTR;
  1547. X    allBut, squash: Boolean;
  1548. X{ XIndex -- conditionally invert value from strindex }
  1549. Xfunction XIndex (var inSet: StringType; c: CharType;
  1550. X        allBut: Boolean; lastTo: Integer): Integer;
  1551. Xbegin
  1552. X    if (c = ENDFILE) then
  1553. X        XIndex := 0
  1554. X    else if (not allBut) then
  1555. X        XIndex := StrIndex(inSet,c)
  1556. X    else if (StrIndex(inSet,c) > 0) then
  1557. X        XIndex := 0
  1558. X    else
  1559. X        XIndex := lastTo + 1
  1560. Xend;
  1561. Xbegin
  1562. X    ToolInit;
  1563. X    if (not GetArg(1, arg, MAXSTR)) then
  1564. X        Error('usage: translit from to');
  1565. X    allBut := (arg[1] = NEGATE);
  1566. X    if allBut then
  1567. X        i := 2
  1568. X    else
  1569. X        i := 1;
  1570. X    if (not MakeSet(arg, i, fromSet, MaxStr)) then
  1571. X        Error('translit: "from" set too large');
  1572. X    if (not GetArg(2,arg, MAXSTR)) then
  1573. X        toSet[1] := ENDSTR
  1574. X    else if (not MakeSet(arg, 1, toSet, MAXSTR)) then
  1575. X        Error('translit: "to" set too large')
  1576. X    else if (StrLength(fromSet) < StrLength(toSet)) then
  1577. X        Error('Translit: "from" shorter than "to"');
  1578. X    lastTo := StrLength(toSet);
  1579. X    squash := (StrLength(fromSet) > lastTo) or (allBut);
  1580. X    repeat
  1581. X        i := XIndex(fromSet, GetC(c), allBut, lastTo);
  1582. X        if (squash) and (i >= lastTo) and (lastTo > 0) then begin
  1583. X            PutC(toSet[lastTo]);
  1584. X            repeat
  1585. X                i := XIndex(fromSet, GetC(c), allBut, lastTo)
  1586. X            until (i < lastTo)
  1587. X        end;
  1588. X        if (c <> ENDFILE) then begin
  1589. X            if (i > 0) and (lastTo > 0) then { translate }
  1590. X                PutC(toSet[i])
  1591. X            else if (i = 0) then { copy }
  1592. X                PutC(c)
  1593. X            { else delete (don't print) }
  1594. X        end
  1595. X    until (c = ENDFILE)
  1596. Xend;
  1597. /
  1598. echo 'x - unique.pascal'
  1599. sed 's/^X//' > unique.pascal << '/'
  1600. X{
  1601. X    Copyright (c) 1982
  1602. X    By:    Chris Lewis
  1603. X
  1604. X    Right is hereby granted to freely distribute or duplicate this
  1605. X    software, providing distribution or duplication is not for profit
  1606. X    or other commerical gain and that this copyright notice remains 
  1607. X    intact.
  1608. X}
  1609. X{ Unique -- strip adjacent duplicate lines in a file }
  1610. Xprogram Unique;
  1611. X%include swtools
  1612. Xvar
  1613. X    buffer: array [0..1] of StringType;
  1614. X    bufNum: 0..1;
  1615. X    sameRecCount: Integer;
  1616. X    counts: Boolean;
  1617. X    lastRec: StringType;
  1618. Xbegin
  1619. X    ToolInit;
  1620. X    buffer[1,1] := ENDSTR;
  1621. X    buffer[0,1] := NEWLINE;   { just so's they're different }
  1622. X    lastRec := buffer[1];
  1623. X    counts := NArgs > 0;
  1624. X    bufNum := 0;
  1625. X    sameRecCount := 0;
  1626. X    while(GetLine(buffer[bufNum], STDIN, MAXSTR)) do begin
  1627. X        if (not Equal(buffer[0], buffer[1])) then begin
  1628. X            if counts and (sameRecCount <> 0) then begin
  1629. X                PutDec(sameRecCount, 6);
  1630. X                PutC(BLANK)
  1631. X            end;
  1632. X            if sameRecCount <> 0 then
  1633. X                PutStr(lastRec, STDOUT);
  1634. X            lastRec := buffer[bufNum];
  1635. X            sameRecCount := 1
  1636. X        end
  1637. X        else
  1638. X            sameRecCount := sameRecCount + 1;
  1639. X        bufNum := (1 - bufNum)
  1640. X    end;
  1641. X    if sameRecCount <> 0 then begin
  1642. X        if counts then begin
  1643. X            PutDec(sameRecCount, 6);
  1644. X            PutC(BLANK)
  1645. X        end;
  1646. X        PutStr(lastRec, STDOUT)
  1647. X    end
  1648. Xend.
  1649. /
  1650. echo 'x - unrotate.pascal'
  1651. sed 's/^X//' > unrotate.pascal << '/'
  1652. X{
  1653. X    Copyright (c) 1982
  1654. X    By:    Chris Lewis
  1655. X
  1656. X    Right is hereby granted to freely distribute or duplicate this
  1657. X    software, providing distribution or duplication is not for profit
  1658. X    or other commerical gain and that this copyright notice remains 
  1659. X    intact.
  1660. X}
  1661. X{ UnRotate -- Unrotate lines rotated by first half of KWIC }
  1662. XProgram UnRotate;
  1663. X%include swtools
  1664. Xconst
  1665. X    MAXOUT = 80;
  1666. X    MIDDLE = 40;
  1667. X    FOLD = DOLLAR;
  1668. Xvar
  1669. X    inBuf, outBuf: StringType;
  1670. X    tempFile2: FileDesc;
  1671. X    i, j, f: Integer;
  1672. Xbegin
  1673. X    ToolInit;
  1674. X    tempFile2 := STDIN;
  1675. X    while (GetLine(inBuf, tempFile2, MAXSTR)) do begin
  1676. X        for i := 1 to MAXOUT -1 do
  1677. X             outBuf[i] := BLANK;
  1678. X        f := StrIndex(inBuf, FOLD);
  1679. X        j := MIDDLE - 1;
  1680. X        for i := StrLength(inBuf)-1 downto f+1 do begin
  1681. X             outBuf[j] := inBuf[i];
  1682. X             j := j - 1;
  1683. X             if (j <= 0) then
  1684. X                 j := MAXOUT - 1
  1685. X        end;
  1686. X        j := MIDDLE + 3;
  1687. X        for i := 1 to f-1 do begin
  1688. X             outBuf[j] := inBuf[i];
  1689. X             j := j mod (MAXOUT - 1) + 1
  1690. X        end;
  1691. X        for j := 1 to MAXOUT - 1 do
  1692. X             if (outBuf[j] <> BLANK) then
  1693. X                 i := j;
  1694. X        outBuf[i+1] := ENDSTR;
  1695. X        PutStr(outBuf, STDOUT);
  1696. X        PutC(NEWLINE)
  1697. X    end
  1698. Xend;
  1699. /
  1700. echo 'Part 04 of pack.out complete.'
  1701. exit
  1702.  
  1703.  
  1704.