10Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
20Sstevel@tonic-gate
30Sstevel@tonic-gate /*
40Sstevel@tonic-gate * Copyright 1987, 1988 by MIT Student Information Processing Board
50Sstevel@tonic-gate *
60Sstevel@tonic-gate * For copyright information, see copyright.h.
70Sstevel@tonic-gate */
80Sstevel@tonic-gate
90Sstevel@tonic-gate #include <string.h>
100Sstevel@tonic-gate #include "copyright.h"
110Sstevel@tonic-gate #include "ss_internal.h" /* includes stdio and string */
120Sstevel@tonic-gate
130Sstevel@tonic-gate extern FILE *output_file;
140Sstevel@tonic-gate
150Sstevel@tonic-gate char *gensym(), *str_concat3(), *quote();
160Sstevel@tonic-gate extern long gensym_n;
170Sstevel@tonic-gate
write_ct(hdr,rql)180Sstevel@tonic-gate void write_ct(hdr, rql)
190Sstevel@tonic-gate char const *hdr, *rql;
200Sstevel@tonic-gate {
210Sstevel@tonic-gate char *sym;
220Sstevel@tonic-gate sym = gensym("ssu");
230Sstevel@tonic-gate fputs("static ss_request_entry ", output_file);
240Sstevel@tonic-gate fputs(sym, output_file);
250Sstevel@tonic-gate fputs("[] = {\n", output_file);
260Sstevel@tonic-gate fputs(rql, output_file);
270Sstevel@tonic-gate fputs(" { 0, 0, 0, 0 }\n", output_file);
280Sstevel@tonic-gate fputs("};\n\nss_request_table ", output_file);
290Sstevel@tonic-gate fputs(hdr, output_file);
300Sstevel@tonic-gate fprintf(output_file, " = { %d, ", SS_RQT_TBL_V2);
310Sstevel@tonic-gate fputs(sym, output_file);
320Sstevel@tonic-gate fputs(" };\n", output_file);
330Sstevel@tonic-gate }
340Sstevel@tonic-gate
generate_cmds_string(cmds)350Sstevel@tonic-gate char * generate_cmds_string(cmds)
360Sstevel@tonic-gate char const *cmds;
370Sstevel@tonic-gate {
380Sstevel@tonic-gate char * var_name = gensym("ssu");
390Sstevel@tonic-gate fputs("static char const * const ", output_file);
400Sstevel@tonic-gate fputs(var_name, output_file);
410Sstevel@tonic-gate fputs("[] = {\n", output_file);
420Sstevel@tonic-gate fputs(cmds, output_file);
430Sstevel@tonic-gate fputs(",\n (char const *)0\n};\n", output_file);
440Sstevel@tonic-gate return(var_name);
450Sstevel@tonic-gate }
460Sstevel@tonic-gate
generate_function_definition(func)470Sstevel@tonic-gate void generate_function_definition(func)
480Sstevel@tonic-gate char const *func;
490Sstevel@tonic-gate {
500Sstevel@tonic-gate fputs("extern void ", output_file);
510Sstevel@tonic-gate fputs(func, output_file);
520Sstevel@tonic-gate fputs(" __SS_PROTO;\n", output_file);
530Sstevel@tonic-gate }
540Sstevel@tonic-gate
generate_rqte(func_name,info_string,cmds,options)550Sstevel@tonic-gate char * generate_rqte(func_name, info_string, cmds, options)
560Sstevel@tonic-gate char const *func_name;
570Sstevel@tonic-gate char const *info_string;
580Sstevel@tonic-gate char const *cmds;
590Sstevel@tonic-gate int options;
600Sstevel@tonic-gate {
610Sstevel@tonic-gate int size;
620Sstevel@tonic-gate char *string, *var_name, numbuf[16];
630Sstevel@tonic-gate var_name = generate_cmds_string(cmds);
640Sstevel@tonic-gate generate_function_definition(func_name);
650Sstevel@tonic-gate size = 6; /* " { " */
66*2881Smp153739 size += strlen(var_name)+8; /* "quux, " */
67*2881Smp153739 size += strlen(func_name)+8; /* "foo, " */
68*2881Smp153739 size += strlen(info_string)+8; /* "\"Info!\", " */
690Sstevel@tonic-gate sprintf(numbuf, "%d", options);
70*2881Smp153739 size += strlen(numbuf)+5; /* " }," + NL + NUL */
71*2881Smp153739 string = malloc(size);
720Sstevel@tonic-gate strcpy(string, " { ");
730Sstevel@tonic-gate strcat(string, var_name);
740Sstevel@tonic-gate strcat(string, ",\n ");
750Sstevel@tonic-gate strcat(string, func_name);
760Sstevel@tonic-gate strcat(string, ",\n ");
770Sstevel@tonic-gate strcat(string, info_string);
780Sstevel@tonic-gate strcat(string, ",\n ");
790Sstevel@tonic-gate strcat(string, numbuf);
800Sstevel@tonic-gate strcat(string, " },\n");
810Sstevel@tonic-gate return(string);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate
840Sstevel@tonic-gate char *
gensym(name)850Sstevel@tonic-gate gensym(name)
860Sstevel@tonic-gate char *name;
870Sstevel@tonic-gate {
880Sstevel@tonic-gate char *symbol;
890Sstevel@tonic-gate
900Sstevel@tonic-gate symbol = malloc((strlen(name)+6) * sizeof(char));
910Sstevel@tonic-gate gensym_n++;
920Sstevel@tonic-gate sprintf(symbol, "%s%05ld", name, gensym_n);
930Sstevel@tonic-gate return(symbol);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate
960Sstevel@tonic-gate /* concatenate three strings and return the result */
str_concat3(a,b,c)970Sstevel@tonic-gate char *str_concat3(a, b, c)
980Sstevel@tonic-gate register char *a, *b, *c;
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate char *result;
1010Sstevel@tonic-gate int size_a = strlen(a);
1020Sstevel@tonic-gate int size_b = strlen(b);
1030Sstevel@tonic-gate int size_c = strlen(c);
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate result = malloc((size_a + size_b + size_c + 2)*sizeof(char));
1060Sstevel@tonic-gate strcpy(result, a);
1070Sstevel@tonic-gate strcpy(&result[size_a], c);
1080Sstevel@tonic-gate strcpy(&result[size_a+size_c], b);
1090Sstevel@tonic-gate return(result);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /* return copy of string enclosed in double-quotes */
quote(string)1130Sstevel@tonic-gate char *quote(string)
1140Sstevel@tonic-gate register char *string;
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate register char *result;
1170Sstevel@tonic-gate int len;
1180Sstevel@tonic-gate len = strlen(string)+1;
1190Sstevel@tonic-gate result = malloc(len+2);
1200Sstevel@tonic-gate result[0] = '"';
1210Sstevel@tonic-gate strncpy(&result[1], string, len-1);
1220Sstevel@tonic-gate result[len] = '"';
1230Sstevel@tonic-gate result[len+1] = '\0';
1240Sstevel@tonic-gate return(result);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate
127*2881Smp153739 #ifndef HAVE_STRDUP
1280Sstevel@tonic-gate /* make duplicate of string and return pointer */
strdup(s)1290Sstevel@tonic-gate char *strdup(s)
1300Sstevel@tonic-gate register char *s;
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate register int len = strlen(s) + 1;
1330Sstevel@tonic-gate register char *new;
1340Sstevel@tonic-gate new = malloc(len);
1350Sstevel@tonic-gate strncpy(new, s, len);
1360Sstevel@tonic-gate return(new);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate #endif
139