void cupsLangFlush(void) { cups_lang_t *lang, /* Current language */ *next; /* Next language */ /* * Free all languages in the cache... */ _cupsMutexLock(&lang_mutex); for (lang = lang_cache; lang != NULL; lang = next) { /* * Free all messages... */ _cupsMessageFree(lang->strings); /* * Then free the language structure itself... */ next = lang->next; free(lang); } lang_cache = NULL; _cupsMutexUnlock(&lang_mutex); }
const char * /* O - Localized message */ _cupsLangString(cups_lang_t *lang, /* I - Language */ const char *message) /* I - Message */ { const char *s; /* Localized message */ /* * Range check input... */ if (!lang || !message || !*message) return (message); _cupsMutexLock(&lang_mutex); /* * Load the message catalog if needed... */ if (!lang->strings) cups_message_load(lang); s = _cupsMessageLookup(lang->strings, message); _cupsMutexUnlock(&lang_mutex); return (s); }
server_printer_t * /* O - Printer or NULL */ serverFindPrinter(const char *resource) /* I - Resource path */ { server_printer_t key, /* Search key */ *match = NULL; /* Matching printer */ _cupsMutexLock(&printer_mutex); if (cupsArrayCount(Printers) == 1 || !strcmp(resource, "/ipp/print")) { /* * Just use the first printer... */ match = cupsArrayFirst(Printers); if (strcmp(match->resource, resource) && strcmp(resource, "/ipp/print")) match = NULL; } else { key.resource = (char *)resource; match = (server_printer_t *)cupsArrayFind(Printers, &key); } _cupsMutexUnlock(&printer_mutex); return (match); }
char * /* O - Pointer to string */ _cupsStrRetain(const char *s) /* I - String to retain */ { _cups_sp_item_t *item; /* Pointer to string pool item */ if (s) { item = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str)); #ifdef DEBUG_GUARDS if (item->guard != _CUPS_STR_GUARD) { DEBUG_printf(("5_cupsStrRetain: Retaining string %p(%s), guard=%08x, " "ref_count=%d", item, s, item->guard, item->ref_count)); abort(); } #endif /* DEBUG_GUARDS */ _cupsMutexLock(&sp_mutex); item->ref_count ++; _cupsMutexUnlock(&sp_mutex); } return ((char *)s); }
void cupsLangFree(cups_lang_t *lang) /* I - Language to free */ { _cupsMutexLock(&lang_mutex); if (lang != NULL && lang->used > 0) lang->used --; _cupsMutexUnlock(&lang_mutex); }
int /* O - 1 on success, 0 on failure */ cupsSetServerCredentials( const char *path, /* I - Path to keychain/directory */ const char *common_name, /* I - Default common name for server */ int auto_create) /* I - 1 = automatically create self-signed certificates */ { char temp[1024]; /* Default path buffer */ DEBUG_printf(("cupsSetServerCredentials(path=\"%s\", common_name=\"%s\", auto_create=%d)", path, common_name, auto_create)); /* * Use defaults as needed... */ if (!path) path = http_gnutls_default_path(temp, sizeof(temp)); /* * Range check input... */ if (!path || !common_name) { _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); return (0); } _cupsMutexLock(&tls_mutex); /* * Free old values... */ if (tls_keypath) _cupsStrFree(tls_keypath); if (tls_common_name) _cupsStrFree(tls_common_name); /* * Save the new values... */ tls_keypath = _cupsStrAlloc(path); tls_auto_create = auto_create; tls_common_name = _cupsStrAlloc(common_name); _cupsMutexUnlock(&tls_mutex); return (1); }
size_t /* O - Number of strings */ _cupsStrStatistics(size_t *alloc_bytes, /* O - Allocated bytes */ size_t *total_bytes) /* O - Total string bytes */ { size_t count, /* Number of strings */ abytes, /* Allocated string bytes */ tbytes, /* Total string bytes */ len; /* Length of string */ _cups_sp_item_t *item; /* Current item */ /* * Loop through strings in pool, counting everything up... */ _cupsMutexLock(&sp_mutex); for (count = 0, abytes = 0, tbytes = 0, item = (_cups_sp_item_t *)cupsArrayFirst(stringpool); item; item = (_cups_sp_item_t *)cupsArrayNext(stringpool)) { /* * Count allocated memory, using a 64-bit aligned buffer as a basis. */ count += item->ref_count; len = (strlen(item->str) + 8) & (size_t)~7; abytes += sizeof(_cups_sp_item_t) + len; tbytes += item->ref_count * len; } _cupsMutexUnlock(&sp_mutex); /* * Return values... */ if (alloc_bytes) *alloc_bytes = abytes; if (total_bytes) *total_bytes = tbytes; return (count); }
void _cupsStrFlush(void) { _cups_sp_item_t *item; /* Current item */ DEBUG_printf(("4_cupsStrFlush: %d strings in array", cupsArrayCount(stringpool))); _cupsMutexLock(&sp_mutex); for (item = (_cups_sp_item_t *)cupsArrayFirst(stringpool); item; item = (_cups_sp_item_t *)cupsArrayNext(stringpool)) free(item); cupsArrayDelete(stringpool); stringpool = NULL; _cupsMutexUnlock(&sp_mutex); }
cups_lang_t * /* O - Language data */ cupsLangGet(const char *language) /* I - Language or locale */ { int i; /* Looping var */ #ifndef __APPLE__ char locale[255]; /* Copy of locale name */ #endif /* !__APPLE__ */ char langname[16], /* Requested language name */ country[16], /* Country code */ charset[16], /* Character set */ *csptr, /* Pointer to CODESET string */ *ptr, /* Pointer into language/charset */ real[48]; /* Real language name */ cups_encoding_t encoding; /* Encoding to use */ cups_lang_t *lang; /* Current language... */ static const char * const locale_encodings[] = { /* Locale charset names */ "ASCII", "ISO88591", "ISO88592", "ISO88593", "ISO88594", "ISO88595", "ISO88596", "ISO88597", "ISO88598", "ISO88599", "ISO885910", "UTF8", "ISO885913", "ISO885914", "ISO885915", "CP874", "CP1250", "CP1251", "CP1252", "CP1253", "CP1254", "CP1255", "CP1256", "CP1257", "CP1258", "KOI8R", "KOI8U", "ISO885911", "ISO885916", "MACROMAN", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "CP932", "CP936", "CP949", "CP950", "CP1361", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "EUCCN", "EUCJP", "EUCKR", "EUCTW", "SHIFT_JISX0213" }; DEBUG_printf(("2cupsLangGet(language=\"%s\")", language)); #ifdef __APPLE__ /* * Set the character set to UTF-8... */ strlcpy(charset, "UTF8", sizeof(charset)); /* * Apple's setlocale doesn't give us the user's localization * preference so we have to look it up this way... */ if (!language) { if (!getenv("SOFTWARE") || (language = getenv("LANG")) == NULL) language = appleLangDefault(); DEBUG_printf(("4cupsLangGet: language=\"%s\"", language)); } #else /* * Set the charset to "unknown"... */ charset[0] = '\0'; /* * Use setlocale() to determine the currently set locale, and then * fallback to environment variables to avoid setting the locale, * since setlocale() is not thread-safe! */ if (!language) { /* * First see if the locale has been set; if it is still "C" or * "POSIX", use the environment to get the default... */ # ifdef LC_MESSAGES ptr = setlocale(LC_MESSAGES, NULL); # else ptr = setlocale(LC_ALL, NULL); # endif /* LC_MESSAGES */ DEBUG_printf(("4cupsLangGet: current locale is \"%s\"", ptr)); if (!ptr || !strcmp(ptr, "C") || !strcmp(ptr, "POSIX")) { /* * Get the character set from the LC_CTYPE locale setting... */ if ((ptr = getenv("LC_CTYPE")) == NULL) if ((ptr = getenv("LC_ALL")) == NULL) if ((ptr = getenv("LANG")) == NULL) ptr = "en_US"; if ((csptr = strchr(ptr, '.')) != NULL) { /* * Extract the character set from the environment... */ for (ptr = charset, csptr ++; *csptr; csptr ++) if (ptr < (charset + sizeof(charset) - 1) && _cups_isalnum(*csptr)) *ptr++ = *csptr; *ptr = '\0'; } /* * Get the locale for messages from the LC_MESSAGES locale setting... */ if ((ptr = getenv("LC_MESSAGES")) == NULL) if ((ptr = getenv("LC_ALL")) == NULL) if ((ptr = getenv("LANG")) == NULL) ptr = "en_US"; } if (ptr) { strlcpy(locale, ptr, sizeof(locale)); language = locale; /* * CUPS STR #2575: Map "nb" to "no" for back-compatibility... */ if (!strncmp(locale, "nb", 2)) locale[1] = 'o'; DEBUG_printf(("4cupsLangGet: new language value is \"%s\"", language)); } } #endif /* __APPLE__ */ /* * If "language" is NULL at this point, then chances are we are using * a language that is not installed for the base OS. */ if (!language) { /* * Switch to the POSIX ("C") locale... */ language = "C"; } #ifdef CODESET /* * On systems that support the nl_langinfo(CODESET) call, use * this value as the character set... */ if (!charset[0] && (csptr = nl_langinfo(CODESET)) != NULL) { /* * Copy all of the letters and numbers in the CODESET string... */ for (ptr = charset; *csptr; csptr ++) if (_cups_isalnum(*csptr) && ptr < (charset + sizeof(charset) - 1)) *ptr++ = *csptr; *ptr = '\0'; DEBUG_printf(("4cupsLangGet: charset set to \"%s\" via " "nl_langinfo(CODESET)...", charset)); } #endif /* CODESET */ /* * If we don't have a character set by now, default to UTF-8... */ if (!charset[0]) strlcpy(charset, "UTF8", sizeof(charset)); /* * Parse the language string passed in to a locale string. "C" is the * standard POSIX locale and is copied unchanged. Otherwise the * language string is converted from ll-cc[.charset] (language-country) * to ll_CC[.CHARSET] to match the file naming convention used by all * POSIX-compliant operating systems. Invalid language names are mapped * to the POSIX locale. */ country[0] = '\0'; if (language == NULL || !language[0] || !strcmp(language, "POSIX")) strlcpy(langname, "C", sizeof(langname)); else { /* * Copy the parts of the locale string over safely... */ for (ptr = langname; *language; language ++) if (*language == '_' || *language == '-' || *language == '.') break; else if (ptr < (langname + sizeof(langname) - 1)) *ptr++ = (char)tolower(*language & 255); *ptr = '\0'; if (*language == '_' || *language == '-') { /* * Copy the country code... */ for (language ++, ptr = country; *language; language ++) if (*language == '.') break; else if (ptr < (country + sizeof(country) - 1)) *ptr++ = (char)toupper(*language & 255); *ptr = '\0'; } if (*language == '.' && !charset[0]) { /* * Copy the encoding... */ for (language ++, ptr = charset; *language; language ++) if (_cups_isalnum(*language) && ptr < (charset + sizeof(charset) - 1)) *ptr++ = (char)toupper(*language & 255); *ptr = '\0'; } /* * Force a POSIX locale for an invalid language name... */ if (strlen(langname) != 2) { strlcpy(langname, "C", sizeof(langname)); country[0] = '\0'; charset[0] = '\0'; } } DEBUG_printf(("4cupsLangGet: langname=\"%s\", country=\"%s\", charset=\"%s\"", langname, country, charset)); /* * Figure out the desired encoding... */ encoding = CUPS_AUTO_ENCODING; if (charset[0]) { for (i = 0; i < (int)(sizeof(locale_encodings) / sizeof(locale_encodings[0])); i ++) if (!_cups_strcasecmp(charset, locale_encodings[i])) { encoding = (cups_encoding_t)i; break; } if (encoding == CUPS_AUTO_ENCODING) { /* * Map alternate names for various character sets... */ if (!_cups_strcasecmp(charset, "iso-2022-jp") || !_cups_strcasecmp(charset, "sjis")) encoding = CUPS_WINDOWS_932; else if (!_cups_strcasecmp(charset, "iso-2022-cn")) encoding = CUPS_WINDOWS_936; else if (!_cups_strcasecmp(charset, "iso-2022-kr")) encoding = CUPS_WINDOWS_949; else if (!_cups_strcasecmp(charset, "big5")) encoding = CUPS_WINDOWS_950; } } DEBUG_printf(("4cupsLangGet: encoding=%d(%s)", encoding, encoding == CUPS_AUTO_ENCODING ? "auto" : lang_encodings[encoding])); /* * See if we already have this language/country loaded... */ if (country[0]) snprintf(real, sizeof(real), "%s_%s", langname, country); else strlcpy(real, langname, sizeof(real)); _cupsMutexLock(&lang_mutex); if ((lang = cups_cache_lookup(real, encoding)) != NULL) { _cupsMutexUnlock(&lang_mutex); DEBUG_printf(("3cupsLangGet: Using cached copy of \"%s\"...", real)); return (lang); } /* * See if there is a free language available; if so, use that * record... */ for (lang = lang_cache; lang != NULL; lang = lang->next) if (lang->used == 0) break; if (lang == NULL) { /* * Allocate memory for the language and add it to the cache. */ if ((lang = calloc(sizeof(cups_lang_t), 1)) == NULL) { _cupsMutexUnlock(&lang_mutex); return (NULL); } lang->next = lang_cache; lang_cache = lang; } else { /* * Free all old strings as needed... */ _cupsMessageFree(lang->strings); lang->strings = NULL; } /* * Then assign the language and encoding fields... */ lang->used ++; strlcpy(lang->language, real, sizeof(lang->language)); if (encoding != CUPS_AUTO_ENCODING) lang->encoding = encoding; else lang->encoding = CUPS_UTF8; /* * Return... */ _cupsMutexUnlock(&lang_mutex); return (lang); }
char * /* O - String pointer */ _cupsStrAlloc(const char *s) /* I - String */ { size_t slen; /* Length of string */ _cups_sp_item_t *item, /* String pool item */ *key; /* Search key */ /* * Range check input... */ if (!s) return (NULL); /* * Get the string pool... */ _cupsMutexLock(&sp_mutex); if (!stringpool) stringpool = cupsArrayNew((cups_array_func_t)compare_sp_items, NULL); if (!stringpool) { _cupsMutexUnlock(&sp_mutex); return (NULL); } /* * See if the string is already in the pool... */ key = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str)); if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL) { /* * Found it, return the cached string... */ item->ref_count ++; #ifdef DEBUG_GUARDS DEBUG_printf(("5_cupsStrAlloc: Using string %p(%s) for \"%s\", guard=%08x, " "ref_count=%d", item, item->str, s, item->guard, item->ref_count)); if (item->guard != _CUPS_STR_GUARD) abort(); #endif /* DEBUG_GUARDS */ _cupsMutexUnlock(&sp_mutex); return (item->str); } /* * Not found, so allocate a new one... */ slen = strlen(s); item = (_cups_sp_item_t *)calloc(1, sizeof(_cups_sp_item_t) + slen); if (!item) { _cupsMutexUnlock(&sp_mutex); return (NULL); } item->ref_count = 1; memcpy(item->str, s, slen + 1); #ifdef DEBUG_GUARDS item->guard = _CUPS_STR_GUARD; DEBUG_printf(("5_cupsStrAlloc: Created string %p(%s) for \"%s\", guard=%08x, " "ref_count=%d", item, item->str, s, item->guard, item->ref_count)); #endif /* DEBUG_GUARDS */ /* * Add the string to the pool and return it... */ cupsArrayAdd(stringpool, item); _cupsMutexUnlock(&sp_mutex); return (item->str); }
void _cupsStrFree(const char *s) /* I - String to free */ { _cups_sp_item_t *item, /* String pool item */ *key; /* Search key */ /* * Range check input... */ if (!s) return; /* * Check the string pool... * * We don't need to lock the mutex yet, as we only want to know if * the stringpool is initialized. The rest of the code will still * work if it is initialized before we lock... */ if (!stringpool) return; /* * See if the string is already in the pool... */ _cupsMutexLock(&sp_mutex); key = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str)); #ifdef DEBUG_GUARDS if (key->guard != _CUPS_STR_GUARD) { DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, " "ref_count=%d", key, key->str, key->guard, key->ref_count)); abort(); } #endif /* DEBUG_GUARDS */ if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL && item == key) { /* * Found it, dereference... */ item->ref_count --; if (!item->ref_count) { /* * Remove and free... */ cupsArrayRemove(stringpool, item); free(item); } } _cupsMutexUnlock(&sp_mutex); }
void _cupsRWUnlock(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ { _cupsMutexUnlock((_cups_mutex_t *)rwlock); }
void DLLExport _cups_debug_printf(const char *format, /* I - Printf-style format string */ ...) /* I - Additional arguments as needed */ { va_list ap; /* Pointer to arguments */ struct timeval curtime; /* Current time */ char buffer[2048]; /* Output buffer */ ssize_t bytes; /* Number of bytes in buffer */ int level; /* Log level in message */ /* * See if we need to do any logging... */ if (!debug_init) _cups_debug_set(getenv("CUPS_DEBUG_LOG"), getenv("CUPS_DEBUG_LEVEL"), getenv("CUPS_DEBUG_FILTER"), 0); if (_cups_debug_fd < 0) return; /* * Filter as needed... */ if (isdigit(format[0])) level = *format++ - '0'; else level = 0; if (level > _cups_debug_level) return; if (debug_filter) { int result; /* Filter result */ _cupsMutexLock(&debug_init_mutex); result = regexec(debug_filter, format, 0, NULL, 0); _cupsMutexUnlock(&debug_init_mutex); if (result) return; } /* * Format the message... */ gettimeofday(&curtime, NULL); snprintf(buffer, sizeof(buffer), "T%03d %02d:%02d:%02d.%03d ", debug_thread_id(), (int)((curtime.tv_sec / 3600) % 24), (int)((curtime.tv_sec / 60) % 60), (int)(curtime.tv_sec % 60), (int)(curtime.tv_usec / 1000)); va_start(ap, format); bytes = _cups_safe_vsnprintf(buffer + 19, sizeof(buffer) - 20, format, ap) + 19; va_end(ap); if ((size_t)bytes >= (sizeof(buffer) - 1)) { buffer[sizeof(buffer) - 2] = '\n'; bytes = sizeof(buffer) - 1; } else if (buffer[bytes - 1] != '\n') { buffer[bytes++] = '\n'; buffer[bytes] = '\0'; } /* * Write it out... */ _cupsMutexLock(&debug_log_mutex); write(_cups_debug_fd, buffer, (size_t)bytes); _cupsMutexUnlock(&debug_log_mutex); }
void DLLExport _cups_debug_set(const char *logfile, /* I - Log file or NULL */ const char *level, /* I - Log level or NULL */ const char *filter, /* I - Filter string or NULL */ int force) /* I - Force initialization */ { _cupsMutexLock(&debug_init_mutex); if (!debug_init || force) { /* * Restore debug settings to defaults... */ if (_cups_debug_fd != -1) { close(_cups_debug_fd); _cups_debug_fd = -1; } if (debug_filter) { regfree((regex_t *)debug_filter); debug_filter = NULL; } _cups_debug_level = 1; /* * Open logs, set log levels, etc. */ if (!logfile) _cups_debug_fd = -1; else if (!strcmp(logfile, "-")) _cups_debug_fd = 2; else { char buffer[1024]; /* Filename buffer */ snprintf(buffer, sizeof(buffer), logfile, getpid()); if (buffer[0] == '+') _cups_debug_fd = open(buffer + 1, O_WRONLY | O_APPEND | O_CREAT, 0644); else _cups_debug_fd = open(buffer, O_WRONLY | O_TRUNC | O_CREAT, 0644); } if (level) _cups_debug_level = atoi(level); if (filter) { if ((debug_filter = (regex_t *)calloc(1, sizeof(regex_t))) == NULL) fputs("Unable to allocate memory for CUPS_DEBUG_FILTER - results not " "filtered!\n", stderr); else if (regcomp(debug_filter, filter, REG_EXTENDED)) { fputs("Bad regular expression in CUPS_DEBUG_FILTER - results not " "filtered!\n", stderr); free(debug_filter); debug_filter = NULL; } } debug_init = 1; } _cupsMutexUnlock(&debug_init_mutex); }
void DLLExport _cups_debug_puts(const char *s) /* I - String to output */ { struct timeval curtime; /* Current time */ char buffer[2048]; /* Output buffer */ ssize_t bytes; /* Number of bytes in buffer */ int level; /* Log level in message */ /* * See if we need to do any logging... */ if (!debug_init) _cups_debug_set(getenv("CUPS_DEBUG_LOG"), getenv("CUPS_DEBUG_LEVEL"), getenv("CUPS_DEBUG_FILTER"), 0); if (_cups_debug_fd < 0) return; /* * Filter as needed... */ if (isdigit(s[0])) level = *s++ - '0'; else level = 0; if (level > _cups_debug_level) return; if (debug_filter) { int result; /* Filter result */ _cupsMutexLock(&debug_init_mutex); result = regexec(debug_filter, s, 0, NULL, 0); _cupsMutexUnlock(&debug_init_mutex); if (result) return; } /* * Format the message... */ gettimeofday(&curtime, NULL); bytes = snprintf(buffer, sizeof(buffer), "T%03d %02d:%02d:%02d.%03d %s", debug_thread_id(), (int)((curtime.tv_sec / 3600) % 24), (int)((curtime.tv_sec / 60) % 60), (int)(curtime.tv_sec % 60), (int)(curtime.tv_usec / 1000), s); if ((size_t)bytes >= (sizeof(buffer) - 1)) { buffer[sizeof(buffer) - 2] = '\n'; bytes = sizeof(buffer) - 1; } else if (buffer[bytes - 1] != '\n') { buffer[bytes++] = '\n'; buffer[bytes] = '\0'; } /* * Write it out... */ _cupsMutexLock(&debug_log_mutex); write(_cups_debug_fd, buffer, (size_t)bytes); _cupsMutexUnlock(&debug_log_mutex); }