]> andersk Git - moira.git/blame - regtape/students.dc
limit the number of tuples returned from a query, and avoid any
[moira.git] / regtape / students.dc
CommitLineData
093c49c9 1/* $Header$
2 */
3
4#include <stdio.h>
5#include <strings.h>
6#include <ctype.h>
7#include <sys/time.h>
932028de 8#include <moira.h>
9#include <moira_site.h>
093c49c9 10
11
5e2b5b5f 12##define WHO 11859 /* root */
13##define PROG "stu-tape"
14
15#define MAX_ID_VALUE 32766
16#define MIN_ID_VALUE 101
17
093c49c9 18/* File format is:
19
200-29 name
2130-38 id number
2250-54 school code
2355-79 year
2480-109 address
25110-124 room
26125-144 city
27145-158 state
28159-168 dorm phone
29169-212 home address
30213-232 home city
31243-251 mit phone (?)
32*/
33
34#define LOC_NAME 0
35#define LOC_ID 30
36#define LOC_COURSE 50
37#define LOC_YEAR 55
38#define LOC_ADDRESS 80
39#define LOC_DORM_ROOM 110
40#define LOC_CITY 125
41#define LOC_STATE 145
e3c8ca24 42#define LOC_DPHONE 159
43#define LOC_MPHONE 243
093c49c9 44
e3c8ca24 45#define LEN_NAME 30
093c49c9 46#define LEN_ID 9
e3c8ca24 47#define LEN_COURSE 5
48#define LEN_YEAR 25
49#define LEN_ADDRESS 30
50#define LEN_DORM_ROOM 15
51#define LEN_CITY 20
52#define LEN_STATE 14
093c49c9 53#define LEN_DPHONE 9
54#define LEN_MPHONE 9
55
56struct entry {
e3c8ca24 57 char *name;
093c49c9 58 char *last;
59 char *first;
60 char *middle;
61 char *title;
62 char *id;
63 char *eid;
64 char *course;
65 char *year;
66 char *address;
67 char *dorm;
68 char *city;
69 char *state;
70 char *dphone;
71 char *mphone;
72 char *class;
093c49c9 73};
74
75
76char *whoami;
a97bc039 77int newfinger = 0;
e3c8ca24 78int addxuser = 0;
093c49c9 79
80
81main(argc, argv)
82int argc;
83char **argv;
5e2b5b5f 84##{
093c49c9 85 FILE *in;
86 struct entry *e, *get_next_entry();
a97bc039 87 int i, wait = 0;
88 char buf[BUFSIZ], *file = NULL;
093c49c9 89
90 whoami = rindex(argv[0], '/');
91 if (whoami)
92 whoami++;
93 else
94 whoami = argv[0];
95
a97bc039 96 for (i = 1; i < argc; i++) {
97 if (!strcmp(argv[i], "-w"))
98 wait++;
99 else if (!strcmp(argv[i], "-D"))
100 setenv("ING_SET", "set printqry");
101 else if (!strcmp(argv[i], "-n"))
102 newfinger++;
e3c8ca24 103 else if (!strcmp(argv[i], "-u"))
104 addxuser++;
a97bc039 105 else if (file != NULL)
e3c8ca24 106 fprintf(stderr, "Usage: %s [-w] [-D] [-n] [-u] inputfile\n", whoami);
a97bc039 107 else
108 file = argv[i];
109 }
093c49c9 110
a97bc039 111 in = fopen(file, "r");
093c49c9 112 if (in == NULL) {
a97bc039 113 fprintf(stderr, "Unable to open %s for input\n", file);
093c49c9 114 exit(1);
115 }
116
5e2b5b5f 117## ingres sms
118## range of u is users
119
093c49c9 120 while (e = get_next_entry(in)) {
121 process_entry(e);
122 if (wait) {
123 printf("Next");
124 fflush(stdout);
125 gets(buf);
126 }
127 }
128
5e2b5b5f 129## exit
093c49c9 130 exit(0);
5e2b5b5f 131##}
093c49c9 132
133
134struct entry *get_next_entry(in)
135FILE *in;
136{
137 static struct entry e;
138 static char buf[BUFSIZ], eid[16], classbuf[10], titlebuf[12];
e3c8ca24 139 static char name[LEN_NAME+1], id[LEN_ID+1], course[LEN_COURSE+1];
140 static char year[LEN_YEAR+1], address[LEN_ADDRESS+1];
141 static char dorm_room[LEN_DORM_ROOM+1], city[LEN_CITY+1];
142 static char state[LEN_STATE+1], dphone[LEN_DPHONE+1], mphone[LEN_MPHONE+1];
143 static char sname[LEN_NAME+1], title[128];
144 static int nyear = 0;
b2916c40 145 int ends_jr, ends_iii, ends_iv, ends_sr;
093c49c9 146 char *p;
147
e3c8ca24 148 if (nyear == 0) {
093c49c9 149 struct tm *tm;
150 struct timeval tv;
151
152 gettimeofday(&tv, NULL);
153 tm = localtime(&tv.tv_sec);
e3c8ca24 154 nyear = tm->tm_year;
093c49c9 155 if (tm->tm_mon > 5)
e3c8ca24 156 nyear++;
093c49c9 157 }
158
22a6d0dc 159 if (fgets(buf, sizeof(buf), in) == NULL)
160 return((struct entry *)NULL);
e3c8ca24 161
162 strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
163 strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
164 strncpy(course, &buf[LOC_COURSE], LEN_COURSE); course[LEN_COURSE] = 0;
165 strncpy(year, &buf[LOC_YEAR], LEN_YEAR); year[LEN_YEAR] = 0;
166 strncpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS); address[LEN_ADDRESS] = 0;
167 strncpy(dorm_room, &buf[LOC_DORM_ROOM], LEN_DORM_ROOM); dorm_room[LEN_DORM_ROOM] = 0;
168 strncpy(city, &buf[LOC_CITY], LEN_CITY); city[LEN_CITY] = 0;
169 strncpy(state, &buf[LOC_STATE], LEN_STATE); state[LEN_STATE] = 0;
170 strncpy(dphone, &buf[LOC_DPHONE], LEN_DPHONE); dphone[LEN_DPHONE] = 0;
171 strncpy(mphone, &buf[LOC_MPHONE], LEN_MPHONE); mphone[LEN_MPHONE] = 0;
172
173 strcpy(sname, name);
174 e.name = strtrim(sname);
175 p = index(name, ',');
093c49c9 176 if (p)
177 *p = 0;
e3c8ca24 178 e.last = strtrim(name);
093c49c9 179 if (p) {
e3c8ca24 180 p++;
181 while (isspace(*p))
182 p++;
183 e.first = p;
093c49c9 184 if (p = index(e.first, ' ')) {
185 *p = 0;
e3c8ca24 186 e.first = strtrim(e.first);
093c49c9 187 e.middle = strtrim(p + 1);
6d6c60e6 188 } else {
189 e.first = strtrim(e.first);
190 e.middle = "";
191 }
e3c8ca24 192 } else {
193 e.first = "";
194 e.middle = "";
195 }
b2916c40 196 ends_jr = ends_iii = ends_iv = ends_sr = 0;
093c49c9 197 LookForSt(e.last);
198 LookForO(e.last);
b2916c40 199 LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv);
200 LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv);
093c49c9 201 FixCase(e.last);
202 FixCase(e.first);
203 FixCase(e.middle);
093c49c9 204
e3c8ca24 205 e.id = id;
093c49c9 206 e.id[LEN_ID] = 0;
207 e.eid = eid;
208 EncryptID(e.eid, e.id, e.first, e.last);
209
e3c8ca24 210 e.year = strtrim(year);
211 e.title = title;
212 if (e.year[0] == 'G') {
213 e.class = "G";
214 sprintf(title, "Grad Student");
215 } else {
093c49c9 216 e.class = classbuf;
e3c8ca24 217 sprintf(classbuf, "%d", nyear + 4 - atoi(e.year) + 1900);
218 sprintf(title, "Undergrad (class of %s)", classbuf);
093c49c9 219 }
e3c8ca24 220
221 e.course = strtrim(course);
222 e.address = strtrim(address);
223 e.dorm = strtrim(dorm_room);
224 e.city = strtrim(city);
225 e.state = strtrim(state);
226 e.dphone = strtrim(dphone);
227 e.mphone = strtrim(mphone);
093c49c9 228 return(&e);
229}
230
231
093c49c9 232process_entry(e)
233struct entry *e;
5e2b5b5f 234##{
a97bc039 235 int changed, nochange;
5e2b5b5f 236 char buf[BUFSIZ], *from, *to;
e3c8ca24 237## char *first, *last, *eid, *title, *sid, *name;
238## char class[9], haddr[128], hphone[17], ophone[13], dept[24];
239## int id, status;
5e2b5b5f 240
241 first = e->first;
9e4455c2 242 if (strlen(first) > 16)
243 first[16] = 0;
5e2b5b5f 244 last = e->last;
9e4455c2 245 if (strlen(last) > 16)
246 last[16] = 0;
5e2b5b5f 247 eid = e->eid;
248 id = 0;
249## repeat retrieve (id = u.users_id, class = u.mit_year, haddr = u.home_addr,
250## hphone = u.home_phone, ophone = u.office_phone,
e3c8ca24 251## status = u.#status, dept = u.mit_dept)
5e2b5b5f 252## where u.#last = @last and u.#first = @first and u.mit_id = @eid
253 if (id == 0) {
254 newuser(e);
093c49c9 255 return;
256 }
a262e022 257 if (strcmp(e->class, strtrim(class)) &&
258 strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
a97bc039 259 com_err(whoami, 0, "updating class for user %s %s from %s to %s",
260 first, last, class, e->class);
e3c8ca24 261 if (status == US_NOT_ALLOWED) status = US_NO_LOGIN_YET;
262 if (status == US_ENROLL_NOT_ALLOWED) status = US_ENROLLED;
5e2b5b5f 263 strcpy(class, e->class);
e3c8ca24 264## repeat replace u (mit_year = @class, #status = @status, ugdefault = 1,
5e2b5b5f 265## modtime = "now", modby = WHO, modwith = PROG)
fac211e8 266## where u.users_id = @id
093c49c9 267 }
a97bc039 268 changed = nochange = 0;
e3c8ca24 269 strcpy(buf, e->address);
093c49c9 270 if (*e->dorm) {
271 strcat(buf, " ");
5e2b5b5f 272 strcat(buf, e->dorm);
093c49c9 273 }
274 if (*e->city) {
275 strcat(buf, " ");
5e2b5b5f 276 strcat(buf, e->city);
093c49c9 277 }
5e2b5b5f 278 FixCase(buf);
093c49c9 279 if (*e->state) {
280 strcat(buf, " ");
5e2b5b5f 281 strcat(buf, e->state);
093c49c9 282 }
5e2b5b5f 283 while (to = index(buf, ','))
284 *to = ';';
285 while (to = index(buf, ':'))
286 *to = ';';
a97bc039 287 if (newfinger) {
288 if (haddr[0] == ' ') {
289 strncpy(haddr, buf, 80);
290 haddr[80] = 0;
291 changed++;
292 } else if (strncmp(strtrim(haddr), buf, 80))
293 nochange++;
294 } else {
295 if (strncmp(strtrim(haddr), buf, 80))
296 changed++;
5e2b5b5f 297 strncpy(haddr, buf, 80);
a97bc039 298 haddr[80] = 0;
093c49c9 299 }
300 from = e->dphone;
301 to = buf;
302 while (*from) {
303 if (isdigit(*from))
304 *to++ = *from;
305 from++;
306 }
307 *to = 0;
a97bc039 308 if (newfinger) {
309 if (hphone[0] == ' ') {
310 strncpy(hphone, buf, 16);
311 hphone[16] = 0;
312 } else if (strncmp(strtrim(hphone), buf, 16))
313 nochange++;
314 } else {
315 if (strncmp(strtrim(hphone), buf, 16))
316 changed++;
5e2b5b5f 317 strncpy(hphone, buf, 16);
a97bc039 318 hphone[16] = 0;
093c49c9 319 }
320 from = e->mphone;
321 to = buf;
322 while (*from) {
323 if (isdigit(*from))
324 *to++ = *from;
325 from++;
326 }
327 *to = 0;
a97bc039 328 if (newfinger) {
329 if (ophone[0] == ' ') {
330 strncpy(ophone, buf, 12);
331 ophone[12] = 0;
332 } else if (strncmp(strtrim(ophone), buf, 12))
333 nochange++;
334 } else {
335 if (strncmp(strtrim(ophone), buf, 12))
336 changed++;
5e2b5b5f 337 strncpy(ophone, buf, 12);
a97bc039 338 ophone[12] = 0;
093c49c9 339 }
e3c8ca24 340 e->course = e->course;
a97bc039 341 if (newfinger) {
342 if (dept[0] == ' ') {
343 strncpy(dept, e->course, 12);
344 dept[12] = 0;
345 } else if (strncmp(strtrim(dept), e->course, 11))
346 nochange++;
347 } else {
348 if (strncmp(strtrim(dept), e->course, 11))
349 changed++;
5e2b5b5f 350 strncpy(dept, e->course, 12);
a97bc039 351 dept[12] = 0;
093c49c9 352 }
353 if (changed) {
5e2b5b5f 354 com_err(whoami, 0, "updating finger for %s %s", first, last);
355## repeat replace u (home_addr = @haddr, home_phone = @hphone,
e3c8ca24 356## office_phone = @ophone, #mit_dept = @dept, ugdefault = 1,
5e2b5b5f 357## fmodtime = "now", fmodby = WHO, fmodwith = PROG)
358## where u.users_id = @id
e3c8ca24 359 } /* else if (nochange)
a97bc039 360 com_err(whoami, 0, "NOT updating finger for %s %s", first, last);
e3c8ca24 361 */
362 if (!changed && !strcmp(e->class, class)) {
363## repeat replace u (ugdefault = 1) where u.users_id = @id
364 }
365 if (addxuser) {
366 sid = e->id;
367 name = e->name;
368 strcpy(dept, e->course);
369 title = e->title;
370 strcpy(haddr, e->address);
371 if (*e->dorm) {
372 strcat(haddr, " ");
373 strcat(haddr, e->dorm);
374 }
375 if (*e->city) {
376 strcat(haddr, " ");
377 strcat(haddr, e->city);
378 }
379 FixCase(haddr);
380 if (*e->state) {
381 strcat(haddr, " ");
382 strcat(haddr, e->state);
383 }
384
6d6c60e6 385 status = 1;
386## repeat retrieve (status = any (xuser.users_id
387## where xuser.users_id = @id))
388 if (!status) {
e3c8ca24 389## append xuser (users_id = id, #id = sid, #name = name, #dept = dept,
390## #title = title, address = haddr, #phone1 = hphone,
391## #phone2 = ophone, modtime = "now")
6d6c60e6 392 }
e3c8ca24 393 }
5e2b5b5f 394##}
093c49c9 395
5e2b5b5f 396
397newuser(e)
093c49c9 398struct entry *e;
5e2b5b5f 399##{
400 char buf[512], *from, *to;
401## int id, uid;
402## char *last, *first, *class, *middle, login[9], *eid, fullname[65];
e3c8ca24 403## char haddr[81], hphone[17], ophone[13], dept[24], *sid, *title;
5e2b5b5f 404
405
e3c8ca24 406 strcpy(buf, e->address);
5e2b5b5f 407 if (*e->dorm) {
408 strcat(buf, " ");
e3c8ca24 409 strcat(buf, e->dorm);
5e2b5b5f 410 }
411 if (*e->city) {
412 strcat(buf, " ");
e3c8ca24 413 strcat(buf, e->city);
5e2b5b5f 414 }
415 if (*e->state) {
416 strcat(buf, " ");
e3c8ca24 417 strcat(buf, e->state);
5e2b5b5f 418 }
419 strncpy(haddr, buf, 80);
420 from = e->dphone;
421 to = buf;
422 while (*from) {
423 if (isdigit(*from))
424 *to++ = *from;
425 from++;
426 }
427 *to = 0;
428 strncpy(hphone, buf, 16);
429 from = e->mphone;
430 to = buf;
431 while (*from) {
432 if (isdigit(*from))
433 *to++ = *from;
434 from++;
435 }
436 *to = 0;
437 strncpy(ophone, buf, 12);
5e2b5b5f 438 strncpy(dept, e->course, 12);
5e2b5b5f 439
27736587 440 id = set_next_object_id("users_id", 0);
441 uid = set_next_object_id("uid", 1);
5e2b5b5f 442 sprintf(login, "#%d", uid);
443 last = e->last;
444 first = e->first;
445 middle = e->middle;
446 eid = e->eid;
447 class = e->class;
448 if (*middle)
449 sprintf(fullname, "%s %s %s", first, middle, last);
450 else
451 sprintf(fullname, "%s %s", first, last);
452
453## append users (#login = login, users_id = id, #uid = uid, shell = "/bin/csh",
454## #last = last, #first = first, #middle = middle, status = 0,
e3c8ca24 455## #mit_id = eid, #mit_year = class, ugdefault = 1,
5e2b5b5f 456## modtime = "now", modby = WHO, modwith = PROG,
457## #fullname = fullname, home_addr = haddr, home_phone = hphone,
458## office_phone = ophone, #mit_dept = dept,
459## fmodtime = "now", fmodby = WHO, fmodwith = PROG,
460## potype = "NONE")
e3c8ca24 461
462 sid = e->id;
463 strcpy(fullname, e->name);
464 strcpy(dept, e->course);
465 title = e->title;
466 strcpy(haddr, e->address);
467 if (*e->dorm) {
468 strcat(haddr, " ");
469 strcat(haddr, e->dorm);
470 }
471 if (*e->city) {
472 strcat(haddr, " ");
473 strcat(haddr, e->city);
474 }
475 FixCase(haddr);
476 if (*e->state) {
477 strcat(haddr, " ");
478 strcat(haddr, e->state);
479 }
480
481 if (addxuser) {
482## append xuser (users_id = id, #id = sid, #name = fullname, #dept = dept,
483## #title = title, address = haddr, #phone1 = hphone,
484## #phone2 = ophone, modtime = "now")
485 }
093c49c9 486 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
5e2b5b5f 487##}
488
489
27736587 490set_next_object_id(object, limit)
5e2b5b5f 491 char *object;
27736587 492 int limit;
5e2b5b5f 493##{
494## char *name;
495## int rowcount, exists, value;
496
497 name = object;
498## begin transaction
499## repeat retrieve (value = values.#value) where values.#name = @name
500## inquire_equel(rowcount = "rowcount")
501 if (rowcount != 1) {
502## abort
503 return(0);
093c49c9 504 }
5e2b5b5f 505
506## retrieve (exists = any(users.name where users.name = value))
507## inquire_equel(rowcount = "rowcount")
508 if (rowcount != 1) {
509## abort
510 return(0);
511 }
512 while (exists) {
513 value++;
27736587 514 if (limit && value > MAX_ID_VALUE)
5e2b5b5f 515 value = MIN_ID_VALUE;
516## retrieve (exists = any(users.name where users.name = value))
517 }
518
519## repeat replace values (#value = @value) where values.#name = @name
520## end transaction
521 return(value);
522##}
523
524
This page took 0.271585 seconds and 5 git commands to generate.