Annotation of OpenXM_contrib2/fep/fep_util.c, Revision 1.3
1.1 noro 1: /* Copyright (c) 1987, 1988 by Software Research Associates, Inc. */
2:
3: #ifndef lint
4: static char rcsid[]=
5: "$Header: fep_util.c,v 4.1 88/08/28 18:57:41 utashiro Exp $ (SRA)";
6: #endif /* lint */
7:
8: #ifndef MKARGDEBUG
9:
10: #include <stdio.h>
1.3 ! ohara 11: #include <string.h>
1.1 noro 12: #include <pwd.h>
13: #include <sys/types.h>
1.2 noro 14: #if defined(__CYGWIN__) || defined(sun)
1.1 noro 15: #include <dirent.h>
16: #else
17: #include <sys/dir.h>
18: #endif
19: #include <ctype.h>
20: #include "fep_defs.h"
21:
22: message(messageString)
23: char *messageString;
24: {
25: write (2, messageString, strlen (messageString));
26: }
27:
28: errorBell()
29: {
30: write (2, "\007", 1);
31: }
32:
33: ctlprint(string)
34: char *string;
35: {
36: register char *cp;
37:
38: for (cp = string; *cp; cp++) {
39: if (isctlchar (*cp)) {
40: putchar ('^');
41: putchar (unctl (*cp));
42: }
43: else
44: putchar (*cp);
45: }
46: fputs ("\r\n", stdout);
47: fflush (stdout);
48: }
49:
50: /*
51: * Print string using "^" for control characters
52: */
53: printS (string)
54: char *string;
55: {
56: char *cp;
57:
58: for (cp = string; *cp; cp++)
59: putChar (*cp);
60: }
61:
62: /*
63: * Check the line is empty or not
64: */
65: is_empty_line(line)
66: char *line;
67: {
68: register char *cp;
69:
70: for (cp = line; *cp; cp++) {
71: if (!isspace(*cp)) {
72: return(0);
73: }
74: }
75: return(1);
76: }
77:
78: /*
79: * Put character using "^" for control characters
80: */
81: putChar(c)
82: char c;
83: {
84: if (isctlchar(c)) {
85: (void) putchar('^');
86: (void) putchar(unctl(c));
87: }
88: else
89: (void) putchar(c);
90: }
91:
92: char *
93: x_dirname (dir)
94: char *dir;
95: {
96: static char dirname [256];
97: char *index();
98:
99: if (*dir != '~')
100: strcpy (dirname, dir);
101: else {
102: struct passwd *pw;
103:
104: if (*(dir+1) == '/' || *(dir+1) == '\0') {
105: pw = getpwuid (getuid ());
106: }
107: else {
108: char user [64], *sp, *dp;
109:
110: for (sp = dir+1, dp = user; *sp && *sp != '/'; sp++, dp++)
111: *dp = *sp;
112: *dp = '\0';
113: pw = getpwnam (user);
114: }
115:
116: if (pw) {
117: strcpy (dirname, pw->pw_dir);
118: if (any ('/', dir))
119: strcat (dirname, index (dir, '/'));
120: }
121: else {
122: strcpy (dirname, dir);
123: }
124: }
125:
126: return (dirname);
127: }
128:
129: DIR *
130: x_opendir (dir)
131: char *dir;
132: {
133: return (opendir (x_dirname (dir)));
134: }
135:
136: /*
137: * Strring compare for qsort
138: */
139: scmp (a, b)
140: char **a, **b;
141: {
142:
143: return (strcmp (*a, *b));
144: }
145:
146: /*
147: * Return 1 if "str" is prefixed by "sub"
148: */
149: prefix (sub, str)
150: register char *sub, *str;
151: {
152:
153: for (;;) {
154: if (*sub == 0)
155: return (1);
156: if (*str == 0)
157: return (0);
158: if (*sub++ != *str++)
159: return (0);
160: }
161: }
162:
163: /*
164: * Return 1 if s includes character c
165: */
166: any (c, s)
167: register int c;
168: register char *s;
169: {
170:
171: while (*s)
172: if (*s++ == c)
173: return(1);
174: return(0);
175: }
176:
177: #ifndef max
178: /*
179: * Return maximum number of d1 and d2
180: */
181: max (d1, d2)
182: int d1, d2;
183: {
184: return (d1 > d2 ? d1 : d2);
185: }
186: #endif /* max */
187:
188: #else /* MKARGDEBUG */
189:
190: #include <stdio.h>
191: #include <ctype.h>
192:
193: #define MAXARGS 64
194:
195: main()
196: {
197: char s[128];
198: char *argv[MAXARGS];
199:
200: while (gets (s)) {
201: register int c;
202:
203: showArgs (s);
204: }
205: }
206: #endif /* MKARGDEBUG */
207:
208: showArgs (comline)
209: char *comline;
210: {
211: char *argv[MAXARGS];
212: register int c;
213: register char **argp;
214: register int i;
215:
216: c = mkargv (comline, argv, MAXARGS);
217: if (c < 0) {
218: printf ("%s\n", argv[0]);
219: return;
220: }
221: printf ("argc = %d\n", c);
222: for (i = 0, argp = argv; *argp; argp++, i++) {
223: printf ("\"%s\" ", *argp);
224: }
225: printf ("\n");
226: }
227:
228: mkargv (s, argv, maxarg)
229: char *s;
230: char *argv[];
231: int maxarg;
232: {
233: register char *cp;
234: register int argc = 0;
235: int insquot = 0, indquot = 0, ignorenext = 0;
236: enum {STRING, SPACE} status = SPACE;
237: static char buf[1024], *bp;
238:
239: for (cp = s, bp = buf; *cp; cp++) {
240:
241: if (argc > maxarg)
242: return (argc);
243:
244: /*
245: * Found white space
246: */
247: if (isspace (*cp)) {
248: /*
249: * In outside of quotation
250: */
251: if (!ignorenext && !insquot && !indquot) {
252: /*
253: * If status was in string, go next arg
254: */
255: if (status == STRING) {
256: status = SPACE;
257: *bp++ = '\0';
258: continue;
259: }
260: else
261: continue;
262: }
263: }
264:
265: # define SPECIALCHARS "\"\'\\"
266: if (!ignorenext && index (SPECIALCHARS, *cp)) {
267: switch (*cp) {
268:
269: /*
270: * Literal next character
271: */
272: case '\\':
273: if (indquot || insquot)
274: goto THROUGH;
275: else {
276: ignorenext = 1;
277: continue;
278: }
279:
280: /*
281: * Double quotation
282: */
283: case '\"':
284: if (insquot)
285: goto THROUGH;
286: if (indquot && *(bp-1) == '\\') {
287: bp--;
288: goto THROUGH;
289: }
290: indquot = !indquot;
291: break;
292:
293: /*
294: * Single quotation
295: */
296: case '\'':
297: if (indquot)
298: goto THROUGH;
299: if (insquot && *(bp-1) == '\\') {
300: bp--;
301: goto THROUGH;
302: }
303: insquot = !insquot;
304: break;
305: }
306:
307: /*
308: * Only in " or ' case.
309: */
310: if (status == SPACE) {
311: status = STRING;
312: argc++;
313: argv[argc-1] = bp;
314: *bp = 0;
315: }
316: continue;
317: }
318:
319: THROUGH:
320: /*
321: * Found non-space character
322: */
323: ignorenext = 0;
324: if (status == SPACE) {
325: status = STRING;
326: argc++;
327: argv[argc-1] = bp;
328: }
329: *bp++ = *cp;
330: }
331:
332: if (indquot || insquot) {
333: argv[0] = indquot ? "Unmatched \"." : "Unmatched \'.";
334: return (-1);
335: }
336:
337: *bp = '\0';
338: argv[argc] = (char *)0;
339: return (argc);
340: }
341:
342: reverse_strcpy (to, from)
343: register char *to, *from;
344: {
345: register int len;
346:
347: for (len = strlen (from); len >= 0; len--)
348: *(to + len) = *(from + len);
349: }
350:
351: char *search_string (s, lookup)
352: char *s, *lookup;
353: {
354: int len = strlen (lookup);
355: enum {SQUOTE, DQUOTE, NORMAL} status = NORMAL;
356:
357: while (*s) {
358: switch (*s) {
359: case '\'':
360: if (status == DQUOTE) break;
361: status = (status == SQUOTE) ? NORMAL : SQUOTE;
362: break;
363:
364: case '\"':
365: if (status == SQUOTE) break;
366: status = (status == DQUOTE) ? NORMAL : DQUOTE;
367: break;
368:
369: case '\\':
370: ++s;
371: break;
372: }
373: if (!*s) break;
374: if (status == NORMAL && strncmp (s, lookup, len) == 0)
375: return s;
376: ++s;
377: }
378: return ((char*) 0);
379: }
380:
381: #ifdef KANJI
382: /*
383: * Uuuuuuuum. I hate search kanji in Shift-JIS code from the end of string
384: * This function check if i'th character is first byte of KANJI code
385: * in string starting from s.
386: * It is assumed that first byte of strint s can't be second byte of KANJI
387: * code.
388: */
389: iskanji_in_string (s, i)
390: char *s;
391: int i;
392: {
393: register char *cp = s, *target = s + i;
394:
395: if (i < 0)
396: return (0);
397:
398: while (cp < target) {
399: if (iskanji (*cp))
400: cp += 2;
401: else
402: cp++;
403: }
404:
405: if (cp != target)
406: return (0);
407: else
408: return (iskanji (*cp));
409: }
410: #endif /* KANJI */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>