]> andersk Git - moira.git/blob - regtape/rafnu.c
update name if necessary
[moira.git] / regtape / rafnu.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  */
6
7 #ifndef lint
8 static char *rcsid_rafnu_c = "$Header$";
9
10 #endif  lint
11
12
13 /* ReadAndFixNextUser reads in a student record line in the format
14  * of the registrar's tape and hacks on the info to make it conform
15  * to athenareg's standards.  For example, the line
16   
17 123321123 ALBERGHETTI III                        JOANNA MARIA             97
18   
19  * gets processed into:
20  *
21  * -->
22  * {nil, nil, nil, nil,
23  *  last -> "Alberghetti",
24  *  first -> "Joanna III",
25  *  mid_init -> "M",
26  *  nil,
27  *  crypt_ssn -> "aj7.NBbasJbn8"        ; encrypted with salt "aj"
28  *  year -> "97",
29  *  nil}
30  *
31  */
32
33 #include <stdio.h>
34 #include <ctype.h>
35
36 #define LAST_LEN                15
37 #define FIRST_LEN               15
38 #define FULL_NAME_LEN           31
39
40 #define REG_TAPE_LINE_LEN       77
41 #define SSN_OFFSET              0
42 #define SSN_ILEN                9
43 #define LAST_NAME_OFFSET        10
44 #define LAST_NAME_MAX_ILEN      39
45 #define FIRST_NAME_OFFSET       49
46 #define FIRST_NAME_MAX_ILEN     25
47 #define MIT_YEAR_OFFSET         74
48 #define MIT_YEAR_ILEN           2
49
50 #define BUFSIZE  1024
51
52 int 
53 ReadAndFixNextUser(s, querc, querv)
54     FILE *s;
55     int querc;
56     char **querv;
57
58 {
59     char ibuf[BUFSIZE];         /* input line buffer */
60     static lno = 0;             /* line number */
61     int len;                    /* line length */
62     char issn[SSN_ILEN + 1];
63     char ilnm[LAST_NAME_MAX_ILEN + 1];
64     char ifnm[FIRST_NAME_MAX_ILEN + 1 + 6];     /* extra for ", Jr.", et al */
65     char iyr[MIT_YEAR_ILEN + 1];
66     int ends_sr = 0;            /* 1 if name ends in "SR" or "SR." */
67     int ends_jr = 0;            /* 1 if name ends in "JR" or "JR." */
68     int ends_iii = 0;           /* 1 if name ends in "III" */
69     int ends_iv = 0;            /* 1 if name ends in "IV" */
70     char salt[2];
71     register char *p;
72     char *strcat();
73     char *strcpy();
74     char *strncpy();
75     char *crypt();
76
77     for (;;) {                  /* ... well, at least until we get a good
78                                  * input line */
79         if (fgets(ibuf, BUFSIZE, s) == NULL) {
80             fprintf(stderr, "Reached EOF after line %d.\n", lno);
81             return (EOF);       /* reached EOF */
82         }
83 /* 
84  * POTENTIAL  BUG!!!   fgets will not remove \n from input stream
85  *                      May need to manually get that character off
86  */
87         lno++;
88
89         if ((len = strlen(ibuf)) != REG_TAPE_LINE_LEN) {
90             fprintf(stderr, "bad length\n");
91             goto complain_and_continue;
92         }
93
94         /* grab and check input SSN */
95
96         issn[SSN_ILEN] = '\0';
97         (void) strncpy(issn, ibuf + SSN_OFFSET, SSN_ILEN);
98         for (p = issn; *p; p++) {
99             if (!isdigit(*p)) {
100                 fprintf(stderr, "bad char \"%c\"\n", *p);
101                 goto complain_and_continue;
102             }
103         }
104
105         /* check for blank before last name */
106
107         if (!isspace(ibuf[LAST_NAME_OFFSET - 1])) {
108             fprintf(stderr, "not space \"%c\"\n", ibuf[LAST_NAME_OFFSET - 1]);
109             goto complain_and_continue;
110         }
111
112         /* grab input last name */
113
114         ilnm[LAST_NAME_MAX_ILEN] = '\0';
115         (void) strncpy(ilnm, ibuf + LAST_NAME_OFFSET, LAST_NAME_MAX_ILEN);
116         for (p = ilnm; *p; p++) {
117             if (!isalpha(*p) && *p != '-' && *p != ' ' &&
118                 *p != '.' && *p != '\'') {
119                 fprintf(stderr, "bad char in name \"%c\"\n", *p);
120                 goto complain_and_continue;
121             }
122         }
123
124         /* grab input first name */
125
126         ifnm[FIRST_NAME_MAX_ILEN] = '\0';
127         (void) strncpy(ifnm, ibuf + FIRST_NAME_OFFSET, FIRST_NAME_MAX_ILEN);
128
129         for (p = ifnm; *p; p++) {
130             if (!isalpha(*p) && *p != '-' && *p != ' ' && *p != '.') {
131                 fprintf(stderr, "bad char in fname \"%c\"\n", *p);
132                 goto complain_and_continue;
133             }
134         }
135
136         /* grab MIT year */
137
138         iyr[MIT_YEAR_ILEN] = '\0';
139         (void) strncpy(iyr, ibuf + MIT_YEAR_OFFSET, MIT_YEAR_ILEN);
140         if (strcmp(iyr, "GG") && strcmp(iyr, "90") && strcmp(iyr, "88")
141             && strcmp(iyr, "89") && strcmp(iyr, "91")) {
142             fprintf(stderr, "bad year \"%s\"\n", iyr);
143             goto complain_and_continue;
144         }
145
146         /* Okay, got everything, now hack on it */
147
148         /* Last name ... */
149
150         TrimTrailingSpace(ilnm);
151         LookForJrAndIII(ilnm, &ends_sr, &ends_jr, &ends_iii, &ends_iv);
152         LookForSt(ilnm);
153         LookForO(ilnm);
154         FixCase(ilnm);
155         (void) strncpy(querv[4], ilnm, LAST_LEN);
156
157         /* First name  & middle initial ... */
158
159         TrimTrailingSpace(ifnm);
160         LookForJrAndIII(ifnm, &ends_sr, &ends_jr, &ends_iii, &ends_iv);
161
162         GetMidInit(ifnm, querv[6]);
163         FixCase(ifnm);
164         (void) strncpy(querv[5], ifnm, FIRST_LEN);
165
166
167         /* okay, finish up first name */
168         AppendJrOrIII(ifnm, &ends_sr, &ends_jr, &ends_iii, &ends_iv);
169
170         /* MIT Year ... */
171
172         if (!strcmp(iyr, "GG")) {
173             (void) strcpy(querv[9], "G\0");
174         }
175         else {
176             (void) strcpy(querv[9], iyr);
177         }
178
179         /* MIT-ID (Social Security Number) ... */
180
181         salt[0] = tolower(*ilnm);
182         salt[1] = tolower(*ifnm);
183         (void) strcpy(querv[8], crypt(issn + 2, salt)); /* last 7 digits only */
184         (void) strcat(querv[8], "\0");
185         /* That's all, folks */
186
187
188         return (lno);
189
190 complain_and_continue:
191         fprintf(stderr, "\007ERROR on input line no. %d, len %d:\n",
192                 lno, len);
193         fprintf(stderr, "%s\n", ibuf);
194     }
195 }
196
197 TrimTrailingSpace(ip)
198     register char *ip;
199 {
200     register char *p;
201
202     for (p = ip + strlen(ip) - 1; p >= ip && isspace(*p); p--) {
203         *p = '\0';
204     }
205 }
206
207 GetMidInit(nm, mi)
208     register char *nm;          /* truncate at first space, if any such */
209     register char *mi;          /* set to first char after first space, if
210                                  * any such */
211 {
212     while (*nm && !isspace(*nm)) {
213         nm++;
214     }
215     if (*nm) {
216         *nm++ = '\0';
217     }
218     while (*nm && isspace(*nm)) {
219         nm++;
220     }
221     if (*nm) {
222         *mi++ = *nm;
223     }
224     *mi = '\0';
225 }
226
227 AppendJrOrIII(nm, phas_sr, phas_jr, phas_iii, phas_iv)
228     register char *nm;
229     register int *phas_sr;
230     register int *phas_jr;
231     register int *phas_iii;
232     register int *phas_iv;
233 {
234     if (*phas_sr) {
235         (void) strcat(nm, ", Sr.");
236     }
237     else if (*phas_jr) {
238         (void) strcat(nm, ", Jr.");
239     }
240     else if (*phas_iii) {
241         (void) strcat(nm, " III");
242     }
243     else if (*phas_iv) {
244         (void) strcat(nm, " IV");
245     }
246 }
247
248 FixCase(p)
249     register char *p;
250 {
251     register int cflag;         /* convert to lcase, unless at start or
252                                  * following */
253
254     /* a space or punctuation mark (e.g., '-') */
255
256     for (cflag = 0; *p; p++) {
257         if (cflag && isupper(*p)) {
258             *p = tolower(*p);
259         }
260         else if (isspace(*p) || ispunct(*p)) {
261             cflag = 0;
262         }
263         else {
264             cflag = 1;
265         }
266     }
267 }
268
269 LookForJrAndIII(nm, pends_sr, pends_jr, pends_iii, pends_iv)
270     register char *nm;
271     register int *pends_sr;
272     register int *pends_jr;
273     register int *pends_iii;
274     register int *pends_iv;
275 {
276     register int len = strlen(nm);
277
278     if (len >= 4 && !strcmp(nm + len - 3, " SR")) {
279         *pends_sr = 1;
280         nm[len - 3] = '\0';
281     }
282     else if (len >= 4 && !strcmp(nm + len - 3, " JR")) {
283         *pends_jr = 1;
284         nm[len - 3] = '\0';
285     }
286     else if (len >= 4 && !strcmp(nm + len - 3, " IV")) {
287         *pends_iv = 1;
288         nm[len - 3] = '\0';
289     }
290     else if (len >= 5 && !strcmp(nm + len - 4, " SR.")) {
291         *pends_sr = 1;
292         nm[len - 4] = '\0';
293     }
294     else if (len >= 5 && !strcmp(nm + len - 4, " JR.")) {
295         *pends_jr = 1;
296         nm[len - 4] = '\0';
297     }
298     else if (len >= 5 && !strcmp(nm + len - 4, " III")) {
299         *pends_iii = 1;
300         nm[len - 4] = '\0';
301     }
302 }
303
304 LookForSt(nm)                   /* ST PIERRE, etc. */
305     register char *nm;
306 {
307     char temp[256];
308
309     if (!strcmp(nm, "ST ")) {
310         (void) strcpy(temp, nm + 3);
311         (void) strcpy(nm, "ST. ");
312         (void) strcat(nm, temp);
313     }
314 }
315
316 LookForO(nm)                    /* O BRIEN, etc. */
317     register char *nm;
318 {
319     if (!strcmp(nm, "O ") && isalpha(nm[2])) {
320         nm[1] = '\'';
321     }
322 }
This page took 0.063661 seconds and 5 git commands to generate.