]>
Commit | Line | Data |
---|---|---|
7ac48069 | 1 | /* $Id$ |
2 | * | |
3 | * Load data into Moira from Registrar's 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>. | |
093c49c9 | 8 | */ |
9 | ||
7ac48069 | 10 | #include <mit-copyright.h> |
932028de | 11 | #include <moira.h> |
12 | #include <moira_site.h> | |
dfaf9b68 | 13 | #include <moira_schema.h> |
f0df8832 | 14 | #include "common.h" |
7ac48069 | 15 | |
16 | #include <ctype.h> | |
17 | #include <stdio.h> | |
18 | #include <stdlib.h> | |
19 | #include <string.h> | |
20 | #include <time.h> | |
21 | ||
8638fcf9 | 22 | EXEC SQL INCLUDE sqlca; |
093c49c9 | 23 | |
7ac48069 | 24 | RCSID("$Header$"); |
093c49c9 | 25 | |
26 | /* File format is: | |
27 | ||
f0df8832 | 28 | 0-8 MIT ID |
29 | 9-38 last name | |
30 | 39-68 first name | |
31 | 69-98 middle name | |
32 | 99 year | |
33 | 100-103 department | |
34 | 104-133 address | |
35 | 134-173 city | |
36 | 174-175 state | |
37 | 176-184 zip code | |
38 | 185-204 phone | |
39 | 205-234 office address | |
40 | 235-254 office phone | |
41 | ||
093c49c9 | 42 | */ |
43 | ||
f0df8832 | 44 | #define LOC_ID 0 |
093c49c9 | 45 | #define LEN_ID 9 |
f0df8832 | 46 | #define LOC_LAST (LOC_ID + LEN_ID) |
47 | #define LEN_LAST 30 | |
48 | #define LOC_FIRST (LOC_LAST + LEN_LAST) | |
49 | #define LEN_FIRST 30 | |
50 | #define LOC_MIDDLE (LOC_FIRST + LEN_FIRST) | |
51 | #define LEN_MIDDLE 30 | |
52 | #define LOC_YEAR (LOC_MIDDLE + LEN_MIDDLE) | |
53 | #define LEN_YEAR 1 | |
54 | #define LOC_COURSE (LOC_YEAR + LEN_YEAR) | |
55 | #define LEN_COURSE 4 | |
56 | #define LOC_ADDRESS (LOC_COURSE + LEN_COURSE) | |
e3c8ca24 | 57 | #define LEN_ADDRESS 30 |
f0df8832 | 58 | #define LOC_CITY (LOC_ADDRESS + LEN_ADDRESS) |
59 | #define LEN_CITY 40 | |
60 | #define LOC_STATE (LOC_CITY + LEN_CITY) | |
61 | #define LEN_STATE 2 | |
62 | #define LOC_ZIP (LOC_STATE + LEN_STATE) | |
63 | #define LEN_ZIP 9 | |
64 | #define LOC_PHONE (LOC_ZIP + LEN_ZIP) | |
65 | #define LEN_PHONE 20 | |
66 | #define LOC_OADDR (LOC_PHONE + LEN_PHONE) | |
67 | #define LEN_OADDR 30 | |
68 | #define LOC_OPHONE (LOC_OADDR + LEN_OADDR) | |
69 | #define LEN_OPHONE 20 | |
70 | ||
71 | EXEC SQL BEGIN DECLARE SECTION; | |
72 | int who; | |
73 | char *prog = "stuload"; | |
74 | EXEC SQL END DECLARE SECTION; | |
093c49c9 | 75 | |
093c49c9 | 76 | char *whoami; |
77 | ||
7ac48069 | 78 | struct entry *get_next_entry(FILE *in); |
f0df8832 | 79 | void process_entry(struct entry *e, int secure); |
093c49c9 | 80 | |
5eaef520 | 81 | int main(int argc, char **argv) |
8638fcf9 | 82 | { |
5eaef520 | 83 | FILE *in; |
7ac48069 | 84 | struct entry *e; |
5eaef520 | 85 | int i, wait = 0; |
dfaf9b68 | 86 | char buf[80], *file = NULL; |
5eaef520 | 87 | EXEC SQL BEGIN DECLARE SECTION; |
88 | char *db = "moira"; | |
89 | EXEC SQL END DECLARE SECTION; | |
90 | ||
91 | whoami = strrchr(argv[0], '/'); | |
92 | if (whoami) | |
93 | whoami++; | |
94 | else | |
95 | whoami = argv[0]; | |
96 | ||
97 | setvbuf(stdout, NULL, _IOLBF, BUFSIZ); | |
98 | setvbuf(stderr, NULL, _IOLBF, BUFSIZ); | |
99 | ||
100 | for (i = 1; i < argc; i++) | |
101 | { | |
102 | if (!strcmp(argv[i], "-w")) | |
103 | wait++; | |
15e28a76 | 104 | if (file) |
105 | { | |
106 | fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami); | |
107 | exit(1); | |
108 | } | |
5eaef520 | 109 | else |
110 | file = argv[i]; | |
111 | } | |
112 | ||
15e28a76 | 113 | if (!file) |
114 | { | |
115 | fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami); | |
116 | exit(1); | |
117 | } | |
118 | ||
5eaef520 | 119 | in = fopen(file, "r"); |
120 | if (!in) | |
121 | { | |
122 | fprintf(stderr, "Unable to open %s for input\n", file); | |
123 | exit(1); | |
124 | } | |
125 | ||
126 | initialize_sms_error_table(); | |
127 | ||
128 | EXEC SQL CONNECT :db IDENTIFIED BY :db; | |
129 | if (sqlca.sqlcode) | |
130 | { | |
131 | dbmserr("connecting", sqlca.sqlcode); | |
132 | exit(1); | |
133 | } | |
134 | ||
f0df8832 | 135 | EXEC SQL SELECT users_id INTO :who FROM users WHERE login = 'root'; |
136 | ||
5eaef520 | 137 | while ((e = get_next_entry(in))) |
138 | { | |
f0df8832 | 139 | process_entry(e, 1); |
5eaef520 | 140 | EXEC SQL COMMIT WORK; |
141 | if (sqlca.sqlcode) | |
142 | { | |
9f5e5c05 | 143 | dbmserr("committing work", sqlca.sqlcode); |
144 | exit(1); | |
837b0ec6 | 145 | } |
5eaef520 | 146 | if (wait) |
147 | { | |
148 | printf("Next"); | |
149 | fflush(stdout); | |
dfaf9b68 | 150 | fgets(buf, sizeof(buf), stdin); |
093c49c9 | 151 | } |
152 | } | |
153 | ||
5eaef520 | 154 | exit(0); |
8638fcf9 | 155 | } |
093c49c9 | 156 | |
157 | ||
5eaef520 | 158 | struct entry *get_next_entry(FILE *in) |
093c49c9 | 159 | { |
5eaef520 | 160 | static struct entry e; |
0e5cfe5f | 161 | static char buf[BUFSIZ], classbuf[10]; |
f0df8832 | 162 | static char namebuf[LEN_FIRST + LEN_MIDDLE + LEN_LAST + 2]; |
163 | static char addrbuf[USERS_HOME_ADDR_SIZE], xaddrbuf[USERS_XADDRESS_SIZE]; | |
164 | static char first[LEN_FIRST + 1], last[LEN_LAST + 1], middle[LEN_MIDDLE + 1]; | |
165 | static char id[LEN_ID + 1], course[LEN_COURSE + 1]; | |
5eaef520 | 166 | static char year[LEN_YEAR + 1], address[LEN_ADDRESS + 1]; |
f0df8832 | 167 | static char city[LEN_CITY + 1]; |
168 | static char state[LEN_STATE + 1], phone[LEN_PHONE + 1]; | |
169 | static char ophone[LEN_OPHONE + 1], title[128]; | |
170 | static char zip[LEN_ZIP + 1], oaddr[LEN_OADDR + 1]; | |
5eaef520 | 171 | static int nyear = 0; |
172 | int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v; | |
173 | char *p; | |
174 | ||
175 | if (nyear == 0) | |
176 | { | |
177 | struct tm *tm; | |
178 | struct timeval tv; | |
179 | ||
180 | gettimeofday(&tv, NULL); | |
181 | tm = localtime(&tv.tv_sec); | |
182 | nyear = tm->tm_year; | |
41247d24 | 183 | if (tm->tm_mon >= 5) |
5eaef520 | 184 | nyear++; |
185 | } | |
186 | ||
187 | if (!fgets(buf, sizeof(buf), in)) | |
188 | return NULL; | |
189 | ||
f0df8832 | 190 | strlcpy(id, &buf[LOC_ID], LEN_ID + 1); |
191 | strtrim(id); | |
192 | strlcpy(last, &buf[LOC_LAST], LEN_LAST + 1); | |
193 | strtrim(last); | |
194 | strlcpy(first, &buf[LOC_FIRST], LEN_FIRST + 1); | |
195 | strtrim(first); | |
196 | strlcpy(middle, &buf[LOC_MIDDLE], LEN_MIDDLE + 1); | |
197 | strtrim(middle); | |
198 | strlcpy(year, &buf[LOC_YEAR], LEN_YEAR + 1); | |
199 | strtrim(year); | |
200 | strlcpy(course, &buf[LOC_COURSE], LEN_COURSE + 1); | |
201 | strtrim(course); | |
202 | strlcpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS + 1); | |
203 | strtrim(address); | |
204 | strlcpy(city, &buf[LOC_CITY], LEN_CITY + 1); | |
205 | strtrim(city); | |
206 | strlcpy(state, &buf[LOC_STATE], LEN_STATE + 1); | |
207 | strtrim(state); | |
208 | strlcpy(zip, &buf[LOC_ZIP], LEN_ZIP + 1); | |
209 | strtrim(zip); | |
210 | strlcpy(phone, &buf[LOC_PHONE], LEN_PHONE + 1); | |
211 | strtrim(phone); | |
212 | strlcpy(oaddr, &buf[LOC_OADDR], LEN_OADDR + 1); | |
213 | strtrim(oaddr); | |
214 | strlcpy(ophone, &buf[LOC_OPHONE], LEN_OPHONE + 1); | |
215 | strtrim(ophone); | |
216 | ||
217 | e.first = first; | |
218 | e.last = last; | |
219 | e.middle = middle; | |
5eaef520 | 220 | ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0; |
5eaef520 | 221 | LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv, |
222 | &ends_ii, &ends_v); | |
f0df8832 | 223 | if (middle[1] == '.' && middle[2] == '\0') |
224 | middle[1] = '\0'; | |
225 | e.name = namebuf; | |
226 | if (*middle) | |
227 | sprintf(e.name, "%s %s %s", first, middle, last); | |
228 | else | |
229 | sprintf(e.name, "%s %s", first, last); | |
5eaef520 | 230 | |
231 | e.id = id; | |
5eaef520 | 232 | |
f0df8832 | 233 | e.xtitle = title; |
4d47cd81 | 234 | if (year[0] == 'G' || year[0] == 'N') |
5eaef520 | 235 | { |
f0df8832 | 236 | e.type = "G"; |
5eaef520 | 237 | sprintf(title, "Grad Student"); |
238 | } | |
239 | else | |
240 | { | |
f0df8832 | 241 | e.type = classbuf; |
242 | sprintf(classbuf, "%d", nyear + 4 - atoi(year) + 1900); | |
5eaef520 | 243 | sprintf(title, "Undergrad (class of %s)", classbuf); |
244 | } | |
245 | ||
f0df8832 | 246 | e.dept = course; |
093c49c9 | 247 | |
f0df8832 | 248 | e.oaddr = oaddr; |
249 | fixaddress(e.oaddr); | |
250 | e.ophone = e.xphone2 = ophone; | |
251 | fixphone(e.ophone); | |
093c49c9 | 252 | |
f0df8832 | 253 | e.haddr = addrbuf; |
254 | strlcpy(e.haddr, address, sizeof(addrbuf)); | |
255 | if (*city) | |
5eaef520 | 256 | { |
f0df8832 | 257 | strlcat(e.haddr, " ", sizeof(addrbuf)); |
258 | strlcat(e.haddr, city, sizeof(addrbuf)); | |
5eaef520 | 259 | } |
f0df8832 | 260 | if (*state) |
5eaef520 | 261 | { |
f0df8832 | 262 | strlcat(e.haddr, " ", sizeof(addrbuf)); |
263 | strlcat(e.haddr, state, sizeof(addrbuf)); | |
264 | strlcat(e.haddr, zip, sizeof(addrbuf)); | |
093c49c9 | 265 | } |
f0df8832 | 266 | fixaddress(e.haddr); |
c48e3c93 | 267 | |
f0df8832 | 268 | e.hphone = e.xphone1 = phone; |
269 | fixphone(e.hphone); | |
c48e3c93 | 270 | |
f0df8832 | 271 | e.xaddress = xaddrbuf; |
272 | strlcpy(e.xaddress, address, sizeof(xaddrbuf)); | |
273 | strlcat(e.xaddress, " |", sizeof(xaddrbuf)); | |
274 | if (*city) | |
5eaef520 | 275 | { |
f0df8832 | 276 | strlcat(e.xaddress, city, sizeof(xaddrbuf)); |
277 | if (*state) | |
5eaef520 | 278 | { |
f0df8832 | 279 | strlcat(e.xaddress, " ", sizeof(xaddrbuf)); |
280 | strlcat(e.xaddress, state, sizeof(xaddrbuf)); | |
281 | strlcat(e.xaddress, zip, sizeof(xaddrbuf)); | |
5eaef520 | 282 | } |
5eaef520 | 283 | } |
284 | else | |
f0df8832 | 285 | strlcat(e.xaddress, "MIT INTERDEPARTMENTAL MAIL", sizeof(xaddrbuf)); |
5eaef520 | 286 | |
f0df8832 | 287 | return &e; |
9f5e5c05 | 288 | } |