summaryrefslogtreecommitdiffstats
path: root/src/to_cat.c
blob: c6aeb5f089ca285c4c70c58ad68f612298ca591e (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern char *rindex (const char *, int);	/* not always in <string.h> */

#include "defs.h"
#include "manfile.h"
#include "man-config.h"
#include "to_cat.h"
#include "util.h"

/*
 * Given PATH/man1/name.1, return a pointer to the '/' following PATH.
 */
static char *
mantail_of(char *name) {
     char *s0, *s1, *s;

     s0 = s1 = 0;
     for (s = name; *s; s++) {
	     if (*s == '/') {
		     s0 = s1;
		     s1 = s;
	     }
     }
     return s0;
}

/*
 * Given PATH/man1/name.1, return PATH, newly allocated.
 * The argument must be writable, not a constant string.
 */
const char *
mandir_of(const char *name) {
     char *p, *q;

     q = my_strdup(name);
     p = mantail_of(q);
     if (p) {
	  *p = 0;
	  return q;
     }
     free(q);
     return NULL;
}

/*
 * Change a name of the form PATH/man1/name.1[.Z]
 *                      into PATH/cat1/name.1.EXT
 * or (FSSTND) change /usr/PA/man/PB/man1/name.1
 *               into /var/catman/PA/PB/cat1/name.1.EXT
 * or (FHS) change /usr/PATH/share/man/LOC/man1/name.1
 *            into /var/cache/man/PATH/LOC/cat1/name.1.EXT
 * (here the /LOC part is absent or a single [locale] dir).
 *
 * Returns 0 on failure.
 */

const char *
convert_to_cat (const char *name0, const char *ext, int standards) {
     char *name, *freename, *cat_name = 0;
     char *t0, *t2, *t3, *t4;
     struct dirs *dlp;
     int len;

     freename = name = my_strdup (name0);

     t0 = rindex (name, '.');
     if (t0 && get_expander(t0)) /* remove compressee extension */
	  *t0 = 0;

     t2 = mantail_of (name);
     if (t2 == NULL)
	  return 0;
     *t2 = 0;			/* remove man1/name.1 part */

     if (strncmp(t2+1, "man", 3) != 0)
	  return 0;
     t2[1] = 'c';
     t2[3] = 't';

     len = (ext ? strlen(ext) : 0);

     /* Explicitly given cat file? */
     for (dlp = cfdirlist.nxt; dlp; dlp = dlp->nxt) {
	  if (!strcmp (name, dlp->mandir)) {
	       if (!dlp->catdir[0])
		    break;
	       *t2 = '/';
	       len += strlen (dlp->catdir) + strlen (t2) + 1;
	       cat_name = (char *) my_malloc (len);
	       strcpy (cat_name, dlp->catdir);
	       strcat (cat_name, t2);
	       goto gotit;
	  }
     }

     if (standards & FHS) {
	  if (*name != '/')
	       return 0;

	  /* possibly strip locale part */
	  t3 = t2;
	  if ((t4 = rindex(name,'/')) != NULL && strcmp(t4, "/man")) {
	       *t3 = '/';
	       t3 = t4;
	       *t3 = 0;
	  }

	  if(t3 - name >= 4 && !strcmp(t3 - 4, "/man")) {
	       /* fhs is applicable; strip leading /usr and trailing share */
	       if(!strncmp(name, "/usr/", 5))
	            name += 4;
	       t4 = t3 - 4;
	       *t4 = 0;
	       if(t4 - name >= 6 && !strcmp(t4 - 6, "/share"))
		    t4[-6] = 0;
	       *t3 = '/';

	       len += strlen("/var/cache/man") + strlen(name) + strlen(t3) + 1;
	       cat_name = (char *) my_malloc (len);
	       strcpy (cat_name, "/var/cache/man");
	       strcat (cat_name, name);
	       strcat (cat_name, t3);
	       goto gotit;
	  }

	  return 0;
     }

     if ((standards & FSSTND) && !strncmp(name, "/usr/", 5)) {
	  /* search, starting at the end, for a part `man' to delete */
	  t3 = t2;
	  while ((t4 = rindex(name, '/')) != NULL && strcmp(t4, "/man")) {
	       *t3 = '/';
	       t3 = t4;
	       *t3 = 0;
	  }
	  *t3 = '/';
	  if (t4) {
	       *t4 = 0;
	       len += strlen("/var/catman") + strlen (name+4) + strlen (t3) + 1;
	       cat_name = (char *) my_malloc (len);
	       strcpy (cat_name, "/var/catman");
	       strcat (cat_name, name+4);
	       strcat (cat_name, t3);
	       goto gotit;
	  }
     } else
	  *t2 = '/';

     if (ext) {			/* allocate room for extension */
	  len += strlen(name) + 1;
	  cat_name = (char *) my_malloc (len);
	  strcpy (cat_name, name);
     } else
	  cat_name = name;

gotit:

     if ((standards & DO_HP) && get_expander(cat_name)) {
	  /* nothing - we have cat1.Z/file.1 */
     } else if (ext)
	  strcat (cat_name, ext);

     if (name != cat_name)
	  free (freename);

     return cat_name;
}