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

  1. Subject: Software Tools in Pascal (Part 3 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 9
  7. Submitted by: ihnp4!mnetor!clewis (Chris Lewis)
  8.  
  9. #!/bin/sh
  10. echo 'Start of pack.out, part 03 of 06:'
  11. echo 'x - amatch.pascal'
  12. sed 's/^X//' > amatch.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{ AMatch -- look for match of pat[i]... at lin[offset]... }
  30. Xsegment AMatch;
  31. X%include swtools
  32. X%include patdef
  33. X%include matchdef
  34. X%include metadef
  35. Xfunction RAMatch (var lin: StringType; offset: Integer;
  36. X        var pat: StringType; j: Integer): Integer;
  37. X    forward;
  38. Xfunction AMatch;
  39. Xvar
  40. X    k: Integer;
  41. Xbegin
  42. X    metaStackPointer := 1;
  43. X    metaIndex := 1;
  44. X    metaTable := nullMetaTable;
  45. X    metaTable[0].first := offset;
  46. X    k := RAMatch(lin, offset, pat, j);
  47. X    metaTable[0].last := k;
  48. X    AMatch := k;
  49. Xend;
  50. X{ RAMatch -- new AMatch with metas }
  51. Xfunction RAMatch;
  52. Xvar
  53. X    i, k: Integer;
  54. X    metaStackTemp: Integer;
  55. X    done: Boolean;
  56. Xbegin
  57. X    done := false;
  58. X    while (not done) and (pat[j] <> ENDSTR) do
  59. X        if (pat[j] = CLOSURE) then begin
  60. X            metaStackTemp := metaStackPointer;
  61. X            j := j + PatSize(pat, j);
  62. X            i := offset;
  63. X            {match as many as possible }
  64. X            while (not done) and (lin[i] <> ENDSTR) do
  65. X                if (not OMatch(lin, i, pat, j)) then begin
  66. X                    metaStackPointer := metaStackTemp;
  67. X                    done := true;
  68. X                end
  69. X                else
  70. X                    metaStackTemp := metaStackPointer;
  71. X            { i points to input character that made us fail }
  72. X            { match rest of pattern against rest of input }
  73. X            { shrink closure by 1 after each failure }
  74. X            done := false;
  75. X            while (not done) and (i >= offset) do begin
  76. X                metaStackTemp := metaStackPointer;
  77. X                k := RAMatch(lin, i, pat, j+PatSize(pat, j));
  78. X                if (k > 0) then { matched rest of pattern}
  79. X                    done := true
  80. X                else begin
  81. X                    metaStackPointer := metaStackTemp;
  82. X                    i := i - 1
  83. X                end
  84. X            end;
  85. X            offset := k;  { if k = 0 failure, else success }
  86. X            done := true
  87. X        end
  88. X        else if (not OMatch(lin, offset, pat, j)) then begin
  89. X            offset := 0;
  90. X            done := true
  91. X        end
  92. X        else  { OMatch succeeded on this pattern element }
  93. X            j := j + PatSize(pat, j);
  94. X    RAMatch := offset
  95. Xend;
  96. /
  97. echo 'x - default.pascal'
  98. sed 's/^X//' > default.pascal << '/'
  99. X{
  100. X    Copyright (c) 1981
  101. X    By:    Bell Telephone Laboratories, Inc. and
  102. X        Whitesmiths, Ltd.,
  103. X
  104. X    This software is derived from the book
  105. X        "Software Tools In Pascal", by
  106. X        Brian W. Kernighan and P.J. Plauger
  107. X        Addison-Wesley, 1981
  108. X        ISBN 0-201-10342-7
  109. X
  110. X    Right is hereby granted to freely distribute or duplicate this
  111. X    software, providing distribution or duplication is not for profit
  112. X    or other commerical gain and that this copyright notice remains 
  113. X    intact.
  114. X}
  115. X{ Default -- set Defaulted line numbers }
  116. Xsegment Default;
  117. X%include swtools
  118. X%include editcons
  119. X%include edittype
  120. X%include editproc
  121. X%include editref
  122. Xfunction Default;
  123. Xbegin
  124. X    if (nLines = 0) then begin
  125. X        line1 := def1;
  126. X        line2 := def2
  127. X    end;
  128. X    if (line1 > line2) or (line1 <= 0) then
  129. X        status := ERR
  130. X    else
  131. X       status := OK;
  132. X    Default := status
  133. Xend;
  134. /
  135. echo 'x - eval.pascal'
  136. sed 's/^X//' > eval.pascal << '/'
  137. X{
  138. X    Copyright (c) 1981
  139. X    By:    Bell Telephone Laboratories, Inc. and
  140. X        Whitesmiths, Ltd.,
  141. X
  142. X    This software is derived from the book
  143. X        "Software Tools In Pascal", by
  144. X        Brian W. Kernighan and P.J. Plauger
  145. X        Addison-Wesley, 1981
  146. X        ISBN 0-201-10342-7
  147. X
  148. X    Right is hereby granted to freely distribute or duplicate this
  149. X    software, providing distribution or duplication is not for profit
  150. X    or other commerical gain and that this copyright notice remains 
  151. X    intact.
  152. X}
  153. X{ Eval -- expand args i..j: do built-in or push back defn }
  154. Xsegment Eval;
  155. X%include swtools
  156. X%include macdefs
  157. X%include macproc
  158. Xprocedure Eval;
  159. Xvar
  160. X    argNo, k, t: Integer;
  161. X    temp: StringType;
  162. X    l,m,n: Integer;
  163. Xbegin
  164. X    t := argStk[i];
  165. X    if traceing then begin
  166. X        MPutStr('Traceing -$E', STDOUT);
  167. X        case td of
  168. X            DEFTYPE:
  169. X                MPutStr('define($N$E', STDOUT);
  170. X            EXPRTYPE:
  171. X                MPutStr('expr($N$E', STDOUT);
  172. X            SUBTYPE:
  173. X                MPutStr('substr($N$E', STDOUT);
  174. X            IFTYPE:
  175. X                MPutStr('ifelse($N$E', STDOUT);
  176. X            LENTYPE:
  177. X                MPutStr('len($N$E', STDOUT);
  178. X            CHQTYPE:
  179. X                MPutStr('changeq($N$E', STDOUT)
  180. X            otherwise
  181. X                MPutStr('macro expansion:$N$E', STDOUT);
  182. X        end {case};
  183. X        for l := i + 2 to j do begin
  184. X            CsCopy(evalStk, argStk[l], temp);
  185. X            PutStr(temp, STDOUT);
  186. X            PutCF(NEWLINE, STDOUT)
  187. X        end {for};
  188. X        MPutStr('<<<<<<$N$E', STDOUT);
  189. X    end {if};
  190. X
  191. X    if (td = DEFTYPE) then
  192. X        DoDef(argStk, i, j)
  193. X    else if (td = EXPRTYPE) then
  194. X        DoExpr(argStk, i, j)
  195. X    else if (td = SUBTYPE) then
  196. X        DoSub(argStk, i, j)
  197. X    else if (td = IFTYPE) then
  198. X        DoIf(argStk, i, j)
  199. X    else if (td = LENTYPE) then
  200. X        DoLen(argStk, i, j)
  201. X    else if (td = CHQTYPE) then
  202. X        DoChq(argStk, i, j)
  203. X    else begin
  204. X        k := t;
  205. X        while (evalStk[k] <> ENDSTR) do
  206. X            k := k + 1;
  207. X        k := k - 1;   { last character of data }
  208. X        while (k > t) do begin
  209. X            if (evalStk[k-1] <> ARGFLAG) then
  210. X                PutBack(evalStk[k])
  211. X            else begin
  212. X                argNo := Ord(evalStk[k]) - Ord(DIG0);
  213. X                if (argNo >= 0) and (argNo < j-1) then begin
  214. X                    CsCopy(evalStk, argStk[i+argNo+1], temp);
  215. X                    PBStr(temp)
  216. X                end {if};
  217. X                k := k - 1 { skip over $ }
  218. X            end {if};
  219. X            k := k - 1
  220. X        end {while};
  221. X        if (k = t) then   { do last character }
  222. X            PutBack(evalStk[k])
  223. X    end {if}
  224. Xend {Eval};
  225. /
  226. echo 'x - kwic.pascal'
  227. sed 's/^X//' > kwic.pascal << '/'
  228. X{
  229. X    Copyright (c) 1982
  230. X    By:    Chris Lewis
  231. X
  232. X    Right is hereby granted to freely distribute or duplicate this
  233. X    software, providing distribution or duplication is not for profit
  234. X    or other commerical gain and that this copyright notice remains 
  235. X    intact.
  236. X}
  237. X{ Kwic -- make Keyword in Context index }
  238. Xprogram Kwic;
  239. X%include swtools
  240. X%include cms
  241. Xconst
  242. X    FOLD = DOLLAR;
  243. Xvar
  244. X    buf: StringType;
  245. X    tempFile1: FileDesc;
  246. X    tempFile2: FileDesc;
  247. X    fileName: StringType;
  248. X    RCode: Integer;
  249. X{ Rotate -- output rotated lines }
  250. Xprocedure Rotate (var buf: StringType; n: Integer);
  251. Xvar
  252. X    i: Integer;
  253. Xbegin
  254. X    i := n;
  255. X    while (buf[i] <> NEWLINE) and (buf[i] <> ENDSTR) do begin
  256. X        PutCF(buf[i], tempFile1);
  257. X        i := i + 1
  258. X    end;
  259. X    PutCF(FOLD, tempFile1);
  260. X    for i := 1 to n - 1 do
  261. X        PutCF(buf[i], tempFile1);
  262. X    PutCF(NEWLINE, tempFile1)
  263. Xend;
  264. X{ PutRot -- create lines with keyword at front }
  265. Xprocedure PutRot(var buf: StringType);
  266. Xvar
  267. X    i: Integer;
  268. Xbegin
  269. X    i := 1;
  270. X    while (buf[i] <> NEWLINE) and (buf[i] <> ENDSTR) do begin
  271. X        if (IsAlphaNum(buf[i])) then begin
  272. X            Rotate(buf, i); { token starts at "i" }
  273. X            repeat
  274. X                i := i + 1
  275. X            until (not IsAlphaNum(buf[i]))
  276. X        end;
  277. X        i := i + 1
  278. X    end
  279. Xend;
  280. X/* temporarily commented out until CMS cmd works
  281. X{ UnRotate -- Unrotate lines rotated by first half of KWIC }
  282. Xprocedure UnRotate;
  283. Xconst
  284. X    MAXOUT = 80;
  285. X    MIDDLE = 40;
  286. Xvar
  287. X    inBuf, outBuf: StringType;
  288. X    i, j, f: Integer;
  289. Xbegin
  290. X    while (GetLine(inBuf, tempFile2, MAXSTR)) do begin
  291. X        for i := 1 to MAXOUT -1 do
  292. X             outBuf[i] := BLANK;
  293. X        f := StrIndex(inBuf, FOLD);
  294. X        j := MIDDLE - 1;
  295. X        for i := StrLength(inBuf)-1 downto f+1 do begin
  296. X             outBuf[j] := inBuf[i];
  297. X             j := j - 1;
  298. X             if (j <= 0) then
  299. X                 j := MAXOUT - 1
  300. X        end;
  301. X        j := MIDDLE + 3;
  302. X        for i := 1 to f-1 do begin
  303. X             outBuf[j] := inBuf[i];
  304. X             j := j mod (MAXOUT - 1) + 1
  305. X        end;
  306. X        for j := 1 to MAXOUT - 1 do
  307. X             if (outBuf[j] <> BLANK) then
  308. X                 i := j;
  309. X        outBuf[i+1] := ENDSTR;
  310. X        PutStr(outBuf, STDOUT);
  311. X        PutC(NEWLINE)
  312. X    end
  313. Xend;
  314. X*/
  315. X{ Main program for Kwic }
  316. Xbegin
  317. X    ToolInit;
  318. X/* Cannot get CMS to call sort properly
  319. X    CvtSST('KWIC1 TEMP A', fileName);
  320. X    tempFile1 := FOpen(fileName, IOWRITE);
  321. X    if tempFile1 = IOERROR then
  322. X        Error('Cannot open first KWIC temporary');
  323. X*/
  324. X/* */
  325. X    tempFile1 := STDOUT;
  326. X/* */
  327. X    while (GetLine(buf, STDIN, MAXSTR)) do
  328. X        PutRot(buf);
  329. X/*
  330. X    Cms('EXEC OSSORT KWIC1 TEMP A KWIC2 TEMP A 1 10', RCode);
  331. X    if RCode <> 0 then
  332. X         Error('KWIC: BNRSORT failed');
  333. X    CvtSST('KWIC2 TEMP A', fileName);
  334. X    tempFile2 := FOpen(fileName, IOREAD);
  335. X    if tempFile2 = IOERROR then
  336. X         Error('KWIC: cannot open sorted rotated file');
  337. X    UnRotate
  338. X*/
  339. Xend.
  340. /
  341. echo 'x - macro.pascal'
  342. sed 's/^X//' > macro.pascal << '/'
  343. X{
  344. X    Copyright (c) 1981
  345. X    By:    Bell Telephone Laboratories, Inc. and
  346. X        Whitesmiths, Ltd.,
  347. X
  348. X    This software is derived from the book
  349. X        "Software Tools In Pascal", by
  350. X        Brian W. Kernighan and P.J. Plauger
  351. X        Addison-Wesley, 1981
  352. X        ISBN 0-201-10342-7
  353. X
  354. X    Right is hereby granted to freely distribute or duplicate this
  355. X    software, providing distribution or duplication is not for profit
  356. X    or other commerical gain and that this copyright notice remains 
  357. X    intact.
  358. X}
  359. X{ Macro -- expand macros with arguments }
  360. Xprogram Macro;
  361. X%include swtools
  362. X%include macdefs
  363. X%include macproc
  364. Xbegin
  365. X    ToolInit;
  366. X    InitMacro;
  367. X    Install(defName, null, DEFTYPE);
  368. X    Install(exprName, null, EXPRTYPE);
  369. X    Install(subName, null, SUBTYPE);
  370. X    Install(ifName, null, IFTYPE);
  371. X    Install(lenName, null, LENTYPE);
  372. X    Install(chqName, null, CHQTYPE);
  373. X
  374. X    cp := 0;
  375. X    ap := 1;
  376. X    ep := 1;
  377. X    while (GetTok(token, MAXTOK) <> ENDFILE) do
  378. X        if (IsLetter(token[1])) then begin
  379. X            if (not Lookup(token, defn, tokType)) then
  380. X                PutTok(token)
  381. X            else begin
  382. X                cp := cp + 1;
  383. X                if (cp > CALLSIZE) then
  384. X                    Error('Macro: call stack overflow');
  385. X                callStk[cp] := ap;
  386. X                typeStk[cp] := tokType;
  387. X                ap := Push(ep, argStk, ap);
  388. X                PutTok(defn);      { push definition }
  389. X                PutChr(ENDSTR);
  390. X                ap := Push(ep, argStk, ap);
  391. X                PutTok(token);    { stack name }
  392. X                PutChr(ENDSTR);
  393. X                ap := Push(ep, argStk, ap);
  394. X                t := GetTok(token, MAXTOK); { peek at next }
  395. X                PBStr(token);
  396. X                if (t <> LPAREN) then begin { add () }
  397. X                    PutBack(RPAREN);
  398. X                    PutBack(LPAREN);
  399. X                end;
  400. X                pLev[cp] := 0
  401. X            end
  402. X        end
  403. X        else if (token[1] = lQuote) then begin { strip quotes }
  404. X            nlPar := 1;
  405. X            repeat
  406. X                t := GetTok(token, MAXTOK);
  407. X                if (t = rQuote) then
  408. X                    nlPar := nlPar - 1
  409. X                else if (t = lQuote) then
  410. X                    nlPar := nlPar + 1
  411. X                else if (t = ENDFILE) then
  412. X                    Error('Macro: missing right quote');
  413. X                if nlPar > 0 then
  414. X                    PutTok(token)
  415. X            until (nlPar = 0)
  416. X        end
  417. X        else if (cp = 0) then { not in macro at all }
  418. X            PutTok(token)
  419. X        else if (token[1] = LPAREN) then begin
  420. X            if (pLev[cp] > 0) then
  421. X                PutTok(token);
  422. X            pLev[cp] := pLev[cp] + 1
  423. X        end {then}
  424. X        else if (token[1] = RPAREN) then begin
  425. X            pLev[cp] := pLev[cp] - 1;
  426. X            if (pLev[cp] > 0) then
  427. X                PutTok(token)
  428. X            else begin { end of argument list }
  429. X                PutChr(ENDSTR);
  430. X                Eval(argStk, typeStk[cp], callStk[cp], ap - 1);
  431. X                ap := callStk[cp];  { pop eval stack }
  432. X                ep := argStk[ap];
  433. X                cp := cp - 1
  434. X            end
  435. X        end
  436. X        else if (token[1] = COMMA) and (pLev[cp] = 1) then begin
  437. X            PutChr(ENDSTR);   { new argument }
  438. X            ap := Push(ep, argStk, ap)
  439. X        end {then}
  440. X        else
  441. X            PutTok(token);   { just stack it }
  442. X    if (cp <> 0) then
  443. X        Error('Macro: unexpected end of input')
  444. Xend.
  445. /
  446. echo 'x - makepat.pascal'
  447. sed 's/^X//' > makepat.pascal << '/'
  448. X{
  449. X    Copyright (c) 1981
  450. X    By:    Bell Telephone Laboratories, Inc. and
  451. X        Whitesmiths, Ltd.,
  452. X
  453. X    This software is derived from the book
  454. X        "Software Tools In Pascal", by
  455. X        Brian W. Kernighan and P.J. Plauger
  456. X        Addison-Wesley, 1981
  457. X        ISBN 0-201-10342-7
  458. X
  459. X    Right is hereby granted to freely distribute or duplicate this
  460. X    software, providing distribution or duplication is not for profit
  461. X    or other commerical gain and that this copyright notice remains 
  462. X    intact.
  463. X}
  464. X{ MakePat -- make pattern from arg[i], terminate at delim }
  465. Xsegment MakePat;
  466. X%include swtools
  467. X%include patdef
  468. X%include metadef
  469. Xfunction MakePat;
  470. Xvar
  471. X    i,j, lastJ, lj: Integer;
  472. X    k: Integer;
  473. X    done, junk: Boolean;
  474. Xbegin
  475. X    j := 1;  { pat index}
  476. X    i := start;  { arg index}
  477. X    metaStackPointer := 0;
  478. X    metaIndex := 1;
  479. X    done := false;
  480. X    k := start;
  481. X    while (arg[k] <> delim) and ((k + 2) <= MAXSTR) do
  482. X        if (arg[k] = NEWLINE) or (arg[k] = ENDSTR) then begin
  483. X            arg[k] := delim;
  484. X            arg[k+1] := NEWLINE;
  485. X            arg[k+2] := ENDSTR;
  486. X        end
  487. X        else
  488. X            k := k + 1;
  489. X
  490. X    while (not done) and (arg[i] <> delim) and
  491. X          (arg[i] <> ENDSTR) do begin
  492. X        lj := j;
  493. X        if (arg[i] = ANY) then
  494. X            junk := AddStr(ANY, pat, j, MAXPAT)
  495. X        else if (arg[i] = BOL) and (i = start) then
  496. X            junk := AddStr(BOL, pat, j, MAXPAT)
  497. X        else if (arg[i] = BOM) then begin
  498. X             junk := AddStr(BOM, pat, j, MAXPAT);
  499. X             metaStackPointer := metaStackPointer + 1;
  500. X             metaIndex := metaIndex + 1;
  501. X             if (metaStackPointer > 9) or
  502. X               (metaIndex > 9) then
  503. X                 done := true
  504. X        end
  505. X        else if (arg[i] = EOM) and (metaStackPointer > 0) then begin
  506. X            junk := AddStr(EOM, pat, j, MAXPAT);
  507. X            metaStackPointer := metaStackPointer - 1;
  508. X            if (metaStackPointer < 0) then
  509. X                done := true
  510. X        end
  511. X        else if (arg[i] = EOL) and (arg[i+1] = delim) then
  512. X            junk := AddStr(EOL, pat, j, MAXPAT)
  513. X        else if (arg[i] = CCL) then
  514. X            done := (GetCCL(arg, i, pat, j) = false)
  515. X        else if (arg[i] = CLOSURE) and (i > start) then begin
  516. X            lj := lastJ;
  517. X            if (pat[lj] in [BOL, EOL, CLOSURE]) then
  518. X                done := true             { force loop termination }
  519. X            else
  520. X                STClose(pat, j, lastJ)
  521. X        end
  522. X        else begin
  523. X            junk := AddStr(LITCHAR, pat, j, MAXPAT);
  524. X            junk := AddStr(Esc(arg,i), pat, j, MAXPAT)
  525. X        end;
  526. X        lastJ := lj;
  527. X        if (not done) then
  528. X            i := i + 1;
  529. X    end;
  530. X    if (done) or (arg[i] <> delim) or (metaStackPointer <> 0) then
  531. X        MakePat := 0
  532. X    else if (not AddStr(ENDSTR, pat, j, MAXPAT)) then
  533. X        MakePat := 0                { no room}
  534. X    else
  535. X        MakePat := i;
  536. Xend;
  537. /
  538. echo 'x - setbuf.pascal'
  539. sed 's/^X//' > setbuf.pascal << '/'
  540. X{
  541. X    Copyright (c) 1981
  542. X    By:    Bell Telephone Laboratories, Inc. and
  543. X        Whitesmiths, Ltd.,
  544. X
  545. X    This software is derived from the book
  546. X        "Software Tools In Pascal", by
  547. X        Brian W. Kernighan and P.J. Plauger
  548. X        Addison-Wesley, 1981
  549. X        ISBN 0-201-10342-7
  550. X
  551. X    Right is hereby granted to freely distribute or duplicate this
  552. X    software, providing distribution or duplication is not for profit
  553. X    or other commerical gain and that this copyright notice remains 
  554. X    intact.
  555. X}
  556. X{ SetBuf -- set Buffer and other Buffer handlers (new-free) }
  557. Xsegment SetBuf;
  558. X%include swtools
  559. X%include editcons
  560. X%include edittype
  561. X%include editproc
  562. X%include editref
  563. Xconst
  564. X    MAXLINES = 10000;
  565. Xtype
  566. X    BufType =    { in-memory new/free buffer handler }
  567. X        record
  568. X            txt: StringPtr;      { text of line }
  569. X            mark: Boolean;      { mark for line }
  570. X        end;
  571. Xref OUTOFSPACE: Boolean;
  572. Xstatic heapMark: @ Integer;
  573. Xstatic  { This is a PRIVATE buffer }
  574. X    intBuff: array [0..MAXLINES] of BufType;
  575. X{ SetBuf -- (new-free) initialize line storage Buffer }
  576. Xprocedure SetBuf;
  577. Xvar
  578. X    i: 0..MAXLINES;
  579. Xbegin
  580. X    Mark(heapMark);
  581. X    for i := 0 to MAXLINES do
  582. X        intBuff[i].txt := nil;
  583. X    curLn := 0;
  584. X    lastLn := 0
  585. Xend;
  586. X{ ClrBuf -- (new-free) release storage }
  587. Xprocedure ClrBuf;
  588. Xvar i: 0..MAXLINES;
  589. Xbegin
  590. X    Release(heapMark)
  591. Xend;
  592. X{ GetTxt -- (new-free) get text from line n into s }
  593. Xprocedure GetTxt;
  594. Xbegin
  595. X    { note: the null is already there }
  596. X    if intBuff[n].txt = nil then
  597. X        s[1] := ENDSTR
  598. X    else
  599. X        s := intBuff[n].txt@;
  600. Xend;
  601. X{ PutTxt -- (new-free) put text from lin after curLn }
  602. Xfunction PutTxt;
  603. Xvar
  604. X    sSize: Integer;
  605. Xbegin
  606. X    PutTxt := ERR;
  607. X    if (lastLn < MAXLINES) then begin
  608. X        lastLn := lastLn + 1;
  609. X        sSize := StrLength(lin) + 1;
  610. X        if intBuff[lastLn].txt = nil then
  611. X            New(intBuff[lastLn].txt, sSize)
  612. X        else if (sSize > MaxLength(intBuff[lastLn].txt@)) then begin
  613. X            Dispose(intBuff[lastLn].txt);
  614. X            New(intBuff[lastLn].txt, sSize)
  615. X        end;
  616. X        { Check for New failing }
  617. X        if OUTOFSPACE then begin
  618. X            intBuff[lastLn].txt := nil;  { insurance }
  619. X            lastLn := lastLn - 1; { insurance }
  620. X            OUTOFSPACE := false;
  621. X            Message('out of space, write out and edit again');
  622. X            return   { error }
  623. X        end;
  624. X        WriteStr(intBuff[lastLn].txt@, lin:sSize);
  625. X        PutMark(lastLn, false);
  626. X        BlkMove(lastLn, lastLn, curLn);
  627. X        curLn := curLn + 1;
  628. X        PutTxt := OK
  629. X    end
  630. Xend;
  631. X{ GetMark -- get mark from nth line }
  632. Xfunction GetMark;
  633. Xbegin
  634. X    GetMark := intBuff[n].mark
  635. Xend;
  636. X{ PutMark -- put mark m on nth line }
  637. Xprocedure PutMark;
  638. Xbegin
  639. X    intBuff[n].mark := m
  640. Xend;
  641. X{ BlkMove -- move block of lines n1..n2 to after n3 }
  642. Xprocedure BlkMove;
  643. Xbegin
  644. X    if (n3 < n1-1) then begin
  645. X        Reverse (n3+1,n1-1);
  646. X        Reverse (n1,n2);
  647. X        Reverse (n3+1,n2)
  648. X    end
  649. X    else if (n3 > n2) then begin
  650. X        Reverse(n1,n2);
  651. X        Reverse(n2+1,n3);
  652. X        Reverse(n1,n3)
  653. X    end
  654. Xend;
  655. X{ Reverse -- reverse intBuff[n1]...intBuff[n2] }
  656. Xprocedure Reverse;
  657. Xvar temp: BufType;
  658. Xbegin
  659. X    while (n1 < n2) do begin
  660. X        temp := intBuff[n1];
  661. X        intBuff[n1] := intBuff[n2];
  662. X        intBuff[n2] := temp;
  663. X        n1 := n1 + 1;
  664. X        n2 := n2 - 1
  665. X    end
  666. Xend;
  667. /
  668. echo 'x - sortdriv.pascal'
  669. sed 's/^X//' > sortdriv.pascal << '/'
  670. X{
  671. X    Copyright (c) 1981
  672. X    By:    Bell Telephone Laboratories, Inc. and
  673. X        Whitesmiths, Ltd.,
  674. X
  675. X    This software is derived from the book
  676. X        "Software Tools In Pascal", by
  677. X        Brian W. Kernighan and P.J. Plauger
  678. X        Addison-Wesley, 1981
  679. X        ISBN 0-201-10342-7
  680. X
  681. X    Right is hereby granted to freely distribute or duplicate this
  682. X    software, providing distribution or duplication is not for profit
  683. X    or other commerical gain and that this copyright notice remains 
  684. X    intact.
  685. X}
  686. X{ SortDriv -- Driver and Quick sort }
  687. Xprogram SortDriv;
  688. X%include SWTOOLS
  689. X%include ioref
  690. Xconst
  691. X    inCoreSize = 500;
  692. Xtype
  693. X    LineType = StringPtr;
  694. Xvar
  695. X    notEof: Boolean;
  696. X    inBuf: array [1..inCoreSize] of LineType;
  697. X    i: Integer;
  698. X    temp: StringType;
  699. Xprocedure PText (nLines: Integer; outFile: FileDesc);
  700. Xvar
  701. X    i: Integer;
  702. Xbegin
  703. X    for i := 1 to nLines do
  704. X        PutStr (inBuf[i]@, outFile);
  705. Xend; {PText}
  706. Xfunction GText (var nLines: Integer; inFile: FileDesc): Boolean;
  707. Xvar
  708. X    i: Integer;
  709. X    temp: StringType;
  710. Xbegin
  711. X    nLines := 0;
  712. X    done := (GetLine(temp, inFile, MAXSTR) = false);
  713. X    while (not done) and (nLines < inCoreSize) do begin
  714. X        nLines := nLines + 1;
  715. X        inBuf[nLines]@ := Str(temp);
  716. X        done := (GetLine(temp, inFile, MAXSTR) = false);
  717. X    end; {while}
  718. Xend; {GText}
  719. X
  720. Xprocedure QSort(l,r: integer);
  721. X    var i,j: integer;
  722. X        temp, hold: LineType;
  723. Xbegin
  724. X    i := l;
  725. X    j := r;
  726. X    temp := inBuf[(i+j) div 2];
  727. X    repeat
  728. X        while inBuf[i]@ < temp@ do
  729. X            i := i+1;
  730. X        while temp@ < inBuf[j]@ do
  731. X            j := j-1;
  732. X        if i <= j then begin
  733. X            hold := inBuf[i];
  734. X            inBuf[i] := inBuf[j];
  735. X            inBuf[j] := hold;
  736. X            i := i+1;
  737. X            j := j-1
  738. X        end
  739. X    until i > j;
  740. X    if l < j then
  741. X        QSort(l,j);
  742. X    if i < r then
  743. X        QSort(i,r)
  744. Xend {QSort} ;
  745. Xvar
  746. X    done: Boolean;
  747. X    nLines: Integer;
  748. X    high: Integer;
  749. X    outFile: FileDesc;
  750. Xbegin
  751. X    ToolInit;
  752. X    high := 0;
  753. X    for i := 1 to inCoreSize do
  754. X        New(inBuf[i], SizeOf(StringType));
  755. X    repeat { initial formation of runs }
  756. X        done := GText (nLines, STDIN);
  757. X        QSort(1, nLines);
  758. X        high := high + 1;
  759. X        outFile := MakeFile(high);
  760. X        PText (nLines, outFile);
  761. X        Close (outFile);
  762. X    until (done);
  763. X    low := 1;
  764. X    while (low < high) do begin { merge runs }
  765. X        lim := Min(low +  MERGEORDER - 1, high);
  766. X        GOpen (inFile, low, lim);
  767. X        high := high + 1;
  768. X        outFile := MakeFile(high);
  769. X        Merge(inFile, lim-low+1, outFile);
  770. X        Close (outFile);
  771. X        GRemove (inFile, low, lim);
  772. X        low := low + MERGEORDER;
  773. X    end; {while}
  774. X    GName (high, name) { final cleanup }
  775. X    outFile := FOpen (name, IOREAD);
  776. X    FCopy (outFile, STDOUT);
  777. X    Close (outFile);
  778. X    Remove (name);
  779. Xend.
  780. /
  781. echo 'x - swtools.copy'
  782. sed 's/^X//' > swtools.copy << '/'
  783. X*COPY NOTICE
  784. X{
  785. X    Copyright (c) 1981
  786. X    By:    Bell Telephone Laboratories, Inc. and
  787. X        Whitesmiths, Ltd.,
  788. X
  789. X    This software is derived from the book
  790. X        "Software Tools In Pascal", by
  791. X        Brian W. Kernighan and P.J. Plauger
  792. X        Addison-Wesley, 1981
  793. X        ISBN 0-201-10342-7
  794. X
  795. X    Right is hereby granted to freely distribute or duplicate this
  796. X    software, providing distribution or duplication is not for profit
  797. X    or other commerical gain and that this copyright notice remains 
  798. X    intact.
  799. X}
  800. X*COPY SWTOOLS
  801. X{ SWTOOLS -- Software Tools Environment Definitions }
  802. X%print off
  803. Xconst
  804. X    IOERROR = 0;    { status values for open files }
  805. X    STDIN = 1;
  806. X    STDOUT = 2;
  807. X    STDERR = 3;
  808. X
  809. X{  other IO-related stuff }
  810. X
  811. X    IOAVAIL = 1;
  812. X    IOREAD  = 2;
  813. X    IOWRITE = 3;
  814. X    MAXOPEN = 10;
  815. X    MAXARG  = 30;
  816. X
  817. X{  universal manifest constants }
  818. X
  819. X    ENDFILE = Chr(1);
  820. X    ENDSTR = Chr(0);
  821. X    MAXSTR = 200;
  822. X
  823. X{ EBCDIC character set }
  824. X
  825. X    BACKSPACE = Chr(8);
  826. X    BACKSLASH = CHR(224);
  827. X    TAB    = Chr(5);
  828. X    NEWLINE = Chr(10);
  829. X    BLANK  = ' ';
  830. X    EXCLAM = '!';
  831. X    QUESTION = '?';
  832. X    DQUOTE = '"';
  833. X    SHARP  = '#';
  834. X    DOLLAR = '$';
  835. X    PERCENT = '%';
  836. X    AMPER  = '&';
  837. X    SQUOTE = '''';
  838. X    ACUTE  = SQUOTE;
  839. X    LPAREN = '(';
  840. X    RPAREN = ')';
  841. X    STAR   = '*';
  842. X    PLUS   = '+';
  843. X    COMMA  = ',';
  844. X    MINUS  = '-';
  845. X    DASH   = MINUS;
  846. X    PERIOD = '.';
  847. X    SLASH  = '/';
  848. X    COLON  = ':';
  849. X    SEMICOL = ';';
  850. X    LESS   = '<';
  851. X    EQUALS = '=';
  852. X    GREATER = '>';
  853. X    ATSIGN = '@';
  854. X    ESCAPE = ATSIGN;
  855. X    LBRACK = Chr(173);
  856. X    RBRACK = Chr(189);
  857. X    CARET  = '^';
  858. X    UNDERLINE = '_';
  859. X    GRAVE  = '9C'XC;
  860. X    LBRACE = Chr(139);
  861. X    RBRACE = Chr(155);
  862. X    BAR    = '|';
  863. X    TILDE  = '~';
  864. X    LETA = 'a';
  865. X    LETB = 'b';
  866. X    LETC = 'c';
  867. X    LETD = 'd';
  868. X    LETE = 'e';
  869. X    LETF = 'f';
  870. X    LETG = 'g';
  871. X    LETH = 'h';
  872. X    LETI = 'i';
  873. X    LETJ = 'j';
  874. X    LETK = 'k';
  875. X    LETL = 'l';
  876. X    LETM = 'm';
  877. X    LETN = 'n';
  878. X    LETO = 'o';
  879. X    LETP = 'p';
  880. X    LETQ = 'q';
  881. X    LETR = 'r';
  882. X    LETS = 's';
  883. X    LETT = 't';
  884. X    LETU = 'u';
  885. X    LETV = 'v';
  886. X    LETW = 'w';
  887. X    LETX = 'x';
  888. X    LETY = 'y';
  889. X    LETZ = 'z';
  890. X    BIGA = 'A';
  891. X    BIGB = 'B';
  892. X    BIGC = 'C';
  893. X    BIGD = 'D';
  894. X    BIGE = 'E';
  895. X    BIGF = 'F';
  896. X    BIGG = 'G';
  897. X    BIGH = 'H';
  898. X    BIGI = 'I';
  899. X    BIGJ = 'J';
  900. X    BIGK = 'K';
  901. X    BIGL = 'L';
  902. X    BIGM = 'M';
  903. X    BIGN = 'N';
  904. X    BIGO = 'O';
  905. X    BIGP = 'P';
  906. X    BIGQ = 'Q';
  907. X    BIGR = 'R';
  908. X    BIGS = 'S';
  909. X    BIGT = 'T';
  910. X    BIGU = 'U';
  911. X    BIGV = 'V';
  912. X    BIGW = 'W';
  913. X    BIGX = 'X';
  914. X    BIGY = 'Y';
  915. X    BIGZ = 'Z';
  916. X    DIG0 = '0';
  917. X    DIG1 = '1';
  918. X    DIG2 = '2';
  919. X    DIG3 = '3';
  920. X    DIG4 = '4';
  921. X    DIG5 = '5';
  922. X    DIG6 = '6';
  923. X    DIG7 = '7';
  924. X    DIG8 = '8';
  925. X    DIG9 = '9';
  926. X
  927. X{ Standard types }
  928. X
  929. Xtype
  930. X    FileDesc = IOERROR..MAXOPEN;
  931. X    StringType = packed array [1..MAXSTR] of Char;
  932. X    CharType = Char;
  933. X
  934. X{ Externally supplied primitive interfaces }
  935. X
  936. Xprocedure Error (s: String(MAXSTR));
  937. X    external;
  938. Xprocedure FClose (fd: FileDesc);
  939. X    external;
  940. Xfunction FCreate (name: StringType; mode: Integer): FileDesc;
  941. X    external;
  942. Xfunction FOpen (name: StringType; mode: Integer): FileDesc;
  943. X    external;
  944. Xprocedure FSeek (recno: Integer; fd: FileDesc);
  945. X    external;
  946. Xfunction GetArg (n: Integer; var str: StringType;
  947. X        maxSize: Integer): Boolean;
  948. X    external;
  949. Xfunction GetC (var c: CharType): CharType;
  950. X    external;
  951. Xfunction GetCF (var c: CharType; fd: FileDesc): CharType;
  952. X    external;
  953. Xfunction GetLine (var str: StringType; fd: FileDesc;
  954. X        maxSize: Integer): Boolean;
  955. X    external;
  956. Xprocedure Message (s: String(MAXSTR));
  957. X    external;
  958. Xfunction Nargs: Integer;
  959. X    external;
  960. Xprocedure PutC (c: CharType);
  961. X    external;
  962. Xprocedure PutCF (c: CharType; fd: FileDesc);
  963. X    external;
  964. Xprocedure PutStr (const str: StringType; fd: FileDesc);
  965. X    external;
  966. Xprocedure MPutStr (const str: StringType; fd: FileDesc);
  967. X    external;
  968. Xprocedure Remove (var name: StringType);
  969. X    external;
  970. Xprocedure SysExit (status: Integer);
  971. X    external;
  972. Xprocedure ToolInit;
  973. X    external;
  974. X
  975. X{ Externally supplied utilities }
  976. X
  977. Xfunction AddStr (c: CharType; var outSet: StringType;
  978. X        var j: Integer; maxSet: Integer): Boolean;
  979. X    external;
  980. Xfunction CToI (var s: StringType; var i: Integer): Integer;
  981. X    external;
  982. Xprocedure CvtSST (src: String(MAXSTR); var dest: StringType);
  983. X    external;
  984. Xprocedure CvtSTS (src: StringType; var dest: String(MAXSTR));
  985. X    external;
  986. Xfunction Equal (var str1, str2: StringType): Boolean;
  987. X    external;
  988. Xfunction Esc (var s: StringType; var i: Integer): CharType;
  989. X    external;
  990. Xprocedure FCopy (fin, fout: FileDesc);
  991. X    external;
  992. Xfunction GetFid (var line: StringType; idx: Integer;
  993. X        var fileName: StringType): Boolean;
  994. X    external;
  995. Xfunction GetWord (var s: StringType; i: Integer;
  996. X        var out: StringType): Integer;
  997. X    external;
  998. Xfunction IsAlphaNum (c: CharType): Boolean;
  999. X    external;
  1000. Xfunction IsDigit (c: CharType): Boolean;
  1001. X    external;
  1002. Xfunction IsLetter (c: CharType): Boolean;
  1003. X    external;
  1004. Xfunction IsLower (c: CharType): Boolean;
  1005. X    external;
  1006. Xfunction IsUpper (c: CharType): Boolean;
  1007. X    external;
  1008. Xfunction IToC (n: Integer; var s: StringType; i: Integer): Integer;
  1009. X    external;
  1010. Xfunction MustOpen (var fName: StringType; fMode: Integer): FileDesc;
  1011. X    external;
  1012. Xprocedure PutDec (n, w: Integer);
  1013. X    external;
  1014. Xprocedure SCopy (var src: StringType; i: Integer;
  1015. X        var dest: StringType; j: Integer);
  1016. X    external;
  1017. Xfunction StrIndex (const s: StringType; c: CharType): Integer;
  1018. X    external;
  1019. Xfunction StrLength (const s: StringType): Integer;
  1020. X    external;
  1021. Xprocedure ProgExit (const returnCode: Integer); external;
  1022. X%print on
  1023. X*COPY EDITCONS
  1024. X{ EditCons -- const declarations for edit }
  1025. Xconst
  1026. X    CURLINE = PERIOD;
  1027. X    LASTLINE = DOLLAR;
  1028. X    SCAN = SLASH;
  1029. X    BACKSCAN = BACKSLASH;
  1030. X    ACMD = LETA;
  1031. X    CCMD = LETC;
  1032. X    DCMD = LETD;
  1033. X    ECMD = LETE;
  1034. X    EQCMD = EQUALS;
  1035. X    FCMD = LETF;
  1036. X    GCMD = LETG;
  1037. X    ICMD = LETI;
  1038. X    MCMD = LETM;
  1039. X    KCMD = LETK;
  1040. X    OCMD = LETO;
  1041. X    PCMD = LETP;
  1042. X    LCMD = LETL;
  1043. X    QCMD = LETQ;
  1044. X    RCMD = LETR;
  1045. X    SCMD = LETS;
  1046. X    WCMD = LETW;
  1047. X    XCMD = LETX;
  1048. X    promptFlag = 0;
  1049. X    verboseFlag = 1;
  1050. X    noMetaFlag = 2;
  1051. X    { insert more option flags here }
  1052. X    numFlag = 15;
  1053. X*COPY EDITTYPE
  1054. X{ EditType -- types for in-memory version of edit }
  1055. Xtype
  1056. X    STCode = (ENDDATA, ERR, OK);      { status returns }
  1057. X*COPY EDITPROC
  1058. X{ EditProc -- routine declarations for SW editor }
  1059. Xfunction GetList (var lin: StringType; var i: Integer;
  1060. X                  var status: STCode): STCode; external;
  1061. Xfunction GetOne (var lin: StringType; var i, num: Integer;
  1062. X                 var status: STCode): STCode; external;
  1063. Xfunction GetNum (var lin: StringType; var i, num: integer;
  1064. X                 var status: STCode): STCode; external;
  1065. Xfunction OptPat (var lin: StringType; var i: Integer): STCode; external;
  1066. Xfunction PatScan (way: CharType; var n: Integer): STCode; external;
  1067. Xfunction NextLn (n: Integer): Integer; external;
  1068. Xfunction PrevLn (n: Integer): Integer; external;
  1069. Xfunction Default (def1, def2: Integer;
  1070. X                  var status: STCode): STCode; external;
  1071. Xfunction DoPrint (n1, n2: Integer): STCode; external;
  1072. Xfunction DoLPrint (n1, n2: Integer): STCode; external;
  1073. Xfunction DoCmd (var lin: StringType; var i: Integer;
  1074. X                glob: Boolean; var status: STCode): STCode; external;
  1075. Xfunction Append (line: Integer; glob: Boolean): STCode; external;
  1076. Xprocedure BlkMove (n1, n2, n3: Integer); external;
  1077. Xprocedure Reverse (n1, n2: Integer); external;
  1078. Xprocedure GetTxt (n: Integer; var s: StringType); external;
  1079. Xprocedure SetBuf; external;
  1080. Xfunction PutTxt (var lin: StringType): STCode; external;
  1081. Xfunction CkP (var lin: StringType; i: Integer;
  1082. X              var pFlag: Boolean; var status: STCode):
  1083. X              STCode; external;
  1084. Xfunction LnDelete (n1, n2: Integer; var status: STCode):
  1085. X              STCode; external;
  1086. Xfunction Move (line3: Integer): STCode; external;
  1087. Xfunction Kopy (line3: Integer): STCode; external;
  1088. Xfunction GetRHS (var lin: StringType; var i: Integer;
  1089. X                 var sub: StringType; var gFlag: Boolean):
  1090. X                 STCode; external;
  1091. Xfunction SubSt (var sub: StringType; gFlag, glob: Boolean):
  1092. X                STCode; external;
  1093. Xprocedure SkipBl (var s: StringType; var i: Integer);
  1094. X    external;
  1095. Xfunction GetFn(var lin: StringType; var i:Integer;
  1096. X               var fil: StringType): STCode; external;
  1097. Xfunction DoRead (n: integer; var fil: StringType): STCode; external;
  1098. Xfunction DoWrite (n1, n2: Integer; var fil: StringType): STCode;
  1099. X                  external;
  1100. Xfunction CkGlob (var lin: StringType; var i: Integer;
  1101. X                 var status: STCode): STCode; external;
  1102. Xfunction DoGlob (var lin: StringType; var i, curSave: Integer;
  1103. X                 var status: STCode): STCode; external;
  1104. Xprocedure ClrBuf; external;
  1105. Xfunction GetMark(n: Integer): Boolean; external;
  1106. Xprocedure PutMark(n: Integer; m: Boolean); external;
  1107. Xfunction DoOption(var lin: STringType; var i: Integer):
  1108. X    STCode; external;
  1109. Xfunction OptIsOn(flag: promptFlag..numFlag): Boolean; external;
  1110. X*COPY IODEF
  1111. Xtype
  1112. X    IOBlock =
  1113. X        record
  1114. X            fileVar: Text;
  1115. X            mode: IOERROR..IOWRITE
  1116. X        end;
  1117. Xfunction FDAlloc: Integer; External;
  1118. X*COPY IOREF
  1119. X{ GlobRef -- standard global references (IO support mainly) }
  1120. X%include iodef
  1121. Xref openList: array [FileDesc] of IOBlock;
  1122. Xref ERRORIO: Boolean;
  1123. Xref ATTENTION: Boolean;
  1124. Xref cmdLin: StringType;
  1125. Xref cmdArgs: 0..MAXARG;
  1126. Xref cmdIdx: array [1..MAXARG] of 1..MAXSTR;
  1127. X*COPY EDITREF
  1128. X{ EditRef -- external reference definitions for SW editor }
  1129. Xref
  1130. X    line1: Integer;    { first line number }
  1131. X    line2: Integer;    { second line number }
  1132. X    nLines: Integer;   { # of lines specified }
  1133. X    curLn: Integer;    { current line }
  1134. X    lastLn: Integer;   { last line in buffer }
  1135. X    pat: StringType;   { pattern string }
  1136. X    lin: StringType;   { input line }
  1137. X    saveFile: StringType;  { current remembered file name }
  1138. X*COPY MATCHDEF
  1139. X{ MatchDef -- definitions of match and sub-fcns }
  1140. Xfunction PatSize (var pat: StringType; n: Integer): Integer;
  1141. X    external;
  1142. Xfunction OMatch (var lin: StringType; var i: Integer;
  1143. X                 var pat: StringType; j: Integer): Boolean;
  1144. X    external;
  1145. Xfunction Locate (c: CharType; var pat: StringType;
  1146. X                 offset: Integer): Boolean;
  1147. X    external;
  1148. Xfunction Match (var lin, pat: StringType): Boolean;
  1149. X    external;
  1150. Xfunction AMatch (var lin: StringType; offset: Integer;
  1151. X        var pat: StringType; j: Integer): Integer;
  1152. X    external;
  1153. X*COPY PATDEF
  1154. X{ PatDef -- pattern constant declarations for GetPat }
  1155. Xconst
  1156. X    MAXPAT = MAXSTR;
  1157. X    CLOSIZE = 1;   { size of closure entry }
  1158. X    BOL = PERCENT;
  1159. X    EOL = DOLLAR;
  1160. X    ANY = QUESTION;
  1161. X    CCL = LBRACK;
  1162. X    CCLEND = RBRACK;
  1163. X    NEGATE = CARET;
  1164. X    NCCL = SHARP;{ cannot be the same as NEGATE }
  1165. X    LITCHAR = LETC;
  1166. X    NCHAR = EXCLAM;
  1167. X    CLOSURE = STAR;
  1168. Xfunction GetCCL (var arg: StringType; var i: Integer;
  1169. X            var pat: StringType; var j: Integer)
  1170. X            :Boolean;
  1171. X    external;
  1172. Xprocedure StClose(var pat: StringType; var j: Integer;
  1173. X            lastJ: Integer);
  1174. X    external;
  1175. Xfunction GetPat (var arg, pat: StringType): Boolean;
  1176. X    external;
  1177. Xfunction MakePat (var arg: StringType; start: Integer;
  1178. X        delim: CharType; var pat: StringType): Integer;
  1179. X    external;
  1180. Xprocedure DoDash (delim: CharType; var src: StringType;
  1181. X        var i: Integer; var dest: StringType;
  1182. X        var j: Integer; maxSet: Integer);
  1183. X    external;
  1184. Xfunction MakeSet (var inSet: StringType; k: Integer;
  1185. X        var outSet: StringType; maxSet: Integer): Boolean;
  1186. X    external;
  1187. X*COPY SUBDEF
  1188. X{ subdef -- definitions of substitution routines }
  1189. Xconst
  1190. X    DITTO = Chr(255);
  1191. Xprocedure SubLine (var lin, pat, sub: StringType);
  1192. X    external;
  1193. Xprocedure CatSub (var lin: StringType; s1,s2: Integer;
  1194. X        var sub: StringType; var new: StringType;
  1195. X        var k: Integer; maxNew: Integer);
  1196. X    external;
  1197. Xprocedure PutSub(var lin: StringType; s1, s2: Integer;
  1198. X                 var sub: StringType);
  1199. X    external;
  1200. Xfunction MakeSub (var arg: StringType; from: Integer;
  1201. X        delim: CharType; var sub: StringType): Integer;
  1202. X    external;
  1203. Xfunction GetSub (var arg, sub: StringType): Boolean;
  1204. X    external;
  1205. X*COPY DEFVAR
  1206. X{ DefVar -- var declarations for define }
  1207. Xdef
  1208. X    hashTab:    array [1..HASHSIZE] of NDPtr;
  1209. X    NDTable:    CharBuf;
  1210. X    nextTab:    CharPos;        { first free position in NDTable }
  1211. X    buf:        array [1..BUFSIZE] of CharType; { for push back }
  1212. X    bp:         0..BUFSIZE;     { next available character; init = 0 }
  1213. X    defn:   StringType;
  1214. X    token:  StringType;
  1215. X    tokType:    STType;     { type returned by lookup }
  1216. X    defName:    StringType; { value is 'define' }
  1217. X    null:       StringType; { value is '' }
  1218. X*COPY DEFDEF
  1219. X{ DefDef  -- definitions needed for define }
  1220. X{ DefCons -- const declarations for define }
  1221. Xconst
  1222. X    BUFSIZE     = 500;      { size of push back buffer }
  1223. X    MAXCHARS    = 5000;     { size of name-defn table }
  1224. X    MAXDEF      = MAXSTR;   { max chars in a defn }
  1225. X    MAXTOK      = MAXSTR;   { max chars in a token }
  1226. X    HASHSIZE    = 53;       { size of hash table }
  1227. X{ DefType -- type declarations for define }
  1228. Xtype
  1229. X    CharPos     = 1..MAXCHARS;
  1230. X    CharBuf     = array [1..MAXCHARS] of CharType;
  1231. X    STType      = (DEFTYPE, MACTYPE);       { symbol table types }
  1232. X    NDPtr       = -> NDBlock;       { pointer to name-defn block }
  1233. X    NDBlock     =
  1234. X        record
  1235. X            name:       CharPos;
  1236. X            defn:       CharPos;
  1237. X            kind:       STType;
  1238. X            nextPtr:    NDPtr;
  1239. X        end;
  1240. X*COPY DEFPROC
  1241. X{ DefProc -- procedures needed for define }
  1242. Xprocedure CSCopy (var cb: CharBuf; i: CharPos;
  1243. X        var s: StringType);
  1244. X    external;
  1245. Xprocedure SCCopy (var s: StringType; var cb: CharBuf;
  1246. X        i: CharPos);
  1247. X    external;
  1248. Xprocedure PutBack (c: CharType);
  1249. X    external;
  1250. Xfunction GetPBC (var c: CharType): CharType;
  1251. X    external;
  1252. Xprocedure PBStr (var s: StringType);
  1253. X    external;
  1254. Xfunction GetTok (var token: StringType; tokSize: Integer): CharType;
  1255. X    external;
  1256. Xprocedure GetDef (var token: StringType; tokSize: Integer;
  1257. X        var defn: StringType; defSize: Integer);
  1258. X    external;
  1259. Xprocedure InitHash;
  1260. X    external;
  1261. Xfunction Hash (var name: StringType): Integer;
  1262. X    external;
  1263. Xfunction HashFind (var name: StringType): NDPtr;
  1264. X    external;
  1265. Xprocedure Install (var name, defn: StringType; t: STType);
  1266. X    external;
  1267. Xfunction Lookup (var name, defn: StringType; var t: STType): Boolean;
  1268. X    external;
  1269. Xprocedure InitDef;
  1270. X    external;
  1271. X*COPY DEFREF
  1272. Xdef
  1273. X    hashTab:    array [1..HASHSIZE] of NDPtr;
  1274. X    NDTable:    CharBuf;
  1275. X    nextTab:    CharPos;        { first free position in NDTable }
  1276. X    buf:        array [1..BUFSIZE] of CharType; { for push back }
  1277. X    bp:         0..BUFSIZE;     { next available character; init = 0 }
  1278. X    defn:   StringType;
  1279. X    token:  StringType;
  1280. X    tokType:    STType;     { type returned by lookup }
  1281. X    defName:    StringType; { value is 'define' }
  1282. X    null:       StringType; { value is '' }
  1283. X*COPY METADEF
  1284. X{ MetaDef -- definitions for Meta bracket implementation }
  1285. Xconst
  1286. X    BOM = LBRACE;  { start of meta bracket }
  1287. X    EOM = RBRACE;  { end of meta bracket }
  1288. Xtype
  1289. X    MetaIndexType = Integer;
  1290. X    MetaElementType =
  1291. X        record
  1292. X            first: Integer;
  1293. X            last: Integer;
  1294. X        end;
  1295. X    MetaTableType = array [0..9] of MetaElementType;
  1296. X    MetaStackType = array [0..9] of MetaIndexType;
  1297. Xdef
  1298. X    metaIndex: MetaIndexType;
  1299. X    metaTable: MetaTableType;
  1300. X    nullMetaTable: MetaTableType;
  1301. X    metaStack: MetaStackType;
  1302. X    metaStackPointer: Integer;
  1303. X*COPY CHARDEF
  1304. Xconst
  1305. X    ChLetter = 0;
  1306. X    ChLower  = 1;
  1307. X    ChUpper  = 2;
  1308. X    ChDigit  = 3;
  1309. X    ChSpecial = 4;
  1310. Xtype
  1311. X    ChEntry = packed set of 0..7;
  1312. X    ChTable = array [0..255] of ChEntry;
  1313. Xdef
  1314. X    CharTable: ChTable;
  1315. Xfunction CharClass(const tIndex: CharType): ChEntry; external;
  1316. X*COPY MACPROC
  1317. X{ MacProc -- procedures needed for define }
  1318. Xprocedure CSCopy (var cb: CharBuf; i: CharPos;
  1319. X        var s: StringType);
  1320. X    external;
  1321. Xprocedure SCCopy (var s: StringType; var cb: CharBuf;
  1322. X        i: CharPos);
  1323. X    external;
  1324. Xprocedure PutBack (c: CharType);
  1325. X    external;
  1326. Xfunction GetPBC (var c: CharType): CharType;
  1327. X    external;
  1328. Xprocedure PBStr (var s: StringType);
  1329. X    external;
  1330. Xfunction GetTok (var token: StringType; tokSize: Integer): CharType;
  1331. X    external;
  1332. Xprocedure GetDef (var token: StringType; tokSize: Integer;
  1333. X        var defn: StringType; defSize: Integer);
  1334. X    external;
  1335. Xprocedure InitHash;
  1336. X    external;
  1337. Xfunction Hash (var name: StringType): Integer;
  1338. X    external;
  1339. Xfunction HashFind (var name: StringType): NDPtr;
  1340. X    external;
  1341. Xprocedure Install (var name, defn: StringType; t: STType);
  1342. X    external;
  1343. Xfunction Lookup (var name, defn: StringType; var t: STType): Boolean;
  1344. X    external;
  1345. Xprocedure PutTok(var s: StringType);
  1346. X    external;
  1347. Xprocedure PutChr(c: CharType);
  1348. X    external;
  1349. Xprocedure InitMacro;
  1350. X    external;
  1351. Xfunction Push (ep: Integer; var argStk: PosBuf;
  1352. X        ap: Integer): Integer;
  1353. X    external;
  1354. Xprocedure Eval(var argStk: PosBuf; td: StType;
  1355. X        i,j: Integer);
  1356. X    external;
  1357. Xprocedure DoDef (var argStk: PosBuf; i,j: Integer);
  1358. X    external;
  1359. Xprocedure DoIf(var argStk: PosBuf; i,j: Integer);
  1360. X    external;
  1361. Xprocedure DoExpr(var argStk: PosBuf; i,j: Integer);
  1362. X    external;
  1363. Xfunction Expr(var s: StringType; var i: Integer): Integer;
  1364. X    external;
  1365. Xfunction Term(var s: StringType; var i: Integer): Integer;
  1366. X    external;
  1367. Xfunction Factor(var s: StringType; var i: Integer): Integer;
  1368. X    external;
  1369. Xfunction GnbChar(var s: StringType; var i: Integer): CharType;
  1370. X    external;
  1371. Xprocedure DoLen(var argStk: PosBuf; i,j: Integer);
  1372. X    external;
  1373. Xprocedure DoSub(var argStk: PosBuf; i,j: Integer);
  1374. X    external;
  1375. Xprocedure DoChq(var argStk: PosBuf; i,j: Integer);
  1376. X    external;
  1377. Xprocedure PBNum(n: Integer);
  1378. X    external;
  1379. X*COPY MACDEFS
  1380. X{ Macdefs -- all definitions for Macro }
  1381. Xconst
  1382. X    BUFSIZE = 1000;       { size of pushback buffer }
  1383. X    MAXCHARS = 5000;      { size of name-defn table }
  1384. X    MAXPOS = 500;
  1385. X    CALLSIZE = MAXPOS;
  1386. X    ARGSIZE = MAXPOS;
  1387. X    EVALSIZE = MAXCHARS;
  1388. X    MAXDEF = MAXSTR;      { max chars in a defn }
  1389. X    MAXTOK = MAXSTR;      { max length of a token }
  1390. X    HASHSIZE = 53;        { size of hash table }
  1391. X    ARGFLAG = DOLLAR;     { macro invocation character }
  1392. X
  1393. X{ MacType -- type declarations for Macro }
  1394. Xtype
  1395. X    CharPos = 1..MAXCHARS;
  1396. X    CharBuf = packed array [1..MAXCHARS] of CharType;
  1397. X    PosBuf = packed array [1..MAXPOS] of CharPos;
  1398. X    Pos = 0..MAXPOS;
  1399. X    StType = (DEFTYPE, MACTYPE, IFTYPE, SUBTYPE,
  1400. X        EXPRTYPE, LENTYPE, CHQTYPE); { symbol table types }
  1401. X    NdPtr = ->NdBlock;
  1402. X    NdBlock =
  1403. X        record
  1404. X            name: CharPos;
  1405. X            defn: CharPos;
  1406. X            kind: StType;
  1407. X            nextPtr: NdPtr;
  1408. X        end {record};
  1409. X{ Macvar -- def declarations for macro }
  1410. Xdef
  1411. X    traceing: Boolean;
  1412. X    buf: packed array [1..BUFSIZE] of CharType; { for pushback }
  1413. X    bp: 0..BUFSIZE;
  1414. X    hashTab: array [1..HASHSIZE] of NdPtr;
  1415. X    ndTable: CharBuf;
  1416. X    nextTab: CharPos;    { first free position in ndTable }
  1417. X    callStk: PosBuf;
  1418. X    cp: Pos;             { current call stack position }
  1419. X    typeStk: array [1..CALLSIZE] of StType; { type }
  1420. X    pLev: array [1..CALLSIZE] of Integer; { paren level }
  1421. X    argStk: PosBuf;      { argument stack for this call }
  1422. X    ap: Pos;             { current argument position }
  1423. X    evalStk: CharBuf;    { evaluation stack }
  1424. X    ep: CharPos;         { first character unused in evalStk }
  1425. X    { builtins }
  1426. X    defName: StringType; { 'define' }
  1427. X    exprName: StringType;{ 'expr' }
  1428. X    subName: StringType; { 'substr' }
  1429. X    ifName: StringType;  { 'ifelse' }
  1430. X    lenName: StringType; { 'len' }
  1431. X    chqName: StringType; { 'changeq' }
  1432. X    null: StringType;    { value is '' }
  1433. X    lQuote: CharType;    { left quote character }
  1434. X    rQuote: CharType;    { right quote character }
  1435. X
  1436. X    defn: StringType;
  1437. X    token: StringType;
  1438. X    tokType: StType;
  1439. X    t: CharType;
  1440. X    nlPar: Integer;
  1441. /
  1442. echo 'x - toolinit.pascal'
  1443. sed 's/^X//' > toolinit.pascal << '/'
  1444. X{
  1445. X    Copyright (c) 1982
  1446. X    By:    Chris Lewis
  1447. X
  1448. X    Right is hereby granted to freely distribute or duplicate this
  1449. X    software, providing distribution or duplication is not for profit
  1450. X    or other commerical gain and that this copyright notice remains 
  1451. X    intact.
  1452. X}
  1453. X{ ToolInit -- (CMS) standard program prologue }
  1454. Xsegment ToolInit;
  1455. X%include swtools
  1456. X%include iodef
  1457. Xdef openList: array [FileDesc] of IOBlock;
  1458. Xdef cmdLin: StringType;
  1459. Xdef cmdArgs: 0..MAXARG;
  1460. Xdef cmdIdx: array [1..MAXARG] of 1..MAXSTR;
  1461. Xdef termInput: Boolean;
  1462. Xref ERRORIO: Boolean;
  1463. Xvalue
  1464. X    termInput := false;
  1465. Xprocedure ToolInit;
  1466. Xvar
  1467. X    t: 1..MAXSTR;
  1468. X    i: FileDesc;
  1469. X    idx: 1..MAXSTR;
  1470. X    delim: CharType;
  1471. X    PARMSTRING: String(MAXSTR);
  1472. X    fileName: StringType;
  1473. X    cmdLength: 0..MAXSTR;
  1474. X    redirIn: Boolean;
  1475. X    j: 1..MAXSTR;
  1476. X    dummy: StringType;
  1477. X    okay: Boolean;
  1478. X    tempArgs: 0..MAXARG;
  1479. X    XFileName: String(MAXSTR);
  1480. X    k: 0..MAXSTR;
  1481. X    nextChar: 1..MAXSTR;
  1482. Xbegin
  1483. X    TermIn(input);
  1484. X    TermOut(output);
  1485. X    for i := STDIN to MAXOPEN do
  1486. X        openList[i].mode := IOAVAIL;
  1487. X    openList[STDERR].mode := IOWRITE;
  1488. X    TermOut(openList[STDERR].fileVar);
  1489. X    PARMSTRING := PARMS;
  1490. X    if (Length(PARMSTRING) >= 1) and (PARMSTRING[1] = STAR) then begin
  1491. X        WriteLn('Input Command Parameters:');
  1492. X        ReadLn(PARMSTRING);
  1493. X        PARMSTRING := PARMSTRING || SubStr(PARMS, 2, Length(PARMS)-1)
  1494. X    end;
  1495. X    for idx := 1 to Length(PARMSTRING) do
  1496. X        cmdLin[idx] := PARMSTRING[idx];
  1497. X    cmdLin[Length(PARMSTRING) + 1] := NEWLINE;
  1498. X    cmdLin[Length(PARMSTRING) + 2] := ENDSTR;
  1499. X    idx := 1;
  1500. X    cmdArgs := 0;
  1501. X    while ((cmdLin[idx] <> ENDSTR) and
  1502. X      (cmdLin[idx] <> NEWLINE)) do begin
  1503. X        while (cmdLin[idx] = BLANK) do
  1504. X            idx := idx + 1;
  1505. X        if (cmdLin[idx] <> NEWLINE) then begin
  1506. X            delim := BLANK;
  1507. X            cmdArgs := cmdArgs + 1;
  1508. X            if (cmdLin[idx] = SQUOTE) or
  1509. X              (cmdLin[idx] = DQUOTE) then begin
  1510. X                cmdIdx[cmdArgs] := idx + 1;
  1511. X                delim := cmdLin[idx];
  1512. X                idx := idx + 1
  1513. X            end
  1514. X            else
  1515. X                cmdIdx[cmdArgs] := idx;
  1516. X            while ((cmdLin[idx] <> NEWLINE) and
  1517. X              (cmdLin[idx] <> delim)) do
  1518. X                idx := idx + 1;
  1519. X            cmdLin[idx] := ENDSTR;
  1520. X            idx := idx + 1;
  1521. X        end
  1522. X    end;
  1523. X    j := 1;
  1524. X    tempArgs := cmdArgs;
  1525. X    while (j <= cmdArgs) do begin
  1526. X        okay := GetArg(j, dummy, MAXSTR);
  1527. X        j := j + 1;
  1528. X        if (dummy[1] = LESS) or (dummy[1] = GREATER) then begin
  1529. X            if dummy[1] = LESS then
  1530. X                redirIn := true
  1531. X            else
  1532. X                redirIn := false;
  1533. X            SCopy(dummy, 2, fileName, 1);
  1534. X            nextChar := StrLength(fileName) + 1;
  1535. X            tempArgs := tempArgs - 1;
  1536. X            k := j;
  1537. X            while (k <= cmdArgs) do begin
  1538. X                okay := GetArg(k, dummy, MAXSTR);
  1539. X                k := k + 1;
  1540. X                if okay and (dummy[1] <> LESS) and
  1541. X                  (dummy[1]<> GREATER) then begin
  1542. X                    tempArgs := tempArgs - 1;
  1543. X                    fileName[nextChar] := BLANK;
  1544. X                    nextChar := nextChar + 1;
  1545. X                    SCopy(dummy, 1, fileName, nextChar);
  1546. X                    nextChar := StrLength(fileName) + 1;
  1547. X                    j := j + 1;
  1548. X                end
  1549. X                else
  1550. X                    k := cmdArgs + 1;
  1551. X            end;
  1552. X            t := 1;
  1553. X            okay := GetFid(fileName, t, fileName);
  1554. X            if not okay then
  1555. X                Error('Bad redirection file name');
  1556. X            CvtSTS(fileName, XFileName);
  1557. X            if redirIn then begin
  1558. X                 openList[STDIN].mode := IOREAD;
  1559. X                 Reset(openList[STDIN].fileVar, 'NAME=' ||
  1560. X                     XFileName);
  1561. X                 termInput := false;
  1562. X                 if ERRORIO then begin
  1563. X                     openList[STDIN].mode := IOAVAIL;
  1564. X                     Error('Cannot open STDIN file');
  1565. X                     ERRORIO := false
  1566. X                 end
  1567. X            end
  1568. X            else begin
  1569. X                 openList[STDOUT].mode := IOWRITE;
  1570. X                 Remove(fileName);
  1571. X                 ReWrite(openList[STDOUT].fileVar,
  1572. X                     'LRECL=1000,NAME=' || XFileName);
  1573. X                 if ERRORIO then begin
  1574. X                     openList[STDOUT].mode := IOAVAIL;
  1575. X                     ERRORIO := false
  1576. X                 end
  1577. X            end
  1578. X        end
  1579. X    end;
  1580. X    cmdArgs := tempArgs;
  1581. X    if openList[STDIN].mode = IOAVAIL then begin
  1582. X        TermIn(openList[STDIN].fileVar);
  1583. X        openList[STDIN].mode := IOREAD;
  1584. X        termInput := true;
  1585. X    end;
  1586. X    if openList[STDOUT].mode = IOAVAIL then begin
  1587. X        TermOut(openList[STDOUT].fileVar);
  1588. X        openList[STDOUT].mode := IOWRITE;
  1589. X    end;
  1590. Xend;
  1591. /
  1592. echo 'Part 03 of pack.out complete.'
  1593. exit
  1594.  
  1595.  
  1596.