|
|
1.1 root 1: /*
2: * build0.c
3: * 07/11/92
4: * Routines common to build and install.
5: */
6:
7: #include <stdio.h>
8: #include <sys/stat.h>
9: #include "build0.h"
10:
11: /* Globals. */
12: int abortmsg; /* print abort message */
13: char *argv0; /* for error messages */
14: char buf[NBUF]; /* input buffer */
15: char cmd[NBUF]; /* command buffer */
16: int dflag; /* debug */
17: char *usagemsg; /* usage message */
18: int vflag; /* verbose */
19:
20: /*
21: * Clear the IBM AT console screen.
22: * Prompt for <Enter> if the flag is true or if dflag.
23: */
24: void
25: cls(flag) register int flag;
26: {
27: if (flag || dflag)
28: get_line("\Hit <Enter> to continue...");
29: putchar(0x1B); /* ESC */
30: putchar('[');
31: putchar('2');
32: putchar('J');
33: fflush(stdout);
34: }
35:
36: /*
37: * Return 1 if file exists, 0 if not.
38: */
39: int
40: exists(file) register char *file;
41: {
42: register int fd;
43:
44: if ((fd = open(file, 0)) < 0)
45: return 0;
46: close(fd);
47: return 1;
48: }
49:
50: /*
51: * Print a fatal error message.
52: * Print "Installation aborted..." message if invoked from build or install.
53: */
54: void
55: fatal(s) char *s;
56: {
57: fprintf(stderr, "%s: %r\n", argv0, &s);
58: if (abortmsg)
59: fprintf(stderr, "Installation aborted before completion.\n");
60: exit(1);
61: }
62:
63: /*
64: * Get an integer value in the given range.
65: */
66: /* VARARGS */
67: int
68: get_int(min, max, prompt) int min, max; char *prompt;
69: {
70: register char *s;
71: register int i;
72:
73: s = get_line("%r", &prompt);
74: for (;;) {
75: if (*s >= '0' && *s <= '9') {
76: i = atoi(s);
77: if (i >= min && i <= max)
78: return i;
79: }
80: get_line("Enter a value between %d and %d:", min, max);
81: }
82: }
83:
84: /*
85: * Get an integer value from a given list.
86: * Val_list is a an int array of acceptable int values.
87: * Num_vals is the number of acceptable values in val_list, must be > 0.
88: * If strict is 0, allow user to enter value not in list after a warning.
89: * If default is not -1, display it in brackets and use it if just <enter>
90: * is pressed.
91: * If hex is nonzero, display numeric values in hexadecimal, else decimal.
92: */
93: /* VARARGS */
94: int
95: get_allowed_int(val_list, num_vals, hex, default_val, strict_val, prompt)
96: int * val_list;
97: int num_vals;
98: int hex;
99: int default_val;
100: int strict_val;
101: char *prompt;
102: {
103: register char *s;
104: register int i;
105: int user_val;
106: char * fmt_str;
107:
108: /* Display the given prompt string. */
109: printf("%r\n", &prompt);
110:
111: /* Get input from user. */
112: for (;;) {
113: /* Display second prompt line with acceptable values. */
114: if (strict_val)
115: printf("Valid choices are ");
116: else
117: printf("Standard choices are ");
118:
119: for (i = 0; i < num_vals; i++) {
120: if (i > 0)
121: putchar('/');
122: fmt_str = hex ? "%x" : "%d" ;
123: printf(fmt_str, val_list[i]);
124: }
125:
126: if (default_val != -1) {
127: fmt_str = hex ? " [%x]" : " [%d]" ;
128: printf(fmt_str, default_val);
129: }
130: s = get_line(" : ");
131:
132: if (*s == '\0')
133: return default_val;
134:
135: fmt_str = hex ? "%x" : "%d" ;
136:
137: if (sscanf(s, fmt_str, & user_val)) {
138:
139: /* Check the value entered. */
140: for (i = 0; i < num_vals; i++) {
141: if (user_val == val_list[i])
142: return user_val;
143: }
144: if (!strict_val &&
145: yes_no("That is not one of the standard values. "
146: "Use anyway? "))
147: return user_val;
148: }
149: }
150: }
151:
152: /*
153: * Print the args and get a line from the user to buf[].
154: * Strip the trailing newline and return a pointer to the first non-space.
155: */
156: /* VARARGS */
157: char *
158: get_line(args) char *args;
159: {
160: register char *s;
161:
162: printf("%r ", &args);
163: fflush(stdout);
164: fgets(buf, sizeof buf, stdin);
165: buf[strlen(buf) - 1] = '\0';
166: for (s = buf; ; ++s)
167: if (*s != ' ' && *s != '\t')
168: return s;
169: }
170:
171: /*
172: * Return:
173: * 1 dir exists and is a directory,
174: * 0 dir does not exist,
175: * or -1 dir exists but is not a directory.
176: */
177: int
178: is_dir(dir) register char *dir;
179: {
180: struct stat s;
181:
182: if (stat(dir, &s) == -1)
183: return 0; /* does not exist */
184: return (s.st_mode & S_IFDIR) ? 1 : -1; /* directory or nondirectory */
185: }
186:
187: /*
188: * Print a nonfatal error message.
189: * Make sure the user sees it by waiting until he hits <Enter>.
190: */
191: void
192: nonfatal(s) char *s;
193: {
194: fprintf(stderr, "%s: %r\n", argv0, &s);
195: get_line("\Hit <Enter> to continue...");
196: }
197:
198: /*
199: * Execute the given command and return its exit status.
200: * The flag tells what to do if the command returns an error status:
201: * S_IGNORE ignore it
202: * S_NONFATAL report it
203: * S_FATAL report it and die
204: */
205: int
206: sys(command, flag) char *command; int flag;
207: {
208: register int status;
209:
210: if (dflag || vflag)
211: printf("%s\n", command);
212: if (dflag)
213: return 0;
214: if ((status = system(command)) != 0) {
215: if (flag == S_NONFATAL)
216: nonfatal("command \"%s\" failed", command);
217: else if (flag == S_FATAL)
218: fatal("command \"%s\" failed", command);
219: }
220: sync();
221: return status;
222: }
223:
224: /*
225: * Print a usage message and die.
226: */
227: void
228: usage()
229: {
230: fprintf(stderr, usagemsg);
231: exit(1);
232: }
233:
234: /*
235: * Get the answer to a yes/no question.
236: * Return 1 for yes, 0 for no.
237: */
238: /* VARARGS */
239: int
240: yes_no(args) char *args;
241: {
242: register char *s;
243:
244: for (;;) {
245: printf("%r", &args);
246: s = get_line(" [y or n]?");
247: if (*s == 'y' || *s == 'Y')
248: return 1;
249: else if (*s == 'n' || *s == 'N')
250: return 0;
251: }
252: }
253:
254: /* end of build0.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.