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;
27 * name (last, first middle) [30]
37 #define LOC_NAME (LOC_ID + LEN_ID)
39 #define LOC_OFFICE (LOC_NAME + LEN_NAME)
41 #define LOC_PHONE (LOC_OFFICE + LEN_OFFICE)
43 #define LOC_PHONE2 (LOC_PHONE + LEN_PHONE)
45 #define LOC_DEPT (LOC_PHONE2 + LEN_PHONE2)
47 #define LOC_TITLE (LOC_DEPT + LEN_DEPT)
50 struct entry *get_next_entry(FILE *in);
51 void process_entry(struct entry *e, int secure);
53 EXEC SQL BEGIN DECLARE SECTION;
55 char *prog = "stafload";
56 EXEC SQL END DECLARE SECTION;
60 int main(int argc, char **argv)
65 char buf[80], *file = NULL;
66 EXEC SQL BEGIN DECLARE SECTION;
68 EXEC SQL END DECLARE SECTION;
70 whoami = strrchr(argv[0], '/');
76 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
77 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
79 for (i = 1; i < argc; i++)
81 if (!strcmp(argv[i], "-w"))
85 fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
94 fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
98 in = fopen(file, "r");
101 fprintf(stderr, "Unable to open %s for input\n", file);
105 initialize_sms_error_table();
107 EXEC SQL CONNECT :db IDENTIFIED BY :db;
110 dbmserr("opening database", sqlca.sqlcode);
114 EXEC SQL SELECT users_id INTO :who FROM users WHERE login = 'root';
116 while ((e = get_next_entry(in)))
119 EXEC SQL COMMIT WORK;
122 dbmserr("committing work", sqlca.sqlcode);
129 fgets(buf, sizeof(buf), stdin);
136 struct entry *get_next_entry(FILE *in)
138 static struct entry e;
139 static char buf[BUFSIZ];
140 static char name[LEN_NAME + 1], id[LEN_ID + 1];
141 static char office[LEN_OFFICE + 1], phone[LEN_PHONE + 1];
142 static char phone2[LEN_PHONE2 + 1], dept[LEN_DEPT + 1], title[LEN_TITLE + 1];
143 int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
146 if (!fgets(buf, sizeof(buf), in))
149 strlcpy(id, &buf[LOC_ID], LEN_ID + 1);
150 strlcpy(name, &buf[LOC_NAME], LEN_NAME + 1);
151 strlcpy(office, &buf[LOC_OFFICE], LEN_OFFICE + 1);
152 strlcpy(phone, &buf[LOC_PHONE], LEN_PHONE + 1);
153 strlcpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2 + 1);
154 strlcpy(dept, &buf[LOC_DEPT], LEN_DEPT + 1);
155 strlcpy(title, &buf[LOC_TITLE], LEN_TITLE + 1);
157 p = strchr(name, ',');
160 e.last = strtrim(name);
167 p = strchr(e.first, ' ');
171 e.first = strtrim(e.first);
172 e.middle = strtrim(p + 1);
176 e.first = strtrim(e.first);
185 ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
188 LookForJrAndIII(e.last, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
190 LookForJrAndIII(e.first, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
198 sprintf(e.name, "%s %s %s", e.first, e.middle, e.last);
200 sprintf(e.name, "%s %s", e.first, e.last);
203 e.haddr = e.hphone = "";
205 /* The following is really gross, but it happens to successfully convert
206 * new-style Warehouse office descriptions into (more-readable) old-style
207 * Personnel Office office descriptions.
209 e.oaddr = p = strtrim(office);
210 while (*p && !isspace(*p))
215 if (*q && q < e.oaddr + LEN_OFFICE / 2)
218 while (*q && q < e.oaddr + LEN_OFFICE / 2)
220 if (*q != ' ' && *q != '-')
226 memset(p, ' ', q - p);
229 p = e.oaddr + LEN_OFFICE / 2;
230 while (*p && !isspace(*p))
240 if (*q != ' ' && *q != '-')
246 memset(p, ' ', q - p);
250 e.xaddress = e.oaddr;
252 e.ophone = e.xphone1 = strtrim(phone);
254 e.xphone2 = strtrim(phone2);
256 e.dept = strtrim(dept);
257 e.xtitle = strtrim(title);
260 if (strstr(e.xtitle, "PROF") || strstr(e.xtitle, "LECTURE"))
262 if (!strcmp(e.dept, "LINCOLN LAB"))