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

Revision 26024, 6.5 KB checked in by jdreed, 11 years ago (diff)
In moira: * Snapshot moira at r4113 to pick up new firewall-related changes
RevLine 
[26024]1/* $Id: staff.pc 4111 2013-05-22 20:26:51Z zacheiss $
[23095]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
[26024]22RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/regtape/staff.pc $ $Id: staff.pc 4111 2013-05-22 20:26:51Z zacheiss $");
[23095]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]
[26024]35 * employee type [40]
36 * email address [100]
[23095]37 */
38
39#define LOC_ID 0
40#define LEN_ID 9
41#define LOC_LAST_NAME (LOC_ID + LEN_ID)
42#define LEN_LAST_NAME 30
43#define LOC_FIRST_NAME (LOC_LAST_NAME + LEN_LAST_NAME)
44#define LEN_FIRST_NAME 30
45#define LOC_MIDDLE_NAME (LOC_FIRST_NAME + LEN_FIRST_NAME)
46#define LEN_MIDDLE_NAME 30
47#define LOC_OFFICE (LOC_MIDDLE_NAME + LEN_MIDDLE_NAME)
48#define LEN_OFFICE 24
49#define LOC_PHONE (LOC_OFFICE + LEN_OFFICE)
50#define LEN_PHONE 12
51#define LOC_PHONE2 (LOC_PHONE + LEN_PHONE)
52#define LEN_PHONE2 12
53#define LOC_DEPT (LOC_PHONE2 + LEN_PHONE2)
54#define LEN_DEPT 50
55#define LOC_TITLE (LOC_DEPT + LEN_DEPT)
56#define LEN_TITLE 50
[26024]57#define LOC_EMPLOYEE_TYPE (LOC_TITLE + LEN_TITLE)
58#define LEN_EMPLOYEE_TYPE 40
59#define LOC_EMAIL_ADDRESS (LOC_EMPLOYEE_TYPE + LEN_EMPLOYEE_TYPE)
60#define LEN_EMAIL_ADDRESS 100
[23095]61
62struct entry *get_next_entry(FILE *in);
63void process_entry(struct entry *e, int secure);
64
65EXEC SQL BEGIN DECLARE SECTION;
66int who;
67char *prog = "stafload";
68EXEC SQL END DECLARE SECTION;
69
70char *whoami;
71
72int main(int argc, char **argv)
73{
74  FILE *in;
75  struct entry *e;
76  int i, wait = 0;
77  char buf[80], *file = NULL;
78  EXEC SQL BEGIN DECLARE SECTION;
79  char *db = "moira";
80  EXEC SQL END DECLARE SECTION;
81
82  whoami = strrchr(argv[0], '/');
83  if (whoami)
84    whoami++;
85  else
86    whoami = argv[0];
87
88  setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
89  setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
90
91  for (i = 1; i < argc; i++)
92    {
93      if (!strcmp(argv[i], "-w"))
94        wait++;
95      else if (file)
96        {
97          fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
98          exit(1);
99        }
100      else
101        file = argv[i];
102    }
103
104  if (!file)
105    {
106      fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
107      exit(1);
108    }
109
110  in = fopen(file, "r");
111  if (!in)
112    {
113      fprintf(stderr, "Unable to open %s for input\n", file);
114      exit(1);
115    }
116
117  initialize_sms_error_table();
118
119  EXEC SQL CONNECT :db IDENTIFIED BY :db;
120  if (sqlca.sqlcode)
121    {
122      dbmserr("opening database", sqlca.sqlcode);
123      exit(1);
124    }
125
126  EXEC SQL SELECT users_id INTO :who FROM users WHERE login = 'root';
127
128  while ((e = get_next_entry(in)))
129    {
130      process_entry(e, 0);
131      EXEC SQL COMMIT WORK;
132      if (sqlca.sqlcode)
133        {
134          dbmserr("committing work", sqlca.sqlcode);
135          exit(1);
136        }
137      if (wait)
138        {
139          printf("Next");
140          fflush(stdout);
141          fgets(buf, sizeof(buf), stdin);
142        }
143    }
144
145  exit(0);
146}
147
148struct entry *get_next_entry(FILE *in)
149{
150  static struct entry e;
151  static char buf[BUFSIZ];
152  static char last_name[LEN_LAST_NAME + 1], id[LEN_ID + 1];
153  static char first_name[LEN_FIRST_NAME + 1], middle_name[LEN_MIDDLE_NAME + 1];
154  static char office[LEN_OFFICE + 1], phone[LEN_PHONE + 1];
155  static char phone2[LEN_PHONE2 + 1], dept[LEN_DEPT + 1], title[LEN_TITLE + 1];
[26024]156  static char employee_type[LEN_EMPLOYEE_TYPE + 1], email_address[LEN_EMAIL_ADDRESS + 1];
[23095]157  int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
158  char *p, *q;
159
160  if (!fgets(buf, sizeof(buf), in))
161    return NULL;
162
163  strlcpy(id, &buf[LOC_ID], LEN_ID + 1);
164  strlcpy(last_name, &buf[LOC_LAST_NAME], LEN_LAST_NAME + 1);
165  strlcpy(first_name, &buf[LOC_FIRST_NAME], LEN_FIRST_NAME + 1);
166  strlcpy(middle_name, &buf[LOC_MIDDLE_NAME], LEN_MIDDLE_NAME + 1);
167  strlcpy(office, &buf[LOC_OFFICE], LEN_OFFICE + 1);
168  strlcpy(phone, &buf[LOC_PHONE], LEN_PHONE + 1);
169  strlcpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2 + 1);
170  strlcpy(dept, &buf[LOC_DEPT], LEN_DEPT + 1);
171  strlcpy(title, &buf[LOC_TITLE], LEN_TITLE + 1);
[26024]172  strlcpy(employee_type, &buf[LOC_EMPLOYEE_TYPE], LEN_EMPLOYEE_TYPE + 1);
173  strlcpy(email_address, &buf[LOC_EMAIL_ADDRESS], LEN_EMAIL_ADDRESS + 1);
[23095]174
175  e.last = strtrim(last_name);
176  e.first = strtrim(first_name);
177  e.middle = strtrim(middle_name);
178
179  ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
180  LookForSt(e.last);
181  LookForO(e.last);
182  LookForJrAndIII(e.last, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
183                  &ends_iv, &ends_v);
184  LookForJrAndIII(e.first, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
185                  &ends_iv, &ends_v);
186
187  e.name = buf;
188  if (*e.middle)
189    sprintf(e.name, "%s %s %s", e.first, e.middle, e.last);
190  else
191    sprintf(e.name, "%s %s", e.first, e.last);
192
193  e.id = id;
194  e.haddr = e.hphone = "";
195
[25198]196  /* Not used for employees. */
[25388]197  e.reg_type = "";
[25198]198
[23095]199  /* The following is really gross, but it happens to successfully convert
200   * new-style Warehouse office descriptions into (more-readable) old-style
201   * Personnel Office office descriptions.
202   */
203  e.oaddr = p = strtrim(office);
204  while (*p && !isspace(*p))
205    p++;
206  q = p;
207  while (isspace(*q))
208    q++;
209  if (*q && q < e.oaddr + LEN_OFFICE / 2)
210    {
211      *p++ = '-';
212      while (*q && q < e.oaddr + LEN_OFFICE / 2)
213        {
214          if (*q != ' ' && *q != '-')
215            *p++ = *q;
216          if (q > p)
217            *q = ' ';
218          q++;
219        }
220      memset(p, ' ', q - p);
221    }
222
223  p = e.oaddr + LEN_OFFICE / 2;
224  while (*p && !isspace(*p))
225    p++;
226  q = p;
227  while (isspace(*q))
228    q++;
229  if (*q)
230    {
231      *p++ = '-';
232      while (*q)
233        {
234          if (*q != ' ' && *q != '-')
235            *p++ = *q;
236          if (q > p)
237            *q = ' ';
238          q++;
239        }
240      memset(p, ' ', q - p);
241    }
242  strtrim(e.oaddr);
243  fixaddress(e.oaddr);
244  e.xaddress = e.oaddr;
245
246  e.ophone = e.xphone1 = strtrim(phone);
247  fixphone(e.ophone);
248  e.xphone2 = strtrim(phone2);
249  fixphone(e.xphone2);
250  e.dept = strtrim(dept);
251  e.xtitle = strtrim(title);
252
253  e.type = "MITS";
[26024]254
[25198]255  if ((strstr(uppercase(e.xtitle), "PROF") &&
256       !strstr(uppercase(e.xtitle), "PROFESSION")) ||
[23095]257      strstr(uppercase(e.xtitle), "LECTURE"))
258    e.type = "FACULTY";
[25455]259  if (strstr(uppercase(e.dept), "LINCOLN LAB"))
[23095]260    e.type = "LINCOLN";
261
262  FixCase(e.dept);
263  FixCase(e.xtitle);
264
[26024]265  e.affiliation_detailed = strtrim(employee_type);
266  if (!strcmp(e.affiliation_detailed, "Faculty"))
267    e.affiliation_basic = "faculty";
268  else
269    e.affiliation_basic = "staff";
270
271  e.email_address = strtrim(email_address);
272
[23095]273  return &e;
274}
Note: See TracBrowser for help on using the repository browser.