source: trunk/third/moira/regtape/staff.pc @ 23740

Revision 23740, 5.7 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: staff.pc,v 1.9 2008-03-31 21:00:43 zacheiss Exp $
2 *
3 * Load data into Moira from Personnel Office data file
4 *
5 * Copyright (C) 1990-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
8 */
9
10#include <mit-copyright.h>
11#include <moira.h>
12#include <moira_site.h>
13#include <moira_schema.h>
14#include "common.h"
15
16#include <ctype.h>
17#include <stdio.h>
18#include <string.h>
19
20EXEC SQL INCLUDE sqlca;
21
22RCSID("$Header: /afs/athena.mit.edu/astaff/project/moiradev/repository/moira/regtape/staff.pc,v 1.9 2008-03-31 21:00:43 zacheiss Exp $");
23
24/* File format is:
25 *
26 * id number [9]
27 * last name [30]
28 * first name [30]
29 * middle name [30]
30 * office address [24]
31 * phone1 [12]
32 * phone2 [12]
33 * dept [50]
34 * title [50]
35 */
36
37#define LOC_ID 0
38#define LEN_ID 9
39#define LOC_LAST_NAME (LOC_ID + LEN_ID)
40#define LEN_LAST_NAME 30
41#define LOC_FIRST_NAME (LOC_LAST_NAME + LEN_LAST_NAME)
42#define LEN_FIRST_NAME 30
43#define LOC_MIDDLE_NAME (LOC_FIRST_NAME + LEN_FIRST_NAME)
44#define LEN_MIDDLE_NAME 30
45#define LOC_OFFICE (LOC_MIDDLE_NAME + LEN_MIDDLE_NAME)
46#define LEN_OFFICE 24
47#define LOC_PHONE (LOC_OFFICE + LEN_OFFICE)
48#define LEN_PHONE 12
49#define LOC_PHONE2 (LOC_PHONE + LEN_PHONE)
50#define LEN_PHONE2 12
51#define LOC_DEPT (LOC_PHONE2 + LEN_PHONE2)
52#define LEN_DEPT 50
53#define LOC_TITLE (LOC_DEPT + LEN_DEPT)
54#define LEN_TITLE 50
55
56struct entry *get_next_entry(FILE *in);
57void process_entry(struct entry *e, int secure);
58
59EXEC SQL BEGIN DECLARE SECTION;
60int who;
61char *prog = "stafload";
62EXEC SQL END DECLARE SECTION;
63
64char *whoami;
65
66int main(int argc, char **argv)
67{
68  FILE *in;
69  struct entry *e;
70  int i, wait = 0;
71  char buf[80], *file = NULL;
72  EXEC SQL BEGIN DECLARE SECTION;
73  char *db = "moira";
74  EXEC SQL END DECLARE SECTION;
75
76  whoami = strrchr(argv[0], '/');
77  if (whoami)
78    whoami++;
79  else
80    whoami = argv[0];
81
82  setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
83  setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
84
85  for (i = 1; i < argc; i++)
86    {
87      if (!strcmp(argv[i], "-w"))
88        wait++;
89      else if (file)
90        {
91          fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
92          exit(1);
93        }
94      else
95        file = argv[i];
96    }
97
98  if (!file)
99    {
100      fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
101      exit(1);
102    }
103
104  in = fopen(file, "r");
105  if (!in)
106    {
107      fprintf(stderr, "Unable to open %s for input\n", file);
108      exit(1);
109    }
110
111  initialize_sms_error_table();
112
113  EXEC SQL CONNECT :db IDENTIFIED BY :db;
114  if (sqlca.sqlcode)
115    {
116      dbmserr("opening database", sqlca.sqlcode);
117      exit(1);
118    }
119
120  EXEC SQL SELECT users_id INTO :who FROM users WHERE login = 'root';
121
122  while ((e = get_next_entry(in)))
123    {
124      process_entry(e, 0);
125      EXEC SQL COMMIT WORK;
126      if (sqlca.sqlcode)
127        {
128          dbmserr("committing work", sqlca.sqlcode);
129          exit(1);
130        }
131      if (wait)
132        {
133          printf("Next");
134          fflush(stdout);
135          fgets(buf, sizeof(buf), stdin);
136        }
137    }
138
139  exit(0);
140}
141
142struct entry *get_next_entry(FILE *in)
143{
144  static struct entry e;
145  static char buf[BUFSIZ];
146  static char last_name[LEN_LAST_NAME + 1], id[LEN_ID + 1];
147  static char first_name[LEN_FIRST_NAME + 1], middle_name[LEN_MIDDLE_NAME + 1];
148  static char office[LEN_OFFICE + 1], phone[LEN_PHONE + 1];
149  static char phone2[LEN_PHONE2 + 1], dept[LEN_DEPT + 1], title[LEN_TITLE + 1];
150  int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
151  char *p, *q;
152
153  if (!fgets(buf, sizeof(buf), in))
154    return NULL;
155
156  strlcpy(id, &buf[LOC_ID], LEN_ID + 1);
157  strlcpy(last_name, &buf[LOC_LAST_NAME], LEN_LAST_NAME + 1);
158  strlcpy(first_name, &buf[LOC_FIRST_NAME], LEN_FIRST_NAME + 1);
159  strlcpy(middle_name, &buf[LOC_MIDDLE_NAME], LEN_MIDDLE_NAME + 1);
160  strlcpy(office, &buf[LOC_OFFICE], LEN_OFFICE + 1);
161  strlcpy(phone, &buf[LOC_PHONE], LEN_PHONE + 1);
162  strlcpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2 + 1);
163  strlcpy(dept, &buf[LOC_DEPT], LEN_DEPT + 1);
164  strlcpy(title, &buf[LOC_TITLE], LEN_TITLE + 1);
165
166  e.last = strtrim(last_name);
167  e.first = strtrim(first_name);
168  e.middle = strtrim(middle_name);
169
170  ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
171  LookForSt(e.last);
172  LookForO(e.last);
173  LookForJrAndIII(e.last, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
174                  &ends_iv, &ends_v);
175  LookForJrAndIII(e.first, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
176                  &ends_iv, &ends_v);
177
178  e.name = buf;
179  if (*e.middle)
180    sprintf(e.name, "%s %s %s", e.first, e.middle, e.last);
181  else
182    sprintf(e.name, "%s %s", e.first, e.last);
183
184  e.id = id;
185  e.haddr = e.hphone = "";
186
187  /* The following is really gross, but it happens to successfully convert
188   * new-style Warehouse office descriptions into (more-readable) old-style
189   * Personnel Office office descriptions.
190   */
191  e.oaddr = p = strtrim(office);
192  while (*p && !isspace(*p))
193    p++;
194  q = p;
195  while (isspace(*q))
196    q++;
197  if (*q && q < e.oaddr + LEN_OFFICE / 2)
198    {
199      *p++ = '-';
200      while (*q && q < e.oaddr + LEN_OFFICE / 2)
201        {
202          if (*q != ' ' && *q != '-')
203            *p++ = *q;
204          if (q > p)
205            *q = ' ';
206          q++;
207        }
208      memset(p, ' ', q - p);
209    }
210
211  p = e.oaddr + LEN_OFFICE / 2;
212  while (*p && !isspace(*p))
213    p++;
214  q = p;
215  while (isspace(*q))
216    q++;
217  if (*q)
218    {
219      *p++ = '-';
220      while (*q)
221        {
222          if (*q != ' ' && *q != '-')
223            *p++ = *q;
224          if (q > p)
225            *q = ' ';
226          q++;
227        }
228      memset(p, ' ', q - p);
229    }
230  strtrim(e.oaddr);
231  fixaddress(e.oaddr);
232  e.xaddress = e.oaddr;
233
234  e.ophone = e.xphone1 = strtrim(phone);
235  fixphone(e.ophone);
236  e.xphone2 = strtrim(phone2);
237  fixphone(e.xphone2);
238  e.dept = strtrim(dept);
239  e.xtitle = strtrim(title);
240
241  e.type = "MITS";
242  if (strstr(uppercase(e.xtitle), "PROF") ||
243      strstr(uppercase(e.xtitle), "LECTURE"))
244    e.type = "FACULTY";
245  if (!strcmp(e.dept, "LINCOLN LAB"))
246    e.type = "LINCOLN";
247
248  FixCase(e.dept);
249  FixCase(e.xtitle);
250
251  return &e;
252}
Note: See TracBrowser for help on using the repository browser.