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

3
/* Synchronet string utility routines */
4
5
6
7
8
9
10

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
rswindell's avatar
rswindell committed
11
 * Copyright 2010 Rob Swindell - http://www.synchro.net/copyright.html		*
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
 *																			*
 * 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
42
/* For all the functions that take a 'dest' argument, pass NULL to have the	*/
/* function malloc the buffer for you and return it.						*/
43
/****************************************************************************/
44
45
46
47
48

/****************************************************************************/
/* Removes ctrl-a codes from the string 'str'								*/
/****************************************************************************/
char* DLLCALL remove_ctrl_a(const char *str, char *dest)
49
{
50
	int	i,j;
51

52
	if(dest==NULL && (dest=strdup(str))==NULL)
53
54
55
		return NULL;
	for(i=j=0;str[i];i++) {
		if(str[i]==CTRL_A) {
56
			i++;
57
58
59
60
61
62
			if(str[i]==0 || toupper(str[i])=='Z')	/* EOF */
				break;
			/* convert non-destructive backspace to a destructive backspace */
			if(str[i]=='<' && j)	
				j--;
		}
63
		else dest[j++]=str[i]; 
64
	}
65
	dest[j]=0;
66
	return dest;
67
68
}

69
char* DLLCALL strip_ctrl(const char *str, char* dest)
70
{
71
	int	i,j;
72

73
	if(dest==NULL && (dest=strdup(str))==NULL)
74
75
76
		return NULL;
	for(i=j=0;str[i];i++) {
		if(str[i]==CTRL_A) {
77
			i++;
78
79
80
81
82
83
			if(str[i]==0 || toupper(str[i])=='Z')	/* EOF */
				break;
			/* convert non-destructive backspace to a destructive backspace */
			if(str[i]=='<' && j)	
				j--;
		}
84
		else if((uchar)str[i]>=' ')
85
			dest[j++]=str[i];
86
	}
87
	dest[j]=0;
88
	return dest;
89
90
}

91
char* DLLCALL strip_exascii(const char *str, char* dest)
92
{
93
	int	i,j;
94

95
	if(dest==NULL && (dest=strdup(str))==NULL)
96
97
		return NULL;
	for(i=j=0;str[i];i++)
98
		if(!(str[i]&0x80))
99
100
			dest[j++]=str[i];
	dest[j]=0;
101
	return dest;
102
}
103

104
105
106
107
108
109
110
char* DLLCALL strip_space(const char *str, char* dest)
{
	int	i,j;

	if(dest==NULL && (dest=strdup(str))==NULL)
		return NULL;
	for(i=j=0;str[i];i++)
111
		if(!isspace((unsigned char)str[i]))
112
113
114
115
116
			dest[j++]=str[i];
	dest[j]=0;
	return dest;
}

117
char* DLLCALL prep_file_desc(const char *str, char* dest)
118
{
119
	int	i,j;
120

121
	if(dest==NULL && (dest=strdup(str))==NULL)
122
		return NULL;
123
	for(i=j=0;str[i];i++)
124
		if(str[i]==CTRL_A && str[i+1]!=0) {
125
			i++;
126
127
128
129
130
131
			if(toupper(str[i])=='Z')	/* EOF */
				break;
			/* convert non-destructive backspace to a destructive backspace */
			if(str[i]=='<' && j)	
				j--;
		}
132
		else if(j && str[i]<=' ' && dest[j-1]==' ')
133
			continue;
134
		else if(i && !isalnum((unsigned char)str[i]) && str[i]==str[i-1])
135
			continue;
136
		else if((uchar)str[i]>=' ')
137
			dest[j++]=str[i];
138
		else if(str[i]==TAB || (str[i]==CR && str[i+1]==LF))
139
140
			dest[j++]=' ';
	dest[j]=0;
141
	return dest;
142
143
}

144
/****************************************************************************/
145
/* Pattern matching string search of 'insearchof' in 'string'.				*/
146
/****************************************************************************/
147
BOOL DLLCALL findstr_in_string(const char* insearchof, char* string)
148
{
149
	char*	p;
rswindell's avatar
rswindell committed
150
	char	str[256];
151
152
153
	char	search[81];
	int		c;
	int		i;
154
	BOOL	found=FALSE;
155

156
	if(string==NULL || insearchof==NULL)
rswindell's avatar
rswindell committed
157
		return(FALSE);
158
159
160

	SAFECOPY(search,insearchof);
	strupr(search);
161
	SAFECOPY(str,string);
162

163
164
	p=str;	
	SKIP_WHITESPACE(p);
165

166
167
	if(*p==';')		/* comment */
		return(FALSE);
168

169
170
171
172
	if(*p=='!')	{	/* !match */
		found=TRUE;
		p++;
	}
173

174
175
176
177
178
179
180
181
182
183
	truncsp(p);
	c=strlen(p);
	if(c) {
		c--;
		strupr(p);
		if(p[c]=='~') {
			p[c]=0;
			if(strstr(search,p))
				found=!found; 
		}
184

185
186
187
188
		else if(p[c]=='^' || p[c]=='*') {
			p[c]=0;
			if(!strncmp(p,search,c))
				found=!found; 
189
190
		}

191
192
193
194
195
		else if(p[0]=='*') {
			i=strlen(search);
			if(i<c)
				return(found);
			if(!strncmp(p+1,search+(i-c),c))
196
				found=!found; 
197
198
199
200
201
202
203
204
205
		}

		else if(!strcmp(p,search))
			found=!found; 
	} 
	return(found);
}

/****************************************************************************/
rswindell's avatar
rswindell committed
206
/* Pattern matching string search of 'insearchof' in 'list'.				*/
207
208
209
210
211
/****************************************************************************/
BOOL DLLCALL findstr_in_list(const char* insearchof, str_list_t list)
{
	size_t	index;
	BOOL	found=FALSE;
212
	char*	p;
213
214
215
216

	if(list==NULL || insearchof==NULL)
		return(FALSE);

217
218
219
220
221
222
223
	for(index=0; list[index]!=NULL; index++) {
		p=list[index];
		SKIP_WHITESPACE(p);
		found=findstr_in_string(insearchof,p);
		if(found!=(*p=='!'))
			break;
	}
rswindell's avatar
rswindell committed
224
225
226
227
228
229
230
231
	return(found);
}

/****************************************************************************/
/* Pattern matching string search of 'insearchof' in 'fname'.				*/
/****************************************************************************/
BOOL DLLCALL findstr(const char* insearchof, const char* fname)
{
232
233
	char		str[256];
	BOOL		found=FALSE;
rswindell's avatar
rswindell committed
234
235
	FILE*		fp;

236
	if(insearchof==NULL || fname==NULL)
rswindell's avatar
rswindell committed
237
238
239
240
241
		return(FALSE);

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

242
243
244
245
246
	while(!feof(fp) && !ferror(fp) && !found) {
		if(!fgets(str,sizeof(str),fp))
			break;
		found=findstr_in_string(insearchof,str);
	}
rswindell's avatar
rswindell committed
247

248
	fclose(fp);
249
250
251
252
253
254
255
	return(found);
}

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

rswindell's avatar
rswindell committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
	return(findstr(insearchof,trashcan_fname(cfg,name,fname,sizeof(fname))));
}

/****************************************************************************/
char* DLLCALL trashcan_fname(scfg_t* cfg, const char* name, char* fname, size_t maxlen)
{
	safe_snprintf(fname,maxlen,"%s%s.can",cfg->text_dir,name);
	return fname;
}

/****************************************************************************/
str_list_t DLLCALL trashcan_list(scfg_t* cfg, const char* name)
{
	char	fname[MAX_PATH+1];
	FILE*	fp;
	str_list_t	list;

	if((fp=fopen(trashcan_fname(cfg, name, fname, sizeof(fname)),"r"))==NULL)
		return NULL;

	list=strListReadFile(fp,NULL,255);

	fclose(fp);

	return list;
285
}
286
287
288
289
290

/****************************************************************************/
/* Returns the number of characters in 'str' not counting ctrl-ax codes		*/
/* or the null terminator													*/
/****************************************************************************/
291
size_t bstrlen(const char *str)
292
{
293
	size_t i=0;
294
295

	while(*str) {
296
		if(*str==CTRL_A) {
297
			str++;
298
299
300
301
302
303
304
			if(toupper(*str)=='Z')	/* EOF */
				break;
			if(*str=='[')
				i=0;
			else if(*str=='<' && i)
				i--;
		} else
305
306
			i++;
		if(!(*str)) break;
307
308
		str++; 
	}
309
310
311
312
313
314
315
	return(i);
}

/****************************************************************************/
/* Returns in 'string' a character representation of the number in l with   */
/* commas.																	*/
/****************************************************************************/
316
char* DLLCALL ultoac(ulong l, char *string)
317
318
{
	char str[256];
319
	int i,j,k;
320
321
322
323
324
325
326
327

	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))
328
329
			string[j--]=','; 
	}
330
331
332
	return(string);
}

333
334
335
/****************************************************************************/
/* Truncate string at first occurance of char in specified character set	*/
/****************************************************************************/
336
char* DLLCALL truncstr(char* str, const char* set)
337
338
339
340
{
	char* p;

	p=strpbrk(str,set);
341
	if(p!=NULL && *p!=0)
342
343
344
345
346
		*p=0;

	return(p);
}

347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/****************************************************************************/
/* rot13 encoder/decoder - courtesy of Mike Acar							*/
/****************************************************************************/
char* DLLCALL rot13(char* str)
{
	char ch, cap;
	char* p;
  
	p=str;
	while((ch=*p)!=0) {
		cap = ch & 32;
		ch &= ~cap;
		ch = ((ch >= 'A') && (ch <= 'Z') ? ((ch - 'A' + 13) % 26 + 'A') : ch) | cap;
		*(p++)=ch;
    }

	return(str);
}

366
367
368
/****************************************************************************/
/* Puts a backslash on path strings if not just a drive letter and colon	*/
/****************************************************************************/
369
char* backslashcolon(char *str)
370
371
372
373
{
    int i;

	i=strlen(str);
374
375
	if(i && !IS_PATH_DELIM(str[i-1]) && str[i-1]!=':') {
		str[i]=PATH_DELIM; 
rswindell's avatar
rswindell committed
376
377
		str[i+1]=0; 
	}
378
379

	return str;
380
381
382
383
384
}

/****************************************************************************/
/* Compares pointers to pointers to char. Used in conjuction with qsort()   */
/****************************************************************************/
385
int pstrcmp(const char **str1, const char **str2)
386
387
388
389
390
391
392
{
	return(strcmp(*str1,*str2));
}

/****************************************************************************/
/* Returns the number of characters that are the same between str1 and str2 */
/****************************************************************************/
393
int strsame(const char *str1, const char *str2)
394
395
396
397
398
399
400
401
{
	int i,j=0;

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

402

403
404
405
406
407
408
409
410
411
412
413
414
/****************************************************************************/
/* 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);
}

415
416
417
418
/****************************************************************************/
/* Converts an ASCII Hex string into an ulong                               */
/* by Steve Deppe (Ille Homine Albe)										*/
/****************************************************************************/
419
ulong ahtoul(const char *str)
420
421
422
423
424
425
426
427
{
    ulong l,val=0;

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

428
429
430
/****************************************************************************/
/* Converts hex-plus string to integer										*/
/****************************************************************************/
431
uint hptoi(const char *str)
432
433
434
435
436
437
438
439
440
441
442
443
{
	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);
}

444
/****************************************************************************/
445
/* Returns TRUE if a is a valid ctrl-a "attribute" code, FALSE if it isn't. */
446
/****************************************************************************/
447
BOOL DLLCALL valid_ctrl_a_attr(char a)
448
449
{
	switch(toupper(a)) {
450
451
		case '+':	/* push attr	*/
		case '-':   /* pop attr		*/
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
		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 'M':   /* magenta  fg  */
		case 'N':   /* normal       */
		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);
}

477
478
479
480
481
482
483
484
485
486
487
488
489
490
/****************************************************************************/
/* Returns TRUE if a is a valid QWKnet compatible Ctrl-A code, else FALSE	*/
/****************************************************************************/
BOOL DLLCALL valid_ctrl_a_code(char a)
{
	switch(toupper(a)) {
		case 'P':		/* Pause */
		case 'L':		/* CLS */
		case ',':		/* 100ms delay */
			return TRUE;
	}
	return valid_ctrl_a_attr(a);
}

491
/****************************************************************************/
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/****************************************************************************/
char DLLCALL ctrl_a_to_ascii_char(char a)
{
	switch(toupper(a)) {
		case 'L':   /* cls          */
			return FF;
		case '<':	/* backspace	*/
			return '\b';
		case '[':	/* CR			*/
			return '\r';
		case ']':	/* LF			*/
			return '\n';
	}
	return 0;
}

/****************************************************************************/
/* Strips invalid Ctrl-Ax "attribute" sequences from str                    */
510
511
/* Returns number of ^A's in line                                           */
/****************************************************************************/
512
size_t DLLCALL strip_invalid_attr(char *str)
513
{
514
    char*	dest;
515
    size_t	a,c,d;
516

517
	dest=str;
518
519
	for(a=c=d=0;str[c];c++) {
		if(str[c]==CTRL_A) {
520
			a++;
521
522
			if(str[c+1]==0)
				break;
523
			if(!valid_ctrl_a_attr(str[c+1])) {
524
525
526
				/* convert non-destructive backspace to a destructive backspace */
				if(str[c+1]=='<' && d)	
					d--;
527
528
				c++;
				continue; 
529
			}
530
		}
531
		dest[d++]=str[c]; 
532
	}
533
	dest[d]=0;
534
535
	return(a);
}
536

537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/****************************************************************************/
/****************************************************************************/
char DLLCALL exascii_to_ascii_char(uchar ch)
{
	/* Seven bit table for EXASCII to ASCII conversion */
	const char *sbtbl="CUeaaaaceeeiiiAAEaAooouuyOUcLYRfaiounNao?--24!<>"
			"###||||++||++++++--|-+||++--|-+----++++++++##[]#"
			"abrpEout*ono%0ENE+><rj%=o..+n2* ";

	if(ch&0x80)
		return sbtbl[ch^0x80];
	return ch;
}

/****************************************************************************/
/* Convert string from IBM extended ASCII to just ASCII						*/
/****************************************************************************/
char* DLLCALL ascii_str(uchar* str)
{
deuce's avatar
deuce committed
556
	uchar*	p=str;
557
558
559
560
561
562
563
564
565

	while(*p) {
		if((*p)&0x80)
			*p=exascii_to_ascii_char(*p);	
		p++;
	}
	return((char*)str);
}

566
char* replace_named_values(const char* src
567
568
					   ,char* buf
					   ,size_t buflen	/* includes '\0' terminator */
569
					   ,char* escape_seq
570
571
					   ,named_string_t* string_list
					   ,named_int_t* int_list
572
573
					   ,BOOL case_sensitive)
{
574
	char	val[32];
575
	size_t	i;
576
	size_t	esc_len=0;
577
578
579
580
581
582
583
584
585
586
	size_t	name_len;
	size_t	value_len;
	char*	p = buf;
	int (*cmp)(const char*, const char*, size_t);

	if(case_sensitive)
		cmp=strncmp;
	else
		cmp=strnicmp;

587
588
589
590
591
592
593
594
595
596
597
	if(escape_seq!=NULL)
		esc_len = strlen(escape_seq);

	while(*src && (size_t)(p-buf) < buflen-1) {
		if(esc_len) {
			if(cmp(src, escape_seq, esc_len)!=0) {
				*p++ = *src++;
				continue;
			}
			src += esc_len;	/* skip the escape seq */
		}
598
599
600
601
602
603
604
605
606
607
608
609
		if(string_list) {
			for(i=0; string_list[i].name!=NULL /* terminator */; i++) {
				name_len = strlen(string_list[i].name);
				if(cmp(src, string_list[i].name, name_len)==0) {
					value_len = strlen(string_list[i].value);
					if((p-buf)+value_len > buflen-1)	/* buffer overflow? */
						value_len = (buflen-1)-(p-buf);	/* truncate value */
					memcpy(p, string_list[i].value, value_len);
					p += value_len;
					src += name_len;
					break;
				}
610
			}
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
			if(string_list[i].name!=NULL) /* variable match */
				continue;
		}
		if(int_list) {
			for(i=0; int_list[i].name!=NULL /* terminator */; i++) {
				name_len = strlen(int_list[i].name);
				if(cmp(src, int_list[i].name, name_len)==0) {
					SAFEPRINTF(val,"%d",int_list[i].value);
					value_len = strlen(val);
					if((p-buf)+value_len > buflen-1)	/* buffer overflow? */
						value_len = (buflen-1)-(p-buf);	/* truncate value */
					memcpy(p, val, value_len);
					p += value_len;
					src += name_len;
					break;
				}
			}
			if(int_list[i].name!=NULL) /* variable match */
				continue;
		}

		*p++ = *src++;
	}
	*p=0;	/* terminate string in destination buffer */

	return(buf);
}

char* replace_keyed_values(const char* src
					   ,char* buf
					   ,size_t buflen	/* includes '\0' terminator */
					   ,char esc_char
					   ,keyed_string_t* string_list
					   ,keyed_int_t* int_list
					   ,BOOL case_sensitive)
{
	char	val[32];
	size_t	i;
	size_t	value_len;
	char*	p = buf;


	while(*src && (size_t)(p-buf) < buflen-1) {
		if(esc_char) {
			if(*src != esc_char) {
				*p++ = *src++;
				continue;
			}
			src ++;	/* skip the escape char */
660
		}
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
		if(string_list) {
			for(i=0; string_list[i].key!=0 /* terminator */; i++) {
				if((case_sensitive && *src == string_list[i].key)
					|| ((!case_sensitive) && toupper(*src) == toupper(string_list[i].key))) {
					value_len = strlen(string_list[i].value);
					if((p-buf)+value_len > buflen-1)	/* buffer overflow? */
						value_len = (buflen-1)-(p-buf);	/* truncate value */
					memcpy(p, string_list[i].value, value_len);
					p += value_len;
					src++;
					break;
				}
			}
			if(string_list[i].key!=0) /* variable match */
				continue;
		}
		if(int_list) {
			for(i=0; int_list[i].key!=0 /* terminator */; i++) {
				if((case_sensitive && *src == int_list[i].key)
					|| ((!case_sensitive) && toupper(*src) == toupper(int_list[i].key))) {
					SAFEPRINTF(val,"%d",int_list[i].value);
					value_len = strlen(val);
					if((p-buf)+value_len > buflen-1)	/* buffer overflow? */
						value_len = (buflen-1)-(p-buf);	/* truncate value */
					memcpy(p, val, value_len);
					p += value_len;
					src++;
					break;
				}
			}
			if(int_list[i].key!=0) /* variable match */
				continue;
		}

		*p++ = *src++;
696
697
698
699
700
701
	}
	*p=0;	/* terminate string in destination buffer */

	return(buf);
}

702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
uint32_t DLLCALL str_to_bits(uint32_t val, const char *str)
{
	/* op can be 0 for replace, + for add, or - for remove */
	int op=0;
	char *s;
	char ctrl;

	for(s=str; *s; s++) {
		if(*s=='+')
			op=1;
		else if(*s=='-')
			op=2;
		else {
			if(!op) {
				val=0;
				op=1;
			}
			ctrl=toupper(*s);
			ctrl&=0x1f;			/* Ensure it fits */
			switch(op) {
				case 1:		/* Add to the set */
					val |= 1<<ctrl;
					break;
				case 2:		/* Remove from the set */
					val &= ~(1<<ctrl);
					break;
			}
		}
	}
}
732
733

#if 0	/* replace_*_values test */
734
735
736
737

void main(void)
{
	char buf[128];
738
739
740
741
742
743
744
745
746
	keyed_string_t strs[] = {
		{ '+', "plus" },
		{ '=', "equals" },
		{ 0 }
	};
	keyed_int_t ints[] = {
		{ 'o', 1 },
		{ 't', 2 },
		{ 'h', 3 },
747
748
749
		{ NULL }
	};

750
751
	printf("'%s'\n", replace_keyed_values("$o $+ $t $= $h", buf, sizeof(buf), '$'
		,strs, ints, FALSE));
752
753
754
755

}

#endif
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914

#if 0 /* to be moved here from xtrn.cpp */

char* quoted_string(const char* str, char* buf, size_t maxlen)
{
	if(strchr(str,' ')==NULL)
		return((char*)str);
	safe_snprintf(buf,maxlen,"\"%s\"",str);
	return(buf);
}

#endif

#if 0 /* I think is is a misguided idea :-(  */

char* sbbs_cmdstr(const char* src
					,char* buf
					,size_t buflen	/* includes '\0' terminator */
					,scfg_t* scfg
					,user_t* user
					,int node_num
					,int minutes
					,int rows
					,int timeleft
					,SOCKET socket_descriptor
					,char* protocol
					,char* ip_address
					,char* fpath
					,char* fspec
					)
{
	const char* nulstr = "";
	char alias_buf[LEN_ALIAS+1];
	char fpath_buf[MAX_PATH+1];
	char fspec_buf[MAX_PATH+1];
	char sysop_buf[sizeof(scfg->sys_op)];

	keyed_string_t str_list[] = {
		/* user alias */
		{ 'a',	user!=NULL ? quoted_string(user->alias, alias_buf, sizeof(alias_buf)) : nulstr },
		{ 'A',	user!=NULL ? user->alias : nulstr },
		
		/* connection */
		{ 'c',	protocol },
		{ 'C',	protocol },

		/* file path */
		{ 'f',	quoted_string(fpath, fpath_buf, sizeof(fpath_buf)) },
		{ 'F',	fpath },

		/* temp dir */
		{ 'g',	scfg->temp_dir },
		{ 'G',	scfg->temp_dir },

		/* IP address */
		{ 'h',	ip_address },
		{ 'H',	ip_address },

		/* data dir */
		{ 'j',	scfg->data_dir },
		{ 'J',	scfg->data_dir },

		/* ctrl dir */
		{ 'k',	scfg->ctrl_dir },
		{ 'K',	scfg->ctrl_dir },

		/* node dir */
		{ 'n',	scfg->node_dir },
		{ 'N',	scfg->node_dir },

		/* sysop */
		{ 'o',	quoted_string(scfg->sys_op, sysop_buf, sizeof(sysop_buf)) },
		{ 'O',	scfg->sys_op },

		/* protocol */
		{ 'p',	protocol },
		{ 'P',	protocol },

		/* system QWK-ID */
		{ 'q',	scfg->sys_id },
		{ 'Q',	scfg->sys_id },

		/* file spec */
		{ 's',	quoted_string(fspec, fspec_buf, sizeof(fspec_buf)) },
		{ 'S',	fspec },

		/* UART I/O Address (in hex) 'f' for FOSSIL */
		{ 'u',	"f" },
		{ 'U',	"f" },

		/* text dir */
		{ 'z',	scfg->text_dir },
		{ 'Z',	scfg->text_dir },

		/* exec dir */
		{ '!',	scfg->exec_dir },
		{ '@',
#ifndef __unix__
			scfg->exec_dir
#else
			nulstr
#endif
		},

		/* .exe (on Windows) */
		{ '.',
#ifndef __unix__
		".exe"
#else
		nulstr
#endif
		},

		/* terminator */
		{ 0 }	
	};
	keyed_int_t int_list[] = {
		/* node number */
		{ '#',	node_num },

		/* DTE rate */
		{ 'b',	38400 },	
		{ 'B',	38400 },

		/* DCE rate */
		{ 'd',	30000 },
		{ 'D',	30000 },

		/* Estimated Rate (cps) */
		{ 'e',	3000 },
		{ 'E',	3000 },

		{ 'h',	socket_descriptor },
		{ 'H',	socket_descriptor },

		{ 'l',	user==NULL ? 0 : scfg->level_linespermsg[user->level] },
		{ 'L',	user==NULL ? 0 : scfg->level_linespermsg[user->level] },

		{ 'm',	minutes },
		{ 'M',	minutes },

		{ 'r',	rows },
		{ 'R',	rows },

		/* Time left in seconds */
		{ 't',	timeleft },
		{ 'T',	timeleft },

		/* Credits */
		{ '$',	user==NULL ? 0 : (user->cdt+user->freecdt) },

		/* terminator */
		{ 0 }	
	};


	return replace_keyed_values(src, buf, buflen, '%', str_list, int_list, TRUE);
}

rswindell's avatar
rswindell committed
915
#endif