void cgiGetAttributes(ipp_t *request, /* I - IPP request */ const char *tmpl) /* I - Base filename */ { int num_attrs; /* Number of attributes */ char *attrs[1000]; /* Attributes */ int i; /* Looping var */ char filename[1024], /* Filename */ locale[16]; /* Locale name */ const char *directory, /* Directory */ *lang; /* Language */ FILE *in; /* Input file */ int ch; /* Character from file */ char name[255], /* Name of variable */ *nameptr; /* Pointer into name */ /* * Convert the language to a locale name... */ if ((lang = getenv("LANG")) != NULL) { for (i = 0; lang[i] && i < 15; i ++) if (isalnum(lang[i] & 255)) locale[i] = tolower(lang[i]); else locale[i] = '_'; locale[i] = '\0'; } else locale[0] = '\0'; /* * See if we have a template file for this language... */ directory = cgiGetTemplateDir(); snprintf(filename, sizeof(filename), "%s/%s/%s", directory, locale, tmpl); if (access(filename, 0)) { locale[2] = '\0'; snprintf(filename, sizeof(filename), "%s/%s/%s", directory, locale, tmpl); if (access(filename, 0)) snprintf(filename, sizeof(filename), "%s/%s", directory, tmpl); } /* * Open the template file... */ if ((in = fopen(filename, "r")) == NULL) return; /* * Loop through the file adding attribute names as needed... */ num_attrs = 0; attrs[0] = NULL; /* Eliminate compiler warning */ while ((ch = getc(in)) != EOF) if (ch == '\\') getc(in); else if (ch == '{' && num_attrs < (sizeof(attrs) / sizeof(attrs[0]))) { /* * Grab the name... */ for (nameptr = name; (ch = getc(in)) != EOF;) if (strchr("}]<>=! \t\n", ch)) break; else if (nameptr > name && ch == '?') break; else if (nameptr < (name + sizeof(name) - 1)) { if (ch == '_') *nameptr++ = '-'; else *nameptr++ = ch; } *nameptr = '\0'; if (!strncmp(name, "printer_state_history", 21)) strcpy(name, "printer_state_history"); /* * Possibly add it to the list of attributes... */ for (i = 0; i < num_attrs; i ++) if (!strcmp(attrs[i], name)) break; if (i >= num_attrs) { attrs[num_attrs] = strdup(name); num_attrs ++; } } /* * If we have attributes, add a requested-attributes attribute to the * request... */ if (num_attrs > 0) { ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", num_attrs, NULL, (const char **)attrs); for (i = 0; i < num_attrs; i ++) free(attrs[i]); } fclose(in); }
void cgiCopyTemplateLang(const char *tmpl) /* I - Base filename */ { char filename[1024], /* Filename */ locale[16], /* Locale name */ *locptr; /* Pointer into locale name */ const char *directory, /* Directory for templates */ *lang; /* Language */ FILE *in; /* Input file */ fprintf(stderr, "DEBUG2: cgiCopyTemplateLang(tmpl=\"%s\")\n", tmpl ? tmpl : "(null)"); /* * Convert the language to a locale name... */ locale[0] = '\0'; if ((lang = getenv("LANG")) != NULL) { locale[0] = '/'; strlcpy(locale + 1, lang, sizeof(locale) - 1); if ((locptr = strchr(locale, '.')) != NULL) *locptr = '\0'; /* Strip charset */ } fprintf(stderr, "DEBUG2: lang=\"%s\", locale=\"%s\"...\n", lang ? lang : "(null)", locale); /* * See if we have a template file for this language... */ directory = cgiGetTemplateDir(); snprintf(filename, sizeof(filename), "%s%s/%s", directory, locale, tmpl); if ((in = fopen(filename, "r")) == NULL) { locale[3] = '\0'; snprintf(filename, sizeof(filename), "%s%s/%s", directory, locale, tmpl); if ((in = fopen(filename, "r")) == NULL) { snprintf(filename, sizeof(filename), "%s/%s", directory, tmpl); in = fopen(filename, "r"); } } fprintf(stderr, "DEBUG2: Template file is \"%s\"...\n", filename); /* * Open the template file... */ if (!in) { fprintf(stderr, "ERROR: Unable to open template file \"%s\" - %s\n", filename, strerror(errno)); return; } /* * Parse the file to the end... */ cgi_copy(stdout, in, 0, 0, 0); /* * Close the template file and return... */ fclose(in); }