SANE_Status tcp_dev_open (struct device *dev) { SANE_Status status; char* strhost; char* strport; int port; struct servent *sp; struct timeval tv; SANE_String_Const devname; devname = dev->sane.name; DBG (3, "%s: open %s\n", __FUNCTION__, devname); if (strncmp (devname, "tcp", 3) != 0) return SANE_STATUS_INVAL; devname += 3; devname = sanei_config_skip_whitespace (devname); if (!*devname) return SANE_STATUS_INVAL; devname = sanei_config_get_string (devname, &strhost); devname = sanei_config_skip_whitespace (devname); if (*devname) devname = sanei_config_get_string (devname, &strport); else strport = "9400"; if (isdigit(*strport)) { port = atoi(strport); } else { if ((sp = getservbyname(strport, "tcp"))) { port = ntohs(sp->s_port); } else { DBG (1, "%s: unknown TCP service %s\n", __FUNCTION__, strport); return SANE_STATUS_IO_ERROR; } } status = sanei_tcp_open(strhost, port, &dev->dn); if (status == SANE_STATUS_GOOD) { tv.tv_sec = RECV_TIMEOUT; tv.tv_usec = 0; if (setsockopt (dev->dn, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof tv) < 0) { DBG(1, "%s: setsockopts %s", __FUNCTION__, strerror(errno)); } } return status; }
SANE_Status sane_init (SANE_Int * piVersion, SANE_Auth_Callback pfnAuth) { FILE *conf_fp; /* Config file stream */ SANE_Char line[PATH_MAX]; SANE_Char *str = NULL; SANE_String_Const proper_str; int nline = 0; /* prevent compiler from complaing about unused parameters */ pfnAuth = pfnAuth; strcpy(usb_devfile, "/dev/usb/scanner0"); _pFirstSaneDev = 0; iNumSaneDev = 0; InitHp5400_internal(); DBG_INIT (); HP5400_DBG (DBG_MSG, "sane_init: SANE hp5400 backend version %d.%d-%d (from %s)\n", SANE_CURRENT_MAJOR, V_MINOR, BUILD, PACKAGE_STRING); sanei_usb_init (); conf_fp = sanei_config_open (HP5400_CONFIG_FILE); iNumSaneDev = 0; if (conf_fp) { HP5400_DBG (DBG_MSG, "Reading config file\n"); while (sanei_config_read (line, sizeof (line), conf_fp)) { ++nline; if (str) { free (str); } proper_str = sanei_config_get_string (line, &str); /* Discards white lines and comments */ if (!str || proper_str == line || str[0] == '#') { HP5400_DBG (DBG_MSG, "Discarding line %d\n", nline); } else { /* If line's not blank or a comment, then it's the device * filename or a usb directive. */ HP5400_DBG (DBG_MSG, "Trying to attach %s\n", line); sanei_usb_attach_matching_devices (line, attach_one_device); } } /* while */ fclose (conf_fp); } else { HP5400_DBG (DBG_ERR, "Unable to read config file \"%s\": %s\n", HP5400_CONFIG_FILE, strerror (errno)); HP5400_DBG (DBG_MSG, "Using default built-in values\n"); attach_one_device (usb_devfile); } if (piVersion != NULL) { *piVersion = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, V_MINOR, BUILD); } return SANE_STATUS_GOOD; }
SANE_Status sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize) { SANE_Char line[PATH_MAX]; SANE_Char *word, *end; SANE_String_Const cp; SANE_Int linenumber; FILE *fp; DBG_INIT (); DBG (2, "SANE Mustek USB backend version %d.%d build %d from %s\n", SANE_CURRENT_MAJOR, V_MINOR, BUILD, PACKAGE_STRING); if (version_code) *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, V_MINOR, BUILD); DBG (5, "sane_init: authorize %s null\n", authorize ? "!=" : "=="); num_devices = 0; first_dev = 0; first_handle = 0; devlist = 0; new_dev = 0; new_dev_len = 0; new_dev_alloced = 0; sanei_usb_init (); fp = sanei_config_open (MUSTEK_USB_CONFIG_FILE); if (!fp) { /* default to /dev/usb/scanner instead of insisting on config file */ DBG (3, "sane_init: couldn't open config file `%s': %s. Using " "/dev/usb/scanner directly\n", MUSTEK_USB_CONFIG_FILE, strerror (errno)); attach ("/dev/usb/scanner", 0, SANE_FALSE); return SANE_STATUS_GOOD; } linenumber = 0; DBG (4, "sane_init: reading config file `%s'\n", MUSTEK_USB_CONFIG_FILE); while (sanei_config_read (line, sizeof (line), fp)) { word = 0; linenumber++; cp = sanei_config_get_string (line, &word); if (!word || cp == line) { DBG (5, "sane_init: config file line %d: ignoring empty line\n", linenumber); if (word) free (word); continue; } if (word[0] == '#') { DBG (5, "sane_init: config file line %d: ignoring comment line\n", linenumber); free (word); continue; } if (strcmp (word, "option") == 0) { free (word); word = 0; cp = sanei_config_get_string (cp, &word); if (!word) { DBG (1, "sane_init: config file line %d: missing quotation mark?\n", linenumber); continue; } if (strcmp (word, "max_block_size") == 0) { free (word); word = 0; cp = sanei_config_get_string (cp, &word); if (!word) { DBG (1, "sane_init: config file line %d: missing quotation mark?\n", linenumber); continue; } errno = 0; max_block_size = strtol (word, &end, 0); if (end == word) { DBG (3, "sane-init: config file line %d: max_block_size " "must have a parameter; using 8192 bytes\n", linenumber); max_block_size = 8192; } if (errno) { DBG (3, "sane-init: config file line %d: max_block_size `%s' " "is invalid (%s); using 8192 bytes\n", linenumber, word, strerror (errno)); max_block_size = 8192; } else { DBG (3, "sane_init: config file line %d: max_block_size set " "to %d bytes\n", linenumber, max_block_size); } if (word) free (word); word = 0; } else if (strcmp (word, "1200ub") == 0) { if (new_dev_len > 0) { /* this is a 1200 UB */ new_dev[new_dev_len - 1]->chip->scanner_type = MT_1200UB; new_dev[new_dev_len - 1]->sane.model = "1200 UB"; DBG (3, "sane_init: config file line %d: `%s' is a Mustek " "1200 UB\n", linenumber, new_dev[new_dev_len - 1]->sane.name); } else { DBG (3, "sane_init: config file line %d: option " "1200ub ignored, was set before any device " "name\n", linenumber); } if (word) free (word); word = 0; } else if (strcmp (word, "1200cu") == 0) { if (new_dev_len > 0) { /* this is a 1200 CU */ new_dev[new_dev_len - 1]->chip->scanner_type = MT_1200CU; new_dev[new_dev_len - 1]->sane.model = "1200 CU"; DBG (3, "sane_init: config file line %d: `%s' is a Mustek " "1200 CU\n", linenumber, new_dev[new_dev_len - 1]->sane.name); } else { DBG (3, "sane_init: config file line %d: option " "1200cu ignored, was set before any device " "name\n", linenumber); } if (word) free (word); word = 0; } else if (strcmp (word, "1200cu_plus") == 0) { if (new_dev_len > 0) { /* this is a 1200 CU Plus */ new_dev[new_dev_len - 1]->chip->scanner_type = MT_1200CU_PLUS; new_dev[new_dev_len - 1]->sane.model = "1200 CU Plus"; DBG (3, "sane_init: config file line %d: `%s' is a Mustek " "1200 CU Plus\n", linenumber, new_dev[new_dev_len - 1]->sane.name); } else { DBG (3, "sane_init: config file line %d: option " "1200cu_plus ignored, was set before any device " "name\n", linenumber); } if (word) free (word); word = 0; } else if (strcmp (word, "600cu") == 0) { if (new_dev_len > 0) { /* this is a 600 CU */ new_dev[new_dev_len - 1]->chip->scanner_type = MT_600CU; new_dev[new_dev_len - 1]->sane.model = "600 CU"; DBG (3, "sane_init: config file line %d: `%s' is a Mustek " "600 CU\n", linenumber, new_dev[new_dev_len - 1]->sane.name); } else { DBG (3, "sane_init: config file line %d: option " "600cu ignored, was set before any device " "name\n", linenumber); } if (word) free (word); word = 0; } else { DBG (3, "sane_init: config file line %d: option " "%s is unknown\n", linenumber, word); if (word) free (word); word = 0; } } else { new_dev_len = 0; DBG (4, "sane_init: config file line %d: trying to attach `%s'\n", linenumber, line); sanei_usb_attach_matching_devices (line, attach_one_device); if (word) free (word); word = 0; } } if (new_dev_alloced > 0) { new_dev_len = new_dev_alloced = 0; free (new_dev); } fclose (fp); DBG (5, "sane_init: exit\n"); return SANE_STATUS_GOOD; }
SANE_Status sanei_configure_attach (const char *config_file, SANEI_Config * config, SANE_Status (*attach) (SANEI_Config * config, const char *devname)) { SANE_Char line[PATH_MAX]; SANE_Char *token, *string; SANE_Int len; const char *lp, *lp2; FILE *fp; SANE_Status status = SANE_STATUS_GOOD; int i, j, count; void *value = NULL; int size=0; SANE_Bool found; SANE_Word *wa; SANE_Bool *ba; DBG (3, "sanei_configure_attach: start\n"); /* open configuration file */ fp = sanei_config_open (config_file); if (!fp) { DBG (2, "sanei_configure_attach: couldn't access %s\n", config_file); DBG (3, "sanei_configure_attach: exit\n"); return SANE_STATUS_ACCESS_DENIED; } /* loop reading the configuration file, all line beginning by "option " are * parsed for value to store in configuration structure, other line are * used are device to try to attach */ while (sanei_config_read (line, PATH_MAX, fp) && status == SANE_STATUS_GOOD) { /* skip white spaces at beginning of line */ lp = sanei_config_skip_whitespace (line); /* skip empty lines */ if (*lp == 0) continue; /* skip comment line */ if (line[0] == '#') continue; len = strlen (line); /* delete newline characters at end */ if (line[len - 1] == '\n') line[--len] = '\0'; lp2 = lp; /* to ensure maximum compatibility, we accept line like: * option "option_name" "option_value" * "option_name" "option_value" * So we parse the line 2 time to find an option */ /* check if it is an option */ lp = sanei_config_get_string (lp, &token); if (strncmp (token, "option", 6) == 0) { /* skip the "option" token */ free (token); lp = sanei_config_get_string (lp, &token); } /* search for a matching descriptor */ i = 0; found = SANE_FALSE; while (config!=NULL && i < config->count && !found) { if (strcmp (config->descriptors[i]->name, token) == 0) { found = SANE_TRUE; switch (config->descriptors[i]->type) { case SANE_TYPE_INT: size=config->descriptors[i]->size; value = malloc (size); wa = (SANE_Word *) value; count = config->descriptors[i]->size / sizeof (SANE_Word); for (j = 0; j < count; j++) { lp = sanei_config_get_string (lp, &string); if (string == NULL) { DBG (2, "sanei_configure_attach: couldn't find a string to parse"); return SANE_STATUS_INVAL; } wa[j] = strtol (string, NULL, 0); free (string); } break; case SANE_TYPE_BOOL: size=config->descriptors[i]->size; value = malloc (size); ba = (SANE_Bool *) value; count = config->descriptors[i]->size / sizeof (SANE_Bool); for (j = 0; j < count; j++) { lp = sanei_config_get_string (lp, &string); if (string == NULL) { DBG (2, "sanei_configure_attach: couldn't find a string to parse"); return SANE_STATUS_INVAL; } if ((strcmp (string, "1") == 0) || (strcmp (string, "true") == 0)) { ba[j] = SANE_TRUE; } else { if ((strcmp (string, "0") == 0) || (strcmp (string, "false") == 0)) ba[j] = SANE_FALSE; else { DBG (2, "sanei_configure_attach: couldn't find a valid boolean value"); return SANE_STATUS_INVAL; } } free (string); } break; case SANE_TYPE_FIXED: size=config->descriptors[i]->size; value = malloc (size); wa = (SANE_Word *) value; count = config->descriptors[i]->size / sizeof (SANE_Word); for (j = 0; j < count; j++) { lp = sanei_config_get_string (lp, &string); if (string == NULL) { DBG (2, "sanei_configure_attach: couldn't find a string to parse"); return SANE_STATUS_INVAL; } wa[j] = SANE_FIX(strtod (string, NULL)); free (string); } break; case SANE_TYPE_STRING: sanei_config_get_string (lp, &string); if (string == NULL) { DBG (2, "sanei_configure_attach: couldn't find a string value to parse"); return SANE_STATUS_INVAL; } value = string; size=strlen(string)+1; if(size>config->descriptors[i]->size) { size=config->descriptors[i]->size-1; string[size]=0; } break; default: DBG (1, "sanei_configure_attach: incorrect type %d for option %s, skipping option ...\n", config->descriptors[i]->type, config->descriptors[i]->name); } /* check decoded value */ status = sanei_check_value (config->descriptors[i], value); /* if value OK, copy it in configuration struct */ if (status == SANE_STATUS_GOOD) { memcpy (config->values[i], value, size); } if (value != NULL) { free (value); value = NULL; } } if (status != SANE_STATUS_GOOD) { DBG (1, "sanei_configure_attach: failed to parse option '%s', line '%s'\n", token, line); } i++; } free (token); /* not detected as an option, so we call the attach function * with it */ if (!found && status == SANE_STATUS_GOOD) { /* if not an option, try to attach */ /* to avoid every backend to depend on scsi and usb functions * we call back the backend for attach. In turn it will call * sanei_usb_attach_matching_devices, sanei_config_attach_matching_devices * or other. This means 2 callback functions per backend using this * function. */ DBG (3, "sanei_configure_attach: trying to attach with '%s'\n", lp2); if(attach!=NULL) attach (config, lp2); } } fclose (fp); DBG (3, "sanei_configure_attach: exit\n"); return status; }