xref: /titanic_50/usr/src/lib/krb5/ss/utils.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
2*7c478bd9Sstevel@tonic-gate 
3*7c478bd9Sstevel@tonic-gate /*
4*7c478bd9Sstevel@tonic-gate  * Copyright 1987, 1988 by MIT Student Information Processing Board
5*7c478bd9Sstevel@tonic-gate  *
6*7c478bd9Sstevel@tonic-gate  * For copyright information, see copyright.h.
7*7c478bd9Sstevel@tonic-gate  */
8*7c478bd9Sstevel@tonic-gate 
9*7c478bd9Sstevel@tonic-gate #include <string.h>
10*7c478bd9Sstevel@tonic-gate #include "copyright.h"
11*7c478bd9Sstevel@tonic-gate #include "ss_internal.h"	/* includes stdio and string */
12*7c478bd9Sstevel@tonic-gate 
13*7c478bd9Sstevel@tonic-gate extern FILE *output_file;
14*7c478bd9Sstevel@tonic-gate 
15*7c478bd9Sstevel@tonic-gate char *gensym(), *str_concat3(), *quote();
16*7c478bd9Sstevel@tonic-gate extern long gensym_n;
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate void write_ct(hdr, rql)
19*7c478bd9Sstevel@tonic-gate     char const *hdr, *rql;
20*7c478bd9Sstevel@tonic-gate {
21*7c478bd9Sstevel@tonic-gate     char *sym;
22*7c478bd9Sstevel@tonic-gate     sym = gensym("ssu");
23*7c478bd9Sstevel@tonic-gate     fputs("static ss_request_entry ", output_file);
24*7c478bd9Sstevel@tonic-gate     fputs(sym, output_file);
25*7c478bd9Sstevel@tonic-gate     fputs("[] = {\n", output_file);
26*7c478bd9Sstevel@tonic-gate     fputs(rql, output_file);
27*7c478bd9Sstevel@tonic-gate     fputs("    { 0, 0, 0, 0 }\n", output_file);
28*7c478bd9Sstevel@tonic-gate     fputs("};\n\nss_request_table ", output_file);
29*7c478bd9Sstevel@tonic-gate     fputs(hdr, output_file);
30*7c478bd9Sstevel@tonic-gate     fprintf(output_file, " = { %d, ", SS_RQT_TBL_V2);
31*7c478bd9Sstevel@tonic-gate     fputs(sym, output_file);
32*7c478bd9Sstevel@tonic-gate     fputs(" };\n", output_file);
33*7c478bd9Sstevel@tonic-gate }
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate char * generate_cmds_string(cmds)
36*7c478bd9Sstevel@tonic-gate     char const *cmds;
37*7c478bd9Sstevel@tonic-gate {
38*7c478bd9Sstevel@tonic-gate     char * var_name = gensym("ssu");
39*7c478bd9Sstevel@tonic-gate     fputs("static char const * const ", output_file);
40*7c478bd9Sstevel@tonic-gate     fputs(var_name, output_file);
41*7c478bd9Sstevel@tonic-gate     fputs("[] = {\n", output_file);
42*7c478bd9Sstevel@tonic-gate     fputs(cmds, output_file);
43*7c478bd9Sstevel@tonic-gate     fputs(",\n    (char const *)0\n};\n", output_file);
44*7c478bd9Sstevel@tonic-gate     return(var_name);
45*7c478bd9Sstevel@tonic-gate }
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate void generate_function_definition(func)
48*7c478bd9Sstevel@tonic-gate     char const *func;
49*7c478bd9Sstevel@tonic-gate {
50*7c478bd9Sstevel@tonic-gate     fputs("extern void ", output_file);
51*7c478bd9Sstevel@tonic-gate     fputs(func, output_file);
52*7c478bd9Sstevel@tonic-gate     fputs(" __SS_PROTO;\n", output_file);
53*7c478bd9Sstevel@tonic-gate }
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate char * generate_rqte(func_name, info_string, cmds, options)
56*7c478bd9Sstevel@tonic-gate     char const *func_name;
57*7c478bd9Sstevel@tonic-gate     char const *info_string;
58*7c478bd9Sstevel@tonic-gate     char const *cmds;
59*7c478bd9Sstevel@tonic-gate     int options;
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate     int size;
62*7c478bd9Sstevel@tonic-gate     char *string, *var_name, numbuf[16];
63*7c478bd9Sstevel@tonic-gate     var_name = generate_cmds_string(cmds);
64*7c478bd9Sstevel@tonic-gate     generate_function_definition(func_name);
65*7c478bd9Sstevel@tonic-gate     size = 6;		/* "    { " */
66*7c478bd9Sstevel@tonic-gate     size += strlen(var_name)+7; /* "quux, " */
67*7c478bd9Sstevel@tonic-gate     size += strlen(func_name)+7; /* "foo, " */
68*7c478bd9Sstevel@tonic-gate     size += strlen(info_string)+9; /* "\"Info!\", " */
69*7c478bd9Sstevel@tonic-gate     sprintf(numbuf, "%d", options);
70*7c478bd9Sstevel@tonic-gate     size += strlen(numbuf);
71*7c478bd9Sstevel@tonic-gate     size += 4;		/* " }," + NL */
72*7c478bd9Sstevel@tonic-gate     string = malloc(size * sizeof(char *));
73*7c478bd9Sstevel@tonic-gate     strcpy(string, "    { ");
74*7c478bd9Sstevel@tonic-gate     strcat(string, var_name);
75*7c478bd9Sstevel@tonic-gate     strcat(string, ",\n      ");
76*7c478bd9Sstevel@tonic-gate     strcat(string, func_name);
77*7c478bd9Sstevel@tonic-gate     strcat(string, ",\n      ");
78*7c478bd9Sstevel@tonic-gate     strcat(string, info_string);
79*7c478bd9Sstevel@tonic-gate     strcat(string, ",\n      ");
80*7c478bd9Sstevel@tonic-gate     strcat(string, numbuf);
81*7c478bd9Sstevel@tonic-gate     strcat(string, " },\n");
82*7c478bd9Sstevel@tonic-gate     return(string);
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate char *
86*7c478bd9Sstevel@tonic-gate gensym(name)
87*7c478bd9Sstevel@tonic-gate 	char *name;
88*7c478bd9Sstevel@tonic-gate {
89*7c478bd9Sstevel@tonic-gate 	char *symbol;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	symbol = malloc((strlen(name)+6) * sizeof(char));
92*7c478bd9Sstevel@tonic-gate 	gensym_n++;
93*7c478bd9Sstevel@tonic-gate 	sprintf(symbol, "%s%05ld", name, gensym_n);
94*7c478bd9Sstevel@tonic-gate 	return(symbol);
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate /* concatenate three strings and return the result */
98*7c478bd9Sstevel@tonic-gate char *str_concat3(a, b, c)
99*7c478bd9Sstevel@tonic-gate 	register char *a, *b, *c;
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate 	char *result;
102*7c478bd9Sstevel@tonic-gate 	int size_a = strlen(a);
103*7c478bd9Sstevel@tonic-gate 	int size_b = strlen(b);
104*7c478bd9Sstevel@tonic-gate 	int size_c = strlen(c);
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	result = malloc((size_a + size_b + size_c + 2)*sizeof(char));
107*7c478bd9Sstevel@tonic-gate 	strcpy(result, a);
108*7c478bd9Sstevel@tonic-gate 	strcpy(&result[size_a], c);
109*7c478bd9Sstevel@tonic-gate 	strcpy(&result[size_a+size_c], b);
110*7c478bd9Sstevel@tonic-gate 	return(result);
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate /* return copy of string enclosed in double-quotes */
114*7c478bd9Sstevel@tonic-gate char *quote(string)
115*7c478bd9Sstevel@tonic-gate 	register char *string;
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate 	register char *result;
118*7c478bd9Sstevel@tonic-gate 	int len;
119*7c478bd9Sstevel@tonic-gate 	len = strlen(string)+1;
120*7c478bd9Sstevel@tonic-gate 	result = malloc(len+2);
121*7c478bd9Sstevel@tonic-gate 	result[0] = '"';
122*7c478bd9Sstevel@tonic-gate 	strncpy(&result[1], string, len-1);
123*7c478bd9Sstevel@tonic-gate 	result[len] = '"';
124*7c478bd9Sstevel@tonic-gate 	result[len+1] = '\0';
125*7c478bd9Sstevel@tonic-gate 	return(result);
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate #ifndef HAS_STRDUP
129*7c478bd9Sstevel@tonic-gate /* make duplicate of string and return pointer */
130*7c478bd9Sstevel@tonic-gate char *strdup(s)
131*7c478bd9Sstevel@tonic-gate 	register char *s;
132*7c478bd9Sstevel@tonic-gate {
133*7c478bd9Sstevel@tonic-gate 	register int len = strlen(s) + 1;
134*7c478bd9Sstevel@tonic-gate 	register char *new;
135*7c478bd9Sstevel@tonic-gate 	new = malloc(len);
136*7c478bd9Sstevel@tonic-gate 	strncpy(new, s, len);
137*7c478bd9Sstevel@tonic-gate 	return(new);
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate #endif
140