1 /*
2  *	(c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
3  *      Copyright (c) 1996-2005 Michael T Pins.  All rights reserved.
4  *
5  *	Pack subject by eliminating RE prefixes and - (nf) suffixes.
6  *	Also collapse multiple blanks into single blanks.
7  */
8 
9 #include <stdlib.h>
10 #include <string.h>
11 #include <ctype.h>
12 #include "config.h"
13 #include "global.h"
14 
15 int
pack_subject(register char * dest,register char * src,int * re_counter_ptr,int max_length)16 pack_subject(register char *dest, register char *src, int *re_counter_ptr, int max_length)
17 {
18     register char  *max_dest;
19     int             re = 0;
20     char           *start_dest = dest;
21 
22     if (src) {
23 	max_dest = dest + max_length;
24 
25 	while (*src) {
26 	    if (isspace(*src)) {
27 		src++;
28 		continue;
29 	    }
30 	    /* count and remove 'Re: Re: ...' */
31 
32 	    if (*src != 'R' && *src != 'r')
33 		break;
34 	    *dest++ = *src++;
35 
36 	    if (*src != 'e' && *src != 'E')
37 		break;
38 	    *dest++ = *src++;
39 
40 	    if (*src == ':' || *src == ' ') {
41 		src++;
42 		dest = start_dest;
43 		re++;
44 		continue;
45 	    }
46 	    if (*src != '^')
47 		break;
48 
49 	    src++;
50 	    dest = start_dest;
51 
52 	    while (isdigit(*src))
53 		*dest++ = *src++;
54 	    if (dest == start_dest)
55 		re++;
56 	    else {
57 		*dest = NUL;
58 		dest = start_dest;
59 		re += atoi(dest);
60 	    }
61 	    if (*src == ':')
62 		src++;
63 	}
64 
65 	while (*src && dest < max_dest) {
66 	    if (*src == '-' && strncmp("- (nf)", src, 5) == 0)
67 		break;
68 	    if (isascii(*src) && isspace(*src)) {
69 		do
70 		    src++;
71 		while (isascii(*src) && isspace(*src));
72 		*dest++ = SP;
73 	    } else
74 		*dest++ = *src++;
75 	}
76     }
77     *dest = NUL;
78     *re_counter_ptr = (char) re;
79 
80     return dest - start_dest;
81 }
82