Annotation of libwww/Library/src/HTAlert.c, revision 2.30
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:
2.30 ! frystyk 81: case HT_PROG_WAIT:
! 82: fprintf(TDEST, "Waiting for free socket...\n");
! 83: break;
! 84:
2.28 frystyk 85: default:
86: fprintf(TDEST, "UNKNOWN PROGRESS STATE\n");
87: break;
88: }
2.21 frystyk 89: }
90:
91:
2.27 frystyk 92: PUBLIC void HTAlert ARGS2(HTRequest *, request, CONST char *, Msg)
1.1 timbl 93: {
2.20 frystyk 94: #ifdef NeXTStep
2.6 timbl 95: NXRunAlertPanel(NULL, "%s", NULL, NULL, NULL, Msg);
96: #else
2.21 frystyk 97: fprintf(TDEST, "\nWARNING: %s\n", Msg ? Msg : "UNKNOWN");
2.6 timbl 98: #endif
1.1 timbl 99: }
100:
2.27 frystyk 101: PUBLIC BOOL HTConfirm ARGS2(HTRequest *, request, CONST char *, Msg)
1.1 timbl 102: {
2.8 luotonen 103: char Reply[4]; /* One more for terminating NULL -- AL */
1.1 timbl 104: char *URep;
105:
2.21 frystyk 106: fprintf(TDEST, "%s (y/n) ", Msg ? Msg : "UNKNOWN");
2.20 frystyk 107: #ifndef NO_STDIO
2.16 frystyk 108: if (!HTInteractive || !fgets(Reply, 4, stdin)) /* get reply, max 3 chars */
2.20 frystyk 109: #endif
2.14 frystyk 110: return NO;
1.1 timbl 111: URep=Reply;
2.8 luotonen 112: while (*URep) {
2.9 luotonen 113: if (*URep == '\n') {
2.10 luotonen 114: *URep = (char)0; /* Overwrite newline */
2.9 luotonen 115: break;
116: }
2.8 luotonen 117: *URep=TOUPPER(*URep);
118: URep++; /* This was previously embedded in the TOUPPER */
119: /* call an it became evaluated twice because */
120: /* TOUPPER is a macro -- AL */
121: }
122:
1.1 timbl 123: if ((strcmp(Reply,"YES")==0) || (strcmp(Reply,"Y")==0))
124: return(YES);
125: else
126: return(NO);
127: }
128:
2.20 frystyk 129: /* Prompt for answer and get text back. Reply text is either NULL on
130: ** error or a dynamic string which the caller must free.
1.1 timbl 131: */
2.27 frystyk 132: PUBLIC char * HTPrompt ARGS3(HTRequest *, request, CONST char *, Msg,
133: CONST char *, deflt)
1.1 timbl 134: {
2.20 frystyk 135: char buffer[200];
136: char *reply = NULL;
2.24 frystyk 137: fprintf(TDEST, "%s ", Msg ? Msg : "UNKNOWN");
2.20 frystyk 138: if (deflt)
2.24 frystyk 139: fprintf(TDEST, "(RETURN for [%s]) ", deflt);
2.20 frystyk 140:
141: if (HTInteractive) {
142: #ifndef NO_STDIO
143: if (!fgets(buffer, 200, stdin))
144: return NULL; /* NULL string on error, Henrik */
145: buffer[strlen(buffer)-1] = '\0'; /* Overwrite newline */
2.24 frystyk 146: if (*buffer)
147: StrAllocCopy(reply, buffer);
148: else if (deflt)
149: StrAllocCopy(reply, deflt);
2.20 frystyk 150: #endif
151: }
152: return reply;
1.1 timbl 153: }
2.8 luotonen 154:
155:
2.20 frystyk 156: /* Prompt for password without echoing the reply. Reply text is
157: ** either NULL on error or a dynamic string which the caller must free.
2.8 luotonen 158: */
2.27 frystyk 159: PUBLIC char * HTPromptPassword ARGS2(HTRequest *, request, CONST char *, Msg)
2.8 luotonen 160: {
2.20 frystyk 161: char *reply = NULL;
162: if (HTInteractive) {
163: #ifndef NO_PASSWD
164: char *pw = (char *) getpass(Msg ? Msg : "Password: ");
165: if (pw)
166: StrAllocCopy(reply, pw);
2.19 roeber 167: #endif
2.20 frystyk 168: }
169: return reply;
2.11 luotonen 170: }
171:
172:
173: /* Prompt both username and password HTPromptUsernameAndPassword()
174: ** ---------------------------------
175: ** On entry,
176: ** Msg is the prompting message.
177: ** *username and
178: ** *password are char pointers; they are changed
179: ** to point to result strings.
180: **
181: ** If *username is not NULL, it is taken
182: ** to point to a default value.
183: ** Initial value of *password is
184: ** completely discarded.
185: **
186: ** On exit,
187: ** *username and *password point to newly allocated
188: ** strings -- original strings pointed to by them
189: ** are NOT freed.
190: **
191: */
2.27 frystyk 192: PUBLIC void HTPromptUsernameAndPassword ARGS4(HTRequest *, request,
193: CONST char *, Msg,
2.11 luotonen 194: char **, username,
195: char **, password)
196: {
2.21 frystyk 197: fprintf(TDEST, "%s\n", Msg ? Msg : "UNKNOWN");
2.27 frystyk 198: *username = HTPrompt(request, "Username:", *username);
199: *password = HTPromptPassword(request, "Password: ");
2.8 luotonen 200: }
201:
2.21 frystyk 202:
203: /* HTErrorMsg
204: **
205: ** Default function that creates an error message using HTAlert() to
206: ** put out the contents of the error_stack messages. Furthermore, the
207: ** error_info structure contains a name of a help file that might be put
208: ** up as a link. This file can then be multi-linguistic.
209: **
210: ** This function might be overwritten by a smart server or client.
211: */
212: PUBLIC void HTErrorMsg ARGS1(HTRequest *, request)
213: {
2.29 frystyk 214: HTList *cur = HTRequest_errorStack(request);
2.21 frystyk 215: BOOL highest = YES;
216: HTChunk *chunk;
217: HTErrorInfo *pres;
218:
219: /* Output messages */
220: chunk = HTChunkCreate(128);
221: while ((pres = (HTErrorInfo *) HTList_nextObject(cur))) {
222:
223: /* Check if we are going to show the message */
224: if ((!pres->ignore || HTErrorShowMask & HT_ERR_SHOW_IGNORE) &&
225: (HTErrorShowMask & pres->severity)) {
226:
227: /* Output code number */
228: if (highest) { /* If first time through */
229: if (TRACE)
230: fprintf(TDEST,
231: "HTError..... Generating message.\n");
232:
233: /* Output title */
234: if (pres->severity == ERR_WARN)
235: HTChunkPuts(chunk, "Warning ");
236: else if (pres->severity == ERR_NON_FATAL)
237: HTChunkPuts(chunk, "Non Fatal Error ");
238: else if (pres->severity == ERR_FATAL)
239: HTChunkPuts(chunk, "Fatal Error ");
240: else if (pres->severity == ERR_INFO)
241: HTChunkPuts(chunk, "Information ");
242: else {
243: if (TRACE)
244: fprintf(TDEST, "HTError..... Unknown Classification of Error (%d)...\n", pres->severity);
245: HTChunkFree(chunk);
246: return;
247: }
248:
249: /* Only output error code if it is a real HTTP code */
250: if (pres->element < HTERR_HTTP_CODES_END) {
251: char codestr[10];
252: sprintf(codestr, "%d ", error_info[pres->element].code);
253: HTChunkPuts(chunk, codestr);
254: }
255: highest = NO;
256: } else
257: HTChunkPuts(chunk, "\nReason: ");
258:
259: /* Output error message */
260: if (pres->element != HTERR_SYSTEM) {
261: HTChunkPuts(chunk, error_info[pres->element].msg);
262: HTChunkPutc(chunk, ' ');
263: }
264:
265: /* Output parameters */
266: if (pres->par && HTErrorShowMask & HT_ERR_SHOW_PARS) {
267: unsigned int cnt;
268: char ch;
2.22 frystyk 269: HTChunkPutc(chunk, '(');
2.21 frystyk 270: for (cnt=0; cnt<pres->par_length; cnt++) {
271: ch = *((char *)(pres->par)+cnt);
272: if (ch < 0x20 || ch >= 0x7F)
273: HTChunkPutc(chunk, '#'); /* Can't print real content */
274: else
275: HTChunkPutc(chunk, ch);
276: }
2.22 frystyk 277: HTChunkPutc(chunk, ')');
2.21 frystyk 278: }
279:
280: /* Output location */
281: if (pres->where && HTErrorShowMask & HT_ERR_SHOW_LOCATION) {
282: HTChunkPuts(chunk, "This occured in ");
283: HTChunkPuts(chunk, pres->where);
284: HTChunkPutc(chunk, '\n');
285: }
286:
287: /* We don't want the message more than once */
288: HTErrorIgnore(request, pres->handle);
289:
290: /* If we only are going to show the higest entry */
291: if (HTErrorShowMask & HT_ERR_SHOW_FIRST)
292: break;
293: }
294: }
295: HTChunkPutc(chunk, '\n');
296: HTChunkTerminate(chunk);
2.26 frystyk 297: if (HTChunkSize(chunk) > 2)
2.27 frystyk 298: HTAlert(request, HTChunkData(chunk));
2.21 frystyk 299: HTChunkFree(chunk);
300: return;
301: }
Webmaster