1 /* 2 * Common routines. 3 * 4 * Copyright (C) 2019 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 /* Obtain a suitable interface class name depending on the component role. */ 167 168 char *get_interface_class_name(struct interface *iface, 169 enum component_role component) 170 { 171 char *name; 172 173 switch (component) 174 { 175 case CLIENT_ROLE: 176 if (asprintf(&name, "client_%s", iface->name) == -1) 177 name = NULL; 178 break; 179 180 case SERVER_ROLE: 181 name = strdup(iface->name); 182 break; 183 184 default: 185 name = NULL; 186 break; 187 } 188 189 return name; 190 } 191 192 /* Generate a qualified operation name for declarations and definitions. */ 193 194 char *get_signature_operation_name(struct interface *iface, 195 struct signature *sig, 196 enum component_role component, 197 enum signature_role role, enum language lang) 198 { 199 char *opname, *class_name; 200 201 switch (lang) 202 { 203 /* In C, interface members use the bare names. */ 204 205 case C_LANGUAGE: 206 if ((role == MEMBER_DECLARATION_ROLE) || (role == ACCESS_ROLE)) 207 opname = strdup(sig->operation); 208 else 209 opname = get_operation_name(iface, sig); 210 break; 211 212 /* In C++, method definitions are qualified using class names. */ 213 214 case CPP_LANGUAGE: 215 if (role == DEFINITION_ROLE) 216 { 217 class_name = get_interface_class_name(iface, component); 218 219 if (asprintf(&opname, "%s::%s", class_name, sig->operation) == -1) 220 opname = NULL; 221 222 free(class_name); 223 } 224 else 225 opname = strdup(sig->operation); 226 break; 227 228 default: 229 opname = NULL; 230 break; 231 } 232 233 return opname; 234 } 235 236 /* Generate a qualified operation name for structure and function naming. */ 237 238 char *get_operation_name(struct interface *iface, struct signature *sig) 239 { 240 char *opname; 241 242 if (asprintf(&opname, "%s_%s", iface->name, sig->operation) == -1) 243 opname = NULL; 244 245 return opname; 246 } 247 248 /* Return the invocation opcode given protocol and operation name details. */ 249 250 char *get_opcode_identifier(char *protocol, char *opname) 251 { 252 char *opcode; 253 254 if (protocol != NULL) 255 return protocol; 256 257 if (asprintf(&opcode, "opcode_%s", opname) == -1) 258 return NULL; 259 260 return opcode; 261 } 262 263 /* Return the appropriate parameter decoration. Generally, output parameters 264 involve addresses. However, completion functions accept output parameters as 265 values. */ 266 267 const char *get_parameter_decoration(struct parameter *param, 268 enum parameter_role role, 269 enum function_role function) 270 { 271 if ((param->specifier & OUT_PARAMETER) && (function != COMPLETION_ROLE)) 272 { 273 switch (role) 274 { 275 case SIGNATURE_ROLE: return "*"; 276 case INVOCATION_ROLE: return "&"; 277 default: break; 278 } 279 } 280 281 return ""; 282 } 283 284 /* Return the parameter name, being the final identifier in a list. */ 285 286 char *get_parameter_name(struct identifier *ident) 287 { 288 if (ident->tail != NULL) 289 return get_parameter_name(ident->tail); 290 else 291 return ident->identifier; 292 } 293 294 /* Get the appropriate terminating text after a signature. */ 295 296 char *get_signature_terminator(enum signature_role role) 297 { 298 /* Signatures in declarations or member initialisation. */ 299 300 if (role & DECLARATION_ROLE) 301 return COMPLETE_SIGNATURE; 302 303 /* Signatures at the start of functions. */ 304 305 if (role & DEFINITION_ROLE) 306 return BEGIN_FUNCTION; 307 308 return ""; 309 } 310 311 /* Count the different kinds of parameters in terms of inputs, outputs, words 312 and items. */ 313 314 void count_parameters(struct parameter *param, int *input_words, 315 int *input_items, int *output_words, int *output_items) 316 { 317 for (*input_words = 0, *input_items = 0, *output_words = 0, *output_items = 0; 318 param != NULL; 319 param = param->tail) 320 { 321 if (param->specifier & IN_PARAMETER) 322 { 323 if (param->cls & ITEM_CLASS) (*input_items)++; 324 else (*input_words)++; 325 } 326 327 if (param->specifier & OUT_PARAMETER) 328 { 329 if (param->cls & ITEM_CLASS) (*output_items)++; 330 else (*output_words)++; 331 } 332 } 333 } 334 335 /* Return the maximum number of input items accepted by any one of the given 336 signatures. */ 337 338 int get_max_input_items(struct signature *sig) 339 { 340 int input_words, input_items, output_words, output_items; 341 int max_input_items = 0; 342 343 for (; sig != NULL; sig = sig->tail) 344 { 345 count_parameters(sig->parameters, &input_words, &input_items, &output_words, &output_items); 346 347 if (input_items > max_input_items) 348 max_input_items = input_items; 349 } 350 351 return max_input_items; 352 } 353 354 /* Return any operator needed to access the given parameter as a local name. */ 355 356 char *access_name(struct parameter *param, enum component_role component) 357 { 358 /* In the client, employ the dereferencing operator for "inout" and "out" 359 word parameters since the parameters will employ pointer types. */ 360 361 if ((component == CLIENT_ROLE) && (param->specifier & OUT_PARAMETER) && 362 (param->cls & WORD_CLASS)) 363 return "*"; 364 365 /* In the server, when importing items from the message, the address of the 366 variable to hold the capability value must be used. */ 367 368 if ((component == SERVER_ROLE) && (param->specifier & IN_PARAMETER) && 369 (param->cls & ITEM_CLASS)) 370 return "&"; 371 372 return ""; 373 } 374 375 /* Return the item type name for the given parameter. */ 376 377 char *item_type_name(struct parameter *param) 378 { 379 return param->cls == FPAGE_ITEM ? "fpage" : "capability"; 380 } 381 382 /* When copying output items from the message (in the client) or copying into 383 the message (in the server, but only with a new message), the message address 384 must be obtained using the appropriate operator. */ 385 386 char *reference_message(enum component_role component, 387 enum function_role function) 388 { 389 return (component == CLIENT_ROLE) || (function == COMPLETION_ROLE) ? "&" : ""; 390 } 391 392 /* Return the naming prefix for structure access to a message. */ 393 394 char *structure_prefix(enum specifier direction) 395 { 396 return direction == IN_PARAMETER ? "in" : "out"; 397 } 398 399 /* Return whether message access for the given component role and specifier (or 400 direction) involves writing to (or populating) the message. */ 401 402 int writing_to_message(enum component_role component, enum specifier direction) 403 { 404 return (component == CLIENT_ROLE) && (direction & IN_PARAMETER) || 405 (component == SERVER_ROLE) && (direction & OUT_PARAMETER); 406 }