]> andersk Git - moira.git/blob - regtape/staff.pc
Append to logfile instead of blowing it away.
[moira.git] / regtape / staff.pc
1 /* $Id$
2  *
3  * Load data into Moira from Personnel 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 <string.h>
19
20 EXEC SQL INCLUDE sqlca;
21
22 RCSID("$Header$");
23
24 /* File format is:
25  *
26  * id number [9]
27  * name (last, first middle) [30]
28  * office address [24]
29  * phone1 [12]
30  * phone2 [12]
31  * dept [50]
32  * title [50]
33  */
34
35 #define LOC_ID 0
36 #define LEN_ID 9
37 #define LOC_NAME (LOC_ID + LEN_ID)
38 #define LEN_NAME 30
39 #define LOC_OFFICE (LOC_NAME + LEN_NAME)
40 #define LEN_OFFICE 24
41 #define LOC_PHONE (LOC_OFFICE + LEN_OFFICE)
42 #define LEN_PHONE 12
43 #define LOC_PHONE2 (LOC_PHONE + LEN_PHONE)
44 #define LEN_PHONE2 12
45 #define LOC_DEPT (LOC_PHONE2 + LEN_PHONE2)
46 #define LEN_DEPT 50
47 #define LOC_TITLE (LOC_DEPT + LEN_DEPT)
48 #define LEN_TITLE 50
49
50 struct entry *get_next_entry(FILE *in);
51 void process_entry(struct entry *e, int secure);
52
53 EXEC SQL BEGIN DECLARE SECTION;
54 int who;
55 char *prog = "stafload";
56 EXEC SQL END DECLARE SECTION;
57
58 char *whoami;
59
60 int main(int argc, char **argv)
61 {
62   FILE *in;
63   struct entry *e;
64   int i, wait = 0;
65   char buf[80], *file = NULL;
66   EXEC SQL BEGIN DECLARE SECTION;
67   char *db = "moira";
68   EXEC SQL END DECLARE SECTION;
69
70   whoami = strrchr(argv[0], '/');
71   if (whoami)
72     whoami++;
73   else
74     whoami = argv[0];
75
76   setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
77   setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
78
79   for (i = 1; i < argc; i++)
80     {
81       if (!strcmp(argv[i], "-w"))
82         wait++;
83       else if (file)
84         {
85           fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
86           exit(1);
87         }
88       else
89         file = argv[i];
90     }
91
92   if (!file)
93     {
94       fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
95       exit(1);
96     }
97
98   in = fopen(file, "r");
99   if (!in)
100     {
101       fprintf(stderr, "Unable to open %s for input\n", file);
102       exit(1);
103     }
104
105   initialize_sms_error_table();
106
107   EXEC SQL CONNECT :db IDENTIFIED BY :db;
108   if (sqlca.sqlcode)
109     {
110       dbmserr("opening database", sqlca.sqlcode);
111       exit(1);
112     }
113
114   EXEC SQL SELECT users_id INTO :who FROM users WHERE login = 'root';
115
116   while ((e = get_next_entry(in)))
117     {
118       process_entry(e, 0);
119       EXEC SQL COMMIT WORK;
120       if (sqlca.sqlcode)
121         {
122           dbmserr("committing work", sqlca.sqlcode);
123           exit(1);
124         }
125       if (wait)
126         {
127           printf("Next");
128           fflush(stdout);
129           fgets(buf, sizeof(buf), stdin);
130         }
131     }
132
133   exit(0);
134 }
135
136 struct entry *get_next_entry(FILE *in)
137 {
138   static struct entry e;
139   static char buf[BUFSIZ];
140   static char name[LEN_NAME + 1], sname[LEN_NAME + 1], id[LEN_ID + 1];
141   static char office[LEN_OFFICE + 1], phone[LEN_PHONE + 1];
142   static char phone2[LEN_PHONE2 + 1], dept[LEN_DEPT + 1], title[LEN_TITLE + 1];
143   int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
144   char *p, *q;
145
146   if (!fgets(buf, sizeof(buf), in))
147     return NULL;
148
149   strlcpy(id, &buf[LOC_ID], LEN_ID + 1);
150   strlcpy(name, &buf[LOC_NAME], LEN_NAME + 1);
151   strlcpy(office, &buf[LOC_OFFICE], LEN_OFFICE + 1);
152   strlcpy(phone, &buf[LOC_PHONE], LEN_PHONE + 1);
153   strlcpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2 + 1);
154   strlcpy(dept, &buf[LOC_DEPT], LEN_DEPT + 1);
155   strlcpy(title, &buf[LOC_TITLE], LEN_TITLE + 1);
156
157   strcpy(sname, name);
158   e.name = strtrim(sname);
159   p = strchr(name, ',');
160   if (p)
161     *p = '\0';
162   e.last = strtrim(name);
163   if (p)
164     {
165       p++;
166       while (isspace(*p))
167         p++;
168       e.first = p;
169       p = strchr(e.first, ' ');
170       if (p)
171         {
172           *p = '\0';
173           e.first = strtrim(e.first);
174           e.middle = strtrim(p + 1);
175         }
176       else
177         {
178           e.first = strtrim(e.first);
179           e.middle = "";
180         }
181     }
182   else
183     {
184       e.first = "";
185       e.middle = "";
186     }
187   ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
188   LookForSt(e.last);
189   LookForO(e.last);
190   LookForJrAndIII(e.last, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
191                   &ends_iv, &ends_v);
192   LookForJrAndIII(e.first, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
193                   &ends_iv, &ends_v);
194   FixCase(e.last);
195   FixCase(e.first);
196   FixCase(e.middle);
197
198   e.id = id;
199   e.haddr = e.hphone = "";
200
201   /* The following is really gross, but it happens to successfully convert
202    * new-style Warehouse office descriptions into (more-readable) old-style
203    * Personnel Office office descriptions.
204    */
205   e.oaddr = p = strtrim(office);
206   while (*p && !isspace(*p))
207     p++;
208   q = p;
209   while (isspace(*q))
210     q++;
211   if (*q && q < e.oaddr + LEN_OFFICE / 2)
212     {
213       *p++ = '-';
214       while (*q && q < e.oaddr + LEN_OFFICE / 2)
215         {
216           if (*q != ' ' && *q != '-')
217             *p++ = *q;
218           if (q > p)
219             *q = ' ';
220           q++;
221         }
222       memset(p, ' ', q - p);
223     }
224
225   p = e.oaddr + LEN_OFFICE / 2;
226   while (*p && !isspace(*p))
227     p++;
228   q = p;
229   while (isspace(*q))
230     q++;
231   if (*q)
232     {
233       *p++ = '-';
234       while (*q)
235         {
236           if (*q != ' ' && *q != '-')
237             *p++ = *q;
238           if (q > p)
239             *q = ' ';
240           q++;
241         }
242       memset(p, ' ', q - p);
243     }
244   strtrim(e.oaddr);
245   fixaddress(e.oaddr);
246   e.xaddress = e.oaddr;
247
248   e.ophone = e.xphone1 = strtrim(phone);
249   fixphone(e.ophone);
250   e.xphone2 = strtrim(phone2);
251   fixphone(e.xphone2);
252   e.dept = strtrim(dept);
253   e.xtitle = strtrim(title);
254
255   e.type = "MITS";
256   if (strstr(e.xtitle, "PROF") || strstr(e.xtitle, "LECTURE"))
257     e.type = "FACULTY";
258   if (!strcmp(e.dept, "LINCOLN LAB"))
259     e.type = "LINCOLN";
260
261   FixCase(e.dept);
262   FixCase(e.xtitle);
263
264   return &e;
265 }
This page took 0.047455 seconds and 5 git commands to generate.