* added RCS Id keyword strings to sources
[citadel.git] / citadel / support.c
1 /* $Id$ */
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <ctype.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <pthread.h>
8 #include "citadel.h"
9 #include "server.h"
10 #include "support.h"
11
12
13 /*
14  * strproc()  -  make a string 'nice'
15  */
16 void strproc(char *string)
17 {
18         int a;
19
20         if (strlen(string)==0) return;
21
22         /* Convert non-printable characters to blanks */
23         for (a=0; a<strlen(string); ++a) {
24                 if (string[a]<32) string[a]=32;
25                 if (string[a]>126) string[a]=32;
26                 }
27
28         /* Remove leading and trailing blanks */
29         while( (string[0]<33) && (strlen(string)>0) )
30                 strcpy(string,&string[1]);
31         while( (string[strlen(string)-1]<33) && (strlen(string)>0) )
32                 string[strlen(string)-1]=0;
33
34         /* Remove double blanks */
35         for (a=0; a<strlen(string); ++a) {
36                 if ((string[a]==32)&&(string[a+1]==32)) {
37                         strcpy(&string[a],&string[a+1]);
38                         a=0;
39                         }
40                 }
41
42         /* remove characters which would interfere with the network */
43         for (a=0; a<strlen(string); ++a) {
44                 while (string[a]=='!') strcpy(&string[a],&string[a+1]);
45                 while (string[a]=='@') strcpy(&string[a],&string[a+1]);
46                 while (string[a]=='_') strcpy(&string[a],&string[a+1]);
47                 while (string[a]==',') strcpy(&string[a],&string[a+1]);
48                 while (string[a]=='%') strcpy(&string[a],&string[a+1]);
49                 while (string[a]=='|') strcpy(&string[a],&string[a+1]);
50                 }
51
52         }
53
54
55
56 /*
57  * num_parms()  -  discover number of parameters...
58  */
59 int num_parms(char *source)
60 {
61         int a;
62         int count = 1;
63
64         for (a=0; a<strlen(source); ++a) 
65                 if (source[a]=='|') ++count;
66         return(count);
67         }
68
69 /*
70  * extract()  -  extract a parameter from a series of "|" separated...
71  */
72 void extract(char *dest, char *source, int parmnum)
73 {
74         char buf[256];
75         int count = 0;
76         int n;
77
78         if (strlen(source)==0) {
79                 strcpy(dest,"");
80                 return;
81                 }
82
83         n = num_parms(source);
84
85         if (parmnum >= n) {
86                 strcpy(dest,"");
87                 return;
88                 }
89         strcpy(buf,source);
90         if ( (parmnum == 0) && (n == 1) ) {
91                 strcpy(dest,buf);
92                 for (n=0; n<strlen(dest); ++n)
93                         if (dest[n]=='|') dest[n] = 0;
94                 return;
95                 }
96
97         while (count++ < parmnum) do {
98                 strcpy(buf,&buf[1]);
99                 } while( (strlen(buf)>0) && (buf[0]!='|') );
100         if (buf[0]=='|') strcpy(buf,&buf[1]);
101         for (count = 0; count<strlen(buf); ++count)
102                 if (buf[count] == '|') buf[count] = 0;
103         strcpy(dest,buf);
104         }
105
106 /*
107  * extract_int()  -  extract an int parm w/o supplying a buffer
108  */
109 int extract_int(char *source, int parmnum)
110 {
111         char buf[256];
112         
113         extract(buf,source,parmnum);
114         return(atoi(buf));
115         }
116
117 /*
118  * extract_long()  -  extract an long parm w/o supplying a buffer
119  */
120 long extract_long(char *source, long int parmnum)
121 {
122         char buf[256];
123         
124         extract(buf,source,parmnum);
125         return(atol(buf));
126         }
127
128
129
130 /*
131  * get a line of text from a file
132  * ignores lines starting with #
133  */
134 int getstring(FILE *fp, char *string)
135 {
136         int a,c;
137         do {
138                 strcpy(string,"");
139                 a=0;
140                 do {
141                         c=getc(fp);
142                         if (c<0) {
143                                 string[a]=0;
144                                 return(-1);
145                                 }
146                         string[a++]=c;
147                         } while(c!=10);
148                         string[a-1]=0;
149                 } while(string[0]=='#');
150         return(strlen(string));
151         }
152
153
154 /*
155  * pattern2()  -  searches for patn within search string, returns pos 
156  */ 
157 int pattern2(char *search, char *patn)
158 {
159         int a;
160         for (a=0; a<strlen(search); ++a) {
161                 if (!strncasecmp(&search[a],patn,strlen(patn))) return(a);
162                 }
163         return(-1);
164         }
165
166
167 /*
168  * mesg_locate()  -  locate a message or help file, case insensitive
169  */
170 void mesg_locate(char *targ, char *searchfor, int numdirs, char **dirs)
171 {
172         int a;
173         char buf[256];
174         FILE *ls;
175
176         for (a=0; a<numdirs; ++a) {
177                 sprintf(buf,"cd %s; exec ls",dirs[a]);
178                 ls = (FILE *) popen(buf,"r");
179                 if (ls != NULL) {
180                         while(fgets(buf,255,ls)!=NULL) {
181                                 while (isspace(buf[strlen(buf)-1]))
182                                         buf[strlen(buf)-1] = 0;
183                                 if (!strcasecmp(buf,searchfor)) {
184                                         pclose(ls);
185                                         sprintf(targ,"%s/%s",dirs[a],buf);
186                                         return;
187                                         }
188                                 }
189                         pclose(ls);
190                         }
191                 }
192         strcpy(targ,"");
193         }
194
195
196 #ifndef HAVE_STRERROR
197 /*
198  * replacement strerror() for systems that don't have it
199  */
200 char *strerror(int e)
201 {
202         static char buf[32];
203
204         sprintf(buf,"errno = %d",e);
205         return(buf);
206         }
207 #endif
208