1 /* 2 * Common routines. 3 * 4 * Copyright (C) 2019, 2020 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <libgen.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include "common.h" 26 #include "templates.h" 27 28 29 30 /* Generate an error message for file opening failure. */ 31 32 void open_error(const char *dir, const char *name) 33 { 34 fprintf(stderr, "Could not open source file for writing in directory %s (name %s).\n", dir, name); 35 } 36 37 /* Obtain a new basename string. */ 38 39 char *make_basename(const char *path) 40 { 41 char s[strlen(path) + 1]; 42 43 strcpy(s, path); 44 return strdup(basename(s)); 45 } 46 47 /* Obtain a new directory name string. */ 48 49 char *make_dirname(const char *path) 50 { 51 char s[strlen(path) + 1]; 52 53 strcpy(s, path); 54 return strdup(dirname(s)); 55 } 56 57 /* Obtain a file pointer using a parameterised filename. */ 58 59 FILE *get_output_file(const char *filename_template, const char *dir, const char *name) 60 { 61 FILE *fp; 62 char *filename; 63 64 if (asprintf(&filename, filename_template, dir, name) == -1) 65 fp = NULL; 66 else 67 { 68 fp = fopen(filename, "w"); 69 free(filename); 70 } 71 72 /* Write an error message upon failure. */ 73 74 if (fp == NULL) 75 open_error(dir, name); 76 77 return fp; 78 } 79 80 /* Find an attribute value. */ 81 82 char *get_attribute_value(struct attribute *attr, const char *name) 83 { 84 if (attr == NULL) 85 return NULL; 86 87 if (!strcmp(attr->attribute, name) && (attr->identifiers != NULL)) 88 return attr->identifiers->identifier; 89 90 return get_attribute_value(attr->tail, name); 91 } 92 93 /* Return any conversion to a different object type in a dispatcher. */ 94 95 char *get_object_conversion(struct interface *iface, int compound) 96 { 97 char *conversion; 98 99 if (!compound) 100 return strdup("_self"); 101 102 if (asprintf(&conversion, "convert_to_%s(_self)", iface->name) == -1) 103 conversion = NULL; 104 105 return conversion; 106 } 107 108 /* Return any opcode attribute value. */ 109 110 char *get_opcode(struct attribute *attr) 111 { 112 return get_attribute_value(attr, "opcode"); 113 } 114 115 /* Return any protocol attribute value. */ 116 117 char *get_protocol(struct attribute *attr) 118 { 119 return get_attribute_value(attr, "protocol"); 120 } 121 122 /* Return the wrapper function prefix. */ 123 124 char *get_operation_wrapper_prefix(struct attribute *attr) 125 { 126 if (have_attribute(attr, "completion")) 127 return "initiate"; 128 else 129 return "wrap"; 130 } 131 132 int have_attribute(struct attribute *attr, const char *name) 133 { 134 if (attr == NULL) 135 return 0; 136 137 if (!strcmp(attr->attribute, name)) 138 return 1; 139 140 return have_attribute(attr->tail, name); 141 } 142 143 int have_attribute_value(struct attribute *attr, const char *name, 144 const char *value) 145 { 146 if (attr == NULL) 147 return 0; 148 149 if (!strcmp(attr->attribute, name) && have_identifier(attr->identifiers, value)) 150 return 1; 151 152 return have_attribute_value(attr->tail, name, value); 153 } 154 155 int have_identifier(struct identifier *ident, const char *value) 156 { 157 if (ident == NULL) 158 return 0; 159 160 if (!strcmp(ident->identifier, value)) 161 return 1; 162 163 return have_identifier(ident->tail, value); 164 } 165 166 /* Return the function role of the given signature for server use. */ 167 168 enum function_role get_server_function_role(struct signature *sig) 169 { 170 return have_attribute(sig->attributes, "completion") ? COMPLETION_ROLE 171 : GENERAL_FUNCTION_ROLE; 172 } 173 174 /* Obtain a suitable interface class name depending on the component role. */ 175 176 char *get_interface_class_name(struct interface *iface, 177 enum component_role component) 178 { 179 char *name; 180 181 switch (component) 182 { 183 case CLIENT_ROLE: 184 if (asprintf(&name, "client_%s", iface->name) == -1) 185 name = NULL; 186 break; 187 188 case SERVER_ROLE: 189 name = strdup(iface->name); 190 break; 191 192 default: 193 name = NULL; 194 break; 195 } 196 197 return name; 198 } 199 200 /* Generate a qualified operation name for declarations and definitions. */ 201 202 char *get_signature_operation_name(struct interface *iface, 203 struct signature *sig, 204 enum component_role component, 205 enum signature_role role, enum language lang) 206 { 207 char *opname, *class_name; 208 209 switch (lang) 210 { 211 /* In C, interface members use the bare names. */ 212 213 case C_LANGUAGE: 214 if ((role == MEMBER_DECLARATION_ROLE) || (role == ACCESS_ROLE)) 215 opname = strdup(sig->operation); 216 else 217 opname = get_operation_name(iface, sig); 218 break; 219 220 /* In C++, method definitions are qualified using class names. */ 221 222 case CPP_LANGUAGE: 223 if (role == DEFINITION_ROLE) 224 { 225 class_name = get_interface_class_name(iface, component); 226 227 if (asprintf(&opname, "%s::%s", class_name, sig->operation) == -1) 228 opname = NULL; 229 230 free(class_name); 231 } 232 else 233 opname = strdup(sig->operation); 234 break; 235 236 default: 237 opname = NULL; 238 break; 239 } 240 241 return opname; 242 } 243 244 /* Generate a qualified operation name for structure and function naming. */ 245 246 char *get_operation_name(struct interface *iface, struct signature *sig) 247 { 248 char *opname; 249 250 if (asprintf(&opname, "%s_%s", iface->name, sig->operation) == -1) 251 opname = NULL; 252 253 return opname; 254 } 255 256 /* Return the invocation opcode given protocol and operation name details. */ 257 258 char *get_opcode_identifier(char *protocol, char *opname) 259 { 260 char *opcode; 261 262 if (protocol != NULL) 263 return protocol; 264 265 if (asprintf(&opcode, "opcode_%s", opname) == -1) 266 return NULL; 267 268 return opcode; 269 } 270 271 /* Return the appropriate parameter decoration. Generally, output parameters 272 involve addresses. However, completion functions accept output parameters as 273 values. */ 274 275 const char *get_parameter_decoration(struct parameter *param, 276 enum parameter_role role, 277 enum function_role function) 278 { 279 if ((param->specifier & OUT_PARAMETER) && (function != COMPLETION_ROLE)) 280 { 281 switch (role) 282 { 283 case SIGNATURE_ROLE: return "*"; 284 case INVOCATION_ROLE: return "&"; 285 default: break; 286 } 287 } 288 289 return ""; 290 } 291 292 /* Return the parameter name, being the final identifier in a list. */ 293 294 char *get_parameter_name(struct identifier *ident) 295 { 296 if (ident->tail != NULL) 297 return get_parameter_name(ident->tail); 298 else 299 return ident->identifier; 300 } 301 302 /* Get the appropriate terminating text after a signature. */ 303 304 char *get_signature_terminator(enum signature_role role) 305 { 306 /* Signatures in declarations or member initialisation. */ 307 308 if (role & DECLARATION_ROLE) 309 return COMPLETE_SIGNATURE; 310 311 /* Signatures at the start of functions. */ 312 313 if (role & DEFINITION_ROLE) 314 return BEGIN_FUNCTION; 315 316 return ""; 317 } 318 319 /* Count the different kinds of parameters in terms of inputs, outputs, words 320 and items. */ 321 322 void count_parameters(struct parameter *param, int *input_words, 323 int *input_items, int *output_words, int *output_items) 324 { 325 for (*input_words = 0, *input_items = 0, *output_words = 0, *output_items = 0; 326 param != NULL; 327 param = param->tail) 328 { 329 if (param->specifier & IN_PARAMETER) 330 { 331 if (param->cls & ITEM_CLASS) (*input_items)++; 332 else (*input_words)++; 333 } 334 335 if (param->specifier & OUT_PARAMETER) 336 { 337 if (param->cls & ITEM_CLASS) (*output_items)++; 338 else (*output_words)++; 339 } 340 } 341 } 342 343 /* Return the maximum number of input items accepted by any one of the given 344 signatures. */ 345 346 int get_max_input_items(struct signature *sig) 347 { 348 int input_words, input_items, output_words, output_items; 349 int max_input_items = 0; 350 351 for (; sig != NULL; sig = sig->tail) 352 { 353 count_parameters(sig->parameters, &input_words, &input_items, &output_words, &output_items); 354 355 if (input_items > max_input_items) 356 max_input_items = input_items; 357 } 358 359 return max_input_items; 360 } 361 362 /* Return any operator needed to access the given parameter as a local name. */ 363 364 char *access_name(struct parameter *param, enum component_role component) 365 { 366 /* In the client, employ the dereferencing operator for "inout" and "out" 367 word parameters since the parameters will employ pointer types. */ 368 369 if ((component == CLIENT_ROLE) && (param->specifier & OUT_PARAMETER) && 370 (param->cls & WORD_CLASS)) 371 return "*"; 372 373 /* In the server, when importing items from the message, the address of the 374 variable to hold the capability value must be used. */ 375 376 if ((component == SERVER_ROLE) && (param->specifier & IN_PARAMETER) && 377 (param->cls & ITEM_CLASS)) 378 return "&"; 379 380 return ""; 381 } 382 383 /* Return the item type name for the given parameter. */ 384 385 char *item_type_name(struct parameter *param) 386 { 387 return param->cls == FPAGE_ITEM ? "fpage" : "capability"; 388 } 389 390 /* When copying output items from the message (in the client) or copying into 391 the message (in the server, but only with a new message), the message address 392 must be obtained using the appropriate operator. */ 393 394 char *reference_message(enum component_role component, 395 enum function_role function) 396 { 397 return (component == CLIENT_ROLE) || (function == COMPLETION_ROLE) ? "&" : ""; 398 } 399 400 /* Return the naming prefix for structure access to a message. */ 401 402 char *structure_prefix(enum specifier direction) 403 { 404 return direction == IN_PARAMETER ? "in" : "out"; 405 } 406 407 /* Return whether message access for the given component role and specifier (or 408 direction) involves writing to (or populating) the message. */ 409 410 int writing_to_message(enum component_role component, enum specifier direction) 411 { 412 return (component == CLIENT_ROLE) && (direction & IN_PARAMETER) || 413 (component == SERVER_ROLE) && (direction & OUT_PARAMETER); 414 }