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