3 * Load data into Moira from Personnel Office data file
5 * Copyright (C) 1990-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
12 #include <moira_site.h>
13 #include <moira_schema.h>
20 EXEC SQL INCLUDE sqlca;
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)
47 #define LOC_PHONE (LOC_OFFICE + LEN_OFFICE)
49 #define LOC_PHONE2 (LOC_PHONE + LEN_PHONE)
51 #define LOC_DEPT (LOC_PHONE2 + LEN_PHONE2)
53 #define LOC_TITLE (LOC_DEPT + LEN_DEPT)
56 struct entry *get_next_entry(FILE *in);
57 void process_entry(struct entry *e, int secure);
59 EXEC SQL BEGIN DECLARE SECTION;
61 char *prog = "stafload";
62 EXEC SQL END DECLARE SECTION;
66 int main(int argc, char **argv)
71 char buf[80], *file = NULL;
72 EXEC SQL BEGIN DECLARE SECTION;
74 EXEC SQL END DECLARE SECTION;
76 whoami = strrchr(argv[0], '/');
82 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
83 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
85 for (i = 1; i < argc; i++)
87 if (!strcmp(argv[i], "-w"))
91 fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
100 fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
104 in = fopen(file, "r");
107 fprintf(stderr, "Unable to open %s for input\n", file);
111 initialize_sms_error_table();
113 EXEC SQL CONNECT :db IDENTIFIED BY :db;
116 dbmserr("opening database", sqlca.sqlcode);
120 EXEC SQL SELECT users_id INTO :who FROM users WHERE login = 'root';
122 while ((e = get_next_entry(in)))
125 EXEC SQL COMMIT WORK;
128 dbmserr("committing work", sqlca.sqlcode);
135 fgets(buf, sizeof(buf), stdin);
142 struct entry *get_next_entry(FILE *in)
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;
153 if (!fgets(buf, sizeof(buf), in))
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);
166 e.last = strtrim(last_name);
167 e.first = strtrim(first_name);
168 e.middle = strtrim(middle_name);
170 ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
173 LookForJrAndIII(e.last, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
175 LookForJrAndIII(e.first, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
180 sprintf(e.name, "%s %s %s", e.first, e.middle, e.last);
182 sprintf(e.name, "%s %s", e.first, e.last);
185 e.haddr = e.hphone = "";
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.
191 e.oaddr = p = strtrim(office);
192 while (*p && !isspace(*p))
197 if (*q && q < e.oaddr + LEN_OFFICE / 2)
200 while (*q && q < e.oaddr + LEN_OFFICE / 2)
202 if (*q != ' ' && *q != '-')
208 memset(p, ' ', q - p);
211 p = e.oaddr + LEN_OFFICE / 2;
212 while (*p && !isspace(*p))
222 if (*q != ' ' && *q != '-')
228 memset(p, ' ', q - p);
232 e.xaddress = e.oaddr;
234 e.ophone = e.xphone1 = strtrim(phone);
236 e.xphone2 = strtrim(phone2);
238 e.dept = strtrim(dept);
239 e.xtitle = strtrim(title);
242 if (strstr(uppercase(e.xtitle), "PROF") ||
243 strstr(uppercase(e.xtitle), "LECTURE"))
245 if (!strcmp(e.dept, "LINCOLN LAB"))