Permalink
Browse files

ship with par

  • Loading branch information...
martinh committed Jun 3, 2011
1 parent aaf35ac commit aed2fbc56590cae04a6252d5c711cea251a7df23
Showing with 4,112 additions and 0 deletions.
  1. +17 −0 par/Makefile
  2. +215 −0 par/buffer.c
  3. +78 −0 par/buffer.h
  4. +319 −0 par/charset.c
  5. +71 −0 par/charset.h
  6. +23 −0 par/errmsg.c
  7. +43 −0 par/errmsg.h
  8. +936 −0 par/par.c
  9. +1,345 −0 par/par.doc
  10. +112 −0 par/protoMakefile
  11. +575 −0 par/reformat.c
  12. +29 −0 par/reformat.h
  13. +219 −0 par/releasenotes
  14. +130 −0 vico.xcodeproj/project.pbxproj
View
@@ -0,0 +1,17 @@
+#! /usr/bin/make
+
+BIN=$(DESTDIR)/usr/bin
+DOC=$(DESTDIR)/usr/share/doc/par
+MAN=$(DESTDIR)/usr/share/man/man1
+
+include protoMakefile
+
+CC = cc $(CFLAGS) -c
+
+install: par par.doc
+ install -o root -g root -m 0755 par $(BIN)/par
+ install -d $(DOC) -o root -g root -m 0755
+ install -o root -g root -m 0644 par.doc $(DOC)
+ install -d $(MAN) -o root -g root -m 0755
+ install -o root -g root -m 0644 par.1 $(MAN)
+
View
@@ -0,0 +1,215 @@
+/***********************/
+/* buffer.c */
+/* for Par 1.52-i18n.3 */
+/* Copyright 2001 by */
+/* Adam M. Costello */
+/***********************/
+
+/* This is ANSI C code (C89). */
+
+
+/* additem(), copyitems(), and nextitem() rely on the fact that */
+/* sizeof (char) is 1. See section A7.4.8 of The C Programming */
+/* Language, Second Edition, by Kerninghan and Ritchie. */
+
+
+#include "buffer.h" /* Makes sure we're consistent with the prototypes. */
+ /* Also includes <stddef.h> and "errmsg.h". */
+
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+
+#undef NULL
+#define NULL ((void *) 0)
+
+#ifdef DONTFREE
+#define free(ptr)
+#endif
+
+
+struct buffer {
+ struct block *firstblk, /* The first block. */
+ *current, /* The last non-empty block, or */
+ /* firstblk if all are empty. */
+ *nextblk; /* The block containing the item to be */
+ /* returned by nextitem(), or NULL. */
+ int nextindex; /* Index of item in nextblock->items. */
+ size_t itemsize; /* The size of an item. */
+};
+
+typedef struct block {
+ struct block *next; /* The next block, or NULL if none. */
+ void *items; /* Storage for the items in this block. */
+ int maxhere, /* Number of items that fit in *items. */
+ numprevious, /* Total of numhere for all previous blocks. */
+ numhere; /* The first numhere slots in *items are filled. */
+} block;
+
+
+buffer *newbuffer(size_t itemsize, errmsg_t errmsg)
+{
+ buffer *buf;
+ block *blk;
+ void *items;
+ int maxhere;
+
+ maxhere = 124 / itemsize;
+ if (maxhere < 4) maxhere = 4;
+
+ buf = malloc(sizeof (buffer));
+ blk = malloc(sizeof (block));
+ items = malloc(maxhere * itemsize);
+ if (!buf || !blk || !items) {
+ wcscpy(errmsg,outofmem);
+ goto nberror;
+ }
+
+ buf->itemsize = itemsize;
+ buf->firstblk = buf->current = buf->nextblk = blk;
+ buf->nextindex = 0;
+ blk->next = NULL;
+ blk->numprevious = blk->numhere = 0;
+ blk->maxhere = maxhere;
+ blk->items = items;
+
+ *errmsg = '\0';
+ return buf;
+
+nberror:
+
+ if (buf) free(buf);
+ if (blk) free(blk);
+ if (items) free(items);
+ return NULL;
+}
+
+
+void freebuffer(buffer *buf)
+{
+ block *blk, *tmp;
+
+ blk = buf->firstblk;
+ while (blk) {
+ tmp = blk;
+ blk = blk->next;
+ if (tmp->items) free(tmp->items);
+ free(tmp);
+ }
+
+ free(buf);
+}
+
+
+void clearbuffer(buffer *buf)
+{
+ block *blk;
+
+ for (blk = buf->firstblk; blk; blk = blk->next)
+ blk->numhere = 0;
+
+ buf->current = buf->firstblk;
+}
+
+
+void additem(buffer *buf, const void *item, errmsg_t errmsg)
+{
+ block *blk, *new;
+ void *items;
+ int maxhere;
+ size_t itemsize = buf->itemsize;
+
+ blk = buf->current;
+
+ if (blk->numhere == blk->maxhere) {
+ new = blk->next;
+ if (!new) {
+ maxhere = 2 * blk->maxhere;
+ new = malloc(sizeof (block));
+ items = malloc(maxhere * itemsize);
+ if (!new || !items) {
+ wcscpy(errmsg,outofmem);
+ goto aierror;
+ }
+ blk->next = new;
+ new->next = NULL;
+ new->maxhere = maxhere;
+ new->numprevious = blk->numprevious + blk->numhere;
+ new->numhere = 0;
+ new->items = items;
+ }
+ blk = buf->current = new;
+ }
+
+ memcpy( ((char *) blk->items) + (blk->numhere * itemsize), item, itemsize );
+
+ ++blk->numhere;
+
+ *errmsg = '\0';
+ return;
+
+aierror:
+
+ if (new) free(new);
+ if (items) free(items);
+}
+
+
+int numitems(buffer *buf)
+{
+ block *blk = buf->current;
+ return blk->numprevious + blk->numhere;
+}
+
+
+void *copyitems(buffer *buf, errmsg_t errmsg)
+{
+ int n;
+ void *r;
+ block *blk, *b;
+ size_t itemsize = buf->itemsize;
+
+ b = buf->current;
+ n = b->numprevious + b->numhere;
+ if (!n) return NULL;
+
+ r = malloc(n * itemsize);
+ if (!r) {
+ wcscpy(errmsg,outofmem);
+ return NULL;
+ }
+
+ b = b->next;
+
+ for (blk = buf->firstblk; blk != b; blk = blk->next)
+ memcpy( ((char *) r) + (blk->numprevious * itemsize),
+ blk->items, blk->numhere * itemsize);
+
+ *errmsg = '\0';
+ return r;
+}
+
+
+void rewindbuffer(buffer *buf)
+{
+ buf->nextblk = buf->firstblk;
+ buf->nextindex = 0;
+}
+
+
+void *nextitem(buffer *buf)
+{
+ void *r;
+
+ if (!buf->nextblk || buf->nextindex >= buf->nextblk->numhere)
+ return NULL;
+
+ r = ((char *) buf->nextblk->items) + (buf->nextindex * buf->itemsize);
+
+ if (++buf->nextindex >= buf->nextblk->maxhere) {
+ buf->nextblk = buf->nextblk->next;
+ buf->nextindex = 0;
+ }
+
+ return r;
+}
View
@@ -0,0 +1,78 @@
+/***********************/
+/* buffer.h */
+/* for Par 1.52-i18n.3 */
+/* Copyright 2001 by */
+/* Adam M. Costello */
+/***********************/
+
+/* This is ANSI C code (C89). */
+
+
+/* Note: Those functions declared here which do not use errmsg */
+/* always succeed, provided that they are passed valid arguments. */
+
+
+#include "errmsg.h"
+
+#include <stddef.h>
+
+
+typedef struct buffer buffer;
+
+
+buffer *newbuffer(size_t itemsize, errmsg_t errmsg);
+
+ /* newbuffer(itemsize,errmsg) returns a pointer to a */
+ /* new empty buffer which holds items of size itemsize. */
+ /* itemsize must not be 0. Returns NULL on failure. */
+
+
+void freebuffer(buffer *buf);
+
+ /* freebuffer(buf) frees the memory associated with */
+ /* *buf. buf may not be used after this call. */
+
+
+void clearbuffer(buffer *buf);
+
+ /* clearbuffer(buf) removes */
+ /* all items from *buf, but */
+ /* does not free any memory. */
+
+
+void additem(buffer *buf, const void *item, errmsg_t errmsg);
+
+ /* additem(buf,item,errmsg) copies *item to the end of */
+ /* *buf. item must point to an object of the proper size */
+ /* for *buf. If additem() fails, *buf will be unaffected. */
+
+
+int numitems(buffer *buf);
+
+ /* numitems(buf) returns the number of items in *buf. */
+
+
+void *copyitems(buffer *buf, errmsg_t errmsg);
+
+ /* copyitems(buf,errmsg) returns an array of objects of */
+ /* the proper size for *buf, one for each item in *buf, */
+ /* or NULL if there are no items in buf. The elements */
+ /* of the array are copied from the items in *buf, in */
+ /* order. The array is allocated with malloc(), so it */
+ /* may be freed with free(). Returns NULL on failure. */
+
+
+void *nextitem(buffer *buf);
+
+ /* When buf was created by newbuffer, a pointer associated with buf */
+ /* was initialized to point at the first slot in *buf. If there is */
+ /* an item in the slot currently pointed at, nextitem(buf) advances */
+ /* the pointer to the next slot and returns the old value. If there */
+ /* is no item in the slot, nextitem(buf) leaves the pointer where it */
+ /* is and returns NULL. */
+
+
+void rewindbuffer(buffer *buf);
+
+ /* rewindbuffer(buf) resets the pointer used by */
+ /* nextitem() to point at the first slot in *buf. */
Oops, something went wrong.

0 comments on commit aed2fbc

Please sign in to comment.