/** 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; }
/** 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; }
static void tinput_text_complete(tinput_t *ti) { void *state; size_t cstart; char *ctmp; char **compl; /* Array of completions */ size_t compl_len; /* Current length of @c compl array */ size_t cnum; size_t i; int rc; if (ti->compl_ops == NULL) return; /* * Obtain list of all possible completions (growing array). */ rc = (*ti->compl_ops->init)(ti->buffer, ti->pos, &cstart, &state); if (rc != EOK) return; cnum = 0; compl_len = 1; compl = malloc(compl_len * sizeof(char *)); if (compl == NULL) { printf("Error: Out of memory.\n"); return; } while (true) { rc = (*ti->compl_ops->get_next)(state, &ctmp); if (rc != EOK) break; if (cnum >= compl_len) { /* Extend array */ compl_len = 2 * compl_len; compl = realloc(compl, compl_len * sizeof(char *)); if (compl == NULL) { printf("Error: Out of memory.\n"); break; } } compl[cnum] = str_dup(ctmp); if (compl[cnum] == NULL) { printf("Error: Out of memory.\n"); break; } cnum++; } (*ti->compl_ops->fini)(state); if (cnum > 1) { /* * More than one match. Determine maximum common prefix. */ size_t cplen; cplen = str_length(compl[0]); for (i = 1; i < cnum; i++) cplen = min(cplen, common_pref_len(compl[0], compl[i])); /* Compute how many bytes we should skip. */ size_t istart = str_lsize(compl[0], ti->pos - cstart); if (cplen > istart) { /* Insert common prefix. */ /* Copy remainder of common prefix. */ char *cpref = str_ndup(compl[0] + istart, str_lsize(compl[0], cplen - istart)); /* Insert it. */ tinput_insert_string(ti, cpref); free(cpref); } else { /* No common prefix. Sort and display all entries. */ qsort(compl, cnum, sizeof(char *), compl_cmp, NULL); tinput_jump_after(ti); tinput_show_completions(ti, compl, cnum); tinput_display(ti); } } else if (cnum == 1) { /* * We have exactly one match. Insert it. */ /* Compute how many bytes of completion string we should skip. */ size_t istart = str_lsize(compl[0], ti->pos - cstart); /* Insert remainder of completion string at current position. */ tinput_insert_string(ti, compl[0] + istart); } for (i = 0; i < cnum; i++) free(compl[i]); free(compl); }
int main(int argc, char **argv) { sysarg_t baud = 38400; service_id_t svc_id; char *serial_port_name = NULL; int arg = 1; int rc; isdv4_event_fn event_fn = emit_event; if (argc > arg && str_test_prefix(argv[arg], "--baud=")) { size_t arg_offset = str_lsize(argv[arg], 7); char* arg_str = argv[arg] + arg_offset; if (str_length(arg_str) == 0) { fprintf(stderr, "--baud requires an argument\n"); syntax_print(); return 1; } char *endptr; baud = strtol(arg_str, &endptr, 10); if (*endptr != '\0') { fprintf(stderr, "Invalid value for baud\n"); syntax_print(); return 1; } arg++; } if (argc > arg && str_cmp(argv[arg], "--print-events") == 0) { event_fn = print_and_emit_event; arg++; } if (argc > arg) { serial_port_name = argv[arg]; rc = loc_service_get_id(serial_port_name, &svc_id, 0); if (rc != EOK) { fprintf(stderr, "Cannot find device service %s\n", argv[arg]); return 1; } arg++; } else { category_id_t serial_cat_id; rc = loc_category_get_id("serial", &serial_cat_id, 0); if (rc != EOK) { fprintf(stderr, "Failed getting id of category " "'serial'\n"); return 1; } service_id_t *svc_ids; size_t svc_count; rc = loc_category_get_svcs(serial_cat_id, &svc_ids, &svc_count); if (rc != EOK) { fprintf(stderr, "Failed getting list of services\n"); return 1; } if (svc_count == 0) { fprintf(stderr, "No service in category 'serial'\n"); free(svc_ids); return 1; } svc_id = svc_ids[0]; rc = loc_service_get_name(svc_id, &serial_port_name); if (rc != EOK) { fprintf(stderr, "Failed getting name of serial service\n"); return 1; } free(svc_ids); } if (argc > arg) { fprintf(stderr, "Too many arguments\n"); syntax_print(); return 1; } fibril_mutex_initialize(&client_mutex); printf(NAME ": Using serial port %s\n", serial_port_name); async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF, IPC_FLAG_BLOCKING); if (!sess) { fprintf(stderr, "Failed connecting to service\n"); } async_exch_t *exch = async_exchange_begin(sess); rc = async_req_4_0(exch, SERIAL_SET_COM_PROPS, baud, SERIAL_NO_PARITY, 8, 1); async_exchange_end(exch); if (rc != EOK) { fprintf(stderr, "Failed setting serial properties\n"); return 2; } rc = isdv4_init(&state, sess, event_fn); if (rc != EOK) { fprintf(stderr, "Failed initializing isdv4 state"); return 2; } rc = isdv4_init_tablet(&state); if (rc != EOK) { fprintf(stderr, "Failed initializing tablet"); return 2; } printf("Tablet information:\n"); printf(" Stylus: %ux%u pressure: %u tilt: ", state.stylus_max_x, state.stylus_max_y, state.stylus_max_pressure); if (state.stylus_tilt_supported) { printf("%ux%u\n", state.stylus_max_xtilt, state.stylus_max_ytilt); } else { printf("not supported\n"); } printf(" Touch: %ux%u type: %s\n", state.touch_max_x, state.touch_max_y, touch_type(state.touch_type)); fid_t fibril = fibril_create(read_fibril, NULL); /* From this on, state is to be used only by read_fibril */ fibril_add_ready(fibril); async_set_fallback_port_handler(mouse_connection, NULL); rc = loc_server_register(NAME); if (rc != EOK) { printf("%s: Unable to register driver.\n", NAME); return rc; } service_id_t service_id; char *service_name; rc = asprintf(&service_name, "mouse/isdv4-%" PRIun, svc_id); if (rc < 0) { printf(NAME ": Unable to create service name\n"); return rc; } rc = loc_service_register(service_name, &service_id); if (rc != EOK) { printf(NAME ": Unable to register service %s.\n", service_name); return rc; } category_id_t mouse_category; rc = loc_category_get_id("mouse", &mouse_category, IPC_FLAG_BLOCKING); if (rc != EOK) { printf(NAME ": Unable to get mouse category id.\n"); } else { rc = loc_service_add_to_cat(service_id, mouse_category); if (rc != EOK) { printf(NAME ": Unable to add device to mouse category.\n"); } } printf("%s: Accepting connections\n", NAME); task_retval(0); async_manager(); /* Not reached */ return 0; }
int main(int argc, char **argv) { sysarg_t baud = 9600; service_id_t svc_id; int arg = 1; int rc; if (argc > arg && str_test_prefix(argv[arg], "--baud=")) { size_t arg_offset = str_lsize(argv[arg], 7); char* arg_str = argv[arg] + arg_offset; if (str_length(arg_str) == 0) { fprintf(stderr, "--baud requires an argument\n"); syntax_print(); return 1; } char *endptr; baud = strtol(arg_str, &endptr, 10); if (*endptr != '\0') { fprintf(stderr, "Invalid value for baud\n"); syntax_print(); return 1; } arg++; } if (argc > arg) { rc = loc_service_get_id(argv[arg], &svc_id, 0); if (rc != EOK) { fprintf(stderr, "Cannot find device service %s\n", argv[arg]); return 1; } arg++; } else { category_id_t serial_cat_id; rc = loc_category_get_id("serial", &serial_cat_id, 0); if (rc != EOK) { fprintf(stderr, "Failed getting id of category " "'serial'\n"); return 1; } service_id_t *svc_ids; size_t svc_count; rc = loc_category_get_svcs(serial_cat_id, &svc_ids, &svc_count); if (rc != EOK) { fprintf(stderr, "Failed getting list of services\n"); return 1; } if (svc_count == 0) { fprintf(stderr, "No service in category 'serial'\n"); free(svc_ids); return 1; } svc_id = svc_ids[0]; free(svc_ids); } if (argc > arg) { fprintf(stderr, "Too many arguments\n"); syntax_print(); return 1; } async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF, IPC_FLAG_BLOCKING); if (!sess) { fprintf(stderr, "Failed connecting to service\n"); } async_exch_t *exch = async_exchange_begin(sess); rc = async_req_4_0(exch, SERIAL_SET_COM_PROPS, baud, SERIAL_NO_PARITY, 8, 1); async_exchange_end(exch); if (rc != EOK) { fprintf(stderr, "Failed setting serial properties\n"); return 2; } uint8_t *buf = (uint8_t *) malloc(BUF_SIZE); if (buf == NULL) { fprintf(stderr, "Failed allocating buffer\n"); return 3; } while (true) { ssize_t read = char_dev_read(sess, buf, BUF_SIZE); if (read < 0) { fprintf(stderr, "Failed reading from serial device\n"); break; } ssize_t i; for (i = 0; i < read; i++) { printf("%02hhx ", buf[i]); } fflush(stdout); } free(buf); return 0; }