aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/old/man_extractor.c
blob: eeb40a8e5705c1eb996e4ab1b211fbe62090b66c (plain) (blame)
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
/*
     This file is part of libextractor.
     (C) 2002, 2003, 2004, 2009 Vidyut Samanta and Christian Grothoff

     libextractor is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     libextractor is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libextractor; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
 */

#include "platform.h"
#include "extractor.h"
#include <ctype.h>

static char *
stndup (const char *str, size_t n)
{
  char *tmp;
  tmp = malloc (n + 1);
  if (tmp == NULL)
    return NULL;
  tmp[n] = '\0';
  memcpy (tmp, str, n);
  return tmp;
}

static int
addKeyword (enum EXTRACTOR_MetaType type,
            char *keyword, 
	    EXTRACTOR_MetaDataProcessor proc,
	    void *proc_cls)
{
  int ret;
  if (keyword == NULL)
    return 0;
  if (strlen (keyword) == 0)
    {
      free (keyword);
      return 0;
    }
  if ((keyword[0] == '\"') && (keyword[strlen (keyword) - 1] == '\"'))
    {
      char *tmp;

      keyword[strlen (keyword) - 1] = '\0';
      tmp = strdup (&keyword[1]);
      free (keyword);
      if (tmp == NULL)
	return 0;
      keyword = tmp;
    }
  if (strlen (keyword) == 0)
    {
      free (keyword);
      return 0;
    }
  ret = proc (proc_cls, 
	      "man",
	      type,
	      EXTRACTOR_METAFORMAT_UTF8,
	      "text/plain",
	      keyword,
	      strlen (keyword)+1);
  free (keyword);
  return ret;
}

static void
NEXT (size_t * end, const char *buf, const size_t size)
{
  int quot;

  quot = 0;
  while ((*end < size) && (((quot & 1) != 0) || ((buf[*end] != ' '))))
    {
      if (buf[*end] == '\"')
        quot++;
      (*end)++;
    }
  if ((quot & 1) == 1)
    (*end) = size + 1;
}

/**
 * How many bytes do we actually try to scan? (from the beginning
 * of the file).
 */
#define MAX_READ (16 * 1024)

#define ADD(t,s) do { if (0 != addKeyword (t, s, proc, proc_cls)) return 1; } while (0)

int 
EXTRACTOR_man_extract (const char *buf,
		       size_t size,
		       EXTRACTOR_MetaDataProcessor proc,
		       void *proc_cls,
		       const char *options)
{
  int pos;
  size_t xsize;
  const size_t xlen = strlen (".TH ");

  if (size > MAX_READ)
    size = MAX_READ;
  pos = 0;
  if (size < xlen)
    return 0;
  while ((pos < size - xlen) &&
         ((0 != strncmp (".TH ",
                         &buf[pos],
                         xlen)) || ((pos != 0) && (buf[pos - 1] != '\n'))))
    {
      if (!isgraph ((unsigned char) buf[pos]) && 
	  !isspace ((unsigned char) buf[pos]))
        return 0;
      pos++;
    }
  xsize = pos;
  while ((xsize < size) && (buf[xsize] != '\n'))
    xsize++;
  size = xsize;

  if (0 == strncmp (".TH ", &buf[pos], xlen))
    {
      size_t end;

      pos += xlen;
      end = pos;
      NEXT (&end, buf, size);
      if (end > size)
        return 0;
      if (end - pos > 0)
        {
          ADD (EXTRACTOR_METATYPE_TITLE, stndup (&buf[pos], end - pos));
          pos = end + 1;
        }
      if (pos >= size)
        return 0;
      end = pos;
      NEXT (&end, buf, size);
      if (end > size)
        return 0;
      if (buf[pos] == '\"')
        pos++;
      if ((end - pos >= 1) && (end - pos <= 4))
        {
          switch (buf[pos])
            {
            case '1':
              ADD (EXTRACTOR_METATYPE_SECTION,
		   strdup (_("Commands")));
              break;
            case '2':
              ADD (EXTRACTOR_METATYPE_SECTION,
                                 strdup (_("System calls")));
              break;
            case '3':
              ADD (EXTRACTOR_METATYPE_SECTION,
                                 strdup (_("Library calls")));
              break;
            case '4':
              ADD (EXTRACTOR_METATYPE_SECTION,
                                 strdup (_("Special files")));
              break;
            case '5':
              ADD (EXTRACTOR_METATYPE_SECTION,
                                 strdup (_("File formats and conventions")));
              break;
            case '6':
              ADD (EXTRACTOR_METATYPE_SECTION,
                                 strdup (_("Games")));
              break;
            case '7':
              ADD (EXTRACTOR_METATYPE_SECTION,
                                 strdup (_("Conventions and miscellaneous")));
              break;
            case '8':
              ADD (EXTRACTOR_METATYPE_SECTION,
                                 strdup (_("System management commands")));
              break;
            case '9':
              ADD (EXTRACTOR_METATYPE_SECTION,
                                 strdup (_("Kernel routines")));
              break;
            }
          pos = end + 1;
        }
      end = pos;
      NEXT (&end, buf, size);
      if (end > size)
        return 0;
      if (end - pos > 0)
        {
          ADD (EXTRACTOR_METATYPE_MODIFICATION_DATE, stndup (&buf[pos], end - pos));
          pos = end + 1;
        }
      end = pos;
      NEXT (&end, buf, size);
      if (end > size)
        return 0;
      if (end - pos > 0)
        {
          ADD (EXTRACTOR_METATYPE_SOURCE,
	       stndup (&buf[pos], end - pos));
          pos = end + 1;
        }
      end = pos;
      NEXT (&end, buf, size);
      if (end > size)
        return 0;
      if (end - pos > 0)
        {
          ADD (EXTRACTOR_METATYPE_BOOK_TITLE,
	       stndup (&buf[pos], end - pos));
          pos = end + 1;
        }
    }

  return 0;
}

/* end of man_extractor.c */