#include <stdio.h>
#include <libintl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <sys/systeminfo.h>
#include <strings.h>
#include <time.h>
#include <ctype.h>
#include <zone.h>
#include "err.h"
#include "lut.h"
#include "fn.h"
#include "kw.h"
static void kw_printer(const char *lhs, void *rhs, void *arg);
#define MAXDIGITS 100
static struct lut *Keywords;
extern time_t Now;
void
kw_init(struct fn *fnp, struct fn *nfnp)
{
static char *fullpath;
static char *nfullpath;
static char *splitpath;
static char secs[MAXDIGITS];
static struct utsname un;
static char platform[SYS_NMLN];
static char isa[SYS_NMLN];
static char domain[256];
static char *home;
static char *user;
static char *logname;
static char zonename[ZONENAME_MAX];
static zoneid_t zoneid;
static int initialized;
char *ptr;
if (fullpath)
FREE(fullpath);
fullpath = STRDUP(fn_s(fnp));
Keywords = lut_add(Keywords, "file", fullpath);
if (nfullpath)
FREE(nfullpath);
if (nfnp == NULL) {
nfullpath = NULL;
Keywords = lut_add(Keywords, "nfile", "");
} else {
nfullpath = STRDUP(fn_s(nfnp));
Keywords = lut_add(Keywords, "nfile", nfullpath);
}
if (splitpath)
FREE(splitpath);
splitpath = STRDUP(fn_s(fnp));
if ((ptr = strrchr(splitpath, '/')) == NULL) {
Keywords = lut_add(Keywords, "basename", splitpath);
Keywords = lut_add(Keywords, "dirname", ".");
} else {
*ptr++ = '\0';
Keywords = lut_add(Keywords, "basename", ptr);
Keywords = lut_add(Keywords, "dirname", splitpath);
}
if (initialized)
return;
(void) snprintf(secs, MAXDIGITS, "%d", (int)Now);
Keywords = lut_add(Keywords, "secs", secs);
if (uname(&un) < 0)
err(EF_SYS, "uname");
Keywords = lut_add(Keywords, "nodename", un.nodename);
Keywords = lut_add(Keywords, "release", un.release);
Keywords = lut_add(Keywords, "machine", un.machine);
if (sysinfo(SI_ARCHITECTURE, isa, sizeof (isa)) == -1)
err(EF_WARN|EF_SYS, "sysinfo(SI_ARCHITECTURE) failed.");
else
Keywords = lut_add(Keywords, "isa", isa);
if (sysinfo(SI_PLATFORM, platform, sizeof (platform)) == -1)
err(EF_WARN|EF_SYS, "sysinfo(SI_PLATFORM) failed.");
else
Keywords = lut_add(Keywords, "platform", platform);
if (sysinfo(SI_SRPC_DOMAIN, domain, sizeof (domain)) == -1)
err(EF_WARN|EF_SYS, "sysinfo(SI_SRPC_DOMAIN) failed.");
else
Keywords = lut_add(Keywords, "domain", domain);
if ((home = getenv("HOME")) != NULL)
Keywords = lut_add(Keywords, "home", STRDUP(home));
if ((user = getenv("USER")) != NULL)
Keywords = lut_add(Keywords, "user", STRDUP(user));
if ((logname = getenv("LOGNAME")) != NULL)
Keywords = lut_add(Keywords, "logname", STRDUP(logname));
zoneid = getzoneid();
if ((getzonenamebyid(zoneid, zonename, sizeof (zonename))) == -1)
err(EF_WARN|EF_SYS, "getzonenamebyid() failed.");
else
Keywords = lut_add(Keywords, "zonename", STRDUP(zonename));
initialized = 1;
}
static void
kw_printer(const char *lhs, void *rhs, void *arg)
{
FILE *stream = (FILE *)arg;
(void) fprintf(stream, "%20.20s %s\n", lhs, (char *)rhs);
}
void
kw_print(FILE *stream)
{
lut_walk(Keywords, kw_printer, stream);
}
boolean_t
kw_expand(struct fn *src, struct fn *dst, int n, boolean_t gz)
{
int c;
char buf[MAXDIGITS];
boolean_t hasn = B_FALSE;
struct fn *kw = fn_new(NULL);
char *ptr;
struct tm *gmt_tm = localtime(&Now);
while ((c = fn_getc(src)) != '\0')
switch (c) {
case '.':
case '(':
case ')':
case '^':
case '+':
case '{':
case '}':
if (n < 0)
fn_putc(dst, '\\');
fn_putc(dst, c);
break;
case '?':
if (n < 0)
fn_putc(dst, '.');
break;
case '*':
if (n < 0)
fn_putc(dst, '.');
fn_putc(dst, '*');
break;
case '$':
switch (c = fn_getc(src)) {
case '$':
if (n < 0)
fn_putc(dst, '\\');
fn_putc(dst, '$');
break;
case '#':
break;
case 'n':
case 'N':
if (c == 'N' || !islower(fn_peekc(src))) {
hasn = B_TRUE;
if (n < 0)
fn_puts(dst, "([0-9]+)$0");
else {
(void) snprintf(buf,
MAXDIGITS, "%d",
(c == 'n') ? n : n + 1);
fn_puts(dst, buf);
}
break;
}
default:
fn_renew(kw, NULL);
fn_putc(kw, c);
while (islower(fn_peekc(src)))
fn_putc(kw, fn_getc(src));
if ((ptr = (char *)lut_lookup(Keywords,
fn_s(kw))) == NULL) {
if (n < 0)
fn_putc(dst, '\\');
fn_putc(dst, '$');
fn_putfn(dst, kw);
} else
fn_puts(dst, ptr);
}
break;
case '%':
if (n < 0) {
if ((c = fn_getc(src)) == 'E' || c == 'O') {
c = fn_getc(src);
fn_puts(dst, ".*");
} else
switch (c) {
case 'd':
case 'g':
case 'G':
case 'H':
case 'I':
case 'j':
case 'm':
case 'M':
case 'S':
case 'u':
case 'U':
case 'V':
case 'w':
case 'W':
case 'y':
case 'Y':
fn_puts(dst, "[0-9]+");
break;
case 'e':
case 'k':
case 'l':
fn_puts(dst, " *[0-9]+");
break;
case 'D':
fn_puts(dst,
"[0-9]+/[0-9]+/[0-9]+");
break;
case 'R':
fn_puts(dst, "[0-9]+:[0-9]+");
break;
case 'T':
fn_puts(dst,
"[0-9]+:[0-9]+:[0-9]+");
break;
default:
fn_puts(dst, ".*");
}
} else {
char tbuf[4];
tbuf[0] = '%';
tbuf[1] = fn_getc(src);
if (tbuf[1] == 'E' || tbuf[1] == 'O') {
tbuf[2] = fn_getc(src);
tbuf[3] = '\0';
} else
tbuf[2] = '\0';
if (strftime(buf, MAXDIGITS, tbuf, gmt_tm) == 0)
fn_puts(dst, tbuf);
else
fn_puts(dst, buf);
}
break;
default:
fn_putc(dst, c);
}
if (gz) {
if (n < 0)
fn_puts(dst, "(\\.gz){0,1}");
else
fn_puts(dst, ".gz");
}
fn_free(kw);
return (hasn);
}
#ifdef TESTMODULE
time_t Now;
int
main(int argc, char *argv[])
{
int i;
struct fn *src = fn_new(NULL);
struct fn *dst = fn_new(NULL);
err_init(argv[0]);
setbuf(stdout, NULL);
Now = time(0);
if (argc < 2)
err(0, "first arg must be fname");
kw_init(fn_new(argv[1]), NULL);
kw_print(stdout);
for (i = 2; i < argc; i++) {
int n;
for (n = -1; n < 2; n++) {
fn_renew(src, argv[i]);
fn_renew(dst, NULL);
printf("expand<%s> n %d hasn %d ",
argv[i], n, kw_expand(src, dst, n, B_FALSE));
printf("result <%s>\n", fn_s(dst));
}
}
err_done(0);
return (0);
}
#endif