Annotation of libwww/Library/src/HTAlert.c, revision 2.29
2.18 frystyk 1: /* HTAlert.c
2: ** DISPLAYING MESSAGES AND GETTING INPUT FOR LINEMODE BROWSER
3: **
2.23 frystyk 4: ** (c) COPYRIGHT MIT 1995.
2.18 frystyk 5: ** Please first read the full copyright statement in the file COPYRIGH.
1.1 timbl 6: **
7: ** REPLACE THIS MODULE with a GUI version in a GUI environment!
8: **
9: ** History:
10: ** Jun 92 Created May 1992 By C.T. Barker
11: ** Feb 93 Simplified, portablised TBL
2.8 luotonen 12: ** Sep 93 Corrected 3 bugs in HTConfirm() :-( AL
1.1 timbl 13: */
14:
2.20 frystyk 15: /* Library include files */
2.29 ! frystyk 16: #include "WWWLib.h"
2.21 frystyk 17: #include "HTError.h" /* Implemented here */
18: #include "HTAlert.h" /* Implemented here */
1.1 timbl 19:
2.28 frystyk 20: typedef struct _HTProgMsg {
21: HTProgressState state;
22: char * msg;
23: } HTProgMsg;
24:
2.25 frystyk 25: PRIVATE BOOL HTInteractive=YES; /* Any prompts from the Library? */
1.1 timbl 26:
2.21 frystyk 27: /* ------------------------------------------------------------------------- */
2.25 frystyk 28:
2.26 frystyk 29: PUBLIC void HTPrompt_setInteractive ARGS1(BOOL, interactive)
2.25 frystyk 30: {
31: HTInteractive = interactive;
32: }
33:
2.26 frystyk 34: PUBLIC BOOL HTPrompt_interactive NOARGS
2.25 frystyk 35: {
36: return HTInteractive;
37: }
2.21 frystyk 38:
2.28 frystyk 39: PUBLIC void HTProgress ARGS3(HTRequest *, request, HTProgressState, state,
40: void *, param)
2.21 frystyk 41: {
2.28 frystyk 42: /* This is just to avoid that we get a lot of progress messages in LMB */
43: if (!TRACE) return;
44:
2.29 ! frystyk 45: if (!request) {
2.28 frystyk 46: if (TRACE)
47: fprintf(TDEST, "HTProgress.. Bad argument\n");
48: return;
49: }
50: switch (state) {
51: case HT_PROG_DNS:
52: fprintf(TDEST, "Looking up %s\n", (char *) param);
53: break;
54:
55: case HT_PROG_CONNECT:
56: fprintf(TDEST, "Contacting host...\n");
57: break;
58:
59: case HT_PROG_READ:
60: {
2.29 ! frystyk 61: long cl = HTAnchor_length(HTRequest_anchor(request));
2.28 frystyk 62: if (cl > 0) {
2.29 ! frystyk 63: long b_read = HTRequest_bytesRead(request);
2.28 frystyk 64: double pro = (double) b_read/cl*100;
65: char buf[10];
66: HTNumToStr((unsigned long) cl, buf);
67: fprintf(TDEST, "Read (%d%% of %s)\n", (int) pro, buf);
68: } else
69: fprintf(TDEST, "Reading...\n");
70: }
71: break;
72:
73: case HT_PROG_WRITE:
74: fprintf(TDEST, "Writing...\n");
75: break;
76:
77: case HT_PROG_DONE:
78: fprintf(TDEST, "Finished\n");
79: break;
80:
81: default:
82: fprintf(TDEST, "UNKNOWN PROGRESS STATE\n");
83: break;
84: }
2.21 frystyk 85: }
86:
87:
2.27 frystyk 88: PUBLIC void HTAlert ARGS2(HTRequest *, request, CONST char *, Msg)
1.1 timbl 89: {
2.20 frystyk 90: #ifdef NeXTStep
2.6 timbl 91: NXRunAlertPanel(NULL, "%s", NULL, NULL, NULL, Msg);
92: #else
2.21 frystyk 93: fprintf(TDEST, "\nWARNING: %s\n", Msg ? Msg : "UNKNOWN");
2.6 timbl 94: #endif
1.1 timbl 95: }
96:
2.27 frystyk 97: PUBLIC BOOL HTConfirm ARGS2(HTRequest *, request, CONST char *, Msg)
1.1 timbl 98: {
2.8 luotonen 99: char Reply[4]; /* One more for terminating NULL -- AL */
1.1 timbl 100: char *URep;
101:
2.21 frystyk 102: fprintf(TDEST, "%s (y/n) ", Msg ? Msg : "UNKNOWN");
2.20 frystyk 103: #ifndef NO_STDIO
2.16 frystyk 104: if (!HTInteractive || !fgets(Reply, 4, stdin)) /* get reply, max 3 chars */
2.20 frystyk 105: #endif
2.14 frystyk 106: return NO;
1.1 timbl 107: URep=Reply;
2.8 luotonen 108: while (*URep) {
2.9 luotonen 109: if (*URep == '\n') {
2.10 luotonen 110: *URep = (char)0; /* Overwrite newline */
2.9 luotonen 111: break;
112: }
2.8 luotonen 113: *URep=TOUPPER(*URep);
114: URep++; /* This was previously embedded in the TOUPPER */
115: /* call an it became evaluated twice because */
116: /* TOUPPER is a macro -- AL */
117: }
118:
1.1 timbl 119: if ((strcmp(Reply,"YES")==0) || (strcmp(Reply,"Y")==0))
120: return(YES);
121: else
122: return(NO);
123: }
124:
2.20 frystyk 125: /* Prompt for answer and get text back. Reply text is either NULL on
126: ** error or a dynamic string which the caller must free.
1.1 timbl 127: */
2.27 frystyk 128: PUBLIC char * HTPrompt ARGS3(HTRequest *, request, CONST char *, Msg,
129: CONST char *, deflt)
1.1 timbl 130: {
2.20 frystyk 131: char buffer[200];
132: char *reply = NULL;
2.24 frystyk 133: fprintf(TDEST, "%s ", Msg ? Msg : "UNKNOWN");
2.20 frystyk 134: if (deflt)
2.24 frystyk 135: fprintf(TDEST, "(RETURN for [%s]) ", deflt);
2.20 frystyk 136:
137: if (HTInteractive) {
138: #ifndef NO_STDIO
139: if (!fgets(buffer, 200, stdin))
140: return NULL; /* NULL string on error, Henrik */
141: buffer[strlen(buffer)-1] = '\0'; /* Overwrite newline */
2.24 frystyk 142: if (*buffer)
143: StrAllocCopy(reply, buffer);
144: else if (deflt)
145: StrAllocCopy(reply, deflt);
2.20 frystyk 146: #endif
147: }
148: return reply;
1.1 timbl 149: }
2.8 luotonen 150:
151:
2.20 frystyk 152: /* Prompt for password without echoing the reply. Reply text is
153: ** either NULL on error or a dynamic string which the caller must free.
2.8 luotonen 154: */
2.27 frystyk 155: PUBLIC char * HTPromptPassword ARGS2(HTRequest *, request, CONST char *, Msg)
2.8 luotonen 156: {
2.20 frystyk 157: char *reply = NULL;
158: if (HTInteractive) {
159: #ifndef NO_PASSWD
160: char *pw = (char *) getpass(Msg ? Msg : "Password: ");
161: if (pw)
162: StrAllocCopy(reply, pw);
2.19 roeber 163: #endif
2.20 frystyk 164: }
165: return reply;
2.11 luotonen 166: }
167:
168:
169: /* Prompt both username and password HTPromptUsernameAndPassword()
170: ** ---------------------------------
171: ** On entry,
172: ** Msg is the prompting message.
173: ** *username and
174: ** *password are char pointers; they are changed
175: ** to point to result strings.
176: **
177: ** If *username is not NULL, it is taken
178: ** to point to a default value.
179: ** Initial value of *password is
180: ** completely discarded.
181: **
182: ** On exit,
183: ** *username and *password point to newly allocated
184: ** strings -- original strings pointed to by them
185: ** are NOT freed.
186: **
187: */
2.27 frystyk 188: PUBLIC void HTPromptUsernameAndPassword ARGS4(HTRequest *, request,
189: CONST char *, Msg,
2.11 luotonen 190: char **, username,
191: char **, password)
192: {
2.21 frystyk 193: fprintf(TDEST, "%s\n", Msg ? Msg : "UNKNOWN");
2.27 frystyk 194: *username = HTPrompt(request, "Username:", *username);
195: *password = HTPromptPassword(request, "Password: ");
2.8 luotonen 196: }
197:
2.21 frystyk 198:
199: /* HTErrorMsg
200: **
201: ** Default function that creates an error message using HTAlert() to
202: ** put out the contents of the error_stack messages. Furthermore, the
203: ** error_info structure contains a name of a help file that might be put
204: ** up as a link. This file can then be multi-linguistic.
205: **
206: ** This function might be overwritten by a smart server or client.
207: */
208: PUBLIC void HTErrorMsg ARGS1(HTRequest *, request)
209: {
2.29 ! frystyk 210: HTList *cur = HTRequest_errorStack(request);
2.21 frystyk 211: BOOL highest = YES;
212: HTChunk *chunk;
213: HTErrorInfo *pres;
214:
215: /* Output messages */
216: chunk = HTChunkCreate(128);
217: while ((pres = (HTErrorInfo *) HTList_nextObject(cur))) {
218:
219: /* Check if we are going to show the message */
220: if ((!pres->ignore || HTErrorShowMask & HT_ERR_SHOW_IGNORE) &&
221: (HTErrorShowMask & pres->severity)) {
222:
223: /* Output code number */
224: if (highest) { /* If first time through */
225: if (TRACE)
226: fprintf(TDEST,
227: "HTError..... Generating message.\n");
228:
229: /* Output title */
230: if (pres->severity == ERR_WARN)
231: HTChunkPuts(chunk, "Warning ");
232: else if (pres->severity == ERR_NON_FATAL)
233: HTChunkPuts(chunk, "Non Fatal Error ");
234: else if (pres->severity == ERR_FATAL)
235: HTChunkPuts(chunk, "Fatal Error ");
236: else if (pres->severity == ERR_INFO)
237: HTChunkPuts(chunk, "Information ");
238: else {
239: if (TRACE)
240: fprintf(TDEST, "HTError..... Unknown Classification of Error (%d)...\n", pres->severity);
241: HTChunkFree(chunk);
242: return;
243: }
244:
245: /* Only output error code if it is a real HTTP code */
246: if (pres->element < HTERR_HTTP_CODES_END) {
247: char codestr[10];
248: sprintf(codestr, "%d ", error_info[pres->element].code);
249: HTChunkPuts(chunk, codestr);
250: }
251: highest = NO;
252: } else
253: HTChunkPuts(chunk, "\nReason: ");
254:
255: /* Output error message */
256: if (pres->element != HTERR_SYSTEM) {
257: HTChunkPuts(chunk, error_info[pres->element].msg);
258: HTChunkPutc(chunk, ' ');
259: }
260:
261: /* Output parameters */
262: if (pres->par && HTErrorShowMask & HT_ERR_SHOW_PARS) {
263: unsigned int cnt;
264: char ch;
2.22 frystyk 265: HTChunkPutc(chunk, '(');
2.21 frystyk 266: for (cnt=0; cnt<pres->par_length; cnt++) {
267: ch = *((char *)(pres->par)+cnt);
268: if (ch < 0x20 || ch >= 0x7F)
269: HTChunkPutc(chunk, '#'); /* Can't print real content */
270: else
271: HTChunkPutc(chunk, ch);
272: }
2.22 frystyk 273: HTChunkPutc(chunk, ')');
2.21 frystyk 274: }
275:
276: /* Output location */
277: if (pres->where && HTErrorShowMask & HT_ERR_SHOW_LOCATION) {
278: HTChunkPuts(chunk, "This occured in ");
279: HTChunkPuts(chunk, pres->where);
280: HTChunkPutc(chunk, '\n');
281: }
282:
283: /* We don't want the message more than once */
284: HTErrorIgnore(request, pres->handle);
285:
286: /* If we only are going to show the higest entry */
287: if (HTErrorShowMask & HT_ERR_SHOW_FIRST)
288: break;
289: }
290: }
291: HTChunkPutc(chunk, '\n');
292: HTChunkTerminate(chunk);
2.26 frystyk 293: if (HTChunkSize(chunk) > 2)
2.27 frystyk 294: HTAlert(request, HTChunkData(chunk));
2.21 frystyk 295: HTChunkFree(chunk);
296: return;
297: }
Webmaster