=================================================================== RCS file: /cvs/texi2mdoc/main.c,v retrieving revision 1.9 retrieving revision 1.10 diff -u -p -r1.9 -r1.10 --- texi2mdoc/main.c 2015/02/18 14:52:45 1.9 +++ texi2mdoc/main.c 2015/02/18 15:30:31 1.10 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.9 2015/02/18 14:52:45 kristaps Exp $ */ +/* $Id: main.c,v 1.10 2015/02/18 15:30:31 kristaps Exp $ */ /* * Copyright (c) 2015 Kristaps Dzonsons * @@ -27,6 +27,7 @@ #include #include #include +#include #include /* @@ -191,6 +192,8 @@ struct texi { int seenws; /* ws has been seen (and ignored) */ int ign; /* if >0, don't print anything */ int literal; /* if >0, literal context */ + char *title; /* title of document */ + char *subtitle; /* subtitle of document */ }; /* FIXME: FIND A BETTER WAY. */ @@ -202,7 +205,7 @@ struct texi { #define isws(_x) \ (' ' == (_x) || '\t' == (_x)) #define ismspace(_x) \ - (isws((_x) || '\n' == (_x))) + (isws((_x)) || '\n' == (_x)) static void doblock(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dobracket(struct texi *, enum texicmd, const char *, size_t, size_t *); @@ -235,6 +238,7 @@ static void dosection(struct texi *, enum texicmd, con static void dosp(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dosubsection(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dosymbol(struct texi *, enum texicmd, const char *, size_t, size_t *); +static void dotitle(struct texi *, enum texicmd, const char *, size_t, size_t *); static const struct texitok texitoks[TEXICMD__MAX] = { { doignargn, "acronym", 7 }, /* TEXICMD_ACRONYM */ @@ -315,7 +319,7 @@ static const struct texitok texitoks[TEXICMD__MAX] = { { doignline, "set", 3 }, /* TEXICMD_SET */ { doignline, "setchapternewpage", 17 }, /* TEXICMD_SETCHAPNEWPAGE */ { doignline, "setfilename", 11 }, /* TEXICMD_SETFILENAME */ - { doignline, "settitle", 8 }, /* TEXICMD_SETTITLE */ + { dotitle, "settitle", 8 }, /* TEXICMD_SETTITLE */ { dosp, "sp", 2 }, /* TEXICMD_SP */ { dosymbol, " ", 1 }, /* TEXICMD_SPACE */ { doexample, "smallexample", 12 }, /* TEXICMD_SMALLEXAMPLE */ @@ -373,7 +377,10 @@ texiexit(struct texi *p) for (i = 0; i < p->dirsz; i++) free(p->dirs[i]); + free(p->dirs); + free(p->subtitle); + free(p->title); } /* @@ -1235,6 +1242,23 @@ dobye(struct texi *p, enum texicmd cmd, } static void +dotitle(struct texi *p, enum texicmd cmd, + const char *buf, size_t sz, size_t *pos) +{ + size_t start, end; + + while (*pos < sz && isws(buf[*pos])) + advance(p, buf, pos); + start = end = *pos; + while (end < sz && '\n' != buf[end]) + end++; + free(p->subtitle); + p->subtitle = malloc(end - start + 1); + memcpy(p->subtitle, &buf[start], end - start); + p->subtitle[end - start] = '\0'; +} + +static void dosymbol(struct texi *p, enum texicmd cmd, const char *buf, size_t sz, size_t *pos) { @@ -1449,15 +1473,31 @@ static void dotop(struct texi *p, enum texicmd cmd, const char *buf, size_t sz, size_t *pos) { + const char *cp; + time_t t; + char date[32]; + t = time(NULL); + strftime(date, sizeof(date), "%F", localtime(&t)); + p->ign--; advanceeoln(p, buf, sz, pos, 1); - teximacro(p, "Dd $Mdocdate: February 18 2015 $"); - teximacro(p, "Dt SOMETHING 7"); + teximacroopen(p, "Dd"); + texiputchars(p, date); + teximacroclose(p); + teximacroopen(p, "Dt"); + for (cp = p->title; '\0' != *cp; cp++) + texiputchar(p, toupper(*cp)); + teximacroclose(p); teximacro(p, "Os"); teximacro(p, "Sh NAME"); - teximacro(p, "Nm Something"); - teximacro(p, "Nd Something"); + teximacroopen(p, "Nm"); + texiputchars(p, p->title); + teximacroclose(p); + teximacroopen(p, "Nd"); + texiputchars(p, NULL != p->subtitle ? + p->subtitle : "Unknown description"); + teximacroclose(p); } static void @@ -1599,7 +1639,7 @@ main(int argc, char *argv[]) struct texi texi; int c; char *path, *dir; - const char *progname, *Idir; + const char *progname, *Idir, *cp; progname = strrchr(argv[0], '/'); if (progname == NULL) @@ -1607,7 +1647,9 @@ main(int argc, char *argv[]) else ++progname; + memset(&texi, 0, sizeof(struct texi)); Idir = NULL; + while (-1 != (c = getopt(argc, argv, "I:"))) switch (c) { case ('I'): @@ -1631,7 +1673,17 @@ main(int argc, char *argv[]) } free(path); - memset(&texi, 0, sizeof(struct texi)); + if (NULL != (cp = strrchr(argv[0], '/'))) + texi.title = strdup(cp + 1); + else + texi.title = strdup(argv[0]); + + if (NULL == texi.title) { + perror(NULL); + exit(EXIT_FAILURE); + } else if (NULL != (path = strchr(texi.title, '.'))) + *path = '\0'; + texi.ign = 1; texi.dirs = parsedirs(dir, Idir, &texi.dirsz); parsefile(&texi, argv[0]);