? config-cmdline.diff
Index: libgnomeprint/gnome-print-config.c
===================================================================
RCS file: /cvs/gnome/libgnomeprint/libgnomeprint/gnome-print-config.c,v
retrieving revision 1.28
diff -u -r1.28 gnome-print-config.c
--- libgnomeprint/gnome-print-config.c	28 Mar 2003 17:55:22 -0000	1.28
+++ libgnomeprint/gnome-print-config.c	28 Mar 2003 22:39:13 -0000
@@ -36,6 +36,7 @@
 #include <libgnomeprint/gnome-print-config.h>
 #include <libgnomeprint/gnome-print-config-private.h>
 #include <libgnomeprint/gnome-print-job.h>
+#include <libgnomeprint/gnome-print-i18n.h>
 
 typedef struct _GnomePrintConfigClass GnomePrintConfigClass;
 
@@ -225,6 +226,18 @@
 	return result;
 }
 
+static gboolean
+gnome_print_config_parse_boolean (const guchar * v)
+{
+	if (!g_ascii_strcasecmp (v, "true") ||
+	    !g_ascii_strcasecmp (v, "y") ||
+	    !g_ascii_strcasecmp (v, "yes") ||
+	    (atoi (v) != 0)) {
+		return TRUE;
+	}
+	return FALSE;
+}
+
 gboolean
 gnome_print_config_get_boolean (GnomePrintConfig *config, const guchar *key, gboolean *val)
 {
@@ -238,14 +251,7 @@
 	v = gnome_print_config_get (config, key);
 
 	if (v != NULL) {
-		if (!g_ascii_strcasecmp (v, "true") ||
-		    !g_ascii_strcasecmp (v, "y") ||
-		    !g_ascii_strcasecmp (v, "yes") ||
-		    (atoi (v) != 0)) {
-			*val = TRUE;
-			return TRUE;
-		}
-		*val = FALSE;
+		*val = gnome_print_config_parse_boolean (v);
 		g_free (v);
 		return TRUE;
 	}
@@ -567,4 +573,177 @@
 	}
 
 	return TRUE;
+}
+
+static const gchar *
+gnome_print_cmdline_map_orientation (const gchar * orient)
+{
+	if (!orient)
+		return "R0";
+	else if (!g_ascii_strcasecmp (orient, "R0") || !g_ascii_strcasecmp (orient, "portrait"))
+		return "R0";
+	else if (!g_ascii_strcasecmp (orient, "R90") || !g_ascii_strcasecmp (orient, "landscape"))
+		return "R90";
+	else if (!g_ascii_strcasecmp (orient, "R180") || !g_ascii_strcasecmp (orient, "reverse-protrait"))
+		return "R180";
+	else if (!g_ascii_strcasecmp (orient, "R270") || !g_ascii_strcasecmp (orient, "reverse-landscape"))
+		return "R270";
+	else {
+		g_warning (_("Unrecognized orientation '%s'\n"), orient);
+		return "R0";
+	}
+}
+
+static gboolean
+gnome_print_cmdline_param_match (const gchar * str, const gchar * param_name)
+{
+	int i;
+	
+	for (i = 0; str[i] != '\0' && str[i] != ':'; i++)
+		if (param_name[i] != str[i])
+			return FALSE;
+	return str[i] == ':' && param_name[i] == '\0';
+}
+
+static int
+gnome_print_cmdline_arg_offset (const gchar * str)
+{
+	int i;
+	
+	for (i = 0; str[i] != '\0' && str[i] != ':'; i++)
+		;
+	if (str[i] != '\0') 
+		i++;
+	for (; str[i] == ' '; i++)
+		;
+	return i;
+}
+
+static gboolean
+gnome_print_cmdline_parse_arg (GnomePrintConfig * cfg, const gchar * str)
+{
+	int arg_off;
+	
+	arg_off = gnome_print_cmdline_arg_offset (str);
+	
+	if (gnome_print_cmdline_param_match (str, "paper")) {
+		if (!gnome_print_config_set (cfg, GNOME_PRINT_KEY_PAPER_SIZE, str + arg_off)) {
+			g_warning (_("Error setting paper to '%s'\n"), str + arg_off);
+			return FALSE;
+		}
+	} else if (gnome_print_cmdline_param_match (str, "orientation")) {
+		if (!gnome_print_config_set (cfg, GNOME_PRINT_KEY_ORIENTATION, gnome_print_cmdline_map_orientation (str + arg_off))) {
+			g_warning (_("Error setting orientation to '%s'\n"), str + arg_off);
+			return FALSE;
+		}
+	} else if (gnome_print_cmdline_param_match (str, "resolution")) {
+		if (gnome_print_config_set (cfg, GNOME_PRINT_KEY_RESOLUTION, str + arg_off)) {
+			g_warning (_("Error setting resolution to '%s'\n"), str + arg_off);
+			return FALSE;
+		}
+	} else if (gnome_print_cmdline_param_match (str, "ncopies")) {
+		if (!gnome_print_config_set_int (cfg, GNOME_PRINT_KEY_NUM_COPIES, atoi (str + arg_off))) {
+			g_warning (_("Error setting #copies to '%s'\n"), str + arg_off);
+			return FALSE;
+		}
+	} else if (gnome_print_cmdline_param_match (str, "collate")) {
+		if (!gnome_print_config_set_boolean (cfg, GNOME_PRINT_KEY_COLLATE, gnome_print_config_parse_boolean (str + arg_off))) {
+			g_warning (_("Error setting collate to '%s'\n"), str + arg_off);
+			return FALSE;
+		}
+	} else if (gnome_print_cmdline_param_match (str, "document-name")) {
+		if (!gnome_print_config_set (cfg, GNOME_PRINT_KEY_DOCUMENT_NAME, str + arg_off)) {
+			g_warning (_("Error setting document name to '%s'\n"), str + arg_off);
+			return FALSE;
+		}
+	} else if (gnome_print_cmdline_param_match (str, "device")) {
+		/* let lpr == the default printer for now */
+		if (g_ascii_strcasecmp (str + arg_off, "lpr") != 0) {
+			GPANode *config;
+
+			config = gnome_print_config_get_node (cfg);
+			if (!config) {
+				g_warning (_("Error setting printer\n"));
+				return FALSE;
+			}
+			
+			/* Postscript (GENERIC) is the default */
+			if (strstr (str + arg_off, "pdf") != NULL || strstr (str + arg_off, "PDF"))
+				if (!gpa_node_set_path_value (config, "Globals.Printers", "PDF")) {
+					g_warning (_("Error setting printer to PDF\n"));
+					return FALSE;
+				}
+			
+			if (!gnome_print_config_set (cfg, GNOME_PRINT_KEY_OUTPUT_FILENAME, str + arg_off)) {
+				g_warning (_("Error setting the output filename to '%s'\n"), str + arg_off);
+				return FALSE;
+			}
+		}
+	} else {
+		g_warning (_("Unrecognized command: '%s'\n"), str);
+		return FALSE;
+	}
+
+	return TRUE;
+}
+
+/**
+ * gnome_print_config_parse_cmdline:
+ * @description: The command-line printing argument(s)
+ * 
+ * Turns a CSS2-like argument string into a GnomePrintConfig.
+ * Accepted values (see also the #GnomePrintConfig documentation):
+ *
+ * paper: <paper string> (USLetter, A4, ...)
+ * orientation: <orientation string> (R0|portrait, R90|landscape, R180|reverse-portrait, R270|reverse-landscape)
+ * resolution: <resolution string> (eg: 300x300 or 300dpi)
+ * ncopies: <integer>
+ * collate: <boolean> (yes, y, 1, true)
+ * document-name: <string>
+ * device: <string>
+ *
+ * Example usage:
+ * device:lpr;collate:true;paper:Custom;paper-width:20in;paper-height:10in;ncopies:2;collate:false;document-name:Hello World;orientation:portrait
+ *
+ * Any unspecified argument assumes the default value.
+ *
+ * Returns: A new #GnomePrintConfig or %NULL
+ */
+GnomePrintConfig *
+gnome_print_config_parse_cmdline (const guchar * description)
+{
+	GnomePrintConfig * cfg;
+	int start, end;
+	gchar * arg;
+	gboolean result = TRUE;
+	
+	cfg = gnome_print_config_default ();
+	if (!description || !strlen(description) || !g_ascii_strcasecmp (description, "default"))
+		return cfg;
+	
+	start = 0;
+	while (description[start] != '\0') {
+		for (end = start; description[end] != '\0' && description[end] != ';'; end++)
+			;
+		arg = g_new (gchar, 1 + end - start);
+		memcpy (arg, description + start, end - start);
+		arg[end - start] = '\0';
+
+		/* keep on parsing so we get the maximum # of errors printed out at once */
+		result = result && gnome_print_cmdline_parse_arg (cfg, arg);
+		g_free (arg);
+		start = end;
+
+		if (description[start] == ';') 
+			start++;
+		while (description[start] == ' ') 
+			start++;
+	}
+	
+	if (!result) {
+		gnome_print_config_unref (cfg);
+		return NULL;
+	}
+
+	return cfg;
 }
Index: libgnomeprint/gnome-print-config.h
===================================================================
RCS file: /cvs/gnome/libgnomeprint/libgnomeprint/gnome-print-config.h,v
retrieving revision 1.22
diff -u -r1.22 gnome-print-config.h
--- libgnomeprint/gnome-print-config.h	12 Feb 2003 04:53:33 -0000	1.22
+++ libgnomeprint/gnome-print-config.h	28 Mar 2003 22:39:14 -0000
@@ -146,6 +146,10 @@
 gchar *           gnome_print_config_to_string (GnomePrintConfig *gpc, guint flags);
 GnomePrintConfig *gnome_print_config_from_string (const gchar *str, guint flags);
 
+#ifdef GNOME_PRINT_UNSTABLE_API
+GnomePrintConfig *gnome_print_config_parse_cmdline (const guchar * description);
+#endif
+
 /*
  * Get/Set key value pairs
  *
