/** Translate the character string to the protocol family number. * * @param[in] name The protocol family name. * @param[out] pf The corresponding protocol family number * or EPFNOSUPPORTED. * * @return EOK on success. * @return ENOTSUP on unsupported protocol family. * */ int socket_parse_protocol_family(const char *name, int *pf) { if (str_lcmp(name, "PF_INET", 7) == 0) { *pf = PF_INET; return EOK; } if (str_lcmp(name, "PF_INET6", 8) == 0) { *pf = PF_INET6; return EOK; } *pf = EPFNOSUPPORT; return ENOTSUP; }
/** Translate the character string to the socket type number. * * @param[in] name The socket type name. * @param[out] sockt The corresponding socket type number * or ESOCKTNOSUPPORT. * * @return EOK on success. * @return ENOTSUP on unsupported socket type. * */ int socket_parse_socket_type(const char *name, int *sockt) { if (str_lcmp(name, "SOCK_DGRAM", 11) == 0) { *sockt = SOCK_DGRAM; return EOK; } if (str_lcmp(name, "SOCK_STREAM", 12) == 0) { *sockt = SOCK_STREAM; return EOK; } *sockt = ESOCKTNOSUPPORT; return ENOTSUP; }
/** Translate the character string to the address family number. * * @param[in] name The address family name. * @param[out] af The corresponding address family number * or EAFNOSUPPORTED. * * @return EOK on success. * @return ENOTSUP on unsupported address family. * */ int socket_parse_address_family(const char *name, int *af) { if (str_lcmp(name, "AF_INET", 7) == 0) { *af = AF_INET; return EOK; } if (str_lcmp(name, "AF_INET6", 8) == 0) { *af = AF_INET6; return EOK; } *af = EAFNOSUPPORT; return ENOTSUP; }
/** Try to find a command beginning with prefix */ const char *cmdtab_enum(const char *name, const char **h, void **ctx) { link_t **startpos = (link_t**) ctx; size_t namelen = str_length(name); spinlock_lock(&cmd_lock); if (*startpos == NULL) *startpos = cmd_list.head.next; for (; *startpos != &cmd_list.head; *startpos = (*startpos)->next) { cmd_info_t *hlp = list_get_instance(*startpos, cmd_info_t, link); const char *curname = hlp->name; if (str_length(curname) < namelen) continue; if (str_lcmp(curname, name, namelen) == 0) { *startpos = (*startpos)->next; if (h) *h = hlp->description; spinlock_unlock(&cmd_lock); return (curname + str_lsize(curname, namelen)); } } spinlock_unlock(&cmd_lock); return NULL; }
async_sess_t* ahci_get_sess(devman_handle_t funh, char **name) { // FIXME: Use a better way than substring match *name = NULL; char devn[MAX_NAME_LENGTH]; int rc = devman_fun_get_name(funh, devn, MAX_NAME_LENGTH); if (rc != EOK) return NULL; size_t devn_size = str_size(devn); if ((devn_size > 5) && (str_lcmp(devn, "ahci_", 5) == 0)) { async_sess_t *sess = devman_device_connect(funh, IPC_FLAG_BLOCKING); if (sess) { *name = str_dup(devn); return sess; } } return NULL; }
/** Find symbols that match the parameter forward and print them. * * @param name Search string * @param startpos Starting position, changes to found position * * @return Pointer to the part of string that should be completed or NULL. * */ static const char *symtab_search_one(const char *name, size_t *startpos) { size_t namelen = str_length(name); size_t pos; for (pos = *startpos; symbol_table[pos].address_le; pos++) { const char *curname = symbol_table[pos].symbol_name; /* Find a ':' in curname */ const char *colon = str_chr(curname, ':'); if (colon == NULL) continue; if (str_length(curname) < namelen) continue; if (str_lcmp(name, curname, namelen) == 0) { *startpos = pos; return (curname + str_lsize(curname, namelen)); } } return NULL; }
/** Parse one command-line option. * * @param argc Number of all command-line arguments. * @param argv All command-line arguments. * @param index Current argument index (in, out). */ static int nettest2_parse_opt(int argc, char *argv[], int *index) { int value; int rc; switch (argv[*index][1]) { /* * Short options with only one letter */ case 'f': rc = arg_parse_name_int(argc, argv, index, &family, 0, socket_parse_protocol_family); if (rc != EOK) return rc; break; case 'h': nettest2_print_help(); return EOK; break; case 'm': rc = arg_parse_int(argc, argv, index, &messages, 0); if (rc != EOK) return rc; break; case 'n': rc = arg_parse_int(argc, argv, index, &sockets, 0); if (rc != EOK) return rc; break; case 'p': rc = arg_parse_int(argc, argv, index, &value, 0); if (rc != EOK) return rc; port = (uint16_t) value; break; case 's': rc = arg_parse_int(argc, argv, index, &value, 0); if (rc != EOK) return rc; size = (value >= 0) ? (size_t) value : 0; break; case 't': rc = arg_parse_name_int(argc, argv, index, &value, 0, socket_parse_socket_type); if (rc != EOK) return rc; type = (sock_type_t) value; break; case 'v': verbose = true; break; /* * Long options with double dash ('-') */ case '-': if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) { rc = arg_parse_name_int(argc, argv, index, &family, 9, socket_parse_protocol_family); if (rc != EOK) return rc; } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) { nettest2_print_help(); return EOK; } else if (str_lcmp(argv[*index] + 2, "messages=", 6) == 0) { rc = arg_parse_int(argc, argv, index, &messages, 8); if (rc != EOK) return rc; } else if (str_lcmp(argv[*index] + 2, "sockets=", 6) == 0) { rc = arg_parse_int(argc, argv, index, &sockets, 8); if (rc != EOK) return rc; } else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) { rc = arg_parse_int(argc, argv, index, &value, 7); if (rc != EOK) return rc; port = (uint16_t) value; } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) { rc = arg_parse_name_int(argc, argv, index, &value, 7, socket_parse_socket_type); if (rc != EOK) return rc; type = (sock_type_t) value; } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) { verbose = 1; } else { nettest2_print_help(); return EINVAL; } break; default: nettest2_print_help(); return EINVAL; } return EOK; }
static int netecho_parse_option(int argc, char *argv[], int *index) { int value; int rc; switch (argv[*index][1]) { case 'b': rc = arg_parse_int(argc, argv, index, &backlog, 0); if (rc != EOK) return rc; break; case 'c': rc = arg_parse_int(argc, argv, index, &count, 0); if (rc != EOK) return rc; break; case 'f': rc = arg_parse_name_int(argc, argv, index, &family, 0, socket_parse_protocol_family); if (rc != EOK) return rc; break; case 'h': echo_print_help(); exit(0); break; case 'p': rc = arg_parse_int(argc, argv, index, &value, 0); if (rc != EOK) return rc; port = (uint16_t) value; break; case 'r': rc = arg_parse_string(argc, argv, index, &reply, 0); if (rc != EOK) return rc; break; case 's': rc = arg_parse_int(argc, argv, index, &value, 0); if (rc != EOK) return rc; size = (value >= 0) ? (size_t) value : 0; break; case 't': rc = arg_parse_name_int(argc, argv, index, &value, 0, socket_parse_socket_type); if (rc != EOK) return rc; type = (sock_type_t) value; break; case 'v': verbose = 1; break; /* Long options with double dash */ case '-': if (str_lcmp(argv[*index] + 2, "backlog=", 6) == 0) { rc = arg_parse_int(argc, argv, index, &backlog, 8); if (rc != EOK) return rc; } else if (str_lcmp(argv[*index] + 2, "count=", 6) == 0) { rc = arg_parse_int(argc, argv, index, &count, 8); if (rc != EOK) return rc; } else if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) { rc = arg_parse_name_int(argc, argv, index, &family, 9, socket_parse_protocol_family); if (rc != EOK) return rc; } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) { echo_print_help(); exit(0); } else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) { rc = arg_parse_int(argc, argv, index, &value, 7); if (rc != EOK) return rc; port = (uint16_t) value; } else if (str_lcmp(argv[*index] + 2, "reply=", 6) == 0) { rc = arg_parse_string(argc, argv, index, &reply, 8); if (rc != EOK) return rc; } else if (str_lcmp(argv[*index] + 2, "size=", 5) == 0) { rc = arg_parse_int(argc, argv, index, &value, 7); if (rc != EOK) return rc; size = (value >= 0) ? (size_t) value : 0; } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) { rc = arg_parse_name_int(argc, argv, index, &value, 7, socket_parse_socket_type); if (rc != EOK) return rc; type = (sock_type_t) value; } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) { verbose = 1; } else { echo_print_help(); return EINVAL; } break; default: echo_print_help(); return EINVAL; } return EOK; }