static gboolean receive_line(const struct sr_dev_inst *sdi) { struct dev_context *devc; const struct agdmm_recv *recvs, *recv; GRegex *reg; GMatchInfo *match; gboolean stop = FALSE; int i; devc = sdi->priv; /* Strip CRLF */ while (devc->buflen) { if (*(devc->buf + devc->buflen - 1) == '\r' || *(devc->buf + devc->buflen - 1) == '\n') *(devc->buf + --devc->buflen) = '\0'; else break; } sr_spew("Received '%s'.", devc->buf); recv = NULL; recvs = devc->profile->recvs; for (i = 0; (&recvs[i])->recv_regex; i++) { reg = g_regex_new((&recvs[i])->recv_regex, 0, 0, NULL); if (g_regex_match(reg, (char *)devc->buf, 0, &match)) { recv = &recvs[i]; break; } g_match_info_unref(match); g_regex_unref(reg); } if (recv) { enum job_type type = recv->recv(sdi, match); if (type == job_current(devc)->type) job_done(devc); else if (type == JOB_AGAIN) job_again(devc); else if (type == JOB_STOP) stop = TRUE; g_match_info_unref(match); g_regex_unref(reg); } else sr_dbg("Unknown line '%s'.", devc->buf); /* Done with this. */ devc->buflen = 0; return stop; }
gchar *add_links_to_text(const gchar * original) { /* return if original already contains links */ if (g_regex_match_simple("<a\\s.*>", original, 0, 0)) { return g_strdup(original); } gchar *newText, *url, *link; GMatchInfo *match_info; GRegex *regex = g_regex_new("(https?://[a-zA-Z0-9_\\-\\./?=&]+)", 0, 0, NULL); // extract url and build escaped link g_regex_match(regex, original, 0, &match_info); url = g_match_info_fetch(match_info, 1); if(url) { link = g_markup_printf_escaped("<a href='%s'>%s</a>", url, url); newText = g_regex_replace(regex, original, -1, 0, link, 0, NULL); g_free(url); g_free(link); } else { newText = g_strdup(original); } g_regex_unref(regex); g_match_info_unref(match_info); return newText; }
static VALUE rg_match_all(gint argc, VALUE *argv, VALUE self) { VALUE rb_string, rb_start_position, rb_match_options, rb_options; VALUE rb_frozen_string, rb_match_info; GMatchInfo *match_info = NULL; GError *error = NULL; const gchar *string; gssize string_len = -1; gint start_position = 0; GRegexMatchFlags match_options = 0; rb_scan_args(argc, argv, "11", &rb_string, &rb_options); rbg_scan_options(rb_options, "start_position", &rb_start_position, "match_options", &rb_match_options, NULL); if (OBJ_FROZEN(rb_string)) { rb_frozen_string = rb_string; } else { rb_frozen_string = rb_str_dup(rb_string); rb_str_freeze(rb_frozen_string); } string = RVAL2CSTR(rb_frozen_string); string_len = RSTRING_LEN(rb_frozen_string); if (!NIL_P(rb_start_position)) start_position = NUM2INT(rb_start_position); if (!NIL_P(rb_match_options)) match_options = RVAL2GREGEXMATCHOPTIONSFLAGS(rb_match_options); g_regex_match_all_full(_SELF(self), string, string_len, start_position, match_options, &match_info, &error); if (error) RAISE_GERROR(error); if (!match_info) return Qnil; rb_match_info = GMATCHINFO2RVAL(match_info); g_match_info_unref(match_info); rb_iv_set(rb_match_info, "@string", rb_frozen_string); return rb_match_info; }
static void receive_line(const struct sr_dev_inst *sdi) { struct dev_context *devc; const struct agdmm_recv *recvs, *recv; GRegex *reg; GMatchInfo *match; int i; devc = sdi->priv; /* Strip CRLF */ while (devc->buflen) { if (*(devc->buf + devc->buflen - 1) == '\r' || *(devc->buf + devc->buflen - 1) == '\n') *(devc->buf + --devc->buflen) = '\0'; else break; } sr_spew("Received '%s'.", devc->buf); recv = NULL; recvs = devc->profile->recvs; for (i = 0; (&recvs[i])->recv_regex; i++) { reg = g_regex_new((&recvs[i])->recv_regex, 0, 0, NULL); if (g_regex_match(reg, (char *)devc->buf, 0, &match)) { recv = &recvs[i]; break; } g_match_info_unref(match); g_regex_unref(reg); } if (recv) { recv->recv(sdi, match); g_match_info_unref(match); g_regex_unref(reg); } else sr_dbg("Unknown line '%s'.", devc->buf); /* Done with this. */ devc->buflen = 0; }
redirect_cb(WebKitWebView* web_view, WebKitPolicyDecision* decision, WebKitPolicyDecisionType type, gpointer udata) #endif { GtTwitchLoginDlg* self = GT_TWITCH_LOGIN_DLG(udata); GtTwitchLoginDlgPrivate* priv = gt_twitch_login_dlg_get_instance_private(self); GMatchInfo* match_info = NULL; const gchar* uri = NULL; #ifdef USE_DEPRECATED_WEBKIT uri = webkit_network_request_get_uri(request); #else if (type == WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION) { WebKitNavigationAction* action = webkit_navigation_policy_decision_get_navigation_action( WEBKIT_NAVIGATION_POLICY_DECISION(decision)); WebKitURIRequest* request = webkit_navigation_action_get_request(action); uri = webkit_uri_request_get_uri(request); } #endif if (uri == NULL || strlen(uri) == 0) return FALSE; MESSAGE("Redirect uri is '%s'", uri); g_regex_match(priv->token_redirect_regex, uri, 0, &match_info); if (g_match_info_matches(match_info)) { g_autofree gchar* token = g_match_info_fetch(match_info, 1); MESSAGEF("Successfully got OAuth token '%s'", token); gt_twitch_fetch_oauth_info_async(main_app->twitch, token, fetch_oauth_info_cb, priv->cancel, self); } else if (g_str_has_prefix(uri, "http://localhost/?error=access_denied")) WARNING("Error logging in or login cancelled"); g_match_info_unref(match_info); return FALSE; }
static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) { struct dev_context *devc; struct sr_dev_inst *sdi; struct sr_scpi_hw_info *hw_info; struct sr_channel_group *cg; struct sr_channel *ch; const struct scpi_pps *device; struct pps_channel *pch; struct channel_spec *channels; struct channel_group_spec *channel_groups, *cgs; struct pps_channel_group *pcg; GRegex *model_re; GMatchInfo *model_mi; GSList *l; uint64_t mask; unsigned int num_channels, num_channel_groups, ch_num, ch_idx, i, j; int ret; const char *vendor; char ch_name[16]; if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { sr_info("Couldn't get IDN response."); return NULL; } device = NULL; for (i = 0; i < num_pps_profiles; i++) { vendor = sr_vendor_alias(hw_info->manufacturer); if (g_ascii_strcasecmp(vendor, pps_profiles[i].vendor)) continue; model_re = g_regex_new(pps_profiles[i].model, 0, 0, NULL); if (g_regex_match(model_re, hw_info->model, 0, &model_mi)) device = &pps_profiles[i]; g_match_info_unref(model_mi); g_regex_unref(model_re); if (device) break; } if (!device) { sr_scpi_hw_info_free(hw_info); return NULL; } sdi = g_malloc0(sizeof(struct sr_dev_inst)); sdi->status = SR_ST_INACTIVE; sdi->vendor = g_strdup(vendor); sdi->model = g_strdup(hw_info->model); sdi->version = g_strdup(hw_info->firmware_version); sdi->conn = scpi; sdi->driver = &scpi_pps_driver_info; sdi->inst_type = SR_INST_SCPI; sdi->serial_num = g_strdup(hw_info->serial_number); devc = g_malloc0(sizeof(struct dev_context)); devc->device = device; sdi->priv = devc; if (device->num_channels) { /* Static channels and groups. */ channels = (struct channel_spec *)device->channels; num_channels = device->num_channels; channel_groups = (struct channel_group_spec *)device->channel_groups; num_channel_groups = device->num_channel_groups; } else { /* Channels and groups need to be probed. */ ret = device->probe_channels(sdi, hw_info, &channels, &num_channels, &channel_groups, &num_channel_groups); if (ret != SR_OK) { sr_err("Failed to probe for channels."); return NULL; } /* * Since these were dynamically allocated, we'll need to free them * later. */ devc->channels = channels; devc->channel_groups = channel_groups; } ch_idx = 0; for (ch_num = 0; ch_num < num_channels; ch_num++) { /* Create one channel per measurable output unit. */ for (i = 0; i < ARRAY_SIZE(pci); i++) { if (!scpi_cmd_get(devc->device->commands, pci[i].command)) continue; g_snprintf(ch_name, 16, "%s%s", pci[i].prefix, channels[ch_num].name); ch = sr_channel_new(sdi, ch_idx++, SR_CHANNEL_ANALOG, TRUE, ch_name); pch = g_malloc0(sizeof(struct pps_channel)); pch->hw_output_idx = ch_num; pch->hwname = channels[ch_num].name; pch->mq = pci[i].mq; ch->priv = pch; } } for (i = 0; i < num_channel_groups; i++) { cgs = &channel_groups[i]; cg = g_malloc0(sizeof(struct sr_channel_group)); cg->name = g_strdup(cgs->name); for (j = 0, mask = 1; j < 64; j++, mask <<= 1) { if (cgs->channel_index_mask & mask) { for (l = sdi->channels; l; l = l->next) { ch = l->data; pch = ch->priv; if (pch->hw_output_idx == j) cg->channels = g_slist_append(cg->channels, ch); } } } pcg = g_malloc0(sizeof(struct pps_channel_group)); pcg->features = cgs->features; cg->priv = pcg; sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); } sr_scpi_hw_info_free(hw_info); hw_info = NULL; scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL); sr_scpi_close(scpi); return sdi; }
/** * Find USB devices according to a connection string. * * @param usb_ctx libusb context to use while scanning. * @param conn Connection string specifying the device(s) to match. This * can be of the form "<bus>.<address>", or "<vendorid>.<productid>". * * @return A GSList of struct sr_usb_dev_inst, with bus and address fields * matching the device that matched the connection string. The GSList and * its contents must be freed by the caller. */ SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn) { struct sr_usb_dev_inst *usb; struct libusb_device **devlist; struct libusb_device_descriptor des; GSList *devices; GRegex *reg; GMatchInfo *match; int vid, pid, bus, addr, b, a, ret, i; char *mstr; vid = pid = bus = addr = 0; reg = g_regex_new(CONN_USB_VIDPID, 0, 0, NULL); if (g_regex_match(reg, conn, 0, &match)) { if ((mstr = g_match_info_fetch(match, 1))) vid = strtoul(mstr, NULL, 16); g_free(mstr); if ((mstr = g_match_info_fetch(match, 2))) pid = strtoul(mstr, NULL, 16); g_free(mstr); sr_dbg("Trying to find USB device with VID:PID = %04x:%04x.", vid, pid); } else { g_match_info_unref(match); g_regex_unref(reg); reg = g_regex_new(CONN_USB_BUSADDR, 0, 0, NULL); if (g_regex_match(reg, conn, 0, &match)) { if ((mstr = g_match_info_fetch(match, 1))) bus = strtoul(mstr, NULL, 10); g_free(mstr); if ((mstr = g_match_info_fetch(match, 2))) addr = strtoul(mstr, NULL, 10); g_free(mstr); sr_dbg("Trying to find USB device with bus.address = " "%d.%d.", bus, addr); } } g_match_info_unref(match); g_regex_unref(reg); if (vid + pid + bus + addr == 0) { sr_err("Neither VID:PID nor bus.address was specified."); return NULL; } if (bus > 64) { sr_err("Invalid bus specified: %d.", bus); return NULL; } if (addr > 127) { sr_err("Invalid address specified: %d.", addr); return NULL; } /* Looks like a valid USB device specification, but is it connected? */ devices = NULL; libusb_get_device_list(usb_ctx, &devlist); for (i = 0; devlist[i]; i++) { if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { sr_err("Failed to get device descriptor: %s.", libusb_error_name(ret)); continue; } if (vid + pid && (des.idVendor != vid || des.idProduct != pid)) continue; b = libusb_get_bus_number(devlist[i]); a = libusb_get_device_address(devlist[i]); if (bus + addr && (b != bus || a != addr)) continue; sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = " "%d.%d).", des.idVendor, des.idProduct, b, a); usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), libusb_get_device_address(devlist[i]), NULL); devices = g_slist_append(devices, usb); } libusb_free_device_list(devlist, 1); sr_dbg("Found %d device(s).", g_slist_length(devices)); return devices; }
/** * Set serial parameters for the specified serial port from parameter string. * * @param serial Previously initialized serial port structure. * @param[in] paramstr A serial communication parameters string of the form * "<baudrate>/<bits><parity><stopbits>{/<option>}".\n * Examples: "9600/8n1", "600/7o2/dtr=1/rts=0" or "460800/8n1/flow=2".\n * \<baudrate\>=integer Baud rate.\n * \<bits\>=5|6|7|8 Number of data bits.\n * \<parity\>=n|e|o None, even, odd.\n * \<stopbits\>=1|2 One or two stop bits.\n * Options:\n * dtr=0|1 Set DTR off resp. on.\n * flow=0|1|2 Flow control. 0 for none, 1 for RTS/CTS, 2 for XON/XOFF.\n * rts=0|1 Set RTS off resp. on.\n * Please note that values and combinations of these parameters must be * supported by the concrete serial interface hardware and the drivers for it. * * @retval SR_OK Success. * @retval SR_ERR Failure. * * @private */ SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial, const char *paramstr) { /** @cond PRIVATE */ #define SERIAL_COMM_SPEC "^(\\d+)/([5678])([neo])([12])(.*)$" /** @endcond */ GRegex *reg; GMatchInfo *match; int speed, databits, parity, stopbits, flow, rts, dtr, i; char *mstr, **opts, **kv; speed = databits = parity = stopbits = flow = 0; rts = dtr = -1; sr_spew("Parsing parameters from \"%s\".", paramstr); reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL); if (g_regex_match(reg, paramstr, 0, &match)) { if ((mstr = g_match_info_fetch(match, 1))) speed = strtoul(mstr, NULL, 10); g_free(mstr); if ((mstr = g_match_info_fetch(match, 2))) databits = strtoul(mstr, NULL, 10); g_free(mstr); if ((mstr = g_match_info_fetch(match, 3))) { switch (mstr[0]) { case 'n': parity = SP_PARITY_NONE; break; case 'e': parity = SP_PARITY_EVEN; break; case 'o': parity = SP_PARITY_ODD; break; } } g_free(mstr); if ((mstr = g_match_info_fetch(match, 4))) stopbits = strtoul(mstr, NULL, 10); g_free(mstr); if ((mstr = g_match_info_fetch(match, 5)) && mstr[0] != '\0') { if (mstr[0] != '/') { sr_dbg("missing separator before extra options"); speed = 0; } else { /* A set of "key=value" options separated by / */ opts = g_strsplit(mstr + 1, "/", 0); for (i = 0; opts[i]; i++) { kv = g_strsplit(opts[i], "=", 2); if (!strncmp(kv[0], "rts", 3)) { if (kv[1][0] == '1') rts = 1; else if (kv[1][0] == '0') rts = 0; else { sr_dbg("invalid value for rts: %c", kv[1][0]); speed = 0; } } else if (!strncmp(kv[0], "dtr", 3)) { if (kv[1][0] == '1') dtr = 1; else if (kv[1][0] == '0') dtr = 0; else { sr_dbg("invalid value for dtr: %c", kv[1][0]); speed = 0; } } else if (!strncmp(kv[0], "flow", 4)) { if (kv[1][0] == '0') flow = 0; else if (kv[1][0] == '1') flow = 1; else if (kv[1][0] == '2') flow = 2; else { sr_dbg("invalid value for flow: %c", kv[1][0]); speed = 0; } } g_strfreev(kv); } g_strfreev(opts); } } g_free(mstr); } g_match_info_unref(match); g_regex_unref(reg); if (speed) { return serial_set_params(serial, speed, databits, parity, stopbits, flow, rts, dtr); } else { sr_dbg("Could not infer speed from parameter string."); return SR_ERR_ARG; } }
static void match_info_cleanup (void *loc) { GMatchInfo **match = (GMatchInfo**)loc; if (*match) g_match_info_unref (*match); }
static GFile * filebox_uploader_handle_task_create_file (HevFileboxUploader *self, GHashTable *res_htb, const gchar *fp_path, const gchar *fm_path, const gchar *filename, const gchar *contents, gsize length) { GFile *file = NULL, *meta = NULL; GRegex *regex = NULL; GMatchInfo *match_info = NULL; gchar *file_name = NULL, *basename = NULL, *file_path = NULL, *meta_path = NULL; GFileOutputStream *file_ostream = NULL; g_debug ("%s:%d[%s]", __FILE__, __LINE__, __FUNCTION__); /* fetch filename */ regex = g_regex_new ("filename=\"(.+)\"", 0, 0, NULL); if (g_regex_match (regex, filename, 0, &match_info)) file_name = g_match_info_fetch (match_info, 1); g_match_info_unref (match_info); g_regex_unref (regex); /* build file path and meta path */ basename = g_path_get_basename (file_name); g_free (file_name); file_path = g_build_filename (fp_path, basename, NULL); meta_path = g_build_filename (fm_path, basename, NULL); g_free (basename); /* new file */ file = g_file_new_for_path (file_path); g_free (file_path); if (!file) { g_hash_table_insert (res_htb, g_strdup ("Status"), g_strdup ("500 Internal Server Error")); g_free (meta_path); return NULL; } /* craete file */ file_ostream = g_file_create (file, G_FILE_CREATE_PRIVATE, NULL, NULL); if (!file_ostream) { g_hash_table_insert (res_htb, g_strdup ("Status"), g_strdup ("403 Forbidden")); g_object_unref (file); g_free (meta_path); return NULL; } /* write contents to file */ if (!g_output_stream_write_all (G_OUTPUT_STREAM (file_ostream), contents, length, NULL, NULL, NULL)) { g_hash_table_insert (res_htb, g_strdup ("Status"), g_strdup ("500 Internal Server Error")); g_object_unref (file_ostream); g_file_delete (file, NULL, NULL); g_object_unref (file); g_free (meta_path); return NULL; } /* new meta file */ meta = g_file_new_for_path (meta_path); g_free (meta_path); if (!meta) { g_hash_table_insert (res_htb, g_strdup ("Status"), g_strdup ("500 Internal Server Error")); g_object_unref (file_ostream); g_file_delete (file, NULL, NULL); g_object_unref (file); return NULL; } g_object_set_data_full (G_OBJECT (file), "meta", meta, (GDestroyNotify) g_object_unref); g_object_unref (file_ostream); return file; }
static void filebox_uploader_handle_task_handler (GTask *task, gpointer source_object, gpointer task_data, GCancellable *cancellable) { HevFileboxUploader *self = HEV_FILEBOX_UPLOADER (source_object); HevFileboxUploaderPrivate *priv = HEV_FILEBOX_UPLOADER_GET_PRIVATE (self); GObject *scgi_task = task_data; gboolean status = TRUE; GObject *request = NULL; GInputStream *req_stream = NULL; GHashTable *req_htb = NULL; GObject *response = NULL; GOutputStream *res_stream = NULL; GHashTable *res_htb = NULL; const gchar *content_type = NULL, *content_length = NULL; GRegex *regex = NULL; GMatchInfo *match_info = NULL; gchar rand_pass[16], *boundary = NULL, *fp_path = NULL, *fm_path = NULL, *ft_path = NULL; gint rand_pass_len; guint64 length = 0; GFile *file_tmp = NULL; g_debug ("%s:%d[%s]", __FILE__, __LINE__, __FUNCTION__); request = hev_scgi_task_get_request (HEV_SCGI_TASK (scgi_task)); req_stream = hev_scgi_request_get_input_stream (HEV_SCGI_REQUEST (request)); req_htb = hev_scgi_request_get_header_hash_table (HEV_SCGI_REQUEST (request)); response = hev_scgi_task_get_response (HEV_SCGI_TASK (scgi_task)); res_stream = hev_scgi_response_get_output_stream (HEV_SCGI_RESPONSE (response)); res_htb = hev_scgi_response_get_header_hash_table (HEV_SCGI_RESPONSE (response)); content_type = g_hash_table_lookup (req_htb, "CONTENT_TYPE"); content_length = g_hash_table_lookup (req_htb, "CONTENT_LENGTH"); /* get boundary string from content type */ regex = g_regex_new ("^multipart/form-data;\\s*boundary=(.+)$", 0, 0, NULL); if (!g_regex_match (regex, content_type, 0, &match_info)) { g_hash_table_insert (res_htb, g_strdup ("Status"), g_strdup ("400 Bad Request")); hev_scgi_response_write_header (HEV_SCGI_RESPONSE (response), NULL); g_regex_unref (regex); g_task_return_boolean (task, FALSE); return; } boundary = g_match_info_fetch (match_info, 1); g_match_info_unref (match_info); g_regex_unref (regex); fp_path = g_key_file_get_string (priv->config, "Module", "FilePoolPath", NULL); fm_path = g_key_file_get_string (priv->config, "Module", "FileMetaPath", NULL); ft_path = g_key_file_get_string (priv->config, "Module", "FileTempPath", NULL); length = g_ascii_strtoull (content_length, NULL, 10); rand_pass_len = g_snprintf (rand_pass, 16, "%u", g_random_int_range (99999, 999999)); g_object_set_data (scgi_task, "rand-pass", rand_pass); /* create tmp file */ file_tmp = filebox_uploader_handle_task_create_tmp (self, scgi_task, req_stream, req_htb, res_htb, ft_path, length); if (file_tmp) { gchar *path = NULL; GMappedFile *mapped_file = NULL; path = g_file_get_path (file_tmp); mapped_file = g_mapped_file_new (path, FALSE, NULL); g_free (path); if (mapped_file) { GPtrArray *files = NULL; gchar *duration = NULL, *one_off = NULL; /* split files from tmp file */ files = filebox_uploader_handle_task_split_tmp (self, scgi_task, mapped_file, res_htb, fp_path, fm_path, boundary, &duration, &one_off); if (files) { /* write meta files */ g_object_set_data (scgi_task, "duration", duration); g_object_set_data (scgi_task, "one-off", one_off); g_ptr_array_foreach (files, file_ptr_array_foreach_write_meta_handler, scgi_task); g_ptr_array_unref (files); } g_free (duration); g_free (one_off); g_mapped_file_unref (mapped_file); } g_file_delete (file_tmp, NULL, NULL); g_object_unref (file_tmp); } g_free (boundary); g_free (fp_path); g_free (fm_path); g_free (ft_path); if (!g_hash_table_contains (res_htb, "Status")) g_hash_table_insert (res_htb, g_strdup ("Status"), g_strdup ("200 OK")); hev_scgi_response_write_header (HEV_SCGI_RESPONSE (response), NULL); g_output_stream_write_all (res_stream, rand_pass, rand_pass_len, NULL, NULL, NULL); g_task_return_boolean (task, status); }
static int parse_conn_busaddr(struct sr_dev_inst *sdi, const char *conn) { struct context *ctx; libusb_device **devlist; struct libusb_device_descriptor des; GRegex *reg; GMatchInfo *match; int bus, addr, found, err, i; char *busstr, *addrstr; found = FALSE; reg = g_regex_new(DMM_CONN_USB_BUSADDR, 0, 0, NULL); if (g_regex_match(reg, conn, 0, &match)) { /* Extract bus. */ if (!(busstr = g_match_info_fetch(match, 0))) { sr_err("failed to fetch bus from regex"); goto err; } bus = strtoul(busstr, NULL, 16); g_free(busstr); if (bus > 64) { sr_err("invalid bus"); goto err; } /* Extract address. */ if (!(addrstr = g_match_info_fetch(match, 0))) { sr_err("failed to fetch address from regex"); goto err; } addr = strtoul(addrstr, NULL, 16); g_free(addrstr); if (addr > 127) { sr_err("invalid address"); goto err; } /* Looks like a valid bus/address, but is it connected? */ libusb_get_device_list(genericdmm_usb_context, &devlist); for (i = 0; devlist[i]; i++) { if ((err = libusb_get_device_descriptor(devlist[i], &des))) { sr_err("genericdmm: failed to get device descriptor: %d", err); goto err; } if (libusb_get_bus_number(devlist[i]) == bus && libusb_get_device_address(devlist[i]) == addr) { ctx = sdi->priv; ctx->usb = sr_usb_dev_inst_new(bus, addr, NULL); found = TRUE; break; } } libusb_free_device_list(devlist, 1); } err: if (match) g_match_info_unref(match); g_regex_unref(reg); return found; }
static int parse_conn_vidpid(struct sr_dev_inst *sdi, const char *conn) { struct context *ctx; libusb_device **devlist; struct libusb_device_descriptor des; GRegex *reg; GMatchInfo *match; int vid, pid, found, err, i; char *vidstr, *pidstr; found = FALSE; reg = g_regex_new(DMM_CONN_USB_VIDPID, 0, 0, NULL); if (g_regex_match(reg, conn, 0, &match)) { /* Extract VID. */ if (!(vidstr = g_match_info_fetch(match, 0))) { sr_err("failed to fetch VID from regex"); goto err; } vid = strtoul(vidstr, NULL, 16); g_free(vidstr); if (vid > 0xffff) { sr_err("invalid VID"); goto err; } /* Extract PID. */ if (!(pidstr = g_match_info_fetch(match, 0))) { sr_err("failed to fetch PID from regex"); goto err; } pid = strtoul(pidstr, NULL, 16); g_free(pidstr); if (pid > 0xffff) { sr_err("invalid PID"); goto err; } /* Looks like a valid VID:PID, but is it connected? */ libusb_get_device_list(genericdmm_usb_context, &devlist); for (i = 0; devlist[i]; i++) { if ((err = libusb_get_device_descriptor(devlist[i], &des))) { sr_err("genericdmm: failed to get device descriptor: %d", err); goto err; } if (des.idVendor == vid && des.idProduct == pid) { ctx = sdi->priv; ctx->usb = sr_usb_dev_inst_new( libusb_get_bus_number(devlist[i]), libusb_get_device_address(devlist[i]), NULL); found = TRUE; break; } } libusb_free_device_list(devlist, 1); } err: if (match) g_match_info_unref(match); g_regex_unref(reg); return found; }
int main(int ac, char* av[]) { GError *local_err = NULL; GRegex *file_regex, *folder_regex; gchar* key; gchar* handle; gint i; tool_init_bare(&ac, &av, "- download exported files from mega.co.nz", entries); if (!strcmp(opt_path, "-")) opt_noprogress = opt_stream = TRUE; if (ac < 2) { g_printerr("ERROR: No links specified for download!\n"); tool_fini(NULL); return 1; } if (opt_stream && ac != 2) { g_printerr("ERROR: Can't stream from multiple files!\n"); tool_fini(NULL); return 1; } // prepare link parsers file_regex = g_regex_new("^https?://mega.co.nz/#!([a-z0-9_-]{8})!([a-z0-9_-]{43})$", G_REGEX_CASELESS, 0, NULL); g_assert(file_regex != NULL); folder_regex = g_regex_new("^https?://mega.co.nz/#F!([a-z0-9_-]{8})!([a-z0-9_-]{22})$", G_REGEX_CASELESS, 0, NULL); g_assert(folder_regex != NULL); // create session s = mega_session_new(); mega_session_watch_status(s, status_callback, NULL); // process links for (i = 1; i < ac; i++) { GMatchInfo* m1 = NULL; GMatchInfo* m2 = NULL; key = NULL; handle = NULL; if (g_regex_match(file_regex, av[i], 0, &m1)) { handle = g_match_info_fetch(m1, 1); key = g_match_info_fetch(m1, 2); // perform download if (!mega_session_dl(s, handle, key, opt_stream ? NULL : opt_path, &local_err)) { if (!opt_noprogress) g_print("\r" ESC_CLREOL "\n"); g_printerr("ERROR: Download failed for '%s': %s\n", av[i], local_err->message); g_clear_error(&local_err); } else { if (!opt_noprogress) g_print("\r" ESC_CLREOL "Downloaded %s\n", cur_file); } } else if (g_regex_match(folder_regex, av[i], 0, &m2)) { if (opt_stream) { g_printerr("ERROR: Can't stream from a directory!\n"); if (m1) g_match_info_unref(m1); if (m2) g_match_info_unref(m2); tool_fini(s); return 1; } handle = g_match_info_fetch(m2, 1); key = g_match_info_fetch(m2, 2); // perform download if (!mega_session_open_exp_folder(s, handle, key, &local_err)) { g_printerr("ERROR: Can't open folder '%s': %s\n", av[i], local_err->message); g_clear_error(&local_err); } else { GSList* l = mega_session_ls(s, "/", FALSE); if (g_slist_length(l) == 1) { mega_node* root_node = l->data; GFile* local_dir = g_file_new_for_path(opt_path); if (g_file_query_file_type(local_dir, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL) == G_FILE_TYPE_DIRECTORY) { dl_sync_dir(root_node, local_dir, root_node->path); } else { g_printerr("ERROR: %s must be a directory\n", opt_path); } } else { g_printerr("ERROR: EXP folder fs has multiple toplevel nodes? Weird!\n"); } } } else { g_printerr("WARNING: Skipping invalid Mega download link: %s\n", av[i]); } if (m1) g_match_info_unref(m1); if (m2) g_match_info_unref(m2); g_free(handle); g_free(key); } tool_fini(s); return 0; }