1 | |
---|
2 | |
---|
3 | /*** |
---|
4 | extract_doc.c, |
---|
5 | |
---|
6 | This program extracts API doc from libabac/abac.hh and |
---|
7 | generates header file ABAC_HH |
---|
8 | This program assumes abac.hh is local or linked locally |
---|
9 | |
---|
10 | rules: |
---|
11 | 1) Enclosed lines within slashes with triple stars(/***,***SLASH/) need |
---|
12 | to be extracted |
---|
13 | 2) line start with 'f', add a new line |
---|
14 | 3) Enclosed lines with tripple stars and 2 i's (/*ii**. **ii*SLASH/) |
---|
15 | are internal functions, no need to put in API file |
---|
16 | |
---|
17 | cc extract_doc.c |
---|
18 | |
---|
19 | ***/ |
---|
20 | #include <stdio.h> |
---|
21 | #include <stdlib.h> |
---|
22 | #include <string.h> |
---|
23 | |
---|
24 | char *sstr="/***"; |
---|
25 | char *estr="***/"; |
---|
26 | char *prestr="/***PRE"; |
---|
27 | char *poststr="/***POST"; |
---|
28 | |
---|
29 | void extract_p(FILE *in, FILE* out, char *match) |
---|
30 | { |
---|
31 | rewind(in); |
---|
32 | int yes=0; |
---|
33 | char *line=NULL; |
---|
34 | size_t num=0; |
---|
35 | int n=strlen(match); |
---|
36 | int nn=strlen(estr); |
---|
37 | while(getline(&line,&num,in) != -1) { |
---|
38 | if(!yes) { |
---|
39 | if(strncmp(match,line,n)==0) { |
---|
40 | yes=1; |
---|
41 | free(line); line=NULL; |
---|
42 | continue; |
---|
43 | } else { |
---|
44 | free(line); |
---|
45 | line=NULL; |
---|
46 | } |
---|
47 | }else { |
---|
48 | if(strncmp(estr,line, nn)==0) { |
---|
49 | yes=0; |
---|
50 | free(line); |
---|
51 | line=NULL; |
---|
52 | break; |
---|
53 | } else { |
---|
54 | fprintf(out,"%s",line); |
---|
55 | free(line); |
---|
56 | line=NULL; |
---|
57 | } |
---|
58 | } |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | void extract(FILE* in, FILE* out, FILE *cout) |
---|
63 | { |
---|
64 | int yes=0; |
---|
65 | char *line=NULL; |
---|
66 | size_t num=0; |
---|
67 | int n=strlen(sstr); |
---|
68 | int nn=strlen(estr); |
---|
69 | while(getline(&line,&num,in) != -1) { |
---|
70 | if(!yes) { |
---|
71 | if(strncmp(sstr,line, n)==0) { |
---|
72 | yes=1; |
---|
73 | free(line); line=NULL; |
---|
74 | continue; |
---|
75 | } else { |
---|
76 | fprintf(cout,"%s",line); |
---|
77 | free(line); |
---|
78 | line=NULL; |
---|
79 | } |
---|
80 | }else { |
---|
81 | if(strncmp(estr,line, nn)==0) { |
---|
82 | yes=0; |
---|
83 | free(line); |
---|
84 | line=NULL; |
---|
85 | continue; |
---|
86 | } else { |
---|
87 | if(line[0]=='f') { |
---|
88 | line[0]=' '; |
---|
89 | fprintf(out,"\n"); |
---|
90 | } |
---|
91 | if(line[0]=='A') fprintf(out,"\n"); |
---|
92 | fprintf(out,"%s",line); |
---|
93 | free(line); |
---|
94 | line=NULL; |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | int main() |
---|
101 | { |
---|
102 | char *selfname="extract_doc.c"; |
---|
103 | char *filename="abac.hh"; |
---|
104 | char *docname="API"; |
---|
105 | char *codename="ABAC_HH"; |
---|
106 | |
---|
107 | FILE *dfp=fopen(selfname,"r"); |
---|
108 | FILE *fp=fopen(filename,"r"); |
---|
109 | FILE *ofp=fopen(docname,"w"); |
---|
110 | FILE *cfp=fopen(codename,"w"); |
---|
111 | if(fp && dfp && ofp && cfp ) { |
---|
112 | extract_p(dfp,ofp,prestr); |
---|
113 | extract(fp,ofp,cfp); |
---|
114 | extract_p(dfp,ofp,poststr); |
---|
115 | fclose(fp); |
---|
116 | fclose(dfp); |
---|
117 | fclose(ofp); |
---|
118 | fclose(cfp); |
---|
119 | } |
---|
120 | return 0; |
---|
121 | } |
---|
122 | |
---|
123 | /***PRE |
---|
124 | C++ API |
---|
125 | |
---|
126 | (see bottom for notes on C, Perl, Python and Java) |
---|
127 | |
---|
128 | ABAC::abac_chunk_t |
---|
129 | Structure, represents a blob of memory |
---|
130 | used to load/return DER-encoded X509 certificates |
---|
131 | -unsigned char *data |
---|
132 | -int len |
---|
133 | ***/ |
---|
134 | |
---|
135 | |
---|
136 | /***POST |
---|
137 | C API |
---|
138 | |
---|
139 | The C API is nearly identical to the C++ API. Due to lack of namespaces, |
---|
140 | all function names are preceeded by abac_. Furthermore, the parameter |
---|
141 | representing the object must be passed explicitly. Each of the C++ calls |
---|
142 | are appended with a matching C routine call. The C function declaration |
---|
143 | can be found in abac.h |
---|
144 | |
---|
145 | Examples: |
---|
146 | |
---|
147 | C++: head.role_name() |
---|
148 | C: abac_role_name(head) |
---|
149 | or |
---|
150 | C++: ctxt.load_attribute_file("test_attr.der") |
---|
151 | C: abac_context_load_attribute_file(ctxt, "test_attr.der") |
---|
152 | |
---|
153 | Instead of copy constructors, the C API uses _dup. Therefore, |
---|
154 | to copy a role use abac_aspect_dup(m_role), |
---|
155 | to copy a oset use abac_aspect_dup(m_oset), |
---|
156 | to copy a context use abac_context_dup(m_ctxt), |
---|
157 | to copy a constraint use abac_condition_dup(m_constraint), |
---|
158 | to copy a data term use abac_term_dup(m_term), |
---|
159 | to copy a ID use abac_id_dup(m_id) |
---|
160 | and to copy an attribute use abac_attribute_dup(m_attr) |
---|
161 | |
---|
162 | Various flavors of abac_context_query() and abac_context_credentials() |
---|
163 | return NULL-terminated arrays of Attribute objects (abac_credential_t * in C). |
---|
164 | abac_context_principals() returns NULL-terminated array of ID objects |
---|
165 | (abac_id_credential_t * in C) |
---|
166 | |
---|
167 | When you are done with them, you must free the whole array at once using |
---|
168 | abac_free_credentials() and abac_free_principals() respectively. |
---|
169 | |
---|
170 | PERL, PYTHON AND JAVA API |
---|
171 | |
---|
172 | The Perl, Python and Java APIs are even more similar to the C++ API. The main |
---|
173 | changes are the use of native types instead of C/C++ types. |
---|
174 | |
---|
175 | - native strings instead of char * |
---|
176 | |
---|
177 | Java: |
---|
178 | - String instead of char * |
---|
179 | - Context::query returns a vector of Credentials: |
---|
180 | credentials = ctxt.query(role, principal) |
---|
181 | success if credentials' size is > 0 |
---|
182 | |
---|
183 | Perl: |
---|
184 | - arrayref instead of vector |
---|
185 | - string instead of chunk_t |
---|
186 | - Context::query returns a list of two elements: |
---|
187 | my ($success, $credentials) = $ctxt->query($role, $principal) |
---|
188 | $success is a boolean |
---|
189 | $credentials is an arrayref of Credential objects |
---|
190 | |
---|
191 | Python: |
---|
192 | - tuple instead of vector |
---|
193 | - bytearray instead of chunk_t (>= 2.6) |
---|
194 | - string instead of chunk_t (< 2.6) |
---|
195 | - Context::query returns a tuple with two elements: |
---|
196 | (success, credentials) = ctxt.query(role, principal) |
---|
197 | success is a boolean |
---|
198 | credentials is a tuple of Credential objects |
---|
199 | |
---|
200 | ***/ |
---|