1 /* Parse HyperText Document Address HTParse.c
2 ** ================================
14 /* char * search; no - treated as part of path */
19 /* Strip white space off a string
20 ** ------------------------------
23 ** Return value points to first non-white character, or to 0 if none.
24 ** All trailing white space is OVERWRITTEN with zero.
28 char * HTStrip(char * s)
34 #define SPACE(c) ((c==' ')||(c=='\t')||(c=='\n'))
36 for(p=s;*p;p++); /* Find end of string */
38 if(SPACE(*p)) *p=0; /* Zap trailing blanks */
41 while(SPACE(*s))s++; /* Strip leading blanks */
46 /* Scan a filename for its consituents
47 ** -----------------------------------
50 ** name points to a document name which may be incomplete.
52 ** absolute or relative may be nonzero (but not both).
53 ** host, anchor and access may be nonzero if they were specified.
54 ** Any which are nonzero point to zero terminated strings.
57 PRIVATE void scan(char * name, struct struct_parts *parts)
59 PRIVATE void scan(name, parts)
61 struct struct_parts *parts;
66 int length = strlen(name);
75 for(p=name; *p; p++) {
78 parts->access = name; /* Access name has been specified */
85 for(p=name+length-1; p>=name; p--) {
88 *p=0; /* terminate the rest */
94 parts->host = p+2; /* host has been specified */
95 *p=0; /* Terminate access */
96 p=strchr(parts->host,'/'); /* look for end of host name if any */
98 *p=0; /* Terminate host */
99 parts->absolute = p+1; /* Root has been found */
102 parts->absolute = p+1; /* Root found but no host */
105 parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
107 #ifdef NOT_DEFINED /* search is just treated as part of path */
109 char *p = relative ? relative : absolute;
111 char * q = strchr(p, '?'); /* Any search string? */
113 *q = 0; /* If so, chop that off. */
122 /* Parse a Name relative to another name
123 ** -------------------------------------
125 ** This returns those parts of a name which are given (and requested)
126 ** substituting bits from the related name where necessary.
129 ** aName A filename given
130 ** relatedName A name relative to which aName is to be parsed
131 ** wanted A mask for the bits which are wanted.
134 ** returns A pointer to a malloc'd string which MUST BE FREED
137 char * HTParse(const char * aName, const char * relatedName, int wanted)
139 char * HTParse(aName, relatedName, wanted)
147 char * return_value = 0;
152 struct struct_parts given, related;
154 /* Make working copies of input strings to cut up:
156 len = strlen(aName)+strlen(relatedName)+10;
157 result=(char *)malloc(len); /* Lots of space: more than enough */
158 if (result == NULL) outofmem(__FILE__, "HTParse");
160 StrAllocCopy(name, aName);
161 StrAllocCopy(rel, relatedName);
165 result[0]=0; /* Clear string */
166 if (wanted & PARSE_ACCESS)
167 if (given.access|| related.access) {
168 strcat(result, given.access ? given.access : related.access);
169 if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
172 if (given.access && related.access) /* If different, inherit nothing. */
173 if (strcmp(given.access, related.access)!=0) {
180 if (wanted & PARSE_HOST)
181 if(given.host || related.host) {
182 if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
183 strcat(result, given.host ? given.host : related.host);
186 if (given.host && related.host) /* If different hosts, inherit no path. */
187 if (strcmp(given.host, related.host)!=0) {
193 if (wanted & PARSE_PATH) {
194 if(given.absolute) { /* All is given */
195 if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
196 strcat(result, given.absolute);
197 } else if(related.absolute) { /* Adopt path not name */
199 strcat(result, related.absolute);
200 if (given.relative) {
201 p = strchr(result, '?'); /* Search part? */
202 if (!p) p=result+strlen(result)-1;
203 for (; *p!='/'; p--); /* last / */
204 p[1]=0; /* Remove filename */
205 strcat(result, given.relative); /* Add given one */
208 } else if(given.relative) {
209 strcat(result, given.relative); /* what we've got */
210 } else if(related.relative) {
211 strcat(result, related.relative);
212 } else { /* No inheritance */
217 if (wanted & PARSE_ANCHOR)
218 if(given.anchor || related.anchor) {
219 if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
220 strcat(result, given.anchor ? given.anchor : related.anchor);
225 StrAllocCopy(return_value, result);
227 return return_value; /* exactly the right length */
230 /* Simplify a filename
231 // -------------------
233 // A unix-style file is allowed to contain the seqeunce xxx/../ which may be
234 // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
235 // Simplification helps us recognize duplicate filenames.
237 // Thus, /etc/junk/../fred becomes /etc/fred
238 // /etc/junk/./fred becomes /etc/junk/fred
241 void HTSimplify(char * filename)
243 void HTSimplify(filename)
250 for(p=filename+2; *p; p++) {
252 if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
253 for (q=p-1; (q>filename) && (*q!='/'); q--); /* prev slash */
255 strcpy(q, p+3); /* Remove /xxx/.. */
256 if (!*filename) strcpy(filename, "/");
257 p = q-1; /* Start again with prev slash */
258 } else { /* xxx/.. error? */
259 strcpy(filename, p[3] ? p+4 : p+3); /* rm xxx/../ */
260 p = filename; /* Start again */
262 } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
263 strcpy(p, p+2); /* Remove a slash and a dot */
270 /* Make Relative Name
271 ** ------------------
273 ** This function creates and returns a string which gives an expression of
274 ** one address as related to another. Where there is no relation, an absolute
275 ** address is retured.
278 ** Both names must be absolute, fully qualified names of nodes
282 ** The return result points to a newly allocated name which, if
283 ** parsed by HTParse relative to relatedName, will yield aName.
284 ** The caller is responsible for freeing the resulting name later.
288 char * HTRelative(const char * aName, const char *relatedName)
290 char * HTRelative(aName, relatedName)
296 CONST char *p = aName;
297 CONST char *q = relatedName;
298 CONST char * after_access = 0;
299 CONST char * path = 0;
300 CONST char * last_slash = 0;
303 for(;*p; p++, q++) { /* Find extent of match */
305 if (*p==':') after_access = p+1;
309 if (slashes==3) path=p;
313 /* q, p point to the first non-matching character or zero */
315 if (!after_access) { /* Different access */
316 StrAllocCopy(result, aName);
317 } else if (slashes<3){ /* Different nodes */
318 StrAllocCopy(result, after_access);
319 } else if (slashes==3){ /* Same node, different path */
320 StrAllocCopy(result, path);
321 } else { /* Some path in common */
323 for(; *q && (*q!='#'); q++) if (*q=='/') levels++;
324 result = (char *)malloc(3*levels + strlen(last_slash) + 1);
325 if (result == NULL) outofmem(__FILE__, "HTRelative");
327 for(;levels; levels--)strcat(result, "../");
328 strcat(result, last_slash+1);
330 if (TRACE) printf("HT: `%s' expressed relative to\n `%s' is\n `%s'.",
331 aName, relatedName, result);