File: pr_comment.c

package info (click to toggle)
indent 2.2.12-4%2Bdeb12u3
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 5,980 kB
  • sloc: ansic: 37,897; sh: 5,187; makefile: 708; yacc: 291; sed: 16; cpp: 5
file content (450 lines) | stat: -rw-r--r-- 13,645 bytes parent folder | download | duplicates (11)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* Copyright (c) 1992, Free Software Foundation, Inc.  All rights reserved.

   Copyright (c) 1985 Sun Microsystems, Inc. Copyright (c) 1980 The Regents
   of the University of California. Copyright (c) 1976 Board of Trustees of
   the University of Illinois. All rights reserved.

   Redistribution and use in source and binary forms are permitted
   provided that
   the above copyright notice and this paragraph are duplicated in all such
   forms and that any documentation, advertising materials, and other
   materials related to such distribution and use acknowledge that the
   software was developed by the University of California, Berkeley, the
   University of Illinois, Urbana, and Sun Microsystems, Inc.  The name of
   either University or Sun Microsystems may not be used to endorse or
   promote products derived from this software without specific prior written
   permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
   OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */


/* NAME: pr_comment

FUNCTION: This routine takes care of scanning and printing comments.

ALGORITHM: 1) Decide where the comment should be aligned, and if lines should
   be broken. 2) If lines should not be broken and filled, just copy up to
   end of comment. 3) If lines should be filled, then scan thru input_buffer
   copying characters to com_buf.  Remember where the last blank, tab, or
   newline was.  When line is filled, print up to last blank and continue
   copying.

HISTORY: November 1976	D A Willcox of CAC	Initial coding 12/6/76
    A Willcox of CAC	Modification to handle UNIX-style comments

*/

/* this routine processes comments.  It makes an attempt to keep comments
   from going over the max line length.  If a line is too long, it moves
   everything from the last blank to the next comment line.  Blanks and tabs
   from the beginning of the input line are removed */


#include "sys.h"
#include "indent.h"

/* Declared and documented in indent.h.  */
int out_coms;

pr_comment ()
{
  int now_col;			/* column we are in now */
  int adj_max_col;		/* Adjusted max_col for when we decide to
				   spill comments over the right margin */
  char *last_bl;		/* points to the last blank in the output
				   buffer */
  char *t_ptr;			/* used for moving string */
  int unix_comment;		/* tri-state variable used to decide if it is
				   a unix-style comment. 0 means only blanks
				   since /*, 1 means regular style comment, 2
				   means unix style comment */
  int break_delim = comment_delimiter_on_blankline;
  int l_just_saw_decl = parser_state_tos->just_saw_decl;
  /* int         parser_state_tos->last_nl = 0;	/* true iff the last
     significant thing weve seen is a newline */
  int one_liner = 1;		/* true iff this comment is a one-liner */
  adj_max_col = max_col;
  parser_state_tos->just_saw_decl = 0;
  last_bl = 0;			/* no blanks found so far */
  parser_state_tos->box_com = false;	/* at first, assume that we are not
					   in a boxed comment or some other
					   comment that should not be touched */
  ++out_coms;			/* keep track of number of comments */
  unix_comment = 1;		/* set flag to let us figure out if there is
				   a unix-style comment ** DISABLED: use 0 to
				   reenable this hack! */

  /* Figure where to align and how to treat the comment */

  if (parser_state_tos->col_1 && !format_col1_comments)
    {				/* if comment starts in column 1 it should
				   not be touched */
      parser_state_tos->box_com = true;
      parser_state_tos->com_col = 1;
    }
  else
    {
      if (*buf_ptr == '-' || *buf_ptr == '*' || !format_comments)
	{
	  parser_state_tos->box_com = true;	/* a comment with a '-' or
						   '*' immediately after the
						   /* is assumed to be a
						   boxed comment */
	  break_delim = 0;
	}


      /* Used to also check that parser_state_tos->bl_line != 0 */
      if ((s_lab == e_lab) && (s_code == e_code))
	{
	  /* klg: check only if this line is blank */
	  /* If this (*and previous lines are*) blank, dont put comment way
	     out at left */
	  parser_state_tos->com_col
	    = (parser_state_tos->ind_level - unindent_displace) + 1;
	  adj_max_col = block_comment_max_col;
	  if (parser_state_tos->com_col <= 1)
	    parser_state_tos->com_col = 1 + !format_col1_comments;

	  /* If we have a comment in an old-style (pre-ANSI) parameter
	     declaration, indent it like we would the parameter declaration.
	     For example:
	       int destroy (what) / * N things to destroy.  * / int what;
	  */
	  if (parser_state_tos->in_parameter_declaration
	      && indent_parameters != 0
	      && parser_state_tos->dec_nest == 0)
	    {
	      parser_state_tos->com_col = indent_parameters + 1;
	      parser_state_tos->ind_stmt = 0;
	    }
	}
      else
	{
	  register target_col;
	  break_delim = 0;
	  if (s_code != e_code)
	    target_col = count_spaces (compute_code_target (), s_code);
	  else
	    {
	      target_col = 1;
	      if (s_lab != e_lab)
		target_col = count_spaces (compute_label_target (), s_lab);
	    }
	  parser_state_tos->com_col = parser_state_tos->decl_on_line
	    || parser_state_tos->ind_level == 0 ? decl_com_ind : com_ind;
	  /* If we are already past the position for the comment, put it at
	     the next tab stop.  */
	  if (parser_state_tos->com_col < target_col)
	    parser_state_tos->com_col = ((target_col + (tabsize - 1))
					 & ~(tabsize - 1)) + 1;
	  if (else_or_endif)
	    {
	      parser_state_tos->com_col = else_endif_col;
	      else_or_endif = false;
	      /* We want the comment to appear one space after the #else or
	         #endif.  */
	      if (parser_state_tos->com_col < target_col)
		parser_state_tos->com_col = target_col + 1;
	    }
	  if (parser_state_tos->com_col + 24 > adj_max_col)
	    adj_max_col = parser_state_tos->com_col + 24;
	}
    }
  if (parser_state_tos->box_com)
    {
      parser_state_tos->n_comment_delta = 1 - parser_state_tos->com_col;
#if 0
      buf_ptr[-2] = 0;
      parser_state_tos->n_comment_delta = 1 - count_spaces (1, cur_line);
      buf_ptr[-2] = '/';
#endif
    }
  else
    {
      parser_state_tos->n_comment_delta = 0;
      while (*buf_ptr == ' ' || *buf_ptr == '\t')
	buf_ptr++;
    }
  parser_state_tos->comment_delta = 0;
  *e_com++ = '/';		/* put '/*' into buffer */
  *e_com++ = '*';
  if (*buf_ptr != ' ' && !parser_state_tos->box_com)
    *e_com++ = ' ';

  *e_com = '\0';
  if (troff)
    {
      now_col = 1;
      adj_max_col = 80;
    }
  else
    /* figure what column we would be in if we printed the comment now */
    now_col = count_spaces (parser_state_tos->com_col, s_com);

  /* Start to copy the comment */

  while (1)
    {				/* this loop will go until the comment is
				   copied */
      if (*buf_ptr > 040 && *buf_ptr != '*')
	parser_state_tos->last_nl = 0;
      check_com_size;
      switch (*buf_ptr)
	{			/* this checks for various spcl cases */
	case 014:		/* check for a form feed */
	  if (!parser_state_tos->box_com)
	    {			/* in a text comment, break the line here */
	      parser_state_tos->use_ff = true;
	      /* fix so dump_line uses a form feed */
	      dump_line ();
	      last_bl = 0;
	      *e_com++ = ' ';
	      *e_com++ = '*';
	      *e_com++ = ' ';
	      while (*++buf_ptr == ' ' || *buf_ptr == '\t');
	    }
	  else
	    {
	      if (++buf_ptr >= buf_end)
		fill_buffer ();
	      *e_com++ = 014;
	    }
	  break;

	case '\n':
	  if (had_eof)
	    {			/* check for unexpected eof */
	      printf ("Unterminated comment\n");
	      *e_com = '\0';
	      dump_line ();
	      return;
	    }
	  one_liner = 0;
	  if (parser_state_tos->box_com || parser_state_tos->last_nl)
	    {			/* if this is a boxed comment, we dont ignore
				   the newline */
	      if (s_com == e_com)
		{
		  *e_com++ = ' ';
		  *e_com++ = ' ';
		}
	      *e_com = '\0';
	      if (!parser_state_tos->box_com && e_com - s_com > 3)
		{
		  if (break_delim == 1 && s_com[0] == '/'
		      && s_com[1] == '*' && s_com[2] == ' ')
		    {
		      char *t = e_com;
		      break_delim = 2;
		      e_com = s_com + 2;
		      *e_com = 0;
		      if (blanklines_before_blockcomments)
			prefix_blankline_requested = 1;
		      dump_line ();
		      e_com = t;
		      s_com[0] = s_com[1] = s_com[2] = ' ';
		    }
		  dump_line ();
		  check_com_size;
		  *e_com++ = ' ';
		  *e_com++ = ' ';
		}
	      dump_line ();
	      now_col = parser_state_tos->com_col;
	    }
	  else
	    {
	      parser_state_tos->last_nl = 1;
	      if (unix_comment != 1)
		{		/* we not are in unix_style comment */
		  if (unix_comment == 0 && s_code == e_code)
		    {
		      /* if it is a UNIX-style comment, ignore the
		         requirement that previous line be blank for
		         unindention */
		      parser_state_tos->com_col
			= ((parser_state_tos->ind_level - unindent_displace)
			   + ind_size);
		      if (parser_state_tos->com_col <= 1)
			parser_state_tos->com_col = 2;
		    }
		  unix_comment = 2;	/* permanently remember that we are
					   in this type of comment */
		  dump_line ();
		  ++line_no;
		  now_col = parser_state_tos->com_col;
		  *e_com++ = ' ';
		  /* fix so that the star at the start of the line will line
		     up */
		  do		/* flush leading white space */
		    if (++buf_ptr >= buf_end)
		      fill_buffer ();
		  while (*buf_ptr == ' ' || *buf_ptr == '\t');
		  break;
		}
	      if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
		last_bl = e_com - 1;
	      /* if there was a space at the end of the last line, remember
	         where it was */
	      else
		{		/* otherwise, insert one */
		  last_bl = e_com;
		  check_com_size;
		  *e_com++ = ' ';
		  ++now_col;
		}
	    }
	  ++line_no;		/* keep track of input line number */
	  if (!parser_state_tos->box_com)
	    {
	      int nstar = 1;
	      do
		{		/* flush any blanks and/or tabs at start of
				   next line */
		  if (++buf_ptr >= buf_end)
		    fill_buffer ();
		  if (*buf_ptr == '*' && --nstar >= 0)
		    {
		      if (++buf_ptr >= buf_end)
			fill_buffer ();
		      if (*buf_ptr == '/')
			goto end_of_comment;
		    }
		}
	      while (*buf_ptr == ' ' || *buf_ptr == '\t');
	    }
	  else if (++buf_ptr >= buf_end)
	    fill_buffer ();
	  break;		/* end of case for newline */

	case '*':		/* must check for possibility of being at end
				   of comment */
	  if (++buf_ptr >= buf_end)	/* get to next char after * */
	    fill_buffer ();

	  if (unix_comment == 0)/* set flag to show we are not in unix-style
				   comment */
	    unix_comment = 1;

	  if (*buf_ptr == '/')
	    {			/* it is the end!!! */
	    end_of_comment:
	      if (++buf_ptr >= buf_end)
		fill_buffer ();

	      if (*(e_com - 1) != ' ' && !parser_state_tos->box_com)
		{		/* insure blank before end */
		  *e_com++ = ' ';
		  ++now_col;
		}
	      if (break_delim == 1 && !one_liner && s_com[0] == '/'
		  && s_com[1] == '*' && s_com[2] == ' ')
		{
		  char *t = e_com;
		  break_delim = 2;
		  e_com = s_com + 2;
		  *e_com = 0;
		  if (blanklines_before_blockcomments)
		    prefix_blankline_requested = 1;
		  dump_line ();
		  e_com = t;
		  s_com[0] = s_com[1] = s_com[2] = ' ';
		}
	      if (break_delim == 2 && e_com > s_com + 3
	      /* now_col > adj_max_col - 2 && !parser_state_tos->box_com */ )
		{
		  *e_com = '\0';
		  dump_line ();
		  now_col = parser_state_tos->com_col;
		}
	      check_com_size;
	      *e_com++ = '*';
	      *e_com++ = '/';
	      *e_com = '\0';
	      parser_state_tos->just_saw_decl = l_just_saw_decl;
	      return;
	    }
	  else
	    {			/* handle isolated '*' */
	      *e_com++ = '*';
	      ++now_col;
	    }
	  break;
	default:		/* we have a random char */
	  if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
	    unix_comment = 1;	/* we are not in unix-style comment */

	  *e_com = *buf_ptr++;
	  if (buf_ptr >= buf_end)
	    fill_buffer ();

	  if (*e_com == '\t')	/* keep track of column */
	    now_col = now_col + tabsize - (now_col - 1) % tabsize;
	  else if (*e_com == '\b')	/* this is a backspace */
	    --now_col;
	  else
	    ++now_col;

	  if (*e_com == ' ' || *e_com == '\t')
	    last_bl = e_com;
	  /* remember we saw a blank */

	  ++e_com;
	  if (now_col > adj_max_col
	      && !parser_state_tos->box_com
	      && unix_comment == 1
	      && e_com[-1] > ' ')
	    {
	      /* the comment is too long, it must be broken up */
	      if (break_delim == 1 && s_com[0] == '/'
		  && s_com[1] == '*' && s_com[2] == ' ')
		{
		  char *t = e_com;
		  break_delim = 2;
		  e_com = s_com + 2;
		  *e_com = 0;
		  if (blanklines_before_blockcomments)
		    prefix_blankline_requested = 1;
		  dump_line ();
		  e_com = t;
		  s_com[0] = s_com[1] = s_com[2] = ' ';
		}
	      if (last_bl == 0)
		{		/* we have seen no blanks */
		  last_bl = e_com;	/* fake it */
		  *e_com++ = ' ';
		}
	      *e_com = '\0';	/* print what we have */
	      *last_bl = '\0';
	      while (last_bl > s_com && last_bl[-1] < 040)
		*--last_bl = 0;
	      e_com = last_bl;
	      dump_line ();

	      *e_com++ = ' ';	/* add blanks for continuation */
	      *e_com++ = ' ';
	      *e_com++ = ' ';

	      t_ptr = last_bl + 1;
	      last_bl = 0;
	      if (t_ptr >= e_com)
		{
		  while (*t_ptr == ' ' || *t_ptr == '\t')
		    t_ptr++;
		  while (*t_ptr != '\0')
		    {		/* move unprinted part of comment down in
				   buffer */
		      if (*t_ptr == ' ' || *t_ptr == '\t')
			last_bl = e_com;
		      *e_com++ = *t_ptr++;
		    }
		}
	      *e_com = '\0';
	      /* recompute current position */
	      now_col = count_spaces (parser_state_tos->com_col, s_com);
	    }
	  break;
	}
    }
}