astlicense.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2012 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 *
* (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
*
* generate a license comment -- see proto(1)
*
* NOTE: coded for minimal library dependence
* not so for the legal department
*/
#ifndef _PPLIB_H
#include <ast.h>
#include <time.h>
#endif
#ifndef O_cloexec
#ifdef O_CLOEXEC
#define O_cloexec 0
#else
#define O_cloexec 0
#endif
#endif
#define NONE 0
#define INLINE 1
#define TEST 2
#define VERBOSE 3
#define USAGE 4
#define OPEN 5
#define CPL 6
#define EPL 7
#define BSD 8
#define ZLIB 9
#define MIT 10
#define GPL 11
#define SPECIAL 12
#define NONEXCLUSIVE 13
#define NONCOMMERCIAL 14
#define PROPRIETARY 15
#define AUTHOR 0
#define CLASS 1
#define COMPANY 2
#define COMPONENT 3
#define CONTRIBUTOR 4
#define CORPORATION 5
#define DOMAIN 6
#define ID 7
#define INCORPORATION 8
#define LICENSE 9
#define LOCATION 10
#define NAME 11
#define NOTICE 12
#define ORGANIZATION 13
#define PACKAGE 14
#define PARENT 15
#define QUERY 16
#define SINCE 17
#define SOURCE 18
#define START 19
#define STYLE 20
#define URL 21
#define URLMD5 22
#define VERSION 23
#define IDS 64
#define COMDATA 70
#ifndef NiL
#define NiL ((char*)0)
#endif
typedef struct Buffer_s
{
char* buf;
char* nxt;
char* end;
int siz;
} Buffer_t;
typedef struct Item_s
{
char* data;
int size;
int quote;
} Item_t;
typedef struct Id_s
{
} Id_t;
/*
* NOTE: key[] element order must match the corresponding macro
*/
#define KEY(s) {s,sizeof(s)-1,0}
{
KEY("author"),
KEY("class"),
KEY("company"),
KEY("component"),
KEY("contributor"),
KEY("corporation"),
KEY("domain"),
KEY("id"),
KEY("incorporation"),
KEY("license"),
KEY("location"),
KEY("name"),
KEY("notice"),
KEY("organization"),
KEY("package"),
KEY("parent"),
KEY("query"),
KEY("since"),
KEY("source"),
KEY("start"),
KEY("type"),
KEY("url"),
KEY("urlmd5"),
KEY("version"),
{0}
};
#define LIC(s,c) {s,sizeof(s)-1,c}
{
{0}
};
typedef struct Notice_s
{
int test;
int type;
int verbose;
int ids;
char cc[3];
} Notice_t;
/*
* return index given <name,size>
*/
static int
{
register int c;
register int i;
c = name[0];
return i;
return -1;
}
/*
* copy s of size n to b
* n<0 means 0 terminated string
*/
static void
{
if (n < 0)
n = strlen(s);
while (n--)
PUT(b, *s++);
}
/*
* center and copy comment line s to p
* if s==0 then
* n>0 first frame line
* n=0 blank line
* n<0 last frame line
* if u>0 then s converted to upper case
* if u<0 then s is left justified
*/
static void
{
register int i;
register int m;
register int x;
int cc;
if (!s)
{
if (n)
{
for (i = 0; i < COMDATA; i++)
}
else
s = "";
}
if (s)
{
if (n > COMDATA)
n = COMDATA;
if ((x = COMDATA - m - n) < 0)
n--;
while (m-- > 0)
PUT(b, ' ');
while (n-- > 0)
{
i = *s++;
if (u > 0 && i >= 'a' && i <= 'z')
i = i - 'a' + 'A';
PUT(b, i);
}
while (x-- > 0)
PUT(b, ' ');
}
PUT(b, '\n');
}
/*
* expand simple ${...}
*/
static void
{
register char* t;
register char* e;
register int q;
register char* x;
register char* z;
register int c;
int m;
int i;
int k;
{
i = 0;
while (t < e)
{
if (*t == '$' && t < (e + 2) && *(t + 1) == '{')
{
k = m = 0;
x = t += 2;
while (t < e && (c = *t++) != '}')
if (c == '.')
x = t;
else if (c == '-')
{
k = 1;
break;
}
else if (c == '/')
{
m = 1;
break;
}
{
while (x < z)
{
c = *x++;
if (!m || c >= '0' && c <= '9')
PUT(b, c);
}
}
else if (k)
{
k = 0;
i++;
}
if (k || m)
{
k = 1;
while (t < e)
if ((c = *t++) == '{')
k++;
else if (c == '}' && !--k)
break;
}
}
else if (q > 0 && *t == '\\' && (*(t + 1) == q || *(t + 1) == '\\'))
t++;
else if (*t == '}' && i)
{
t++;
i--;
}
else
PUT(b, *t++);
}
}
}
/*
* generate a copright notice
*/
static void
{
register char* x;
register char* t;
{
}
{
}
t = x;
{
PUT(b, '-');
}
copy(b, t, 4);
{
PUT(b, ' ');
}
{
PUT(b, ' ');
{
PUT(b, ' ');
}
}
{
PUT(b, ' ');
}
}
typedef struct Stack_s
{
char* info;
char* file;
int line;
int size;
} Stack_t;
static int
{
char* s;
char* t;
int i;
int n;
char path[1024];
if (size <= 8)
{
return -1;
}
{
n = s - parent + 1;
{
}
}
{
/* this hack viewpath lookup works for default package setups */
for (s = path; *s; s++)
if (s[0] == '/' && s[1] == 'a' && s[2] == 'r' && s[3] == 'c' && s[4] == 'h' && s[5] == '/')
{
t = s;
for (s += 6; *s && *s != '/'; s++);
while (*t++ = *s++);
}
if (i < 0)
{
return -1;
}
}
close(i);
if (n < 0)
{
return -1;
}
info[n++] = 0;
return 0;
}
/*
* read the license file and generate a comment in p, length size
* license length in p returned, -1 on error
* -1 return places 0 terminated error string in p
*/
int
{
register char* s;
register char* v;
register char* x;
register int c;
int i;
int h;
int k;
int n;
int q;
int contributor;
int first;
int level;
int quote;
char* data;
level = 0;
level = -1;
if (options)
{
level++;
}
{
return -1;
}
if (level < 0)
return 0;
for (i = 0; i < ITEMS; i++)
contributor = i = k = 0;
for (;;)
{
first = 1;
while (c = *s)
{
while (c == ' ' || c == '\t' || c == '\n' && ++input[level].line || c == '\r' || c == ',' || c == ';' || c == ')')
c = *++s;
if (!c)
break;
if (c == '#')
{
while (*++s && *s != '\n');
if (*s)
s++;
continue;
}
if (c == '.')
{
while ((c = *++s) && (c == ' ' || c == '\t'));
file = s;
while (c && c != ' ' && c != '\t' && c != '\r' && c != '\n')
c = *++s;
*s = 0;
while (c && c != '\n')
c = *++s;
if (*file)
{
if (++level >= (sizeof(input) / sizeof(input[0])) || push(&input[level], file, input[level-1].file, data, &info[sizeof(info)] - data, &buf))
return -1;
}
continue;
}
if (c == '\n')
{
s++;
continue;
}
if (c == '[')
c = *++s;
x = s;
n = 0;
while (c && c != '+' && c != '=' && c != ']' && c != ')' && c != ',' && c != ' ' && c != '\t' && c != '\n' && c != '\r')
c = *++s;
n = s - x;
if (c == '+' || c == ']')
c = *++s;
quote = 0;
if (c == '=' || first)
{
if (c == '=')
{
q = ((c = *++s) == '"' || c == '\'') ? *s++ : 0;
if (c == '(')
{
s++;
if (h == LICENSE)
contributor = 0;
else if (h == CONTRIBUTOR)
contributor = 1;
else
{
q = 1;
i = 0;
for (;;)
{
switch (*s++)
{
case 0:
s--;
break;
case '(':
if (!i)
q++;
continue;
case ')':
if (!i && !--q)
break;
continue;
case '"':
case '\'':
if (!i)
i = *(s - 1);
else if (i == *(s - 1))
i = 0;
continue;
case '\\':
if (*s == i && i == '"')
i++;
continue;
case '\n':
continue;
default:
continue;
}
break;
}
}
continue;
}
v = s;
while ((c = *s) && (q == '"' && (c == '\\' && (*(s + 1) == '"' || *(s + 1) == '\\') && s++ && (quote = q)) || q && c != q || !q && c != ' ' && c != '\t' && c != '\n' && c != '\r' && c != ',' && c != ';'))
{
if (c == '\n')
s++;
}
}
else
{
h = STYLE;
v = x;
}
if (c == '\n')
if (contributor)
{
break;
if (i < IDS)
{
}
}
else if (h == QUERY)
{
if ((s - v) == 3 && v[0] == 'a' && v[1] == 'l' && v[2] == 'l')
{
for (i = 0; i < ITEMS; i++)
{
for (h = 0;; h++)
{
h = 0;
break;
}
break;
if (h)
if (h)
}
}
else
{
{
}
else
}
}
else
{
if (h == STYLE)
{
case NONE:
return 0;
case TEST:
h = -1;
break;
case VERBOSE:
h = -1;
break;
case USAGE:
h = -1;
break;
case -1:
c = SPECIAL;
/*FALLTHROUGH*/
default:
h = -1;
break;
}
if (h >= 0)
{
k = 1;
}
}
}
else
{
{
*--x = 0;
do *--x = ("0123456789")[n % 10]; while (n /= 10);
}
return -1;
}
if (*s)
s++;
first = 0;
}
if (!level--)
break;
}
if (!k)
return 0;
return 0;
{
{
}
{
}
{
else
{
}
{
{
}
{
{
}
}
}
{
{
}
}
else
}
{
{
{
}
}
{
{
}
}
else
i = -1;
{
if (i >= 0)
{
}
}
if (i >= 0)
if (n >= COMLONG)
else
}
{
}
{
if (notice.item[i = PARENT].data || notice.item[i = CORPORATION].data || notice.item[i = COMPANY].data)
else
}
{
}
{
}
else
{
{
if (notice.item[i = PARENT].data || notice.item[i = CORPORATION].data || notice.item[i = COMPANY].data)
{
}
else
i = -1;
{
if (i >= 0)
{
}
{
}
{
}
}
else
{
if (i >= 0)
{
}
}
}
{
}
{
}
{
}
{
{
if ((notice.item[PARENT].size + (notice.item[CORPORATION].data ? (notice.item[CORPORATION].size + notice.item[INCORPORATION].size) : notice.item[COMPANY].size)) >= (COMLONG - 6))
else
{
}
{
{
}
}
}
{
}
}
{
}
}
{
if (*v == '\n')
v++;
do
{
{
h = 0;
}
else
h = -1;
} while (v++ < x);
}
{
{
}
{
}
}
}
{
q = (x - v) == 1 && (*v == '*' || *v == '-');
for (;;)
{
if (!q)
{
while (v < x && (*v == ' ' || *v == '\t' || *v == '\r' || *v == '\n' || *v == ',' || *v == '+'))
v++;
if (v >= x)
break;
while (v < x && *v != ',' && *v != '+' && *v++ != '>');
}
h = 0;
if (q || item.size == notice.id[i].name.size && !strncmp(item.data, notice.id[i].name.data, item.size))
{
h = 1;
{
}
else
{
if (k < 0)
{
}
k = 1;
}
if (!q)
break;
}
if (q)
break;
if (!h)
{
{
}
else
{
if (k < 0)
{
}
k = 1;
}
}
}
if (k > 0)
}
{
{
}
}
else
}