📄 out.c
字号:
if (outbufpos == 0) { /* blank line */ thisfutureindent = -1; blanklines++; continue; } if (sectionsize > blanklines) blanklines = sectionsize; sectionsize = 0; if (eatblanks) blanklines = 0; while (blanklines > 0) { blanklines--; end_source(); putc_outf('\n'); flush_outfilebuf(); outf_lnum++; } if (thisindent + outbufcount >= linewidth && !dontbreaklines) { numbreaks = 1; bestnumbreaks = 0; bestbadness = BIGBADNESS; breakpos[0] = 0; breakindent[0] = thisindent; breakcount[0] = 0; breakerrorflag = 1; numedits = 0; bestnumedits = 0; savelimit = showbadlimit; numalts = 0; bestnumalts = 0; savemaxlw = maxlinewidth; time0 = time0a = getcurtime(); if (regression) srand(17); if (thisindent + outbufcount > linewidth*3/2) { i = 0; maxdepth = 0; readparens(&i, 0); maxdp = maxdepth; for (;;) { /* try some simple fixed methods first... */ for (i = 1; i <= 20; i++) { randombreaks = -1; trybreakline(0, 0, thisindent, 0.0, 0, NULL); } randombreaks = -2; trybreakline(0, 0, thisindent, 0.0, 0, NULL); for (i = 0; i <= maxdp+1; i++) { randombreaks = i+1; trybreakline(0, 0, thisindent, 0.0, 0, NULL); } if (bestbadness == BIGBADNESS && maxlinewidth < 9999) { maxlinewidth = 9999; /* no choice but to relax */ numalts = 0; } else break; } time0a = getcurtime(); } randombreaks = 0; trybreakline(0, 0, thisindent, 0.0, 0, NULL); if (bestbadness == BIGBADNESS && maxlinewidth < 9999) { numalts = 0; maxlinewidth = 9999; /* no choice but to relax this */ trybreakline(0, 0, thisindent, 0.0, 0, NULL); } time1 = getcurtime() - time0; alts = numalts; if (testinglinebreaker) { if (savelimit < 0 && testinglinebreaker > 1) { showbadlimit = bestbadness * (-savelimit); numalts = 0; bestnumalts = 0; trybreakline(0, 0, thisindent, 0.0, 0, NULL); } fprintf(outf, "\n#if 1 /* accepted #%ld, badness = %g, tried %ld in %.3f sec */\n", bestnumalts, bestbadness, alts, time1/1000.0); } showbadlimit = savelimit; maxlinewidth = savemaxlw; flush_outbuf(bestnumbreaks, bestbreakpos, bestbreakindent, bestnumedits, besteditpos, besteditold, besteditnew); if (((USETIME && time1 > 1000) || alts >= maxalts) && !regression) { sprintf(outbuf, "Line breaker spent %.1f", (time1 + time0 - time0a) / 1000.0); if (time0 != time0a) sprintf(outbuf + strlen(outbuf), "+%.2f", (time0a - time0) / 1000.0); sprintf(outbuf + strlen(outbuf), " seconds, %ld tries on line %d [251]", alts, outf_lnum); note(outbuf); } else if (verbose) { fprintf(logf, "%s, %d/%d: Line breaker spent %ld tries\n", infname, inf_lnum, outf_lnum, alts); } if (testinglinebreaker) fprintf(outf, "#endif\n\n"); } else { if (testinglinebreaker < 2) flush_outbuf(0, NULL, NULL, 0, NULL, NULL, NULL); } thisfutureindent = -1; outbufpos = 0; outbufcount = 0; } else { if (outbufpos == 0) { if (ch == ' ' && !dontbreaklines) /* eat leading spaces */ continue; thisindent = applydelta(outindent, deltaindent); deltaindent = 0; } if (outbufpos == outbufsize) { outbufsize *= 2; outbuf = REALLOC(outbuf, outbufsize, char); } outbuf[outbufpos++] = ch; if (ch >= ' ' || ch == '\f') outbufcount++; } }}void out_n_spaces(n)int n;{ while (--n >= 0) output(" ");}void out_spaces(spc, over, len, delta)int spc, over, len, delta;{ int n; if (spc == -999) spc = commentindent; if (spc < 0) { /* right-justify */ n = (-spc) - cur_column() - len; if (n < minspcthresh) n = minspacing; else over = 1000; } else if (spc >= 2000) { /* tab to multiple */ spc -= 2000; n = (spc-1) - ((cur_column()+spc-1) % spc); if (n < minspcthresh) n += spc; } else if (spc >= 1000) { /* absolute column */ spc -= 1000; n = spc - cur_column(); if (n < minspcthresh) n = minspacing; } else /* relative spacing */ n = spc; if (line_start()) { singleindent(n); } else if (len > 0 && over != 1000 && cur_column() + n + len > linewidth) { output("\n"); out_spaces(over, 1000, len, 0); singleindent(delta); } else { out_n_spaces(n); }}void testlinebreaker(lev, fn)int lev;char *fn;{ char buf[256], *bp, *cp; int first, indent; testinglinebreaker = lev; if (!fn) return; inf = fopen(fn, "r"); if (!inf) { perror(fn); exit_failure(); } sprintf(buf, "%s.br", fn); outf = fopen(buf, "w"); if (!outf) { perror(buf); exit_failure(); } setup_out(); outindent = 4; first = 1; while (fgets(buf, 256, inf)) { cp = buf + strlen(buf) - 2; if (cp >= buf) { bp = buf; indent = 0; while (isspace(*bp)) if (*bp++ == '\t') indent += 8; else indent++; if (first) { first = 0; outindent = indent; } if (!(*cp == '{' || *cp == ')' || *cp == ';') || (*cp == '/' && cp[-1] == '*')) { cp[1] = '\001'; /* eat the \n */ } else { first = 1; } output(bp); } } fclose(outf); fclose(inf);}void outsection(size)int size;{ if (size > sectionsize) sectionsize = size;}int isembedcomment(cmt)Strlist *cmt;{ int len = strlen(embedcomment); return (cmt && len > 0 && !strncmp(cmt->s, embedcomment, len) && (isspace(cmt->s[len]) || (!cmt->s[len] && cmt->next && (*cmt->next->s == '\002' || *cmt->next->s == '\003'))));}Strlist *outcomments(cmt)Strlist *cmt;{ char *cp; int saveindent = outindent, savesingle = deltaindent, theindent; int saveeat = eatcomments; int i = 0; int slash; if (!cmt) return NULL; if (!commentvisible(cmt)) { setcommentkind(cmt, CMT_DONE); return cmt->next; } if (*cmt->s == '\001') { if (cmtdebug) output(format_sd("[] [%s:%d]", CMT_NAMES[getcommentkind(cmt)], cmt->value & CMT_MASK)); cp = cmt->s; if (cp[1] == '\014') { output("\f\n"); cp += 2; if (*cp == '\001') cp++; } for ( ; *cp; cp++) { output("\n"); if (cmtdebug && cp[1]) output("[]"); } setcommentkind(cmt, CMT_DONE); return cmt->next; } dontbreaklines++; if (isembedcomment(cmt)) { embeddedcode = 1; eatcomments = 0; if (!strcmp(cmt->s, embedcomment)) { cmt = cmt->next; theindent = 0; cp = cmt/*->next*/->s + 1; while (*cp++ == ' ') theindent++; } else { strcpy(cmt->s, cmt->s + strlen(embedcomment) + 1); moreindent(deltaindent); theindent = outindent; deltaindent = 0; } slash = 0; } else { moreindent(deltaindent); if (cmt->s[0] == '\004') outindent = 0; theindent = outindent; deltaindent = 0; slash = (slashslash && (slashslash == 2 || outbufpos == 0 || outbuf[0] != '#')); if (!slash) output("/*"); } cp = cmt->s; for (;;) { if (slash) output("//"); if (*cp == '\002') cp++; else if (*cp == '\003' || *cp == '\004') { outindent = 0; cp++; } if (embeddedcode) { for (i = 0; *cp == ' ' && i < theindent; i++) cp++; i = *cp; if (*cp == '#') outindent = 0; } output(cp); if (cmtdebug) output(format_sd(" [%s:%d] ", CMT_NAMES[getcommentkind(cmt)], cmt->value & CMT_MASK)); setcommentkind(cmt, CMT_DONE); cmt = cmt->next; if (!cmt || !commentvisible(cmt)) break; cp = cmt->s; if (*cp != '\002' && *cp != '\003') break; output("\n"); if (!embeddedcode) { outindent = (*cp == '\002') ? theindent : 0; deltaindent = 0; } } if (embeddedcode) { embeddedcode = 0; if (i) { /* eat final blank line */ output("\n"); } } else { if (!slash) output("*/"); output("\n"); } outindent = saveindent; deltaindent = savesingle; dontbreaklines--; eatcomments = saveeat; return cmt;}void outcomment(cmt)Strlist *cmt;{ Strlist *savenext; if (cmt) { savenext = cmt->next; cmt->next = NULL; outcomments(cmt); cmt->next = savenext; }}void outtrailcomment(cmt, serial, indent)Strlist *cmt;int serial, indent;{ int savedelta = deltaindent;#if 0 suppressnewline = 1; output("\n"); suppressnewline = 0;#endif cmt = findcomment(cmt, CMT_TRAIL, serial); if (commentvisible(cmt)) { out_spaces(indent, commentoverindent, commentlen(cmt), 0); outcomment(cmt); deltaindent = savedelta; } else output("\n");}void flushcomments(cmt, kind, serial)Strlist **cmt;int kind, serial;{ Strlist *cmt2, *cmt3; int saveindent, savesingle, saveeat; if (!cmt) cmt = &curcomments; cmt2 = extractcomment(cmt, kind, serial); saveindent = outindent; savesingle = deltaindent; moreindent(deltaindent); deltaindent = 0; saveeat = eatcomments; if (eatcomments == 2) eatcomments = 0; cmt3 = cmt2; while (cmt3) cmt3 = outcomments(cmt3); eatcomments = saveeat; outindent = saveindent; deltaindent = savesingle; strlist_empty(&cmt2);}char *rawCstring(fmt, s, len, special)char *fmt;register char *s;int len, special;{ char buf[500]; register char *cp; register unsigned char ch; cp = buf; while (--len >= 0) { ch = *((unsigned char *) s); s++; if (ch == 0 && (len == 0 || !isdigit(*s))) { *cp++ = '\\'; *cp++ = '0'; } else if (ch == '\n') { *cp++ = '\\'; *cp++ = 'n'; } else if (ch == '\b') { *cp++ = '\\'; *cp++ = 'b'; } else if (ch == '\t') { *cp++ = '\\'; *cp++ = 't'; } else if (ch == '\f') { *cp++ = '\\'; *cp++ = 'f';#if 0 } else if (ch == '\r') { *cp++ = '\\'; *cp++ = 'r';#endif } else if (ch < ' ' || ch >= 127) { *cp++ = '\\'; *cp++ = '0' + (ch>>6); *cp++ = '0' + ((ch>>3) & 7); *cp++ = '0' + (ch & 7); } else if (ch == special) { switch (ch) { case '%': *cp++ = ch; *cp++ = ch; break; } } else { if (ch == '"' || ch == '\\') *cp++ = '\\'; *cp++ = ch; } } *cp = 0; return format_s(fmt, buf);}char *makeCstring(s, len)register char *s;int len;{ return rawCstring("\"%s\"", s, len, 0);}char *makeCchar(ich)int ich;{ char buf[500]; register char *cp; register unsigned char ch = (ich & 0xff); if (ich < 0 || ich > 255 || (ich == 0 && !nullcharconst)) return format_d("%d", ich); cp = buf; if (ch == 0) { *cp++ = '\\'; *cp++ = '0'; } else if (ch == '\n') { *cp++ = '\\'; *cp++ = 'n'; } else if (ch == '\b') { *cp++ = '\\'; *cp++ = 'b'; } else if (ch == '\t') { *cp++ = '\\'; *cp++ = 't'; } else if (ch == '\f') { *cp++ = '\\'; *cp++ = 'f';#if 0 } else if (ch == '\r') { *cp++ = '\\'; *cp++ = 'r';#endif } else if (ch < ' ' || ch >= 127) { *cp++ = '\\'; *cp++ = '0' + (ch>>6); *cp++ = '0' + ((ch>>3) & 7); *cp++ = '0' + (ch & 7); } else { if (ch == '\'' || ch == '\\') *cp++ = '\\'; *cp++ = ch; } *cp = 0; return format_s("'%s'", buf);}/* End. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -