PostgreSQL Source Code: src/tools/pg_bsd_indent/io.c Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32#if 0

33#ifndef lint

34static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";

35#endif

36#endif

37

38#include "c.h"

39

40#include <ctype.h>

42#include <stdio.h>

43#include <stdlib.h>

47

50

51static char *lookahead_buf;

53static char *lookahead_start;

54static char *lookahead_ptr;

55static char *lookahead_end;

56static char *lookahead_bp_save;

57

58static int pad_output(int current, int target);

59

60void

62{

63

64

65

66

67 int cur_col,

68 target_col = 1;

69 static int not_first_line;

70

74 }

78 else {

81 }

82 }

90 }

91 else {

94 }

95 }

100 ps.ind_stmt = 0;

101

102

103

105 ++code_lines;

106

107

108 if (e_lab != s_lab) {

112 }

117 if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0

118 || strncmp(s_lab, "#endif", 6) == 0)) {

121 do putc(*s++, output);

122 while (s < e_lab && 'a' <= *s && *s<='z');

123 while ((*s == ' ' || *s == '\t') && s < e_lab)

124 s++;

126 fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",

127 (int)(e_lab - s), s);

128 }

131 }

132 else

133 cur_col = 1;

134

136

137 if (s_code != e_code) {

138 char *p;

139

143 }

145 {

146 int i;

147

151 }

152 cur_col = pad_output(cur_col, target_col);

154 if (*p == (char) 0200)

156 else

159 }

160 if (s_com != e_com) {

162 char *com_st = s_com;

163

165 while (*com_st == '\t')

166

167 com_st++, target += tabsize;

168 while (target <= 0)

169 if (*com_st == ' ')

170 target++, com_st++;

171 else if (*com_st == '\t')

172 target = tabsize * (1 + (target - 1) / tabsize) + 1, com_st++;

173 else

174 target = 1;

175 if (cur_col > target) {

176

178 cur_col = 1;

180 }

181 while (e_com > com_st && isspace((unsigned char)e_com[-1]))

184 fwrite(com_st, e_com - com_st, 1, output);

186 ++ps.com_lines;

187 }

189 putc('\014', output);

190 else

196 }

197 else

200 }

202

203

204

205

206

207

212 *(e_lab = s_lab) = '\0';

219 not_first_line = 1;

220}

221

222int

224{

226

233 else {

234 int w;

236

239 t -= w + 1;

240 if (t > target_col)

241 target_col = t;

242 }

243 else

244 target_col = t;

245 }

248 return target_col;

249}

250

251int

253{

254 return

256 : *s_lab == '#' ? 1

258}

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274int

276{

277

280

283

284 if (i == EOF)

285 return i;

286 if (i == '\0')

287 continue;

288

290

291 char *new_buf;

292 size_t req;

293

295 req = 64;

296 new_buf = malloc(req);

297 } else {

300 }

301 if (new_buf == NULL)

302 errx(1, "too much lookahead required");

308 }

309

311 }

313}

314

315

316

317

318

319void

321{

322

324

326}

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345void

347{

348 char *p;

349 int i;

351

352 if (bp_save != NULL) {

353 buf_ptr = bp_save;

358 return;

359

360 }

367 errx(1, "input line too long");

370 }

373 } else {

375 if ((i = getc(f)) == EOF) {

376 *p++ = ' ';

377 *p++ = '\n';

379 break;

380 }

381 }

382 if (i != '\0')

383 *p++ = i;

384 if (i == '\n')

385 break;

386 }

389 if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') {

390 if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)

391 fill_buffer();

392 else {

393 int com = 0;

394

396 while (*p == ' ' || *p == '\t')

397 p++;

398 if (*p == '/' && p[1] == '*') {

399 p += 2;

400 while (*p == ' ' || *p == '\t')

401 p++;

402 if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'

403 && p[4] == 'N' && p[5] == 'T') {

404 p += 6;

405 while (*p == ' ' || *p == '\t')

406 p++;

407 if (*p == '*')

408 com = 1;

409 else if (*p == 'O') {

410 if (*++p == 'N')

411 p++, com = 1;

412 else if (*p == 'F' && *++p == 'F')

413 p++, com = 2;

414 }

415 while (*p == ' ' || *p == '\t')

416 p++;

417 if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {

425 }

426 }

427 }

428 }

429 }

430 }

433 do

435 while (*p++ != '\n');

436 }

437}

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467static int

469

470

471

472

473

474{

475 int curr;

476

477 if (current >= target)

478 return (current);

479 curr = current;

481 int tcur;

482

483 while ((tcur = tabsize * (1 + (curr - 1) / tabsize) + 1) <= target) {

485 tcur != curr + 1 ||

487 curr = tcur;

488 }

489 }

490 while (curr++ < target)

491 putc(' ', output);

492

493 return (target);

494}

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516int

518

519

520

521

522{

523 char *buf;

524

525 for (buf = buffer; *buf != '\0' && buf != end; ++buf) {

526 switch (*buf) {

527

528 case '\n':

529 case 014:

531 break;

532

533 case '\t':

535 break;

536

537 case 010:

539 break;

540

541 default:

543 break;

544 }

545 }

546 return (cur);

547}

548

549int

551{

553}

554

555void

556diag4(int level, const char *msg, int a, int b)

557{

558 if (level)

561 fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);

564 }

565 else {

566 fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);

569 }

570}

571

572void

573diag3(int level, const char *msg, int a)

574{

575 if (level)

578 fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);

581 }

582 else {

583 fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);

586 }

587}

588

589void

590diag2(int level, const char *msg)

591{

592 if (level)

595 fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);

598 }

599 else {

600 fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);

601 fprintf(stderr, "%s", msg);

603 }

604}

605

#define fprintf(file, fmt, msg)

void errx(int eval, const char *fmt,...)

int blanklines_after_declarations

int postfix_blankline_requested

int prefix_blankline_requested

int swallow_optional_blanklines

int lineup_to_parens_always

static char * lookahead_buf_end

static char * lookahead_bp_save

int compute_code_target(void)

void diag3(int level, const char *msg, int a)

int compute_label_target(void)

int count_spaces(int cur, char *buffer)

int count_spaces_until(int cur, char *buffer, char *end)

static char * lookahead_start

static char * lookahead_buf

void diag2(int level, const char *msg)

static int pad_output(int current, int target)

void lookahead_reset(void)

void diag4(int level, const char *msg, int a, int b)

static char * lookahead_ptr

static char * lookahead_end