File: makewikiinit.c

package info (click to toggle)
cvstrac 1.1.5-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 824 kB
  • ctags: 680
  • sloc: ansic: 13,963; tcl: 111; makefile: 72; sh: 19
file content (130 lines) | stat: -rw-r--r-- 3,877 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
** Copyright (c) 2002 D. Richard Hipp
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public
** License as published by the Free Software Foundation; either
** version 2 of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
** General Public License for more details.
** 
** You should have received a copy of the GNU General Public
** License along with this library; if not, write to the
** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
** Boston, MA  02111-1307, USA.
**
** Author contact information:
**   [email protected]
**   http://www.hwaci.com/drh/
**
*******************************************************************************
**
** This file implements a standalone program used to generate the
** "wiki_init.c" file for CVSTrac.
**
** "wiki_init.c" contains a single routine that initializes a new
** CVSTrac database with wiki pages containing CVSTrac documentation.
**
** This program generates wiki_init.c by reading an existing CVSTrac
** database file, extract all the wiki pages, and writing the code
** necessary to exactly reproduce those pages.
*/
#include <stdio.h>
#include <sqlite.h>

static int generate_page(void *NotUsed, int argc, char **argv, char **Unused){
  int i;
  char *z;
  printf("static const char z%s[] =\n", argv[0]);
  z = argv[1];
  while( *z ){
    for(i=0; z[i] && z[i]!='\n'; i++){}
    printf("@ %.*s\n", i, z);
    if( z[i]=='\n' ) i++;
    z += i;
  }
  printf(";\n");
  return 0;
}

int main(int argc, char **argv){
  sqlite *db;
  char *zErrMsg;
  int rc;
  int i;
  int nName, nCol;
  char **azName;

  if( argc!=2 ){
    fprintf(stderr,"Usage: %s DATABASE >wikiinit.c\n", argv[0]);
    exit(1);
  }
  if( access(argv[1], 4) ){
    perror("Database file does not exist or is not readable");
    exit(1);
  }
  db = sqlite_open(argv[1], 0, &zErrMsg);
  if( db==0 ){
    fprintf(stderr,"Can't open database: %s\n", zErrMsg);
    exit(1);
  }
  printf(
    "/*** AUTOMATICALLY GENERATED FILE - DO NOT EDIT ****\n"
    "**\n"
    "** This file was generated automatically by the makewikiinit.c\n"
    "** program.  See the sources to that program for additional\n"
    "** information.\n"
    "**\n"
    "** This file contains code used to initialize the wiki for a new\n"
    "** CVSTrac database.\n"
    "*/\n"
    "#include \"config.h\"\n"
    "#include \"wikiinit.h\"\n"
    "#include <time.h>\n"
    "\n"
  );
  rc = sqlite_get_table(db,
         "SELECT DISTINCT name FROM wiki ORDER BY name",
         &azName, &nName, &nCol, &zErrMsg);
  if( rc!=SQLITE_OK ){
    fprintf(stderr,"Database error: %s\n", zErrMsg);
    exit(1);
  }
  for(i=1; i<=nName; i++){
    rc = sqlite_exec_printf(db,
              "SELECT name, text FROM wiki WHERE name='%s' LIMIT 1",
              generate_page, 0, &zErrMsg, azName[i]);
    if( rc!=SQLITE_OK ){
      fprintf(stderr,"Database error: %s\n", zErrMsg);
      exit(1);
    }
  }
  sqlite_close(db);
  printf(
    "static const struct {\n"
    "  const char *zName;\n"
    "  const char *zText;\n"
    "} aPage[] = {\n"
  );
  for(i=1; i<=nName; i++){
    printf("  { \"%s\", z%s },\n", azName[i], azName[i]);
  }
  printf(
    "};\n"
    "void initialize_wiki_pages(void){\n"
    "  int i;\n"
    "  time_t now = time(0);\n"
    "  for(i=0; i<sizeof(aPage)/sizeof(aPage[0]); i++){\n"
    "    db_execute(\"INSERT INTO wiki(name,invtime,locked,who,ipaddr,text)\"\n"
    "       \"VALUES('%%s',%%d,%%d,'','','%%q')\",\n"
    "       aPage[i].zName, -(int)now,\n"
    "       strcmp(aPage[i].zName, \"WikiIndex\")!=0, \n"
    "       aPage[i].zText);\n"
    "  }\n"
    "}\n"
  );
  return 0;
}