[Debootloaders-devel] r58 - trunk/rsrce

Aurélien GÉRÔME ag-guest at costa.debian.org
Tue Jul 11 00:56:51 UTC 2006


Author: ag-guest
Date: 2006-07-11 00:56:50 +0000 (Tue, 11 Jul 2006)
New Revision: 58

Added:
   trunk/rsrce/Copyright
   trunk/rsrce/GPL-2
   trunk/rsrce/Makefile
   trunk/rsrce/README
   trunk/rsrce/command.c
   trunk/rsrce/command.h
   trunk/rsrce/config.h
   trunk/rsrce/main.c
   trunk/rsrce/resource.c
   trunk/rsrce/resource.h
   trunk/rsrce/translate.c
   trunk/rsrce/translate.h
Log:
Initial import of rsrce upstream source code.


Added: trunk/rsrce/Copyright
===================================================================
--- trunk/rsrce/Copyright	2006-07-11 00:53:48 UTC (rev 57)
+++ trunk/rsrce/Copyright	2006-07-11 00:56:50 UTC (rev 58)
@@ -0,0 +1,17 @@
+/* rsrce -- a Macintosh resource fork editor
+ * Copyright (C) 2004  Jeremie Koenig
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */

Added: trunk/rsrce/GPL-2
===================================================================
--- trunk/rsrce/GPL-2	2006-07-11 00:53:48 UTC (rev 57)
+++ trunk/rsrce/GPL-2	2006-07-11 00:56:50 UTC (rev 58)
@@ -0,0 +1,340 @@
+		    GNU GENERAL PUBLIC LICENSE
+		       Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+     59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+			    Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary.  To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+		    GNU GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License.  The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language.  (Hereinafter, translation is included without limitation in
+the term "modification".)  Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+  1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+  2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) You must cause the modified files to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    b) You must cause any work that you distribute or publish, that in
+    whole or in part contains or is derived from the Program or any
+    part thereof, to be licensed as a whole at no charge to all third
+    parties under the terms of this License.
+
+    c) If the modified program normally reads commands interactively
+    when run, you must cause it, when started running for such
+    interactive use in the most ordinary way, to print or display an
+    announcement including an appropriate copyright notice and a
+    notice that there is no warranty (or else, saying that you provide
+    a warranty) and that users may redistribute the program under
+    these conditions, and telling the user how to view a copy of this
+    License.  (Exception: if the Program itself is interactive but
+    does not normally print such an announcement, your work based on
+    the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+    a) Accompany it with the complete corresponding machine-readable
+    source code, which must be distributed under the terms of Sections
+    1 and 2 above on a medium customarily used for software interchange; or,
+
+    b) Accompany it with a written offer, valid for at least three
+    years, to give any third party, for a charge no more than your
+    cost of physically performing source distribution, a complete
+    machine-readable copy of the corresponding source code, to be
+    distributed under the terms of Sections 1 and 2 above on a medium
+    customarily used for software interchange; or,
+
+    c) Accompany it with the information you received as to the offer
+    to distribute corresponding source code.  (This alternative is
+    allowed only for noncommercial distribution and only if you
+    received the program in object code or executable form with such
+    an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it.  For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable.  However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License.  Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+  5. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Program or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+  6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+  7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+  8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded.  In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+  9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time.  Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation.  If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+  10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission.  For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this.  Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+			    NO WARRANTY
+
+  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+		     END OF TERMS AND CONDITIONS
+
+	    How to Apply These Terms to Your New Programs
+
+  If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+  To do so, attach the following notices to the program.  It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+    <one line to give the program's name and a brief idea of what it does.>
+    Copyright (C) <year>  <name of author>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+    Gnomovision version 69, Copyright (C) year  name of author
+    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+    This is free software, and you are welcome to redistribute it
+    under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License.  Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+  `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+  <signature of Ty Coon>, 1 April 1989
+  Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs.  If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library.  If this is what you want to do, use the GNU Library General
+Public License instead of this License.

Added: trunk/rsrce/Makefile
===================================================================
--- trunk/rsrce/Makefile	2006-07-11 00:53:48 UTC (rev 57)
+++ trunk/rsrce/Makefile	2006-07-11 00:56:50 UTC (rev 58)
@@ -0,0 +1,18 @@
+OBJS= resource.o translate.o command.o main.o
+CC= gcc
+CFLAGS= -Wall -g
+RM= rm -f
+
+DESTDIR=
+LOCAL=/local
+USR_BIN=$(DESTDIR)/usr$(LOCAL)/bin
+
+rsrce: $(OBJS)
+	$(CC) $(CFLAGS) -o $@ $(OBJS)
+
+install: rsrce
+	install -m755 rsrce $(USR_BIN)/rsrce
+
+clean:
+	$(RM) $(OBJS) rsrce
+

Added: trunk/rsrce/README
===================================================================
--- trunk/rsrce/README	2006-07-11 00:53:48 UTC (rev 57)
+++ trunk/rsrce/README	2006-07-11 00:56:50 UTC (rev 58)
@@ -0,0 +1,66 @@
+How to use rsrce
+
+
+1. Introduction
+
+This is rsrce, a Macintosh resource fork editor for Unix-like operating
+systems. It has been originally created to change the configuration of the
+miBoot Linux bootloader for OldWorld PowerMac computers.
+
+Since I do not use MacOS, I haven't tested any other usage of this program.
+You should therefore be very careful when using it, as it could very well
+produce unusable output. Consider this as unstable software.
+
+Another consequence of this is wildly missing functionnality. I'm not going to
+work on it, since I couldn't test anything in a real situation. Patches and/or
+bug reports would be very welcome.
+
+
+2. Overview
+
+Rsrce acts on raw resource forks stored in Unix files, such as .rsrc files
+produced by the hexbin command (from the macutil package.) You read such a
+thing into memory using the "read" command, then act on the resources it
+contains, then write it back.
+
+Resources are referred to by specifing their type and id, separated by a
+colon. I mean, something like "STR#:128". If the type field contains less than
+4 letters, spaces are added to complete it.
+
+Rsrce doesn't allow you to edit the resource data directly. It only knows to
+import/export them from/to files, performing conversion for the few resource
+types it knows. The "edit" command exports some resource's data to a temporary
+file, invokes and external editor, then import the file back.
+
+
+3. Commands
+
+read <file>
+    reads the resources from <file>
+write <file>
+    write the resources to <file>
+ls
+    lists resources (type, id, attributes, data lenght, name)
+hexdump <resource>
+    shows and hexdump of <resource>'s data
+import|export <resource> <file>
+    import/export <resource>'s data from/to <file>
+edit <resource>
+    invoke an external editor to change <resource>'s data
+help
+    lists available commands
+exit
+    exits (without saving, use write before exit if you wish to save your
+    changes).
+
+
+4. Conclusion
+
+If you actually use this program, please let me know how it's going. If you
+hack it, please send me your patches. Be careful not to hack it too much,
+however, you'd take the risk of becoming its maintainer.
+
+
+
+                       Jeremie Koenig <sprite at sprite.fr.eu.org>  2004-03-04
+

Added: trunk/rsrce/command.c
===================================================================
--- trunk/rsrce/command.c	2006-07-11 00:53:48 UTC (rev 57)
+++ trunk/rsrce/command.c	2006-07-11 00:56:50 UTC (rev 58)
@@ -0,0 +1,333 @@
+/* rsrce -- a Macintosh resource fork editor
+ * Copyright (C) 2004  Jeremie Koenig
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <ctype.h>
+
+#include "config.h"
+#include "resource.h"
+#include "translate.h"
+#include "command.h"
+
+struct res_fork *cmd_rf;
+struct resource *cmd_res;
+
+int cmd_selectresource(const char *spec)
+{
+	restype_t type;
+	int id;
+	char *colon;
+
+	if(!spec) {
+		fprintf(stderr, "Please specify a resource to act on.\n");
+		return 1;
+	}
+
+	colon = strrchr(spec, ':');
+	if(!colon || colon > spec+4) {
+		fprintf(stderr, "Incorrect resource specification\n");
+		return 1;
+	}
+	memset(type, ' ', sizeof(type));
+	memcpy(type, spec, colon - spec);
+	id = atoi(colon + 1);
+	cmd_res = res_lookup(cmd_rf, type, id);
+	if(!cmd_res) {
+		fprintf(stderr, "No such resource.\n");
+		return 1;
+	}
+
+	return 0;
+}
+
+
+int cmd_read(char **argv)
+{
+	struct res_fork *rf;
+	FILE *f;
+	
+	if(!argv[1]) {
+		fprintf(stderr, "Read which file ?\n");
+		return -1;
+	}
+
+	f = fopen(argv[1], "r");
+	if(!f) {
+		perror(argv[1]);
+		return -1;
+	}
+
+	rf = res_read(f);
+	if(!rf) {
+		fprintf(stderr, "Error while reading %s\n", argv[1]);
+		return -1;
+	}
+
+	res_delfork(cmd_rf);
+	cmd_rf = rf;
+	return 0;
+}
+
+int cmd_write(char **argv)
+{
+	FILE *f;
+	
+	if(!argv[1]) {
+		fprintf(stderr, "Write to which file ?\n");
+		return -1;
+	}
+
+	f = fopen(argv[1], "w");
+	if(!f) {
+		perror(argv[1]);
+		return -1;
+	}
+
+	if(res_write(cmd_rf, f)) {
+		fprintf(stderr, "Write error\n");
+		return -1;
+	}
+
+	return 0;
+}
+
+int cmd_ls(char **argv)
+{
+	res_ls(stdout, cmd_rf);
+	return 0;
+}
+
+int cmd_hexdump(char **argv)
+{
+	int i, j;
+	unsigned char *data;
+	int len;
+
+	if(cmd_selectresource(argv[1]))
+		return 1;
+
+	res_getdata(cmd_res, (void **) &data, &len);
+	
+	for(i=0 ; i < len ; i += 16) {
+		printf("%8x ", i);
+		for(j=0 ; j < 16 ; j++) {
+			if(i+j < len)
+				printf(" %02x", data[i+j]);
+			else
+				printf("   ");
+		}
+		printf("  |");
+		for(j=0 ; j < 16 && i+j < len ; j++)
+			printf("%c", isprint(data[i+j]) ? data[i+j] : '.');
+		printf("|\n");
+	}
+
+	return 0;
+}
+
+
+int cmd_export(char **argv)
+{
+	struct translator *tr;
+	FILE *f;
+	char *c;
+	int what, r;
+	
+	what = (strcmp(argv[0], "export") == 0);
+	
+	if(!argv[2]) {
+		fprintf(stderr, "%s <resource> <file> [<type>]\n", argv[0]);
+		return -1;
+	}
+	if(!argv[3]) {
+		c = strrchr(argv[2], '.');
+		argv[3] = c ? c+1 : NULL;
+	}
+
+	if(cmd_selectresource(argv[1]))
+		return -1;
+	
+	tr = tr_lookup(cmd_res, argv[3]);
+	if(!tr)
+		return -1;
+	
+	f = (strcmp(argv[2], "-") == 0) ? (what ? stdout : stdin)
+					: fopen(argv[2], what ? "w" : "r");
+	if(!f) {
+		perror(argv[2]);
+		return -1;
+	}
+
+	r = (what ? tr_export : tr_import)(tr, cmd_res, f);
+	if(strcmp(argv[2], "-") != 0)
+		fclose(f);
+
+	return r;
+}
+
+/* Export the resource data to a temporary file and return its name */
+static char *cmd_edit_export(struct translator *tr)
+{
+	char filename[] = "/tmp/rsrce.XXXXXX", *nfname;
+	const char *ext;
+	int fd;
+	FILE *f;
+	
+	fd  = mkstemp(filename);
+	ext = tr_ext(tr);
+	nfname = malloc(strlen(filename) + strlen(ext) + 2);
+	sprintf(nfname, "%s.%s", filename, ext);
+	if(rename(filename, nfname) != 0 || (f = fdopen(fd, "w")) == NULL) {
+		perror(nfname);
+		close(fd);
+		free(nfname);
+		return NULL;
+	}
+	if(tr_export(tr, cmd_res, f) != 0) {
+		fprintf(stderr,"Couldn't export resource data to %s\n",nfname);
+		fclose(f);
+		free(nfname);
+		return NULL;
+	}
+	fclose(f);
+
+	return nfname;
+}
+
+/* Invoke the external editor */
+static int cmd_edit_edit(const char *fname)
+{
+	char *cmd;
+	int ret;
+
+	cmd = malloc(strlen(CFG_EDITOR) + strlen(fname) + 2);
+	sprintf(cmd, "%s %s", CFG_EDITOR, fname);
+	ret = system(cmd);
+	free(cmd);
+
+	return ret;
+}
+
+/* Re-import the resource data */
+static int cmd_edit_import(struct translator *tr, const char *filename)
+{
+	FILE *f;
+	int ret;
+
+	f = fopen(filename, "r");
+	if(!f) {
+		perror(filename);
+		return -1;
+	}
+
+	ret = tr_import(tr, cmd_res, f);
+	fclose(f);
+	unlink(filename);
+
+	return ret;
+}
+
+int cmd_edit(char **argv)
+{
+	struct translator *tr;
+	int ret;
+	char *fname;
+	
+	if(cmd_selectresource(argv[1]) != 0)
+		return 1;
+
+	tr = tr_lookup(cmd_res, argv[2]);
+	fname = cmd_edit_export(tr);
+	if(!fname)
+		return 1;
+	ret = (cmd_edit_edit(fname) == 0 && cmd_edit_import(tr, fname) == 0);
+	free(fname);
+
+	return ret ? 0 : 1;
+}
+
+int cmd_help(char **argv);
+
+int cmd_exit(char **argv)
+{
+	exit(0);
+}
+
+struct command {
+	const char *cmd;
+	int (*f)(char **argv);
+	const char *help;
+} cmd_table[] = {
+	{"read", 	cmd_read,	"Read the resource fork from a file"},
+	{"write",	cmd_write,	"Write the resource fork to a file"},
+	{"ls",		cmd_ls,		"List resources"},
+	{"hexdump",	cmd_hexdump,	"Show an hexdump of the resource data"},
+	{"import",	cmd_export,	"Import resource data from a file"},
+	{"export",	cmd_export,	"Export resource data to a file"},
+	{"edit",	cmd_edit,	"Edit resource data"},
+	{"help",	cmd_help,	"This short help listing"},
+	{"exit",	cmd_exit,	"Exit (don't forget to write first!)"},
+	{NULL,		NULL,		NULL}
+};
+
+int cmd_help(char **argv)
+{
+	int i;
+
+	for(i=0 ; cmd_table[i].cmd ; i++) {
+		printf("%10s -- %s\n", cmd_table[i].cmd, cmd_table[i].help);
+	}
+
+	return 0;
+}
+
+
+#define MAXARGC 7
+
+int cmd_exec(char *cmd)
+{
+	char *argv[MAXARGC+1];
+	int argc, i;
+
+	memset(argv, 0, sizeof(argv));
+	argc = 0;
+	while((argv[argc++] = strsep(&cmd, " ")) && argc < MAXARGC+1);
+	
+	if(argv[MAXARGC]) {
+		fprintf(stderr, "Too many arguments\n");
+		return -1;
+	}
+
+	for(i=0 ; cmd_table[i].cmd ; i++) {
+		if(strcmp(cmd_table[i].cmd, argv[0]) == 0)
+			return cmd_table[i].f(argv);
+	}
+
+	fprintf(stderr, "%s: no such command\n", argv[0]);
+	return -1;
+}
+
+void cmd_init(FILE *f)
+{
+	cmd_rf = f ? res_read(f) : res_newfork();
+	if(!cmd_rf) {
+		exit(1);
+	}
+}

Added: trunk/rsrce/command.h
===================================================================
--- trunk/rsrce/command.h	2006-07-11 00:53:48 UTC (rev 57)
+++ trunk/rsrce/command.h	2006-07-11 00:56:50 UTC (rev 58)
@@ -0,0 +1,25 @@
+/* rsrce -- a Macintosh resource fork editor
+ * Copyright (C) 2004  Jeremie Koenig
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#ifndef __COMMAND_H__
+#define __COMMAND_H__
+
+int cmd_exec(char *cmd);
+void cmd_init(FILE *f);
+void cmd_cleanup(FILE *f);
+
+#endif

Added: trunk/rsrce/config.h
===================================================================
--- trunk/rsrce/config.h	2006-07-11 00:53:48 UTC (rev 57)
+++ trunk/rsrce/config.h	2006-07-11 00:56:50 UTC (rev 58)
@@ -0,0 +1,22 @@
+/* rsrce -- a Macintosh resource fork editor
+ * Copyright (C) 2004  Jeremie Koenig
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+/* config.h: configuration macros */
+
+#define CFG_EDITOR "/usr/bin/vi"
+#define CFG_BUFSZ  1024
+#define CFG_PROMPT "rsrce> "

Added: trunk/rsrce/main.c
===================================================================
--- trunk/rsrce/main.c	2006-07-11 00:53:48 UTC (rev 57)
+++ trunk/rsrce/main.c	2006-07-11 00:56:50 UTC (rev 58)
@@ -0,0 +1,57 @@
+/* rsrce -- a Macintosh resource fork editor
+ * Copyright (C) 2004  Jeremie Koenig
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "config.h"
+#include "command.h"
+
+char *filename;
+
+int main(int argc, char **argv)
+{
+	const char *prompt;
+	char buf[512], *c;
+	FILE *f;
+
+	filename = (argc > 1) ? argv[1] : NULL;
+	if(filename) {
+		f = strcmp(filename, "-") ? fopen(filename, "r") : stdin;
+		if(!f) {
+			perror(filename);
+			exit(1);
+		}
+	} else
+		f = NULL;
+	
+	cmd_init(f);
+	prompt = isatty(0) ? CFG_PROMPT : "";
+	
+	while(fputs(prompt, stdout), fgets(buf, 512, stdin)) {
+		c = strchr(buf, '\n');
+		if(*c) *c = '\0';
+		cmd_exec(buf);
+	}
+	if(isatty(0))
+		printf("\n");
+	return 0;
+}
+
+

Added: trunk/rsrce/resource.c
===================================================================
--- trunk/rsrce/resource.c	2006-07-11 00:53:48 UTC (rev 57)
+++ trunk/rsrce/resource.c	2006-07-11 00:56:50 UTC (rev 58)
@@ -0,0 +1,465 @@
+/* rsrce -- a Macintosh resource fork editor
+ * Copyright (C) 2004  Jeremie Koenig
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <ctype.h>
+#include <assert.h>
+#include <netinet/in.h>
+#include <endian.h>
+
+#include "resource.h"
+
+
+struct resource {
+	struct res_fork *f;
+	struct resource *next;
+
+	restype_t *type;
+	int16_t id;
+	uint8_t attr;
+	char *name, *data;
+	int namelen, datalen;
+};
+
+struct res_fork {
+	struct resource *rlist;
+	
+	char *buf;
+	int len;
+	uint16_t attr;
+	int rlen;			/* Reserved space at beginning */
+};
+
+
+struct resource *res_lookup(struct res_fork *f, restype_t type, int16_t id)
+{
+	struct resource *r;
+	for(r=f->rlist ; r ; r=r->next) {
+		if(memcmp(*r->type, type, sizeof(type)) == 0 &&
+		   (id == 0 || r->id == id)) return r;
+	}
+	return NULL;
+}
+
+static void *res_grab(struct res_fork *f, void *data, int len)
+{
+	if((char *) data >= f->buf  &&  (char *) data < f->buf + f->len) {
+		return data;
+	} else {
+		f->len += len;
+		f->buf = realloc(f->buf, f->len);
+		memcpy(f->buf + f->len - len, data, len);
+		return f->buf + f->len - len;
+	}
+}
+
+struct resource *res_new(struct res_fork *f, restype_t type, int16_t id)
+{
+	struct resource *r, **rp;
+
+	r = malloc(sizeof(*r));
+	memset(r, 0, sizeof(*r));
+	r->f = f;
+	r->id = id;
+
+	for(rp = &f->rlist ; *rp && memcmp(*(*rp)->type, type, sizeof(type)) ;
+	    rp = &(*rp)->next);
+	if(*rp) {
+		r->type = (*rp)->type;
+		while(*rp && ((*rp)->type == r->type))
+			rp = &(*rp)->next;
+	} else {
+		r->type = res_grab(f, type, sizeof(type));
+	}
+
+	r->next = *rp;
+	*rp = r;
+
+	return r;
+}
+
+void res_getdata(struct resource *r, void **dp, int *len)
+{
+	*dp  = r->data;
+	*len = r->datalen;
+}
+
+void res_setdata(struct resource *r, void *p, int len)
+{
+	if(len > r->datalen) {
+		r->data = res_grab(r->f, p, len);
+		r->datalen = len;
+	} else {
+		memcpy(r->data, p, len);
+		r->datalen = len;
+	}
+}
+
+void res_gettype(struct resource *r, restype_t type)
+{
+	memcpy(type, r->type, sizeof(type));
+}
+
+/* FIXME: implementer. */
+void res_settype(struct resource *r, restype_t type)
+{
+}
+
+void res_rename(struct resource *r, char *name, int nlen)
+{
+	if(nlen == -1) nlen = strlen(name);
+	if(!r->name || nlen > r->namelen) {
+		r->name = res_grab(r->f, name, nlen);
+		r->namelen = nlen;
+	} else {
+		memcpy(r->name, name, nlen);
+		r->namelen = nlen;
+	}
+}
+
+const char * const res_attrch = "7spLPlw0";
+void res_chattr(struct resource *r, const char *spec)
+{
+	/* FIXME: please a bit more usable */
+	r->attr = atoi(spec);
+}
+
+void res_delete(struct resource *r)
+{
+	struct resource **rp;
+	
+	for(rp = &r->f->rlist ; *rp && *rp != r ; rp = &(*rp)->next);
+	assert(*rp);
+	*rp = r->next;
+	
+	free(r);
+}
+
+void res_printinfo(struct resource *r, FILE *s)
+{	
+	int i;
+	
+	fwrite(r->type, 4, 1, s);
+	fprintf(s, " %11d ", r->id);
+	for(i=0 ; i<8 ; i++)
+		fprintf(s, "%c", ((r->attr << i) & 0x80) ? res_attrch[i] : '-');
+	fprintf(s, " %10d", r->datalen);
+	if(r->name) {
+		fprintf(s, " ");
+		fwrite(r->name, 1, r->namelen, s);
+	}
+	fprintf(s, "\n");
+}
+
+void res_ls(FILE *s, struct res_fork *f)
+{
+	struct resource *r;
+	for(r = f->rlist ; r ; r = r->next)
+		res_printinfo(r, s);
+}
+
+
+/* Resource header */
+struct reshdr {
+	uint32_t dofs, mofs, dlen, mlen;
+};
+
+struct res_fork *res_newfork()
+{
+	struct res_fork *f;
+
+	f = malloc(sizeof(*f));
+	f->buf = NULL;
+	f->len = 0;
+	f->rlen = sizeof(struct reshdr);
+	f->attr = 0;
+	f->rlist = NULL;
+
+	return f;
+}
+
+void res_delfork(struct res_fork *f)
+{
+	struct resource *r;
+	
+	if(f->buf) free(f->buf);
+	while(f->rlist) {
+		r = f->rlist->next;
+		free(f->rlist);
+		f->rlist = r;
+	}
+	free(f);
+}
+
+
+/* Resource map header */
+struct resmaphdr {
+	uint8_t reserved[22];
+	uint16_t attr;
+	uint16_t tlistofs;
+	uint16_t nlistofs;
+};
+
+/* Resource type list entry */
+struct restype {
+	restype_t type;
+	uint16_t rnum;
+	uint16_t refofs;
+};
+
+struct restypelist {
+	uint16_t tnum;
+	struct restype type[0];
+};
+
+/* Resource reference list entry */
+struct resref {
+	uint16_t id;
+	uint16_t nameofs;
+	unsigned int attr:8, dataofs:24;
+	uint32_t reserved;
+};
+
+/* Used to manipulate the 3-bytes offset */
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define ntoh3(a) (ntohl(a) >> 8)
+#define hton3(a) (htonl(a) >> 8)
+#else
+#define ntoh3(a) (a)
+#define hton3(a) (a)
+#endif
+
+/* Resource name list entry */
+struct resname {
+	uint8_t len;
+	char name[0];
+};
+
+/* Resource data */
+struct resdata {
+	uint32_t len;
+	uint8_t data[0];
+};
+
+
+#define BUFSZ 4096
+#define MAXSZ (100*1024*1024)
+
+static int res_readbuf(struct res_fork *f, FILE *stream)
+{
+	int n;
+
+	do {
+		f->buf = realloc(f->buf, f->len + BUFSZ);
+		n = fread(f->buf + f->len, 1, BUFSZ, stream);
+		f->len += n;
+	} while(n == BUFSZ && f->len < MAXSZ);
+	
+	if(f->len >= MAXSZ) {
+		fprintf(stderr, "This resource fork is rather huge !\n");
+		return -1;
+	} else if(!feof(stream)) {
+		perror(NULL);
+		return -1;
+	}
+
+	return 0;
+}
+
+#define CHECKPTR(p, m) \
+if((char *)(p) < f->buf || (char *)(p) + sizeof(*(p)) > f->buf + f->len) { \
+	fprintf(stderr, "Wild " m " offset!\n"); \
+	return -1; \
+}
+
+static int res_parse_reflist(struct res_fork *f, char *dbase, char *tbase,
+		char *nbase, struct restype *t)
+{
+	struct resource *r;
+	struct resref *ref;
+	struct resname *name;
+	struct resdata *data;
+	int i;
+
+	ref = (struct resref *) (tbase + ntohs(t->refofs));
+	for(i=0 ; i < ntohs(t->rnum) + 1 ; i++) {
+		CHECKPTR(ref+i, "reference");
+		r = res_new(f, t->type, ntohs(ref[i].id));
+		r->attr = ref[i].attr;
+		
+		if(ref[i].nameofs != htons(-1)) {
+			name = (struct resname *)(nbase+ntohs(ref[i].nameofs));
+			CHECKPTR(name, "name");
+			res_rename(r, name->name, name->len);
+		}
+
+		data = (struct resdata *)(dbase + ntoh3(ref[i].dataofs));
+		CHECKPTR(data, "data");
+		res_setdata(r, data->data, ntohl(data->len));
+	}
+
+	return 0;
+}
+
+static int res_parse_typelist(struct res_fork *f, char *dbase, char *tbase,
+		char *nbase)
+{
+	struct restypelist *l = (struct restypelist *) tbase;
+	int i;
+
+	for(i=0 ; i < ntohs(l->tnum) + 1 ; i++) {
+		if(res_parse_reflist(f, dbase, tbase, nbase, l->type + i) < 0)
+			return -1;
+	}
+
+	return 0;
+}
+
+static int res_parsebuf(struct res_fork *f)
+{
+	struct reshdr *rh;
+	struct resmaphdr *mh;
+
+	rh = (struct reshdr *) f->buf;
+	mh = (struct resmaphdr *) (f->buf + ntohl(rh->mofs));
+	CHECKPTR(mh, "resource map");
+	f->rlen = ntohl(rh->dofs) < ntohl(rh->mofs)
+		? ntohl(rh->dofs) : ntohl(rh->mofs);
+	return res_parse_typelist(f, f->buf + ntohl(rh->dofs),
+			(char *) mh + ntohs(mh->tlistofs),
+			(char *) mh + ntohs(mh->nlistofs));
+}
+
+struct res_fork *res_read(FILE *stream)
+{
+	struct res_fork *f;
+
+	f = res_newfork();
+
+	if(res_readbuf(f, stream) < 0  ||  res_parsebuf(f) < 0) {
+		res_delfork(f);
+		return NULL;
+	}
+	
+	return f;
+}
+
+void res_recreate(struct res_fork *f)
+{
+	struct resource *r;
+	restype_t *t;
+	char *buf;
+	int tnum, rnum;
+	int mlen, dlen, nlen, rlen, len;
+
+	struct reshdr *rh;
+	struct resmaphdr *mh;
+	struct restypelist *tl;
+	struct restype *type;
+	struct resref *ref;
+	struct resname *name, *name0;
+	struct resdata *data, *data0;
+	
+	t = NULL;
+	dlen = 0;
+	mlen = sizeof(struct resmaphdr) + sizeof(struct restypelist);
+	rlen = 0;
+	nlen = 0;
+	tnum = 0;
+	rnum = 0;
+	for(r = f->rlist ; r ; r = r->next) {
+		dlen += sizeof(struct resdata) + r->datalen;
+		if(r->type != t) {
+			mlen += sizeof(struct restype);
+			tnum++;
+			t = r->type;
+		}
+		rlen += sizeof(struct resref);
+		if(r->name)
+			nlen += sizeof(struct resname) + r->namelen;
+		rnum++;
+	}
+
+	len = f->rlen + dlen + mlen + rlen + nlen;
+	buf = malloc(len);
+
+	rh = (struct reshdr *) buf;
+	data = data0 = (struct resdata *) (buf + f->rlen);
+	mh = (struct resmaphdr*) (buf + f->rlen + dlen);
+	tl = (struct restypelist *) (mh + 1);
+	type = tl->type;
+	ref = (struct resref  *) (buf + f->rlen + dlen + mlen);
+	name = name0 = (struct resname *) (buf + f->rlen + dlen + mlen + rlen);
+	
+	rh->dofs = htonl(f->rlen);
+	rh->dlen = htonl(dlen);
+	rh->mofs = htonl(f->rlen + dlen);
+	rh->mlen = htonl(mlen + rlen + nlen);
+	
+	memset(mh->reserved, 0, sizeof(mh->reserved));
+	mh->attr = htons(f->attr);
+	mh->tlistofs = htons(sizeof(*mh));
+	mh->nlistofs = htons(mlen + rlen);
+	tl->tnum = htons(tnum - 1);
+
+	t = NULL;
+	for(r = f->rlist ; r ; r = r->next) {
+		if(r->type != t) {
+			if(t) type++;
+			type->refofs = htons((char*) ref - (char*) tl);
+			memcpy(type->type, r->type, sizeof(restype_t));
+			type->rnum = htons(0);
+			t = r->type;
+		} else {
+			type->rnum = htons(ntohs(type->rnum) + 1);
+		}
+		r->type = &type->type;
+		
+		ref->id = htons(r->id);
+		ref->nameofs = htons(r->name ? (char*)name - (char*)name0 : -1);
+		ref->dataofs = hton3((char *) data - (char *) data0);
+		ref->attr = r->attr;
+		ref->reserved = htonl(0);
+		ref++;
+
+		if(r->name) {
+			name->len = r->namelen;
+			memcpy(name->name, r->name, r->namelen);
+			r->name = name->name;
+			name = (struct resname *) (name->name + name->len);
+		}
+
+		data->len = htonl(r->datalen);
+		memcpy(data->data, r->data, r->datalen);
+		r->data = data->data;
+		data = (struct resdata *) (data->data + r->datalen);
+	}
+
+	free(f->buf);
+	f->buf = buf;
+	f->len = len;
+}
+
+int res_write(struct res_fork *f, FILE *stream)
+{
+	res_recreate(f);
+	return fwrite(f->buf, 1, f->len, stream) == f->len ? 0 : -1;
+}
+

Added: trunk/rsrce/resource.h
===================================================================
--- trunk/rsrce/resource.h	2006-07-11 00:53:48 UTC (rev 57)
+++ trunk/rsrce/resource.h	2006-07-11 00:56:50 UTC (rev 58)
@@ -0,0 +1,44 @@
+/* rsrce -- a Macintosh resource fork editor
+ * Copyright (C) 2004  Jeremie Koenig
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#ifndef __RESOURCE_H__
+#define __RESOURCE_H__
+
+#include <stdio.h>
+#include <stdint.h>
+
+typedef char restype_t[4];
+struct res_fork;
+struct resource;
+
+struct resource *res_new(struct res_fork *f, restype_t type, int16_t id);
+void res_getdata(struct resource *r, void **dp, int *len);
+void res_setdata(struct resource *r, void *p, int len);
+void res_gettype(struct resource *r, restype_t type);
+void res_settype(struct resource *r, restype_t type);
+void res_chattr(struct resource *r, const char *spec);
+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 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);
+
+#endif
+

Added: trunk/rsrce/translate.c
===================================================================
--- trunk/rsrce/translate.c	2006-07-11 00:53:48 UTC (rev 57)
+++ trunk/rsrce/translate.c	2006-07-11 00:56:50 UTC (rev 58)
@@ -0,0 +1,215 @@
+/* rsrce -- a Macintosh resource fork editor
+ * Copyright (C) 2004  Jeremie Koenig
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <netinet/in.h>
+#include "resource.h"
+#include "translate.h"
+
+
+#define BUFSZ 512
+
+int tr_raw_export(FILE *f, void *data, int len)
+{
+	return (fwrite(data, 1, len, f) == len) ? 0 : -1;
+}
+
+int tr_raw_import(FILE *f, void **data, int *len)
+{
+	int sz, n;
+	
+	sz = *len;
+	*len = 0;
+	
+	for(;;) {
+		if(*len + BUFSZ > sz) {
+			sz += BUFSZ;
+			*data = realloc(*data, sz);
+		}
+		n = fread(*data + *len, 1, sz - *len, f);
+		if(n == 0) return feof(f) ? 0 : -1;
+		*len += n;
+	}
+}
+
+int tr_str_export(FILE *f, void *data, int len)
+{
+	return (fwrite(data+1, 1, len-1, f) == len-1 && fputc('\n', f) == '\n')
+		? 0 : -1;
+}
+
+int tr_str_import(FILE *f, void **data, int *len)
+{
+	int sz, c;
+
+	sz = *len;
+	*len = 1;
+	while((c = fgetc(f)) >= 0 && c != '\n') {
+		if(*len >= sz)
+			*data = realloc(*data, sz += BUFSZ);
+		((unsigned char *) *data)[(*len)++] = c;
+	}
+	if(*len == 1)
+		return -1;
+
+	* (unsigned char *) *data = *len - 1;
+
+	return (feof(f) || c == '\n') ? 0 : -1;
+}
+
+int tr_strhash_export(FILE *f, void *data, int len)
+{
+	unsigned short *n;
+	unsigned char *s;
+	int strn;
+
+	n = (unsigned short *) data;
+	s = (unsigned char  *) (n+1);
+	strn = ntohs(*n);
+	
+	while(strn--) {
+		if(tr_str_export(f, s, *s+1) != 0)
+			return -1;
+		s += *s;
+	}
+
+	return 0;
+}
+
+int tr_strhash_import(FILE *f, void **data, int *len)
+{
+	void *sd;
+	int sl, ret;
+	
+	*len = sizeof(unsigned short);
+	*data = realloc(*data, *len);
+
+	* (unsigned short *) *data = 0;
+	do {
+		sd = NULL;
+		sl = 0;
+		ret = tr_str_import(f, &sd, &sl);
+		if(ret == 0) {	
+			*data = realloc(*data, *len + sl);
+			memcpy(*data + *len, sd, sl);
+			*len += sl;
+			if(sd) free(sd);
+			(* (unsigned short *) *data)++;
+		}
+	} while(ret == 0 && !feof(f));
+	
+	* (unsigned short *) *data = htons(* (unsigned short *) *data);
+
+	return feof(f) ? 0 : -1;
+}
+
+int tr_cmdl_export(FILE *f, void *data, int len)
+{
+	const char empty[] = "";
+
+	if(!len) {
+		data = (void *) empty;
+		len  = sizeof(len);
+	}
+	if(!memchr(data, '\0', len)) {
+		fprintf(stderr, "No terminating null byte.\n");
+		return -1;
+	}
+	fprintf(f, "%s\n", (char*) data);
+
+	return 0;
+}
+
+int tr_cmdl_import(FILE *f, void **data, int *len)
+{
+	char buf[512], *c;
+
+	c = fgets(buf, sizeof(buf), f);
+	if(!c) {
+		perror(NULL);
+		return -1;
+	}
+	c = strchr(buf, '\n');
+	if(c) *(c++) = '\0';
+
+	*len  = strlen(buf) + 1;
+	*data = realloc(*data, *len);
+	strcpy(*data, buf);
+	return 0;
+}
+
+struct translator {
+	restype_t type;
+	const char *ext;
+	int (*export)(FILE *f, void *data, int len);
+	int (*import)(FILE *f, void **data, int *len);
+} tr_table[] = {
+	{"STR ", "txt", tr_str_export,		tr_str_import		},
+	{"STR#", "txt", tr_strhash_export,	tr_strhash_import	},
+	{"CMDL", "txt", tr_cmdl_export,		tr_cmdl_import		},
+	{"",     "bin", tr_raw_export,		tr_raw_import		},
+	{"", NULL, NULL, NULL}
+};
+
+struct translator *tr_lookup(struct resource *r, const char *ext)
+{
+	restype_t type;
+	int i;
+
+	res_gettype(r, type);
+	for(i=0 ; tr_table[i].export ; i++)
+		if(!((type[0] && memcmp(tr_table[i].type, type, sizeof(type)))
+		     || (ext && strcmp(tr_table[i].ext, ext))))
+			return tr_table + i;
+	return NULL;
+}
+
+const char *tr_ext(struct translator *tr)
+{
+	return tr->ext;
+}
+
+int tr_export(struct translator *tr, struct resource *r, FILE *out)
+{
+	void *data;
+	int len;
+
+	res_getdata(r, &data, &len);
+	return tr->export(out, data, len);
+}
+
+int tr_import(struct translator *tr, struct resource *r, FILE *in)
+{
+	void *data;
+	int len, ret;
+
+	data = NULL;
+	len = 0;
+	ret = tr->import(in, &data, &len);
+
+	if(!ret)
+		res_setdata(r, data, len);
+	else
+		fprintf(stderr, "Couldn't import resource data\n");
+	if(data)
+		free(data);
+
+	return ret;
+}
+

Added: trunk/rsrce/translate.h
===================================================================
--- trunk/rsrce/translate.h	2006-07-11 00:53:48 UTC (rev 57)
+++ trunk/rsrce/translate.h	2006-07-11 00:56:50 UTC (rev 58)
@@ -0,0 +1,29 @@
+/* rsrce -- a Macintosh resource fork editor
+ * Copyright (C) 2004  Jeremie Koenig
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#ifndef __TRANSLATE_H__
+#define __TRANSLATE_H__
+
+#include <stdio.h>
+#include "resource.h"
+
+struct translator *tr_lookup(struct resource *r, const char *ext);
+const char *tr_ext(struct translator *t);
+int tr_export(struct translator *t, struct resource *r, FILE *o);
+int tr_import(struct translator *t, struct resource *r, FILE *i);
+
+#endif





More information about the Debootloaders-devel mailing list