out.c
上传用户:upcnvip
上传日期:2007-01-06
资源大小:474k
文件大小:33k
源码类别:

编译器/解释器

开发平台:

C/C++

  1. /* "p2c", a Pascal to C translator.
  2.    Copyright (C) 1989 David Gillespie.
  3.    Author's address: daveg@csvax.caltech.edu; 256-80 Caltech/Pasadena CA 91125.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation (any version).
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; see the file COPYING.  If not, write to
  13. the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  14. /* This needs to go before trans.h (and thus p2c.proto) is read */
  15. typedef struct S_paren {
  16.     struct S_paren *next;
  17.     int pos, indent, qmindent, flags;
  18. } Paren;
  19. #define PROTO_OUT_C
  20. #include "trans.h"
  21. #ifndef USETIME
  22. # if defined(BSD) || defined(hpux)
  23. #  define USETIME 1
  24. # else
  25. #  define USETIME 0
  26. # endif
  27. #endif
  28. #if USETIME
  29. # include <sys/time.h>
  30. #else
  31. # include <time.h>
  32. #endif
  33. /* Output control characters:
  34.    01  B  Possible break point
  35.    02  X  Break point in parentheses
  36.    03  (  Invisible open paren
  37.    04  )  Invisible close paren
  38.    05  T  Set left margin
  39.    06  F  Forced break point
  40.    07  A  Preceding paren requires all-or-none breaking
  41.    10  [  Invisible open paren, becomes visible if not all on one line
  42.    11  S  Break point after last "special argument" of a function
  43.    12  n  (newline)
  44.    13  E  Preceding break has extra penalty
  45.    14  f  (form-feed)
  46.    15  H  Hang-indent the preceding operator
  47.    16  .  (unused)
  48.    17  C  Break point for last : of a ?: construct
  49. */
  50. char spchars[] = ".BX()TFA[SnEfH.C................";
  51. Static int testinglinebreaker = 0;
  52. Static int deltaindent, thisindent, thisfutureindent;
  53. Static int sectionsize, blanklines, codesectsize, hdrsectsize;
  54. Static int codelnum, hdrlnum;
  55. #define MAXBREAKS  200
  56. Static int numbreaks, bestnumbreaks;
  57. Static double bestbadness;
  58. Static int breakpos[MAXBREAKS], breakindent[MAXBREAKS];
  59. Static int breakcount[MAXBREAKS], breakparen[MAXBREAKS];
  60. Static int bestbreakpos[MAXBREAKS], bestbreakindent[MAXBREAKS];
  61. Static int breakerrorflag;
  62. #define MAXEDITS  200
  63. Static int numedits, bestnumedits;
  64. Static int editpos[MAXEDITS], besteditpos[MAXEDITS];
  65. Static char editold[MAXEDITS], editnew[MAXEDITS];
  66. Static char besteditold[MAXEDITS], besteditnew[MAXEDITS];
  67. Static Paren *parenlist;
  68. Static long numalts, bestnumalts;
  69. Static int randombreaks;
  70. Static char *outbuf;
  71. Static int outbufpos, outbufcount, outbufsize;
  72. Static int suppressnewline, lastlinelength;
  73. Static int eatblanks;
  74. Static int embeddedcode;
  75. Static int showingsourcecode = 0;
  76. #define BIGBADNESS  (1e20)
  77. void setup_out()
  78. {
  79.     end_source();
  80.     fprintf(outf, "/* From input file "%s" */n", infname);
  81.     outf_lnum++;
  82.     hdrlnum = 1;
  83.     outindent = 0;
  84.     deltaindent = 0;
  85.     thisindent = 0;
  86.     thisfutureindent = -1;
  87.     sectionsize = 2;
  88.     blanklines = 0;
  89.     dontbreaklines = 0;
  90.     embeddedcode = 0;
  91.     outputmode = 0;
  92.     suppressnewline = 0;
  93.     eatblanks = 0;
  94.     outbufsize = 1000;
  95.     outbuf = ALLOC(outbufsize, char, misc);
  96.     outbufpos = 0;
  97.     outbufcount = 0;
  98.     srand(17);
  99. }
  100. void select_outfile(fp)
  101. FILE *fp;
  102. {
  103.     if (outf == codef) {
  104.         codesectsize = sectionsize;
  105. codelnum = outf_lnum;
  106.     } else {
  107.         hdrsectsize = sectionsize;
  108. hdrlnum = outf_lnum;
  109.     }
  110.     outf = fp;
  111.     if (outf == codef) {
  112.         sectionsize = codesectsize;
  113. outf_lnum = codelnum;
  114.     } else {
  115.         sectionsize = hdrsectsize;
  116. outf_lnum = hdrlnum;
  117.     }
  118. }
  119. void start_source()
  120. {
  121.     if (!showingsourcecode) {
  122. fprintf(outf, "n#ifdef Pascaln");
  123. showingsourcecode = 1;
  124.     }
  125. }
  126. void end_source()
  127. {
  128.     if (showingsourcecode) {
  129. fprintf(outf, "#endif /*Pascal*/nn");
  130. showingsourcecode = 0;
  131.     }
  132. }
  133. int line_start()
  134. {
  135.     return (outbufcount == 0);
  136. }
  137. int cur_column()
  138. {
  139.     if (outbufpos == 0)
  140. return outindent;
  141.     else
  142. return thisindent + outbufcount;
  143. }
  144. int lookback(n)
  145. int n;
  146. {
  147.     if (n <= 0 || n > outbufpos)
  148. return 0;
  149.     else
  150. return outbuf[outbufpos - n];
  151. }
  152. int lookback_prn(n)
  153. int n;
  154. {
  155.     for (;;) {
  156. if (n <= 0 || n > outbufpos)
  157.     return 0;
  158. else if (outbuf[outbufpos - n] >= ' ')
  159.     return outbuf[outbufpos - n];
  160. else
  161.     n++;
  162.     }
  163. }
  164. /* Combine two indentation adjustments */
  165. int adddeltas(d1, d2)
  166. int d1, d2;
  167. {
  168.     if (d2 >= 1000)
  169. return d2;
  170.     else
  171. return d1 + d2;
  172. }
  173. /* Apply an indentation delta */
  174. int applydelta(i, d)
  175. int i, d;
  176. {
  177.     if (d >= 1000)
  178. return d - 1000;
  179.     else
  180. return i + d;
  181. }
  182. /* Adjust the current indentation by delta */
  183. void moreindent(delta)
  184. int delta;
  185. {
  186.     outindent = applydelta(outindent, delta);
  187. }
  188. /* Adjust indentation for just this line */
  189. void singleindent(delta)
  190. int delta;
  191. {
  192.     deltaindent = adddeltas(deltaindent, delta);
  193. }
  194. /* Predict indentation for next line */
  195. void futureindent(num)
  196. int num;
  197. {
  198.     thisfutureindent = applydelta(applydelta(outindent, deltaindent), num);
  199. }
  200. int parsedelta(cp, def)
  201. char *cp;
  202. int def;
  203. {
  204.     if (!cp || !*cp)
  205. return def;
  206.     if ((*cp == '+' || *cp == '-') && isdigit(cp[1]))
  207. return atoi(cp);
  208.     if (*cp == '*' && isdigit(cp[1]))
  209. return 2000 + atoi(cp+1);
  210.     else
  211. return 1000 + atoi(cp);
  212. }
  213. Static void leading_tab(col)
  214. int col;
  215. {
  216.     if (col > maxlinewidth)
  217. return;    /* something wrong happened! */
  218.     if (phystabsize > 0) {
  219. while (col >= phystabsize) {
  220.     putc('t', outf);
  221.     col -= phystabsize;
  222. }
  223.     }
  224.     while (col > 0) {
  225. putc(' ', outf);
  226. col--;
  227.     }
  228. }
  229. void eatblanklines()
  230. {
  231.     eatblanks = 1;
  232. }
  233. Static void flush_outbuf(numbreaks, breakpos, breakindent,
  234.  numedits, editpos, editold, editnew)
  235. int numbreaks, *breakpos, *breakindent, numedits, *editpos;
  236. char *editold, *editnew;
  237. {
  238.     unsigned char ch, ch2;
  239.     char *cp;
  240.     int i, j, linelen = 0, spaces, hashline;
  241.     int editsaves[MAXEDITS];
  242.     end_source();
  243.     if (outbufcount > 0) {
  244. for (i = 0; i < numedits; i++) {
  245.     editsaves[i] = outbuf[editpos[i]];
  246.     outbuf[editpos[i]] = editnew[i];
  247. }
  248. leading_tab(thisindent);
  249. cp = outbuf;
  250. hashline = (*cp == '#');    /* a preprocessor directive */
  251. spaces = 0;
  252. j = 1;
  253. for (i = 0; i < outbufpos; ) {
  254.     if (j < numbreaks && i == breakpos[j]) {
  255. if (hashline)
  256.     fprintf(outf, " \");   /* trailing backslash required */
  257. putc('n', outf);
  258. outf_lnum++;
  259. leading_tab(breakindent[j]);
  260. linelen = breakindent[j];
  261. j++;
  262. while (i < outbufpos && *cp == ' ')
  263.     i++, cp++;   /* eat leading spaces */
  264. spaces = 0;      /* eat trailing spaces */
  265.     } else {
  266. ch = *cp++;
  267. if (ch == ' ') {
  268.     spaces++;
  269. } else if (ch > ' ') {
  270.     linelen += spaces;
  271.     while (spaces > 0)
  272. putc(' ', outf), spaces--;
  273.     linelen++;
  274.     if (ch == '\' && embeddedcode) {
  275. if (*cp == '[') {
  276.     putc('{', outf);
  277.     cp++, i++;
  278. } else if (*cp == ']') {
  279.     putc('}', outf);
  280.     cp++, i++;
  281. } else
  282.     putc(ch, outf);
  283.     } else
  284. putc(ch, outf);
  285. } else if (testinglinebreaker >= 3) {
  286.     linelen += spaces;
  287.     while (spaces > 0)
  288. putc(' ', outf), spaces--;
  289.     linelen++;
  290.     putc('\', outf);
  291.     ch2 = spchars[ch];
  292.     if (ch2 != '.')
  293. putc(ch2, outf);
  294.     else {
  295. putc('0' + ((ch >> 6) & 7), outf);
  296. putc('0' + ((ch >> 3) & 7), outf);
  297. putc('0' + (ch & 7), outf);
  298.     }
  299. }
  300. i++;
  301.     }
  302. }
  303. for (i = 0; i < numedits; i++)
  304.     outbuf[editpos[i]] = editsaves[i];
  305. eatblanks = 0;
  306.     } else if (eatblanks) {
  307. return;
  308.     }
  309.     if (suppressnewline) {
  310. lastlinelength = linelen;
  311.     } else
  312. putc('n', outf);
  313.     outf_lnum++;
  314. }
  315. #define ISQUOTE(ch)  ((ch)=='"' || (ch)==''')
  316. #define ISOPENP(ch)  ((ch)=='(' || (ch)=='[' || (ch)=='03' || (ch)=='10')
  317. #define ISCLOSEP(ch) ((ch)==')' || (ch)==']' || (ch)=='04')
  318. #define ISBREAK(ch)  ((ch)=='01' || (ch)=='02' || (ch)=='06' || (ch)=='11' || (ch)=='17')
  319. Static int readquotes(posp, err)
  320. int *posp, err;
  321. {
  322.     int pos;
  323.     char quote;
  324.     pos = *posp;
  325.     quote = outbuf[pos++];
  326.     while (pos < outbufpos && outbuf[pos] != quote) {
  327. if (outbuf[pos] == '\')
  328.     pos++;
  329. pos++;
  330.     }
  331.     if (pos >= outbufpos) {
  332. if (err && breakerrorflag) {
  333.     intwarning("output", "Mismatched quotes [248]");
  334.     breakerrorflag = 0;
  335. }
  336. return 0;
  337.     } else {
  338. *posp = pos;
  339. return 1;
  340.     }    
  341. }
  342. Static int maxdepth;
  343. Static int readparens(posp, err)
  344. int *posp, err;
  345. {
  346.     char ch, closing;
  347.     int pos, level;
  348.     pos = *posp;
  349.     switch (outbuf[pos]) {
  350.       case '(':
  351. closing = ')';
  352. break;
  353.       case '[':
  354. closing = ']';
  355. break;
  356.       case '03':
  357.       case '10':
  358. closing = '04';
  359. break;
  360.       default:
  361. closing = 0;
  362. break;
  363.     }
  364.     level = 0;
  365.     for (;;) {
  366. pos++;
  367. if (pos >= outbufpos)
  368.     break;
  369. ch = outbuf[pos];
  370. if (ISOPENP(ch)) {
  371.     level++;
  372.     if (level > maxdepth)
  373. maxdepth = level;
  374. } else if (ISCLOSEP(ch)) {
  375.     level--;
  376.     if (level < 0) {
  377. if (closing && outbuf[pos] != closing)
  378.     break;
  379. *posp = pos;
  380. return 1;
  381.     }
  382. } else if (ISQUOTE(ch)) {
  383.     if (!readquotes(&pos, err))
  384. return 0;
  385. }
  386.     }
  387.     if (err && breakerrorflag) {
  388. switch (closing) {
  389.   case ')':
  390.     intwarning("output", "Mismatched parentheses [249]");
  391.     break;
  392.   case ']':
  393.     intwarning("output", "Mismatched brackets [249]");
  394.     break;
  395.   default:
  396.     intwarning("output", "Mismatched clauses [250]");
  397.     break;
  398. }
  399. breakerrorflag = 0;
  400.     }
  401.     return 0;
  402. }
  403. Static int measurechars(first, last)
  404. int first, last;
  405. {
  406.     int count = 0;
  407.     while (first <= last) {
  408. if (outbuf[first] >= ' ')
  409.     count++;
  410. first++;
  411.     }
  412.     return count;
  413. }
  414. Static void makeedit(pos, ch)
  415. int pos, ch;
  416. {
  417.     editpos[numedits] = pos;
  418.     editold[numedits] = outbuf[pos];
  419.     editnew[numedits] = ch;
  420.     outbuf[pos] = ch;
  421.     numedits++;
  422. }
  423. Static void unedit()
  424. {
  425.     numedits--;
  426.     outbuf[editpos[numedits]] = editold[numedits];
  427. }
  428. Static int parencount(par)
  429. Paren *par;
  430. {
  431.     int count = 0;
  432.     while (par) {
  433. count++;
  434. par = par->next;
  435.     }
  436.     return count;
  437. }
  438. /* The following routine explores the tree of all possible line breaks,
  439.    pruning according to the fact that "badness" and "extra" are
  440.    increasing functions.  The object is to find the set of breaks and
  441.    indentation with the least total badness.
  442.    (The basic idea was borrowed from Donald Knuth's "TeX".)
  443. */
  444. /* As an additional optimization, the concept of a "simple" line is used,
  445.    i.e., a line with a structure such that the best break is sure to be
  446.    the straightforward left-to-right fill used by a simple word processor.
  447.    (For example, a long line with nothing but comma-breakpoints is simple.)
  448.    Also, if the line is very long a few initial random passes are made just
  449.    to scope out an estimate of the eventual badness of the line.  This
  450.    combined with the badness cull helps keep the breaker from using up its
  451.    quota of tries before even considering a key break point!  Note that
  452.    when randombreaks==1, each call to trybreakline is fast since only one
  453.    branch is taken at each decision point.
  454. */
  455. #define randtest(lim)  (!randombreaks ? -1    
  456. : randombreaks > 0    
  457.     ? parencount(parens) < randombreaks-1   
  458. : randombreaks == -2  
  459.     ? 0 
  460. : (rand() & 0xfff) < (lim))
  461. #define TB_BRKCOUNT   0x0ff
  462. #define TB_FORCEBRK   0x100
  463. #define TB_NOBREAK    0x200
  464. #define TB_ALREADYBRK 0x400
  465. #define TB_ALLORNONE  0x800
  466. #define TB_EXTRAIND   0x1000
  467. #define TB_EXTRAIND2  0x2000
  468. #define TBR_ABORT     0x1
  469. #define TBR_SIMPLE    0x2
  470. #define TBR_REACHED   0x4
  471. Static int trybreakline(pos, count, indent, badness, flags, parens)
  472. int pos, count, indent, flags;
  473. double badness;
  474. Paren *parens;
  475. {
  476.     int edited;
  477.     int i, j, jmask, f, pos2, r;
  478.     char ch, ch2, closing;
  479.     double extra, penalty;
  480.     Paren *pp;
  481. #if 0
  482.     { static double save = -1;
  483.       if (showbadlimit != save) printf("Showbadlimit = %gn", showbadlimit);
  484.       save = showbadlimit;
  485.     }
  486. #endif
  487.     if (numalts >= maxalts)
  488. return TBR_ABORT;
  489.     jmask = -1;
  490.     for (;;) {
  491. if (numbreaks >= MAXBREAKS) {   /* must leave rest of line alone */
  492.     count += measurechars(pos, outbufpos-1);
  493.     pos = outbufpos;
  494. }
  495. i = count - breakcount[numbreaks-1] +
  496.     breakindent[numbreaks-1] - linewidth;
  497. if (i <= 0)
  498.     extra = 0;
  499. else {
  500.     if (i + linewidth >= maxlinewidth || randombreaks == -2)
  501. return 0;   /* absolutely too long! */
  502.     extra = overwidepenalty + ((long)i*i)*overwideextrapenalty;
  503.     jmask &= ~TBR_SIMPLE;
  504.     if (extra < 0)
  505. extra = 0;
  506. }
  507. if ((testinglinebreaker > 1 && showbadlimit > 0) ?
  508.     (badness + extra >= showbadlimit) :
  509.     (badness + extra >= bestbadness)) {
  510.     numalts++;
  511.     return 0;   /* no point in going on, badness will only increase */
  512. }
  513. if (pos >= outbufpos)
  514.     break;
  515. if (parens && pos >= parens->pos) {
  516.     indent = parens->indent;
  517.     flags = parens->flags;
  518.     parens = parens->next;
  519. }
  520. ch = outbuf[pos++];
  521. if (ch >= ' ')
  522.     count++;
  523. switch (ch) {
  524.   case '(':
  525.   case '[':
  526.   case '03':     /* "invisible open paren" */
  527.   case '10':     /* "semi-invisible open paren" */
  528.     pos2 = pos - 1;
  529.     if (!readparens(&pos2, 1))
  530. break;
  531.     i = measurechars(pos, pos2);
  532.     if (count + i - breakcount[numbreaks-1] +
  533. breakindent[numbreaks-1] <= linewidth) {
  534. /* it fits, so leave it on one line */
  535. #if 0  /* I don't think this is necessary */
  536. while (pos <= pos2) {
  537.     if (outbuf[pos] == '02') {
  538. jmask &= ~TBR_SIMPLE;
  539. pos = pos2 + 1;
  540. break;
  541.     }
  542.     pos++;
  543. }
  544. #else
  545. pos = pos2 + 1;
  546. #endif
  547. count += i;
  548. break;
  549.     }
  550.     pp = ALLOC(1, Paren, parens);   /* doesn't fit, try poss breaks */
  551.     pp->next = parens;
  552.     pp->pos = pos2;
  553.     pp->indent = indent;
  554.     pp->qmindent = indent;
  555.     pp->flags = flags;
  556.     parens = pp;
  557.     flags = 0;
  558.     if (ch == '10' &&       /* change to real parens when broken */
  559. numedits+1 < MAXEDITS) {    /* (assume it will be broken!) */
  560. makeedit(pos-1, '(');
  561. makeedit(pos2, ')');
  562. count++;    /* count the new open paren */
  563. edited = 1;
  564.     } else
  565. edited = 0;
  566.     i = breakindent[numbreaks-1] + count - breakcount[numbreaks-1];
  567.     if (i <= thisindent)
  568. r = 0;  /* e.g., don't break top-level assignments */
  569.     else if (i == indent + extraindent)
  570. r = 1;  /* don't waste time on identical operations */
  571.     else
  572. r = randtest(0xc00);
  573.     if (r != 0) {
  574. j = trybreakline(pos, count, i,
  575.  badness + MAX(- extraindentpenalty,0),
  576.  flags, parens);
  577.     } else
  578. j = 0;
  579.     if (r != 1) {
  580. j &= trybreakline(pos, count, indent + extraindent,
  581.   badness + MAX(extraindentpenalty,0),
  582.   flags | TB_EXTRAIND, parens);
  583.     }
  584.     if (!randombreaks && bumpindent != 0) {
  585. if (i == thisfutureindent) {
  586.     j &= trybreakline(pos, count, i + bumpindent,
  587.       badness + MAX(- extraindentpenalty,0)
  588.               + bumpindentpenalty,
  589.       flags, parens);
  590. } else if (indent + extraindent == thisfutureindent) {
  591.     j &= trybreakline(pos, count,
  592.       indent + extraindent + bumpindent,
  593.       badness + MAX(extraindentpenalty,0)
  594.               + bumpindentpenalty,
  595.       flags | TB_EXTRAIND, parens);
  596. }
  597.     }
  598.     if (edited) {
  599. unedit();
  600. unedit();
  601.     }
  602.     FREE(pp);
  603.     return j & jmask;
  604.   case '05':   /* "set left margin" */
  605.     indent = breakindent[numbreaks-1] +
  606.      count - breakcount[numbreaks-1];
  607.     break;
  608.   case '07':   /* "all-or-none breaking" */
  609.     flags |= TB_ALLORNONE;
  610.     break;
  611.   case '01':   /* "possible break point" */
  612.   case '02':   /* "break point in parens" */
  613.   case '06':   /* "forced break point" */
  614.   case '11':   /* "break point after special args" */
  615.   case '17':   /* "break point for final : operator" */
  616.     /* first try the non-breaking case */
  617.     if (ch != '01' && ch != '06')
  618. jmask &= ~TBR_SIMPLE;
  619.     if ((flags & TB_BRKCOUNT) != TB_BRKCOUNT)
  620. flags++;   /* increment TB_BRKCOUNT field */
  621.     if (outbuf[pos] == '?' && parens)
  622. parens->qmindent = breakindent[numbreaks-1] +
  623.                    count - breakcount[numbreaks-1];
  624.     j = TBR_REACHED;
  625.     if (ch == '06' || (flags & TB_FORCEBRK)) {
  626. /* don't try the non-breaking case */
  627.     } else {
  628. if (ch == '11') {
  629.     i = breakindent[numbreaks-1] +
  630. count - breakcount[numbreaks-1] + 2;
  631. } else {
  632.     i = indent;
  633. }
  634. f = flags;
  635. if (f & TB_ALLORNONE)
  636.     f |= TB_NOBREAK;
  637. r = randtest(0x800);
  638. if (r != 1 || (flags & TB_NOBREAK)) {
  639.     j = trybreakline(pos, count, i, badness, f, parens) &
  640. jmask;
  641.     if (randombreaks == -2 && !(j & TBR_REACHED)) {
  642. r = -1;
  643. j |= TBR_REACHED;
  644.     }
  645.     if (r == 0 || (j & TBR_SIMPLE))
  646. flags |= TB_NOBREAK;
  647. }
  648.     }
  649.     if (flags & TB_NOBREAK)
  650. return j;
  651.     if (flags & TB_ALLORNONE)
  652. flags |= TB_FORCEBRK;
  653.     if (flags & TB_EXTRAIND) {
  654. flags &= ~TB_EXTRAIND;
  655. flags |= TB_EXTRAIND2;
  656.     }
  657.     /* now try breaking here */
  658.     if (ch == '17')
  659. indent = parens->qmindent;
  660.     if (indent < 0)
  661. indent = 0;
  662.     breakpos[numbreaks] = pos;
  663.     breakcount[numbreaks] = count;
  664.     breakindent[numbreaks] = indent;
  665.     breakparen[numbreaks] = parens ? parens->pos : 0;
  666.     numbreaks++;
  667.     penalty = extra;
  668.     if (indent == thisfutureindent) {
  669. i = pos;
  670. while (i < outbufpos-1 && outbuf[i] <= ' ')
  671.     i++;
  672. ch2 = outbuf[i];   /* first character on next line */
  673. if (ch2 != '(' && ch2 != '!' && ch2 != '~' && ch2 != '-')
  674.     penalty += nobumpindentpenalty;
  675.     }
  676.     switch (ch) {
  677.       case '01':
  678. penalty += commabreakpenalty;
  679. if (flags & TB_ALREADYBRK)
  680.     penalty += morebreakpenalty;
  681. break;
  682.       case '11':
  683. i = parencount(parens);
  684. penalty += specialargbreakpenalty + commabreakextrapenalty*i;
  685. break;
  686.       case '02':
  687.       case '17':
  688. i = parencount(parens);
  689. if (outbuf[pos-2] == '(')
  690.     penalty += parenbreakpenalty + parenbreakextrapenalty*i;
  691. else if (outbuf[pos-2] == ',')
  692.     penalty += commabreakpenalty + commabreakextrapenalty*i;
  693. else if (outbuf[pos-2] == '=')
  694.     penalty += assignbreakpenalty + assignbreakextrapenalty*i;
  695. else if (outbuf[pos] == '?') {
  696.     penalty += qmarkbreakpenalty + qmarkbreakextrapenalty*i;
  697.     if (parens)
  698. parens->qmindent = breakindent[numbreaks-1] +
  699.                    count - breakcount[numbreaks-1];
  700. } else
  701.     penalty += opbreakpenalty + opbreakextrapenalty*i;
  702. if (outbuf[pos-2] == '-')
  703.     penalty += exhyphenpenalty;
  704. if (flags & TB_ALREADYBRK)
  705.     penalty += morebreakpenalty + morebreakextrapenalty*i;
  706. break;
  707.       default:
  708. break;
  709.     }
  710.     while (pos < outbufpos && outbuf[pos] == '13') {
  711. penalty += wrongsidepenalty;
  712. pos++;
  713.     }
  714.     penalty -= earlybreakpenalty*(flags & TB_BRKCOUNT);
  715.     /* the following test is not quite right, but it's not too bad. */
  716.     if (breakindent[numbreaks-2] == breakindent[numbreaks-1] &&
  717. breakparen[numbreaks-2] != breakparen[numbreaks-1])
  718. penalty += sameindentpenalty;
  719. #if 0
  720.     else if (ch == '02' && parens &&  /*don't think this is needed*/
  721.      parens->indent == breakindent[numbreaks-1] &&
  722.      parens->pos != breakparen[numbreaks-1])
  723. penalty += sameindentpenalty + 0.001;   /***/
  724. #endif
  725.     penalty += (breakindent[numbreaks-1] - thisindent) *
  726.        indentamountpenalty;
  727.     if (penalty < 1) penalty = 1;
  728.     pos2 = pos;
  729.     while (pos2 < outbufpos && outbuf[pos2] == ' ')
  730. pos2++;
  731.     flags |= TB_ALREADYBRK;
  732.     j = trybreakline(pos2, count, indent, badness + penalty,
  733.      flags, parens) & jmask;
  734.     numbreaks--;
  735.     return j;
  736.     
  737.   case '15':    /* "hang-indent operator" */
  738.     if (count <= breakcount[numbreaks-1] + 2 &&
  739. !(flags & TB_EXTRAIND2)) {
  740. breakindent[numbreaks-1] -= count - breakcount[numbreaks-1];
  741. pos2 = pos;
  742. while (pos2 < outbufpos && outbuf[pos2] <= ' ') {
  743.     if (outbuf[pos2] == ' ')
  744. breakindent[numbreaks-1]--;
  745.     pos2++;
  746. }
  747.     }
  748.     break;
  749.   case '"':
  750.   case ''':
  751.     closing = ch;
  752.     while (pos < outbufpos && outbuf[pos] != closing) {
  753. if (outbuf[pos] == '\')
  754.     pos++, count++;
  755. pos++;
  756. count++;
  757.     }
  758.     if (pos >= outbufpos) {
  759. intwarning("output", "Mismatched quotes [248]");
  760. continue;
  761.     }
  762.     pos++;
  763.     count++;
  764.     break;
  765.   case '/':
  766.     if (pos < outbufpos && (outbuf[pos] == '*' ||
  767.     (outbuf[pos] == '/' && cplus > 0))) {
  768. count += measurechars(pos, outbufpos-1);
  769. pos = outbufpos;   /* assume comment is at end of line */
  770.     }
  771.     break;
  772. }
  773.     }
  774.     numalts++;
  775.     badness += extra;
  776.     if (testinglinebreaker > 1) {
  777. if (badness >= bestbadness &&
  778.     (badness < showbadlimit || showbadlimit == 0)) {
  779.     fprintf(outf, "n#if 0   /* rejected #%ld, badness = %g >= %g */n", numalts, badness, bestbadness);
  780.     flush_outbuf(numbreaks, breakpos, breakindent,
  781.  numedits, editpos, editold, editnew);
  782.     fprintf(outf, "#endifn");
  783.     return TBR_SIMPLE & jmask;
  784. } else if ((bestbadness < showbadlimit || showbadlimit == 0) &&
  785.    bestnumalts > 0) {
  786.     fprintf(outf, "n#if 0   /* rejected #%ld, badness = %g > %g */n", bestnumalts, bestbadness, badness);
  787.     flush_outbuf(bestnumbreaks, bestbreakpos, bestbreakindent,
  788.  bestnumedits, besteditpos,
  789.  besteditold, besteditnew);
  790.     fprintf(outf, "#endifn");
  791. }
  792.     }
  793.     bestbadness = badness;
  794.     bestnumbreaks = numbreaks;
  795.     bestnumalts = numalts;
  796.     for (i = 0; i < numbreaks; i++) {
  797. bestbreakpos[i] = breakpos[i];
  798. bestbreakindent[i] = breakindent[i];
  799.     }
  800.     bestnumedits = numedits;
  801.     for (i = 0; i < numedits; i++) {
  802. besteditpos[i] = editpos[i];
  803. besteditold[i] = editold[i];
  804. besteditnew[i] = editnew[i];
  805.     }
  806.     return TBR_SIMPLE & jmask;
  807. }
  808. int parse_breakstr(cp)
  809. char *cp;
  810. {
  811.     short val = 0;
  812.     if (isdigit(*cp))
  813. return atoi(cp);
  814.     while (*cp && !isspace(*cp) && *cp != '}') {
  815. switch (toupper(*cp++)) {
  816.   case 'N':
  817.   case '=':
  818.     break;
  819.   case 'L':
  820.     val |= BRK_LEFT;
  821.     break;
  822.   case 'R':
  823.     val |= BRK_RIGHT;
  824.     break;
  825.   case 'H':
  826.     val |= BRK_HANG | BRK_LEFT;
  827.     break;
  828.   case '>':
  829.     if (val & BRK_LEFT)
  830. val |= BRK_LPREF;
  831.     else if (val & BRK_RIGHT)
  832. val |= BRK_RPREF;
  833.     else
  834. return -1;
  835.     break;
  836.   case '<':
  837.     if (val & BRK_LEFT)
  838. val |= BRK_RPREF;
  839.     else if (val & BRK_RIGHT)
  840. val |= BRK_LPREF;
  841.     else
  842. return -1;
  843.     break;
  844.   case 'A':
  845.     val |= BRK_ALLNONE;
  846.     break;
  847.   default:
  848.     return -1;
  849. }
  850.     }
  851.     return val;
  852. }
  853. long getcurtime()
  854. {
  855. #if USETIME
  856.     static unsigned long starttime = 0;
  857.     struct timeval t;
  858.     struct timezone tz;
  859.     gettimeofday(&t, &tz);
  860.     if (starttime == 0)
  861. starttime = t.tv_sec;
  862.     t.tv_sec -= starttime;
  863.     return (t.tv_sec*1000 + t.tv_usec/1000);
  864. #else
  865.     static unsigned long starttime = 0;
  866.     if (!starttime) starttime = time(NULL);
  867.     return (time(NULL) - starttime) * 1000;
  868. #endif
  869. }
  870. void output(msg)
  871. register char *msg;
  872. {
  873.     unsigned char ch;
  874.     double savelimit;
  875.     int i, savemaxlw, maxdp;
  876.     long alts;
  877.     long time0, time0a, time1;
  878.     debughook();
  879.     if (outputmode) {
  880. end_source();
  881. while ((ch = *msg++) != 0) {
  882.     if (ch >= ' ') {
  883. putc(ch, outf);
  884.     } else if (ch == 'n') {
  885. putc('n', outf);
  886. outf_lnum++;
  887.     }
  888. }
  889. return;
  890.     }
  891.     while ((ch = *msg++) != 0) {
  892. if (ch == 'n') {
  893.     if (outbufpos == 0) {      /* blank line */
  894. thisfutureindent = -1;
  895. blanklines++;
  896. continue;
  897.     }
  898.     if (sectionsize > blanklines)
  899. blanklines = sectionsize;
  900.     sectionsize = 0;
  901.     if (eatblanks)
  902. blanklines = 0;
  903.             while (blanklines > 0) {
  904.                 blanklines--;
  905. end_source();
  906.                 putc('n', outf);
  907. outf_lnum++;
  908.             }
  909.     if (thisindent + outbufcount >= linewidth && !dontbreaklines) {
  910. numbreaks = 1;
  911. bestnumbreaks = 0;
  912. bestbadness = BIGBADNESS;
  913. breakpos[0] = 0;
  914. breakindent[0] = thisindent;
  915. breakcount[0] = 0;
  916. breakerrorflag = 1;
  917. numedits = 0;
  918. bestnumedits = 0;
  919. savelimit = showbadlimit;
  920. numalts = 0;
  921. bestnumalts = 0;
  922. savemaxlw = maxlinewidth;
  923. time0 = time0a = getcurtime();
  924. if (regression)
  925.     srand(17);
  926. if (thisindent + outbufcount > linewidth*3/2) {
  927.     i = 0;
  928.     maxdepth = 0;
  929.     readparens(&i, 0);
  930.     maxdp = maxdepth;
  931.     for (;;) {    /* try some simple fixed methods first... */
  932. for (i = 1; i <= 20; i++) {
  933.     randombreaks = -1;
  934.     trybreakline(0, 0, thisindent, 0.0, 0, NULL);
  935. }
  936. randombreaks = -2;
  937. trybreakline(0, 0, thisindent, 0.0, 0, NULL);
  938. for (i = 0; i <= maxdp+1; i++) {
  939.     randombreaks = i+1;
  940.     trybreakline(0, 0, thisindent, 0.0, 0, NULL);
  941. }
  942. if (bestbadness == BIGBADNESS && maxlinewidth < 9999) {
  943.     maxlinewidth = 9999;   /* no choice but to relax */
  944.     numalts = 0;
  945. } else
  946.     break;
  947.     }
  948.     time0a = getcurtime();
  949. }
  950. randombreaks = 0;
  951. trybreakline(0, 0, thisindent, 0.0, 0, NULL);
  952. if (bestbadness == BIGBADNESS && maxlinewidth < 9999) {
  953.     numalts = 0;
  954.     maxlinewidth = 9999;   /* no choice but to relax this */
  955.     trybreakline(0, 0, thisindent, 0.0, 0, NULL);
  956. }
  957. time1 = getcurtime() - time0;
  958. alts = numalts;
  959. if (testinglinebreaker) {
  960.     if (savelimit < 0 && testinglinebreaker > 1) {
  961. showbadlimit = bestbadness * (-savelimit);
  962. numalts = 0;
  963. bestnumalts = 0;
  964. trybreakline(0, 0, thisindent, 0.0, 0, NULL);
  965.     }
  966.     fprintf(outf, "n#if 1   /* accepted #%ld, badness = %g, tried %ld in %.3f sec */n", bestnumalts, bestbadness, alts, time1/1000.0);
  967. }
  968. showbadlimit = savelimit;
  969. maxlinewidth = savemaxlw;
  970. flush_outbuf(bestnumbreaks, bestbreakpos, bestbreakindent,
  971.      bestnumedits, besteditpos,
  972.      besteditold, besteditnew);
  973. if (((USETIME && time1 > 1000) || alts >= maxalts) &&
  974.     !regression) {
  975.     sprintf(outbuf, "Line breaker spent %.1f",
  976.     (time1 + time0 - time0a) / 1000.0);
  977.     if (time0 != time0a)
  978. sprintf(outbuf + strlen(outbuf),
  979. "+%.2f", (time0a - time0) / 1000.0);
  980.     sprintf(outbuf + strlen(outbuf),
  981.     " seconds, %ld tries on line %d [251]", alts, outf_lnum);
  982.     note(outbuf);
  983. } else if (verbose) {
  984.     fprintf(logf, "%s, %d/%d: Line breaker spent %ld triesn",
  985.     infname, inf_lnum, outf_lnum, alts);
  986. }
  987. if (testinglinebreaker)
  988.     fprintf(outf, "#endifnn");
  989.     } else {
  990. if (testinglinebreaker < 2)
  991.     flush_outbuf(0, NULL, NULL, 0, NULL, NULL, NULL);
  992.     }
  993.     thisfutureindent = -1;
  994.     outbufpos = 0;
  995.     outbufcount = 0;
  996. } else {
  997.     if (outbufpos == 0) {
  998. if (ch == ' ' && !dontbreaklines)    /* eat leading spaces */
  999.     continue;
  1000. thisindent = applydelta(outindent, deltaindent);
  1001. deltaindent = 0;
  1002.     }
  1003.     if (outbufpos == outbufsize) {
  1004. outbufsize *= 2;
  1005. outbuf = REALLOC(outbuf, outbufsize, char);
  1006.     }
  1007.     outbuf[outbufpos++] = ch;
  1008.     if (ch >= ' ')
  1009. outbufcount++;
  1010. }
  1011.     }
  1012. }
  1013. void out_n_spaces(n)
  1014. int n;
  1015. {
  1016.     while (--n >= 0)
  1017. output(" ");
  1018. }
  1019. void out_spaces(spc, over, len, delta)
  1020. int spc, over, len, delta;
  1021. {
  1022.     int n;
  1023.     if (spc == -999)
  1024. spc = commentindent;
  1025.     if (spc < 0) {               /* right-justify */
  1026. n = (-spc) - cur_column() - len;
  1027. if (n < minspcthresh)
  1028.     n = minspacing;
  1029. else
  1030.     over = 1000;
  1031.     } else if (spc >= 2000) {    /* tab to multiple */
  1032. spc -= 2000;
  1033. n = (spc-1) - ((cur_column()+spc-1) % spc);
  1034. if (n < minspcthresh)
  1035.     n += spc;
  1036.     } else if (spc >= 1000) {    /* absolute column */
  1037. spc -= 1000;
  1038. n = spc - cur_column();
  1039. if (n < minspcthresh)
  1040.     n = minspacing;
  1041.     } else                       /* relative spacing */
  1042. n = spc;
  1043.     if (line_start()) {
  1044. singleindent(n);
  1045.     } else if (len > 0 && over != 1000 && cur_column() + n + len > linewidth) {
  1046. output("n");
  1047. out_spaces(over, 1000, len, 0);
  1048. singleindent(delta);
  1049.     } else {
  1050. out_n_spaces(n);
  1051.     }
  1052. }
  1053. void testlinebreaker(lev, fn)
  1054. int lev;
  1055. char *fn;
  1056. {
  1057.     char buf[256], *bp, *cp;
  1058.     int first, indent;
  1059.     testinglinebreaker = lev;
  1060.     if (!fn)
  1061. return;
  1062.     inf = fopen(fn, "r");
  1063.     if (!inf) {
  1064. perror(fn);
  1065. exit(1);
  1066.     }
  1067.     sprintf(buf, "%s.br", fn);
  1068.     outf = fopen(buf, "w");
  1069.     if (!outf) {
  1070. perror(buf);
  1071. exit(1);
  1072.     }
  1073.     setup_out();
  1074.     outindent = 4;
  1075.     first = 1;
  1076.     while (fgets(buf, 256, inf)) {
  1077. cp = buf + strlen(buf) - 2;
  1078. if (cp >= buf) {
  1079.     bp = buf;
  1080.     indent = 0;
  1081.     while (isspace(*bp))
  1082. if (*bp++ == 't')
  1083.     indent += 8;
  1084. else
  1085.     indent++;
  1086.     if (first) {
  1087. first = 0;
  1088. outindent = indent;
  1089.     }
  1090.     if (!(*cp == '{' ||
  1091.   *cp == ')' ||
  1092.   *cp == ';') ||
  1093.   (*cp == '/' && cp[-1] == '*')) {
  1094. cp[1] = '01';   /* eat the n */
  1095.     } else {
  1096. first = 1;
  1097.     }
  1098.     output(bp);
  1099. }
  1100.     }
  1101.     fclose(outf);
  1102.     fclose(inf);
  1103. }
  1104. void outsection(size)
  1105. int size;
  1106. {
  1107.     if (size > sectionsize)
  1108.         sectionsize = size;
  1109. }
  1110. Strlist *outcomments(cmt)
  1111. Strlist *cmt;
  1112. {
  1113.     char *cp;
  1114.     int saveindent = outindent, savesingle = deltaindent, theindent;
  1115.     int i = 0;
  1116.     if (!cmt)
  1117. return NULL;
  1118.     if (!commentvisible(cmt)) {
  1119. setcommentkind(cmt, CMT_DONE);
  1120. return cmt->next;
  1121.     }
  1122.     if (*cmt->s == '01') {
  1123. if (cmtdebug)
  1124.     output(format_sd("[]  [%s:%d]",
  1125.      CMT_NAMES[getcommentkind(cmt)],
  1126.      cmt->value & CMT_MASK));
  1127. for (cp = cmt->s; *cp; cp++) {
  1128.     output("n");
  1129.     if (cmtdebug && cp[1])
  1130. output("[]");
  1131. }
  1132. setcommentkind(cmt, CMT_DONE);
  1133. return cmt->next;
  1134.     }
  1135.     dontbreaklines++;
  1136.     if (!strcmp(cmt->s, embedcomment) && *embedcomment && cmt->next &&
  1137. (*cmt->next->s == '02' || *cmt->next->s == '03')) {
  1138. cmt = cmt->next;
  1139. embeddedcode = 1;
  1140. theindent = 0;
  1141. cp = cmt->next->s + 1;
  1142. while (*cp++ == ' ')
  1143.     theindent++;
  1144.     } else {
  1145. moreindent(deltaindent);
  1146. if (cmt->s[0] == '04')
  1147.     outindent = 0;
  1148. theindent = outindent;
  1149. deltaindent = 0;
  1150. output("/*");
  1151.     }
  1152.     cp = cmt->s;
  1153.     for (;;) {
  1154. if (*cp == '02' || *cp == '03' || *cp == '04')
  1155.     cp++;
  1156. if (embeddedcode) {
  1157.     for (i = 0; *cp == ' ' && i < theindent; i++)
  1158. cp++;
  1159.     i = *cp;
  1160. }
  1161. output(cp);
  1162. if (cmtdebug)
  1163.     output(format_sd(" [%s:%d] ",
  1164.      CMT_NAMES[getcommentkind(cmt)],
  1165.      cmt->value & CMT_MASK));
  1166. setcommentkind(cmt, CMT_DONE);
  1167. cmt = cmt->next;
  1168. if (!cmt || !commentvisible(cmt))
  1169.     break;
  1170. cp = cmt->s;
  1171. if (*cp != '02' && *cp != '03')
  1172.     break;
  1173. output("n");
  1174. if (!embeddedcode) {
  1175.     outindent = (*cp == '02') ? theindent : 0;
  1176.     deltaindent = 0;
  1177. }
  1178.     }
  1179.     if (embeddedcode) {
  1180. embeddedcode = 0;
  1181. if (i) {   /* eat final blank line */
  1182.     output("n");
  1183. }
  1184.     } else {
  1185. outindent = saveindent;
  1186. deltaindent = savesingle;
  1187. output("*/n");
  1188.     }
  1189.     dontbreaklines--;
  1190.     return cmt;
  1191. }
  1192. void outcomment(cmt)
  1193. Strlist *cmt;
  1194. {
  1195.     Strlist *savenext;
  1196.     
  1197.     if (cmt) {
  1198. savenext = cmt->next;
  1199. cmt->next = NULL;
  1200. outcomments(cmt);
  1201. cmt->next = savenext;
  1202.     }
  1203. }
  1204. void outtrailcomment(cmt, serial, indent)
  1205. Strlist *cmt;
  1206. int serial, indent;
  1207. {
  1208.     int savedelta = deltaindent;
  1209. #if 0
  1210.     suppressnewline = 1;
  1211.     output("n");
  1212.     suppressnewline = 0;
  1213. #endif
  1214.     cmt = findcomment(cmt, CMT_TRAIL, serial);
  1215.     if (commentvisible(cmt)) {
  1216. out_spaces(indent, commentoverindent, commentlen(cmt), 0);
  1217. outcomment(cmt);
  1218. deltaindent = savedelta;
  1219.     } else
  1220. output("n");
  1221. }
  1222. void flushcomments(cmt, kind, serial)
  1223. Strlist **cmt;
  1224. int kind, serial;
  1225. {
  1226.     Strlist *cmt2, *cmt3;
  1227.     int saveindent, savesingle, saveeat;
  1228.     if (!cmt)
  1229. cmt = &curcomments;
  1230.     cmt2 = extractcomment(cmt, kind, serial);
  1231.     saveindent = outindent;
  1232.     savesingle = deltaindent;
  1233.     moreindent(deltaindent);
  1234.     deltaindent = 0;
  1235.     saveeat = eatcomments;
  1236.     if (eatcomments == 2)
  1237. eatcomments = 0;
  1238.     cmt3 = cmt2;
  1239.     while (cmt3)
  1240. cmt3 = outcomments(cmt3);
  1241.     eatcomments = saveeat;
  1242.     outindent = saveindent;
  1243.     deltaindent = savesingle;
  1244.     strlist_empty(&cmt2);
  1245. }
  1246. char *rawCstring(fmt, s, len, special)
  1247. char *fmt;
  1248. register char *s;
  1249. int len, special;
  1250. {
  1251.     char buf[500];
  1252.     register char *cp;
  1253.     register unsigned char ch;
  1254.     cp = buf;
  1255.     while (--len >= 0) {
  1256.         ch = *((unsigned char *) s);
  1257.         s++;
  1258.         if (ch == 0 && (len == 0 || !isdigit(*s))) {
  1259.             *cp++ = '\';
  1260.             *cp++ = '0';
  1261.         } else if (ch == 'n') {
  1262.             *cp++ = '\';
  1263.             *cp++ = 'n';
  1264.         } else if (ch == 'b') {
  1265.             *cp++ = '\';
  1266.             *cp++ = 'b';
  1267.         } else if (ch == 't') {
  1268.             *cp++ = '\';
  1269.             *cp++ = 't';
  1270.         } else if (ch == 'f') {
  1271.             *cp++ = '\';
  1272.             *cp++ = 'f';
  1273. #if 0
  1274.         } else if (ch == 'r') {
  1275.             *cp++ = '\';
  1276.             *cp++ = 'r';
  1277. #endif
  1278.         } else if (ch < ' ' || ch >= 127) {
  1279.             *cp++ = '\';
  1280.             *cp++ = '0' + (ch>>6);
  1281.             *cp++ = '0' + ((ch>>3) & 7);
  1282.             *cp++ = '0' + (ch & 7);
  1283.         } else if (ch == special) {
  1284.             switch (ch) {
  1285.                 case '%':
  1286.                     *cp++ = ch;
  1287.                     *cp++ = ch;
  1288.                     break;
  1289.             }
  1290.         } else {
  1291.             if (ch == '"' || ch == '\')
  1292.                 *cp++ = '\';
  1293.             *cp++ = ch;
  1294.         }
  1295.     }
  1296.     *cp = 0;
  1297.     return format_s(fmt, buf);
  1298. }
  1299. char *makeCstring(s, len)
  1300. register char *s;
  1301. int len;
  1302. {
  1303.     return rawCstring(""%s"", s, len, 0);
  1304. }
  1305. char *makeCchar(ich)
  1306. int ich;
  1307. {
  1308.     char buf[500];
  1309.     register char *cp;
  1310.     register unsigned char ch = (ich & 0xff);
  1311.     if (ich < 0 || ich > 255 || (ich == 0 && !nullcharconst))
  1312.         return format_d("%d", ich);
  1313.     cp = buf;
  1314.     if (ch == 0) {
  1315.         *cp++ = '\';
  1316.         *cp++ = '0';
  1317.     } else if (ch == 'n') {
  1318.         *cp++ = '\';
  1319.         *cp++ = 'n';
  1320.     } else if (ch == 'b') {
  1321.         *cp++ = '\';
  1322.         *cp++ = 'b';
  1323.     } else if (ch == 't') {
  1324.         *cp++ = '\';
  1325.         *cp++ = 't';
  1326.     } else if (ch == 'f') {
  1327.         *cp++ = '\';
  1328.         *cp++ = 'f';
  1329. #if 0
  1330.     } else if (ch == 'r') {
  1331.         *cp++ = '\';
  1332.         *cp++ = 'r';
  1333. #endif
  1334.     } else if (ch < ' ' || ch >= 127) {
  1335.         *cp++ = '\';
  1336.         *cp++ = '0' + (ch>>6);
  1337.         *cp++ = '0' + ((ch>>3) & 7);
  1338.         *cp++ = '0' + (ch & 7);
  1339.     } else {
  1340.         if (ch == ''' || ch == '\')
  1341.             *cp++ = '\';
  1342.         *cp++ = ch;
  1343.     }
  1344.     *cp = 0;
  1345.     return format_s("'%s'", buf);
  1346. }
  1347. /* End. */