Fossil

Check-in [decac09b]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Cleanup and simplify the code for the recently added "wiki" command.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: decac09b7d246ca64cd916121ba74fe8d3b5327e
User & Date: drh 2008-05-14 20:59:10
Context
2008-05-14
23:01
added some help docs + TODO check-in: cf5bbd92 user: stephan tags: trunk
20:59
Cleanup and simplify the code for the recently added "wiki" command. check-in: decac09b user: drh tags: trunk
20:26
cleaned up error handling a bit, minor code style changes, s/import/commit/ check-in: feee32d3 user: stephan tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/wiki.c.

596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
...
635
636
637
638
639
640
641
642
643
644
645
646

647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688




689
690
691
692
693
694
695
696


697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729


730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
  @ up through the next </verbatim>.  The <nowiki> tag
  @ disables all wiki formatting rules through the matching
  @ </nowiki> element.
  @ </ol>
  style_footer();
}

void dump_blob_to_FILE( Blob * b, FILE * f )
{
	fwrite(blob_buffer(b), 1, blob_size(b), stdout);
}

/*
** COMMAND: wiki
**
** Usage: %fossil wiki (export|commit|list) WikiName
**
** Run various subcommands to fetch wiki entries.
**
................................................................................
  }
  n = strlen(g.argv[2]);
  if( n==0 ){
    goto wiki_cmd_usage;
  }

  if( strncmp(g.argv[2],"export",n)==0 ){
    Stmt q;
    char *wname;
    Blob buf;
    int rid;
    char * sql;

    if( g.argc!=4 ){
      usage("export EntryName");
    }
    wname = g.argv[3];
    rid = -1;
    sql = mprintf("SELECT x.rid FROM tag t, tagxref x"
      " WHERE x.tagid=t.tagid AND t.tagname='wiki-%q'"
      " ORDER BY x.mtime DESC LIMIT 1",
      wname );
    db_prepare(&q, "%z", sql );
    if( db_step(&q) == SQLITE_ROW ){
      rid = db_column_int(&q,0);
    }
    db_finalize(&q);
    if( -1 == rid ){
      fprintf(stderr,"export error: wiki entry [%s] not found.\n",wname);
      exit(1);
    }
    if( ! content_get(rid,&buf) ){
      fprintf(stderr,"export error: content_get(%d) returned 0\n", rid );
      exit(1);
    }else
    {
      /* Unfortunately, the content_get() routine does ALMOST what i want,
	 but not quite. It is quite complex, so i don't want to fork a
	 modified copy here. That's what all the skipping-over bits are for...

	 We look for the first line starting with 'W', then expect '
	 NUMBER\n' immediately after that, followed by NUMBER bytes
	 of plain blob content.
      */
      int len;
      char * it;
      Blob numbuf;
      blob_zero(&numbuf);
      it = blob_buffer(&buf);
      while(*it){
        if( *it != 'W' ){
          ++it;
          while( *it ){
            if( *it == '\n') { ++it; break; }
            ++it;




          }
          continue;
        }
        if( ! *it )
        {
            fprintf(stderr,
              "export reached end of input before finding a 'W' card.\n");
            exit(1);


        }
        ++it;
        while( (*it) && (*it != '\n') ){
          if( isspace(*it) ) { ++it; continue; }
          blob_append(&numbuf,it,1);
          ++it;
        }
        if( '\n' == *it ) ++it;
        if( 0 == blob_size(&numbuf) ){
          fprintf(stderr,
          "export error: didn't find \"W NUMBER\" line in input!\n");
          blob_reset(&buf);
          blob_reset(&numbuf);
          exit(1);
        }
        len = atoi(blob_buffer(&numbuf));
        //fprintf(stderr,"Writing %s (%d) bytes...\n",blob_buffer(&numbuf),len);
        blob_reset(&numbuf);
        if( ( (it - blob_buffer(&buf)) + len) > blob_size(&buf) ){
          fprintf(stderr,
            "export error: manifest data doesn't match actual data size!"
            " Manifest says [%s (%d)] bytes.\n",
            blob_buffer(&numbuf), len );
          blob_reset(&buf);
          blob_reset(&numbuf);
          exit(1);
        }
        fwrite(it,sizeof(char),len,stdout);
        blob_reset(&buf);
        return;
      }
    }
    blob_reset(&buf);


    return;
  }else
  if( strncmp(g.argv[2],"commit",n)==0 ){
    char *wname;
    if( g.argc!=4 ){
      usage("commit EntryName");
    }
    wname = g.argv[3];
    fprintf(stderr,"commit not yet implemented.\n");
    exit(1);
  }else
  if( strncmp(g.argv[2],"delete",n)==0 ){
    if( g.argc!=5 ){
      usage("delete WikiName");
    }
    fprintf(stderr,"delete not yet implemented.\n");
    exit(1);
  }else
  if( strncmp(g.argv[2],"list",n)==0 ){
    Stmt q;
    db_prepare(&q, 
               "SELECT substr(tagname, 6, 1000) FROM tag WHERE tagname GLOB 'wiki-*'"
               " ORDER BY lower(tagname)"
               );
    while( db_step(&q)==SQLITE_ROW ){
        const char *zName = db_column_text(&q, 0);
        printf( "%s\n",zName);
    }
    db_finalize(&q);
  }else
  {
    goto wiki_cmd_usage;
  }
  return;

wiki_cmd_usage:
  usage("delete|export|commit|list [EntryName]");
}








<
<
<
<
<







 







|
|
|
|
|
>

|


<
|


|
|
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
>
>
>
>
|
<
|
<
<
<
<
<
>
>
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
>
>





|


|
<



|

|
<




|
|
|

|
|









|

<
596
597
598
599
600
601
602





603
604
605
606
607
608
609
...
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646

647
648
649
650
651




652














653












654
655
656
657
658

659





660
661
662
































663
664
665
666
667
668
669
670
671
672
673

674
675
676
677
678
679

680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700

  @ up through the next &lt;/verbatim&gt;.  The &lt;nowiki&gt; tag
  @ disables all wiki formatting rules through the matching
  @ &lt;/nowiki&gt; element.
  @ </ol>
  style_footer();
}






/*
** COMMAND: wiki
**
** Usage: %fossil wiki (export|commit|list) WikiName
**
** Run various subcommands to fetch wiki entries.
**
................................................................................
  }
  n = strlen(g.argv[2]);
  if( n==0 ){
    goto wiki_cmd_usage;
  }

  if( strncmp(g.argv[2],"export",n)==0 ){
    char *wname;            /* Name of the wiki page to export */
    int rid;                /* Artifact ID of the wiki page */
    int i;                  /* Loop counter */
    char *zBody = 0;        /* Wiki page content */
    Manifest m;             /* Parsed wiki page content */

    if( g.argc!=4 ){
      usage("export PAGENAME");
    }
    wname = g.argv[3];

    rid = db_int(0, "SELECT x.rid FROM tag t, tagxref x"
      " WHERE x.tagid=t.tagid AND t.tagname='wiki-%q'"
      " ORDER BY x.mtime DESC LIMIT 1",
      wname 
    );




    if( rid ){














      Blob content;












      content_get(rid, &content);
      manifest_parse(&m, &content);
      if( m.type==CFTYPE_WIKI ){
        zBody = m.zWiki;
      }

    }





    if( zBody==0 ){
      fossil_fatal("wiki page [%s] not found",wname);
    }
































    for(i=strlen(zBody); i>0 && isspace(zBody[i-1]); i--){}
    printf("%.*s\n", i, zBody);
    return;
  }else
  if( strncmp(g.argv[2],"commit",n)==0 ){
    char *wname;
    if( g.argc!=4 ){
      usage("commit PAGENAME");
    }
    wname = g.argv[3];
    fossil_fatal("wiki commit not yet implemented.");

  }else
  if( strncmp(g.argv[2],"delete",n)==0 ){
    if( g.argc!=5 ){
      usage("delete PAGENAME");
    }
    fossil_fatal("delete not yet implemented.");

  }else
  if( strncmp(g.argv[2],"list",n)==0 ){
    Stmt q;
    db_prepare(&q, 
      "SELECT substr(tagname, 6) FROM tag WHERE tagname GLOB 'wiki-*'"
      " ORDER BY lower(tagname)"
    );
    while( db_step(&q)==SQLITE_ROW ){
      const char *zName = db_column_text(&q, 0);
      printf( "%s\n",zName);
    }
    db_finalize(&q);
  }else
  {
    goto wiki_cmd_usage;
  }
  return;

wiki_cmd_usage:
  usage("delete|export|commit|list ...");
}