📄 out.c
字号:
/* "p2c", a Pascal to C translator. Copyright (C) 1989, 1990, 1991, 1992, 1993 Free Software Foundation. Author's address: daveg@synaptics.com.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation (any version).This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; see the file COPYING. If not, write tothe Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* This needs to go before trans.h (and thus p2c.proto) is read */typedef struct S_paren { struct S_paren *next; int pos, indent, qmindent, flags;} Paren;#define PROTO_OUT_C#include "trans.h"#ifndef USETIME# if defined(BSD) || defined(hpux)# define USETIME 1# else# define USETIME 0# endif#endif#if USETIME# include <sys/time.h>#else# include <time.h>#endif/* Output control characters: \001 \B Possible break point \002 \X Break point in parentheses \003 \( Invisible open paren \004 \) Invisible close paren \005 \T Set left margin \006 \F Forced break point \007 \A Preceding paren requires all-or-none breaking \010 \[ Invisible open paren, becomes visible if not all on one line \011 \S Break point after last "special argument" of a function \012 \n (newline) \013 \E Preceding break has extra penalty \014 \f (form-feed) \015 \r (return) \016 \H Hang-indent the preceding operator \017 \C Break point for last : of a ?: construct*/char spchars[] = ".BX()TFA[SnEfrHC................";Static int testinglinebreaker = 0;Static int deltaindent, thisindent, thisfutureindent;Static int sectionsize, blanklines, codesectsize, hdrsectsize;Static int codelnum, hdrlnum;#define MAXBREAKS 200Static int numbreaks, bestnumbreaks;Static double bestbadness;Static int breakpos[MAXBREAKS], breakindent[MAXBREAKS];Static int breakcount[MAXBREAKS], breakparen[MAXBREAKS];Static int bestbreakpos[MAXBREAKS], bestbreakindent[MAXBREAKS];Static int breakerrorflag;#define MAXEDITS 200Static int numedits, bestnumedits;Static int editpos[MAXEDITS], besteditpos[MAXEDITS];Static char editold[MAXEDITS], editnew[MAXEDITS];Static char besteditold[MAXEDITS], besteditnew[MAXEDITS];Static Paren *parenlist;Static long numalts, bestnumalts;Static int randombreaks;Static char *outbuf, *outfilebuf, *outfilebufptr, *outfilebufend;Static int outbufpos, outbufcount, outbufsize, outfilebufsize;Static int suppressnewline, lastlinelength;Static int eatblanks;Static int embeddedcode;Static int showingsourcecode = 0;#define BIGBADNESS (1e20)void setup_out(){ end_source(); if (!nobanner) if (slashslash) fprintf(outf, "// From input file \"%s\"\n", infname); else fprintf(outf, "/* From input file \"%s\" */\n", infname); outf_lnum++; hdrlnum = 1; outindent = 0; deltaindent = 0; thisindent = 0; thisfutureindent = -1; sectionsize = 2; blanklines = 0; dontbreaklines = 0; embeddedcode = 0; outputmode = 0; suppressnewline = 0; eatblanks = 0; outbufsize = 1000; outbuf = ALLOC(outbufsize, char, misc); outbufpos = 0; outbufcount = 0; outfilebufsize = 10; outfilebuf = ALLOC(outfilebufsize, char, misc); outfilebufptr = outfilebuf; outfilebufend = outfilebuf + outfilebufsize/2; srand(17);}int grow_outfilebuf(){ int pos = outfilebufptr - outfilebuf; outfilebufsize *= 2; outfilebuf = REALLOC(outfilebuf, outfilebufsize, char); outfilebufptr = outfilebuf + pos; outfilebufend = outfilebuf + outfilebufsize/2; return 1;}void flush_outfilebuf(){ if (outfilebufptr > outfilebuf) { *outfilebufptr = 0; replacestrings(outfilebuf, replaceafter); fputs(outfilebuf, outf); outfilebufptr = outfilebuf; }}#define putc_outf(ch) ( \ (outfilebufptr == outfilebufend) ? grow_outfilebuf() : 0, \ *outfilebufptr++ = (ch) \)void puts_outf(s)char *s;{ int len = strlen(s); if (len > 0) { while (outfilebufptr + len > outfilebufend) grow_outfilebuf(); strcpy(outfilebufptr, s); outfilebufptr += len; if (outfilebufptr[-1] == '\n') flush_outfilebuf(); }}void select_outfile(fp)FILE *fp;{ flush_outfilebuf(); if (outf == codef) { codesectsize = sectionsize; codelnum = outf_lnum; } else { hdrsectsize = sectionsize; hdrlnum = outf_lnum; } outf = fp; if (outf == codef) { sectionsize = codesectsize; outf_lnum = codelnum; } else { sectionsize = hdrsectsize; outf_lnum = hdrlnum; }}void start_source(){ if (!showingsourcecode) { puts_outf("\n#ifdef Pascal\n"); showingsourcecode = 1; }}void end_source(){ if (showingsourcecode) { puts_outf("#endif /*Pascal*/\n\n"); showingsourcecode = 0; }}int line_start(){ return (outbufcount == 0);}int cur_column(){ if (outbufpos == 0) return outindent; else return thisindent + outbufcount;}int lookback(n)int n;{ if (n <= 0 || n > outbufpos) return 0; else return outbuf[outbufpos - n];}int lookback_prn(n)int n;{ for (;;) { if (n <= 0 || n > outbufpos) return 0; else if (outbuf[outbufpos - n] >= ' ') return outbuf[outbufpos - n]; else n++; }}/* Combine two indentation adjustments */int adddeltas(d1, d2)int d1, d2;{ if (d2 >= 1000) return d2; else return d1 + d2;}/* Apply an indentation delta */int applydelta(i, d)int i, d;{ if (d >= 1000) return d - 1000; else return i + d;}/* Adjust the current indentation by delta */void moreindent(delta)int delta;{ outindent = applydelta(outindent, delta);}/* Adjust indentation for just this line */void singleindent(delta)int delta;{ deltaindent = adddeltas(deltaindent, delta);}/* Predict indentation for next line */void futureindent(num)int num;{ thisfutureindent = applydelta(applydelta(outindent, deltaindent), num);}int parsedelta(cp, def)char *cp;int def;{ if (!cp || !*cp) return def; if ((*cp == '+' || *cp == '-') && isdigit(cp[1])) return atoi(cp); if (*cp == '*' && isdigit(cp[1])) return 2000 + atoi(cp+1); else return 1000 + atoi(cp);}Static void leading_tab(col)int col;{ if (col > maxlinewidth) return; /* something wrong happened! */ if (phystabsize > 0) { while (col >= phystabsize) { putc_outf('\t'); col -= phystabsize; } } while (col > 0) { putc_outf(' '); col--; }}void eatblanklines(){ eatblanks = 1;}Static void flush_outbuf(numbreaks, breakpos, breakindent, numedits, editpos, editold, editnew)int numbreaks, *breakpos, *breakindent, numedits, *editpos;char *editold, *editnew;{ unsigned char ch, ch2; char *cp; int i, j, linelen = 0, spaces, hashline; int editsaves[MAXEDITS]; end_source(); if (outbufcount > 0) { for (i = 0; i < numedits; i++) { editsaves[i] = outbuf[editpos[i]]; outbuf[editpos[i]] = editnew[i]; } cp = outbuf; if (*cp != '\f' || cp[1]) leading_tab(thisindent); hashline = (*cp == '#'); /* a preprocessor directive */ spaces = 0; j = 1; for (i = 0; i < outbufpos; ) { if (j < numbreaks && i == breakpos[j]) { if (hashline) puts_outf(" \\"); /* trailing backslash required */ putc_outf('\n'); flush_outfilebuf(); outf_lnum++; leading_tab(breakindent[j]); linelen = breakindent[j]; j++; while (i < outbufpos && *cp == ' ') i++, cp++; /* eat leading spaces */ spaces = 0; /* eat trailing spaces */ } else { ch = *cp++; if (ch == ' ') { spaces++; } else if (ch > ' ' || ch == '\f') { linelen += spaces; while (spaces > 0) putc_outf(' '), spaces--; linelen++; if (ch == '\\' && embeddedcode) { if (*cp == '[') { putc_outf('{'); cp++, i++; } else if (*cp == ']') { putc_outf('}'); cp++, i++; } else putc_outf(ch); } else putc_outf(ch); } else if (testinglinebreaker >= 3) { linelen += spaces; while (spaces > 0) putc_outf(' '), spaces--; linelen++; putc_outf('\\'); ch2 = spchars[ch]; if (ch2 != '.') putc_outf(ch2); else { putc_outf('0' + ((ch >> 6) & 7)); putc_outf('0' + ((ch >> 3) & 7)); putc_outf('0' + (ch & 7)); } } i++; } } for (i = 0; i < numedits; i++) outbuf[editpos[i]] = editsaves[i]; eatblanks = 0; } else if (eatblanks) { return; } if (suppressnewline) { lastlinelength = linelen; } else { putc_outf('\n'); flush_outfilebuf(); } outf_lnum++;}#define ISQUOTE(ch) ((ch)=='"' || (ch)=='\'')#define ISOPENP(ch) ((ch)=='(' || (ch)=='[' || (ch)=='\003' || (ch)=='\010')#define ISCLOSEP(ch) ((ch)==')' || (ch)==']' || (ch)=='\004')#define ISBREAK(ch) ((ch)=='\001' || (ch)=='\002' || (ch)=='\006' || (ch)=='\011' || (ch)=='\017')Static int readquotes(posp, err)int *posp, err;{ int pos; char quote; pos = *posp; quote = outbuf[pos++]; while (pos < outbufpos && outbuf[pos] != quote) { if (outbuf[pos] == '\\') pos++; pos++; } if (pos >= outbufpos) { if (err && breakerrorflag) { intwarning("output", "Mismatched quotes [248]"); breakerrorflag = 0; } return 0; } else { *posp = pos; return 1; } }Static int maxdepth;Static int readparens(posp, err)int *posp, err;{ char ch, closing; int pos, level; pos = *posp; switch (outbuf[pos]) { case '(': closing = ')'; break; case '[': closing = ']'; break; case '\003': case '\010': closing = '\004'; break; default: closing = 0; break; } level = 0; for (;;) { pos++; if (pos >= outbufpos) break; ch = outbuf[pos]; if (ISOPENP(ch)) { level++; if (level > maxdepth) maxdepth = level; } else if (ISCLOSEP(ch)) { level--; if (level < 0) { if (closing && outbuf[pos] != closing) break; *posp = pos; return 1; } } else if (ISQUOTE(ch)) { if (!readquotes(&pos, err)) return 0; } } if (err && breakerrorflag) { switch (closing) { case ')': intwarning("output", "Mismatched parentheses [249]"); break; case ']': intwarning("output", "Mismatched brackets [249]"); break; default: intwarning("output", "Mismatched clauses [250]"); break; } breakerrorflag = 0; } return 0;}Static int measurechars(first, last)int first, last;{ int count = 0; while (first <= last) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -