xref: /titanic_50/usr/src/lib/libbsm/auditxml (revision e07d9cb85217949d497b02d7211de8a197d2f2eb)
1#!/usr/perl5/bin/perl -w
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22#
23# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26# ident	"%Z%%M%	%I%	%E% SMI"
27#
28
29# auditxml [-d] <xml input file>
30
31# auditxml takes the audit record description (.xml file) and
32# generates the files needed for the C audit api.
33
34use auditxml;
35use Getopt::Std;
36use vars qw($opt_d);
37use strict;
38
39
40our $debug = 0; # normal use is to set via the file being parsed.
41               # <debug set="on"/> or <debug set="off"/> or <debug/>
42               # if the set attribute is omitted, debug state is toggled
43               # Override with appDebug, but toggle won't do what you
44               # want.
45my $appDebug = 0; # used after return from "new auditxml";
46
47my $genNotice = "
48DO NOT EDIT. This file is auto generated by the Solaris Audit
49system from adt.xml.
50
51See http://opensolaris.org/os/project/audit/
52";
53
54# trim leading/trailing newlines
55$genNotice =~ s/^\n//s;
56$genNotice =~ s/\n$//s;
57my $prog = $0; $prog =~ s|.*/||g;
58my $usage = "usage: $prog [-d] file.xml\n";
59
60getopts('d');
61
62$appDebug = $opt_d;
63
64my $uniLabel = "adr";
65my $xlateUniLabelInc = 0;
66
67die $usage if ($#ARGV < 0);
68
69# where everything comes from and where it goes:
70
71my $bsmBuildPath = "./common";
72my $xlateFile = "$bsmBuildPath/adt_xlate.c";
73my $headerFile = "$bsmBuildPath/adt_event_N.h";
74
75my $doc = new auditxml ($ARGV[0]);  # input XML file
76
77$debug = $appDebug;
78
79my %xlateEventTable = ();
80my @xlateTypeList = ();
81my %xlateTypeList = ();
82my %eventAPI = ();
83my %eventExtra = ();
84my %headers = ();
85my %externalIdNo = ();
86my @outputState = ();
87my %nameTranslation = ();
88my @xlateDefaults = ();
89my %xlateDefault = ();
90my %msg_list = ();
91
92my $event;
93while ($event = $doc->getNextEvent()) {
94    my $eventId = $event->getId();
95    my $eventHeader = $event->getHeader();
96    my $idNo = $event->getIdNo();
97    $externalIdNo{$eventId} = $idNo;
98    addHeader($eventHeader) if defined ($eventHeader);
99    my $super;
100    my $omit = $event->getOmit();
101    my $eventType = '';
102    if ($super = $event->getSuperClass()) {
103	$event = $super;
104	$eventType = 'instance';
105    } else {
106	$eventType = $event->getType();
107    }
108
109    # header file for API use
110    generateAPIFile($event, $eventId, $eventType, $eventHeader, $idNo)
111        unless $omit eq 'always';
112
113    # c file table for translation
114    generateTableC($event, $eventId, $eventType, $eventHeader, $omit);
115}
116
117my $textList;
118while ($textList = $doc->getNextMsgId()) {
119    generateMsgLists($textList);  # enum -> text mappings
120}
121
122printTableC($xlateFile);
123printAPIFile($headerFile, $doc);
124
125exit 0;
126
127
128sub printTableC {
129    my $file = shift;
130
131    unless (open(Cfile, ">$file")) {
132	print STDERR "can't open output file ($file): $!\n";
133	return;
134    }
135
136    my $notice = $genNotice;
137    $notice =~ s/\n/\n * /gs;
138    $notice =~ s/\s+\n/\n/gs;
139    print Cfile <<EOF;
140/*
141 * $notice
142 */
143
144#include <bsm/libbsm.h>
145#include <adt_xlate.h>
146#include <libintl.h>
147
148EOF
149    print Cfile "#ifndef _PRAUDIT\n";
150    print Cfile "/* Internal data type definitions */\n\n";
151    my $extDef;
152    foreach $extDef (@xlateTypeList) {
153      print Cfile "static $extDef\n";
154    }
155    @xlateTypeList = ();
156
157    print Cfile "\n/* External event structure to internal event structure */\n\n";
158
159    my @pointers = ();
160
161    foreach my $eventId (sort keys %xlateEventTable) {
162	if ($xlateEventTable{$eventId}) {
163	    my ($ref1, $eventType, $firstToken, $eventHeader) =
164	      @{$xlateEventTable{$eventId}};
165	    my @entries = @$ref1;
166	    my $entry;
167	    my $entries = $#entries;
168	    my $count = $entries + 1;
169	    my $externalName = $nameTranslation{$eventId};
170	    my $externalRoot = $externalName;
171	    $externalRoot =~ s/AUE_//;
172	    my $structName = "XX_$externalRoot";
173	    my $root = $eventId;
174	    $root =~ s/AUE_//;
175	    my $externalId = $eventId;
176	    $externalId =~ s/AUE_/ADT_/;
177
178	    unless ($eventType eq 'generic') {
179		print Cfile "static struct entry $structName\[$count\] = {\n";
180		foreach $entry (@entries) {
181		    if ($entries--) {
182			$entry =~ s/EOL/,/;
183		    }
184		    else {
185			$entry =~ s/EOL//;
186		    }
187		    $entry =~ s/selfReference/$structName/;
188		    print Cfile "\t$entry\n";
189		}
190		print Cfile "};\n";
191
192		print Cfile "static struct translation X_$externalRoot = {\n";
193		push (@pointers, "X_$externalRoot");
194
195		print Cfile "\t0,\n";   # tx_offsetsCalculated = 0
196		print Cfile "\t$externalId,\n";
197		print Cfile "\t$externalName,\n";
198
199		print Cfile "\t$count,\n";
200		print Cfile "\t&XX_$externalRoot\[$firstToken\],\n";
201		print Cfile "\t&XX_$externalRoot\[0\]\n};\n";
202	    }
203	} else {
204	    print STDERR "expected entry for $eventId but none found\n";
205	}
206    }
207
208    my $count = $#pointers + 2;
209    print Cfile "struct translation *xlate_table[$count] = {\n";
210
211    my $firstEvent = 1;
212    foreach my $eventId (@pointers) {
213	if ($firstEvent) {
214	    $firstEvent = 0;
215	}
216	else {
217	    print Cfile ",\n";
218	}
219	print Cfile "\t&$eventId";
220    }
221    print Cfile ",\n\tNULL\n};\n";
222
223    # generate the adt_preload() function
224
225    print Cfile <<EOF;
226
227void
228adt_preload(au_event_t event_id, adt_event_data_t *event_data)
229{
230	switch (event_id) {
231EOF
232
233        foreach my $id (@xlateDefaults) {
234		my $adtID = $id;
235		$adtID =~ s/AUE/ADT/;
236
237		print Cfile <<EOF;
238	case $adtID:
239EOF
240		my @preloads = @{$xlateDefault{$id}};
241		while (@preloads) {
242			my $fieldName = shift @preloads;
243			my $default = shift @preloads;
244			my $lcid = lc $id;
245			$lcid =~ s/aue_/adt_/;
246
247			print Cfile <<EOF;
248		event_data->$lcid.$fieldName = $default;
249EOF
250		}
251
252		print Cfile <<EOF;
253		break;
254EOF
255	}
256
257    print Cfile <<EOF;
258	default:
259		break;
260	}
261}
262#endif
263
264/* message lists */
265
266EOF
267    my $listName;
268    my @listName;
269    foreach $listName (sort keys %msg_list) {
270        my ($listRef, $headref) = @{$msg_list{$listName}};
271	my ($header, $start, $public, $deprecated) = @$headref;
272
273	my @listValue =  @$listRef;
274	my $listValue;
275	my $listLength = $#listValue + 1;
276
277	$listName = 'NULL' if ($#listValue < 0);
278
279        push (@listName, [$listName, $listLength - 1, $start, $public]);
280
281	next if ($#listValue < 0);
282
283	print Cfile "/* Deprecated message list */\n" if ($deprecated);
284	print Cfile "static char *msg_$listName\[$listLength] = {\n";
285
286	my $ffirst = 1;
287	foreach $listValue (@listValue) {
288	    print Cfile ",\n" unless $ffirst;
289	    $ffirst = 0;
290	    my ($id, $text) = split(/\s*::\s*/, $listValue);
291	    if ($text) {
292	        print Cfile "\t\"$text\"";
293	    }
294	    else {
295	        print Cfile "\tNULL";
296	    }
297	}
298	print Cfile "\n};\n";
299    }
300    print Cfile "\nstruct msg_text adt_msg_text[", $#listName + 1,
301                "] = {\n";
302    my $ffirst = 1;
303    foreach $listName (@listName) {
304        my ($name, $max, $start) = @$listName;
305	$start = -$start if $start;
306        print Cfile ",\n" unless $ffirst;
307	$ffirst = 0;
308	$name = "msg_$name" if ($name ne 'NULL');
309        print Cfile "\t{0, $max, $name, $start}";
310    }
311    print Cfile "\n};\n";
312
313    close Cfile;
314}
315
316sub printAPIFile {
317    my $file = shift;
318    my $xmlDoc = shift;
319
320    my @Hfile;
321    @Hfile = openHeaderFiles($file);
322
323    my $notice = $genNotice;
324    $notice =~ s/\n/\n * /gs;
325    $notice =~ s/\s+\n/\n/gs;
326
327    foreach my $header (keys %headers) {
328    	next unless $Hfile[$header];
329	*Hfile = $Hfile[$header];
330	my $include = "adt.h";
331	my $adt_event_n = "_ADT_EVENT_H";
332	if ($header > 0) {
333	    $include = "adt_event.h";
334	    $adt_event_n = "_ADT_EVENT_".$header."_H";
335	}
336	print Hfile <<EOF;
337/*
338 * $notice
339 */
340
341#ifndef $adt_event_n
342#define	$adt_event_n
343
344#include <bsm/$include>
345
346#ifdef	__cplusplus
347extern "C" {
348#endif
349
350/*
351 * adt_put_event() status values.  Positive values are for kernel-generated
352 * failure, -1 for user-space.  For ADT_SUCCESS, the adt_put_event() return_val
353 * is not used; the convention is to set it to ADT_SUCCESS.
354 */
355#define	ADT_SUCCESS	0
356#define	ADT_FAILURE	-1
357
358EOF
359    }
360
361    foreach my $listName (sort keys %msg_list) {
362	my $shortName = uc $listName;
363	$shortName =~ s/_TEXT//;
364
365        my ($listRef, $headref) = @{$msg_list{$listName}};
366	my ($header, $start, $public, $deprecated) = @$headref;
367	next unless $Hfile[$header];
368	*Hfile = $Hfile[$header];
369
370	print Hfile "/* Deprecated message list */\n" if $deprecated;
371	print Hfile "#define\tADT_$shortName\t$start\n" if $start;
372
373	my @listValue =  @$listRef;
374	next unless ($#listValue >= 0);
375	print Hfile "enum\tadt_$listName", " {\n";
376
377	my $listValue;
378	my $i = 0;
379	my $j = $#listValue;
380	my $comma = ',';
381	foreach $listValue (@listValue) {
382	    my ($id, $text) = split(/\s*::\s*/, $listValue);
383	    $comma = '' if $i++ == $j;
384	    if ($start) {
385		$start = " = $start$comma";
386	    } else {
387	        $start = "$comma\t";
388	    }
389	    $text = "(no token will be generated)" unless $text;
390	    print Hfile "\tADT_$shortName", "_$id$start\t/* $text */\n";
391	    $start = '';
392	}
393	print Hfile "};\n";
394    }
395
396    # generate defines for ADT_* external event names
397
398    foreach my $eventId (sort keys %eventAPI) {
399        my ($header, $idNo) = @{$eventExtra{$eventId}};
400	unless (defined ($header)) {
401	    print STDERR "missing header selection for $eventId\n";
402	    next;
403	}
404	*Hfile = $Hfile[$header];
405	next unless $Hfile[$header];
406
407	my $l = length($eventId) + 8; # label plus preceding #define\t
408	$l = 5 - int(($l + 8)/8);
409	$l = 1 if $l < 1;
410	my $tab = "\t" x $l;
411
412        print STDERR "missing id number for $eventId\n" unless $idNo;
413
414	$eventId =~ s/AUE_/ADT_/;
415	print Hfile "#define\t$eventId$tab$idNo\n";
416    }
417
418
419    # generate per-event structures
420
421    foreach my $eventId (sort keys %eventAPI) {
422        my ($header, $idNo) = @{$eventExtra{$eventId}};
423	my $dataId = $eventId;
424	$dataId =~ s/^AUE_/adt_/;
425	unless(defined ($header)) {
426	    print STDERR "$eventId is missing the header assignment\n";
427	    next;
428	}
429	*Hfile = $Hfile[$header];
430	next unless $Hfile[$header];
431
432	my $externalId = $eventId;
433	$externalId =~ s/AUE_/ADT_/;
434
435	print Hfile "\nstruct $dataId {\t/* $externalId */\n";
436
437	my @entries = @{$eventAPI{$eventId}};
438	my $entry;
439	if ($#entries < 0) {
440	    print Hfile "\tint\tdummy;\t/* not used */\n";
441	} else {
442	    foreach $entry (@entries) {
443		$entry =~ s/termid/adt_termid_t/;
444		print Hfile "\t$entry\n";
445	    }
446	}
447	print Hfile "};\n";
448	$eventId =~ s/^AUE_/adt_/;
449	print Hfile "typedef struct $dataId $eventId","_t;\n";
450    }
451
452    foreach my $header (sort keys %headers) {
453	$outputState[$header] = 0;
454    }
455
456    foreach my $eventId (sort keys %eventAPI) {
457        my ($header, $idNo) = @{$eventExtra{$eventId}};
458	unless(defined ($header)) {
459	    # don't print duplicate error message
460	    next;
461	}
462	*Hfile = $Hfile[$header];
463	next unless $Hfile[$header];
464	if ($outputState[$header] == 0) {
465	    $outputState[$header] = 1;
466	    my $suffix = '';
467	    $suffix = "_$header" if $header;
468	    print Hfile "\nunion adt_event_data$suffix {\n";
469	}
470        my $elementName = $eventId;
471	$elementName =~ s/^AUE_/adt_/;
472	$eventId =~ s/^AUE_/adt_/;
473	$elementName =~ s/_t$//;
474
475	print Hfile "\t\t$eventId","_t\t$elementName;\n";
476    }
477    foreach my $header (sort keys %headers) {
478	if ($outputState[$header]) {
479	    *Hfile = $Hfile[$header];
480	    next unless $Hfile[$header];
481	    print Hfile "};\n";
482	}
483    }
484    foreach my $header (keys %headers) {
485    	next unless $Hfile[$header];
486	*Hfile = $Hfile[$header];
487	my $adt_event_n = "_ADT_EVENT_H";
488	if ($header > 0) {
489	    $adt_event_n = "_ADT_EVENT_".$header."_H";
490	}
491	print Hfile <<EOF;
492
493
494#ifndef	ADT_PRIVATE
495#define	ADT_PRIVATE
496
497/*
498 * These interfaces are project private and will change without
499 * notice as needed for the BSM API project.
500 */
501
502extern	void	adt_get_auid(const adt_session_data_t *, au_id_t *);
503extern	void	adt_set_auid(const adt_session_data_t *, const au_id_t);
504
505extern	void	adt_get_mask(const adt_session_data_t *, au_mask_t *);
506extern	void	adt_set_mask(const adt_session_data_t *, const au_mask_t *);
507
508extern	void	adt_get_termid(const adt_session_data_t *, au_tid_addr_t *);
509extern	void	adt_set_termid(const adt_session_data_t *,
510    const au_tid_addr_t *);
511
512extern	void	adt_get_asid(const adt_session_data_t *, au_asid_t *);
513extern	void	adt_set_asid(const adt_session_data_t *, const au_asid_t);
514
515#endif
516
517#ifdef	__cplusplus
518}
519#endif
520
521#endif	/* $adt_event_n */
522EOF
523    }
524    closeHeaderFiles(@Hfile);
525}
526
527sub generateTableC {
528    my $event = shift;
529    my $eventId = shift;
530    my $eventType = shift;
531    my $eventHeader = shift;
532    my $omit = shift;
533
534    my %tokenType = (
535		  'acl'			=> 'AUT_ACL',
536		  'arbitrary'		=> 'AUT_ARBITRARY',
537		  'arg'			=> 'AUT_ARG',
538		  'attr'		=> 'AUT_ATTR',
539		  'command'		=> 'AUT_CMD',
540		  'command_1'		=> 'ADT_CMD_ALT',	# dummy token id
541		  'date'		=> 'AUT_TEXT',
542		  'exec_args'   	=> 'AUT_EXEC_ARGS',
543		  'exec_env'    	=> 'AUT_EXEC_ENV',
544		  'exit'        	=> 'AUT_EXIT',
545		  'file'        	=> 'AUT_FILE',
546		  'fmri'        	=> 'AUT_FMRI',
547		  'groups'      	=> 'AUT_GROUPS',
548	#	  'header'      	=> 'AUT_HEADER',	# not used
549		  'in_addr'     	=> 'AUT_IN_ADDR',
550		  'tid'          	=> 'AUT_TID',
551		  'ipc'         	=> 'AUT_IPC',
552		  'ipc_perm'    	=> 'AUT_IPC_PERM',
553		  'iport'		=> 'AUT_IPORT',
554		  'label'		=> 'AUT_LABEL',
555		  'newgroups'   	=> 'AUT_NEWGROUPS',
556		  'opaque'      	=> 'AUT_OPAQUE',
557		  'path'        	=> 'AUT_PATH',
558		  'path_list'		=> '-AUT_PATH',		# dummy token id
559		  'process'     	=> 'AUT_PROCESS',
560		  'priv_effective'	=> 'ADT_AUT_PRIV_E',	# dummy token id
561		  'priv_limit'		=> 'ADT_AUT_PRIV_L', 	# dummy token id
562		  'priv_inherit'	=> 'ADT_AUT_PRIV_I',	# dummy token id
563		  'return'      	=> 'AUT_RETURN',
564		  'seq'         	=> 'AUT_SEQ',
565		  'socket'      	=> 'AUT_SOCKET',
566		  'socket-inet' 	=> 'AUT_SOCKET_INET',
567		  'subject'     	=> 'AUT_SUBJECT',
568		  'text'        	=> 'AUT_TEXT',
569	#	  'trailer'     	=> 'AUT_TRAILER',	# not used
570		  'uauth'		=> 'AUT_UAUTH',
571		  'zonename'		=> 'AUT_ZONENAME'
572		 );
573
574    my @xlateEntryList = ();
575
576    my $external = $event->getExternal();
577    my $internal = $event->getInternal();
578
579    unless ($external) {
580	print STDERR "No external object captured for event $eventId\n";
581	return;
582    }
583    if ($eventType) {
584	$nameTranslation{$eventId} = $eventId;
585    } else {
586	$nameTranslation{$eventId} = $external->getInternalName();
587    }
588    unless ($internal) {
589	print STDERR "No internal object captured for event $eventId\n";
590	return;
591    }
592    my @entryRef = $internal->getEntries();
593    my $entryRef;
594    my @tokenOrder = ();
595    my $firstTokenIndex = 0; # djdj not used yet, djdj BUG!
596    			     # needs to be used by translate table
597
598    if ($internal->isReorder()) { # prescan the entry list to get the token order
599      my @inputOrder;
600      foreach $entryRef (@entryRef) {
601	my ($intEntry, $entry) = @$entryRef;
602	push (@inputOrder, $intEntry->getAttr('order'));
603      }
604
605      my $i; # walk down the inputOrder list once
606      my $k = 1; # discover next in line
607      my $l = 0; # who should point to next in line
608      for ($i = 0; $i <= $#inputOrder; $i++) {
609	my $j;
610	for ($j = 0; $j <= $#inputOrder; $j++) {
611	  if ($k == $inputOrder[$j]) {
612	    if ($k == 1) {
613	        $firstTokenIndex = $j;
614	    } else {
615	        $tokenOrder[$l] = "&(selfReference[$j])";
616	    }
617	    $l = $j;
618	    last;
619	  }
620	}
621	$k++;
622      }
623      $tokenOrder[$l] = 'NULL';
624    }
625    else { # default order -- input order same as output
626      my $i;
627      my $j;
628      for ($i = 0; $i < $#entryRef; $i++) {
629	my $j = $i + 1;
630	$tokenOrder[$i] = "&(selfReference[$j])";
631      }
632      $tokenOrder[$#entryRef] = 'NULL';
633    }
634
635    my $sequence = 0;
636    foreach $entryRef (@entryRef) {
637      my ($intEntry, $entry) = @$entryRef;
638      my $entryId = $entry->getAttr('id');
639
640      my ($extEntry, $unusedEntry, $tokenId) =
641	$external->getEntry($entryId);
642      my $opt = $extEntry->getAttr('opt');
643
644      if ($opt eq 'none') {
645	if (defined ($doc->getToken($tokenId))) {
646	  if (defined ($tokenType{$tokenId})) {
647	    $tokenId = $tokenType{$tokenId};
648	  }
649	  else {
650	    print STDERR "token id $tokenId not implemented\n";
651	  }
652	}
653	else {
654	  print STDERR "token = $tokenId is undefined\n";
655	  $tokenId = 'error';
656	}
657	my ($xlate, $jni) =
658	  formatTableEntry ('', $tokenId, $eventId, '', 0, 0, $tokenOrder[$sequence],
659			    'NULL', '');
660	push (@xlateEntryList, $xlate);
661      }
662      else {
663	my $dataType = $extEntry->getAttr('type');
664	$dataType =~ s/\s+//g;   # remove blanks (char * => char*)
665
666	my $enumGroup = '';
667	if ($dataType =~ /^msg/i) {
668	    $enumGroup = $dataType;
669	    $enumGroup =~ s/^msg\s*//i;
670	    $enumGroup = 'adt_' . $enumGroup;
671	}
672	my $required = ($opt eq 'required') ? 1 : 0;
673	my $tsol = 0;
674	my $tokenId = $intEntry->getAttr('token');
675	my $token;
676	my $tokenName;
677	my $tokenFormat = $intEntry->getAttr('format');
678	if (defined ($tokenFormat)) {
679	  $tokenFormat = "\"$tokenFormat\"";
680	}
681	else {
682	  $tokenFormat = 'NULL';
683	}
684
685	if (defined ($token = $doc->getToken($tokenId))) {
686	  $tsol = (lc $token->getUsage() eq 'tsol') ? 1 : 0;
687	  if (defined ($tokenType{$tokenId})) {
688	    $tokenName = $tokenType{$tokenId};
689	  }
690	  else {
691	    print STDERR "token id $tokenId not implemented\n";
692	  }
693	}
694	else {
695	  print STDERR
696	    "$tokenId is an unimplemented token ($entryId in $eventId)\n";
697	  $tokenName = 'AUT_TEXT';
698	}
699	my ($xlate, $jni) =
700	  formatTableEntry($entryId, $tokenName, $eventId, $dataType, $required,
701			   $tsol, $tokenOrder[$sequence], $tokenFormat,
702			   $enumGroup, (uc $omit eq 'JNI'));
703	push (@xlateEntryList, $xlate);
704      }
705      $sequence++;
706    }
707    $xlateEventTable{$eventId} = [\@xlateEntryList, $eventType, $firstTokenIndex,
708				 $eventHeader];
709}
710
711sub formatTableEntry {
712    my ($id, $token, $eventId, $type, $required, $tsol, $sequence, $format, $enumGroup,
713	$omitJNI) = @_;
714
715
716    # does this map belong in the xml source?  (at least the defaults?)
717    # fill in the default value only if it is other than zero.
718    #		      base type		    adt name,	default value
719    my %entryDef = ( 'au_asid_t'       	=> ['ADT_UINT32',	''],
720		     'uint_t'		=> ['ADT_UINT32',      	''],
721		     'int'		=> ['ADT_INT',		''],
722		     'int32_t'		=> ['ADT_INT32',	''],
723		     'uid_t'		=> ['ADT_UID',		'AU_NOAUDITID'],
724		     'gid_t'		=> ['ADT_GID',		'AU_NOAUDITID'],
725		     'uid_t*'		=> ['ADT_UIDSTAR',	''],
726		     'gid_t*'		=> ['ADT_GIDSTAR',	''],
727		     'char'		=> ['ADT_CHAR',		''],
728		     'char*'		=> ['ADT_CHARSTAR',	''],
729		     'char**'		=> ['ADT_CHAR2STAR',	''],
730		     'long'		=> ['ADT_LONG',		''],
731		     'pid_t'		=> ['ADT_PID',		''],
732		     'priv_set_t*'	=> ['ADT_PRIVSTAR',	''],
733		     'ulong_t'		=> ['ADT_ULONG',	''],
734		     'uint16_t',	=> ['ADT_UINT16',	''],
735		     'uint32_t'		=> ['ADT_UINT32',	''],
736		     'uint32_t*'	=> ['ADT_UINT32STAR',	''],
737		     'uint32_t[]'	=> ['ADT_UINT32ARRAY',  ''],
738		     'uint64_t'		=> ['ADT_UINT64',	''],
739		     'uint64_t*'	=> ['ADT_UINT64STAR',	''],
740		     'm_label_t*'	=> ['ADT_MLABELSTAR',	''],
741		    );
742    my $xlateLabel = $uniLabel.$xlateUniLabelInc;
743    my $xlateLabelInc = 0;
744    my $xlateLine = '';
745    my @jniLine = ();
746
747	# the list handling should be a simple loop with a loop of one
748        # falling out naturally.
749
750    unless ($type =~ /,/) {	# if list, then generate sequence of entries
751      my $dataType;
752      my $dataSize;
753      my $xlateLabelRef = '';
754
755      my $arraySize = '';
756      $arraySize = $1 if ($type =~ s/\[(\d+)\]/[]/);
757
758      my $entryType = ${$entryDef{$type}}[0];
759
760      my @xlateType = ();	# for adt_xlate.c
761      my $typeCount = 1;
762
763      if ($entryType) {
764	$dataType = $entryType;
765	$type =~ s/([^*]+)\s*(\*+)/$1 $2/;
766	$type =~ s/\[\]//;
767	$dataSize = "sizeof ($type)";
768	if ($arraySize) {
769		$dataSize = "$arraySize * " . $dataSize;
770	}
771	$xlateLine = "{{$dataType, $dataSize}}";
772	push (@jniLine, [$id, $dataType, $format, $enumGroup, $required]);
773      } elsif ($type eq '') {
774	  $xlateLabelRef = 'NULL';
775      } elsif ($type =~ /^msg/i) {
776	$type =~ s/^msg//i;
777	$dataType = 'ADT_MSG';
778	my $dataEnum = 'ADT_LIST_' . uc $type;
779	$xlateLine = "{{$dataType, $dataEnum}}";
780	push (@jniLine, [$id, $dataType, $format, $enumGroup, $required]);
781      } elsif ($type =~ /time_t/i) {
782	$dataType = 'ADT_DATE';
783	$dataSize = "sizeof (time_t)";
784	$xlateLine = "{{$dataType, $dataSize}}";
785	push (@jniLine, [$id, $dataType, $format, $enumGroup, $required]);
786      } elsif ($type =~ /termid/i) {
787	$dataType = 'ADT_TERMIDSTAR';
788	$dataSize = "sizeof (au_tid_addr_t *)";
789	$xlateLine = "{{$dataType, $dataSize}}";
790	push (@jniLine, [$id, $dataType, $format, $enumGroup, $required]);
791      } elsif ($omitJNI) {
792	$xlateLabelRef = 'NULL';
793      } else {
794	print STDERR "$type is not an implemented data type\n";
795	$xlateLabelRef = 'NULL';
796      }
797      if ($xlateLine && !($xlateTypeList{$xlateLine})) {
798	$xlateTypeList{$xlateLine} = $xlateLabel;
799	push (@xlateTypeList, "datadef\t$xlateLabel\[1\] =\t$xlateLine;");
800	$xlateLabelInc = 1;
801      } else {
802	$xlateLabel = $xlateTypeList{$xlateLine};
803      }
804      $xlateLabelRef = '&' . $xlateLabel . '[0]'
805	unless $xlateLabelRef eq 'NULL';
806
807      # "EOL" is where a comma should go unless end of list
808      $xlateLine = "{$token,\t1,\t$xlateLabelRef,\t$sequence,\n" .
809	  "\t\t0,\t$required,\t$tsol,\t$format}EOL";
810
811      if (${$entryDef{$type}}[1]) {
812	  my @list = ();
813	  if ($xlateDefault{$eventId}) {
814	      @list = @{$xlateDefault{$eventId}};
815	  } else {
816	      push (@xlateDefaults, $eventId);
817	  }
818	  push (@list, $id, ${$entryDef{$type}}[1]);
819	  $xlateDefault{$eventId} = \@list;
820      }
821    } else {	# is a list
822      my @type = split(/,/, $type);
823      my @arraySize = ();
824      my @id   = split(/,/, $id);
825      my @jniId  = @id;
826      my $dataType;
827      my $typeCount = ($#type + 1);
828      my @xlateType = ();
829      my @default = ();
830
831      foreach my $dtype (@type) {
832	my $jniId = shift @jniId;
833	my $id = shift @id;
834	my $arraySize = '';
835	$arraySize = $1 if ($dtype =~ s/\[(\d+)\]/[]/);
836
837	my $entryType = ${$entryDef{$dtype}}[0];
838	if ($entryType) {
839	  my $type = $dtype;
840	  $type =~ s/([^*]+)\s*(\*+)/$1 $2/;
841	  $type =~ s/\[\]//;
842
843	  my $sizeString = "sizeof";
844	  $sizeString = "$arraySize * " . $sizeString if $arraySize;
845	  push (@xlateType, "\{$entryType, $sizeString ($type)\}");
846	  push (@jniLine, [$jniId, $entryType, $format, $enumGroup, $required]);
847	} elsif ($type =~ /^msg/i) {
848	  $type =~ s/^msg//i;
849	  $dataType = 'ADT_MSG';
850	  my $dataEnum = 'ADT_LIST_' . uc $type;
851	  push (@xlateType, "\{$dataType, $dataEnum\}};");
852	  push (@jniLine, [$jniId, $dataType, $format, $enumGroup, $required]);
853	} elsif ($type =~ /time_t/i) {
854	  $dataType = 'ADT_DATE';
855	  push (@xlateType, "\{$entryType, sizeof ($type)\}");
856	  push (@jniLine, [$jniId, $entryType, $format, $enumGroup, $required]);
857	} elsif ($type =~ /termid/i) {
858	  $dataType = 'ADT_TERMIDSTAR';
859	  push (@xlateType, "\{$dataType, sizeof (au_tid_addr_t *)\}");
860	  push (@jniLine, [$jniId, $dataType, $format, $enumGroup, $required]);
861	} elsif ($omitJNI) {
862	  # nothing to do.
863	} else {
864	  print STDERR "$dtype is not an implemented data type\n";
865	}
866	if (${$entryDef{$dtype}}[1]) {
867	  push (@default, $id, ${$entryDef{$dtype}}[1]);
868	}
869      }
870      my $xlateArray = "\[$typeCount\] =\t{" . join(",\n\t\t\t\t", @xlateType) . "};";
871
872      unless ($xlateTypeList{$xlateArray}) {
873	$xlateTypeList{$xlateArray} = $xlateLabel;
874	$xlateArray = "datadef\t$xlateLabel" . $xlateArray;
875	push (@xlateTypeList, $xlateArray);
876	$xlateLabelInc = 1;
877      } else {
878	$xlateLabel = $xlateTypeList{$xlateArray};
879      }
880      $xlateLine =
881	"{$token,\t$typeCount,\t&$xlateLabel\[0\],\t$sequence,\n" .
882        "\t\t0,\t$required,\t$tsol,\t$format}EOL";
883      if (@default) {
884	  my @list = ();
885	  if ($xlateDefault{$eventId}) {
886	      @list = @{$xlateDefault{$eventId}};
887	  } else {
888	      push (@xlateDefaults, $eventId);
889	  }
890	  push (@list, @default);
891	  $xlateDefault{$eventId} = \@list;
892      }
893    }
894    $xlateUniLabelInc++ if $xlateLabelInc;
895    return ($xlateLine, \@jniLine);
896}
897
898sub generateAPIFile {
899    my $event = shift;
900    my $eventId = shift;
901    my $eventType = shift;
902    my $eventHeader = shift;
903    my $idNo = shift;
904
905    my @entryList = ();
906
907    my $external = $event->getExternal();
908
909    if ($eventType && $debug) {
910	print STDERR "event $eventId is of type $eventType\n";
911    }
912
913    return unless $external;
914
915    my ($extEntry, $entry, $tokenId, $format);
916    while (($extEntry, $entry, $tokenId, $format) = $external->getNextEntry()) {
917	last unless $entry;
918	my $entryId = $entry->getAttr('id');
919
920	unless (defined $entryId) {
921	    print STDERR "undefined entry id for external $eventId\n";
922	    next;
923	}
924	my $option = $extEntry->getAttr('opt');
925	next if ($option eq 'none');
926
927	if (defined (my $token = $doc->getToken($tokenId))) {
928	  $option = 'Trusted Solaris only'
929	    if (lc $token->getUsage() eq 'tsol') ? 1 : 0;
930	}
931	$option .= " (format: $format)" if $format;
932
933	my $dataType = $extEntry->getAttr('type');
934	unless (defined $dataType) {
935	  print STDERR "no type defined for external tag for $eventId\n";
936	  $dataType = "error";
937	}
938
939	my $comment = $entry->getContent();
940
941	if (($dataType =~ /,/) || ($entryId =~ /,/)) {
942	  my @type = split(/\s*,\s*/, $dataType);
943	  my @id   = split(/\s*,\s*/, $entryId);
944	  if ($#type != $#id) {
945	    print STDERR
946	      "number of data types ($dataType) does not match number of ids ($entryId)",
947	      " for event $eventId\n";
948	    if ($#type < $#id) {
949	      $#id = $#type;
950	    }
951	    else {
952	      $#type = $#id;
953	    }
954	  }
955
956	  my $i;
957	  my $line = '';
958	  $line = "/* $comment */\n\t" if defined $comment;
959	  for ($i = 0; $i <= $#type; $i++) {
960	    my ($primitive, $dereference) =
961	        ($type[$i] =~ /([^\*]+)\s*(\**)/);
962	    $id[$i] .= $1 if ($primitive =~ s/(\[\d+\])//);
963	    $line .= "$primitive\t$dereference$id[$i];\t/*  $option  */";
964	    push (@entryList, $line);
965	    $line = '';
966	  }
967	}
968	else {
969	  my $line = '';
970	  $line = "/* $comment */\n\t" if defined $comment;
971	  if ($dataType =~ /^msg/i) {
972	      $dataType =~ s/^msg\s*//i;
973	      $line .= "enum adt_$dataType" . "\t$entryId;\t/*  $option  */";
974	  }
975	  elsif ($dataType =~ /time_t/i) {
976	      $line .= "time_t\t$entryId;\t/* $option */";
977	  }
978	  else {
979	    my ($primitive, $dereference) =
980	        ($dataType =~ /([^\*]+)\s*(\**)/);
981	    $entryId .= $1 if ($primitive =~ s/(\[\d+\])//);
982	    $line .= "$primitive\t$dereference$entryId;\t/* $option */";
983	  }
984	  push (@entryList, $line);
985	}
986    }
987    $eventExtra{$eventId} = [$eventHeader, $idNo];
988    $eventAPI{$eventId} = \@entryList;
989}
990
991sub generateMsgLists {
992    my $textList = shift;
993
994    my $textName = $textList->getId();
995    my $header = $textList->getHeader();
996    my $start = $textList->getMsgStart();
997    my $public = $textList->getMsgPublic();
998    my $deprecated = $textList->getDeprecated();
999
1000    addHeader($header);
1001    print "$textName starts at $start\n" if $debug;
1002
1003    my $entry;
1004    my @entry;
1005    while ($entry = $textList->getNextMsg()) {
1006        if ($debug) {
1007	    my ($id, $text) = split(/\s*::\s*/, $entry);
1008	    print "   $id = $text\n";
1009	}
1010	unshift (@entry, $entry);
1011    }
1012    $msg_list{$textName} =
1013	[\@entry, [$header, $start, $public, $deprecated]];
1014}
1015
1016sub addHeader {
1017    my $header_index = shift;
1018
1019    die "invalid adt_event_N.h index: $header_index\n"
1020        unless ($header_index =~ /^\d+$/);
1021
1022    $headers{$header_index} = $header_index;
1023}
1024
1025# $header = 0 is a special case; it is for adt_event.h
1026# $header > 0 creates adt_event_N.h, where N = $header
1027
1028sub openHeaderFiles {
1029    my $outfile = shift;	# path to an adt_event_N.h file
1030
1031    my $header;
1032    my @Hfile = (); # potentially sparse array of file handles
1033    my @HfileName = (); # parallel array to Hfile, file name (not path)
1034    foreach $header (sort keys %headers) {
1035        my $file = $outfile;
1036	if ($header > 0) {
1037	    $file =~ s/_N/_$header/;
1038	} else {
1039	    $file =~ s/_N//;
1040	}
1041	unless (open($Hfile[$header], ">$file")) {
1042	    print STDERR "can't open output ($file): $!\n";
1043	    $HfileName[$header] = '';
1044	    $Hfile[$header] = '';
1045	} else {
1046	    my @tmp = split(/\//, $file);
1047	    $HfileName[$header] = $tmp[$#tmp];
1048	}
1049    }
1050    return (@Hfile);
1051}
1052
1053sub closeHeaderFiles {
1054    my @Hfile = @_;
1055
1056    my $header;
1057    foreach $header (sort keys %headers) {
1058	close $Hfile[$header] if $Hfile[$header];
1059    }
1060}
1061