summaryrefslogtreecommitdiff
path: root/src/lib/libast/vec
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libast/vec')
-rw-r--r--src/lib/libast/vec/vecargs.c76
-rw-r--r--src/lib/libast/vec/vecfile.c62
-rw-r--r--src/lib/libast/vec/vecfree.c48
-rw-r--r--src/lib/libast/vec/vecload.c96
-rw-r--r--src/lib/libast/vec/vecstring.c46
5 files changed, 328 insertions, 0 deletions
diff --git a/src/lib/libast/vec/vecargs.c b/src/lib/libast/vec/vecargs.c
new file mode 100644
index 0000000..421ade0
--- /dev/null
+++ b/src/lib/libast/vec/vecargs.c
@@ -0,0 +1,76 @@
+/***********************************************************************
+* *
+* This software is part of the ast package *
+* Copyright (c) 1985-2011 AT&T Intellectual Property *
+* and is licensed under the *
+* Eclipse Public License, Version 1.0 *
+* by AT&T Intellectual Property *
+* *
+* A copy of the License is available at *
+* http://www.eclipse.org/org/documents/epl-v10.html *
+* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
+* *
+* Information and Software Systems Research *
+* AT&T Research *
+* Florham Park NJ *
+* *
+* Glenn Fowler <gsf@research.att.com> *
+* David Korn <dgk@research.att.com> *
+* Phong Vo <kpv@research.att.com> *
+* *
+***********************************************************************/
+#pragma prototyped
+/*
+ * Glenn Fowler
+ * AT&T Bell Laboratories
+ *
+ * string vector argv insertion
+ */
+
+#include <ast.h>
+#include <vecargs.h>
+#include <ctype.h>
+
+/*
+ * insert the string vector vec between
+ * (*argvp)[0] and (*argvp)[1], sliding (*argvp)[1] ... over
+ * null and blank args are deleted
+ *
+ * vecfree always called
+ *
+ * -1 returned if insertion failed
+ */
+
+int
+vecargs(register char** vec, int* argcp, char*** argvp)
+{
+ register char** argv;
+ register char** oargv;
+ char** ovec;
+ char* s;
+ int num;
+
+ if (!vec) return(-1);
+ if ((num = (char**)(*(vec - 1)) - vec) > 0)
+ {
+ if (!(argv = newof(0, char*, num + *argcp + 1, 0)))
+ {
+ vecfree(vec, 0);
+ return(-1);
+ }
+ oargv = *argvp;
+ *argvp = argv;
+ *argv++ = *oargv++;
+ ovec = vec;
+ while (s = *argv = *vec++)
+ {
+ while (isspace(*s)) s++;
+ if (*s) argv++;
+ }
+ vecfree(ovec, 1);
+ while (*argv = *oargv++) argv++;
+ *argcp = argv - *argvp;
+ }
+ else vecfree(vec, 0);
+ return(0);
+}
diff --git a/src/lib/libast/vec/vecfile.c b/src/lib/libast/vec/vecfile.c
new file mode 100644
index 0000000..6c6991b
--- /dev/null
+++ b/src/lib/libast/vec/vecfile.c
@@ -0,0 +1,62 @@
+/***********************************************************************
+* *
+* This software is part of the ast package *
+* Copyright (c) 1985-2011 AT&T Intellectual Property *
+* and is licensed under the *
+* Eclipse Public License, Version 1.0 *
+* by AT&T Intellectual Property *
+* *
+* A copy of the License is available at *
+* http://www.eclipse.org/org/documents/epl-v10.html *
+* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
+* *
+* Information and Software Systems Research *
+* AT&T Research *
+* Florham Park NJ *
+* *
+* Glenn Fowler <gsf@research.att.com> *
+* David Korn <dgk@research.att.com> *
+* Phong Vo <kpv@research.att.com> *
+* *
+***********************************************************************/
+#pragma prototyped
+/*
+ * Glenn Fowler
+ * AT&T Research
+ *
+ * string vector load support
+ */
+
+#include <ast.h>
+#include <ls.h>
+#include <vecargs.h>
+
+/*
+ * load a string vector from lines in file
+ */
+
+char**
+vecfile(const char* file)
+{
+ register int n;
+ register char* buf;
+ register char** vec;
+ int fd;
+ struct stat st;
+
+ vec = 0;
+ if ((fd = open(file, O_RDONLY)) >= 0)
+ {
+ if (!fstat(fd, &st) && S_ISREG(st.st_mode) && (n = st.st_size) > 0 && (buf = newof(0, char, n + 1, 0)))
+ {
+ if (read(fd, buf, n) == n)
+ {
+ buf[n] = 0;
+ vec = vecload(buf);
+ }
+ if (!vec) free(buf);
+ }
+ close(fd);
+ }
+ return(vec);
+}
diff --git a/src/lib/libast/vec/vecfree.c b/src/lib/libast/vec/vecfree.c
new file mode 100644
index 0000000..12b3bc2
--- /dev/null
+++ b/src/lib/libast/vec/vecfree.c
@@ -0,0 +1,48 @@
+/***********************************************************************
+* *
+* This software is part of the ast package *
+* Copyright (c) 1985-2011 AT&T Intellectual Property *
+* and is licensed under the *
+* Eclipse Public License, Version 1.0 *
+* by AT&T Intellectual Property *
+* *
+* A copy of the License is available at *
+* http://www.eclipse.org/org/documents/epl-v10.html *
+* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
+* *
+* Information and Software Systems Research *
+* AT&T Research *
+* Florham Park NJ *
+* *
+* Glenn Fowler <gsf@research.att.com> *
+* David Korn <dgk@research.att.com> *
+* Phong Vo <kpv@research.att.com> *
+* *
+***********************************************************************/
+#pragma prototyped
+/*
+ * Glenn Fowler
+ * AT&T Bell Laboratories
+ *
+ * file to string vector support
+ */
+
+#include <ast.h>
+#include <vecargs.h>
+
+/*
+ * free a string vector generated by vecload()
+ *
+ * retain!=0 frees the string pointers but retains the string data
+ * in this case the data is permanently allocated
+ */
+
+void
+vecfree(register char** vec, int retain)
+{
+ if (vec)
+ {
+ if (*(vec -= 2) && !retain) free(*vec);
+ free(vec);
+ }
+}
diff --git a/src/lib/libast/vec/vecload.c b/src/lib/libast/vec/vecload.c
new file mode 100644
index 0000000..5f83470
--- /dev/null
+++ b/src/lib/libast/vec/vecload.c
@@ -0,0 +1,96 @@
+/***********************************************************************
+* *
+* This software is part of the ast package *
+* Copyright (c) 1985-2011 AT&T Intellectual Property *
+* and is licensed under the *
+* Eclipse Public License, Version 1.0 *
+* by AT&T Intellectual Property *
+* *
+* A copy of the License is available at *
+* http://www.eclipse.org/org/documents/epl-v10.html *
+* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
+* *
+* Information and Software Systems Research *
+* AT&T Research *
+* Florham Park NJ *
+* *
+* Glenn Fowler <gsf@research.att.com> *
+* David Korn <dgk@research.att.com> *
+* Phong Vo <kpv@research.att.com> *
+* *
+***********************************************************************/
+#pragma prototyped
+/*
+ * Glenn Fowler
+ * AT&T Bell Laboratories
+ *
+ * string vector load support
+ */
+
+#include <ast.h>
+#include <vecargs.h>
+
+/*
+ * load a string vector from lines in buf
+ * buf may be modified on return
+ *
+ * each line in buf is treated as a new vector element
+ * lines with # as first char are comments
+ * \ as the last char joins consecutive lines
+ *
+ * the vector ends with a 0 sentinel
+ *
+ * the string array pointer is returned
+ */
+
+char**
+vecload(char* buf)
+{
+ register char* s;
+ register int n;
+ register char** p;
+ char** vec;
+
+ vec = 0;
+ n = (*buf == '#') ? -1 : 0;
+ for (s = buf;; s++)
+ {
+ if (*s == '\n')
+ {
+ if (s > buf && *(s - 1) == '\\') *(s - 1) = *s = ' ';
+ else
+ {
+ *s = 0;
+ if (*(s + 1) != '#')
+ {
+ n++;
+ if (!*(s + 1)) break;
+ }
+ }
+ }
+ else if (!*s)
+ {
+ n++;
+ break;
+ }
+ }
+ if (n < 0) n = 0;
+ if (p = newof(0, char*, n + 3, 0))
+ {
+ *p++ = s = buf;
+ vec = ++p;
+ if (n > 0) for (;;)
+ {
+ if (*s != '#')
+ {
+ *p++ = s;
+ if (--n <= 0) break;
+ }
+ while (*s) s++;
+ s++;
+ }
+ *p = 0;
+ *(vec - 1) = (char*)p;
+ }
+ return(vec);
+}
diff --git a/src/lib/libast/vec/vecstring.c b/src/lib/libast/vec/vecstring.c
new file mode 100644
index 0000000..6aeac9d
--- /dev/null
+++ b/src/lib/libast/vec/vecstring.c
@@ -0,0 +1,46 @@
+/***********************************************************************
+* *
+* This software is part of the ast package *
+* Copyright (c) 1985-2011 AT&T Intellectual Property *
+* and is licensed under the *
+* Eclipse Public License, Version 1.0 *
+* by AT&T Intellectual Property *
+* *
+* A copy of the License is available at *
+* http://www.eclipse.org/org/documents/epl-v10.html *
+* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
+* *
+* Information and Software Systems Research *
+* AT&T Research *
+* Florham Park NJ *
+* *
+* Glenn Fowler <gsf@research.att.com> *
+* David Korn <dgk@research.att.com> *
+* Phong Vo <kpv@research.att.com> *
+* *
+***********************************************************************/
+#pragma prototyped
+/*
+ * Glenn Fowler
+ * AT&T Bell Laboratories
+ *
+ * string vector load support
+ */
+
+#include <ast.h>
+#include <vecargs.h>
+
+/*
+ * load a string vector from lines in str
+ */
+
+char**
+vecstring(const char* str)
+{
+ register char* buf;
+ register char** vec;
+
+ if (!str || !*str || !(buf = strdup(str))) vec = 0;
+ else if (!(vec = vecload(buf))) free(buf);
+ return(vec);
+}