source: trunk/third/moira/gen/confluence.pc @ 23740

Revision 23740, 3.0 KB checked in by broder, 16 years ago (diff)
In moira: * New CVS snapshot (Trac: #195) * Drop patches that have been incorporated upstream. * Update to build without krb4 on systems that no longer have it. This doesn't build yet on squeeze, which lacks a krb4 library, but I'm committing now before I start hacking away at a patch to fix that.
Line 
1/* $Id: confluence.pc,v 1.1 2007-11-15 22:09:20 zacheiss Exp $
2 *
3 * (c) Copyright 2007 by the Massachusetts Institute of Technology.
4 */
5
6#include <mit-copyright.h>
7#include <moira.h>
8#include <moira_site.h>
9
10#include <sys/stat.h>
11
12#include <ctype.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
17#include "util.h"
18
19EXEC SQL INCLUDE sqlca;
20
21char *whoami = "confluence.gen";
22char *db = "moira/moira";
23
24struct hash *lists;
25
26void output_list(int id, void *list, void *out);
27
28int main(int argc, char **argv)
29{
30  char filename[MAXPATHLEN], *targetfile, *l;
31  FILE *out = stdout;
32  int cnt = 0;
33  EXEC SQL BEGIN DECLARE SECTION;
34  int lid;
35  char lname[LIST_NAME_SIZE];
36  EXEC SQL END DECLARE SECTION;
37
38  EXEC SQL CONNECT :db;
39
40  if (argc == 2)
41    {
42      targetfile = argv[1];
43      sprintf(filename, "%s~", targetfile);
44      if (!(out = fopen(filename, "w")))
45        {
46          fprintf(stderr, "unable to open %s for output\n", filename);
47          exit(MR_OCONFIG);
48        }
49    }
50  else if (argc != 1)
51    {
52      fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
53      exit(MR_ARGS);
54    }
55
56  lists = create_hash(15000);
57
58  EXEC SQL DECLARE l_cursor CURSOR FOR
59    SELECT l.list_id, l.name FROM list l
60    WHERE l.active = 1 and l.grouplist = 1;
61  EXEC SQL OPEN l_cursor;
62  while (1)
63    {
64      EXEC SQL FETCH l_cursor INTO :lid, :lname;
65      if (sqlca.sqlcode)
66        break;
67      l = strdup(strtrim(lname));
68      if (hash_store(lists, lid, l) < 0)
69        {
70          fprintf(stderr, "Out of memory!\n");
71          exit(MR_NO_MEM);
72        }
73      cnt++;
74    }
75  EXEC SQL CLOSE l_cursor;
76  fprintf(stderr, "Loaded %d lists\n", cnt);
77
78  hash_step(lists, output_list, out);
79
80  if (fclose(out))
81    {
82      perror("close failed");
83      exit(MR_CCONFIG);
84    }
85 
86  if (argc == 2)
87    fix_file(targetfile);
88  exit(MR_SUCCESS);
89}
90
91void output_list(int id, void *list, void *out)
92{
93  EXEC SQL BEGIN DECLARE SECTION;
94  char *l = list;
95  int lid = id;
96  int lgid;
97  char login[USERS_LOGIN_SIZE];
98  char stringmember[STRINGS_STRING_SIZE];
99  EXEC SQL END DECLARE SECTION;
100  char *maybecomma = "";
101
102  EXEC SQL SELECT gid INTO :lgid FROM list WHERE list_id = :lid;
103
104  fprintf(out, "%s:%d:", list, lgid);
105
106  EXEC SQL DECLARE u_cursor CURSOR FOR
107    SELECT UNIQUE u.login FROM users u, imembers i, list l
108    WHERE l.list_id = :lid AND l.list_id = i.list_id AND
109    i.member_type = 'USER' AND i.member_id = u.users_id;
110  EXEC SQL OPEN u_cursor;
111  while (1)
112    {
113      EXEC SQL FETCH u_cursor INTO :login;
114      if (sqlca.sqlcode)
115        break;
116      fprintf(out, "%s%s%s", maybecomma, strtrim(login), "@mit.edu");
117      maybecomma = ",";
118    }
119  EXEC SQL CLOSE u_cursor;
120
121  EXEC SQL DECLARE s_cursor CURSOR FOR
122    SELECT UNIQUE s.string FROM strings s, imembers i, list l
123    WHERE l.list_id = :lid AND l.list_id = i.list_id AND
124    i.member_type = 'STRING' AND i.member_id = s.string_id;
125  EXEC SQL OPEN s_cursor;
126  while (1)
127    {
128      EXEC SQL FETCH s_cursor INTO :stringmember;
129      if (sqlca.sqlcode)
130        break;
131      fprintf(out, "%s%s", maybecomma, strtrim(stringmember));
132      maybecomma = ",";
133    }
134  EXEC SQL CLOSE s_cursor;
135
136  fprintf(out, "\n");
137}
Note: See TracBrowser for help on using the repository browser.