]>
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++; | |
5eaef520 | 104 | else if (file) |
105 | fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami); | |
106 | else | |
107 | file = argv[i]; | |
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("connecting", sqlca.sqlcode); | |
123 | exit(1); | |
124 | } | |
125 | ||
f0df8832 | 126 | EXEC SQL SELECT users_id INTO :who FROM users WHERE login = 'root'; |
127 | ||
5eaef520 | 128 | while ((e = get_next_entry(in))) |
129 | { | |
f0df8832 | 130 | process_entry(e, 1); |
5eaef520 | 131 | EXEC SQL COMMIT WORK; |
132 | if (sqlca.sqlcode) | |
133 | { | |
9f5e5c05 | 134 | dbmserr("committing work", sqlca.sqlcode); |
135 | exit(1); | |
837b0ec6 | 136 | } |
5eaef520 | 137 | if (wait) |
138 | { | |
139 | printf("Next"); | |
140 | fflush(stdout); | |
dfaf9b68 | 141 | fgets(buf, sizeof(buf), stdin); |
093c49c9 | 142 | } |
143 | } | |
144 | ||
5eaef520 | 145 | exit(0); |
8638fcf9 | 146 | } |
093c49c9 | 147 | |
148 | ||
5eaef520 | 149 | struct entry *get_next_entry(FILE *in) |
093c49c9 | 150 | { |
5eaef520 | 151 | static struct entry e; |
0e5cfe5f | 152 | static char buf[BUFSIZ], classbuf[10]; |
f0df8832 | 153 | static char namebuf[LEN_FIRST + LEN_MIDDLE + LEN_LAST + 2]; |
154 | static char addrbuf[USERS_HOME_ADDR_SIZE], xaddrbuf[USERS_XADDRESS_SIZE]; | |
155 | static char first[LEN_FIRST + 1], last[LEN_LAST + 1], middle[LEN_MIDDLE + 1]; | |
156 | static char id[LEN_ID + 1], course[LEN_COURSE + 1]; | |
5eaef520 | 157 | static char year[LEN_YEAR + 1], address[LEN_ADDRESS + 1]; |
f0df8832 | 158 | static char city[LEN_CITY + 1]; |
159 | static char state[LEN_STATE + 1], phone[LEN_PHONE + 1]; | |
160 | static char ophone[LEN_OPHONE + 1], title[128]; | |
161 | static char zip[LEN_ZIP + 1], oaddr[LEN_OADDR + 1]; | |
5eaef520 | 162 | static int nyear = 0; |
163 | int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v; | |
164 | char *p; | |
165 | ||
166 | if (nyear == 0) | |
167 | { | |
168 | struct tm *tm; | |
169 | struct timeval tv; | |
170 | ||
171 | gettimeofday(&tv, NULL); | |
172 | tm = localtime(&tv.tv_sec); | |
173 | nyear = tm->tm_year; | |
174 | if (tm->tm_mon > 5) | |
175 | nyear++; | |
176 | } | |
177 | ||
178 | if (!fgets(buf, sizeof(buf), in)) | |
179 | return NULL; | |
180 | ||
f0df8832 | 181 | strlcpy(id, &buf[LOC_ID], LEN_ID + 1); |
182 | strtrim(id); | |
183 | strlcpy(last, &buf[LOC_LAST], LEN_LAST + 1); | |
184 | strtrim(last); | |
185 | strlcpy(first, &buf[LOC_FIRST], LEN_FIRST + 1); | |
186 | strtrim(first); | |
187 | strlcpy(middle, &buf[LOC_MIDDLE], LEN_MIDDLE + 1); | |
188 | strtrim(middle); | |
189 | strlcpy(year, &buf[LOC_YEAR], LEN_YEAR + 1); | |
190 | strtrim(year); | |
191 | strlcpy(course, &buf[LOC_COURSE], LEN_COURSE + 1); | |
192 | strtrim(course); | |
193 | strlcpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS + 1); | |
194 | strtrim(address); | |
195 | strlcpy(city, &buf[LOC_CITY], LEN_CITY + 1); | |
196 | strtrim(city); | |
197 | strlcpy(state, &buf[LOC_STATE], LEN_STATE + 1); | |
198 | strtrim(state); | |
199 | strlcpy(zip, &buf[LOC_ZIP], LEN_ZIP + 1); | |
200 | strtrim(zip); | |
201 | strlcpy(phone, &buf[LOC_PHONE], LEN_PHONE + 1); | |
202 | strtrim(phone); | |
203 | strlcpy(oaddr, &buf[LOC_OADDR], LEN_OADDR + 1); | |
204 | strtrim(oaddr); | |
205 | strlcpy(ophone, &buf[LOC_OPHONE], LEN_OPHONE + 1); | |
206 | strtrim(ophone); | |
207 | ||
208 | e.first = first; | |
209 | e.last = last; | |
210 | e.middle = middle; | |
5eaef520 | 211 | ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0; |
5eaef520 | 212 | LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv, |
213 | &ends_ii, &ends_v); | |
f0df8832 | 214 | if (middle[1] == '.' && middle[2] == '\0') |
215 | middle[1] = '\0'; | |
216 | e.name = namebuf; | |
217 | if (*middle) | |
218 | sprintf(e.name, "%s %s %s", first, middle, last); | |
219 | else | |
220 | sprintf(e.name, "%s %s", first, last); | |
5eaef520 | 221 | |
222 | e.id = id; | |
5eaef520 | 223 | |
f0df8832 | 224 | e.xtitle = title; |
225 | if (year[0] == 'G') | |
5eaef520 | 226 | { |
f0df8832 | 227 | e.type = "G"; |
5eaef520 | 228 | sprintf(title, "Grad Student"); |
229 | } | |
230 | else | |
231 | { | |
f0df8832 | 232 | e.type = classbuf; |
233 | sprintf(classbuf, "%d", nyear + 4 - atoi(year) + 1900); | |
5eaef520 | 234 | sprintf(title, "Undergrad (class of %s)", classbuf); |
235 | } | |
236 | ||
f0df8832 | 237 | e.dept = course; |
093c49c9 | 238 | |
f0df8832 | 239 | e.oaddr = oaddr; |
240 | fixaddress(e.oaddr); | |
241 | e.ophone = e.xphone2 = ophone; | |
242 | fixphone(e.ophone); | |
093c49c9 | 243 | |
f0df8832 | 244 | e.haddr = addrbuf; |
245 | strlcpy(e.haddr, address, sizeof(addrbuf)); | |
246 | if (*city) | |
5eaef520 | 247 | { |
f0df8832 | 248 | strlcat(e.haddr, " ", sizeof(addrbuf)); |
249 | strlcat(e.haddr, city, sizeof(addrbuf)); | |
5eaef520 | 250 | } |
f0df8832 | 251 | if (*state) |
5eaef520 | 252 | { |
f0df8832 | 253 | strlcat(e.haddr, " ", sizeof(addrbuf)); |
254 | strlcat(e.haddr, state, sizeof(addrbuf)); | |
255 | strlcat(e.haddr, zip, sizeof(addrbuf)); | |
093c49c9 | 256 | } |
f0df8832 | 257 | fixaddress(e.haddr); |
c48e3c93 | 258 | |
f0df8832 | 259 | e.hphone = e.xphone1 = phone; |
260 | fixphone(e.hphone); | |
c48e3c93 | 261 | |
f0df8832 | 262 | e.xaddress = xaddrbuf; |
263 | strlcpy(e.xaddress, address, sizeof(xaddrbuf)); | |
264 | strlcat(e.xaddress, " |", sizeof(xaddrbuf)); | |
265 | if (*city) | |
5eaef520 | 266 | { |
f0df8832 | 267 | strlcat(e.xaddress, city, sizeof(xaddrbuf)); |
268 | if (*state) | |
5eaef520 | 269 | { |
f0df8832 | 270 | strlcat(e.xaddress, " ", sizeof(xaddrbuf)); |
271 | strlcat(e.xaddress, state, sizeof(xaddrbuf)); | |
272 | strlcat(e.xaddress, zip, sizeof(xaddrbuf)); | |
5eaef520 | 273 | } |
5eaef520 | 274 | } |
275 | else | |
f0df8832 | 276 | strlcat(e.xaddress, "MIT INTERDEPARTMENTAL MAIL", sizeof(xaddrbuf)); |
5eaef520 | 277 | |
f0df8832 | 278 | return &e; |
9f5e5c05 | 279 | } |