~hww3/caudium

ref: 93d5a75fba08db71b832716377276978cdd7888d caudium/tools/defvar2autodoc.pike -rw-r--r-- 3.5 KiB
93d5a75fWilliam Welliver storage: method "None" was missing stop function. should fix error on shutdown 1 year, 1 month ago
                                                                                
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
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env pike
// Generate inline auto-doc from the defvar statements in Caudium modules.
#define THIS split[i][e+1]

#define ENDINGS "\n" \
"/*\n" \
" * If you visit a file that doesn't contain these lines at its end, please\n" \
" * cut and paste everything from here to that file.\n" \
" */\n" \
"\n" \
"/*\n" \
" * Local Variables:\n" \
" * c-basic-offset: 2\n" \
" * End:\n" \
" *\n" \
" * vim: softtabstop=2 tabstop=2 expandtab autoindent formatoptions=croqlt smartindent cindent shiftwidth=2\n" \
" */\n" \
"\n" 


int get_next_string(array arr, int pos, mapping res) {
  string s = "";
  int i;
  for(i = pos; i < sizeof(arr); i++) {
    if(arrayp(arr[i])) {
      res->res = "";
      return i+1;
    }
    if(((string)arr[i])[0..0] == "\"") {
      s += arr[i];
      break;
    }
  }
  for(i = i+1; i < sizeof(arr); i++) {
    if(arrayp(arr[i])) {
      break;      
    }
    if(((string)arr[i])[0..0] != "\"") {
      break;
    } else
      s += arr[i];
  }
  if(strlen(s)) {
    s = "string res = "+ s + ";";
    res->res = compile_string(s)()->res;
  } else
    res->res = 0;
  return i+1;
}

int get_token(array arr, int pos, mapping res) {
  string s = "";
  int i;
  for(i = pos; i < sizeof(arr); i++) {
    if(((string)arr[i])[0..0] == "," ||
       ((string)arr[i])[0..0] == ")")
      break;
    s += arr[i];
  }
  res->token = s;
  return i+1;
}
#define SEP "\n/* START AUTOGENERATED DEFVAR DOCS */\n\n"

int main(int argc, array(string) argv)
{
  
  foreach(argv[1..], string file) {
    string doc = SEP;
    string origdata;
    if((file / ".")[-1] != "pike")
      continue;
    string data = Stdio.read_file(file);
    origdata = data;
    if(!data) continue;
    array split;
    sscanf(data, "%s\n/* START AUTOGENERATED DEFVAR", data);
      
    array err = catch {
      if(!has_value(data, "defvar") && !has_value(data, "globvar")) {
	write("    "+file+": no defvars\n");
	continue;
      }
      split = Parser.Pike->group(Parser.Pike->hide_whitespaces(Parser.Pike->tokenize(Parser.Pike->split(data))));
      if(!arrayp(split))
	continue;
      for(int i = 0; i < sizeof(split); i++) {
	if(arrayp(split[i]))
	  for(int e = 0; e < sizeof(split[i]) - 1; e++) {
	    int pos;
	    mapping res = ([]);
	    if(split[i][e] == "defvar" ||
	       split[i][e] == "globvar") {
	      pos = get_next_string(THIS, 0, res);
	      res->name = res->res;
	      while(!arrayp(THIS[pos]) && ((string)(THIS[pos])) != ",")
		pos++;
	      pos = get_next_string(THIS, pos+1, res);
	      if(res->name && strlen(res->name) &&
		 res->res && strlen(res->res)) {
		res->longname = res->res;
		doc += ("//! defvar: "+res->name +"\n");
		pos = get_token(THIS, pos, res);
		pos = get_next_string(THIS, pos, res);
		if(res->res) {
		  res->res =
		    replace(res->res, ({ "<br>",  "&nbsp;", "\n" }),
			    ({ "<br />", "&#xa0;", "\n//!"}));
		  if(strlen(res->res - " "))
		    doc += ("//! "+ res->res +"\n");
		}
		doc += ("//!  type: "+res->token +"\n");
		doc += ("//!  name: "+res->longname+"\n//\n");
	      }
	    }
	  }
      }
      if(doc != SEP) {
	data += doc;
	if(data != origdata) {
	  if(!mv(file, file+"~")) {
	    werror("Failed to write backup-file "+file+"~ - aborting.\n");
	    exit(1);
	  }
          data += ENDINGS;
	  Stdio.write_file(file, data);
	  write("+++ "+ file+" updated\n");
	} else
	  write("    "+file+" unmodified\n");
      } else {
	write("    "+file+": no defvars\n");
      }
    };
    if(err) {
      werror("*** "+file+" failed.\n");
      werror(describe_backtrace(err));
    }
  }
}