2002-07-03 07:54:35 +08:00
|
|
|
/*@-boundsread@*/
|
2000-08-28 02:34:02 +08:00
|
|
|
/** \ingroup rpmrc rpmio
|
|
|
|
* \file rpmio/macro.c
|
|
|
|
*/
|
|
|
|
|
2000-12-13 04:03:45 +08:00
|
|
|
#include "system.h"
|
1999-03-28 08:47:40 +08:00
|
|
|
#include <stdarg.h>
|
1998-01-13 05:31:29 +08:00
|
|
|
|
1999-10-28 07:18:10 +08:00
|
|
|
#if !defined(isblank)
|
1998-07-09 01:50:48 +08:00
|
|
|
#define isblank(_c) ((_c) == ' ' || (_c) == '\t')
|
1999-10-28 07:18:10 +08:00
|
|
|
#endif
|
1999-08-17 02:57:37 +08:00
|
|
|
#define iseol(_c) ((_c) == '\n' || (_c) == '\r')
|
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
#define STREQ(_t, _f, _fn) ((_fn) == (sizeof(_t)-1) && !strncmp((_t), (_f), (_fn)))
|
1997-03-31 22:13:21 +08:00
|
|
|
|
1998-01-13 05:31:29 +08:00
|
|
|
#ifdef DEBUG_MACROS
|
1999-07-03 05:15:25 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-03-31 22:13:21 +08:00
|
|
|
#define rpmError fprintf
|
|
|
|
#define RPMERR_BADSPEC stderr
|
1999-04-21 03:17:23 +08:00
|
|
|
#undef _
|
1998-09-28 06:03:52 +08:00
|
|
|
#define _(x) x
|
1999-11-13 01:20:49 +08:00
|
|
|
|
|
|
|
#define vmefail() (exit(1), NULL)
|
|
|
|
#define urlPath(_xr, _r) *(_r) = (_xr)
|
|
|
|
|
|
|
|
typedef FILE * FD_t;
|
1999-12-13 05:14:05 +08:00
|
|
|
#define Fopen(_path, _fmode) fopen(_path, "r");
|
1999-11-13 01:20:49 +08:00
|
|
|
#define Ferror ferror
|
|
|
|
#define Fstrerror(_fd) strerror(errno)
|
|
|
|
#define Fread fread
|
|
|
|
#define Fclose fclose
|
|
|
|
|
2000-11-01 00:18:34 +08:00
|
|
|
#define fdGetFILE(_fd) (_fd)
|
|
|
|
|
1997-03-31 22:13:21 +08:00
|
|
|
#else
|
1999-11-13 01:20:49 +08:00
|
|
|
|
2000-06-10 02:57:23 +08:00
|
|
|
#include <rpmio_internal.h>
|
2000-06-10 05:24:37 +08:00
|
|
|
#include <rpmmessages.h>
|
|
|
|
#include <rpmerr.h>
|
2004-03-20 04:08:20 +08:00
|
|
|
#include <rpmlua.h>
|
1999-11-13 01:20:49 +08:00
|
|
|
|
1997-03-31 22:13:21 +08:00
|
|
|
#endif
|
|
|
|
|
1999-07-14 05:37:57 +08:00
|
|
|
#include <rpmmacro.h>
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2000-12-13 04:03:45 +08:00
|
|
|
#include "debug.h"
|
|
|
|
|
2002-09-17 23:21:03 +08:00
|
|
|
#if defined(__LCLINT__)
|
|
|
|
/*@-exportheader@*/
|
|
|
|
extern const unsigned short int **__ctype_b_loc (void) /*@*/;
|
|
|
|
/*@=exportheader@*/
|
|
|
|
#endif
|
|
|
|
|
2000-11-01 00:18:34 +08:00
|
|
|
/*@access FD_t@*/ /* XXX compared with NULL */
|
|
|
|
/*@access MacroContext@*/
|
|
|
|
/*@access MacroEntry@*/
|
2004-03-23 15:18:55 +08:00
|
|
|
/*@access rpmlua @*/
|
2000-11-01 00:18:34 +08:00
|
|
|
|
2001-10-15 11:22:10 +08:00
|
|
|
static struct MacroContext_s rpmGlobalMacroContext_s;
|
|
|
|
/*@-compmempass@*/
|
|
|
|
MacroContext rpmGlobalMacroContext = &rpmGlobalMacroContext_s;
|
|
|
|
/*@=compmempass@*/
|
|
|
|
|
|
|
|
static struct MacroContext_s rpmCLIMacroContext_s;
|
|
|
|
/*@-compmempass@*/
|
|
|
|
MacroContext rpmCLIMacroContext = &rpmCLIMacroContext_s;
|
|
|
|
/*@=compmempass@*/
|
1999-04-02 06:26:44 +08:00
|
|
|
|
2000-09-02 05:15:40 +08:00
|
|
|
/**
|
2000-09-30 03:50:29 +08:00
|
|
|
* Macro expansion state.
|
2000-09-02 05:15:40 +08:00
|
|
|
*/
|
2001-06-06 03:26:22 +08:00
|
|
|
typedef /*@abstract@*/ struct MacroBuf_s {
|
2002-06-22 02:00:50 +08:00
|
|
|
/*@kept@*/ /*@exposed@*/
|
|
|
|
const char * s; /*!< Text to expand. */
|
|
|
|
/*@shared@*/
|
|
|
|
char * t; /*!< Expansion buffer. */
|
2001-06-06 03:26:22 +08:00
|
|
|
size_t nb; /*!< No. bytes remaining in expansion buffer. */
|
|
|
|
int depth; /*!< Current expansion depth. */
|
|
|
|
int macro_trace; /*!< Pre-print macro to expand? */
|
|
|
|
int expand_trace; /*!< Post-print macro expansion? */
|
2002-06-22 02:00:50 +08:00
|
|
|
/*@kept@*/ /*@exposed@*/ /*@null@*/
|
|
|
|
void * spec; /*!< (future) %file expansion info?. */
|
|
|
|
/*@kept@*/ /*@exposed@*/
|
|
|
|
MacroContext mc;
|
2001-06-06 03:26:22 +08:00
|
|
|
} * MacroBuf;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
|
|
|
#define SAVECHAR(_mb, _c) { *(_mb)->t = (_c), (_mb)->t++, (_mb)->nb--; }
|
1997-03-31 22:13:21 +08:00
|
|
|
|
2001-06-04 21:55:58 +08:00
|
|
|
/*@-exportlocal -exportheadervar@*/
|
2001-10-15 11:22:10 +08:00
|
|
|
|
2004-03-02 09:31:01 +08:00
|
|
|
#define _MAX_MACRO_DEPTH 16
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@unchecked@*/
|
2004-03-02 09:31:01 +08:00
|
|
|
int max_macro_depth = _MAX_MACRO_DEPTH;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2004-03-02 09:31:01 +08:00
|
|
|
#define _PRINT_MACRO_TRACE 0
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@unchecked@*/
|
2004-03-02 09:31:01 +08:00
|
|
|
int print_macro_trace = _PRINT_MACRO_TRACE;
|
|
|
|
|
|
|
|
#define _PRINT_EXPAND_TRACE 0
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@unchecked@*/
|
2004-03-02 09:31:01 +08:00
|
|
|
int print_expand_trace = _PRINT_EXPAND_TRACE;
|
2001-06-04 21:55:58 +08:00
|
|
|
/*@=exportlocal =exportheadervar@*/
|
1997-03-31 22:13:21 +08:00
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
#define MACRO_CHUNK_SIZE 16
|
1997-03-31 22:13:21 +08:00
|
|
|
|
2001-10-18 00:43:36 +08:00
|
|
|
/* forward ref */
|
|
|
|
static int expandMacro(MacroBuf mb)
|
|
|
|
/*@globals rpmGlobalMacroContext,
|
2003-05-09 04:39:29 +08:00
|
|
|
print_macro_trace, print_expand_trace, h_errno, fileSystem @*/
|
2001-10-18 00:43:36 +08:00
|
|
|
/*@modifies mb, rpmGlobalMacroContext,
|
2002-06-22 02:00:50 +08:00
|
|
|
print_macro_trace, print_expand_trace, fileSystem @*/;
|
2001-10-18 00:43:36 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
/**
|
|
|
|
* Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
|
2001-06-04 21:55:58 +08:00
|
|
|
* @param p memory to free
|
2001-05-04 05:00:18 +08:00
|
|
|
* @retval NULL always
|
|
|
|
*/
|
2001-06-04 21:55:58 +08:00
|
|
|
/*@unused@*/ static inline /*@null@*/ void *
|
2001-06-06 03:26:22 +08:00
|
|
|
_free(/*@only@*/ /*@null@*/ const void * p)
|
|
|
|
/*@modifies p@*/
|
2001-06-04 21:55:58 +08:00
|
|
|
{
|
|
|
|
if (p != NULL) free((void *)p);
|
2001-05-04 05:00:18 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
/* =============================================================== */
|
1997-03-31 22:13:21 +08:00
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Compare macro entries by name (qsort/bsearch).
|
|
|
|
* @param ap 1st macro entry
|
|
|
|
* @param bp 2nd macro entry
|
|
|
|
* @return result of comparison
|
|
|
|
*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static int
|
2001-06-06 03:26:22 +08:00
|
|
|
compareMacroName(const void * ap, const void * bp)
|
|
|
|
/*@*/
|
1997-03-31 22:13:21 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
MacroEntry ame = *((MacroEntry *)ap);
|
|
|
|
MacroEntry bme = *((MacroEntry *)bp);
|
|
|
|
|
|
|
|
if (ame == NULL && bme == NULL)
|
|
|
|
return 0;
|
|
|
|
if (ame == NULL)
|
|
|
|
return 1;
|
|
|
|
if (bme == NULL)
|
|
|
|
return -1;
|
|
|
|
return strcmp(ame->name, bme->name);
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
1997-03-31 22:13:21 +08:00
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Enlarge macro table.
|
|
|
|
* @param mc macro context
|
|
|
|
*/
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@-boundswrite@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static void
|
2001-05-04 05:00:18 +08:00
|
|
|
expandMacroTable(MacroContext mc)
|
2001-06-06 03:26:22 +08:00
|
|
|
/*@modifies mc @*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
if (mc->macroTable == NULL) {
|
|
|
|
mc->macrosAllocated = MACRO_CHUNK_SIZE;
|
|
|
|
mc->macroTable = (MacroEntry *)
|
|
|
|
xmalloc(sizeof(*(mc->macroTable)) * mc->macrosAllocated);
|
|
|
|
mc->firstFree = 0;
|
|
|
|
} else {
|
|
|
|
mc->macrosAllocated += MACRO_CHUNK_SIZE;
|
|
|
|
mc->macroTable = (MacroEntry *)
|
|
|
|
xrealloc(mc->macroTable, sizeof(*(mc->macroTable)) *
|
|
|
|
mc->macrosAllocated);
|
|
|
|
}
|
|
|
|
memset(&mc->macroTable[mc->firstFree], 0, MACRO_CHUNK_SIZE * sizeof(*(mc->macroTable)));
|
1997-03-31 22:13:21 +08:00
|
|
|
}
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@=boundswrite@*/
|
1997-03-31 22:13:21 +08:00
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Sort entries in macro table.
|
|
|
|
* @param mc macro context
|
|
|
|
*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static void
|
2001-05-04 05:00:18 +08:00
|
|
|
sortMacroTable(MacroContext mc)
|
2001-06-06 03:26:22 +08:00
|
|
|
/*@modifies mc @*/
|
1997-03-31 22:13:21 +08:00
|
|
|
{
|
2001-05-04 05:00:18 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (mc == NULL || mc->macroTable == NULL)
|
|
|
|
return;
|
1999-08-17 00:18:25 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
qsort(mc->macroTable, mc->firstFree, sizeof(*(mc->macroTable)),
|
1998-07-09 01:50:48 +08:00
|
|
|
compareMacroName);
|
1999-08-17 00:18:25 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
/* Empty pointers are now at end of table. Reset first free index. */
|
|
|
|
for (i = 0; i < mc->firstFree; i++) {
|
|
|
|
if (mc->macroTable[i] != NULL)
|
|
|
|
continue;
|
|
|
|
mc->firstFree = i;
|
|
|
|
break;
|
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
1997-03-31 22:13:21 +08:00
|
|
|
|
1998-12-02 07:28:26 +08:00
|
|
|
void
|
2001-05-04 05:00:18 +08:00
|
|
|
rpmDumpMacroTable(MacroContext mc, FILE * fp)
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2001-05-04 05:00:18 +08:00
|
|
|
int nempty = 0;
|
|
|
|
int nactive = 0;
|
1999-03-28 08:47:40 +08:00
|
|
|
|
2001-10-15 11:22:10 +08:00
|
|
|
if (mc == NULL) mc = rpmGlobalMacroContext;
|
2001-05-04 05:00:18 +08:00
|
|
|
if (fp == NULL) fp = stderr;
|
1997-03-31 22:13:21 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
fprintf(fp, "========================\n");
|
|
|
|
if (mc->macroTable != NULL) {
|
|
|
|
int i;
|
1998-07-09 01:50:48 +08:00
|
|
|
for (i = 0; i < mc->firstFree; i++) {
|
2001-05-04 05:00:18 +08:00
|
|
|
MacroEntry me;
|
|
|
|
if ((me = mc->macroTable[i]) == NULL) {
|
|
|
|
/* XXX this should never happen */
|
|
|
|
nempty++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fprintf(fp, "%3d%c %s", me->level,
|
1998-07-09 01:50:48 +08:00
|
|
|
(me->used > 0 ? '=' : ':'), me->name);
|
2001-05-04 05:00:18 +08:00
|
|
|
if (me->opts && *me->opts)
|
|
|
|
fprintf(fp, "(%s)", me->opts);
|
|
|
|
if (me->body && *me->body)
|
|
|
|
fprintf(fp, "\t%s", me->body);
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
nactive++;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2001-05-04 05:00:18 +08:00
|
|
|
}
|
|
|
|
fprintf(fp, _("======================== active %d empty %d\n"),
|
1998-07-09 01:50:48 +08:00
|
|
|
nactive, nempty);
|
|
|
|
}
|
1997-03-31 22:13:21 +08:00
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Find entry in macro table.
|
|
|
|
* @param mc macro context
|
|
|
|
* @param name macro name
|
2004-03-02 09:31:01 +08:00
|
|
|
* @param namelen no. of bytes
|
2000-09-30 03:50:29 +08:00
|
|
|
* @return address of slot in macro table with name (or NULL)
|
|
|
|
*/
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@-boundswrite@*/
|
2002-06-22 02:00:50 +08:00
|
|
|
/*@dependent@*/ /*@null@*/
|
|
|
|
static MacroEntry *
|
2001-06-06 03:26:22 +08:00
|
|
|
findEntry(MacroContext mc, const char * name, size_t namelen)
|
2002-06-22 02:00:50 +08:00
|
|
|
/*@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2001-05-04 05:00:18 +08:00
|
|
|
MacroEntry key, *ret;
|
|
|
|
struct MacroEntry_s keybuf;
|
|
|
|
char namebuf[1024];
|
|
|
|
|
2002-06-22 02:00:50 +08:00
|
|
|
/*@-globs@*/
|
2001-10-15 11:22:10 +08:00
|
|
|
if (mc == NULL) mc = rpmGlobalMacroContext;
|
2002-06-22 02:00:50 +08:00
|
|
|
/*@=globs@*/
|
2001-05-04 05:00:18 +08:00
|
|
|
if (mc->macroTable == NULL || mc->firstFree == 0)
|
|
|
|
return NULL;
|
|
|
|
|
2002-05-17 00:55:21 +08:00
|
|
|
/*@-branchstate@*/
|
2001-05-04 05:00:18 +08:00
|
|
|
if (namelen > 0) {
|
|
|
|
strncpy(namebuf, name, namelen);
|
|
|
|
namebuf[namelen] = '\0';
|
|
|
|
name = namebuf;
|
|
|
|
}
|
2002-05-17 00:55:21 +08:00
|
|
|
/*@=branchstate@*/
|
1997-03-31 22:13:21 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
key = &keybuf;
|
|
|
|
memset(key, 0, sizeof(*key));
|
2001-06-06 03:26:22 +08:00
|
|
|
/*@-temptrans -assignexpose@*/
|
2001-05-04 05:00:18 +08:00
|
|
|
key->name = (char *)name;
|
2001-06-06 03:26:22 +08:00
|
|
|
/*@=temptrans =assignexpose@*/
|
2001-05-04 05:00:18 +08:00
|
|
|
ret = (MacroEntry *) bsearch(&key, mc->macroTable, mc->firstFree,
|
1998-07-09 01:50:48 +08:00
|
|
|
sizeof(*(mc->macroTable)), compareMacroName);
|
2001-05-04 05:00:18 +08:00
|
|
|
/* XXX TODO: find 1st empty slot and return that */
|
|
|
|
return ret;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@=boundswrite@*/
|
1997-03-31 22:13:21 +08:00
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
/* =============================================================== */
|
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* fgets(3) analogue that reads \ continuations. Last newline always trimmed.
|
2002-06-22 02:00:50 +08:00
|
|
|
* @param buf input buffer
|
|
|
|
* @param size inbut buffer size (bytes)
|
|
|
|
* @param fd file handle
|
|
|
|
* @param escapes permit escaped newlines?
|
|
|
|
* @return buffer, or NULL on end-of-file
|
2000-09-30 03:50:29 +08:00
|
|
|
*/
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@-boundswrite@*/
|
2002-06-22 02:00:50 +08:00
|
|
|
/*@null@*/
|
|
|
|
static char *
|
2004-04-09 04:27:53 +08:00
|
|
|
rdcl(/*@returned@*/ char * buf, size_t size, FD_t fd)
|
2001-10-16 01:53:34 +08:00
|
|
|
/*@globals fileSystem @*/
|
2001-06-06 03:26:22 +08:00
|
|
|
/*@modifies buf, fileSystem @*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2002-06-22 02:00:50 +08:00
|
|
|
char *q = buf - 1; /* initialize just before buffer. */
|
2001-05-04 05:00:18 +08:00
|
|
|
size_t nb = 0;
|
|
|
|
size_t nread = 0;
|
|
|
|
FILE * f = fdGetFILE(fd);
|
2004-04-09 04:27:53 +08:00
|
|
|
int pc = 0, bc = 0;
|
|
|
|
char *p = buf;
|
2001-05-04 05:00:18 +08:00
|
|
|
|
|
|
|
if (f != NULL)
|
|
|
|
do {
|
2002-06-22 02:00:50 +08:00
|
|
|
*(++q) = '\0'; /* terminate and move forward. */
|
|
|
|
if (fgets(q, size, f) == NULL) /* read next line. */
|
2001-05-04 05:00:18 +08:00
|
|
|
break;
|
|
|
|
nb = strlen(q);
|
2002-06-22 02:00:50 +08:00
|
|
|
nread += nb; /* trim trailing \r and \n */
|
2001-05-04 05:00:18 +08:00
|
|
|
for (q += nb - 1; nb > 0 && iseol(*q); q--)
|
|
|
|
nb--;
|
2004-04-09 04:27:53 +08:00
|
|
|
for (; p <= q; p++) {
|
|
|
|
switch (*p) {
|
|
|
|
case '\\':
|
|
|
|
switch (*(p+1)) {
|
2004-04-19 20:12:12 +08:00
|
|
|
case '\0': /*@switchbreak@*/ break;
|
|
|
|
default: p++; /*@switchbreak@*/ break;
|
2004-04-09 04:27:53 +08:00
|
|
|
}
|
2004-04-19 20:12:12 +08:00
|
|
|
/*@switchbreak@*/ break;
|
2004-04-09 04:27:53 +08:00
|
|
|
case '%':
|
|
|
|
switch (*(p+1)) {
|
2004-04-19 20:12:12 +08:00
|
|
|
case '{': p++, bc++; /*@switchbreak@*/ break;
|
|
|
|
case '(': p++, pc++; /*@switchbreak@*/ break;
|
|
|
|
case '%': p++; /*@switchbreak@*/ break;
|
2004-04-09 04:27:53 +08:00
|
|
|
}
|
2004-04-19 20:12:12 +08:00
|
|
|
/*@switchbreak@*/ break;
|
|
|
|
case '{': if (bc > 0) bc++; /*@switchbreak@*/ break;
|
|
|
|
case '}': if (bc > 0) bc--; /*@switchbreak@*/ break;
|
|
|
|
case '(': if (pc > 0) pc++; /*@switchbreak@*/ break;
|
|
|
|
case ')': if (pc > 0) pc--; /*@switchbreak@*/ break;
|
2004-04-09 04:27:53 +08:00
|
|
|
}
|
|
|
|
}
|
2004-04-19 20:12:12 +08:00
|
|
|
if (nb == 0 || (*q != '\\' && !bc && !pc) || *(q+1) == '\0') {
|
2001-05-04 05:00:18 +08:00
|
|
|
*(++q) = '\0'; /* trim trailing \r, \n */
|
|
|
|
break;
|
|
|
|
}
|
2004-04-09 04:27:53 +08:00
|
|
|
q++; p++; nb++; /* copy newline too */
|
2001-05-04 05:00:18 +08:00
|
|
|
size -= nb;
|
|
|
|
if (*q == '\r') /* XXX avoid \r madness */
|
|
|
|
*q = '\n';
|
|
|
|
} while (size > 0);
|
2002-06-22 02:00:50 +08:00
|
|
|
return (nread > 0 ? buf : NULL);
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@=boundswrite@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
2000-11-01 00:18:34 +08:00
|
|
|
* Return text between pl and matching pr characters.
|
2000-09-30 03:50:29 +08:00
|
|
|
* @param p start of text
|
|
|
|
* @param pl left char, i.e. '[', '(', '{', etc.
|
|
|
|
* @param pr right char, i.e. ']', ')', '}', etc.
|
|
|
|
* @return address of last char before pr (or NULL)
|
|
|
|
*/
|
2003-04-02 06:20:45 +08:00
|
|
|
/*@null@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static const char *
|
2001-06-06 03:26:22 +08:00
|
|
|
matchchar(const char * p, char pl, char pr)
|
|
|
|
/*@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
int lvl = 0;
|
|
|
|
char c;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
while ((c = *p++) != '\0') {
|
|
|
|
if (c == '\\') { /* Ignore escaped chars */
|
|
|
|
p++;
|
|
|
|
continue;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2001-06-06 03:26:22 +08:00
|
|
|
if (c == pr) {
|
|
|
|
if (--lvl <= 0) return --p;
|
|
|
|
} else if (c == pl)
|
|
|
|
lvl++;
|
|
|
|
}
|
|
|
|
return (const char *)NULL;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Pre-print macro expression to be expanded.
|
|
|
|
* @param mb macro expansion state
|
|
|
|
* @param s current expansion string
|
|
|
|
* @param se end of string
|
|
|
|
*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static void
|
2001-06-06 03:26:22 +08:00
|
|
|
printMacro(MacroBuf mb, const char * s, const char * se)
|
2001-10-16 01:53:34 +08:00
|
|
|
/*@globals fileSystem @*/
|
2001-06-06 03:26:22 +08:00
|
|
|
/*@modifies fileSystem @*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
const char *senl;
|
|
|
|
const char *ellipsis;
|
|
|
|
int choplen;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
if (s >= se) { /* XXX just in case */
|
|
|
|
fprintf(stderr, _("%3d>%*s(empty)"), mb->depth,
|
|
|
|
(2 * mb->depth + 1), "");
|
|
|
|
return;
|
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
if (s[-1] == '{')
|
|
|
|
s--;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
/* Print only to first end-of-line (or end-of-string). */
|
|
|
|
for (senl = se; *senl && !iseol(*senl); senl++)
|
|
|
|
{};
|
|
|
|
|
|
|
|
/* Limit trailing non-trace output */
|
|
|
|
choplen = 61 - (2 * mb->depth);
|
|
|
|
if ((senl - s) > choplen) {
|
|
|
|
senl = s + choplen;
|
|
|
|
ellipsis = "...";
|
|
|
|
} else
|
|
|
|
ellipsis = "";
|
|
|
|
|
|
|
|
/* Substitute caret at end-of-macro position */
|
|
|
|
fprintf(stderr, "%3d>%*s%%%.*s^", mb->depth,
|
|
|
|
(2 * mb->depth + 1), "", (int)(se - s), s);
|
|
|
|
if (se[1] != '\0' && (senl - (se+1)) > 0)
|
|
|
|
fprintf(stderr, "%-.*s%s", (int)(senl - (se+1)), se+1, ellipsis);
|
|
|
|
fprintf(stderr, "\n");
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Post-print expanded macro expression.
|
|
|
|
* @param mb macro expansion state
|
|
|
|
* @param t current expansion string result
|
|
|
|
* @param te end of string
|
|
|
|
*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static void
|
2001-06-06 03:26:22 +08:00
|
|
|
printExpansion(MacroBuf mb, const char * t, const char * te)
|
2001-10-16 01:53:34 +08:00
|
|
|
/*@globals fileSystem @*/
|
2001-06-06 03:26:22 +08:00
|
|
|
/*@modifies fileSystem @*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
const char *ellipsis;
|
|
|
|
int choplen;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
if (!(te > t)) {
|
|
|
|
fprintf(stderr, _("%3d<%*s(empty)\n"), mb->depth, (2 * mb->depth + 1), "");
|
|
|
|
return;
|
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
/* Shorten output which contains newlines */
|
|
|
|
while (te > t && iseol(te[-1]))
|
|
|
|
te--;
|
|
|
|
ellipsis = "";
|
|
|
|
if (mb->depth > 0) {
|
|
|
|
const char *tenl;
|
|
|
|
|
|
|
|
/* Skip to last line of expansion */
|
|
|
|
while ((tenl = strchr(t, '\n')) && tenl < te)
|
|
|
|
t = ++tenl;
|
|
|
|
|
|
|
|
/* Limit expand output */
|
|
|
|
choplen = 61 - (2 * mb->depth);
|
|
|
|
if ((te - t) > choplen) {
|
|
|
|
te = t + choplen;
|
|
|
|
ellipsis = "...";
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2001-06-06 03:26:22 +08:00
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
fprintf(stderr, "%3d<%*s", mb->depth, (2 * mb->depth + 1), "");
|
|
|
|
if (te > t)
|
|
|
|
fprintf(stderr, "%.*s%s", (int)(te - t), t, ellipsis);
|
|
|
|
fprintf(stderr, "\n");
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define SKIPBLANK(_s, _c) \
|
2002-06-22 02:00:50 +08:00
|
|
|
/*@-globs@*/ /* FIX: __ctype_b */ \
|
1998-07-09 01:50:48 +08:00
|
|
|
while (((_c) = *(_s)) && isblank(_c)) \
|
2002-06-22 02:00:50 +08:00
|
|
|
(_s)++; \
|
|
|
|
/*@=globs@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
|
|
|
|
#define SKIPNONBLANK(_s, _c) \
|
2002-06-22 02:00:50 +08:00
|
|
|
/*@-globs@*/ /* FIX: __ctype_b */ \
|
1999-08-17 02:57:37 +08:00
|
|
|
while (((_c) = *(_s)) && !(isblank(_c) || iseol(_c))) \
|
2002-06-22 02:00:50 +08:00
|
|
|
(_s)++; \
|
|
|
|
/*@=globs@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
|
|
|
|
#define COPYNAME(_ne, _s, _c) \
|
|
|
|
{ SKIPBLANK(_s,_c); \
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@-boundswrite@*/ \
|
2001-04-29 09:05:43 +08:00
|
|
|
while(((_c) = *(_s)) && (xisalnum(_c) || (_c) == '_')) \
|
1998-07-09 01:50:48 +08:00
|
|
|
*(_ne)++ = *(_s)++; \
|
|
|
|
*(_ne) = '\0'; \
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@=boundswrite@*/ \
|
1997-03-31 22:13:21 +08:00
|
|
|
}
|
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
#define COPYOPTS(_oe, _s, _c) \
|
2002-06-24 03:47:08 +08:00
|
|
|
{ /*@-boundswrite@*/ \
|
|
|
|
while(((_c) = *(_s)) && (_c) != ')') \
|
1998-07-09 01:50:48 +08:00
|
|
|
*(_oe)++ = *(_s)++; \
|
|
|
|
*(_oe) = '\0'; \
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@=boundswrite@*/ \
|
1997-03-31 22:13:21 +08:00
|
|
|
}
|
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Save source and expand field into target.
|
|
|
|
* @param mb macro expansion state
|
|
|
|
* @param f field
|
|
|
|
* @param flen no. bytes in field
|
|
|
|
* @return result of expansion
|
|
|
|
*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static int
|
2001-06-06 03:26:22 +08:00
|
|
|
expandT(MacroBuf mb, const char * f, size_t flen)
|
2003-05-09 04:39:29 +08:00
|
|
|
/*@globals rpmGlobalMacroContext, h_errno, fileSystem@*/
|
2001-10-18 00:43:36 +08:00
|
|
|
/*@modifies mb, rpmGlobalMacroContext, fileSystem @*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
char *sbuf;
|
|
|
|
const char *s = mb->s;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
sbuf = alloca(flen + 1);
|
|
|
|
memset(sbuf, 0, (flen + 1));
|
|
|
|
|
|
|
|
strncpy(sbuf, f, flen);
|
|
|
|
sbuf[flen] = '\0';
|
|
|
|
mb->s = sbuf;
|
|
|
|
rc = expandMacro(mb);
|
|
|
|
mb->s = s;
|
|
|
|
return rc;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Save target and expand sbuf into target.
|
|
|
|
* @param mb macro expansion state
|
|
|
|
* @param tbuf target buffer
|
|
|
|
* @param tbuflen no. bytes in target buffer
|
|
|
|
* @return result of expansion
|
|
|
|
*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static int
|
2001-06-06 03:26:22 +08:00
|
|
|
expandS(MacroBuf mb, char * tbuf, size_t tbuflen)
|
2002-06-22 02:00:50 +08:00
|
|
|
/*@globals rpmGlobalMacroContext, fileSystem@*/
|
2001-10-18 00:43:36 +08:00
|
|
|
/*@modifies mb, *tbuf, rpmGlobalMacroContext, fileSystem @*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
const char *t = mb->t;
|
|
|
|
size_t nb = mb->nb;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
mb->t = tbuf;
|
|
|
|
mb->nb = tbuflen;
|
|
|
|
rc = expandMacro(mb);
|
|
|
|
mb->t = t;
|
|
|
|
mb->nb = nb;
|
|
|
|
return rc;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Save source/target and expand macro in u.
|
|
|
|
* @param mb macro expansion state
|
|
|
|
* @param u input macro, output expansion
|
|
|
|
* @param ulen no. bytes in u buffer
|
|
|
|
* @return result of expansion
|
|
|
|
*/
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@-boundswrite@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static int
|
2001-06-06 03:26:22 +08:00
|
|
|
expandU(MacroBuf mb, char * u, size_t ulen)
|
2003-05-09 04:39:29 +08:00
|
|
|
/*@globals rpmGlobalMacroContext, h_errno, fileSystem@*/
|
2001-10-18 00:43:36 +08:00
|
|
|
/*@modifies mb, *u, rpmGlobalMacroContext, fileSystem @*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
const char *s = mb->s;
|
|
|
|
char *t = mb->t;
|
|
|
|
size_t nb = mb->nb;
|
|
|
|
char *tbuf;
|
|
|
|
int rc;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
tbuf = alloca(ulen + 1);
|
|
|
|
memset(tbuf, 0, (ulen + 1));
|
|
|
|
|
|
|
|
mb->s = u;
|
|
|
|
mb->t = tbuf;
|
|
|
|
mb->nb = ulen;
|
|
|
|
rc = expandMacro(mb);
|
|
|
|
|
|
|
|
tbuf[ulen] = '\0'; /* XXX just in case */
|
|
|
|
if (ulen > mb->nb)
|
|
|
|
strncpy(u, tbuf, (ulen - mb->nb + 1));
|
|
|
|
|
|
|
|
mb->s = s;
|
|
|
|
mb->t = t;
|
|
|
|
mb->nb = nb;
|
|
|
|
|
|
|
|
return rc;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@=boundswrite@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Expand output of shell command into target buffer.
|
|
|
|
* @param mb macro expansion state
|
|
|
|
* @param cmd shell command
|
|
|
|
* @param clen no. bytes in shell command
|
|
|
|
* @return result of expansion
|
|
|
|
*/
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@-boundswrite@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static int
|
2001-06-06 03:26:22 +08:00
|
|
|
doShellEscape(MacroBuf mb, const char * cmd, size_t clen)
|
2003-05-09 04:39:29 +08:00
|
|
|
/*@globals rpmGlobalMacroContext, h_errno, fileSystem @*/
|
2002-06-22 02:00:50 +08:00
|
|
|
/*@modifies mb, rpmGlobalMacroContext, fileSystem @*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
char pcmd[BUFSIZ];
|
|
|
|
FILE *shf;
|
|
|
|
int rc;
|
|
|
|
int c;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
strncpy(pcmd, cmd, clen);
|
|
|
|
pcmd[clen] = '\0';
|
|
|
|
rc = expandU(mb, pcmd, sizeof(pcmd));
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
if ((shf = popen(pcmd, "r")) == NULL)
|
|
|
|
return 1;
|
|
|
|
while(mb->nb > 0 && (c = fgetc(shf)) != EOF)
|
|
|
|
SAVECHAR(mb, c);
|
|
|
|
(void) pclose(shf);
|
|
|
|
|
|
|
|
/* XXX delete trailing \r \n */
|
|
|
|
while (iseol(mb->t[-1])) {
|
|
|
|
*(mb->t--) = '\0';
|
|
|
|
mb->nb++;
|
|
|
|
}
|
|
|
|
return 0;
|
1997-03-31 22:13:21 +08:00
|
|
|
}
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@=boundswrite@*/
|
1997-03-31 22:13:21 +08:00
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Parse (and execute) new macro definition.
|
|
|
|
* @param mb macro expansion state
|
|
|
|
* @param se macro definition to parse
|
|
|
|
* @param level macro recursion level
|
|
|
|
* @param expandbody should body be expanded?
|
|
|
|
* @return address to continue parsing
|
|
|
|
*/
|
2000-11-01 00:18:34 +08:00
|
|
|
/*@dependent@*/ static const char *
|
2002-06-22 02:00:50 +08:00
|
|
|
doDefine(MacroBuf mb, /*@returned@*/ const char * se, int level, int expandbody)
|
2003-05-09 04:39:29 +08:00
|
|
|
/*@globals rpmGlobalMacroContext, h_errno @*/
|
2001-10-18 00:43:36 +08:00
|
|
|
/*@modifies mb, rpmGlobalMacroContext @*/
|
1997-03-31 22:13:21 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
const char *s = se;
|
|
|
|
char buf[BUFSIZ], *n = buf, *ne = n;
|
|
|
|
char *o = NULL, *oe;
|
|
|
|
char *b, *be;
|
|
|
|
int c;
|
|
|
|
int oc = ')';
|
|
|
|
|
|
|
|
/* Copy name */
|
|
|
|
COPYNAME(ne, s, c);
|
|
|
|
|
|
|
|
/* Copy opts (if present) */
|
|
|
|
oe = ne + 1;
|
|
|
|
if (*s == '(') {
|
|
|
|
s++; /* skip ( */
|
|
|
|
o = oe;
|
|
|
|
COPYOPTS(oe, s, oc);
|
|
|
|
s++; /* skip ) */
|
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
/* Copy body, skipping over escaped newlines */
|
|
|
|
b = be = oe + 1;
|
|
|
|
SKIPBLANK(s, c);
|
|
|
|
if (c == '{') { /* XXX permit silent {...} grouping */
|
|
|
|
if ((se = matchchar(s, c, '}')) == NULL) {
|
|
|
|
rpmError(RPMERR_BADSPEC,
|
|
|
|
_("Macro %%%s has unterminated body\n"), n);
|
|
|
|
se = s; /* XXX W2DO? */
|
2002-06-22 02:00:50 +08:00
|
|
|
return se;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2001-06-06 03:26:22 +08:00
|
|
|
s++; /* XXX skip { */
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@-boundswrite@*/
|
2001-06-06 03:26:22 +08:00
|
|
|
strncpy(b, s, (se - s));
|
|
|
|
b[se - s] = '\0';
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@=boundswrite@*/
|
2001-06-06 03:26:22 +08:00
|
|
|
be += strlen(b);
|
|
|
|
se++; /* XXX skip } */
|
|
|
|
s = se; /* move scan forward */
|
|
|
|
} else { /* otherwise free-field */
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@-boundswrite@*/
|
2004-04-09 04:27:53 +08:00
|
|
|
int bc = 0, pc = 0;
|
|
|
|
while (*s && (bc || pc || !iseol(*s))) {
|
|
|
|
switch (*s) {
|
|
|
|
case '\\':
|
|
|
|
switch (*(s+1)) {
|
2004-04-19 20:12:12 +08:00
|
|
|
case '\0': /*@switchbreak@*/ break;
|
|
|
|
default: s++; /*@switchbreak@*/ break;
|
2004-04-09 04:27:53 +08:00
|
|
|
}
|
2004-04-19 20:12:12 +08:00
|
|
|
/*@switchbreak@*/ break;
|
2004-04-09 04:27:53 +08:00
|
|
|
case '%':
|
|
|
|
switch (*(s+1)) {
|
2004-04-19 20:12:12 +08:00
|
|
|
case '{': *be++ = *s++; bc++; /*@switchbreak@*/ break;
|
|
|
|
case '(': *be++ = *s++; pc++; /*@switchbreak@*/ break;
|
|
|
|
case '%': *be++ = *s++; /*@switchbreak@*/ break;
|
2004-04-09 04:27:53 +08:00
|
|
|
}
|
2004-04-19 20:12:12 +08:00
|
|
|
/*@switchbreak@*/ break;
|
|
|
|
case '{': if (bc > 0) bc++; /*@switchbreak@*/ break;
|
|
|
|
case '}': if (bc > 0) bc--; /*@switchbreak@*/ break;
|
|
|
|
case '(': if (pc > 0) pc++; /*@switchbreak@*/ break;
|
|
|
|
case ')': if (pc > 0) pc--; /*@switchbreak@*/ break;
|
2004-04-09 04:27:53 +08:00
|
|
|
}
|
|
|
|
*be++ = *s++;
|
|
|
|
}
|
|
|
|
*be = '\0';
|
|
|
|
|
|
|
|
if (bc || pc) {
|
|
|
|
rpmError(RPMERR_BADSPEC,
|
|
|
|
_("Macro %%%s has unterminated body\n"), n);
|
|
|
|
se = s; /* XXX W2DO? */
|
|
|
|
return se;
|
|
|
|
}
|
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
/* Trim trailing blanks/newlines */
|
2002-01-19 06:51:30 +08:00
|
|
|
/*@-globs@*/
|
2001-06-06 03:26:22 +08:00
|
|
|
while (--be >= b && (c = *be) && (isblank(c) || iseol(c)))
|
|
|
|
{};
|
2002-01-19 06:51:30 +08:00
|
|
|
/*@=globs@*/
|
2001-06-06 03:26:22 +08:00
|
|
|
*(++be) = '\0'; /* one too far */
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@=boundswrite@*/
|
2001-06-06 03:26:22 +08:00
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
/* Move scan over body */
|
|
|
|
while (iseol(*s))
|
|
|
|
s++;
|
|
|
|
se = s;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
/* Names must start with alphabetic or _ and be at least 3 chars */
|
|
|
|
if (!((c = *n) && (xisalpha(c) || c == '_') && (ne - n) > 2)) {
|
|
|
|
rpmError(RPMERR_BADSPEC,
|
|
|
|
_("Macro %%%s has illegal name (%%define)\n"), n);
|
2002-06-22 02:00:50 +08:00
|
|
|
return se;
|
2001-06-06 03:26:22 +08:00
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
/* Options must be terminated with ')' */
|
|
|
|
if (o && oc != ')') {
|
|
|
|
rpmError(RPMERR_BADSPEC, _("Macro %%%s has unterminated opts\n"), n);
|
2002-06-22 02:00:50 +08:00
|
|
|
return se;
|
2001-06-06 03:26:22 +08:00
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
if ((be - b) < 1) {
|
|
|
|
rpmError(RPMERR_BADSPEC, _("Macro %%%s has empty body\n"), n);
|
2002-06-22 02:00:50 +08:00
|
|
|
return se;
|
2001-06-06 03:26:22 +08:00
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@-modfilesys@*/
|
2001-06-06 03:26:22 +08:00
|
|
|
if (expandbody && expandU(mb, b, (&buf[sizeof(buf)] - b))) {
|
|
|
|
rpmError(RPMERR_BADSPEC, _("Macro %%%s failed to expand\n"), n);
|
2002-06-22 02:00:50 +08:00
|
|
|
return se;
|
2001-06-06 03:26:22 +08:00
|
|
|
}
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@=modfilesys@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
addMacro(mb->mc, n, o, b, (level - 1));
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2002-06-22 02:00:50 +08:00
|
|
|
return se;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Parse (and execute) macro undefinition.
|
|
|
|
* @param mc macro context
|
|
|
|
* @param se macro name to undefine
|
|
|
|
* @return address to continue parsing
|
|
|
|
*/
|
2000-11-01 00:18:34 +08:00
|
|
|
/*@dependent@*/ static const char *
|
2002-06-22 02:00:50 +08:00
|
|
|
doUndefine(MacroContext mc, /*@returned@*/ const char * se)
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@globals rpmGlobalMacroContext @*/
|
2001-10-18 00:43:36 +08:00
|
|
|
/*@modifies mc, rpmGlobalMacroContext @*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
const char *s = se;
|
|
|
|
char buf[BUFSIZ], *n = buf, *ne = n;
|
|
|
|
int c;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
COPYNAME(ne, s, c);
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
/* Move scan over body */
|
|
|
|
while (iseol(*s))
|
|
|
|
s++;
|
|
|
|
se = s;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
/* Names must start with alphabetic or _ and be at least 3 chars */
|
|
|
|
if (!((c = *n) && (xisalpha(c) || c == '_') && (ne - n) > 2)) {
|
|
|
|
rpmError(RPMERR_BADSPEC,
|
|
|
|
_("Macro %%%s has illegal name (%%undefine)\n"), n);
|
2002-06-22 02:00:50 +08:00
|
|
|
return se;
|
2001-06-06 03:26:22 +08:00
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
delMacro(mc, n);
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2002-06-22 02:00:50 +08:00
|
|
|
return se;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
|
|
|
|
1998-10-08 19:55:37 +08:00
|
|
|
#ifdef DYING
|
1998-07-09 01:50:48 +08:00
|
|
|
static void
|
2001-06-06 03:26:22 +08:00
|
|
|
dumpME(const char * msg, MacroEntry me)
|
2001-10-16 01:53:34 +08:00
|
|
|
/*@globals fileSystem @*/
|
2001-06-06 03:26:22 +08:00
|
|
|
/*@modifies fileSystem @*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
if (msg)
|
|
|
|
fprintf(stderr, "%s", msg);
|
|
|
|
fprintf(stderr, "\tme %p", me);
|
|
|
|
if (me)
|
|
|
|
fprintf(stderr,"\tname %p(%s) prev %p",
|
|
|
|
me->name, me->name, me->prev);
|
|
|
|
fprintf(stderr, "\n");
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
1998-10-08 19:55:37 +08:00
|
|
|
#endif
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Push new macro definition onto macro entry stack.
|
|
|
|
* @param mep address of macro entry slot
|
|
|
|
* @param n macro name
|
|
|
|
* @param o macro parameters (NULL if none)
|
|
|
|
* @param b macro body (NULL becomes "")
|
|
|
|
* @param level macro recursion level
|
|
|
|
*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static void
|
2001-06-06 03:26:22 +08:00
|
|
|
pushMacro(/*@out@*/ MacroEntry * mep,
|
|
|
|
const char * n, /*@null@*/ const char * o,
|
|
|
|
/*@null@*/ const char * b, int level)
|
|
|
|
/*@modifies *mep @*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2001-05-04 05:00:18 +08:00
|
|
|
MacroEntry prev = (mep && *mep ? *mep : NULL);
|
|
|
|
MacroEntry me = (MacroEntry) xmalloc(sizeof(*me));
|
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
/*@-assignexpose@*/
|
2001-05-04 05:00:18 +08:00
|
|
|
me->prev = prev;
|
2001-06-06 03:26:22 +08:00
|
|
|
/*@=assignexpose@*/
|
2001-05-04 05:00:18 +08:00
|
|
|
me->name = (prev ? prev->name : xstrdup(n));
|
|
|
|
me->opts = (o ? xstrdup(o) : NULL);
|
|
|
|
me->body = xstrdup(b ? b : "");
|
|
|
|
me->used = 0;
|
|
|
|
me->level = level;
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@-boundswrite@*/
|
2002-05-17 00:55:21 +08:00
|
|
|
/*@-branchstate@*/
|
2001-05-04 05:00:18 +08:00
|
|
|
if (mep)
|
1998-07-09 01:50:48 +08:00
|
|
|
*mep = me;
|
2001-05-04 05:00:18 +08:00
|
|
|
else
|
|
|
|
me = _free(me);
|
2002-05-17 00:55:21 +08:00
|
|
|
/*@=branchstate@*/
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@=boundswrite@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Pop macro definition from macro entry stack.
|
|
|
|
* @param mep address of macro entry slot
|
|
|
|
*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static void
|
2001-06-06 03:26:22 +08:00
|
|
|
popMacro(MacroEntry * mep)
|
|
|
|
/*@modifies *mep @*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2001-05-04 05:00:18 +08:00
|
|
|
MacroEntry me = (*mep ? *mep : NULL);
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2002-05-17 00:55:21 +08:00
|
|
|
/*@-branchstate@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
if (me) {
|
|
|
|
/* XXX cast to workaround const */
|
2001-05-01 06:32:22 +08:00
|
|
|
/*@-onlytrans@*/
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@-boundswrite@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
if ((*mep = me->prev) == NULL)
|
2001-05-04 05:00:18 +08:00
|
|
|
me->name = _free(me->name);
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@=boundswrite@*/
|
2001-05-04 05:00:18 +08:00
|
|
|
me->opts = _free(me->opts);
|
|
|
|
me->body = _free(me->body);
|
|
|
|
me = _free(me);
|
2001-05-01 06:32:22 +08:00
|
|
|
/*@=onlytrans@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2002-05-17 00:55:21 +08:00
|
|
|
/*@=branchstate@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Free parsed arguments for parameterized macro.
|
|
|
|
* @param mb macro expansion state
|
|
|
|
*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static void
|
2001-06-06 03:26:22 +08:00
|
|
|
freeArgs(MacroBuf mb)
|
|
|
|
/*@modifies mb @*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2001-05-04 05:00:18 +08:00
|
|
|
MacroContext mc = mb->mc;
|
|
|
|
int ndeleted = 0;
|
|
|
|
int i;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
if (mc == NULL || mc->macroTable == NULL)
|
|
|
|
return;
|
1999-08-17 00:18:25 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
/* Delete dynamic macro definitions */
|
|
|
|
for (i = 0; i < mc->firstFree; i++) {
|
|
|
|
MacroEntry *mep, me;
|
|
|
|
int skiptest = 0;
|
|
|
|
mep = &mc->macroTable[i];
|
|
|
|
me = *mep;
|
|
|
|
|
|
|
|
if (me == NULL) /* XXX this should never happen */
|
|
|
|
continue;
|
|
|
|
if (me->level < mb->depth)
|
|
|
|
continue;
|
|
|
|
if (strlen(me->name) == 1 && strchr("#*0", *me->name)) {
|
|
|
|
if (*me->name == '*' && me->used > 0)
|
|
|
|
skiptest = 1; /* XXX skip test for %# %* %0 */
|
|
|
|
} else if (!skiptest && me->used <= 0) {
|
1998-07-09 01:50:48 +08:00
|
|
|
#if NOTYET
|
2001-05-04 05:00:18 +08:00
|
|
|
rpmError(RPMERR_BADSPEC,
|
|
|
|
_("Macro %%%s (%s) was not used below level %d\n"),
|
|
|
|
me->name, me->body, me->level);
|
1998-07-09 01:50:48 +08:00
|
|
|
#endif
|
|
|
|
}
|
2001-05-04 05:00:18 +08:00
|
|
|
popMacro(mep);
|
|
|
|
if (!(mep && *mep))
|
|
|
|
ndeleted++;
|
|
|
|
}
|
1999-08-17 00:18:25 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
/* If any deleted macros, sort macro table */
|
|
|
|
if (ndeleted)
|
|
|
|
sortMacroTable(mc);
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Parse arguments (to next new line) for parameterized macro.
|
2001-09-15 21:49:11 +08:00
|
|
|
* @todo Use popt rather than getopt to parse args.
|
2000-09-30 03:50:29 +08:00
|
|
|
* @param mb macro expansion state
|
|
|
|
* @param me macro entry slot
|
|
|
|
* @param se arguments to parse
|
|
|
|
* @param lastc stop parsing at lastc
|
|
|
|
* @return address to continue parsing
|
|
|
|
*/
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@-bounds@*/
|
2000-11-01 00:18:34 +08:00
|
|
|
/*@dependent@*/ static const char *
|
2003-12-30 23:12:50 +08:00
|
|
|
grabArgs(MacroBuf mb, const MacroEntry me, /*@returned@*/ const char * se,
|
|
|
|
const char * lastc)
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@globals rpmGlobalMacroContext @*/
|
2001-10-18 00:43:36 +08:00
|
|
|
/*@modifies mb, rpmGlobalMacroContext @*/
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
|
|
|
char buf[BUFSIZ], *b, *be;
|
|
|
|
char aname[16];
|
|
|
|
const char *opts, *o;
|
|
|
|
int argc = 0;
|
|
|
|
const char **argv;
|
|
|
|
int c;
|
|
|
|
|
2000-06-22 07:28:50 +08:00
|
|
|
/* Copy macro name as argv[0], save beginning of args. */
|
2001-05-04 05:00:18 +08:00
|
|
|
buf[0] = '\0';
|
2000-06-22 07:28:50 +08:00
|
|
|
b = be = stpcpy(buf, me->name);
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2000-06-22 07:28:50 +08:00
|
|
|
addMacro(mb->mc, "0", NULL, buf, mb->depth);
|
1997-03-31 22:13:21 +08:00
|
|
|
|
2000-06-22 07:28:50 +08:00
|
|
|
argc = 1; /* XXX count argv[0] */
|
|
|
|
|
1999-07-29 20:36:03 +08:00
|
|
|
/* Copy args into buf until lastc */
|
1998-07-09 01:50:48 +08:00
|
|
|
*be++ = ' ';
|
2003-12-27 09:37:56 +08:00
|
|
|
while ((c = *se++) != '\0' && (se-1) != lastc) {
|
2002-01-19 06:51:30 +08:00
|
|
|
/*@-globs@*/
|
2000-06-22 07:28:50 +08:00
|
|
|
if (!isblank(c)) {
|
|
|
|
*be++ = c;
|
|
|
|
continue;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2002-01-19 06:51:30 +08:00
|
|
|
/*@=globs@*/
|
2000-06-22 07:28:50 +08:00
|
|
|
/* c is blank */
|
|
|
|
if (be[-1] == ' ')
|
|
|
|
continue;
|
|
|
|
/* a word has ended */
|
|
|
|
*be++ = ' ';
|
1998-07-09 01:50:48 +08:00
|
|
|
argc++;
|
1997-03-31 22:13:21 +08:00
|
|
|
}
|
2000-06-22 07:28:50 +08:00
|
|
|
if (c == '\0') se--; /* one too far */
|
|
|
|
if (be[-1] != ' ')
|
|
|
|
argc++, be++; /* last word has not trailing ' ' */
|
|
|
|
be[-1] = '\0';
|
|
|
|
if (*b == ' ') b++; /* skip the leading ' ' */
|
1998-07-09 01:50:48 +08:00
|
|
|
|
1999-07-29 19:20:20 +08:00
|
|
|
/*
|
|
|
|
* The macro %* analoguous to the shell's $* means "Pass all non-macro
|
|
|
|
* parameters." Consequently, there needs to be a macro that means "Pass all
|
|
|
|
* (including macro parameters) options". This is useful for verifying
|
|
|
|
* parameters during expansion and yet transparently passing all parameters
|
|
|
|
* through for higher level processing (e.g. %description and/or %setup).
|
|
|
|
* This is the (potential) justification for %{**} ...
|
|
|
|
*/
|
1998-07-09 01:50:48 +08:00
|
|
|
/* Add unexpanded args as macro */
|
1999-07-29 19:20:20 +08:00
|
|
|
addMacro(mb->mc, "**", NULL, b, mb->depth);
|
1998-07-09 01:50:48 +08:00
|
|
|
|
|
|
|
#ifdef NOTYET
|
|
|
|
/* XXX if macros can be passed as args ... */
|
|
|
|
expandU(mb, buf, sizeof(buf));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Build argv array */
|
2001-10-16 22:58:57 +08:00
|
|
|
argv = (const char **) alloca((argc + 1) * sizeof(*argv));
|
2001-09-15 21:49:11 +08:00
|
|
|
be[-1] = ' '; /* assert((be - 1) == (b + strlen(b) == buf + strlen(buf))) */
|
2001-03-24 04:27:48 +08:00
|
|
|
be[0] = '\0';
|
2000-06-22 07:28:50 +08:00
|
|
|
b = buf;
|
1998-07-09 01:50:48 +08:00
|
|
|
for (c = 0; c < argc; c++) {
|
|
|
|
argv[c] = b;
|
2000-06-22 07:28:50 +08:00
|
|
|
b = strchr(b, ' ');
|
|
|
|
*b++ = '\0';
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2001-09-15 21:49:11 +08:00
|
|
|
/* assert(b == be); */
|
1998-07-09 01:50:48 +08:00
|
|
|
argv[argc] = NULL;
|
|
|
|
|
2001-09-15 21:49:11 +08:00
|
|
|
/* Citation from glibc/posix/getopt.c:
|
|
|
|
* Index in ARGV of the next element to be scanned.
|
|
|
|
* This is used for communication to and from the caller
|
|
|
|
* and for communication between successive calls to `getopt'.
|
|
|
|
*
|
|
|
|
* On entry to `getopt', zero means this is the first call; initialize.
|
|
|
|
*
|
|
|
|
* When `getopt' returns -1, this is the index of the first of the
|
|
|
|
* non-option elements that the caller should itself scan.
|
|
|
|
*
|
|
|
|
* Otherwise, `optind' communicates from one call to the next
|
|
|
|
* how much of ARGV has been scanned so far.
|
|
|
|
*/
|
|
|
|
/* 1003.2 says this must be 1 before any call. */
|
|
|
|
|
|
|
|
#ifdef __GLIBC__
|
2001-10-18 00:43:36 +08:00
|
|
|
/*@-mods@*/
|
2003-05-15 21:44:18 +08:00
|
|
|
optind = 0; /* XXX but posix != glibc */
|
2001-10-18 00:43:36 +08:00
|
|
|
/*@=mods@*/
|
2003-12-27 09:37:56 +08:00
|
|
|
#else
|
|
|
|
optind = 1;
|
2001-09-15 21:49:11 +08:00
|
|
|
#endif
|
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
opts = me->opts;
|
|
|
|
|
2000-06-22 07:28:50 +08:00
|
|
|
/* Define option macros. */
|
2003-04-02 06:20:45 +08:00
|
|
|
/*@-nullstate@*/ /* FIX: argv[] can be NULL */
|
|
|
|
while((c = getopt(argc, (char **)argv, opts)) != -1)
|
|
|
|
/*@=nullstate@*/
|
|
|
|
{
|
2000-06-22 07:28:50 +08:00
|
|
|
if (c == '?' || (o = strchr(opts, c)) == NULL) {
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_BADSPEC, _("Unknown option %c in %s(%s)\n"),
|
2001-05-01 06:32:22 +08:00
|
|
|
(char)c, me->name, opts);
|
2002-06-22 02:00:50 +08:00
|
|
|
return se;
|
1997-03-31 22:13:21 +08:00
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
*be++ = '-';
|
|
|
|
*be++ = c;
|
|
|
|
if (o[1] == ':') {
|
2000-06-22 07:28:50 +08:00
|
|
|
*be++ = ' ';
|
|
|
|
be = stpcpy(be, optarg);
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
|
|
|
*be++ = '\0';
|
2000-06-22 07:28:50 +08:00
|
|
|
aname[0] = '-'; aname[1] = c; aname[2] = '\0';
|
1998-07-09 01:50:48 +08:00
|
|
|
addMacro(mb->mc, aname, NULL, b, mb->depth);
|
|
|
|
if (o[1] == ':') {
|
2000-06-22 07:28:50 +08:00
|
|
|
aname[0] = '-'; aname[1] = c; aname[2] = '*'; aname[3] = '\0';
|
|
|
|
addMacro(mb->mc, aname, NULL, optarg, mb->depth);
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2000-06-22 07:28:50 +08:00
|
|
|
be = b; /* reuse the space */
|
1998-05-21 01:05:26 +08:00
|
|
|
}
|
1997-03-31 22:13:21 +08:00
|
|
|
|
2000-06-22 07:28:50 +08:00
|
|
|
/* Add arg count as macro. */
|
|
|
|
sprintf(aname, "%d", (argc - optind));
|
|
|
|
addMacro(mb->mc, "#", NULL, aname, mb->depth);
|
|
|
|
|
|
|
|
/* Add macro for each arg. Concatenate args for %*. */
|
2001-05-04 05:00:18 +08:00
|
|
|
if (be) {
|
|
|
|
*be = '\0';
|
|
|
|
for (c = optind; c < argc; c++) {
|
|
|
|
sprintf(aname, "%d", (c - optind + 1));
|
|
|
|
addMacro(mb->mc, aname, NULL, argv[c], mb->depth);
|
2004-03-28 21:49:19 +08:00
|
|
|
if (be != b) *be++ = ' '; /* Add space between args */
|
2003-04-02 06:20:45 +08:00
|
|
|
/*@-nullpass@*/ /* FIX: argv[] can be NULL */
|
2001-05-04 05:00:18 +08:00
|
|
|
be = stpcpy(be, argv[c]);
|
2003-04-02 06:20:45 +08:00
|
|
|
/*@=nullpass@*/
|
2001-05-04 05:00:18 +08:00
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
1999-04-21 06:45:52 +08:00
|
|
|
|
1999-08-17 00:18:25 +08:00
|
|
|
/* Add unexpanded args as macro. */
|
1999-04-21 06:45:52 +08:00
|
|
|
addMacro(mb->mc, "*", NULL, b, mb->depth);
|
|
|
|
|
2002-06-22 02:00:50 +08:00
|
|
|
return se;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@=bounds@*/
|
1997-03-31 22:13:21 +08:00
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Perform macro message output
|
|
|
|
* @param mb macro expansion state
|
|
|
|
* @param waserror use rpmError()?
|
|
|
|
* @param msg message to ouput
|
|
|
|
* @param msglen no. of bytes in message
|
|
|
|
*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static void
|
2001-06-06 03:26:22 +08:00
|
|
|
doOutput(MacroBuf mb, int waserror, const char * msg, size_t msglen)
|
2003-05-09 04:39:29 +08:00
|
|
|
/*@globals rpmGlobalMacroContext, h_errno, fileSystem @*/
|
2002-06-22 02:00:50 +08:00
|
|
|
/*@modifies mb, rpmGlobalMacroContext, fileSystem @*/
|
1998-01-13 05:31:29 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
char buf[BUFSIZ];
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
strncpy(buf, msg, msglen);
|
|
|
|
buf[msglen] = '\0';
|
|
|
|
(void) expandU(mb, buf, sizeof(buf));
|
|
|
|
if (waserror)
|
|
|
|
rpmError(RPMERR_BADSPEC, "%s\n", buf);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "%s", buf);
|
1998-01-13 05:31:29 +08:00
|
|
|
}
|
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/**
|
|
|
|
* Execute macro primitives.
|
|
|
|
* @param mb macro expansion state
|
|
|
|
* @param negate should logic be inverted?
|
|
|
|
* @param f beginning of field f
|
|
|
|
* @param fn length of field f
|
|
|
|
* @param g beginning of field g
|
|
|
|
* @param gn length of field g
|
|
|
|
*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static void
|
2001-06-06 03:26:22 +08:00
|
|
|
doFoo(MacroBuf mb, int negate, const char * f, size_t fn,
|
2003-04-02 06:20:45 +08:00
|
|
|
/*@null@*/ const char * g, size_t gn)
|
2003-05-09 04:39:29 +08:00
|
|
|
/*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
|
2002-06-22 02:00:50 +08:00
|
|
|
/*@modifies mb, rpmGlobalMacroContext, fileSystem, internalState @*/
|
1997-03-31 22:13:21 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
char buf[BUFSIZ], *b = NULL, *be;
|
|
|
|
int c;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
buf[0] = '\0';
|
2003-04-02 06:20:45 +08:00
|
|
|
if (g != NULL) {
|
2002-04-09 02:56:01 +08:00
|
|
|
strncpy(buf, g, gn);
|
|
|
|
buf[gn] = '\0';
|
2001-06-06 03:26:22 +08:00
|
|
|
(void) expandU(mb, buf, sizeof(buf));
|
|
|
|
}
|
|
|
|
if (STREQ("basename", f, fn)) {
|
|
|
|
if ((b = strrchr(buf, '/')) == NULL)
|
|
|
|
b = buf;
|
2002-08-16 02:50:46 +08:00
|
|
|
else
|
|
|
|
b++;
|
1998-07-09 01:50:48 +08:00
|
|
|
#if NOTYET
|
2001-06-06 03:26:22 +08:00
|
|
|
/* XXX watchout for conflict with %dir */
|
|
|
|
} else if (STREQ("dirname", f, fn)) {
|
|
|
|
if ((b = strrchr(buf, '/')) != NULL)
|
|
|
|
*b = '\0';
|
|
|
|
b = buf;
|
1998-07-09 01:50:48 +08:00
|
|
|
#endif
|
2001-06-06 03:26:22 +08:00
|
|
|
} else if (STREQ("suffix", f, fn)) {
|
|
|
|
if ((b = strrchr(buf, '.')) != NULL)
|
|
|
|
b++;
|
|
|
|
} else if (STREQ("expand", f, fn)) {
|
|
|
|
b = buf;
|
|
|
|
} else if (STREQ("verbose", f, fn)) {
|
|
|
|
if (negate)
|
|
|
|
b = (rpmIsVerbose() ? NULL : buf);
|
|
|
|
else
|
|
|
|
b = (rpmIsVerbose() ? buf : NULL);
|
|
|
|
} else if (STREQ("url2path", f, fn) || STREQ("u2p", f, fn)) {
|
|
|
|
(void)urlPath(buf, (const char **)&b);
|
2002-05-17 00:55:21 +08:00
|
|
|
/*@-branchstate@*/
|
2001-06-06 03:26:22 +08:00
|
|
|
if (*b == '\0') b = "/";
|
2002-05-17 00:55:21 +08:00
|
|
|
/*@=branchstate@*/
|
2001-06-06 03:26:22 +08:00
|
|
|
} else if (STREQ("uncompress", f, fn)) {
|
|
|
|
rpmCompressedMagic compressed = COMPRESSED_OTHER;
|
2002-01-19 06:51:30 +08:00
|
|
|
/*@-globs@*/
|
2001-06-06 03:26:22 +08:00
|
|
|
for (b = buf; (c = *b) && isblank(c);)
|
|
|
|
b++;
|
|
|
|
for (be = b; (c = *be) && !isblank(c);)
|
|
|
|
be++;
|
2002-01-19 06:51:30 +08:00
|
|
|
/*@=globs@*/
|
2001-06-06 03:26:22 +08:00
|
|
|
*be++ = '\0';
|
1998-07-09 01:50:48 +08:00
|
|
|
#ifndef DEBUG_MACROS
|
2001-06-06 03:26:22 +08:00
|
|
|
(void) isCompressed(b, &compressed);
|
1998-07-09 01:50:48 +08:00
|
|
|
#endif
|
2001-06-06 03:26:22 +08:00
|
|
|
switch(compressed) {
|
|
|
|
default:
|
|
|
|
case 0: /* COMPRESSED_NOT */
|
|
|
|
sprintf(be, "%%_cat %s", b);
|
|
|
|
break;
|
|
|
|
case 1: /* COMPRESSED_OTHER */
|
|
|
|
sprintf(be, "%%_gzip -dc %s", b);
|
|
|
|
break;
|
|
|
|
case 2: /* COMPRESSED_BZIP2 */
|
|
|
|
sprintf(be, "%%_bzip2 %s", b);
|
|
|
|
break;
|
|
|
|
case 3: /* COMPRESSED_ZIP */
|
|
|
|
sprintf(be, "%%_unzip %s", b);
|
|
|
|
break;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2001-06-06 03:26:22 +08:00
|
|
|
b = be;
|
|
|
|
} else if (STREQ("S", f, fn)) {
|
|
|
|
for (b = buf; (c = *b) && xisdigit(c);)
|
|
|
|
b++;
|
|
|
|
if (!c) { /* digit index */
|
|
|
|
b++;
|
|
|
|
sprintf(b, "%%SOURCE%s", buf);
|
|
|
|
} else
|
|
|
|
b = buf;
|
|
|
|
} else if (STREQ("P", f, fn)) {
|
|
|
|
for (b = buf; (c = *b) && xisdigit(c);)
|
|
|
|
b++;
|
|
|
|
if (!c) { /* digit index */
|
|
|
|
b++;
|
|
|
|
sprintf(b, "%%PATCH%s", buf);
|
|
|
|
} else
|
|
|
|
b = buf;
|
|
|
|
} else if (STREQ("F", f, fn)) {
|
|
|
|
b = buf + strlen(buf) + 1;
|
|
|
|
sprintf(b, "file%s.file", buf);
|
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
if (b) {
|
|
|
|
(void) expandT(mb, b, strlen(b));
|
|
|
|
}
|
1997-03-31 22:13:21 +08:00
|
|
|
}
|
|
|
|
|
2000-09-02 05:15:40 +08:00
|
|
|
/**
|
2000-09-30 03:50:29 +08:00
|
|
|
* The main macro recursion loop.
|
2000-09-02 05:15:40 +08:00
|
|
|
* @todo Dynamically reallocate target buffer.
|
2000-09-30 03:50:29 +08:00
|
|
|
* @param mb macro expansion state
|
|
|
|
* @return 0 on success, 1 on failure
|
2000-09-02 05:15:40 +08:00
|
|
|
*/
|
1998-07-09 01:50:48 +08:00
|
|
|
static int
|
2001-06-06 03:26:22 +08:00
|
|
|
expandMacro(MacroBuf mb)
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@globals rpmGlobalMacroContext,
|
2003-05-09 04:39:29 +08:00
|
|
|
print_macro_trace, print_expand_trace, h_errno, fileSystem @*/
|
2001-10-18 00:43:36 +08:00
|
|
|
/*@modifies mb, rpmGlobalMacroContext,
|
2002-06-22 02:00:50 +08:00
|
|
|
print_macro_trace, print_expand_trace, fileSystem @*/
|
1997-03-31 22:13:21 +08:00
|
|
|
{
|
2001-05-04 05:00:18 +08:00
|
|
|
MacroEntry *mep;
|
|
|
|
MacroEntry me;
|
1998-07-09 01:50:48 +08:00
|
|
|
const char *s = mb->s, *se;
|
|
|
|
const char *f, *fe;
|
|
|
|
const char *g, *ge;
|
|
|
|
size_t fn, gn;
|
|
|
|
char *t = mb->t; /* save expansion pointer for printExpand */
|
|
|
|
int c;
|
|
|
|
int rc = 0;
|
|
|
|
int negate;
|
2003-12-30 23:12:50 +08:00
|
|
|
const char * lastc;
|
1998-09-06 07:13:35 +08:00
|
|
|
int chkexist;
|
1997-03-31 22:13:21 +08:00
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
if (++mb->depth > max_macro_depth) {
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_BADSPEC,
|
|
|
|
_("Recursion depth(%d) greater than max(%d)\n"),
|
1998-07-09 01:50:48 +08:00
|
|
|
mb->depth, max_macro_depth);
|
|
|
|
mb->depth--;
|
|
|
|
mb->expand_trace = 1;
|
|
|
|
return 1;
|
1997-03-31 22:13:21 +08:00
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2002-05-17 00:55:21 +08:00
|
|
|
/*@-branchstate@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
while (rc == 0 && mb->nb > 0 && (c = *s) != '\0') {
|
|
|
|
s++;
|
|
|
|
/* Copy text until next macro */
|
|
|
|
switch(c) {
|
|
|
|
case '%':
|
|
|
|
if (*s != '%')
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@switchbreak@*/ break;
|
1998-07-09 01:50:48 +08:00
|
|
|
s++; /* skip first % in %% */
|
2000-11-01 00:18:34 +08:00
|
|
|
/*@fallthrough@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
default:
|
|
|
|
SAVECHAR(mb, c);
|
|
|
|
continue;
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@notreached@*/ /*@switchbreak@*/ break;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Expand next macro */
|
|
|
|
f = fe = NULL;
|
|
|
|
g = ge = NULL;
|
|
|
|
if (mb->depth > 1) /* XXX full expansion for outermost level */
|
|
|
|
t = mb->t; /* save expansion pointer for printExpand */
|
|
|
|
negate = 0;
|
2003-12-30 23:12:50 +08:00
|
|
|
lastc = NULL;
|
1998-09-06 07:13:35 +08:00
|
|
|
chkexist = 0;
|
1998-07-09 01:50:48 +08:00
|
|
|
switch ((c = *s)) {
|
|
|
|
default: /* %name substitution */
|
1998-09-06 07:13:35 +08:00
|
|
|
while (strchr("!?", *s) != NULL) {
|
|
|
|
switch(*s++) {
|
|
|
|
case '!':
|
2000-11-01 00:18:34 +08:00
|
|
|
negate = ((negate + 1) % 2);
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@switchbreak@*/ break;
|
1998-09-06 07:13:35 +08:00
|
|
|
case '?':
|
|
|
|
chkexist++;
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@switchbreak@*/ break;
|
1998-09-06 07:13:35 +08:00
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
|
|
|
f = se = s;
|
|
|
|
if (*se == '-')
|
|
|
|
se++;
|
2001-04-29 09:05:43 +08:00
|
|
|
while((c = *se) && (xisalnum(c) || c == '_'))
|
1998-07-09 01:50:48 +08:00
|
|
|
se++;
|
1999-07-29 20:36:03 +08:00
|
|
|
/* Recognize non-alnum macros too */
|
|
|
|
switch (*se) {
|
|
|
|
case '*':
|
|
|
|
se++;
|
|
|
|
if (*se == '*') se++;
|
2001-10-14 03:35:58 +08:00
|
|
|
/*@innerbreak@*/ break;
|
1999-07-29 20:36:03 +08:00
|
|
|
case '#':
|
1998-07-09 01:50:48 +08:00
|
|
|
se++;
|
2001-10-14 03:35:58 +08:00
|
|
|
/*@innerbreak@*/ break;
|
1999-07-29 20:36:03 +08:00
|
|
|
default:
|
2001-10-14 03:35:58 +08:00
|
|
|
/*@innerbreak@*/ break;
|
1999-07-29 20:36:03 +08:00
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
fe = se;
|
|
|
|
/* For "%name " macros ... */
|
2002-01-19 06:51:30 +08:00
|
|
|
/*@-globs@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
if ((c = *fe) && isblank(c))
|
2003-12-30 23:12:50 +08:00
|
|
|
if ((lastc = strchr(fe,'\n')) == NULL)
|
|
|
|
lastc = strchr(fe, '\0');
|
2002-01-19 06:51:30 +08:00
|
|
|
/*@=globs@*/
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@switchbreak@*/ break;
|
1998-07-09 01:50:48 +08:00
|
|
|
case '(': /* %(...) shell escape */
|
|
|
|
if ((se = matchchar(s, c, ')')) == NULL) {
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_BADSPEC,
|
2001-05-01 06:32:22 +08:00
|
|
|
_("Unterminated %c: %s\n"), (char)c, s);
|
1998-07-09 01:50:48 +08:00
|
|
|
rc = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (mb->macro_trace)
|
|
|
|
printMacro(mb, s, se+1);
|
|
|
|
|
|
|
|
s++; /* skip ( */
|
|
|
|
rc = doShellEscape(mb, s, (se - s));
|
|
|
|
se++; /* skip ) */
|
|
|
|
|
|
|
|
s = se;
|
|
|
|
continue;
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@notreached@*/ /*@switchbreak@*/ break;
|
1998-07-09 01:50:48 +08:00
|
|
|
case '{': /* %{...}/%{...:...} substitution */
|
|
|
|
if ((se = matchchar(s, c, '}')) == NULL) {
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_BADSPEC,
|
2001-05-01 06:32:22 +08:00
|
|
|
_("Unterminated %c: %s\n"), (char)c, s);
|
1998-07-09 01:50:48 +08:00
|
|
|
rc = 1;
|
|
|
|
continue;
|
|
|
|
}
|
1998-12-03 03:55:36 +08:00
|
|
|
f = s+1;/* skip { */
|
1998-07-09 01:50:48 +08:00
|
|
|
se++; /* skip } */
|
1998-09-06 07:13:35 +08:00
|
|
|
while (strchr("!?", *f) != NULL) {
|
|
|
|
switch(*f++) {
|
|
|
|
case '!':
|
2000-11-01 00:18:34 +08:00
|
|
|
negate = ((negate + 1) % 2);
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@switchbreak@*/ break;
|
1998-09-06 07:13:35 +08:00
|
|
|
case '?':
|
|
|
|
chkexist++;
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@switchbreak@*/ break;
|
1998-09-06 07:13:35 +08:00
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
1999-07-29 20:36:03 +08:00
|
|
|
for (fe = f; (c = *fe) && !strchr(" :}", c);)
|
1998-07-09 01:50:48 +08:00
|
|
|
fe++;
|
1999-07-29 20:36:03 +08:00
|
|
|
switch (c) {
|
|
|
|
case ':':
|
1998-07-09 01:50:48 +08:00
|
|
|
g = fe + 1;
|
|
|
|
ge = se - 1;
|
2001-10-14 03:35:58 +08:00
|
|
|
/*@innerbreak@*/ break;
|
1999-07-29 20:36:03 +08:00
|
|
|
case ' ':
|
2003-12-30 23:12:50 +08:00
|
|
|
lastc = se-1;
|
2001-10-14 03:35:58 +08:00
|
|
|
/*@innerbreak@*/ break;
|
1999-07-29 20:36:03 +08:00
|
|
|
default:
|
2001-10-14 03:35:58 +08:00
|
|
|
/*@innerbreak@*/ break;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@switchbreak@*/ break;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX Everything below expects fe > f */
|
|
|
|
fn = (fe - f);
|
|
|
|
gn = (ge - g);
|
2001-09-21 23:07:11 +08:00
|
|
|
if ((fe - f) <= 0) {
|
1999-04-21 03:17:23 +08:00
|
|
|
/* XXX Process % in unknown context */
|
|
|
|
c = '%'; /* XXX only need to save % */
|
|
|
|
SAVECHAR(mb, c);
|
|
|
|
#if 0
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_BADSPEC,
|
|
|
|
_("A %% is followed by an unparseable macro\n"));
|
1999-04-21 03:17:23 +08:00
|
|
|
#endif
|
1998-07-09 01:50:48 +08:00
|
|
|
s = se;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mb->macro_trace)
|
|
|
|
printMacro(mb, s, se);
|
|
|
|
|
|
|
|
/* Expand builtin macros */
|
|
|
|
if (STREQ("global", f, fn)) {
|
1998-08-02 23:14:38 +08:00
|
|
|
s = doDefine(mb, se, RMIL_GLOBAL, 1);
|
1998-07-09 01:50:48 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (STREQ("define", f, fn)) {
|
|
|
|
s = doDefine(mb, se, mb->depth, 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (STREQ("undefine", f, fn)) {
|
|
|
|
s = doUndefine(mb->mc, se);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (STREQ("echo", f, fn) ||
|
|
|
|
STREQ("warn", f, fn) ||
|
|
|
|
STREQ("error", f, fn)) {
|
|
|
|
int waserror = 0;
|
|
|
|
if (STREQ("error", f, fn))
|
|
|
|
waserror = 1;
|
2003-04-02 06:20:45 +08:00
|
|
|
if (g != NULL && g < ge)
|
1998-07-09 01:50:48 +08:00
|
|
|
doOutput(mb, waserror, g, gn);
|
|
|
|
else
|
|
|
|
doOutput(mb, waserror, f, fn);
|
|
|
|
s = se;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (STREQ("trace", f, fn)) {
|
|
|
|
/* XXX TODO restore expand_trace/macro_trace to 0 on return */
|
|
|
|
mb->expand_trace = mb->macro_trace = (negate ? 0 : mb->depth);
|
|
|
|
if (mb->depth == 1) {
|
|
|
|
print_macro_trace = mb->macro_trace;
|
|
|
|
print_expand_trace = mb->expand_trace;
|
|
|
|
}
|
|
|
|
s = se;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (STREQ("dump", f, fn)) {
|
1999-12-13 05:14:05 +08:00
|
|
|
rpmDumpMacroTable(mb->mc, NULL);
|
1999-08-17 02:57:37 +08:00
|
|
|
while (iseol(*se))
|
1998-07-09 01:50:48 +08:00
|
|
|
se++;
|
|
|
|
s = se;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-03-20 04:08:20 +08:00
|
|
|
if (STREQ("lua", f, fn)) {
|
2004-03-25 03:47:11 +08:00
|
|
|
rpmlua lua = NULL; /* Global state. */
|
2004-03-20 04:08:20 +08:00
|
|
|
const char *ls = s+sizeof("{lua:")-1;
|
|
|
|
const char *lse = se-sizeof("}")+1;
|
|
|
|
char *scriptbuf = (char *)xmalloc((lse-ls)+1);
|
|
|
|
const char *printbuf;
|
|
|
|
memcpy(scriptbuf, ls, lse-ls);
|
|
|
|
scriptbuf[lse-ls] = '\0';
|
|
|
|
rpmluaSetPrintBuffer(lua, 1);
|
|
|
|
if (rpmluaRunScript(lua, scriptbuf, NULL) == -1)
|
|
|
|
rc = 1;
|
|
|
|
printbuf = rpmluaGetPrintBuffer(lua);
|
|
|
|
if (printbuf) {
|
|
|
|
int len = strlen(printbuf);
|
|
|
|
if (len > mb->nb)
|
|
|
|
len = mb->nb;
|
|
|
|
memcpy(mb->t, printbuf, len);
|
|
|
|
mb->t += len;
|
|
|
|
mb->nb -= len;
|
|
|
|
}
|
2004-03-25 03:47:11 +08:00
|
|
|
rpmluaSetPrintBuffer(lua, 0);
|
2004-03-20 04:08:20 +08:00
|
|
|
free(scriptbuf);
|
|
|
|
s = se;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
/* XXX necessary but clunky */
|
|
|
|
if (STREQ("basename", f, fn) ||
|
|
|
|
STREQ("suffix", f, fn) ||
|
|
|
|
STREQ("expand", f, fn) ||
|
1999-11-20 02:19:41 +08:00
|
|
|
STREQ("verbose", f, fn) ||
|
1998-07-09 01:50:48 +08:00
|
|
|
STREQ("uncompress", f, fn) ||
|
1999-11-20 02:19:41 +08:00
|
|
|
STREQ("url2path", f, fn) ||
|
|
|
|
STREQ("u2p", f, fn) ||
|
1998-07-09 01:50:48 +08:00
|
|
|
STREQ("S", f, fn) ||
|
|
|
|
STREQ("P", f, fn) ||
|
|
|
|
STREQ("F", f, fn)) {
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@-internalglobs@*/ /* FIX: verbose may be set */
|
1999-11-20 02:19:41 +08:00
|
|
|
doFoo(mb, negate, f, fn, g, gn);
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@=internalglobs@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
s = se;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Expand defined macros */
|
|
|
|
mep = findEntry(mb->mc, f, fn);
|
|
|
|
me = (mep ? *mep : NULL);
|
|
|
|
|
|
|
|
/* XXX Special processing for flags */
|
|
|
|
if (*f == '-') {
|
|
|
|
if (me)
|
|
|
|
me->used++; /* Mark macro as used */
|
|
|
|
if ((me == NULL && !negate) || /* Without -f, skip %{-f...} */
|
|
|
|
(me != NULL && negate)) { /* With -f, skip %{!-f...} */
|
|
|
|
s = se;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g && g < ge) { /* Expand X in %{-f:X} */
|
|
|
|
rc = expandT(mb, g, gn);
|
|
|
|
} else
|
2001-05-04 05:00:18 +08:00
|
|
|
if (me && me->body && *me->body) {/* Expand %{-f}/%{-f*} */
|
1998-07-09 01:50:48 +08:00
|
|
|
rc = expandT(mb, me->body, strlen(me->body));
|
|
|
|
}
|
|
|
|
s = se;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1998-09-06 07:13:35 +08:00
|
|
|
/* XXX Special processing for macro existence */
|
|
|
|
if (chkexist) {
|
|
|
|
if ((me == NULL && !negate) || /* Without -f, skip %{?f...} */
|
|
|
|
(me != NULL && negate)) { /* With -f, skip %{!?f...} */
|
|
|
|
s = se;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (g && g < ge) { /* Expand X in %{?f:X} */
|
|
|
|
rc = expandT(mb, g, gn);
|
|
|
|
} else
|
2000-02-21 18:32:01 +08:00
|
|
|
if (me && me->body && *me->body) { /* Expand %{?f}/%{?f*} */
|
1998-09-06 07:13:35 +08:00
|
|
|
rc = expandT(mb, me->body, strlen(me->body));
|
|
|
|
}
|
|
|
|
s = se;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
if (me == NULL) { /* leave unknown %... as is */
|
|
|
|
#ifndef HACK
|
|
|
|
#if DEAD
|
|
|
|
/* XXX hack to skip over empty arg list */
|
|
|
|
if (fn == 1 && *f == '*') {
|
|
|
|
s = se;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* XXX hack to permit non-overloaded %foo to be passed */
|
|
|
|
c = '%'; /* XXX only need to save % */
|
|
|
|
SAVECHAR(mb, c);
|
|
|
|
#else
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_BADSPEC,
|
|
|
|
_("Macro %%%.*s not found, skipping\n"), fn, f);
|
1998-07-09 01:50:48 +08:00
|
|
|
s = se;
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup args for "%name " macros with opts */
|
|
|
|
if (me && me->opts != NULL) {
|
2003-12-30 23:12:50 +08:00
|
|
|
if (lastc != NULL) {
|
|
|
|
se = grabArgs(mb, me, fe, lastc);
|
1999-07-29 20:36:03 +08:00
|
|
|
} else {
|
|
|
|
addMacro(mb->mc, "**", NULL, "", mb->depth);
|
|
|
|
addMacro(mb->mc, "*", NULL, "", mb->depth);
|
|
|
|
addMacro(mb->mc, "#", NULL, "0", mb->depth);
|
|
|
|
addMacro(mb->mc, "0", NULL, me->name, mb->depth);
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recursively expand body of macro */
|
1999-03-28 08:47:40 +08:00
|
|
|
if (me->body && *me->body) {
|
|
|
|
mb->s = me->body;
|
|
|
|
rc = expandMacro(mb);
|
|
|
|
if (rc == 0)
|
|
|
|
me->used++; /* Mark macro as used */
|
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
|
|
|
|
/* Free args for "%name " macros with opts */
|
|
|
|
if (me->opts != NULL)
|
|
|
|
freeArgs(mb);
|
|
|
|
|
|
|
|
s = se;
|
1997-03-31 22:13:21 +08:00
|
|
|
}
|
2002-05-17 00:55:21 +08:00
|
|
|
/*@=branchstate@*/
|
1997-03-31 22:13:21 +08:00
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
*mb->t = '\0';
|
|
|
|
mb->s = s;
|
|
|
|
mb->depth--;
|
|
|
|
if (rc != 0 || mb->expand_trace)
|
|
|
|
printExpansion(mb, t, mb->t);
|
|
|
|
return rc;
|
|
|
|
}
|
1997-03-31 22:13:21 +08:00
|
|
|
|
2004-03-02 09:31:01 +08:00
|
|
|
/* =============================================================== */
|
|
|
|
/* XXX dupe'd to avoid change in linkage conventions. */
|
|
|
|
|
|
|
|
#define POPT_ERROR_NOARG -10 /*!< missing argument */
|
|
|
|
#define POPT_ERROR_BADQUOTE -15 /*!< error in paramter quoting */
|
|
|
|
#define POPT_ERROR_MALLOC -21 /*!< memory allocation failed */
|
|
|
|
|
|
|
|
#define POPT_ARGV_ARRAY_GROW_DELTA 5
|
|
|
|
|
|
|
|
/*@-boundswrite@*/
|
2004-03-27 01:27:57 +08:00
|
|
|
static int XpoptDupArgv(int argc, const char **argv,
|
2004-03-02 09:31:01 +08:00
|
|
|
int * argcPtr, const char *** argvPtr)
|
2004-03-23 15:18:55 +08:00
|
|
|
/*@modifies *argcPtr, *argvPtr @*/
|
2004-03-02 09:31:01 +08:00
|
|
|
{
|
|
|
|
size_t nb = (argc + 1) * sizeof(*argv);
|
|
|
|
const char ** argv2;
|
|
|
|
char * dst;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (argc <= 0 || argv == NULL) /* XXX can't happen */
|
|
|
|
return POPT_ERROR_NOARG;
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (argv[i] == NULL)
|
|
|
|
return POPT_ERROR_NOARG;
|
|
|
|
nb += strlen(argv[i]) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst = malloc(nb);
|
|
|
|
if (dst == NULL) /* XXX can't happen */
|
|
|
|
return POPT_ERROR_MALLOC;
|
|
|
|
argv2 = (void *) dst;
|
|
|
|
dst += (argc + 1) * sizeof(*argv);
|
|
|
|
|
|
|
|
/*@-branchstate@*/
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
argv2[i] = dst;
|
|
|
|
dst += strlen(strcpy(dst, argv[i])) + 1;
|
|
|
|
}
|
|
|
|
/*@=branchstate@*/
|
|
|
|
argv2[argc] = NULL;
|
|
|
|
|
|
|
|
if (argvPtr) {
|
|
|
|
*argvPtr = argv2;
|
|
|
|
} else {
|
|
|
|
free(argv2);
|
|
|
|
argv2 = NULL;
|
|
|
|
}
|
|
|
|
if (argcPtr)
|
|
|
|
*argcPtr = argc;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*@=boundswrite@*/
|
|
|
|
|
|
|
|
/*@-bounds@*/
|
2004-03-27 01:27:57 +08:00
|
|
|
static int XpoptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr)
|
2004-03-23 15:18:55 +08:00
|
|
|
/*@modifies *argcPtr, *argvPtr @*/
|
2004-03-02 09:31:01 +08:00
|
|
|
{
|
|
|
|
const char * src;
|
|
|
|
char quote = '\0';
|
|
|
|
int argvAlloced = POPT_ARGV_ARRAY_GROW_DELTA;
|
|
|
|
const char ** argv = malloc(sizeof(*argv) * argvAlloced);
|
|
|
|
int argc = 0;
|
|
|
|
int buflen = strlen(s) + 1;
|
|
|
|
char * buf = memset(alloca(buflen), 0, buflen);
|
|
|
|
int rc = POPT_ERROR_MALLOC;
|
|
|
|
|
|
|
|
if (argv == NULL) return rc;
|
|
|
|
argv[argc] = buf;
|
|
|
|
|
|
|
|
for (src = s; *src != '\0'; src++) {
|
|
|
|
if (quote == *src) {
|
|
|
|
quote = '\0';
|
|
|
|
} else if (quote != '\0') {
|
|
|
|
if (*src == '\\') {
|
|
|
|
src++;
|
|
|
|
if (!*src) {
|
|
|
|
rc = POPT_ERROR_BADQUOTE;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (*src != quote) *buf++ = '\\';
|
|
|
|
}
|
|
|
|
*buf++ = *src;
|
|
|
|
} else if (isspace(*src)) {
|
|
|
|
if (*argv[argc] != '\0') {
|
|
|
|
buf++, argc++;
|
|
|
|
if (argc == argvAlloced) {
|
|
|
|
argvAlloced += POPT_ARGV_ARRAY_GROW_DELTA;
|
|
|
|
argv = realloc(argv, sizeof(*argv) * argvAlloced);
|
|
|
|
if (argv == NULL) goto exit;
|
|
|
|
}
|
|
|
|
argv[argc] = buf;
|
|
|
|
}
|
|
|
|
} else switch (*src) {
|
|
|
|
case '"':
|
|
|
|
case '\'':
|
|
|
|
quote = *src;
|
|
|
|
/*@switchbreak@*/ break;
|
|
|
|
case '\\':
|
|
|
|
src++;
|
|
|
|
if (!*src) {
|
|
|
|
rc = POPT_ERROR_BADQUOTE;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
/*@fallthrough@*/
|
|
|
|
default:
|
|
|
|
*buf++ = *src;
|
|
|
|
/*@switchbreak@*/ break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(argv[argc])) {
|
|
|
|
argc++, buf++;
|
|
|
|
}
|
|
|
|
|
2004-03-27 01:27:57 +08:00
|
|
|
rc = XpoptDupArgv(argc, argv, argcPtr, argvPtr);
|
2004-03-02 09:31:01 +08:00
|
|
|
|
|
|
|
exit:
|
|
|
|
if (argv) free(argv);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
/*@=bounds@*/
|
|
|
|
/* =============================================================== */
|
2004-03-23 15:18:55 +08:00
|
|
|
/*@unchecked@*/
|
2004-03-02 09:31:01 +08:00
|
|
|
static int _debug = 0;
|
|
|
|
|
|
|
|
int rpmGlob(const char * patterns, int * argcPtr, const char *** argvPtr)
|
|
|
|
{
|
|
|
|
int ac = 0;
|
|
|
|
const char ** av = NULL;
|
|
|
|
int argc = 0;
|
|
|
|
const char ** argv = NULL;
|
|
|
|
char * globRoot = NULL;
|
2004-03-30 08:40:26 +08:00
|
|
|
#ifdef ENABLE_NLS
|
2004-04-08 02:07:08 +08:00
|
|
|
const char * old_collate = NULL;
|
|
|
|
const char * old_ctype = NULL;
|
|
|
|
const char * t;
|
2004-03-30 08:40:26 +08:00
|
|
|
#endif
|
|
|
|
size_t maxb, nb;
|
2004-03-02 09:31:01 +08:00
|
|
|
int i, j;
|
|
|
|
int rc;
|
|
|
|
|
2004-03-27 01:27:57 +08:00
|
|
|
rc = XpoptParseArgvString(patterns, &ac, &av);
|
2004-03-02 09:31:01 +08:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
2004-03-30 08:40:26 +08:00
|
|
|
#ifdef ENABLE_NLS
|
2004-04-08 02:07:08 +08:00
|
|
|
/*@-branchstate@*/
|
2004-03-30 08:40:26 +08:00
|
|
|
t = setlocale(LC_COLLATE, NULL);
|
|
|
|
if (t)
|
2004-04-08 02:07:08 +08:00
|
|
|
old_collate = xstrdup(t);
|
2004-03-30 08:40:26 +08:00
|
|
|
t = setlocale(LC_CTYPE, NULL);
|
|
|
|
if (t)
|
2004-04-08 02:07:08 +08:00
|
|
|
old_ctype = xstrdup(t);
|
|
|
|
/*@=branchstate@*/
|
|
|
|
(void) setlocale(LC_COLLATE, "C");
|
|
|
|
(void) setlocale(LC_CTYPE, "C");
|
2004-03-30 08:40:26 +08:00
|
|
|
#endif
|
|
|
|
|
2004-03-23 15:18:55 +08:00
|
|
|
if (av != NULL)
|
2004-03-02 09:31:01 +08:00
|
|
|
for (j = 0; j < ac; j++) {
|
|
|
|
const char * globURL;
|
|
|
|
const char * path;
|
|
|
|
int ut = urlPath(av[j], &path);
|
|
|
|
glob_t gl;
|
|
|
|
|
|
|
|
if (!Glob_pattern_p(av[j], 0) && strchr(path, '~') == NULL) {
|
|
|
|
argv = xrealloc(argv, (argc+2) * sizeof(*argv));
|
|
|
|
argv[argc] = xstrdup(av[j]);
|
|
|
|
if (_debug)
|
|
|
|
fprintf(stderr, "*** rpmGlob argv[%d] \"%s\"\n", argc, argv[argc]);
|
|
|
|
argc++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
gl.gl_pathc = 0;
|
|
|
|
gl.gl_pathv = NULL;
|
|
|
|
rc = Glob(av[j], GLOB_TILDE, Glob_error, &gl);
|
|
|
|
if (rc)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
/* XXX Prepend the URL leader for globs that have stripped it off */
|
|
|
|
maxb = 0;
|
|
|
|
for (i = 0; i < gl.gl_pathc; i++) {
|
|
|
|
if ((nb = strlen(&(gl.gl_pathv[i][0]))) > maxb)
|
|
|
|
maxb = nb;
|
|
|
|
}
|
|
|
|
|
|
|
|
nb = ((ut > URL_IS_DASH && ut != URL_IS_FTP) ? (path - av[j]) : 0);
|
|
|
|
maxb += nb;
|
|
|
|
maxb += 1;
|
|
|
|
globURL = globRoot = xmalloc(maxb);
|
|
|
|
|
|
|
|
switch (ut) {
|
|
|
|
case URL_IS_HTTP:
|
|
|
|
case URL_IS_PATH:
|
|
|
|
case URL_IS_DASH:
|
|
|
|
strncpy(globRoot, av[j], nb);
|
|
|
|
/*@switchbreak@*/ break;
|
|
|
|
case URL_IS_FTP:
|
|
|
|
case URL_IS_UNKNOWN:
|
|
|
|
/*@switchbreak@*/ break;
|
|
|
|
}
|
|
|
|
globRoot += nb;
|
|
|
|
*globRoot = '\0';
|
|
|
|
if (_debug)
|
|
|
|
fprintf(stderr, "*** GLOB maxb %d diskURL %d %*s globURL %p %s\n", (int)maxb, (int)nb, (int)nb, av[j], globURL, globURL);
|
|
|
|
|
|
|
|
argv = xrealloc(argv, (argc+gl.gl_pathc+1) * sizeof(*argv));
|
|
|
|
|
|
|
|
if (argv != NULL)
|
|
|
|
for (i = 0; i < gl.gl_pathc; i++) {
|
|
|
|
const char * globFile = &(gl.gl_pathv[i][0]);
|
|
|
|
if (globRoot > globURL && globRoot[-1] == '/')
|
|
|
|
while (*globFile == '/') globFile++;
|
|
|
|
strcpy(globRoot, globFile);
|
|
|
|
if (_debug)
|
|
|
|
fprintf(stderr, "*** rpmGlob argv[%d] \"%s\"\n", argc, globURL);
|
|
|
|
argv[argc++] = xstrdup(globURL);
|
|
|
|
}
|
|
|
|
/*@-immediatetrans@*/
|
|
|
|
Globfree(&gl);
|
|
|
|
/*@=immediatetrans@*/
|
|
|
|
globURL = _free(globURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv != NULL && argc > 0) {
|
|
|
|
argv[argc] = NULL;
|
|
|
|
if (argvPtr)
|
|
|
|
*argvPtr = argv;
|
|
|
|
if (argcPtr)
|
|
|
|
*argcPtr = argc;
|
|
|
|
rc = 0;
|
|
|
|
} else
|
|
|
|
rc = 1;
|
|
|
|
|
|
|
|
|
|
|
|
exit:
|
2004-03-30 08:40:26 +08:00
|
|
|
#ifdef ENABLE_NLS
|
2004-04-08 02:07:08 +08:00
|
|
|
/*@-branchstate@*/
|
2004-03-30 08:40:26 +08:00
|
|
|
if (old_collate) {
|
2004-04-08 02:07:08 +08:00
|
|
|
(void) setlocale(LC_COLLATE, old_collate);
|
|
|
|
old_collate = _free(old_collate);
|
2004-03-30 08:40:26 +08:00
|
|
|
}
|
|
|
|
if (old_ctype) {
|
2004-04-08 02:07:08 +08:00
|
|
|
(void) setlocale(LC_CTYPE, old_ctype);
|
|
|
|
old_ctype = _free(old_ctype);
|
2004-03-30 08:40:26 +08:00
|
|
|
}
|
2004-04-08 02:07:08 +08:00
|
|
|
/*@=branchstate@*/
|
2004-03-30 08:40:26 +08:00
|
|
|
#endif
|
|
|
|
av = _free(av);
|
2004-03-02 09:31:01 +08:00
|
|
|
/*@-branchstate@*/
|
|
|
|
if (rc || argvPtr == NULL) {
|
|
|
|
/*@-dependenttrans -unqualifiedtrans@*/
|
|
|
|
if (argv != NULL)
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
argv[i] = _free(argv[i]);
|
|
|
|
argv = _free(argv);
|
|
|
|
/*@=dependenttrans =unqualifiedtrans@*/
|
|
|
|
}
|
|
|
|
/*@=branchstate@*/
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
/* =============================================================== */
|
1999-03-28 08:47:40 +08:00
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
int
|
2001-10-16 01:53:34 +08:00
|
|
|
expandMacros(void * spec, MacroContext mc, char * sbuf, size_t slen)
|
1997-03-31 22:13:21 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
MacroBuf mb = alloca(sizeof(*mb));
|
|
|
|
char *tbuf;
|
|
|
|
int rc;
|
1997-03-31 22:13:21 +08:00
|
|
|
|
2001-10-16 01:53:34 +08:00
|
|
|
if (sbuf == NULL || slen == 0)
|
2001-06-06 03:26:22 +08:00
|
|
|
return 0;
|
2001-10-15 11:22:10 +08:00
|
|
|
if (mc == NULL) mc = rpmGlobalMacroContext;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
tbuf = alloca(slen + 1);
|
|
|
|
memset(tbuf, 0, (slen + 1));
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-10-16 01:53:34 +08:00
|
|
|
mb->s = sbuf;
|
2001-06-06 03:26:22 +08:00
|
|
|
mb->t = tbuf;
|
|
|
|
mb->nb = slen;
|
|
|
|
mb->depth = 0;
|
|
|
|
mb->macro_trace = print_macro_trace;
|
|
|
|
mb->expand_trace = print_expand_trace;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
mb->spec = spec; /* (future) %file expansion info */
|
|
|
|
mb->mc = mc;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
rc = expandMacro(mb);
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-09-21 23:07:11 +08:00
|
|
|
if (mb->nb == 0)
|
2001-06-06 03:26:22 +08:00
|
|
|
rpmError(RPMERR_BADSPEC, _("Target buffer overflow\n"));
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
tbuf[slen] = '\0'; /* XXX just in case */
|
2001-10-16 01:53:34 +08:00
|
|
|
strncpy(sbuf, tbuf, (slen - mb->nb + 1));
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
return rc;
|
1997-03-31 22:13:21 +08:00
|
|
|
}
|
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
void
|
2001-06-06 03:26:22 +08:00
|
|
|
addMacro(MacroContext mc,
|
|
|
|
const char * n, const char * o, const char * b, int level)
|
1997-03-31 22:13:21 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
MacroEntry * mep;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-10-15 11:22:10 +08:00
|
|
|
if (mc == NULL) mc = rpmGlobalMacroContext;
|
1999-04-02 06:26:44 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
/* If new name, expand macro table */
|
|
|
|
if ((mep = findEntry(mc, n, 0)) == NULL) {
|
|
|
|
if (mc->firstFree == mc->macrosAllocated)
|
|
|
|
expandMacroTable(mc);
|
|
|
|
if (mc->macroTable != NULL)
|
|
|
|
mep = mc->macroTable + mc->firstFree++;
|
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
if (mep != NULL) {
|
1998-07-09 01:50:48 +08:00
|
|
|
/* Push macro over previous definition */
|
|
|
|
pushMacro(mep, n, o, b, level);
|
|
|
|
|
|
|
|
/* If new name, sort macro table */
|
|
|
|
if ((*mep)->prev == NULL)
|
2001-06-06 03:26:22 +08:00
|
|
|
sortMacroTable(mc);
|
|
|
|
}
|
1997-03-31 22:13:21 +08:00
|
|
|
}
|
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
void
|
2001-06-06 03:26:22 +08:00
|
|
|
delMacro(MacroContext mc, const char * n)
|
1997-03-31 22:13:21 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
MacroEntry * mep;
|
|
|
|
|
2001-10-15 11:22:10 +08:00
|
|
|
if (mc == NULL) mc = rpmGlobalMacroContext;
|
2001-06-06 03:26:22 +08:00
|
|
|
/* If name exists, pop entry */
|
|
|
|
if ((mep = findEntry(mc, n, 0)) != NULL) {
|
|
|
|
popMacro(mep);
|
|
|
|
/* If deleted name, sort macro table */
|
|
|
|
if (!(mep && *mep))
|
|
|
|
sortMacroTable(mc);
|
|
|
|
}
|
1998-01-13 05:31:29 +08:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:24:21 +08:00
|
|
|
/*@-mustmod@*/ /* LCL: mc is modified through mb->mc, mb is abstract */
|
1999-03-28 08:47:40 +08:00
|
|
|
int
|
2001-06-06 03:26:22 +08:00
|
|
|
rpmDefineMacro(MacroContext mc, const char * macro, int level)
|
1999-03-28 08:47:40 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
MacroBuf mb = alloca(sizeof(*mb));
|
|
|
|
|
|
|
|
memset(mb, 0, sizeof(*mb));
|
|
|
|
/* XXX just enough to get by */
|
2001-10-15 11:22:10 +08:00
|
|
|
mb->mc = (mc ? mc : rpmGlobalMacroContext);
|
2001-10-19 09:24:21 +08:00
|
|
|
(void) doDefine(mb, macro, level, 0);
|
2001-06-06 03:26:22 +08:00
|
|
|
return 0;
|
1999-03-28 08:47:40 +08:00
|
|
|
}
|
2001-10-19 09:24:21 +08:00
|
|
|
/*@=mustmod@*/
|
1999-03-28 08:47:40 +08:00
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
void
|
2001-05-04 05:00:18 +08:00
|
|
|
rpmLoadMacros(MacroContext mc, int level)
|
1999-12-13 05:14:05 +08:00
|
|
|
{
|
|
|
|
|
2001-10-15 11:22:10 +08:00
|
|
|
if (mc == NULL || mc == rpmGlobalMacroContext)
|
2001-05-04 05:00:18 +08:00
|
|
|
return;
|
1999-12-13 05:14:05 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
if (mc->macroTable != NULL) {
|
|
|
|
int i;
|
1999-12-13 05:14:05 +08:00
|
|
|
for (i = 0; i < mc->firstFree; i++) {
|
2001-05-04 05:00:18 +08:00
|
|
|
MacroEntry *mep, me;
|
|
|
|
mep = &mc->macroTable[i];
|
|
|
|
me = *mep;
|
1999-12-13 05:14:05 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
if (me == NULL) /* XXX this should never happen */
|
|
|
|
continue;
|
|
|
|
addMacro(NULL, me->name, me->opts, me->body, (level - 1));
|
1999-12-13 05:14:05 +08:00
|
|
|
}
|
2001-05-04 05:00:18 +08:00
|
|
|
}
|
1999-12-13 05:14:05 +08:00
|
|
|
}
|
|
|
|
|
2004-03-02 09:31:01 +08:00
|
|
|
int
|
|
|
|
rpmLoadMacroFile(MacroContext mc, const char * fn)
|
|
|
|
{
|
|
|
|
FD_t fd = Fopen(fn, "r.fpio");
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
int rc = -1;
|
|
|
|
|
|
|
|
if (fd == NULL || Ferror(fd)) {
|
|
|
|
if (fd) (void) Fclose(fd);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX Assume new fangled macro expansion */
|
|
|
|
/*@-mods@*/
|
|
|
|
max_macro_depth = 16;
|
|
|
|
/*@=mods@*/
|
|
|
|
|
2004-03-23 15:18:55 +08:00
|
|
|
buf[0] = '\0';
|
2004-04-09 04:27:53 +08:00
|
|
|
while(rdcl(buf, sizeof(buf), fd) != NULL) {
|
2004-03-02 09:31:01 +08:00
|
|
|
char c, *n;
|
|
|
|
|
|
|
|
n = buf;
|
|
|
|
SKIPBLANK(n, c);
|
|
|
|
|
|
|
|
if (c != '%')
|
2004-03-23 15:18:55 +08:00
|
|
|
continue;
|
2004-03-02 09:31:01 +08:00
|
|
|
n++; /* skip % */
|
|
|
|
rc = rpmDefineMacro(mc, n, RMIL_MACROFILES);
|
|
|
|
}
|
|
|
|
rc = Fclose(fd);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1999-12-13 05:14:05 +08:00
|
|
|
void
|
2004-03-02 09:31:01 +08:00
|
|
|
rpmInitMacros(MacroContext mc, const char * macrofiles)
|
1998-07-09 01:50:48 +08:00
|
|
|
{
|
2004-03-02 09:31:01 +08:00
|
|
|
char *mfiles, *m, *me;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
if (macrofiles == NULL)
|
|
|
|
return;
|
2001-10-19 09:24:21 +08:00
|
|
|
#ifdef DYING
|
2001-10-15 11:22:10 +08:00
|
|
|
if (mc == NULL) mc = rpmGlobalMacroContext;
|
2001-10-19 09:24:21 +08:00
|
|
|
#endif
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2004-03-02 09:31:01 +08:00
|
|
|
mfiles = xstrdup(macrofiles);
|
|
|
|
for (m = mfiles; m && *m != '\0'; m = me) {
|
|
|
|
const char ** av;
|
|
|
|
int ac;
|
|
|
|
int i;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2004-03-02 09:31:01 +08:00
|
|
|
for (me = m; (me = strchr(me, ':')) != NULL; me++) {
|
|
|
|
/* Skip over URI's. */
|
2001-05-04 05:00:18 +08:00
|
|
|
if (!(me[1] == '/' && me[2] == '/'))
|
2001-06-06 03:26:22 +08:00
|
|
|
/*@innerbreak@*/ break;
|
2001-05-04 05:00:18 +08:00
|
|
|
}
|
1999-11-13 01:20:49 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
if (me && *me == ':')
|
|
|
|
*me++ = '\0';
|
|
|
|
else
|
2004-03-02 09:31:01 +08:00
|
|
|
me = m + strlen(m);
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2004-03-02 09:31:01 +08:00
|
|
|
/* Glob expand the macro file path element, expanding ~ to $HOME. */
|
|
|
|
ac = 0;
|
|
|
|
av = NULL;
|
|
|
|
i = rpmGlob(m, &ac, &av);
|
|
|
|
if (i != 0)
|
2001-05-04 05:00:18 +08:00
|
|
|
continue;
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2004-03-02 09:31:01 +08:00
|
|
|
/* Read macros from each file. */
|
|
|
|
for (i = 0; i < ac; i++)
|
|
|
|
(void) rpmLoadMacroFile(mc, av[i]);
|
|
|
|
av = _free(av);
|
2001-05-04 05:00:18 +08:00
|
|
|
}
|
2004-03-03 09:58:36 +08:00
|
|
|
mfiles = _free(mfiles);
|
1999-12-13 05:14:05 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
/* Reload cmdline macros */
|
2001-10-18 00:43:36 +08:00
|
|
|
/*@-mods@*/
|
2001-10-15 11:22:10 +08:00
|
|
|
rpmLoadMacros(rpmCLIMacroContext, RMIL_CMDLINE);
|
2001-10-18 00:43:36 +08:00
|
|
|
/*@=mods@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
1998-01-13 05:31:29 +08:00
|
|
|
|
2001-05-01 06:32:22 +08:00
|
|
|
/*@-globstate@*/
|
1998-07-09 01:50:48 +08:00
|
|
|
void
|
2001-05-04 05:00:18 +08:00
|
|
|
rpmFreeMacros(MacroContext mc)
|
1998-01-13 05:31:29 +08:00
|
|
|
{
|
|
|
|
|
2001-10-15 11:22:10 +08:00
|
|
|
if (mc == NULL) mc = rpmGlobalMacroContext;
|
1999-04-02 06:26:44 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
if (mc->macroTable != NULL) {
|
|
|
|
int i;
|
1998-07-09 01:50:48 +08:00
|
|
|
for (i = 0; i < mc->firstFree; i++) {
|
2001-05-04 05:00:18 +08:00
|
|
|
MacroEntry me;
|
|
|
|
while ((me = mc->macroTable[i]) != NULL) {
|
|
|
|
/* XXX cast to workaround const */
|
|
|
|
/*@-onlytrans@*/
|
|
|
|
if ((mc->macroTable[i] = me->prev) == NULL)
|
|
|
|
me->name = _free(me->name);
|
|
|
|
/*@=onlytrans@*/
|
|
|
|
me->opts = _free(me->opts);
|
|
|
|
me->body = _free(me->body);
|
|
|
|
me = _free(me);
|
|
|
|
}
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2001-05-04 05:00:18 +08:00
|
|
|
mc->macroTable = _free(mc->macroTable);
|
|
|
|
}
|
|
|
|
memset(mc, 0, sizeof(*mc));
|
1998-01-13 05:31:29 +08:00
|
|
|
}
|
2001-05-01 06:32:22 +08:00
|
|
|
/*@=globstate@*/
|
1998-01-13 05:31:29 +08:00
|
|
|
|
1998-07-09 01:50:48 +08:00
|
|
|
/* =============================================================== */
|
2001-06-06 03:26:22 +08:00
|
|
|
int isCompressed(const char * file, rpmCompressedMagic * compressed)
|
1998-08-01 04:11:49 +08:00
|
|
|
{
|
1998-11-19 05:41:05 +08:00
|
|
|
FD_t fd;
|
|
|
|
ssize_t nb;
|
1999-11-13 01:20:49 +08:00
|
|
|
int rc = -1;
|
1998-08-01 04:11:49 +08:00
|
|
|
unsigned char magic[4];
|
|
|
|
|
|
|
|
*compressed = COMPRESSED_NOT;
|
|
|
|
|
1999-11-13 01:20:49 +08:00
|
|
|
fd = Fopen(file, "r.ufdio");
|
|
|
|
if (fd == NULL || Ferror(fd)) {
|
1999-10-31 00:43:29 +08:00
|
|
|
/* XXX Fstrerror */
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_BADSPEC, _("File %s: %s\n"), file, Fstrerror(fd));
|
2001-05-01 06:32:22 +08:00
|
|
|
if (fd) (void) Fclose(fd);
|
1998-08-01 04:11:49 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2001-10-16 22:58:57 +08:00
|
|
|
nb = Fread(magic, sizeof(magic[0]), sizeof(magic), fd);
|
1998-09-11 00:25:15 +08:00
|
|
|
if (nb < 0) {
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_BADSPEC, _("File %s: %s\n"), file, Fstrerror(fd));
|
1999-11-13 01:20:49 +08:00
|
|
|
rc = 1;
|
1998-09-11 00:25:15 +08:00
|
|
|
} else if (nb < sizeof(magic)) {
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_BADSPEC, _("File %s is smaller than %u bytes\n"),
|
2000-12-03 05:53:44 +08:00
|
|
|
file, (unsigned)sizeof(magic));
|
1999-11-13 01:20:49 +08:00
|
|
|
rc = 0;
|
1998-08-01 04:11:49 +08:00
|
|
|
}
|
2001-05-01 06:32:22 +08:00
|
|
|
(void) Fclose(fd);
|
1999-11-13 01:20:49 +08:00
|
|
|
if (rc >= 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
rc = 0;
|
1998-08-01 04:11:49 +08:00
|
|
|
|
2000-11-03 01:57:25 +08:00
|
|
|
if ((magic[0] == 'B') && (magic[1] == 'Z')) {
|
|
|
|
*compressed = COMPRESSED_BZIP2;
|
|
|
|
} else if ((magic[0] == 0120) && (magic[1] == 0113) &&
|
|
|
|
(magic[2] == 0003) && (magic[3] == 0004)) { /* pkzip */
|
|
|
|
*compressed = COMPRESSED_ZIP;
|
|
|
|
} else if (((magic[0] == 0037) && (magic[1] == 0213)) || /* gzip */
|
|
|
|
((magic[0] == 0037) && (magic[1] == 0236)) || /* old gzip */
|
|
|
|
((magic[0] == 0037) && (magic[1] == 0036)) || /* pack */
|
|
|
|
((magic[0] == 0037) && (magic[1] == 0240)) || /* SCO lzh */
|
|
|
|
((magic[0] == 0037) && (magic[1] == 0235)) /* compress */
|
1998-08-01 04:11:49 +08:00
|
|
|
) {
|
|
|
|
*compressed = COMPRESSED_OTHER;
|
|
|
|
}
|
|
|
|
|
1999-11-13 01:20:49 +08:00
|
|
|
return rc;
|
1998-08-01 04:11:49 +08:00
|
|
|
}
|
|
|
|
|
1999-03-28 08:47:40 +08:00
|
|
|
/* =============================================================== */
|
2000-09-30 03:50:29 +08:00
|
|
|
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@-modfilesys@*/
|
1999-03-28 08:47:40 +08:00
|
|
|
char *
|
|
|
|
rpmExpand(const char *arg, ...)
|
|
|
|
{
|
|
|
|
char buf[BUFSIZ], *p, *pe;
|
|
|
|
const char *s;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (arg == NULL)
|
1999-09-21 11:22:53 +08:00
|
|
|
return xstrdup("");
|
1999-03-28 08:47:40 +08:00
|
|
|
|
2000-11-01 00:18:34 +08:00
|
|
|
buf[0] = '\0';
|
1999-03-28 08:47:40 +08:00
|
|
|
p = buf;
|
2000-06-22 07:28:50 +08:00
|
|
|
pe = stpcpy(p, arg);
|
1999-03-28 08:47:40 +08:00
|
|
|
|
|
|
|
va_start(ap, arg);
|
2000-06-22 07:28:50 +08:00
|
|
|
while ((s = va_arg(ap, const char *)) != NULL)
|
|
|
|
pe = stpcpy(pe, s);
|
1999-03-28 08:47:40 +08:00
|
|
|
va_end(ap);
|
2001-05-01 06:32:22 +08:00
|
|
|
(void) expandMacros(NULL, NULL, buf, sizeof(buf));
|
1999-10-30 00:06:01 +08:00
|
|
|
return xstrdup(buf);
|
1999-03-28 08:47:40 +08:00
|
|
|
}
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@=modfilesys@*/
|
1999-03-28 08:47:40 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
rpmExpandNumeric(const char *arg)
|
|
|
|
{
|
|
|
|
const char *val;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (arg == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
val = rpmExpand(arg, NULL);
|
|
|
|
if (!(val && *val != '%'))
|
|
|
|
rc = 0;
|
|
|
|
else if (*val == 'Y' || *val == 'y')
|
|
|
|
rc = 1;
|
|
|
|
else if (*val == 'N' || *val == 'n')
|
|
|
|
rc = 0;
|
|
|
|
else {
|
|
|
|
char *end;
|
|
|
|
rc = strtol(val, &end, 0);
|
|
|
|
if (!(end && *end == '\0'))
|
|
|
|
rc = 0;
|
|
|
|
}
|
2001-05-04 05:00:18 +08:00
|
|
|
val = _free(val);
|
1999-03-28 08:47:40 +08:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
/* @todo "../sbin/./../bin/" not correct. */
|
2000-01-13 05:48:58 +08:00
|
|
|
char *rpmCleanPath(char * path)
|
1999-03-28 08:47:40 +08:00
|
|
|
{
|
|
|
|
const char *s;
|
1999-11-27 00:19:30 +08:00
|
|
|
char *se, *t, *te;
|
2001-01-19 09:38:55 +08:00
|
|
|
int begin = 1;
|
1999-03-28 08:47:40 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
if (path == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2001-01-19 09:38:55 +08:00
|
|
|
/*fprintf(stderr, "*** RCP %s ->\n", path); */
|
1999-11-27 00:19:30 +08:00
|
|
|
s = t = te = path;
|
2001-04-29 09:05:43 +08:00
|
|
|
while (*s != '\0') {
|
1999-11-27 00:19:30 +08:00
|
|
|
/*fprintf(stderr, "*** got \"%.*s\"\trest \"%s\"\n", (t-path), path, s); */
|
1999-11-24 08:03:54 +08:00
|
|
|
switch(*s) {
|
|
|
|
case ':': /* handle url's */
|
|
|
|
if (s[1] == '/' && s[2] == '/') {
|
|
|
|
*t++ = *s++;
|
|
|
|
*t++ = *s++;
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@switchbreak@*/ break;
|
1999-11-24 08:03:54 +08:00
|
|
|
}
|
2001-01-19 09:38:55 +08:00
|
|
|
begin=1;
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@switchbreak@*/ break;
|
1999-11-24 08:03:54 +08:00
|
|
|
case '/':
|
|
|
|
/* Move parent dir forward */
|
|
|
|
for (se = te + 1; se < t && *se != '/'; se++)
|
2001-06-06 03:26:22 +08:00
|
|
|
{};
|
1999-11-24 08:03:54 +08:00
|
|
|
if (se < t && *se == '/') {
|
|
|
|
te = se;
|
1999-11-27 00:19:30 +08:00
|
|
|
/*fprintf(stderr, "*** next pdir \"%.*s\"\n", (te-path), path); */
|
1999-11-24 08:03:54 +08:00
|
|
|
}
|
|
|
|
while (s[1] == '/')
|
|
|
|
s++;
|
1999-11-27 00:19:30 +08:00
|
|
|
while (t > path && t[-1] == '/')
|
1999-11-24 08:03:54 +08:00
|
|
|
t--;
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@switchbreak@*/ break;
|
1999-11-24 08:03:54 +08:00
|
|
|
case '.':
|
|
|
|
/* Leading .. is special */
|
2002-01-28 00:49:06 +08:00
|
|
|
/* Check that it is ../, so that we don't interpret */
|
|
|
|
/* ..?(i.e. "...") or ..* (i.e. "..bogus") as "..". */
|
|
|
|
/* in the case of "...", this ends up being processed*/
|
|
|
|
/* as "../.", and the last '.' is stripped. This */
|
|
|
|
/* would not be correct processing. */
|
|
|
|
if (begin && s[1] == '.' && (s[2] == '/' || s[2] == '\0')) {
|
2001-01-19 09:38:55 +08:00
|
|
|
/*fprintf(stderr, " leading \"..\"\n"); */
|
1999-11-24 08:03:54 +08:00
|
|
|
*t++ = *s++;
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@switchbreak@*/ break;
|
1999-11-24 08:03:54 +08:00
|
|
|
}
|
|
|
|
/* Single . is special */
|
2001-01-19 09:38:55 +08:00
|
|
|
if (begin && s[1] == '\0') {
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@switchbreak@*/ break;
|
1999-11-24 08:03:54 +08:00
|
|
|
}
|
2001-01-19 09:38:55 +08:00
|
|
|
/* Trim embedded ./ , trailing /. */
|
2004-06-01 21:58:57 +08:00
|
|
|
if ((t[-1] == '/' && s[1] == '\0') || (t > path && t[-1] == '/' && s[1] == '/')) {
|
1999-11-24 08:03:54 +08:00
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Trim embedded /../ and trailing /.. */
|
2001-01-19 09:38:55 +08:00
|
|
|
if (!begin && t > path && t[-1] == '/' && s[1] == '.' && (s[2] == '/' || s[2] == '\0')) {
|
1999-11-24 08:03:54 +08:00
|
|
|
t = te;
|
|
|
|
/* Move parent dir forward */
|
1999-11-27 00:19:30 +08:00
|
|
|
if (te > path)
|
|
|
|
for (--te; te > path && *te != '/'; te--)
|
2001-06-06 03:26:22 +08:00
|
|
|
{};
|
1999-11-27 00:19:30 +08:00
|
|
|
/*fprintf(stderr, "*** prev pdir \"%.*s\"\n", (te-path), path); */
|
1999-11-24 08:03:54 +08:00
|
|
|
s++;
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@switchbreak@*/ break;
|
1999-11-24 08:03:54 +08:00
|
|
|
default:
|
2001-01-19 09:38:55 +08:00
|
|
|
begin = 0;
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@switchbreak@*/ break;
|
1999-11-24 08:03:54 +08:00
|
|
|
}
|
|
|
|
*t++ = *s++;
|
1999-10-30 07:03:12 +08:00
|
|
|
}
|
1999-11-27 00:19:30 +08:00
|
|
|
|
1999-11-24 08:03:54 +08:00
|
|
|
/* Trim trailing / (but leave single / alone) */
|
1999-11-27 00:19:30 +08:00
|
|
|
if (t > &path[1] && t[-1] == '/')
|
1999-11-24 08:03:54 +08:00
|
|
|
t--;
|
|
|
|
*t = '\0';
|
1999-10-30 07:03:12 +08:00
|
|
|
|
2001-01-19 09:38:55 +08:00
|
|
|
/*fprintf(stderr, "\t%s\n", path); */
|
2002-06-22 02:00:50 +08:00
|
|
|
return path;
|
1999-11-27 00:19:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return concatenated and expanded canonical path. */
|
2000-09-30 03:50:29 +08:00
|
|
|
|
1999-11-27 00:19:30 +08:00
|
|
|
const char *
|
|
|
|
rpmGetPath(const char *path, ...)
|
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
const char * s;
|
|
|
|
char * t, * te;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (path == NULL)
|
|
|
|
return xstrdup("");
|
|
|
|
|
2000-11-01 00:18:34 +08:00
|
|
|
buf[0] = '\0';
|
1999-11-27 00:19:30 +08:00
|
|
|
t = buf;
|
|
|
|
te = stpcpy(t, path);
|
|
|
|
*te = '\0';
|
|
|
|
|
|
|
|
va_start(ap, path);
|
|
|
|
while ((s = va_arg(ap, const char *)) != NULL) {
|
|
|
|
te = stpcpy(te, s);
|
|
|
|
*te = '\0';
|
|
|
|
}
|
|
|
|
va_end(ap);
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@-modfilesys@*/
|
2001-05-01 06:32:22 +08:00
|
|
|
(void) expandMacros(NULL, NULL, buf, sizeof(buf));
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@=modfilesys@*/
|
1999-11-27 00:19:30 +08:00
|
|
|
|
2000-09-30 03:50:29 +08:00
|
|
|
(void) rpmCleanPath(buf);
|
|
|
|
return xstrdup(buf); /* XXX xstrdup has side effects. */
|
1999-03-28 08:47:40 +08:00
|
|
|
}
|
|
|
|
|
1999-11-13 01:20:49 +08:00
|
|
|
/* Merge 3 args into path, any or all of which may be a url. */
|
1999-11-24 08:03:54 +08:00
|
|
|
|
1999-11-13 01:20:49 +08:00
|
|
|
const char * rpmGenPath(const char * urlroot, const char * urlmdir,
|
|
|
|
const char *urlfile)
|
|
|
|
{
|
2000-11-01 00:18:34 +08:00
|
|
|
/*@owned@*/ const char * xroot = rpmGetPath(urlroot, NULL);
|
|
|
|
/*@dependent@*/ const char * root = xroot;
|
|
|
|
/*@owned@*/ const char * xmdir = rpmGetPath(urlmdir, NULL);
|
|
|
|
/*@dependent@*/ const char * mdir = xmdir;
|
|
|
|
/*@owned@*/ const char * xfile = rpmGetPath(urlfile, NULL);
|
|
|
|
/*@dependent@*/ const char * file = xfile;
|
1999-11-13 01:20:49 +08:00
|
|
|
const char * result;
|
|
|
|
const char * url = NULL;
|
|
|
|
int nurl = 0;
|
1999-11-24 08:03:54 +08:00
|
|
|
int ut;
|
1999-11-13 01:20:49 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
#if 0
|
|
|
|
if (_debug) fprintf(stderr, "*** RGP xroot %s xmdir %s xfile %s\n", xroot, xmdir, xfile);
|
|
|
|
#endif
|
1999-11-24 08:03:54 +08:00
|
|
|
ut = urlPath(xroot, &root);
|
|
|
|
if (url == NULL && ut > URL_IS_DASH) {
|
1999-11-13 01:20:49 +08:00
|
|
|
url = xroot;
|
|
|
|
nurl = root - xroot;
|
2001-06-06 03:26:22 +08:00
|
|
|
#if 0
|
|
|
|
if (_debug) fprintf(stderr, "*** RGP ut %d root %s nurl %d\n", ut, root, nurl);
|
|
|
|
#endif
|
1999-11-13 01:20:49 +08:00
|
|
|
}
|
1999-11-24 08:03:54 +08:00
|
|
|
if (root == NULL || *root == '\0') root = "/";
|
1999-11-13 01:20:49 +08:00
|
|
|
|
1999-11-24 08:03:54 +08:00
|
|
|
ut = urlPath(xmdir, &mdir);
|
|
|
|
if (url == NULL && ut > URL_IS_DASH) {
|
1999-11-13 01:20:49 +08:00
|
|
|
url = xmdir;
|
|
|
|
nurl = mdir - xmdir;
|
2001-06-06 03:26:22 +08:00
|
|
|
#if 0
|
|
|
|
if (_debug) fprintf(stderr, "*** RGP ut %d mdir %s nurl %d\n", ut, mdir, nurl);
|
|
|
|
#endif
|
1999-11-13 01:20:49 +08:00
|
|
|
}
|
1999-11-24 08:03:54 +08:00
|
|
|
if (mdir == NULL || *mdir == '\0') mdir = "/";
|
1999-11-13 01:20:49 +08:00
|
|
|
|
1999-11-24 08:03:54 +08:00
|
|
|
ut = urlPath(xfile, &file);
|
|
|
|
if (url == NULL && ut > URL_IS_DASH) {
|
1999-11-13 01:20:49 +08:00
|
|
|
url = xfile;
|
|
|
|
nurl = file - xfile;
|
2001-06-06 03:26:22 +08:00
|
|
|
#if 0
|
|
|
|
if (_debug) fprintf(stderr, "*** RGP ut %d file %s nurl %d\n", ut, file, nurl);
|
|
|
|
#endif
|
1999-11-13 01:20:49 +08:00
|
|
|
}
|
|
|
|
|
2002-05-17 00:55:21 +08:00
|
|
|
/*@-branchstate@*/
|
1999-11-13 01:20:49 +08:00
|
|
|
if (url && nurl > 0) {
|
|
|
|
char *t = strncpy(alloca(nurl+1), url, nurl);
|
|
|
|
t[nurl] = '\0';
|
|
|
|
url = t;
|
|
|
|
} else
|
|
|
|
url = "";
|
2002-05-17 00:55:21 +08:00
|
|
|
/*@=branchstate@*/
|
1999-11-13 01:20:49 +08:00
|
|
|
|
1999-11-24 08:03:54 +08:00
|
|
|
result = rpmGetPath(url, root, "/", mdir, "/", file, NULL);
|
1999-11-13 01:20:49 +08:00
|
|
|
|
2001-05-04 05:00:18 +08:00
|
|
|
xroot = _free(xroot);
|
|
|
|
xmdir = _free(xmdir);
|
|
|
|
xfile = _free(xfile);
|
2001-06-06 03:26:22 +08:00
|
|
|
#if 0
|
|
|
|
if (_debug) fprintf(stderr, "*** RGP result %s\n", result);
|
|
|
|
#endif
|
1999-11-13 01:20:49 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1998-08-01 04:11:49 +08:00
|
|
|
/* =============================================================== */
|
|
|
|
|
1999-07-03 04:56:04 +08:00
|
|
|
#if defined(DEBUG_MACROS)
|
|
|
|
|
|
|
|
#if defined(EVAL_MACROS)
|
|
|
|
|
1999-07-03 05:15:25 +08:00
|
|
|
char *macrofiles = "/usr/lib/rpm/macros:/etc/rpm/macros:~/.rpmmacros";
|
1999-07-03 04:56:04 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
int c;
|
|
|
|
int errflg = 0;
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "f:")) != EOF ) {
|
|
|
|
switch (c) {
|
|
|
|
case 'f':
|
|
|
|
macrofiles = optarg;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
errflg++;
|
|
|
|
break;
|
1999-07-03 04:56:04 +08:00
|
|
|
}
|
2001-06-06 03:26:22 +08:00
|
|
|
}
|
|
|
|
if (errflg || optind >= argc) {
|
|
|
|
fprintf(stderr, "Usage: %s [-f macropath ] macro ...\n", argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-07-03 04:56:04 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
rpmInitMacros(NULL, macrofiles);
|
|
|
|
for ( ; optind < argc; optind++) {
|
|
|
|
const char *val;
|
1999-07-03 04:56:04 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
val = rpmGetPath(argv[optind], NULL);
|
|
|
|
if (val) {
|
|
|
|
fprintf(stdout, "%s:\t%s\n", argv[optind], val);
|
|
|
|
val = _free(val);
|
1999-07-03 04:56:04 +08:00
|
|
|
}
|
2001-06-06 03:26:22 +08:00
|
|
|
}
|
|
|
|
rpmFreeMacros(NULL);
|
|
|
|
return 0;
|
1999-07-03 04:56:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !EVAL_MACROS */
|
1998-07-09 01:50:48 +08:00
|
|
|
|
1999-04-21 06:45:52 +08:00
|
|
|
char *macrofiles = "../macros:./testmacros";
|
1998-07-09 01:50:48 +08:00
|
|
|
char *testfile = "./test";
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
1998-01-13 05:31:29 +08:00
|
|
|
{
|
2001-06-06 03:26:22 +08:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
FILE *fp;
|
|
|
|
int x;
|
1998-01-13 05:31:29 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
rpmInitMacros(NULL, macrofiles);
|
|
|
|
rpmDumpMacroTable(NULL, NULL);
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
if ((fp = fopen(testfile, "r")) != NULL) {
|
2004-04-09 04:27:53 +08:00
|
|
|
while(rdcl(buf, sizeof(buf), fp)) {
|
2001-06-06 03:26:22 +08:00
|
|
|
x = expandMacros(NULL, NULL, buf, sizeof(buf));
|
|
|
|
fprintf(stderr, "%d->%s\n", x, buf);
|
|
|
|
memset(buf, 0, sizeof(buf));
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
2001-06-06 03:26:22 +08:00
|
|
|
fclose(fp);
|
|
|
|
}
|
1998-01-13 05:31:29 +08:00
|
|
|
|
2004-04-09 04:27:53 +08:00
|
|
|
while(rdcl(buf, sizeof(buf), stdin)) {
|
2001-06-06 03:26:22 +08:00
|
|
|
x = expandMacros(NULL, NULL, buf, sizeof(buf));
|
|
|
|
fprintf(stderr, "%d->%s\n <-\n", x, buf);
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
}
|
|
|
|
rpmFreeMacros(NULL);
|
1998-07-09 01:50:48 +08:00
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
return 0;
|
1998-07-09 01:50:48 +08:00
|
|
|
}
|
1999-07-03 04:56:04 +08:00
|
|
|
#endif /* EVAL_MACROS */
|
1998-07-09 01:50:48 +08:00
|
|
|
#endif /* DEBUG_MACROS */
|
2002-07-03 07:54:35 +08:00
|
|
|
/*@=boundsread@*/
|