+#define LP_JOBPENDING (1<<5)
+
+void lp_origin (int origin)
+{
+ lp.lp_origin = origin;
+}
+
+/* the converted string should always be shorter, but ... FIXME! */
+static void convert_octal (char *string, charset_t dest)
+{
+ unsigned char *p, *q;
+ char temp[4];
+ long int ch;
+
+ q=p=string;
+ while ( *p != '\0' ) {
+ ch = 0;
+ if ( *p == '\\' ) {
+ p++;
+ if (dest && isdigit(*p) && isdigit(*(p+1)) && isdigit(*(p+2)) ) {
+ temp[0] = *p;
+ temp[1] = *(p+1);
+ temp[2] = *(p+2);
+ temp[3] = 0;
+ ch = strtol( temp, NULL, 8);
+ if ( ch && ch < 0xff)
+ *q = ch;
+ else
+ *q = '.';
+ p += 2;
+ }
+ else
+ *q = '.';
+ }
+ else {
+ *q = *p;
+ }
+ p++;
+ q++;
+ }
+ *q = 0;
+}
+
+
+static void translate(charset_t from, charset_t dest, char **option)
+{
+ char *translated;
+
+ if (*option != NULL) {
+ convert_octal(*option, from);
+ if (from) {
+ if ((size_t) -1 != (convert_string_allocate(from, dest, *option, strlen(*option), &translated)) ) {
+ free (*option);
+ *option = translated;
+ }
+ }
+ }
+}
+
+
+static void lp_setup_comments (charset_t dest)
+{
+ charset_t from=0;
+
+ switch (lp.lp_origin) {
+ case 1:
+ from=CH_MAC;
+ break;
+ case 2:
+ from=CH_UTF8_MAC;
+ break;
+ }
+
+ if (lp.lp_job) {
+#ifdef DEBUG1
+ LOG(log_debug, logtype_papd, "job: %s", lp.lp_job );
+#endif
+ translate(from, dest, &lp.lp_job);
+ }
+ if (lp.lp_created_for) {
+#ifdef DEBUG1
+ LOG(log_debug, logtype_papd, "for: %s", lp.lp_created_for );
+#endif
+ translate(from, dest, &lp.lp_created_for);
+ }
+ if (lp.lp_person) {
+#ifdef DEBUG1
+ LOG(log_debug, logtype_papd, "person: %s", lp.lp_person );
+#endif
+ translate(from, dest, &lp.lp_person);
+ }
+}
+
+#define is_var(a, b) (strncmp((a), (b), 2) == 0)
+
+static size_t quote(char *dest, char *src, const size_t bsize, size_t len)
+{
+size_t used = 0;
+
+ while (len && used < bsize ) {
+ switch (*src) {
+ case '$':
+ case '\\':
+ case '"':
+ case '`':
+ if (used + 2 > bsize )
+ return used;
+ *dest = '\\';
+ dest++;
+ used++;
+ break;
+ }
+ *dest = *src;
+ src++;
+ dest++;
+ len--;
+ used++;
+ }
+ return used;
+}
+
+
+static char* pipexlate(char *src)
+{
+ char *p, *q, *dest;
+ static char destbuf[MAXPATHLEN +1];
+ size_t destlen = MAXPATHLEN;
+ int len = 0;
+
+ dest = destbuf;
+
+ if (!src)
+ return NULL;
+
+ memset(dest, 0, MAXPATHLEN +1);
+ if ((p = strchr(src, '%')) == NULL) { /* nothing to do */
+ strncpy(dest, src, MAXPATHLEN);
+ return destbuf;
+ }
+ /* first part of the path. copy and forward to the next variable. */
+ len = MIN((size_t)(p - src), destlen);
+ if (len > 0) {
+ strncpy(dest, src, len);
+ destlen -= len;
+ dest += len;
+ }
+
+ while (p && destlen > 0) {
+ /* now figure out what the variable is */
+ q = NULL;
+ if (is_var(p, "%U")) {
+ q = lp.lp_person;
+ } else if (is_var(p, "%C") || is_var(p, "%J") ) {
+ q = lp.lp_job;
+ } else if (is_var(p, "%F")) {
+ q = lp.lp_created_for;
+ } else if (is_var(p, "%%")) {
+ q = "%";
+ }
+
+ /* copy the stuff over. if we don't understand something that we
+ * should, just skip it over. */
+ if (q) {
+ len = MIN(strlen(q), destlen);
+ len = quote(dest, q, destlen, len);
+ }
+ else {
+ len = MIN(2, destlen);
+ strncpy(dest, q, len);
+ }
+ dest += len;
+ destlen -= len;
+
+ /* stuff up to next % */
+ src = p + 2;
+ p = strchr(src, '%');
+ len = p ? MIN((size_t)(p - src), destlen) : destlen;
+ if (len > 0) {
+ strncpy(dest, src, len);
+ dest += len;
+ destlen -= len;
+ }
+ }
+ return destbuf;
+}
+