#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "ctf_headers.h"
#include "ctfstabs.h"
#include "forth.h"
#include "utils.h"
#include "memory.h"
char *fth_curtype;
static fth_type_ops_t *fth_type_ops;
static char *fth_model;
static int fth_ignoring;
static int fth_copying;
static int
fth_init(char *model)
{
fth_model = model;
return (0);
}
static int
fth_null_header(ctf_id_t tid)
{
return (0);
}
static int
fth_null_members(char *memfilter, char *format)
{
return (0);
}
static int
fth_null_trailer(void)
{
return (0);
}
static fth_type_ops_t fth_null_ops = {
fth_null_header,
fth_null_members,
fth_null_trailer
};
static int
find_member_cb(const char *memname, ctf_id_t tid, ulong_t off, void *arg)
{
char *memtofind = arg;
if (strcmp(memname, memtofind) == 0)
return (tid);
return (0);
}
static ctf_id_t
find_member(ctf_id_t tid, char *memname)
{
return (ctf_member_iter(ctf, tid, find_member_cb, memname));
}
static int
fth_section_init(char *fullname)
{
ctf_id_t ltid = 0, tid;
char *curtype, *lpart, *part, *npart;
int lkind = 0, kind;
curtype = xstrdup(fullname);
lpart = NULL;
part = strtok(fullname, ".");
for (;;) {
if (lpart == NULL) {
if ((tid = find_type(part)) == CTF_ERR ||
(tid = ctf_type_resolve(ctf, tid)) == CTF_ERR ||
(kind = ctf_type_kind(ctf, tid)) == CTF_ERR) {
free(curtype);
return (parse_warn("Couldn't find %s: %s",
part, ctf_errmsg(ctf_errno(ctf))));
}
} else {
if (lkind != CTF_K_STRUCT && lkind != CTF_K_UNION) {
free(curtype);
return (parse_warn("%s isn't a struct/union",
lpart));
}
if ((tid = find_member(ltid, part)) <= 0) {
free(curtype);
return (parse_warn("%s isn't a member of %s",
part, lpart));
}
if ((kind = ctf_type_kind(ctf, tid)) == CTF_ERR) {
free(curtype);
return (parse_warn("Can't get kind for %s",
part));
}
}
if ((npart = strtok(NULL, ".")) == NULL)
break;
lpart = part;
ltid = tid;
lkind = kind;
part = npart;
}
switch (kind) {
case CTF_K_STRUCT:
case CTF_K_UNION:
fth_type_ops = &fth_struct_ops;
break;
case CTF_K_ENUM:
fth_type_ops = &fth_enum_ops;
break;
default:
fth_type_ops = &fth_null_ops;
free(curtype);
return (parse_warn("%s isn't a struct, union, or enum", part));
}
fth_curtype = curtype;
return (fth_type_ops->fto_header(tid));
}
static int
fth_section_add_member(char *name, char *format)
{
if (fth_curtype == NULL)
return (fth_section_init(name));
if (fth_type_ops->fto_members(name, format) < 0)
return (-1);
return (0);
}
static int
fth_section_end(void)
{
if (fth_curtype == NULL)
return (0);
if (fth_type_ops->fto_trailer() < 0)
return (-1);
free(fth_curtype);
fth_curtype = NULL;
return (0);
}
static int
fth_process_line(char *line)
{
char *format = NULL;
char *word, *name, *c;
int nblank = 0;
int n;
if (strlen(line) == 0) {
if (fth_section_end() < 0)
return (-1);
if (fth_copying == 1 || nblank++ == 1)
(void) fprintf(out, "\n");
return (0);
} else
nblank = 0;
if (line[0] == '\\')
return (0);
if (strcmp(line, "model_end") == 0) {
fth_ignoring = 0;
return (0);
}
if (fth_ignoring == 1)
return (0);
word = "model_start ";
if (strncmp(line, word, strlen(word)) == 0) {
for (c = line + strlen(word); isspace(*c); c++);
if (strlen(c) == strlen(fth_model) &&
strncmp(c, fth_model, strlen(fth_model)) == 0)
;
else
fth_ignoring = 1;
return (0);
}
if (strcmp(line, "verbatim_end") == 0 ||
strcmp(line, "forth_end") == 0) {
char *start = (strcmp(line, "verbatim_end") == 0 ?
"verbatim_begin" : "forth_start");
if (fth_copying == 0) {
(void) parse_warn("Found %s without matching %s",
line, start);
if (fth_curtype != NULL)
(void) fth_section_end();
return (-1);
}
fth_copying = 0;
return (0);
}
if (fth_copying == 1) {
(void) fprintf(out, "%s\n", line);
return (0);
}
if (strcmp(line, "verbatim_begin") == 0 ||
strcmp(line, "forth_start") == 0) {
if (fth_curtype != NULL) {
(void) parse_warn("Expected blank line between %s "
"macro and %s", fth_curtype, line);
return (fth_section_end());
}
fth_copying = 1;
return (0);
}
for (n = 1, word = strtok(line, " \t"); word != NULL;
word = strtok(NULL, " \t"), n++) {
if (n == 1)
name = word;
else if (n == 2)
format = word;
else
(void) parse_warn("Too many words");
}
return (fth_section_add_member(name, format));
}
static int
fth_fini(void)
{
return (fth_section_end());
}
proc_ops_t fth_ops = {
fth_init,
fth_process_line,
fth_fini
};