]> andersk Git - moira.git/blame - regtape/staff.pc
Append to logfile instead of blowing it away.
[moira.git] / regtape / staff.pc
CommitLineData
7ac48069 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>.
469f80df 8 */
9
7ac48069 10#include <mit-copyright.h>
932028de 11#include <moira.h>
12#include <moira_site.h>
dfaf9b68 13#include <moira_schema.h>
f0df8832 14#include "common.h"
7ac48069 15
16#include <ctype.h>
17#include <stdio.h>
18#include <string.h>
19
02cd9ede 20EXEC SQL INCLUDE sqlca;
469f80df 21
7ac48069 22RCSID("$Header$");
469f80df 23
469f80df 24/* File format is:
f0df8832 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 */
469f80df 34
35#define LOC_ID 0
469f80df 36#define LEN_ID 9
f0df8832 37#define LOC_NAME (LOC_ID + LEN_ID)
a6e9fead 38#define LEN_NAME 30
f0df8832 39#define LOC_OFFICE (LOC_NAME + LEN_NAME)
a6e9fead 40#define LEN_OFFICE 24
f0df8832 41#define LOC_PHONE (LOC_OFFICE + LEN_OFFICE)
469f80df 42#define LEN_PHONE 12
f0df8832 43#define LOC_PHONE2 (LOC_PHONE + LEN_PHONE)
469f80df 44#define LEN_PHONE2 12
f0df8832 45#define LOC_DEPT (LOC_PHONE2 + LEN_PHONE2)
e5843fe8 46#define LEN_DEPT 50
f0df8832 47#define LOC_TITLE (LOC_DEPT + LEN_DEPT)
a6e9fead 48#define LEN_TITLE 50
469f80df 49
7ac48069 50struct entry *get_next_entry(FILE *in);
f0df8832 51void process_entry(struct entry *e, int secure);
469f80df 52
f0df8832 53EXEC SQL BEGIN DECLARE SECTION;
54int who;
55char *prog = "stafload";
56EXEC SQL END DECLARE SECTION;
3e77c6a3 57
f0df8832 58char *whoami;
469f80df 59
5eaef520 60int main(int argc, char **argv)
02cd9ede 61{
5eaef520 62 FILE *in;
7ac48069 63 struct entry *e;
5eaef520 64 int i, wait = 0;
dfaf9b68 65 char buf[80], *file = NULL;
5eaef520 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++;
5eaef520 83 else if (file)
15e28a76 84 {
85 fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
86 exit(1);
87 }
5eaef520 88 else
89 file = argv[i];
469f80df 90 }
91
15e28a76 92 if (!file)
93 {
94 fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
95 exit(1);
96 }
97
5eaef520 98 in = fopen(file, "r");
99 if (!in)
100 {
101 fprintf(stderr, "Unable to open %s for input\n", file);
102 exit(1);
469f80df 103 }
104
5eaef520 105 initialize_sms_error_table();
325821e4 106
5eaef520 107 EXEC SQL CONNECT :db IDENTIFIED BY :db;
108 if (sqlca.sqlcode)
109 {
110 dbmserr("opening database", sqlca.sqlcode);
111 exit(1);
3e77c6a3 112 }
469f80df 113
f0df8832 114 EXEC SQL SELECT users_id INTO :who FROM users WHERE login = 'root';
115
5eaef520 116 while ((e = get_next_entry(in)))
117 {
f0df8832 118 process_entry(e, 0);
5eaef520 119 EXEC SQL COMMIT WORK;
120 if (sqlca.sqlcode)
121 {
9f5e5c05 122 dbmserr("committing work", sqlca.sqlcode);
123 exit(1);
3e77c6a3 124 }
5eaef520 125 if (wait)
126 {
127 printf("Next");
128 fflush(stdout);
dfaf9b68 129 fgets(buf, sizeof(buf), stdin);
469f80df 130 }
131 }
132
5eaef520 133 exit(0);
02cd9ede 134}
469f80df 135
5eaef520 136struct entry *get_next_entry(FILE *in)
469f80df 137{
5eaef520 138 static struct entry e;
0e5cfe5f 139 static char buf[BUFSIZ];
5eaef520 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];
5eaef520 143 int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
b92021f7 144 char *p, *q;
5eaef520 145
146 if (!fgets(buf, sizeof(buf), in))
147 return NULL;
148
f0df8832 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);
5eaef520 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))
a6e9fead 167 p++;
5eaef520 168 e.first = p;
f0df8832 169 p = strchr(e.first, ' ');
170 if (p)
5eaef520 171 {
172 *p = '\0';
173 e.first = strtrim(e.first);
174 e.middle = strtrim(p + 1);
a6e9fead 175 }
5eaef520 176 else
177 {
178 e.first = strtrim(e.first);
179 e.middle = "";
180 }
181 }
182 else
183 {
184 e.first = "";
185 e.middle = "";
a6e9fead 186 }
5eaef520 187 ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
188 LookForSt(e.last);
189 LookForO(e.last);
7ac48069 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);
5eaef520 194 FixCase(e.last);
195 FixCase(e.first);
196 FixCase(e.middle);
197
198 e.id = id;
f0df8832 199 e.haddr = e.hphone = "";
5eaef520 200
b92021f7 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 */
f0df8832 205 e.oaddr = p = strtrim(office);
b92021f7 206 while (*p && !isspace(*p))
207 p++;
208 q = p;
209 while (isspace(*q))
210 q++;
f0df8832 211 if (*q && q < e.oaddr + LEN_OFFICE / 2)
b92021f7 212 {
213 *p++ = '-';
f0df8832 214 while (*q && q < e.oaddr + LEN_OFFICE / 2)
b92021f7 215 {
216 if (*q != ' ' && *q != '-')
217 *p++ = *q;
218 if (q > p)
219 *q = ' ';
220 q++;
221 }
222 memset(p, ' ', q - p);
223 }
224
f0df8832 225 p = e.oaddr + LEN_OFFICE / 2;
b92021f7 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 }
f0df8832 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);
5eaef520 252 e.dept = strtrim(dept);
f0df8832 253 e.xtitle = strtrim(title);
5eaef520 254
f0df8832 255 e.type = "MITS";
256 if (strstr(e.xtitle, "PROF") || strstr(e.xtitle, "LECTURE"))
257 e.type = "FACULTY";
5eaef520 258 if (!strcmp(e.dept, "LINCOLN LAB"))
f0df8832 259 e.type = "LINCOLN";
6c4dab06 260
f0df8832 261 FixCase(e.dept);
262 FixCase(e.xtitle);
6c4dab06 263
f0df8832 264 return &e;
9f5e5c05 265}
This page took 0.125279 seconds and 5 git commands to generate.