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