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