]> andersk Git - moira.git/blob - regtape/staff.pc
Make the install rule up to date.
[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], 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   p = strchr(name, ',');
158   if (p)
159     *p = '\0';
160   e.last = strtrim(name);
161   if (p)
162     {
163       p++;
164       while (isspace(*p))
165         p++;
166       e.first = p;
167       p = strchr(e.first, ' ');
168       if (p)
169         {
170           *p = '\0';
171           e.first = strtrim(e.first);
172           e.middle = strtrim(p + 1);
173         }
174       else
175         {
176           e.first = strtrim(e.first);
177           e.middle = "";
178         }
179     }
180   else
181     {
182       e.first = "";
183       e.middle = "";
184     }
185   ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
186   LookForSt(e.last);
187   LookForO(e.last);
188   LookForJrAndIII(e.last, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
189                   &ends_iv, &ends_v);
190   LookForJrAndIII(e.first, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
191                   &ends_iv, &ends_v);
192   FixCase(e.last);
193   FixCase(e.first);
194   FixCase(e.middle);
195
196   e.name = buf;
197   if (*e.middle)
198     sprintf(e.name, "%s %s %s", e.first, e.middle, e.last);
199   else
200     sprintf(e.name, "%s %s", e.first, e.last);
201
202   e.id = id;
203   e.haddr = e.hphone = "";
204
205   /* The following is really gross, but it happens to successfully convert
206    * new-style Warehouse office descriptions into (more-readable) old-style
207    * Personnel Office office descriptions.
208    */
209   e.oaddr = p = strtrim(office);
210   while (*p && !isspace(*p))
211     p++;
212   q = p;
213   while (isspace(*q))
214     q++;
215   if (*q && q < e.oaddr + LEN_OFFICE / 2)
216     {
217       *p++ = '-';
218       while (*q && q < e.oaddr + LEN_OFFICE / 2)
219         {
220           if (*q != ' ' && *q != '-')
221             *p++ = *q;
222           if (q > p)
223             *q = ' ';
224           q++;
225         }
226       memset(p, ' ', q - p);
227     }
228
229   p = e.oaddr + LEN_OFFICE / 2;
230   while (*p && !isspace(*p))
231     p++;
232   q = p;
233   while (isspace(*q))
234     q++;
235   if (*q)
236     {
237       *p++ = '-';
238       while (*q)
239         {
240           if (*q != ' ' && *q != '-')
241             *p++ = *q;
242           if (q > p)
243             *q = ' ';
244           q++;
245         }
246       memset(p, ' ', q - p);
247     }
248   strtrim(e.oaddr);
249   fixaddress(e.oaddr);
250   e.xaddress = e.oaddr;
251
252   e.ophone = e.xphone1 = strtrim(phone);
253   fixphone(e.ophone);
254   e.xphone2 = strtrim(phone2);
255   fixphone(e.xphone2);
256   e.dept = strtrim(dept);
257   e.xtitle = strtrim(title);
258
259   e.type = "MITS";
260   if (strstr(e.xtitle, "PROF") || strstr(e.xtitle, "LECTURE"))
261     e.type = "FACULTY";
262   if (!strcmp(e.dept, "LINCOLN LAB"))
263     e.type = "LINCOLN";
264
265   FixCase(e.dept);
266   FixCase(e.xtitle);
267
268   return &e;
269 }
This page took 0.129227 seconds and 5 git commands to generate.