]> andersk Git - moira.git/blobdiff - regtape/students.dc
Added missing definition of $(SRCDIR)
[moira.git] / regtape / students.dc
index 4c834ce138ebab6b2747f62560b3a144e5956ea5..80462c781370628ca552c4ddd08fbcea566aad11 100644 (file)
@@ -5,14 +5,15 @@
 #include <strings.h>
 #include <ctype.h>
 #include <sys/time.h>
-#include <sms.h>
-#include <sms_app.h>
+#include <moira.h>
+#include <moira_site.h>
+EXEC SQL INCLUDE sqlca;
 
 
-##define WHO 11859             /* root */
-##define PROG "stu-tape"
+#define WHO 11859              /* root */
+#define PROG "stu-tape"
 
-#define MAX_ID_VALUE   32766
+#define MAX_ID_VALUE   31999
 #define MIN_ID_VALUE   101
 
 /* File format is:
 #define LOC_DORM_ROOM 110
 #define LOC_CITY 125
 #define LOC_STATE 145
-#define LOC_DORM_PHONE 159
-#define LOC_MIT_PHONE1 243
+#define LOC_DPHONE 155
+#define LOC_MPHONE 243
 
-#define LEN_NAME 29
+#define LEN_NAME 30
 #define LEN_ID 9
-#define LEN_COURSE 3
-#define LEN_YEAR 23
-#define LEN_ADDRESS 26
-#define LEN_DORM_ROOM 14
-#define LEN_CITY 19
-#define LEN_STATE 12
-#define LEN_DPHONE 9
-#define LEN_MPHONE 9
+#define LEN_COURSE 5
+#define LEN_YEAR 25
+#define LEN_ADDRESS 30
+#define LEN_DORM_ROOM 15
+#define LEN_CITY 20
+#define LEN_STATE 10
+#define LEN_DPHONE 12
+#define LEN_MPHONE 12
 
 struct entry {
+    char *name;
     char *last;
     char *first;
     char *middle;
@@ -75,11 +77,14 @@ struct entry {
 char *whoami;
 int newfinger = 0;
 
+#define SQL_DUPLICATE -40100
+#define SQL_DEADLOCK -49900
+#define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 100)
 
 main(argc, argv)
 int argc;
 char **argv;
-##{
+{
     FILE *in;
     struct entry *e, *get_next_entry();
     int i, wait = 0;
@@ -110,11 +115,28 @@ char **argv;
        exit(1);
     }
 
-##  ingres sms
-##  range of u is users
+    setlinebuf(stdout);
+    setlinebuf(stderr);
+
+    EXEC SQL CONNECT moira;
+    if (sqlca.sqlcode != 0) {
+       com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+       exit(1);
+    }
 
     while (e = get_next_entry(in)) {
+    again:
        process_entry(e);
+       EXEC SQL COMMIT WORK;
+       if (sqlca.sqlcode != 0) {
+           if (sqlca.sqlcode == SQL_DEADLOCK) {
+               com_err(whoami, MR_DEADLOCK, "commiting work");
+               goto again;
+           } else {
+               com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+               exit(1);
+           }
+       }
        if (wait) {
            printf("Next");
            fflush(stdout);
@@ -122,9 +144,8 @@ char **argv;
        }
     }
 
-##  exit
     exit(0);
-##}
+}
 
 
 struct entry *get_next_entry(in)
@@ -132,125 +153,213 @@ FILE *in;
 {
     static struct entry e;
     static char buf[BUFSIZ], eid[16], classbuf[10], titlebuf[12];
-    static int year = 0;
-    int ends_jr, ends_iii, ends_iv, ends_sr;
+    static char name[LEN_NAME+1], id[LEN_ID+1], course[LEN_COURSE+1];
+    static char year[LEN_YEAR+1], address[LEN_ADDRESS+1];
+    static char dorm_room[LEN_DORM_ROOM+1], city[LEN_CITY+1];
+    static char state[LEN_STATE+1], dphone[LEN_DPHONE+1], mphone[LEN_MPHONE+1];
+    static char sname[LEN_NAME+1], title[128];
+    static int nyear = 0;
+    int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v;
     char *p;
 
-    if (year == 0) {
+    if (nyear == 0) {
        struct tm *tm;
        struct timeval tv;
 
        gettimeofday(&tv, NULL);
        tm = localtime(&tv.tv_sec);
-       year = tm->tm_year;
+       nyear = tm->tm_year;
        if (tm->tm_mon > 5)
-         year++;
+         nyear++;
     }
 
     if (fgets(buf, sizeof(buf), in) == NULL)
       return((struct entry *)NULL);
-    buf[LEN_NAME] = 0;
-    p = index(&buf[LOC_NAME], ',');
+
+    strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
+    strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
+    strncpy(course, &buf[LOC_COURSE], LEN_COURSE); course[LEN_COURSE] = 0;
+    strncpy(year, &buf[LOC_YEAR], LEN_YEAR); year[LEN_YEAR] = 0;
+    strncpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS); address[LEN_ADDRESS] = 0;
+    strncpy(dorm_room, &buf[LOC_DORM_ROOM], LEN_DORM_ROOM); dorm_room[LEN_DORM_ROOM] = 0;
+    strncpy(city, &buf[LOC_CITY], LEN_CITY); city[LEN_CITY] = 0;
+    strncpy(state, &buf[LOC_STATE], LEN_STATE); state[LEN_STATE] = 0;
+    strncpy(dphone, &buf[LOC_DPHONE], LEN_DPHONE); dphone[LEN_DPHONE] = 0;
+    strncpy(mphone, &buf[LOC_MPHONE], LEN_MPHONE); mphone[LEN_MPHONE] = 0;
+
+    strcpy(sname, name);
+    e.name = strtrim(sname);
+    p = index(name, ',');
     if (p)
       *p = 0;
-    e.last = strtrim(&buf[LOC_NAME]);
+    e.last = strtrim(name);
     if (p) {
-       e.first = p + 2;
+       p++;
+       while (isspace(*p))
+         p++;
+       e.first = p;    
        if (p = index(e.first, ' ')) {
            *p = 0;
+           e.first = strtrim(e.first);
            e.middle = strtrim(p + 1);
-       } else
-         e.middle = "";
-    } else
-      e.first = "";
-    e.first = strtrim(e.first);
-    ends_jr = ends_iii = ends_iv = ends_sr = 0;
+       } else {
+           e.first = strtrim(e.first);
+           e.middle = "";
+       }
+    } else {
+       e.first = "";
+       e.middle = "";
+    }
+    ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0;
     LookForSt(e.last);
     LookForO(e.last);
-    LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv);
-    LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv);
+    LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
+                   &ends_ii, &ends_v);
+    LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
+                   &ends_ii, &ends_v);
     FixCase(e.last);
     FixCase(e.first);
     FixCase(e.middle);
-    e.title = titlebuf;
-    titlebuf[0] = 0;
-    AppendJrOrIII(titlebuf, &ends_sr, &ends_jr, &ends_iii, &ends_iv);
 
-    e.id = &buf[LOC_ID];
+    e.id = id;
     e.id[LEN_ID] = 0;
     e.eid = eid;
     EncryptID(e.eid, e.id, e.first, e.last);
 
-    e.course = &buf[LOC_COURSE];
-    e.course[LEN_COURSE] = 0;
-    e.year = &buf[LOC_YEAR];
-    e.year[LEN_YEAR] = 0;
-    if (e.year[0] == 'G')
-      e.class = "G";
-    else {
+    e.year = strtrim(year);
+    e.title = title;
+    if (e.year[0] == 'G') {
+       e.class = "G";
+       sprintf(title, "Grad Student");
+    } else {
        e.class = classbuf;
-       sprintf(classbuf, "%d", year + 4 - atoi(e.year) + 1900);
-    }
-    e.address = &buf[LOC_ADDRESS];
-    e.address[LEN_ADDRESS] = 0;
-    e.dorm = &buf[LOC_DORM_ROOM];
-    e.dorm[LEN_DORM_ROOM] = 0;
-    e.city = &buf[LOC_CITY];
-    e.city[LEN_CITY] = 0;
-    e.state = &buf[LOC_STATE];
-    e.state[LEN_STATE] = 0;
-    e.dphone = &buf[LOC_DORM_PHONE];
-    e.dphone[LEN_DPHONE] = 0;
-    e.mphone = &buf[LOC_MIT_PHONE1];
-    e.mphone[LEN_MPHONE] = 0;
+       sprintf(classbuf, "%d", nyear + 4 - atoi(e.year) + 1900);
+       sprintf(title, "Undergrad (class of %s)", classbuf);
+    }
+
+    e.course = strtrim(course);
+    e.address = strtrim(address);
+    e.dorm = strtrim(dorm_room);
+    e.city = strtrim(city);
+    e.state = strtrim(state);
+    e.dphone = strtrim(dphone);
+    e.mphone = strtrim(mphone);
     return(&e);
 }
 
 
 process_entry(e)
 struct entry *e;
-##{
-    int changed, nochange;
+{
+    int changed, nochange, encrypted;
     char buf[BUFSIZ], *from, *to;
-##  char *first, *last, *eid;
-##  char class[9], haddr[81], hphone[17], ophone[13], dept[13];
-##  int id;
-
+    EXEC SQL BEGIN DECLARE SECTION;
+    char *first, *last, *middle, *eid, *title, *sid, *name, *rname, *rdept, *rtitle;
+    char *rophone, *rhphone, *prog;
+    char class[9], haddr[128], hphone[33], ophone[33], dept[33], raddr[128];
+    char dfirst[17], dlast[17], dmiddle[17];
+    int id, status, who;
+    EXEC SQL END DECLARE SECTION;
+
+    who = WHO;
+    prog = PROG;
     first = e->first;
+    if (strlen(first) > 16)
+      first[16] = 0;
     last = e->last;
+    if (strlen(last) > 16)
+      last[16] = 0;
+    middle = e->middle;
     eid = e->eid;
+    sid = e->id;
     id = 0;
-##  repeat retrieve (id = u.users_id, class = u.mit_year, haddr = u.home_addr,
-##           hphone = u.home_phone, ophone = u.office_phone,
-##           dept = u.mit_dept)
-##     where u.#last = @last and u.#first = @first and u.mit_id = @eid
+    encrypted = 0;
+
+    /* Get user info */
+    EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
+      INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
+      FROM users
+      WHERE clearid = :sid;
+    if (sqlfail()) {
+       if (sqlca.sqlcode == SQL_DUPLICATE) {
+           com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
+           return;
+       } else if (sqlca.sqlcode == SQL_DEADLOCK) {
+           com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
+           EXEC SQL ROLLBACK;
+           return process_entry(e);
+       } else
+         sqlexit();
+    }
     if (id == 0) {
-       newuser(e);
-       return;
+       EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
+         INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
+         FROM users
+         WHERE last = :last and first = :first and clearid = :eid;
+       if (sqlfail()) {
+           if (sqlca.sqlcode == SQL_DEADLOCK) {
+               com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
+               EXEC SQL ROLLBACK;
+               return process_entry(e);
+           } else if (sqlca.sqlcode != SQL_DUPLICATE)
+             sqlexit();
+       }
+       encrypted++;
+       if (id == 0) {
+           newuser(e);
+           return;
+       }
     }
+
+    /* See if class changed: if it's different, and the value in the database
+     * is not STAFF or SIPB, then update the database.  Since they were on the
+     * students tape, make the account usable.
+     */
     if (strcmp(e->class, strtrim(class)) &&
        strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
        com_err(whoami, 0, "updating class for user %s %s from %s to %s",
                first, last, class, e->class);
+       if (status == US_NOT_ALLOWED) status = US_NO_LOGIN_YET;
+       if (status == US_ENROLL_NOT_ALLOWED) status = US_ENROLLED;
        strcpy(class, e->class);
-##     repeat replace u (mit_year = @class,
-##                modtime = "now", modby = WHO, modwith = PROG)
-##         where u.users_id = @id
+       EXEC SQL REPEATED UPDATE users
+         SET type = :class, status = :status, modtime = 'now',
+           modby = :who, modwith = :prog
+         WHERE users_id = :id;   
+       if (sqlca.sqlcode != 0) {
+           com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+           exit(1);
+       }
+    }
+
+    /* Update name if necessary */
+    if (strcmp(first, strtrim(dfirst)) ||
+       strcmp(last, strtrim(dlast)) ||
+       strcmp(middle, strtrim(dmiddle))) {
+       com_err(whoami, 0, "updating real name for %s %s", first, last);
+       EXEC SQL UPDATE users
+         SET first = :first, last = :last, middle = :middle,
+               modby = :who, modwith = :prog, modtime = 'now'
+         WHERE users_id = :id;
+       if (sqlca.sqlcode != 0) {
+           com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+           exit(1);
+       }
     }
+
+    /* Deal with updating the finger info if necessary */
     changed = nochange = 0;
-    strcpy(buf, strtrim(e->address));
-    e->dorm = strtrim(e->dorm);
+    if (encrypted) changed++;
+    strcpy(buf, e->address);
     if (*e->dorm) {
        strcat(buf, " ");
        strcat(buf, e->dorm);
     }
-    e->city = strtrim(e->city);
     if (*e->city) {
        strcat(buf, " ");
        strcat(buf, e->city);
     }
     FixCase(buf);
-    e->state = strtrim(e->state);
     if (*e->state) {
        strcat(buf, " ");
        strcat(buf, e->state);
@@ -312,7 +421,7 @@ struct entry *e;
        strncpy(ophone, buf, 12);
        ophone[12] = 0;
     }
-    e->course = strtrim(e->course);
+    e->course = e->course;
     if (newfinger) {
        if (dept[0] == ' ') {
            strncpy(dept, e->course, 12);
@@ -325,38 +434,101 @@ struct entry *e;
        strncpy(dept, e->course, 12);
        dept[12] = 0;
     }
+    sid = e->id;
+    name = e->name;
+    rdept = e->course;
+    rtitle = e->title;
+    rophone = e->mphone;
+    rhphone = e->dphone;
+    strcpy(raddr, e->address);
+    if (*e->dorm) {
+       strcat(raddr, " ");
+       strcat(raddr, e->dorm);
+    }
+    strcat(raddr, "|");
+    if (*e->city) {
+       strcat(raddr, e->city);
+       FixCase(raddr);
+       if (*e->state) {
+           strcat(raddr, " ");
+           if (isupper(e->state[0]) && isupper(e->state[1]) &&
+               isdigit(e->state[2]) && isdigit(e->state[3]) &&
+               isdigit(e->state[4]) && isdigit(e->state[5]) &&
+               isdigit(e->state[6])) {
+               sprintf(buf, "%c%c  %s", e->state[0],e->state[1],
+                       &(e->state[2]));
+               strcat(raddr, buf);
+           } else
+             strcat(raddr, e->state);
+       }
+    } else {
+       FixCase(raddr);
+       strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
+    }
     if (changed) {
        com_err(whoami, 0, "updating finger for %s %s", first, last);
-##     repeat replace u (home_addr = @haddr, home_phone = @hphone,
-##                office_phone = @ophone, #mit_dept = @dept,
-##                fmodtime = "now", fmodby = WHO, fmodwith = PROG)
-##         where u.users_id = @id
-    } else if (nochange)
-      com_err(whoami, 0, "NOT updating finger for %s %s", first, last);
-##}
+       EXEC SQL REPEATED UPDATE users
+         SET home_addr = :haddr, home_phone = :hphone,
+           office_phone = :ophone, department = :dept,
+           fmodtime = 'now', fmodby = :who, fmodwith = :prog,
+           xname = :name, xdept = :rdept, xtitle = :rtitle,
+           xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
+           xmodtime = date('now'), clearid = :sid
+         WHERE users_id = :id;
+       if (sqlca.sqlcode != 0) {
+           if (sqlca.sqlcode == SQL_DEADLOCK) {
+               com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
+               EXEC SQL ROLLBACK;
+               return process_entry(e);
+           } else {
+               com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+               exit(1);
+           }
+       }
+    }  else {
+       EXEC SQL REPEATED UPDATE users
+         SET xname = :name, xdept = :rdept, xtitle = :rtitle,
+           xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
+           xmodtime = date('now'), clearid = :sid
+         WHERE users_id = :id;
+       if (sqlca.sqlcode != 0) {
+           if (sqlca.sqlcode == SQL_DEADLOCK) {
+               com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
+               EXEC SQL ROLLBACK;
+               return process_entry(e);
+           } else {
+               com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+               exit(1);
+           }
+       }
+    }
+}
 
 
 newuser(e)
 struct entry *e;
-##{
+{
     char buf[512], *from, *to;
-##  int id, uid;
-##  char *last, *first, *class, *middle, login[9], *eid, fullname[65];
-##  char haddr[81], hphone[17], ophone[13], dept[13];
-
-
-    strcpy(buf, strtrim(e->address));
+    EXEC SQL BEGIN DECLARE SECTION;
+    int id, uid, who;
+    char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
+    char haddr[81], hphone[17], ophone[13], dept[24], *title, raddr[81], *name;
+    EXEC SQL END DECLARE SECTION;
+
+    who = WHO;
+    prog = PROG;
+    strcpy(buf, e->address);
     if (*e->dorm) {
        strcat(buf, " ");
-       strcat(buf, strtrim(e->dorm));
+       strcat(buf, e->dorm);
     }
     if (*e->city) {
        strcat(buf, " ");
-       strcat(buf, strtrim(e->city));
+       strcat(buf, e->city);
     }
     if (*e->state) {
        strcat(buf, " ");
-       strcat(buf, strtrim(e->state));
+       strcat(buf, e->state);
     }
     strncpy(haddr, buf, 80);
     from = e->dphone;
@@ -377,66 +549,168 @@ struct entry *e;
     }
     *to = 0;
     strncpy(ophone, buf, 12);
-    e->course = strtrim(e->course);
     strncpy(dept, e->course, 12);
-
     
-    id = set_next_object_id("users_id");
-    uid = set_next_object_id("uid");
+    id = set_next_users_id(0);
+    uid = set_next_uid(1);
     sprintf(login, "#%d", uid);
     last = e->last;
     first = e->first;
     middle = e->middle;
-    eid = e->eid;
+    sid = e->id;
     class = e->class;
+    title = e->title;
     if (*middle)
       sprintf(fullname, "%s %s %s", first, middle, last);
     else
       sprintf(fullname, "%s %s", first, last);
-    
-##  append users (#login = login, users_id = id, #uid = uid, shell = "/bin/csh",
-##               #last = last, #first = first, #middle = middle, status = 0,
-##               #mit_id = eid, #mit_year = class,
-##               modtime = "now", modby = WHO, modwith = PROG,
-##               #fullname = fullname, home_addr = haddr, home_phone = hphone,
-##               office_phone = ophone, #mit_dept = dept,
-##               fmodtime = "now", fmodby = WHO, fmodwith = PROG,
-##               potype = "NONE")
-    com_err(whoami, 0, "adding user %s %s", e->first, e->last);
-##}
-
-
-set_next_object_id(object)
-    char *object;
-##{
-##  char *name;
-##  int rowcount, exists, value;
-
-    name = object;
-##  begin transaction
-##  repeat retrieve (value = values.#value) where values.#name = @name
-##  inquire_equel(rowcount = "rowcount")
-    if (rowcount != 1) {
-##     abort
-       return(0);
-    }
-
-##  retrieve (exists = any(users.name where users.name = value))
-##  inquire_equel(rowcount = "rowcount")
-    if (rowcount != 1) {
-##     abort
-       return(0);
-    }
-    while (exists) {
+    name = e->name;
+    strcpy(raddr, e->address);
+    if (*e->dorm) {
+       strcat(raddr, " ");
+       strcat(raddr, e->dorm);
+    }
+    strcat(raddr, "|");
+    if (*e->city) {
+       strcat(raddr, e->city);
+       FixCase(raddr);
+       if (*e->state) {
+           strcat(raddr, " ");
+           if (isupper(e->state[0]) && isupper(e->state[1]) &&
+               isdigit(e->state[2]) && isdigit(e->state[3]) &&
+               isdigit(e->state[4]) && isdigit(e->state[5]) &&
+               isdigit(e->state[6])) {
+               sprintf(buf, "%c%c  %s", e->state[0],e->state[1],
+                       &(e->state[2]));
+               strcat(raddr, buf);
+           } else
+             strcat(raddr, e->state);
+       }
+    } else {
+       FixCase(raddr);
+       strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
+    }
+    EXEC SQL REPEATED INSERT INTO users
+      (login, users_id, uid, shell, last, first, middle, status,
+       clearid, type, modtime, modby, modwith, fullname, home_addr,
+       home_phone, office_phone, department, fmodtime, fmodby, fmodwith,
+       potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
+      VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, 0,
+             :sid, :class, 'now', :who, :prog, :fullname, :haddr, :hphone,
+             :ophone, :dept, 'now', :who, :prog, 'NONE', :name, :dept,
+             :title, :raddr, :hphone, :ophone, date('now'));
+    if (sqlca.sqlcode != 0) {
+       if (sqlca.sqlcode == SQL_DEADLOCK) {
+           com_err(whoami, MR_DEADLOCK, "adding user %s", sid);
+           EXEC SQL ROLLBACK;
+           return newuser(e);
+       } else {
+           com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+           exit(1);
+       }
+    } else
+      com_err(whoami, 0, "adding user %s %s", e->first, e->last);
+}
+
+
+
+set_next_users_id(limit)
+    int limit;
+{
+    EXEC SQL BEGIN DECLARE SECTION;
+    int rowcount, flag, value, retval;
+    EXEC SQL END DECLARE SECTION;
+
+    EXEC SQL REPEATED SELECT value INTO :value FROM numvalues 
+      WHERE name = 'users_id';
+    if (sqlfail()) sqlexit();
+    if (sqlca.sqlerrd[2] != 1) {
+       EXEC SQL ROLLBACK;
+       com_err(whoami, MR_INTERNAL, "values table inconsistancy");
+       exit(1);
+    }
+
+    flag = 0;
+    EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
+      WHERE users_id = :value;
+    if (sqlfail()) sqlexit();
+    if (sqlca.sqlerrd[2] == 0)
+      flag = 0;
+    while (flag) {
+       value++;
+       if (limit && value > MAX_ID_VALUE)
+           value = MIN_ID_VALUE;
+       flag = 0;
+       EXEC SQL REPEATED SELECT users_id INTO :flag FROM users 
+         WHERE users_id = :value;
+       if (sqlfail()) sqlexit();
+       if (sqlca.sqlerrd[2] == 0)
+         flag = 0;
+    }
+
+    retval = value++;
+    if (limit && value > MAX_ID_VALUE)
+      value = MIN_ID_VALUE;
+    EXEC SQL REPEATED UPDATE numvalues SET value = :value
+      WHERE name = 'users_id';
+    if (sqlca.sqlcode != 0) {
+       com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+       exit(1);
+    }
+    return(retval);
+}
+
+set_next_uid(limit)
+    int limit;
+{
+    EXEC SQL BEGIN DECLARE SECTION;
+    int rowcount, flag, value, retval;
+    EXEC SQL END DECLARE SECTION;
+
+    EXEC SQL REPEATED SELECT value INTO :value FROM numvalues 
+      WHERE name = 'uid';
+    if (sqlfail()) sqlexit();
+    if (sqlca.sqlerrd[2] != 1) {
+       EXEC SQL ROLLBACK;
+       com_err(whoami, MR_INTERNAL, "values table inconsistancy");
+       exit(1);
+    }
+
+    flag = 0;
+    EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
+    if (sqlfail()) sqlexit();
+    if (sqlca.sqlerrd[2] == 0)
+      flag = 0;
+    while (flag) {
        value++;
-       if (value > MAX_ID_VALUE)
+       if (limit && value > MAX_ID_VALUE)
            value = MIN_ID_VALUE;
-##     retrieve (exists = any(users.name where users.name = value))
+       flag = 0;
+       EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
+       if (sqlfail()) sqlexit();
+       if (sqlca.sqlerrd[2] == 0)
+         flag = 0;
     }
 
-##  repeat replace values (#value = @value) where values.#name = @name
-##  end transaction
-    return(value);
-##}
+    retval = value++;
+    if (limit && value > MAX_ID_VALUE)
+      value = MIN_ID_VALUE;
+    EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
+    if (sqlca.sqlcode != 0) {
+       com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+       exit(1);
+    }
+    return(retval);
+}
 
 
+sqlexit()
+{
+    if (sqlca.sqlcode == SQL_DEADLOCK)
+      com_err(whoami, MR_DEADLOCK, "unrecoverable ingres error %d",
+             sqlca.sqlcode);
+    else
+      com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+    EXEC SQL ROLLBACK WORK;
+    exit(1);
+}
This page took 0.065502 seconds and 4 git commands to generate.