File: comment.c

package info (click to toggle)
mgt 2.31-7
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 460 kB
  • ctags: 582
  • sloc: ansic: 4,852; sh: 411; pascal: 329; makefile: 88; csh: 3
file content (93 lines) | stat: -rw-r--r-- 1,787 bytes parent folder | download | duplicates (3)
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
/* "mgt" Copyright (c) 1991 Shodan  */

#include "mgt.h"
#include <string.h>
#include <ctype.h>


#define NEWBLOCK 20

typedef char clinetype[MAXCOMMENTWIDTH];

static int nlines = 0;
int clines = 0;
clinetype *cbuf = 0;
char *empty = "";

FUNCTION char *commentGet(line)	/* int */
int line;
{
   if (cbuf && line < clines)
      return cbuf[line];
   else
      return empty;
}


FUNCTION int commentLines()
{
   return clines;
}


FUNCTION void formatComment(comment, width)
char *comment;
int width;
{
   char *partstart, *dest, c;
   int len, partlen, wordlen;

   clines = 0;
   width = MIN(width, MAXCOMMENTWIDTH - 1);
   wordlen = partlen = len = 0;
   dest = *cbuf;
   if (*comment == '\n')
      comment++;
   partstart = comment;
   do {
      if (clines >= nlines) {
	 nlines += NEWBLOCK;
	 cbuf = (clinetype *) (cbuf ? realloc(cbuf, nlines * sizeof(clinetype)) :
			       malloc(nlines * sizeof(clinetype)));
	 if (!cbuf)
	    barf("Memory allocation failure (formatComment)");
	 dest = cbuf[clines];
      }
      c = *(comment++);
      len++;
      partlen++;
      wordlen++;
      if (c == ' ')
	 wordlen = 0;
      else if (c == '\n' && *(comment - 2) == ' ' &&
	       *comment != '\n') {
	 partlen--;
	 len--;
	 strncpy(dest, partstart, partlen);
	 dest += partlen;
	 partstart = comment;
	 wordlen = partlen = 0;
      } else if (c == '\n' || !c) {
	 strncpy(dest, partstart, partlen - 1);
	 *(dest + partlen - 1) = 0;
	 len = partlen = wordlen = 0;
	 dest = cbuf[++clines];
	 partstart = comment;
      }
      if (len == width) {

	 if (len == wordlen)
	    wordlen = 0;

	 else
	    len = partlen - wordlen;

	 strncpy(dest, partstart, len);
	 partstart += len;
	 *(dest + len) = 0;
	 len = partlen = wordlen;

	 dest = cbuf[++clines];
      }
   } while (c);
}