summaryrefslogtreecommitdiff
path: root/Master/tlpkg/dev/lib/C
diff options
context:
space:
mode:
Diffstat (limited to 'Master/tlpkg/dev/lib/C')
-rw-r--r--Master/tlpkg/dev/lib/C/Makefile7
-rw-r--r--Master/tlpkg/dev/lib/C/tlp-config.c75
-rw-r--r--Master/tlpkg/dev/lib/C/tlp-config.h23
-rw-r--r--Master/tlpkg/dev/lib/C/tlp-src.c321
-rw-r--r--Master/tlpkg/dev/lib/C/tlp-src.h75
-rw-r--r--Master/tlpkg/dev/lib/C/tlp-utils.c182
-rw-r--r--Master/tlpkg/dev/lib/C/tlp-utils.h73
7 files changed, 756 insertions, 0 deletions
diff --git a/Master/tlpkg/dev/lib/C/Makefile b/Master/tlpkg/dev/lib/C/Makefile
new file mode 100644
index 00000000000..396b9744290
--- /dev/null
+++ b/Master/tlpkg/dev/lib/C/Makefile
@@ -0,0 +1,7 @@
+CFLAGS = -Wall -I.
+SO = dylib
+
+all: libtlp.$(SO)
+
+libtlp.$(SO): tlp-src.o tlp-config.o tlp-utils.o
+ $(CC) $^ -dynamiclib -o $@
diff --git a/Master/tlpkg/dev/lib/C/tlp-config.c b/Master/tlpkg/dev/lib/C/tlp-config.c
new file mode 100644
index 00000000000..5633db611dd
--- /dev/null
+++ b/Master/tlpkg/dev/lib/C/tlp-config.c
@@ -0,0 +1,75 @@
+/* tlp-config.c - module exporting configuration stuff
+ *
+ * Copyright 2007 Jjgod Jiang
+ * Derived from the Perl implementation by Norbert Preining
+ *
+ * This file is licensed under the GNU General Public License version 2
+ * or any later version. */
+
+#include <tlp-utils.h>
+#include <stdlib.h>
+
+#define TLP_META_CATEGORIES "Collection", "Scheme"
+#define TLP_NORMAL_CATEGORIES "Package", "TLCore", "Documentation"
+#define TLP_DEFAULT_CATEGORY "Package"
+
+const char *_tlp_meta_categories[] = {
+ TLP_META_CATEGORIES,
+};
+
+const char *_tlp_normal_categories[] = {
+ TLP_NORMAL_CATEGORIES,
+};
+
+const char *_tlp_categories[] = {
+ TLP_META_CATEGORIES,
+ TLP_NORMAL_CATEGORIES,
+};
+
+tlp_str_list *tlp_meta_categories;
+tlp_str_list *tlp_normal_categories;
+tlp_str_list *tlp_categories;
+tlp_str tlp_default_category;
+
+#define ARR_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
+
+/* TODO: we might have more complicated implementation later. */
+tlp_result tlp_config_init()
+{
+ tlp_meta_categories = tlp_str_list_from_array(_tlp_meta_categories);
+ if (tlp_meta_categories == NULL)
+ goto failed1;
+
+ tlp_normal_categories = tlp_str_list_from_array(_tlp_normal_categories);
+ if (tlp_normal_categories == NULL)
+ goto failed2;
+
+ tlp_categories = tlp_str_list_from_array(_tlp_categories);
+ if (tlp_categories == NULL)
+ goto failed3;
+
+ tlp_default_category = tlp_str_from_bytes(TLP_DEFAULT_CATEGORY);
+ if (tlp_default_category == NULL)
+ goto failed4;
+
+ return TLP_OK;
+
+failed4:
+ tlp_str_list_free(tlp_categories);
+failed3:
+ tlp_str_list_free(tlp_normal_categories);
+failed2:
+ tlp_str_list_free(tlp_meta_categories);
+failed1:
+ return TLP_FAILED;
+}
+
+void tlp_config_free()
+{
+ tlp_str_list_free(tlp_meta_categories);
+ tlp_str_list_free(tlp_normal_categories);
+ tlp_str_list_free(tlp_categories);
+
+ tlp_str_free(tlp_default_category);
+}
+
diff --git a/Master/tlpkg/dev/lib/C/tlp-config.h b/Master/tlpkg/dev/lib/C/tlp-config.h
new file mode 100644
index 00000000000..c00e2143c84
--- /dev/null
+++ b/Master/tlpkg/dev/lib/C/tlp-config.h
@@ -0,0 +1,23 @@
+/* tlp-config.c - header exporting configuration stuff
+ *
+ * Copyright 2007 Jjgod Jiang
+ * Derived from the Perl implementation by Norbert Preining
+ *
+ * This file is licensed under the GNU General Public License version 2
+ * or any later version. */
+
+#ifndef __TLP_CONFIG__
+#define __TLP_CONFIG__
+
+/* We use the following pointers to encapsulate the real config module
+ * implementation: we could use static allocated arrays, or we could
+ * use dynamic allocated ones, as long as we follow the rule (put a
+ * NULL pointer at the end of it), internal implementation does not
+ * matter. */
+
+extern tlp_str_list tlp_meta_categories;
+extern tlp_str_list tlp_normal_categories;
+extern tlp_str_list tlp_categories;
+
+#endif
+
diff --git a/Master/tlpkg/dev/lib/C/tlp-src.c b/Master/tlpkg/dev/lib/C/tlp-src.c
new file mode 100644
index 00000000000..a67aacbb74f
--- /dev/null
+++ b/Master/tlpkg/dev/lib/C/tlp-src.c
@@ -0,0 +1,321 @@
+/* tlp-src.c - module for using tlpsrc files
+ *
+ * Copyright 2007 Jjgod Jiang
+ * Derived from the Perl implementation by Norbert Preining
+ *
+ * This file is licensed under the GNU General Public License version 2
+ * or any later version. */
+
+#include <tlp-src.h>
+#include <stdio.h>
+
+/* internal function prototypes {{{ */
+
+tlp_result tlp_src_process_line(tlp_src *src, tlp_str line,
+ int *started, int *finished,
+ const char *file);
+
+tlp_result tlp_str_get_word(tlp_str str, tlp_str word, int max);
+
+tlp_result tlp_src_get_name(tlp_str line,
+ tlp_src *src,
+ int *started,
+ const char *file);
+
+tlp_result tlp_src_get_category(tlp_str line,
+ tlp_src *src,
+ int *started,
+ const char *file);
+
+/* }}} */
+
+/* internal data structures {{{ */
+
+struct tlp_src_field {
+ const char *key;
+ tlp_result (*func)(tlp_str line,
+ tlp_src *src,
+ int *started,
+ const char *file);
+};
+
+/* for lines start with "name", we use function tlp_src_get_name() to
+ * process it, and feed it with the current tlp_src instance. */
+
+struct tlp_src_field tlp_src_fields[] = {
+ { "name", tlp_src_get_name },
+ { "category", tlp_src_get_category }, /*
+ { "shortdesc", tlp_src_get_shortdesc },
+ { "longdesc", tlp_src_get_longdesc },
+ { "catalogue", tlp_src_get_catalogue },
+
+ { "runpatterns", tlp_src_get_runpatterns },
+ { "srcpatterns", tlp_src_get_srcpatterns },
+ { "docpatterns", tlp_src_get_docpatterns },
+ { "binpatterns", tlp_src_get_binpatterns },
+
+ { "execute", tlp_src_get_execute },
+ { "depend", tlp_src_get_depend }, */
+};
+
+#define TLP_SRC_TOTAL_FIELDS (sizeof(tlp_src_fields) / sizeof(tlp_src_fields[0]))
+
+/* }}} */
+
+/* tlp_src related {{{ */
+
+tlp_src *tlp_src_new(tlp_str name,
+ tlp_str category,
+ tlp_str shortdesc,
+ tlp_str longdesc,
+ tlp_str catalogue,
+ tlp_pattern *runpatterns,
+ tlp_pattern *srcpatterns,
+ tlp_pattern *docpatterns,
+ tlp_pattern *binpatterns,
+ tlp_execute *executes,
+ tlp_depend *depends)
+{
+ tlp_src *src = tlp_new(tlp_src);
+ if (! src)
+ return NULL;
+
+ src->name = tlp_str_copy(name);
+ src->category = tlp_str_copy(category);
+ src->shortdesc = tlp_str_copy(shortdesc);
+ src->longdesc = tlp_str_copy(longdesc);
+ src->catalogue = tlp_str_copy(catalogue);
+
+ src->runpatterns = runpatterns;
+ src->srcpatterns = srcpatterns;
+ src->docpatterns = docpatterns;
+ src->binpatterns = binpatterns;
+
+ src->executes = executes;
+ src->depends = depends;
+
+ return src;
+}
+
+void tlp_src_free(tlp_src *src)
+{
+ if (! src)
+ return;
+
+ tlp_str_free(src->name);
+ tlp_str_free(src->category);
+ tlp_str_free(src->shortdesc);
+ tlp_str_free(src->longdesc);
+ tlp_str_free(src->catalogue);
+
+ tlp_patterns_free(src->runpatterns);
+ tlp_patterns_free(src->srcpatterns);
+ tlp_patterns_free(src->docpatterns);
+ tlp_patterns_free(src->binpatterns);
+
+ tlp_executes_free(src->executes);
+ tlp_depends_free(src->depends);
+
+ free(src);
+ src = NULL;
+}
+
+tlp_src *tlp_src_from_file(char *file)
+{
+ FILE *fp;
+ tlp_src *src;
+ tlp_char line[TLP_SRC_LINE_MAX];
+ int started, finished;
+
+ fp = fopen(file, "r");
+ if (! fp)
+ return NULL;
+
+ src = tlp_new(tlp_src);
+
+ /* TODO: if the argument is not readable as is, try looking
+ * for it in the hierarchy where we are.*/
+ if (! src)
+ return NULL;
+
+ started = finished = 0;
+
+ while (tlp_fgets(line, TLP_SRC_LINE_MAX, fp))
+ {
+ if (tlp_src_process_line(src, line,
+ &started,
+ &finished,
+ file) != TLP_OK)
+ goto failed;
+ }
+
+ fclose(fp);
+ return src;
+
+failed:
+ tlp_src_free(src);
+ fclose(fp);
+ return NULL;
+}
+
+tlp_result tlp_src_process_line(tlp_src *src, tlp_str line,
+ int *started, int *finished,
+ const char *file)
+{
+ int i;
+
+ /* skip all leading spaces */
+ for (i = 0; tlp_char_is_space(line[i]); i++)
+ ;
+
+ /* skip comment lines */
+ if (line[i] == TLP_CHAR_COMMENT)
+ return TLP_OK;
+
+ if (tlp_char_is_eol(line[i]))
+ {
+ if (! *started || *finished)
+ return TLP_OK;
+
+ tlp_error_msg("%s: empty line not allowed in tlpsrc\n", file);
+ return TLP_FAILED;
+ }
+
+ /* something like " name ..." is not allowed */
+ if (line[0] == ' ')
+ {
+ tlp_error_msg("%s: continuation line not allowed in tlpsrc: ", file);
+ tlp_error_str(line);
+
+ return TLP_FAILED;
+ }
+
+ for (i = 0; i < TLP_SRC_TOTAL_FIELDS; i++)
+ if (tlp_str_start_with_bytes(line, tlp_src_fields[i].key))
+ return tlp_src_fields[i].func(line, src, started, file);
+
+ tlp_error_msg("%s: unknown tlpsrc directive, please fix: %s",
+ file, line);
+ return TLP_OK;
+}
+
+tlp_result tlp_src_get_name(tlp_str line,
+ tlp_src *src,
+ int *started,
+ const char *file)
+{
+ tlp_char name[TLP_SRC_NAME_MAX];
+
+ if (*started)
+ {
+ tlp_error_msg("%s: tlpsrc cannot have two name directives", file);
+ return TLP_FAILED;
+ }
+
+ if (tlp_str_get_word(line + tlp_str_len(TLP_STR("name")),
+ name, TLP_SRC_NAME_MAX) == TLP_OK)
+ {
+ src->name = tlp_str_copy(name);
+ *started = 1;
+
+ return TLP_OK;
+ }
+ else
+ return TLP_FAILED;
+}
+
+tlp_result tlp_src_get_category(tlp_str line,
+ tlp_src *src,
+ int *started,
+ const char *file)
+{
+ if (! *started)
+ {
+ tlp_error_msg("%s: first tlpsrc directive must be 'name'", file);
+ return TLP_FAILED;
+ }
+
+ return TLP_OK;
+}
+
+/* }}} */
+
+/* tlp_pattern related {{{ */
+
+void tlp_pattern_free(tlp_pattern *pat)
+{
+ if (pat != NULL)
+ return;
+
+ switch (pat->type)
+ {
+ case TLP_PATTERN_T:
+ tlp_str_list_free(pat->u.t);
+ break;
+
+ case TLP_PATTERN_F:
+ tlp_str_free(pat->u.f);
+ break;
+
+ case TLP_PATTERN_D:
+ tlp_str_free(pat->u.d);
+ break;
+
+ case TLP_PATTERN_R:
+ tlp_str_free(pat->u.r);
+ break;
+ }
+
+ free(pat);
+ pat = NULL;
+}
+
+void tlp_patterns_free(tlp_pattern *pat)
+{
+ tlp_pattern *node = pat, *temp;
+
+ while (node != NULL)
+ {
+ temp = node->next;
+ tlp_pattern_free(node);
+ node = temp;
+ }
+
+ pat = NULL;
+}
+
+/* }}} */
+
+/* remaining stuff used in tlp_src processing {{{ */
+
+tlp_result tlp_str_get_word(tlp_str str, tlp_str word, int max)
+{
+ int i, j;
+
+ /* skip all leading spaces */
+ for (i = 0; tlp_char_is_space(str[i]); i++)
+ ;
+
+ j = 0;
+ for (; ! tlp_char_is_eol(str[i]); i++)
+ {
+ if (str[i] != TLP_CHAR('-') &&
+ ! tlp_char_in_word(str[i]))
+ return TLP_FAILED;
+
+ if (j >= max)
+ return TLP_FAILED;
+
+ word[j++] = str[i];
+ }
+
+ if (j == 0)
+ return TLP_FAILED;
+
+ word[j] = TLP_CHAR_END;
+ return TLP_OK;
+}
+
+/* }}} */
+
+/* vim:set ts=4 sts=4 sw=4 expandtab foldmethod=marker: */
diff --git a/Master/tlpkg/dev/lib/C/tlp-src.h b/Master/tlpkg/dev/lib/C/tlp-src.h
new file mode 100644
index 00000000000..cf04de32dc7
--- /dev/null
+++ b/Master/tlpkg/dev/lib/C/tlp-src.h
@@ -0,0 +1,75 @@
+/* tlp-src.h - header for using tlpsrc files
+ *
+ * Copyright 2007 Jjgod Jiang
+ * Derived from the Perl implementation by Norbert Preining
+ *
+ * This file is licensed under the GNU General Public License version 2
+ * or any later version. */
+
+#ifndef __TLP_SRC__
+#define __TLP_SRC__
+
+#include <tlp-utils.h>
+
+enum tlp_src_limits {
+ TLP_SRC_LINE_MAX = 1024,
+ TLP_SRC_NAME_MAX = 256,
+};
+
+enum tlp_pattern_type {
+ TLP_PATTERN_T, TLP_PATTERN_F,
+ TLP_PATTERN_D, TLP_PATTERN_R
+};
+
+typedef struct tlp_pattern {
+ enum tlp_pattern_type type;
+
+ union {
+ tlp_str_list *t;
+ tlp_str f;
+ tlp_str d;
+ tlp_str r;
+ } u;
+
+ struct tlp_pattern *next;
+} tlp_pattern;
+
+typedef struct tlp_src {
+ /* name of the package */
+ tlp_str name;
+ /* Collection Scheme TLCore Package Documentation */
+ tlp_str category;
+ /* short one line desc */
+ tlp_str shortdesc;
+ /* longer multiline dscription */
+ tlp_str longdesc;
+ /* name of the respective Catalogue entry */
+ tlp_str catalogue;
+
+ tlp_pattern *runpatterns;
+ tlp_pattern *srcpatterns;
+ tlp_pattern *docpatterns;
+ tlp_pattern *binpatterns;
+
+ tlp_execute *executes;
+ tlp_depend *depends;
+} tlp_src;
+
+tlp_src *tlp_src_new(tlp_str name,
+ tlp_str category,
+ tlp_str shortdesc,
+ tlp_str longdesc,
+ tlp_str catalogue,
+ tlp_pattern *runpatterns,
+ tlp_pattern *srcpatterns,
+ tlp_pattern *docpatterns,
+ tlp_pattern *binpatterns,
+ tlp_execute *executes,
+ tlp_depend *depends);
+
+void tlp_src_free(tlp_src *src);
+void tlp_patterns_free(tlp_pattern *pat);
+void tlp_pattern_free(tlp_pattern *pat);
+
+#endif
+
diff --git a/Master/tlpkg/dev/lib/C/tlp-utils.c b/Master/tlpkg/dev/lib/C/tlp-utils.c
new file mode 100644
index 00000000000..864b7a0597c
--- /dev/null
+++ b/Master/tlpkg/dev/lib/C/tlp-utils.c
@@ -0,0 +1,182 @@
+/* tlp-utils.h - module for general tlp utilities
+ *
+ * Copyright 2007 Jjgod Jiang
+ * Derived from the Perl implementation by Norbert Preining
+ *
+ * This file is licensed under the GNU General Public License version 2
+ * or any later version. */
+
+#include <tlp-utils.h>
+#include <string.h>
+#include <stdio.h>
+#include <ctype.h>
+
+tlp_str tlp_str_from_bytes(const char *bytes)
+{
+ return (tlp_str) strdup(bytes);
+}
+
+tlp_str tlp_str_copy(tlp_str str)
+{
+ if (str == NULL)
+ return NULL;
+
+ return strdup(str);
+}
+
+void tlp_str_free(tlp_str str)
+{
+ free(str);
+}
+
+tlp_str_list *tlp_str_list_new()
+{
+ tlp_str_list *list = tlp_new(tlp_str_list);
+ if (list == NULL)
+ return NULL;
+
+ list->head = list->tail = NULL;
+ return list;
+}
+
+tlp_str_list *tlp_str_list_from_array(const char *array[])
+{
+ tlp_str_list *list = tlp_str_list_new();
+ int i, size;
+
+ if (list == NULL)
+ return NULL;
+
+ size = sizeof(array) / sizeof(array[0]);
+ for (i = 0; i < size; i++)
+ if (tlp_str_list_append_bytes(list, array[i]) != TLP_OK)
+ {
+ tlp_str_list_free(list);
+ return NULL;
+ }
+
+ return list;
+}
+
+tlp_result tlp_str_list_append(tlp_str_list *list, tlp_str str)
+{
+ tlp_str_list_node *node = tlp_new(tlp_str_list_node);
+
+ if (node == NULL)
+ return TLP_FAILED;
+
+ node->str = str;
+ node->next = NULL;
+
+ if (list->head == NULL)
+ list->head = node;
+
+ if (list->tail == NULL)
+ list->tail = node;
+
+ else
+ {
+ list->tail->next = node;
+ list->tail = node;
+ }
+
+ return TLP_OK;
+}
+
+tlp_result tlp_str_list_append_bytes(tlp_str_list *list, const char *bytes)
+{
+ tlp_str str = tlp_str_from_bytes(bytes);
+ if (str == NULL)
+ return TLP_FAILED;
+
+ return tlp_str_list_append(list, str);
+}
+
+void tlp_str_list_free(tlp_str_list *list)
+{
+ tlp_str_list_node *node = list->head, *temp;
+
+ while (node != NULL)
+ {
+ temp = node->next;
+
+ tlp_str_free(node->str);
+ free(node);
+
+ node = temp;
+ }
+
+ free(list);
+ list = NULL;
+}
+
+tlp_str tlp_fgets(tlp_str s, int n, FILE *fp)
+{
+ return (tlp_str) fgets((char *) s, n, fp);
+}
+
+void tlp_str_output(FILE *fp, tlp_str str)
+{
+ fprintf(stderr, "%s", (char *) str);
+}
+
+size_t tlp_str_len(tlp_str str)
+{
+ return strlen((const char *) str);
+}
+
+int tlp_str_start_with(tlp_str str, tlp_str prefix)
+{
+ int len = tlp_str_len(prefix);
+
+ if (tlp_str_len(str) < len)
+ return 0;
+
+ return strncmp((const char *) str,
+ (const char *) prefix, len);
+}
+
+int tlp_str_start_with_bytes(tlp_str str, const char *prefix)
+{
+ tlp_str prefix_str;
+ int ret;
+
+ prefix_str = tlp_str_from_bytes(prefix);
+ if (prefix_str == NULL)
+ return 0;
+
+ ret = tlp_str_start_with(str, prefix_str);
+
+ tlp_str_free(prefix_str);
+ return ret;
+}
+
+int tlp_char_is_space(tlp_char ch)
+{
+ return isspace((int) ch);
+}
+
+int tlp_char_is_eol(tlp_char ch)
+{
+ return (ch == '\r' || ch == '\n');
+}
+
+int tlp_char_in_word(tlp_char ch)
+{
+ return (isalnum(ch) || ch == '_');
+}
+
+void tlp_error_msg(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+}
+
+void tlp_error_str(tlp_str str)
+{
+ tlp_str_output(stderr, str);
+}
+
diff --git a/Master/tlpkg/dev/lib/C/tlp-utils.h b/Master/tlpkg/dev/lib/C/tlp-utils.h
new file mode 100644
index 00000000000..a2332bf8d4f
--- /dev/null
+++ b/Master/tlpkg/dev/lib/C/tlp-utils.h
@@ -0,0 +1,73 @@
+/* tlp-utils.h - header for general tlp definitions
+ *
+ * Copyright 2007 Jjgod Jiang
+ * Derived from the Perl implementation by Norbert Preining
+ *
+ * This file is licensed under the GNU General Public License version 2
+ * or any later version. */
+
+#ifndef __TLP_UTILS__
+#define __TLP_UTILS__
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+typedef char tlp_char;
+typedef tlp_char *tlp_str;
+
+#define TLP_CHAR(x) ((tlp_char) x)
+
+enum tlp_special_chars {
+ TLP_CHAR_COMMENT = TLP_CHAR('#'),
+ TLP_CHAR_END = TLP_CHAR('\0'),
+};
+
+typedef struct tlp_str_list_node {
+ tlp_str str;
+ struct tlp_str_list_node *next;
+} tlp_str_list_node;
+
+typedef struct tlp_str_list {
+ struct tlp_str_list_node *head;
+ struct tlp_str_list_node *tail;
+} tlp_str_list;
+
+typedef tlp_str_list tlp_execute;
+typedef tlp_str_list tlp_depend;
+
+#define tlp_executes_free(x) tlp_str_list_free(x)
+#define tlp_depends_free(x) tlp_str_list_free(x)
+
+typedef enum tlp_result {
+ TLP_FAILED = 0, TLP_OK = 1
+} tlp_result;
+
+#define tlp_new(x) ((x *) malloc(sizeof(x)))
+
+tlp_str tlp_str_from_bytes(const char *bytes);
+tlp_str tlp_str_copy(tlp_str str);
+void tlp_str_free(tlp_str str);
+
+
+tlp_str_list *tlp_str_list_new();
+tlp_str_list *tlp_str_list_from_array(const char *array[]);
+tlp_result tlp_str_list_append(tlp_str_list *list, tlp_str str);
+tlp_result tlp_str_list_append_bytes(tlp_str_list *list, const char *bytes);
+void tlp_str_list_free(tlp_str_list *list);
+
+tlp_str tlp_fgets(tlp_str s, int n, FILE *fp);
+size_t tlp_str_len(tlp_str str);
+void tlp_str_output(FILE *fp, tlp_str str);
+int tlp_str_start_with(tlp_str str, tlp_str prefix);
+int tlp_str_start_with_bytes(tlp_str str, const char *prefix);
+
+int tlp_char_is_space(tlp_char ch);
+int tlp_char_is_eol(tlp_char ch);
+int tlp_char_in_word(tlp_char ch);
+
+void tlp_error_msg(const char *fmt, ...);
+void tlp_error_str(tlp_str str);
+
+#endif
+