-#line 119 "../mdcode.mdc"
+#line 124 "../mdcode.mdc"
#include <unistd.h>
#include <stdlib.h>
+#include <stdio.h>
#include "mdcode.h"
-#line 849 "../mdcode.mdc"
+#line 894 "../mdcode.mdc"
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <string.h>
-#include <stdio.h>
-
-#line 771 "../mdcode.mdc"
-static void code_print(FILE *out, struct code_node *node,
- char *fname)
-{
- for (; node; node = node->next) {
- char *c = node->code.txt;
- int len = node->code.len;
- int undent = 0;
-
- if (!len)
- continue;
-
- fprintf(out, "#line %d \"%s\"\n",
- node->line_no, fname);
- if (*c == ' ' || *c == '\t')
- undent = 1;
- while (len && *c) {
- fprintf(out, "%*s", node->indent, "");
- if (undent) {
- if (*c == '\t' && len > 1) {
- c++;
- len--;
- } else if (strncmp(c, " ", 4) == 0 && len > 4) {
- c += 4;
- len-= 4;
- }
- }
- do {
- fputc(*c, out);
- c++;
- len--;
- } while (len && c[-1] != '\n');
- }
- }
-}
-
-#line 821 "../mdcode.mdc"
+#line 867 "../mdcode.mdc"
static void copy_fname(char *name, int space, struct text t)
{
char *sec = t.txt;
strncpy(name, sec, len);
name[len] = 0;
}
-
-#line 857 "../mdcode.mdc"
+#line 901 "../mdcode.mdc"
static int errs;
static void pr_err(char *msg)
{
fprintf(stderr, "%s\n", msg);
}
+static char *strnchr(char *haystack, int len, char needle)
+{
+ while (len > 0 && *haystack && *haystack != needle) {
+ haystack++;
+ len--;
+ }
+ return len > 0 && *haystack == needle ? haystack : NULL;
+}
+
int main(int argc, char *argv[])
{
int fd;
size_t len;
char *file;
+ struct text section = {NULL, 0};
struct section *table, *s, *prev;
errs = 0;
- if (argc != 2) {
- fprintf(stderr, "Usage: mdcode file.mdc\n");
+ if (argc != 2 && argc != 3) {
+ fprintf(stderr, "Usage: mdcode file.mdc [section]\n");
exit(2);
}
+ if (argc == 3) {
+ section.txt = argv[2];
+ section.len = strlen(argv[2]);
+ }
+
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
fprintf(stderr, "mdcode: cannot open %s: %s\n",
(code_free(s->code), prev = s, s = s->next, free(prev))) {
FILE *fl;
char fname[1024];
- if (strncmp(s->section.txt, "Example:", 8) == 0)
- continue;
- if (strncmp(s->section.txt, "File:", 5) != 0) {
- fprintf(stderr, "Unreferenced section is not a file name: %.*s\n",
+ char *spc = strnchr(s->section.txt, s->section.len, ' ');
+
+ if (spc > s->section.txt && spc[-1] == ':') {
+ if (strncmp(s->section.txt, "File: ", 6) != 0 &&
+ (section.txt == NULL ||
+ text_cmp(s->section, section) != 0))
+ /* Ignore this section */
+ continue;
+ } else {
+ fprintf(stderr, "Code in unreferenced section that is not ignored or a file name: %.*s\n",
s->section.len, s->section.txt);
errs++;
continue;
}
+ if (section.txt) {
+ if (text_cmp(s->section, section) == 0)
+ code_node_print(stdout, s->code, argv[1]);
+ break;
+ }
copy_fname(fname, sizeof(fname), s->section);
if (fname[0] == 0) {
fprintf(stderr, "Missing file name at:%.*s\n",
errs++;
continue;
}
- code_print(fl, s->code, argv[1]);
+ code_node_print(fl, s->code, argv[1]);
fclose(fl);
}
exit(!!errs);
}
-
-#line 126 "../mdcode.mdc"
+#line 132 "../mdcode.mdc"