xref: /illumos-gate/usr/src/lib/krb5/ss/utils.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 1987, 1988 by MIT Student Information Processing Board
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * For copyright information, see copyright.h.
57c478bd9Sstevel@tonic-gate  */
67c478bd9Sstevel@tonic-gate 
77c478bd9Sstevel@tonic-gate #include <string.h>
87c478bd9Sstevel@tonic-gate #include "copyright.h"
97c478bd9Sstevel@tonic-gate #include "ss_internal.h"	/* includes stdio and string */
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate extern FILE *output_file;
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate char *gensym(), *str_concat3(), *quote();
147c478bd9Sstevel@tonic-gate extern long gensym_n;
157c478bd9Sstevel@tonic-gate 
write_ct(hdr,rql)167c478bd9Sstevel@tonic-gate void write_ct(hdr, rql)
177c478bd9Sstevel@tonic-gate     char const *hdr, *rql;
187c478bd9Sstevel@tonic-gate {
197c478bd9Sstevel@tonic-gate     char *sym;
207c478bd9Sstevel@tonic-gate     sym = gensym("ssu");
217c478bd9Sstevel@tonic-gate     fputs("static ss_request_entry ", output_file);
227c478bd9Sstevel@tonic-gate     fputs(sym, output_file);
237c478bd9Sstevel@tonic-gate     fputs("[] = {\n", output_file);
247c478bd9Sstevel@tonic-gate     fputs(rql, output_file);
257c478bd9Sstevel@tonic-gate     fputs("    { 0, 0, 0, 0 }\n", output_file);
267c478bd9Sstevel@tonic-gate     fputs("};\n\nss_request_table ", output_file);
277c478bd9Sstevel@tonic-gate     fputs(hdr, output_file);
287c478bd9Sstevel@tonic-gate     fprintf(output_file, " = { %d, ", SS_RQT_TBL_V2);
297c478bd9Sstevel@tonic-gate     fputs(sym, output_file);
307c478bd9Sstevel@tonic-gate     fputs(" };\n", output_file);
317c478bd9Sstevel@tonic-gate }
327c478bd9Sstevel@tonic-gate 
generate_cmds_string(cmds)337c478bd9Sstevel@tonic-gate char * generate_cmds_string(cmds)
347c478bd9Sstevel@tonic-gate     char const *cmds;
357c478bd9Sstevel@tonic-gate {
367c478bd9Sstevel@tonic-gate     char * var_name = gensym("ssu");
377c478bd9Sstevel@tonic-gate     fputs("static char const * const ", output_file);
387c478bd9Sstevel@tonic-gate     fputs(var_name, output_file);
397c478bd9Sstevel@tonic-gate     fputs("[] = {\n", output_file);
407c478bd9Sstevel@tonic-gate     fputs(cmds, output_file);
417c478bd9Sstevel@tonic-gate     fputs(",\n    (char const *)0\n};\n", output_file);
427c478bd9Sstevel@tonic-gate     return(var_name);
437c478bd9Sstevel@tonic-gate }
447c478bd9Sstevel@tonic-gate 
generate_function_definition(func)457c478bd9Sstevel@tonic-gate void generate_function_definition(func)
467c478bd9Sstevel@tonic-gate     char const *func;
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate     fputs("extern void ", output_file);
497c478bd9Sstevel@tonic-gate     fputs(func, output_file);
507c478bd9Sstevel@tonic-gate     fputs(" __SS_PROTO;\n", output_file);
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
generate_rqte(func_name,info_string,cmds,options)537c478bd9Sstevel@tonic-gate char * generate_rqte(func_name, info_string, cmds, options)
547c478bd9Sstevel@tonic-gate     char const *func_name;
557c478bd9Sstevel@tonic-gate     char const *info_string;
567c478bd9Sstevel@tonic-gate     char const *cmds;
577c478bd9Sstevel@tonic-gate     int options;
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate     int size;
607c478bd9Sstevel@tonic-gate     char *string, *var_name, numbuf[16];
617c478bd9Sstevel@tonic-gate     var_name = generate_cmds_string(cmds);
627c478bd9Sstevel@tonic-gate     generate_function_definition(func_name);
637c478bd9Sstevel@tonic-gate     size = 6;		/* "    { " */
64*56a424ccSmp     size += strlen(var_name)+8; /* "quux, " */
65*56a424ccSmp     size += strlen(func_name)+8; /* "foo, " */
66*56a424ccSmp     size += strlen(info_string)+8; /* "\"Info!\", " */
677c478bd9Sstevel@tonic-gate     sprintf(numbuf, "%d", options);
68*56a424ccSmp     size += strlen(numbuf)+5;		/* " }," + NL + NUL */
69*56a424ccSmp     string = malloc(size);
707c478bd9Sstevel@tonic-gate     strcpy(string, "    { ");
717c478bd9Sstevel@tonic-gate     strcat(string, var_name);
727c478bd9Sstevel@tonic-gate     strcat(string, ",\n      ");
737c478bd9Sstevel@tonic-gate     strcat(string, func_name);
747c478bd9Sstevel@tonic-gate     strcat(string, ",\n      ");
757c478bd9Sstevel@tonic-gate     strcat(string, info_string);
767c478bd9Sstevel@tonic-gate     strcat(string, ",\n      ");
777c478bd9Sstevel@tonic-gate     strcat(string, numbuf);
787c478bd9Sstevel@tonic-gate     strcat(string, " },\n");
797c478bd9Sstevel@tonic-gate     return(string);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate char *
gensym(name)837c478bd9Sstevel@tonic-gate gensym(name)
847c478bd9Sstevel@tonic-gate 	char *name;
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	char *symbol;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	symbol = malloc((strlen(name)+6) * sizeof(char));
897c478bd9Sstevel@tonic-gate 	gensym_n++;
907c478bd9Sstevel@tonic-gate 	sprintf(symbol, "%s%05ld", name, gensym_n);
917c478bd9Sstevel@tonic-gate 	return(symbol);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /* concatenate three strings and return the result */
str_concat3(a,b,c)957c478bd9Sstevel@tonic-gate char *str_concat3(a, b, c)
967c478bd9Sstevel@tonic-gate 	register char *a, *b, *c;
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	char *result;
997c478bd9Sstevel@tonic-gate 	int size_a = strlen(a);
1007c478bd9Sstevel@tonic-gate 	int size_b = strlen(b);
1017c478bd9Sstevel@tonic-gate 	int size_c = strlen(c);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	result = malloc((size_a + size_b + size_c + 2)*sizeof(char));
1047c478bd9Sstevel@tonic-gate 	strcpy(result, a);
1057c478bd9Sstevel@tonic-gate 	strcpy(&result[size_a], c);
1067c478bd9Sstevel@tonic-gate 	strcpy(&result[size_a+size_c], b);
1077c478bd9Sstevel@tonic-gate 	return(result);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /* return copy of string enclosed in double-quotes */
quote(string)1117c478bd9Sstevel@tonic-gate char *quote(string)
1127c478bd9Sstevel@tonic-gate 	register char *string;
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	register char *result;
1157c478bd9Sstevel@tonic-gate 	int len;
1167c478bd9Sstevel@tonic-gate 	len = strlen(string)+1;
1177c478bd9Sstevel@tonic-gate 	result = malloc(len+2);
1187c478bd9Sstevel@tonic-gate 	result[0] = '"';
1197c478bd9Sstevel@tonic-gate 	strncpy(&result[1], string, len-1);
1207c478bd9Sstevel@tonic-gate 	result[len] = '"';
1217c478bd9Sstevel@tonic-gate 	result[len+1] = '\0';
1227c478bd9Sstevel@tonic-gate 	return(result);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
125*56a424ccSmp #ifndef HAVE_STRDUP
1267c478bd9Sstevel@tonic-gate /* make duplicate of string and return pointer */
strdup(s)1277c478bd9Sstevel@tonic-gate char *strdup(s)
1287c478bd9Sstevel@tonic-gate 	register char *s;
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	register int len = strlen(s) + 1;
1317c478bd9Sstevel@tonic-gate 	register char *new;
1327c478bd9Sstevel@tonic-gate 	new = malloc(len);
1337c478bd9Sstevel@tonic-gate 	strncpy(new, s, len);
1347c478bd9Sstevel@tonic-gate 	return(new);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate #endif
137