📄 xmlcatalog.c
字号:
/*
* xmlcatalog.c : a small utility program to handle XML catalogs
*
* See Copyright for the status of this software.
*
* daniel@veillard.com
*/
#include "libxml.h"
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_LIBREADLINE
#include <readline/readline.h>
#ifdef HAVE_LIBHISTORY
#include <readline/history.h>
#endif
#endif
#include <libxml/xmlmemory.h>
#include <libxml/uri.h>
#include <libxml/catalog.h>
#include <libxml/parser.h>
#include <libxml/globals.h>
#if defined(LIBXML_CATALOG_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
static int shell = 0;
static int sgml = 0;
static int noout = 0;
static int create = 0;
static int add = 0;
static int del = 0;
static int convert = 0;
static int no_super_update = 0;
static int verbose = 0;
static char *filename = NULL;
#ifndef XML_SGML_DEFAULT_CATALOG
#define XML_SGML_DEFAULT_CATALOG "/etc/sgml/catalog"
#endif
/************************************************************************
* *
* Shell Interface *
* *
************************************************************************/
/**
* xmlShellReadline:
* @prompt: the prompt value
*
* Read a string
*
* Returns a pointer to it or NULL on EOF the caller is expected to
* free the returned string.
*/
static char *
xmlShellReadline(const char *prompt) {
#ifdef HAVE_LIBREADLINE
char *line_read;
/* Get a line from the user. */
line_read = readline (prompt);
/* If the line has any text in it, save it on the history. */
if (line_read && *line_read)
add_history (line_read);
return (line_read);
#else
char line_read[501];
char *ret;
int len;
if (prompt != NULL)
fprintf(stdout, "%s", prompt);
if (!fgets(line_read, 500, stdin))
return(NULL);
line_read[500] = 0;
len = strlen(line_read);
ret = (char *) malloc(len + 1);
if (ret != NULL) {
memcpy (ret, line_read, len + 1);
}
return(ret);
#endif
}
static void usershell(void) {
char *cmdline = NULL, *cur;
int nbargs;
char command[100];
char arg[400];
char *argv[20];
int i, ret;
xmlChar *ans;
while (1) {
cmdline = xmlShellReadline("> ");
if (cmdline == NULL)
return;
/*
* Parse the command itself
*/
cur = cmdline;
nbargs = 0;
while ((*cur == ' ') || (*cur == '\t')) cur++;
i = 0;
while ((*cur != ' ') && (*cur != '\t') &&
(*cur != '\n') && (*cur != '\r')) {
if (*cur == 0)
break;
command[i++] = *cur++;
}
command[i] = 0;
if (i == 0) continue;
nbargs++;
/*
* Parse the argument string
*/
memset(arg, 0, sizeof(arg));
while ((*cur == ' ') || (*cur == '\t')) cur++;
i = 0;
while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
if (*cur == 0)
break;
arg[i++] = *cur++;
}
arg[i] = 0;
if (i != 0)
nbargs++;
/*
* Parse the arguments
*/
i = 0;
nbargs = 0;
cur = arg;
memset(argv, 0, sizeof(argv));
while (*cur != 0) {
while ((*cur == ' ') || (*cur == '\t')) cur++;
if (*cur == '\'') {
cur++;
argv[i] = cur;
while ((*cur != 0) && (*cur != '\'')) cur++;
if (*cur == '\'') {
*cur = 0;
nbargs++;
i++;
cur++;
}
} else if (*cur == '"') {
cur++;
argv[i] = cur;
while ((*cur != 0) && (*cur != '"')) cur++;
if (*cur == '"') {
*cur = 0;
nbargs++;
i++;
cur++;
}
} else {
argv[i] = cur;
while ((*cur != 0) && (*cur != ' ') && (*cur != '\t'))
cur++;
*cur = 0;
nbargs++;
i++;
cur++;
}
}
/*
* start interpreting the command
*/
if (!strcmp(command, "exit"))
break;
if (!strcmp(command, "quit"))
break;
if (!strcmp(command, "bye"))
break;
if (!strcmp(command, "public")) {
if (nbargs != 1) {
printf("public requires 1 arguments\n");
} else {
ans = xmlCatalogResolvePublic((const xmlChar *) argv[0]);
if (ans == NULL) {
printf("No entry for PUBLIC %s\n", argv[0]);
} else {
printf("%s\n", (char *) ans);
xmlFree(ans);
}
}
} else if (!strcmp(command, "system")) {
if (nbargs != 1) {
printf("system requires 1 arguments\n");
} else {
ans = xmlCatalogResolveSystem((const xmlChar *) argv[0]);
if (ans == NULL) {
printf("No entry for SYSTEM %s\n", argv[0]);
} else {
printf("%s\n", (char *) ans);
xmlFree(ans);
}
}
} else if (!strcmp(command, "add")) {
if (sgml) {
if ((nbargs != 3) && (nbargs != 2)) {
printf("add requires 2 or 3 arguments\n");
} else {
if (argv[2] == NULL)
ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
BAD_CAST argv[1]);
else
ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
BAD_CAST argv[2]);
if (ret != 0)
printf("add command failed\n");
}
} else {
if ((nbargs != 3) && (nbargs != 2)) {
printf("add requires 2 or 3 arguments\n");
} else {
if (argv[2] == NULL)
ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
BAD_CAST argv[1]);
else
ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
BAD_CAST argv[2]);
if (ret != 0)
printf("add command failed\n");
}
}
} else if (!strcmp(command, "del")) {
if (nbargs != 1) {
printf("del requires 1\n");
} else {
ret = xmlCatalogRemove(BAD_CAST argv[0]);
if (ret <= 0)
printf("del command failed\n");
}
} else if (!strcmp(command, "resolve")) {
if (nbargs != 2) {
printf("resolve requires 2 arguments\n");
} else {
ans = xmlCatalogResolve(BAD_CAST argv[0],
BAD_CAST argv[1]);
if (ans == NULL) {
printf("Resolver failed to find an answer\n");
} else {
printf("%s\n", (char *) ans);
xmlFree(ans);
}
}
} else if (!strcmp(command, "dump")) {
if (nbargs != 0) {
printf("dump has no arguments\n");
} else {
xmlCatalogDump(stdout);
}
} else if (!strcmp(command, "debug")) {
if (nbargs != 0) {
printf("debug has no arguments\n");
} else {
verbose++;
xmlCatalogSetDebug(verbose);
}
} else if (!strcmp(command, "quiet")) {
if (nbargs != 0) {
printf("quiet has no arguments\n");
} else {
if (verbose > 0)
verbose--;
xmlCatalogSetDebug(verbose);
}
} else {
if (strcmp(command, "help")) {
printf("Unrecognized command %s\n", command);
}
printf("Commands available:\n");
printf("\tpublic PublicID: make a PUBLIC identifier lookup\n");
printf("\tsystem SystemID: make a SYSTEM identifier lookup\n");
printf("\tresolve PublicID SystemID: do a full resolver lookup\n");
printf("\tadd 'type' 'orig' 'replace' : add an entry\n");
printf("\tdel 'values' : remove values\n");
printf("\tdump: print the current catalog state\n");
printf("\tdebug: increase the verbosity level\n");
printf("\tquiet: decrease the verbosity level\n");
printf("\texit: quit the shell\n");
}
free(cmdline); /* not xmlFree here ! */
}
}
/************************************************************************
* *
* Main *
* *
************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -