Added a warning (always to stderr) when opening the log file fails
[yaz-moved-to-github.git] / src / prt-ext.c
1 /*
2  * Copyright (c) 1995-2004, Index Data.
3  * See the file LICENSE for details.
4  *
5  * $Id: prt-ext.c,v 1.3 2004-10-15 00:19:00 adam Exp $
6  */
7
8 /**
9  * \file prt-ext.c
10  * \brief Implements handling of various Z39.50 Externals
11  */
12
13 #include <yaz/proto.h>
14
15 /*
16  * The table below should be moved to the ODR structure itself and
17  * be an image of the association context: To help
18  * map indirect references when they show up. 
19  */
20 static Z_ext_typeent type_table[] =
21 {
22     {VAL_SUTRS, Z_External_sutrs, (Odr_fun) z_SUTRS},
23     {VAL_EXPLAIN, Z_External_explainRecord, (Odr_fun)z_ExplainRecord},
24     {VAL_RESOURCE1, Z_External_resourceReport1, (Odr_fun)z_ResourceReport1},
25     {VAL_RESOURCE2, Z_External_resourceReport2, (Odr_fun)z_ResourceReport2},
26     {VAL_PROMPT1, Z_External_promptObject1, (Odr_fun)z_PromptObject1 },
27     {VAL_GRS1, Z_External_grs1, (Odr_fun)z_GenericRecord},
28     {VAL_EXTENDED, Z_External_extendedService, (Odr_fun)z_TaskPackage},
29     {VAL_ITEMORDER, Z_External_itemOrder, (Odr_fun)z_IOItemOrder},
30     {VAL_DIAG1, Z_External_diag1, (Odr_fun)z_DiagnosticFormat},
31     {VAL_ESPEC1, Z_External_espec1, (Odr_fun)z_Espec1},
32     {VAL_SUMMARY, Z_External_summary, (Odr_fun)z_BriefBib},
33     {VAL_OPAC, Z_External_OPAC, (Odr_fun)z_OPACRecord},
34     {VAL_SEARCHRES1, Z_External_searchResult1, (Odr_fun)z_SearchInfoReport},
35     {VAL_DBUPDATE, Z_External_update, (Odr_fun)z_IUUpdate},
36     {VAL_DBUPDATE0, Z_External_update0, (Odr_fun)z_IU0Update},
37     {VAL_DBUPDATE1, Z_External_update0, (Odr_fun)z_IU0Update},
38     {VAL_DATETIME, Z_External_dateTime, (Odr_fun)z_DateTime},
39     {VAL_UNIVERSE_REPORT, Z_External_universeReport,(Odr_fun)z_UniverseReport},
40     {VAL_ADMINSERVICE, Z_External_ESAdmin, (Odr_fun)z_Admin},
41     {VAL_USERINFO1, Z_External_userInfo1, (Odr_fun) z_OtherInformation},
42     {VAL_CHARNEG3, Z_External_charSetandLanguageNegotiation, (Odr_fun)
43                   z_CharSetandLanguageNegotiation},
44     {VAL_PROMPT1, Z_External_acfPrompt1, (Odr_fun) z_PromptObject1},
45     {VAL_DES1, Z_External_acfDes1, (Odr_fun) z_DES_RN_Object},
46     {VAL_KRB1, Z_External_acfKrb1, (Odr_fun) z_KRBObject},
47     {VAL_MULTISRCH2, Z_External_multisrch2, (Odr_fun) z_MultipleSearchTerms_2},
48     {VAL_CQL, Z_External_CQL, (Odr_fun) z_InternationalString},
49     {VAL_NONE, 0, 0}
50 };
51
52 Z_ext_typeent *z_ext_getentbyref(oid_value val)
53 {
54     Z_ext_typeent *i;
55
56     for (i = type_table; i->dref != VAL_NONE; i++)
57         if (i->dref == val)
58             return i;
59     return 0;
60 }
61
62 int z_External(ODR o, Z_External **p, int opt, const char *name)
63 {
64     oident *oid;
65     Z_ext_typeent *type;
66
67     static Odr_arm arm[] =
68     {
69         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_single,
70          (Odr_fun)odr_any, 0},
71         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_External_octet,
72          (Odr_fun)odr_octetstring, 0},
73         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_External_arbitrary,
74          (Odr_fun)odr_bitstring, 0},
75         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_sutrs,
76          (Odr_fun)z_SUTRS, 0},
77         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_explainRecord,
78          (Odr_fun)z_ExplainRecord, 0},
79
80         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_resourceReport1,
81          (Odr_fun)z_ResourceReport1, 0},
82         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_resourceReport2,
83          (Odr_fun)z_ResourceReport2, 0},
84         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_promptObject1,
85          (Odr_fun)z_PromptObject1, 0},
86         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_grs1,
87          (Odr_fun)z_GenericRecord, 0},
88         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_extendedService,
89          (Odr_fun)z_TaskPackage, 0},
90
91         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_itemOrder,
92          (Odr_fun)z_IOItemOrder, 0},
93         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_diag1,
94          (Odr_fun)z_DiagnosticFormat, 0},
95         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_espec1,
96          (Odr_fun)z_Espec1, 0},
97         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_summary,
98          (Odr_fun)z_BriefBib, 0},
99         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_OPAC,
100          (Odr_fun)z_OPACRecord, 0},
101
102         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_searchResult1,
103          (Odr_fun)z_SearchInfoReport, 0},
104         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_update,
105          (Odr_fun)z_IUUpdate, 0},
106         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_dateTime,
107          (Odr_fun)z_DateTime, 0},
108         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_universeReport,
109          (Odr_fun)z_UniverseReport, 0},
110         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_ESAdmin,
111          (Odr_fun)z_Admin, 0},
112
113         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_update0,
114          (Odr_fun)z_IU0Update, 0},
115         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_userInfo1,
116          (Odr_fun)z_OtherInformation, 0},
117         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_charSetandLanguageNegotiation,
118          (Odr_fun)z_CharSetandLanguageNegotiation, 0},
119         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_acfPrompt1,
120          (Odr_fun)z_PromptObject1, 0},
121         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_acfDes1,
122          (Odr_fun)z_DES_RN_Object, 0},
123
124         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_acfKrb1,
125          (Odr_fun)z_KRBObject, 0},
126         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_multisrch2,
127          (Odr_fun)z_MultipleSearchTerms_2, 0},
128         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_CQL,
129          (Odr_fun)z_InternationalString, 0},
130         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_OCLCUserInfo,
131          (Odr_fun)z_OCLC_UserInformation, 0},
132         {-1, -1, -1, -1, 0, 0}
133     };
134     
135     odr_implicit_settag(o, ODR_UNIVERSAL, ODR_EXTERNAL);
136     if (!odr_sequence_begin(o, p, sizeof(**p), name))
137         return opt && odr_ok(o);
138     if (!(odr_oid(o, &(*p)->direct_reference, 1, 0) &&
139           odr_integer(o, &(*p)->indirect_reference, 1, 0) &&
140           odr_graphicstring(o, &(*p)->descriptor, 1, 0)))
141         return 0;
142     /*
143      * Do we know this beast?
144      */
145     if (o->direction == ODR_DECODE && (*p)->direct_reference &&
146         (oid = oid_getentbyoid((*p)->direct_reference)) &&
147         (type = z_ext_getentbyref(oid->value)))
148     {
149         int zclass, tag, cons;
150         
151         /*
152          * We know it. If it's represented as an ASN.1 type, bias the CHOICE.
153          */
154         if (!odr_peektag(o, &zclass, &tag, &cons))
155             return opt && odr_ok(o);
156         if (zclass == ODR_CONTEXT && tag == 0 && cons == 1)
157             odr_choice_bias(o, type->what);
158     }
159     return
160         odr_choice(o, arm, &(*p)->u, &(*p)->which, name) &&
161         odr_sequence_end(o);
162 }
163
164 Z_External *z_ext_record(ODR o, int format, const char *buf, int len)
165 {
166     Z_External *thisext;
167
168     thisext = (Z_External *) odr_malloc(o, sizeof(*thisext));
169     thisext->descriptor = 0;
170     thisext->indirect_reference = 0;
171
172     thisext->direct_reference = 
173         yaz_oidval_to_z3950oid (o, CLASS_RECSYN, format);    
174     if (!thisext->direct_reference)
175         return 0;
176
177     if (len < 0) /* Structured data */
178     {
179         
180         /*
181          * We cheat on the pointers here. Obviously, the record field
182          * of the backend-fetch structure should have been a union for
183          * correctness, but we're stuck with this for backwards
184          * compatibility.
185          */
186         thisext->u.grs1 = (Z_GenericRecord*) buf;
187
188         switch (format)
189         {
190         case VAL_SUTRS:
191             thisext->which = Z_External_sutrs;
192             break;
193         case VAL_GRS1:
194             thisext->which = Z_External_grs1;
195             break;
196         case VAL_EXPLAIN:
197             thisext->which = Z_External_explainRecord;
198             break;
199         case VAL_SUMMARY:
200             thisext->which = Z_External_summary;
201             break;
202         case VAL_OPAC:
203             thisext->which = Z_External_OPAC;
204             break;
205         case VAL_EXTENDED:
206             thisext->which = Z_External_extendedService;
207             break;
208         default:
209             return 0;
210         }
211     }
212     else if (format == VAL_SUTRS) /* SUTRS is a single-ASN.1-type */
213     {
214         Odr_oct *sutrs = (Odr_oct *)odr_malloc(o, sizeof(*sutrs));
215         
216         thisext->which = Z_External_sutrs;
217         thisext->u.sutrs = sutrs;
218         sutrs->buf = (unsigned char *)odr_malloc(o, len);
219         sutrs->len = sutrs->size = len;
220         memcpy(sutrs->buf, buf, len);
221     }
222     else
223     {
224         thisext->which = Z_External_octet;
225         if (!(thisext->u.octet_aligned = (Odr_oct *)
226               odr_malloc(o, sizeof(Odr_oct))))
227             return 0;
228         if (!(thisext->u.octet_aligned->buf = (unsigned char *)
229               odr_malloc(o, len)))
230             return 0;
231         memcpy(thisext->u.octet_aligned->buf, buf, len);
232         thisext->u.octet_aligned->len = thisext->u.octet_aligned->size = len;
233     }
234     return thisext;
235 }
236