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
|
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
/* tailored getline() after the Debian Linux manpage about getline() */
/* tailored in the sense that no tests for str and n being NULL are */
/* performed, and it may be assumed that *str == NULL */
/* This function was added because some systems don't offer */
/* a getline() implementation for C */
static size_t const bufsize = 100;
static void check_memory(void const *s)
{
if (!s)
{
fprintf(stderr, "Out of memory!\n");
exit(1);
}
}
static ssize_t y_getline(char **str, size_t *n, FILE *in)
{
char *dest = malloc(bufsize); /* realloc ok for initial buf */
ssize_t size = 0; /* nothing read yet */
check_memory(dest);
while (fgets(dest + size, (int)bufsize, in)) /* read until \n or EOF */
{
/* check for \n: done if found */
char *newline = strchr(dest + size, '\n');
if (newline)
{
size = newline - dest + 1; /* at \n: `size' is # chars read */
break;
}
size += bufsize - 1; /* max # chars read */
/* expand `dest' by bufsize */
check_memory(dest = realloc(dest, size + bufsize));
}
*str = dest;
*n = size;
return size == 0 ? -1 : size; /* -1 must also be returned on EOF */
/* i.e., when 0 chars were read */
}
int main(int argc, char **argv)
{
FILE *input = NULL;
char *endlabel = 0;
char *indent = 0;
char *label = 0;
char *progName = strrchr(argv[0], '/');
char *verbendl = "\n";
char *vindent = 0;
int endLabelFound = 0;
int labelFound = 0;
int verbOpened = 0;
int nrPrefix = 0;
size_t labelsize = 0;
size_t spaces = 0;
size_t tabs = 0;
size_t verbspaces = 8;
size_t verbtabs = 0;
if (!progName)
progName = argv[0];
else
++progName;
while (1)
{
int c;
switch(c = getopt(argc, argv, "t:s:v:S:V:N"))
{
case 's':
spaces = atoi(optarg);
continue;
case 't':
tabs = atoi(optarg);
continue;
case 'S':
verbspaces = atoi(optarg);
continue;
case 'T':
verbtabs = atoi(optarg);
continue;
case 'N':
verbendl = "";
continue;
case 'n':
nrPrefix = 1;
continue;
case -1:
break;
default:
return 1;
}
break; /* only at case -1 */
}
if (argc - optind < 2)
{
fprintf(stderr, "usage: %s "
"[-n] [-s<nr>] [-t<nr>] [-S<nr>] [-T<nr>] "
"marker file\n"
"See also: `man yodlverbinsert'\n",
progName);
return 1;
}
argv += optind;
label = argv[0];
endlabel = label[1] == '/' ? "//=" : "/**/";
labelsize = strlen(label);
input = fopen(argv[1], "r");
if (!input)
{
fprintf(stderr, "Can't read `%s'\n", argv[1]);
return 1;
}
check_memory(vindent = malloc(verbtabs + verbspaces + 1));
check_memory(indent = malloc(tabs + spaces + 1));
memset(vindent, '\t', verbtabs);
memset(vindent + verbtabs, ' ', verbspaces);
vindent[verbtabs + verbspaces] = 0;
memset(indent, '\t', tabs);
memset(indent + tabs, ' ', spaces);
indent[tabs + spaces] = 0;
while (1)
{
char *line = NULL;
size_t nchars;
if (y_getline(&line, &nchars, input) < 0)
break;
if
(
strstr(line, label) == line /* matching (end)label */
&&
isspace((unsigned char)line[labelsize])
)
{
if (label == endlabel) /* when endlabel: done */
{
endLabelFound = 1;
break;
}
labelFound = 1;
label = endlabel; /* now search endlabel */
labelsize = strlen(endlabel);
continue;
}
if (labelFound)
{
if (!verbOpened)
{
verbOpened = 1;
printf("%sverb(%s", vindent, verbendl);
}
if (
if (nrPrefix == 0)
printf("%s%s", indent, line);
else
printf("%s%2d: %s", indent, nrPrefix++, line);
}
free(line);
}
fclose(input); /* to make cppcheck happy */
free(indent);
free(vindent);
if (!labelFound)
{
fprintf(stderr, "%s: label section not found\n", progName);
return 1;
}
if (!endLabelFound)
{
fprintf(stderr, "%s: label section not ended\n", progName);
return 1;
}
printf(")\n"); /* terminate verb(... by ) */
return 0;
}
|