paul@44 | 1 | /* |
paul@44 | 2 | * Common routines. |
paul@44 | 3 | * |
paul@44 | 4 | * Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk> |
paul@44 | 5 | * |
paul@44 | 6 | * This program is free software; you can redistribute it and/or |
paul@44 | 7 | * modify it under the terms of the GNU General Public License as |
paul@44 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@44 | 9 | * the License, or (at your option) any later version. |
paul@44 | 10 | * |
paul@44 | 11 | * This program is distributed in the hope that it will be useful, |
paul@44 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@44 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@44 | 14 | * GNU General Public License for more details. |
paul@44 | 15 | * |
paul@44 | 16 | * You should have received a copy of the GNU General Public License |
paul@44 | 17 | * along with this program; if not, write to the Free Software |
paul@44 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@44 | 19 | * Boston, MA 02110-1301, USA |
paul@44 | 20 | */ |
paul@44 | 21 | |
paul@44 | 22 | #include <libgen.h> |
paul@44 | 23 | #include <stdlib.h> |
paul@44 | 24 | #include <string.h> |
paul@44 | 25 | #include "common.h" |
paul@44 | 26 | |
paul@44 | 27 | |
paul@44 | 28 | |
paul@44 | 29 | /* Generate an error message for file opening failure. */ |
paul@44 | 30 | |
paul@44 | 31 | void open_error(const char *dir, const char *name) |
paul@44 | 32 | { |
paul@55 | 33 | fprintf(stderr, "Could not open source file for writing in directory %s (name %s).\n", dir, name); |
paul@44 | 34 | } |
paul@44 | 35 | |
paul@44 | 36 | /* Obtain a new basename string. */ |
paul@44 | 37 | |
paul@44 | 38 | char *make_basename(const char *path) |
paul@44 | 39 | { |
paul@44 | 40 | char s[strlen(path) + 1]; |
paul@44 | 41 | |
paul@44 | 42 | strcpy(s, path); |
paul@44 | 43 | return strdup(basename(s)); |
paul@44 | 44 | } |
paul@44 | 45 | |
paul@44 | 46 | /* Obtain a new directory name string. */ |
paul@44 | 47 | |
paul@44 | 48 | char *make_dirname(const char *path) |
paul@44 | 49 | { |
paul@44 | 50 | char s[strlen(path) + 1]; |
paul@44 | 51 | |
paul@44 | 52 | strcpy(s, path); |
paul@44 | 53 | return strdup(dirname(s)); |
paul@44 | 54 | } |
paul@44 | 55 | |
paul@44 | 56 | /* Obtain a file pointer using a parameterised filename. */ |
paul@44 | 57 | |
paul@44 | 58 | FILE *get_output_file(const char *filename_template, const char *dir, const char *name) |
paul@44 | 59 | { |
paul@44 | 60 | FILE *fp; |
paul@44 | 61 | char *filename; |
paul@44 | 62 | |
paul@44 | 63 | if (asprintf(&filename, filename_template, dir, name) == -1) |
paul@44 | 64 | fp = NULL; |
paul@44 | 65 | else |
paul@44 | 66 | { |
paul@44 | 67 | fp = fopen(filename, "w"); |
paul@44 | 68 | free(filename); |
paul@44 | 69 | } |
paul@44 | 70 | |
paul@44 | 71 | /* Write an error message upon failure. */ |
paul@44 | 72 | |
paul@44 | 73 | if (fp == NULL) |
paul@44 | 74 | open_error(dir, name); |
paul@44 | 75 | |
paul@44 | 76 | return fp; |
paul@44 | 77 | } |
paul@44 | 78 | |
paul@44 | 79 | /* Find an attribute value. */ |
paul@44 | 80 | |
paul@51 | 81 | char *get_attribute_value(struct attribute *attr, const char *name) |
paul@44 | 82 | { |
paul@51 | 83 | if (attr == NULL) |
paul@44 | 84 | return NULL; |
paul@44 | 85 | |
paul@51 | 86 | if (!strcmp(attr->attribute, name) && (attr->identifiers != NULL)) |
paul@51 | 87 | return attr->identifiers->identifier; |
paul@44 | 88 | |
paul@51 | 89 | return get_attribute_value(attr->tail, name); |
paul@44 | 90 | } |
paul@44 | 91 | |
paul@44 | 92 | /* Return any opcode attribute value. */ |
paul@44 | 93 | |
paul@51 | 94 | char *get_opcode(struct attribute *attr) |
paul@44 | 95 | { |
paul@51 | 96 | return get_attribute_value(attr, "opcode"); |
paul@44 | 97 | } |
paul@44 | 98 | |
paul@44 | 99 | /* Return any protocol attribute value. */ |
paul@44 | 100 | |
paul@51 | 101 | char *get_protocol(struct attribute *attr) |
paul@44 | 102 | { |
paul@51 | 103 | return get_attribute_value(attr, "protocol"); |
paul@44 | 104 | } |
paul@44 | 105 | |
paul@72 | 106 | /* Generate a qualified operation name for server interface operations. */ |
paul@72 | 107 | |
paul@72 | 108 | char *get_interface_operation_name(struct interface *iface, |
paul@72 | 109 | struct signature *sig, enum language lang) |
paul@72 | 110 | { |
paul@72 | 111 | char *opname; |
paul@72 | 112 | |
paul@72 | 113 | switch (lang) |
paul@72 | 114 | { |
paul@72 | 115 | case C_LANGUAGE: |
paul@72 | 116 | if (asprintf(&opname, "%s_%s", iface->name, sig->operation) == -1) |
paul@72 | 117 | opname = NULL; |
paul@72 | 118 | break; |
paul@72 | 119 | |
paul@72 | 120 | case CPP_LANGUAGE: |
paul@72 | 121 | opname = strdup(sig->operation); |
paul@72 | 122 | break; |
paul@72 | 123 | |
paul@72 | 124 | default: |
paul@72 | 125 | opname = NULL; |
paul@72 | 126 | break; |
paul@72 | 127 | } |
paul@72 | 128 | |
paul@72 | 129 | return opname; |
paul@72 | 130 | } |
paul@72 | 131 | |
paul@44 | 132 | /* Generate a qualified operation name for structure naming and possibly opcode |
paul@44 | 133 | naming. */ |
paul@44 | 134 | |
paul@44 | 135 | char *get_operation_name(struct interface *iface, struct signature *sig) |
paul@44 | 136 | { |
paul@44 | 137 | char *opname; |
paul@44 | 138 | |
paul@44 | 139 | if (asprintf(&opname, "%s_%s", iface->name, sig->operation) == -1) |
paul@72 | 140 | opname = NULL; |
paul@44 | 141 | |
paul@44 | 142 | return opname; |
paul@44 | 143 | } |
paul@44 | 144 | |
paul@44 | 145 | /* Return the invocation opcode given protocol and operation name details. */ |
paul@44 | 146 | |
paul@44 | 147 | char *get_opcode_identifier(char *protocol, char *opname) |
paul@44 | 148 | { |
paul@44 | 149 | char *opcode; |
paul@44 | 150 | |
paul@44 | 151 | if (protocol != NULL) |
paul@44 | 152 | return protocol; |
paul@44 | 153 | |
paul@44 | 154 | if (asprintf(&opcode, "opcode_%s", opname) == -1) |
paul@44 | 155 | return NULL; |
paul@44 | 156 | |
paul@44 | 157 | return opcode; |
paul@44 | 158 | } |
paul@44 | 159 | |
paul@44 | 160 | /* Return the parameter name, being the final identifier in a list. */ |
paul@44 | 161 | |
paul@51 | 162 | char *get_parameter_name(struct identifier *ident) |
paul@44 | 163 | { |
paul@44 | 164 | if (ident->tail != NULL) |
paul@44 | 165 | return get_parameter_name(ident->tail); |
paul@44 | 166 | else |
paul@44 | 167 | return ident->identifier; |
paul@44 | 168 | } |
paul@44 | 169 | |
paul@44 | 170 | /* Count the different kinds of parameters in terms of inputs, outputs, words |
paul@44 | 171 | and items. */ |
paul@44 | 172 | |
paul@51 | 173 | void count_parameters(struct parameter *param, int *input_words, |
paul@57 | 174 | int *input_items, int *output_words, int *output_items) |
paul@44 | 175 | { |
paul@51 | 176 | for (*input_words = 0, *input_items = 0, *output_words = 0, *output_items = 0; |
paul@51 | 177 | param != NULL; |
paul@51 | 178 | param = param->tail) |
paul@44 | 179 | { |
paul@44 | 180 | if (param->specifier & IN_PARAMETER) |
paul@44 | 181 | { |
paul@57 | 182 | if (param->cls & ITEM_CLASS) (*input_items)++; |
paul@44 | 183 | else (*input_words)++; |
paul@44 | 184 | } |
paul@44 | 185 | else |
paul@44 | 186 | { |
paul@57 | 187 | if (param->cls & ITEM_CLASS) (*output_items)++; |
paul@44 | 188 | else (*output_words)++; |
paul@44 | 189 | } |
paul@44 | 190 | } |
paul@44 | 191 | } |
paul@61 | 192 | |
paul@70 | 193 | /* Return the maximum number of input items accepted by any one of the given |
paul@70 | 194 | signatures. */ |
paul@70 | 195 | |
paul@70 | 196 | int get_max_input_items(struct signature *sig) |
paul@70 | 197 | { |
paul@70 | 198 | int input_words, input_items, output_words, output_items; |
paul@70 | 199 | int max_input_items = 0; |
paul@70 | 200 | |
paul@70 | 201 | for (; sig != NULL; sig = sig->tail) |
paul@70 | 202 | { |
paul@70 | 203 | count_parameters(sig->parameters, &input_words, &input_items, &output_words, &output_items); |
paul@70 | 204 | |
paul@70 | 205 | if (input_items > max_input_items) |
paul@70 | 206 | max_input_items = input_items; |
paul@70 | 207 | } |
paul@70 | 208 | |
paul@70 | 209 | return max_input_items; |
paul@70 | 210 | } |
paul@70 | 211 | |
paul@61 | 212 | /* Return any operator needed to access the given parameter as a local name. */ |
paul@61 | 213 | |
paul@61 | 214 | char *access_name(struct parameter *param, enum component_role component) |
paul@61 | 215 | { |
paul@61 | 216 | /* In the client, employ the dereferencing operator for "inout" and "out" |
paul@61 | 217 | word parameters since the parameters will employ pointer types. */ |
paul@61 | 218 | |
paul@61 | 219 | if ((component == CLIENT_ROLE) && (param->specifier & OUT_PARAMETER) && |
paul@61 | 220 | (param->cls & WORD_CLASS)) |
paul@61 | 221 | return "*"; |
paul@61 | 222 | |
paul@61 | 223 | /* In the server, when importing items from the message, the address of the |
paul@61 | 224 | variable to hold the capability value must be used. */ |
paul@61 | 225 | |
paul@61 | 226 | if ((component == SERVER_ROLE) && (param->specifier & IN_PARAMETER) && |
paul@61 | 227 | (param->cls & ITEM_CLASS)) |
paul@61 | 228 | return "&"; |
paul@61 | 229 | |
paul@61 | 230 | return ""; |
paul@61 | 231 | } |
paul@61 | 232 | |
paul@61 | 233 | /* Return the item type name for the given parameter. */ |
paul@61 | 234 | |
paul@61 | 235 | char *item_type_name(struct parameter *param) |
paul@61 | 236 | { |
paul@76 | 237 | return param->cls == FPAGE_ITEM ? "fpage" : "capability"; |
paul@61 | 238 | } |
paul@61 | 239 | |
paul@61 | 240 | /* When copying output items from the message (in the client), the message |
paul@61 | 241 | address must be obtained using the appropriate operator. */ |
paul@61 | 242 | |
paul@61 | 243 | char *reference_message(enum component_role component) |
paul@61 | 244 | { |
paul@61 | 245 | return (component == CLIENT_ROLE) ? "&" : ""; |
paul@61 | 246 | } |
paul@61 | 247 | |
paul@61 | 248 | /* Return the naming prefix for structure access to a message. */ |
paul@61 | 249 | |
paul@61 | 250 | char *structure_prefix(enum specifier direction) |
paul@61 | 251 | { |
paul@61 | 252 | return direction == IN_PARAMETER ? "in" : "out"; |
paul@61 | 253 | } |
paul@61 | 254 | |
paul@61 | 255 | /* Return whether message access for the given component role and specifier (or |
paul@61 | 256 | direction) involves writing to (or populating) the message. */ |
paul@61 | 257 | |
paul@61 | 258 | int writing_to_message(enum component_role component, enum specifier direction) |
paul@61 | 259 | { |
paul@61 | 260 | return (component == CLIENT_ROLE) && (direction & IN_PARAMETER) || |
paul@61 | 261 | (component == SERVER_ROLE) && (direction & OUT_PARAMETER); |
paul@61 | 262 | } |