str_util.c 14.4 KB
Newer Older
1
/* str_util.c */
2

3
/* Synchronet string utility routines */
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

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
 * Copyright 2000 Rob Swindell - http://www.synchro.net/copyright.html		*
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"

/****************************************************************************/
41
/* Removes ctrl-a codes from the string 'instr'                             */
42
/****************************************************************************/
43
char* DLLCALL remove_ctrl_a(char *instr, char *outstr)
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
	char str[1024],*p;
	uint i,j;

	for(i=j=0;instr[i] && j<sizeof(str)-1;i++) {
		if(instr[i]==CTRL_A && instr[i+1]!=0)
			i++;
		else str[j++]=instr[i]; 
	}
	str[j]=0;
	if(outstr!=NULL)
		p=outstr;
	else
		p=instr;
	strcpy(p,str);
	return(p);
}

char* DLLCALL strip_ctrl(char *str)
{
	char tmp[1024];
	int i,j;

	for(i=j=0;str[i] && j<(int)sizeof(tmp)-1;i++)
		if(str[i]==CTRL_A && str[i+1]!=0)
			i++;
		else if((uchar)str[i]>=SP)
			tmp[j++]=str[i];
	tmp[j]=0;
	strcpy(str,tmp);
	return(str);
}

char* DLLCALL strip_exascii(char *str)
{
	char tmp[1024];
	int i,j;

	for(i=j=0;str[i] && j<(int)sizeof(tmp)-1;i++)
		if(!(str[i]&0x80))
			tmp[j++]=str[i];
	tmp[j]=0;
	strcpy(str,tmp);
	return(str);
88
}
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

char* DLLCALL prep_file_desc(char *str)
{
	char tmp[1024];
	int i,j;

	for(i=j=0;str[i];i++)
		if(str[i]==CTRL_A && str[i+1]!=0)
			i++;
		else if(j && str[i]<=SP && tmp[j-1]==SP)
			continue;
		else if(i && !isalnum(str[i]) && str[i]==str[i-1])
			continue;
		else if((uchar)str[i]>=SP)
			tmp[j++]=str[i];
		else if(str[i]==TAB || (str[i]==CR && str[i+1]==LF))
			tmp[j++]=SP;
	tmp[j]=0;
	strcpy(str,tmp);
	return(str);
}

111
/****************************************************************************/
112
/* Pattern matching string search of 'insearchof' in 'fname'.				*/
113
/****************************************************************************/
114
BOOL DLLCALL findstr(char* insearchof, char* fname)
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
172
173
174
175
176
	char*	p;
	char	str[128];
	char	search[81];
	int		c;
	int		i;
	BOOL	found;
	FILE*	stream;

	if((stream=fopen(fname,"r"))==NULL)
		return(FALSE); 

	SAFECOPY(search,insearchof);
	strupr(search);

	found=FALSE;

	while(!feof(stream) && !ferror(stream) && !found) {
		if(!fgets(str,sizeof(str),stream))
			break;
		
		found=FALSE;

		p=str;	
		while(*p && *p<=' ') p++; /* Skip white-space */

		if(*p==';')		/* comment */
			continue;

		if(*p=='!')	{	/* !match */
			found=TRUE;
			p++;
		}

		truncsp(p);
		c=strlen(p);
		if(c) {
			c--;
			strupr(p);
			if(p[c]=='~') {
				p[c]=0;
				if(strstr(search,p))
					found=!found; 
			}

			else if(p[c]=='^' || p[c]=='*') {
				p[c]=0;
				if(!strncmp(p,search,c))
					found=!found; 
			}

			else if(p[0]=='*') {
				i=strlen(search);
				if(i<c)
					continue;
				if(!strncmp(p+1,search+(i-c),c))
					found=!found; 
			}

			else if(!strcmp(p,search))
				found=!found; 
		} 
177
	}
178
179
180
181
182
183
184
185
186
187
188
189
190
	fclose(stream);
	return(found);
}

/****************************************************************************/
/* Searches the file <name>.can in the TEXT directory for matches			*/
/* Returns TRUE if found in list, FALSE if not.								*/
/****************************************************************************/
BOOL DLLCALL trashcan(scfg_t* cfg, char* insearchof, char* name)
{
	char fname[MAX_PATH+1];

	sprintf(fname,"%s%s.can",cfg->text_dir,name);
191
	return(findstr(insearchof,fname));
192
}
193
194
195
196
197
198
199
200
201
202

/****************************************************************************/
/* Returns the number of characters in 'str' not counting ctrl-ax codes		*/
/* or the null terminator													*/
/****************************************************************************/
int bstrlen(char *str)
{
	int i=0;

	while(*str) {
rswindell's avatar
rswindell committed
203
		if(*str==CTRL_A)
204
205
206
207
208
209
210
211
212
213
214
215
			str++;
		else
			i++;
		if(!(*str)) break;
		str++; }
	return(i);
}

/****************************************************************************/
/* Returns in 'string' a character representation of the number in l with   */
/* commas.																	*/
/****************************************************************************/
216
char* DLLCALL ultoac(ulong l, char *string)
217
218
{
	char str[256];
219
	int i,j,k;
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

	ultoa(l,str,10);
	i=strlen(str)-1;
	j=i/3+1+i;
	string[j--]=0;
	for(k=1;i>-1;k++) {
		string[j--]=str[i--];
		if(j>0 && !(k%3))
			string[j--]=','; }
	return(string);
}

/****************************************************************************/
/* Truncates white-space chars off end of 'str'								*/
/****************************************************************************/
235
char* DLLCALL truncsp(char *str)
236
237
238
239
{
	uint c;

	c=strlen(str);
240
	while(c && (uchar)str[c-1]<=' ') c--;
241
	str[c]=0;
242
	return(str);
243
244
}

245
246
247
/****************************************************************************/
/* Truncate string at first occurance of char in specified character set	*/
/****************************************************************************/
248
char* DLLCALL truncstr(char* str, const char* set)
249
250
251
252
253
254
255
256
257
258
{
	char* p;

	p=strpbrk(str,set);
	if(p!=NULL)
		*p=0;

	return(p);
}

259
260
261
262
263
264
265
266
267
/****************************************************************************/
/* Puts a backslash on path strings 										*/
/****************************************************************************/
void backslash(char *str)
{
    int i;

	i=strlen(str);
	if(i && str[i-1]!='\\' && str[i-1]!='/') {
rswindell's avatar
rswindell committed
268
269
270
		str[i]=BACKSLASH; 
		str[i+1]=0; 
	}
271
272
273
274
275
276
277
278
279
280
281
}

/****************************************************************************/
/* Puts a backslash on path strings if not just a drive letter and colon	*/
/****************************************************************************/
void backslashcolon(char *str)
{
    int i;

	i=strlen(str);
	if(i && str[i-1]!='\\' && str[i-1]!='/' && str[i-1]!=':') {
rswindell's avatar
rswindell committed
282
283
284
		str[i]=BACKSLASH; 
		str[i+1]=0; 
	}
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
}

/****************************************************************************/
/* Compares pointers to pointers to char. Used in conjuction with qsort()   */
/****************************************************************************/
int pstrcmp(char **str1, char **str2)
{
	return(strcmp(*str1,*str2));
}

/****************************************************************************/
/* Returns the number of characters that are the same between str1 and str2 */
/****************************************************************************/
int strsame(char *str1, char *str2)
{
	int i,j=0;

	for(i=0;str1[i];i++)
		if(str1[i]==str2[i]) j++;
	return(j);
}

/****************************************************************************/
/* Returns an ASCII string for FidoNet address 'addr'                       */
/****************************************************************************/
310
char *faddrtoa(faddr_t* addr, char* outstr)
311
{
312
	static char str[64];
313
314
    char point[25];

315
316
	if(addr==NULL)
		return("0:0/0");
317
	sprintf(str,"%hu:%hu/%hu",addr->zone,addr->net,addr->node);
318
	if(addr->point) {
319
		sprintf(point,".%hu",addr->point);
320
		strcat(str,point); }
321
322
323
324
	if(outstr==NULL)
		return(str);
	strcpy(outstr,str);
	return(outstr);
325
326
}

rswindell's avatar
rswindell committed
327
char* DLLCALL net_addr(net_t* net)
328
329
{
	if(net->type==NET_FIDO)
330
		return(faddrtoa((faddr_t*)net->addr,NULL));
331
332
333
	return(net->addr);
}

334
335
static ulong msgid_serialno(smbmsg_t* msg)
{
336
	return (msg->idx.time-1000000000) + msg->idx.number;
337
338
339
}

/****************************************************************************/
340
/* Returns a FidoNet (FTS-9) message-ID										*/
341
342
343
344
345
/****************************************************************************/
char* DLLCALL ftn_msgid(sub_t *sub, smbmsg_t* msg)
{
	static char msgid[256];

346
	if(msg->ftn_msgid!=NULL && *msg->ftn_msgid!=0)
347
348
		return(msg->ftn_msgid);

349
	snprintf(msgid,sizeof(msgid)
350
		,"%s %08lx %lu.%s %08lX"
351
352
353
354
355
356
357
358
359
360
		,faddrtoa(&sub->faddr,NULL)
		,msgid_serialno(msg)
		,msg->idx.number
		,sub->code
		,msgid_serialno(msg)
		);

	return(msgid);
}

361
362
363
/****************************************************************************/
/* Return a general purpose (RFC-822) message-ID							*/
/****************************************************************************/
364
char* DLLCALL get_msgid(scfg_t* cfg, uint subnum, smbmsg_t* msg)
365
366
367
{
	static char msgid[256];

368
	if(msg->id!=NULL && *msg->id!=0)
369
370
		return(msg->id);

371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
	if(subnum>=cfg->total_subs)
		snprintf(msgid,sizeof(msgid)
			,"<%08lX.%lu@%s>"
			,msg->idx.time
			,msg->idx.number
			,cfg->sys_inetaddr);
	else
		snprintf(msgid,sizeof(msgid)
			,"<%08lX.%lu.%s@%s>"
			,msg->idx.time
			,msg->idx.number
			,cfg->sub[subnum]->code
			,cfg->sys_inetaddr);

	return(msgid);
}
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/****************************************************************************/
/* Retrieve a message by RFC822 message-ID									*/
/****************************************************************************/
BOOL DLLCALL get_msg_by_id(scfg_t* scfg, smb_t* smb, char* id, smbmsg_t* msg)
{
	ulong		n;
	int			ret;

	for(n=0;n<smb->status.last_msg;n++) {
		memset(msg,0,sizeof(smbmsg_t));
		msg->offset=n;
		if(smb_getmsgidx(smb, msg)!=0)
			break;

		if(smb_lockmsghdr(smb,msg)!=0)
			continue;

		ret=smb_getmsghdr(smb,msg);

		smb_unlockmsghdr(smb,msg); 

		if(ret!=SMB_SUCCESS)
			continue;

		if(strcmp(get_msgid(scfg,smb->subnum,msg),id)==0)
			return(TRUE);

		smb_freemsgmem(msg);
	}

	return(FALSE);
}
419

420
421
422
423
424
425
426
427
428
429
430
431
/****************************************************************************/
/* Returns string for 2 digit hex+ numbers up to 575						*/
/****************************************************************************/
char *hexplus(uint num, char *str)
{
	sprintf(str,"%03x",num);
	str[0]=num/0x100 ? 'f'+(num/0x10)-0xf : str[1];
	str[1]=str[2];
	str[2]=0;
	return(str);
}

432
433
434
435
436
437
438
439
440
441
442
443
444
/****************************************************************************/
/* Converts an ASCII Hex string into an ulong                               */
/* by Steve Deppe (Ille Homine Albe)										*/
/****************************************************************************/
ulong ahtoul(char *str)
{
    ulong l,val=0;

	while((l=(*str++)|0x20)!=0x20)
		val=(l&0xf)+(l>>6&1)*9+val*16;
	return(val);
}

445
446
447
/****************************************************************************/
/* Converts hex-plus string to integer										*/
/****************************************************************************/
448
449
450
451
452
453
454
455
456
457
458
459
460
uint hptoi(char *str)
{
	char tmp[128];
	uint i;

	if(!str[1] || toupper(str[0])<='F')
		return(ahtoul(str));
	strcpy(tmp,str);
	tmp[0]='F';
	i=ahtoul(tmp)+((toupper(str[0])-'F')*0x10);
	return(i);
}

461

462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/****************************************************************************/
/* Updates 16-bit "rcrc" with character 'ch'                                */
/****************************************************************************/
void ucrc16(uchar ch, ushort *rcrc) 
{
	ushort i, cy;
    uchar nch=ch;
 
	for (i=0; i<8; i++) {
		cy=*rcrc & 0x8000;
		*rcrc<<=1;
		if (nch & 0x80) *rcrc |= 1;
		nch<<=1;
		if (cy) *rcrc ^= 0x1021; }
}

/****************************************************************************/
/* Returns CRC-16 of ASCIIZ string (not including terminating NULL)			*/
/****************************************************************************/
ushort DLLCALL crc16(char *str)
{
	int 	i=0;
	ushort	crc=0;

	ucrc16(0,&crc);
	while(str[i])
		ucrc16(str[i++],&crc);
	ucrc16(0,&crc);
	ucrc16(0,&crc);
	return(crc);
}

494
495
496
/****************************************************************************/
/* Returns 1 if a is a valid ctrl-a code, 0 if it isn't.                    */
/****************************************************************************/
497
BOOL DLLCALL validattr(char a)
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
{

	switch(toupper(a)) {
		case '-':   /* clear        */
		case '_':   /* clear        */
		case 'B':   /* blue     fg  */
		case 'C':   /* cyan     fg  */
		case 'G':   /* green    fg  */
		case 'H':   /* high     fg  */
		case 'I':   /* blink        */
		case 'K':   /* black    fg  */
		case 'L':   /* cls          */
		case 'M':   /* magenta  fg  */
		case 'N':   /* normal       */
		case 'P':   /* pause        */
		case 'R':   /* red      fg  */
		case 'W':   /* white    fg  */
		case 'Y':   /* yellow   fg  */
		case '0':   /* black    bg  */
		case '1':   /* red      bg  */
		case '2':   /* green    bg  */
		case '3':   /* brown    bg  */
		case '4':   /* blue     bg  */
		case '5':   /* magenta  bg  */
		case '6':   /* cyan     bg  */
		case '7':   /* white    bg  */
			return(TRUE); 
	}
	return(FALSE);
}

/****************************************************************************/
/* Strips invalid Ctrl-Ax sequences from str                                */
/* Returns number of ^A's in line                                           */
/****************************************************************************/
533
size_t DLLCALL strip_invalid_attr(char *strin)
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
{
    char str[1024];
    size_t a,c,d;

	for(a=c=d=0;strin[c] && d<sizeof(str)-1;c++) {
		if(strin[c]==CTRL_A && strin[c+1]!=0) {
			a++;
			if(!validattr(strin[c+1])) {
				c++;
				continue; 
			} 
		}
		str[d++]=strin[c]; 
	}
	str[d]=0;
	strcpy(strin,str);
	return(a);
}
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566

ushort DLLCALL subject_crc(char *subj)
{
	char str[512];

	while(!strnicmp(subj,"RE:",3)) {
		subj+=3;
		while(*subj==SP)
			subj++; 
	}

	SAFECOPY(str,subj);
	strlwr(str);
	return(crc16(str));
}