source: trunk/third/moira/regtape/student.pc @ 25455

Revision 25455, 7.2 KB checked in by jdreed, 12 years ago (diff)
In moira: * Re-snapshot moira at r4073 to pick up new changes to clients; the eunice issue described in the previous entry is no longer relevant
Line 
1/* $id: student.pc 3956 2010-01-05 20:56:56Z zacheiss $
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
22EXEC SQL INCLUDE sqlca;
23
24RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/regtape/student.pc $ $Id: student.pc 4060 2011-12-01 19:50:15Z zacheiss $");
25
26/* File format is:
27
280-8     MIT ID
299-38    last name
3039-68   first name
3169-98   middle name
3299      year
33100-103 department
34104-133 address
35134-173 city
36174-175 state
37176-184 zip code
38185-204 phone
39205-234 office address
40235-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#define LOC_REG_TYPE (LOC_OPHONE + LEN_OPHONE)
71#define LEN_REG_TYPE 50
72
73EXEC SQL BEGIN DECLARE SECTION;
74int who;
75char *prog = "stuload";
76EXEC SQL END DECLARE SECTION;
77
78char *whoami;
79
80struct entry *get_next_entry(FILE *in);
81void process_entry(struct entry *e, int secure);
82
83int main(int argc, char **argv)
84{
85  FILE *in;
86  struct entry *e;
87  int i, wait = 0;
88  char buf[80], *file = NULL;
89  EXEC SQL BEGIN DECLARE SECTION;
90  char *db = "moira";
91  EXEC SQL END DECLARE SECTION;
92
93  whoami = strrchr(argv[0], '/');
94  if (whoami)
95    whoami++;
96  else
97    whoami = argv[0];
98
99  setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
100  setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
101
102  for (i = 1; i < argc; i++)
103    {
104      if (!strcmp(argv[i], "-w"))
105        wait++;
106      if (file)
107        {
108          fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
109          exit(1);
110        }
111      else
112        file = argv[i];
113    }
114
115  if (!file)
116    {
117      fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
118      exit(1);
119    }
120
121  in = fopen(file, "r");
122  if (!in)
123    {
124      fprintf(stderr, "Unable to open %s for input\n", file);
125      exit(1);
126    }
127
128  initialize_sms_error_table();
129
130  EXEC SQL CONNECT :db IDENTIFIED BY :db;
131  if (sqlca.sqlcode)
132    {
133      dbmserr("connecting", sqlca.sqlcode);
134      exit(1);
135    }
136
137  EXEC SQL SELECT users_id INTO :who FROM users WHERE login = 'root';
138
139  while ((e = get_next_entry(in)))
140    {
141      /* Don't require secure registration for cross-registered or
142       * special students.
143       */
144      if (!strcmp(e->reg_type, "Cross-Registered") ||
145          !strcmp(e->reg_type, "Special"))
146        process_entry(e, 0);   
147      else
148        process_entry(e, 1);
149      EXEC SQL COMMIT WORK;
150      if (sqlca.sqlcode)
151        {
152          dbmserr("committing work", sqlca.sqlcode);
153          exit(1);
154        }
155      if (wait)
156        {
157          printf("Next");
158          fflush(stdout);
159          fgets(buf, sizeof(buf), stdin);
160        }
161    }
162
163  exit(0);
164}
165
166
167struct entry *get_next_entry(FILE *in)
168{
169  static struct entry e;
170  static char buf[BUFSIZ], classbuf[10];
171  static char namebuf[LEN_FIRST + LEN_MIDDLE + LEN_LAST + 2];
172  static char addrbuf[USERS_HOME_ADDR_SIZE], xaddrbuf[USERS_XADDRESS_SIZE];
173  static char first[LEN_FIRST + 1], last[LEN_LAST + 1], middle[LEN_MIDDLE + 1];
174  static char id[LEN_ID + 1], course[LEN_COURSE + 1];
175  static char year[LEN_YEAR + 1], address[LEN_ADDRESS + 1];
176  static char city[LEN_CITY + 1];
177  static char state[LEN_STATE + 1], phone[LEN_PHONE + 1];
178  static char ophone[LEN_OPHONE + 1], title[128];
179  static char zip[LEN_ZIP + 1], oaddr[LEN_OADDR + 1];
180  static char reg_type[LEN_REG_TYPE + 1];
181  static int nyear = 0;
182  int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v;
183  char *p;
184
185  if (nyear == 0)
186    {
187      struct tm *tm;
188      struct timeval tv;
189
190      gettimeofday(&tv, NULL);
191      tm = localtime(&tv.tv_sec);
192      nyear = tm->tm_year;
193      if (tm->tm_mon >= 5)
194        nyear++;
195    }
196
197  if (!fgets(buf, sizeof(buf), in))
198    return NULL;
199
200  strlcpy(id, &buf[LOC_ID], LEN_ID + 1);
201  strtrim(id);
202  strlcpy(last, &buf[LOC_LAST], LEN_LAST + 1);
203  strtrim(last);
204  strlcpy(first, &buf[LOC_FIRST], LEN_FIRST + 1);
205  strtrim(first);
206  strlcpy(middle, &buf[LOC_MIDDLE], LEN_MIDDLE + 1);
207  strtrim(middle);
208  strlcpy(year, &buf[LOC_YEAR], LEN_YEAR + 1);
209  strtrim(year);
210  strlcpy(course, &buf[LOC_COURSE], LEN_COURSE + 1);
211  strtrim(course);
212  strlcpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS + 1);
213  strtrim(address);
214  strlcpy(city, &buf[LOC_CITY], LEN_CITY + 1);
215  strtrim(city);
216  strlcpy(state, &buf[LOC_STATE], LEN_STATE + 1);
217  strtrim(state);
218  strlcpy(zip, &buf[LOC_ZIP], LEN_ZIP + 1);
219  strtrim(zip);
220  strlcpy(phone, &buf[LOC_PHONE], LEN_PHONE + 1);
221  strtrim(phone);
222  strlcpy(oaddr, &buf[LOC_OADDR], LEN_OADDR + 1);
223  strtrim(oaddr);
224  strlcpy(ophone, &buf[LOC_OPHONE], LEN_OPHONE + 1);
225  strtrim(ophone);
226  strlcpy(reg_type, &buf[LOC_REG_TYPE], LEN_REG_TYPE + 1);
227  strtrim(reg_type);
228
229  e.first = first;
230  e.last = last;
231  e.middle = middle;
232  ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0;
233  LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
234                  &ends_ii, &ends_v);
235  if (middle[1] == '.' && middle[2] == '\0')
236    middle[1] = '\0';
237  e.name = namebuf;
238  if (*middle)
239    sprintf(e.name, "%s %s %s", first, middle, last);
240  else
241    sprintf(e.name, "%s %s", first, last);
242
243  e.id = id;
244
245  e.xtitle = title;
246  if (year[0] == 'G' || year[0] == 'N')
247    {
248      e.type = "G";
249      sprintf(title, "Grad Student");
250    }
251  else
252    {
253      e.type = classbuf;
254      sprintf(classbuf, "%d", nyear + 4 - atoi(year) + 1900);
255      sprintf(title, "Undergrad (class of %s)", classbuf);
256    }
257
258  e.dept = course;
259
260  /* Used to detect cross-registered students. */
261  e.reg_type = reg_type;
262
263  e.oaddr = oaddr;
264  fixaddress(e.oaddr);
265  e.ophone = e.xphone2 = ophone;
266  fixphone(e.ophone);
267
268  e.haddr = addrbuf;
269  strlcpy(e.haddr, address, sizeof(addrbuf));
270  if (*city)
271    {
272      strlcat(e.haddr, " ", sizeof(addrbuf));
273      strlcat(e.haddr, city, sizeof(addrbuf));
274    }
275  if (*state)
276    {
277      strlcat(e.haddr, " ", sizeof(addrbuf));
278      strlcat(e.haddr, state, sizeof(addrbuf));
279      strlcat(e.haddr, zip, sizeof(addrbuf));
280    }
281  fixaddress(e.haddr);
282
283  e.hphone = e.xphone1 = phone;
284  fixphone(e.hphone);
285
286  e.xaddress = xaddrbuf;
287  strlcpy(e.xaddress, address, sizeof(xaddrbuf));
288  strlcat(e.xaddress, " |", sizeof(xaddrbuf));
289  if (*city)
290    {
291      strlcat(e.xaddress, city, sizeof(xaddrbuf));
292      if (*state)
293        {
294          strlcat(e.xaddress, " ", sizeof(xaddrbuf));
295          strlcat(e.xaddress, state, sizeof(xaddrbuf));
296          strlcat(e.xaddress, zip, sizeof(xaddrbuf));
297        }
298    }
299  else
300    strlcat(e.xaddress, "MIT INTERDEPARTMENTAL MAIL", sizeof(xaddrbuf));
301
302  return &e;
303}
Note: See TracBrowser for help on using the repository browser.