[Debootloaders-devel] r132 - in trunk/rsrce: . debian

Jeremie Koenig jkoenig-guest at costa.debian.org
Sat Jul 15 16:40:17 UTC 2006


Author: jkoenig-guest
Date: 2006-07-15 16:40:12 +0000 (Sat, 15 Jul 2006)
New Revision: 132

Modified:
   trunk/rsrce/Makefile
   trunk/rsrce/TODO
   trunk/rsrce/command.c
   trunk/rsrce/command.h
   trunk/rsrce/debian/changelog
   trunk/rsrce/main.c
   trunk/rsrce/resource.c
   trunk/rsrce/resource.h
   trunk/rsrce/rsrce.1
Log:
Some command-line options implemented


Modified: trunk/rsrce/Makefile
===================================================================
--- trunk/rsrce/Makefile	2006-07-15 15:13:23 UTC (rev 131)
+++ trunk/rsrce/Makefile	2006-07-15 16:40:12 UTC (rev 132)
@@ -16,3 +16,5 @@
 clean:
 	$(RM) $(OBJS) rsrce
 
+resource.o: rsrc-fmt.h
+

Modified: trunk/rsrce/TODO
===================================================================
--- trunk/rsrce/TODO	2006-07-15 15:13:23 UTC (rev 131)
+++ trunk/rsrce/TODO	2006-07-15 16:40:12 UTC (rev 132)
@@ -1,2 +1,2 @@
 - add commands for changing the resource fork's reserved space and attributes
-- add a command-line option to allow for #!/usr/bin/rsrce scripts
+- fix 'rename' to allow both removing the name and setting an empty one

Modified: trunk/rsrce/command.c
===================================================================
--- trunk/rsrce/command.c	2006-07-15 15:13:23 UTC (rev 131)
+++ trunk/rsrce/command.c	2006-07-15 16:40:12 UTC (rev 132)
@@ -26,10 +26,10 @@
 #include "translate.h"
 #include "command.h"
 
-struct res_fork *cmd_rf;
-restype_t cmd_res_type;
-int cmd_res_id;
-struct resource *cmd_res;
+static struct res_fork *cmd_rf;
+static restype_t cmd_res_type;
+static int cmd_res_id;
+static struct resource *cmd_res;
 
 int cmd_parseresource(const char *spec)
 {
@@ -67,24 +67,22 @@
 }
 
 
+static char *cmd_ifname, *cmd_ofname;
+
 int cmd_read(char **argv)
 {
 	struct res_fork *rf;
-	FILE *f;
-	
-	if(!argv[1]) {
+
+	if(argv[1]) {
+		cmd_ifname = realloc(cmd_ifname, strlen(argv[1]) + 1);
+		strcpy(cmd_ifname, argv[1]);
+	}
+	if(!cmd_ifname) {
 		fprintf(stderr, "Read which file ?\n");
 		return 1;
 	}
 
-	f = fopen(argv[1], "r");
-	if(!f) {
-		perror(argv[1]);
-		return 1;
-	}
-
-	rf = res_read(f);
-	fclose(f);
+	rf = res_read(cmd_ifname);
 	if(!rf) {
 		fprintf(stderr, "Error while reading %s\n", argv[1]);
 		return 1;
@@ -97,22 +95,20 @@
 
 int cmd_write(char **argv)
 {
-	FILE *f;
+	const char *fname;
 	int ret;
 
-	if(!argv[1]) {
+	if(argv[1]) {
+		cmd_ofname = realloc(cmd_ofname, strlen(argv[1]) + 1);
+		strcpy(cmd_ofname, argv[1]);
+	}
+	fname = cmd_ofname ? cmd_ofname : cmd_ifname;
+	if(!fname) {
 		fprintf(stderr, "Write to which file ?\n");
 		return 1;
 	}
 
-	f = fopen(argv[1], "w");
-	if(!f) {
-		perror(argv[1]);
-		return 1;
-	}
-
-	ret = res_write(cmd_rf, f);
-	fclose(f);
+	ret = res_write(cmd_rf, fname);
 	if(ret < 0) {
 		fprintf(stderr, "Write error\n");
 		return 1;
@@ -410,9 +406,11 @@
 	return 1;
 }
 
-void cmd_init(FILE *f)
+void cmd_init(const char *ifname, const char *ofname)
 {
-	cmd_rf = f ? res_read(f) : res_newfork();
+	cmd_ifname = ifname ? strdup(ifname) : NULL;
+	cmd_ofname = ofname ? strdup(ofname) : NULL;
+	cmd_rf = cmd_ifname ? res_read(cmd_ifname) : res_newfork();
 	if(!cmd_rf) {
 		exit(1);
 	}

Modified: trunk/rsrce/command.h
===================================================================
--- trunk/rsrce/command.h	2006-07-15 15:13:23 UTC (rev 131)
+++ trunk/rsrce/command.h	2006-07-15 16:40:12 UTC (rev 132)
@@ -19,6 +19,6 @@
 #define __COMMAND_H__
 
 int cmd_exec(char **argv);
-void cmd_init(FILE *f);
+void cmd_init(const char *ifname, const char *ofname);
 
 #endif

Modified: trunk/rsrce/debian/changelog
===================================================================
--- trunk/rsrce/debian/changelog	2006-07-15 15:13:23 UTC (rev 131)
+++ trunk/rsrce/debian/changelog	2006-07-15 16:40:12 UTC (rev 132)
@@ -1,8 +1,9 @@
 rsrce (0.2.2) unstable; urgency=low
 
   * Fix rsrce manpage section.
+  * Add some command line options to allow for '#!/usr/bin/rsrce -ef' scripts.
 
- -- Aurélien GÉRÔME <ag at roxor.cx>  Sat, 15 Jul 2006 16:13:07 +0200
+ -- Jeremie Koenig <sprite at sprite.fr.eu.org>  Sat, 15 Jul 2006 18:26:47 +0200
 
 rsrce (0.2.1) unstable; urgency=low
 

Modified: trunk/rsrce/main.c
===================================================================
--- trunk/rsrce/main.c	2006-07-15 15:13:23 UTC (rev 131)
+++ trunk/rsrce/main.c	2006-07-15 16:40:12 UTC (rev 132)
@@ -28,6 +28,9 @@
 #define MAXCMDSZ 1023
 #define MAXARGC 7
 
+
+/* Command parser */
+
 static char backslashed(char c)
 {
 	static const char bsc[] = "abfnrtv", bse[] = "\a\b\f\n\r\t\v";
@@ -90,28 +93,66 @@
 }
 
 
+/* Command line options */
+
+static struct {
+	FILE *cmdin;
+	int e;
+	const char *ifname, *ofname;
+} opts;
+
+static void usage(const char *myname)
+{
+	fprintf(stderr, "Usage: %s [-e] [-f <script>] "
+			"[-o <output-file>] [<input-file>]\n", myname);
+}
+
+static void do_cmdline(int argc, char **argv)
+{
+	int opt;
+
+	opts.cmdin = stdin;
+	opts.ifname = opts.ofname = NULL;
+	opts.e = 0;
+
+	opterr = 1;
+	while(opt = getopt(argc, argv, "ef:o:"), opt != EOF)
+		switch(opt) {
+			case 'e':
+				opts.e = 1;
+				break;
+			case 'f':
+				opts.cmdin = fopen(optarg, "r");
+				if(!opts.cmdin) {
+					perror(optarg);
+					exit(1);
+				}
+				break;
+			case 'o':
+				opts.ofname = optarg;
+				break;
+			default:
+				usage(argv[0]);
+				exit(2);
+		}
+
+	opts.ifname = argv[optind];
+}
+
 int main(int argc, char **argv)
 {
-	char *filename;
-	FILE *f, *in = stdin;
-	int tty = isatty(0);
+	int tty;
 	int r;
 
-	filename = (argc > 1) ? argv[1] : NULL;
-	if(filename) {
-		f = strcmp(filename, "-") ? fopen(filename, "r") : stdin;
-		if(!f) {
-			perror(filename);
-			exit(1);
-		}
-	} else
-		f = NULL;
+	do_cmdline(argc, argv);
 
-	cmd_init(f);
-	while(!feof(in)) {
+	cmd_init(opts.ifname, opts.ofname);
+	tty = isatty(fileno(opts.cmdin));
+
+	while(!feof(opts.cmdin)) {
 		if(tty) fputs(CFG_PROMPT, stdout);
-		r = parse_command(in);
-		if(!tty && r) exit(1);
+		r = parse_command(opts.cmdin);
+		if(opts.e && r) exit(1);
 	}
 	if(tty) printf("\n");
 

Modified: trunk/rsrce/resource.c
===================================================================
--- trunk/rsrce/resource.c	2006-07-15 15:13:23 UTC (rev 131)
+++ trunk/rsrce/resource.c	2006-07-15 16:40:12 UTC (rev 132)
@@ -341,15 +341,23 @@
 	return res_parse_typelist(c);
 }
 
-struct res_fork *res_read(FILE *stream)
+struct res_fork *res_read(const char *fname)
 {
+	FILE *stream;
 	struct res_parsecontext c;
 	int ok;
 
+	stream = fopen(fname, "r");
+	if(!stream) {
+		perror(fname);
+		return NULL;
+	}
+
 	memset(&c, 0, sizeof c);
 	c.f = res_newfork();
 	ok = (res_readbuf(&c, stream) >= 0) && (res_parsebuf(&c) >= 0);
 	free(c.buf);
+	fclose(stream);
 
 	if(!ok) {
 		res_delfork(c.f);
@@ -372,7 +380,9 @@
 	return 0;
 }
 
-int res_write(struct res_fork *f, FILE *stream)
+/* TODO: split this into intelligible parts */
+/* FIXME: stream is not closed when an error occurs */
+int res_write(struct res_fork *f, const char *fname)
 {
 	int mlen, dlen, nlen, rlen;
 	int rnum, dofs, nofs, rofs;
@@ -382,9 +392,16 @@
 	struct restype type;
 	struct resref ref;
 
+	FILE *stream;
 	struct resource *r;
 	int ti, i;
 
+	stream = fopen(fname, "w");
+	if(!stream) {
+		perror(fname);
+		return -1;
+	}
+
 	/* compute the lenght of parts */
 	dlen = 0;
 	mlen = sizeof(struct resmaphdr);
@@ -470,6 +487,7 @@
 				return -1;
 		}
 
+	fclose(stream);
 	return 0;
 }
 

Modified: trunk/rsrce/resource.h
===================================================================
--- trunk/rsrce/resource.h	2006-07-15 15:13:23 UTC (rev 131)
+++ trunk/rsrce/resource.h	2006-07-15 16:40:12 UTC (rev 132)
@@ -34,8 +34,8 @@
 void res_delete(struct resource *r);
 
 struct res_fork *res_newfork();
-struct res_fork *res_read(FILE *stream);
-int res_write(struct res_fork *f, FILE *s);
+struct res_fork *res_read(const char *fname);
+int res_write(struct res_fork *f, const char *fname);
 struct resource *res_lookup(struct res_fork *f, restype_t type, int16_t id);
 void res_ls(FILE *s, struct res_fork *f);
 void res_delfork(struct res_fork *f);

Modified: trunk/rsrce/rsrce.1
===================================================================
--- trunk/rsrce/rsrce.1	2006-07-15 15:13:23 UTC (rev 131)
+++ trunk/rsrce/rsrce.1	2006-07-15 16:40:12 UTC (rev 132)
@@ -7,7 +7,10 @@
 .SH SYNOPSIS
 
 .B rsrce
-.RI [ filename ]
+.RB [ \-e ]
+.RB [ "\-f \fIscript" ]
+.RB [ "\-o \fIoutput\-file" ]
+.RI [ input\-file ]
 
 .SH DESCRIPTION
 
@@ -21,14 +24,44 @@
 from/to files, performing conversion for
 the few resource types it knows.
 
+.SH OPTIONS
+
+.TP
+.B \-e
+When this option is given, the failure of an editor command will cause rsrce to
+quit immediately with a non-zero exit status. This is similar to the
+.BR \-e " option to " /bin/sh .
+
+.TP
+.BI "\-f " script
+Instructs
+.B rsrce
+to read its commands from the given
+.IR script ,
+instead of using the standard input.
+
+.TP
+.BI "\-o " output\-file
+Specifies a default output file for the
+.BR write command.
+Useful when calling
+.B rsrce
+scripts which save their changes with a
+.B write
+command without a filename.
+
+.PP
+If an
+.I input\-file
+is specified,
+it is loaded before rsrce starts reading commands,
+and is used as the default output
+file if the
+.B \-o
+option has not been given.
+
 .SH USAGE
 
-If a
-.I filename
-is given on the command-line,
-the resources are read from the file
-when rsrce is launched.
-
 Rsrce reads commands
 from its standard input.
 Lines which contain only whitespace





More information about the Debootloaders-devel mailing list