]> andersk Git - moira.git/blob - regtape/student.pc
rototill staff and student load programs
[moira.git] / regtape / student.pc
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>.
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 <stdlib.h>
19 #include <string.h>
20 #include <time.h>
21
22 EXEC SQL INCLUDE sqlca;
23
24 RCSID("$Header$");
25
26 /* File format is:
27
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
42 */
43
44 #define LOC_ID 0
45 #define LEN_ID 9
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)
57 #define LEN_ADDRESS 30
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;
75
76 char *whoami;
77
78 struct entry *get_next_entry(FILE *in);
79 void process_entry(struct entry *e, int secure);
80
81 int main(int argc, char **argv)
82 {
83   FILE *in;
84   struct entry *e;
85   int i, wait = 0;
86   char buf[80], *file = NULL;
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++;
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
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, 1);
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
148
149 struct entry *get_next_entry(FILE *in)
150 {
151   static struct entry e;
152   static char buf[BUFSIZ], classbuf[10];
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];
157   static char year[LEN_YEAR + 1], address[LEN_ADDRESS + 1];
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];
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
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;
211   ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0;
212   LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
213                   &ends_ii, &ends_v);
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);
221
222   e.id = id;
223
224   e.xtitle = title;
225   if (year[0] == 'G')
226     {
227       e.type = "G";
228       sprintf(title, "Grad Student");
229     }
230   else
231     {
232       e.type = classbuf;
233       sprintf(classbuf, "%d", nyear + 4 - atoi(year) + 1900);
234       sprintf(title, "Undergrad (class of %s)", classbuf);
235     }
236
237   e.dept = course;
238
239   e.oaddr = oaddr;
240   fixaddress(e.oaddr);
241   e.ophone = e.xphone2 = ophone;
242   fixphone(e.ophone);
243
244   e.haddr = addrbuf;
245   strlcpy(e.haddr, address, sizeof(addrbuf));
246   if (*city)
247     {
248       strlcat(e.haddr, " ", sizeof(addrbuf));
249       strlcat(e.haddr, city, sizeof(addrbuf));
250     }
251   if (*state)
252     {
253       strlcat(e.haddr, " ", sizeof(addrbuf));
254       strlcat(e.haddr, state, sizeof(addrbuf));
255       strlcat(e.haddr, zip, sizeof(addrbuf));
256     }
257   fixaddress(e.haddr);
258
259   e.hphone = e.xphone1 = phone;
260   fixphone(e.hphone);
261
262   e.xaddress = xaddrbuf;
263   strlcpy(e.xaddress, address, sizeof(xaddrbuf));
264   strlcat(e.xaddress, " |", sizeof(xaddrbuf));
265   if (*city)
266     {
267       strlcat(e.xaddress, city, sizeof(xaddrbuf));
268       if (*state)
269         {
270           strlcat(e.xaddress, " ", sizeof(xaddrbuf));
271           strlcat(e.xaddress, state, sizeof(xaddrbuf));
272           strlcat(e.xaddress, zip, sizeof(xaddrbuf));
273         }
274     }
275   else
276     strlcat(e.xaddress, "MIT INTERDEPARTMENTAL MAIL", sizeof(xaddrbuf));
277
278   return &e;
279 }
This page took 0.228149 seconds and 5 git commands to generate.