2 * type=image is treated like submit
11 typedef struct Field Field;
12 typedef struct Option Option;
16 Field *fields, *efields;
25 int size; /* should be a point, but that feature is deprecated */
31 int state; /* is the button marked? */
58 void h_checkinput(Panel *, int, int);
59 void h_radioinput(Panel *, int, int);
60 void h_submitinput(Panel *, int);
61 void h_submittype(Panel *, char *);
62 void h_submitindex(Panel *, char *);
63 void h_resetinput(Panel *, int);
64 void h_select(Panel *, int, int);
65 void h_cut(Panel *, int);
66 void h_paste(Panel *, int);
67 void h_snarf(Panel *, int);
69 char *selgen(Panel *, int);
70 char *nullgen(Panel *, int);
71 Field *newfield(Form *form){
73 f=emallocz(sizeof(Field), 1);
77 form->efields->next=f;
84 * Called by rdhtml on seeing a forms-related tag
86 void rdform(Hglob *g){
93 fprint(2, "Bad tag <%s> in rdform (Can't happen!)\n", g->token);
97 htmlerror(g->name, g->lineno, "nested forms illegal\n");
100 g->form=emallocz(sizeof(Form), 1);
101 s=pl_getattr(g->attr, "action");
102 g->form->action=strdup((s && s[0]) ? s : g->dst->url->fullname);
103 s=pl_getattr(g->attr, "method");
106 else if(cistrcmp(s, "post")==0)
107 g->form->method=POST;
109 if(cistrcmp(s, "get")!=0)
110 htmlerror(g->name, g->lineno,
111 "unknown form method %s\n", s);
116 g->form->next = g->dst->form;
117 g->dst->form = g->form;
123 htmlerror(g->name, g->lineno, "<%s> not in form, ignored\n",
128 s=pl_getattr(g->attr, "name");
133 s=pl_getattr(g->attr, "value");
138 f->checked=pl_hasattr(g->attr, "checked");
139 s=pl_getattr(g->attr, "size");
144 s=pl_getattr(g->attr, "maxlength");
146 f->maxlength=0x3fffffff;
148 f->maxlength=atoi(s);
149 s=pl_getattr(g->attr, "type");
150 /* bug -- password treated as text */
151 if(s==0 || cistrcmp(s, "text")==0 || cistrcmp(s, "password")==0 || cistrcmp(s, "int")==0){
152 s=pl_getattr(g->attr, "name");
153 if(s!=0 && strcmp(s, "isindex")==0)
158 * If there's exactly one attribute, use its value as the name,
159 * regardless of the attribute name. This makes
160 * http://linus.att.com/ias/puborder.html work.
163 if(g->attr[0].name && g->attr[1].name==0)
164 f->name=strdup(g->attr[0].value);
166 f->name=strdup("no-name");
169 else if(cistrcmp(s, "checkbox")==0)
171 else if(cistrcmp(s, "radio")==0)
173 else if(cistrcmp(s, "submit")==0)
175 else if(cistrcmp(s, "image")==0){
176 /* presotto's egregious hack to make image submits do something */
182 } else if(cistrcmp(s, "reset")==0)
184 else if(cistrcmp(s, "hidden")==0)
187 htmlerror(g->name, g->lineno, "bad field type %s, ignored", s);
190 if((f->type==CHECK || f->type==RADIO) && !pl_hasattr(g->attr, "value")){
192 f->value=strdup("on");
195 pl_htmloutput(g, g->nsp, f->value[0]?f->value:"blank field", f);
198 if(g->form==0) goto BadTag;
200 s=pl_getattr(g->attr, "name");
202 f->name=strdup("select");
203 htmlerror(g->name, g->lineno, "select has no name=\n");
207 s=pl_getattr(g->attr, "size");
211 if(f->size<=0) f->size=1;
213 f->multiple=pl_hasattr(g->attr, "multiple");
221 if(g->form==0) goto BadTag;
223 o=emallocz(sizeof(Option), 1);
224 for(op=&f->options;*op;op=&(*op)->next);
229 g->etext=o->label+NLABEL;
230 memset(o->label, 0, NLABEL+1);
232 o->def=pl_hasattr(g->attr, "selected");
234 s=pl_getattr(g->attr, "value");
241 if(g->form==0) goto BadTag;
243 s=pl_getattr(g->attr, "name");
245 f->name=strdup("enter text");
246 htmlerror(g->name, g->lineno, "select has no name=\n");
250 s=pl_getattr(g->attr, "rows");
252 s=pl_getattr(g->attr, "cols");
253 f->cols=s?atoi(s):30;
255 /* suck up initial text */
256 pl_htmloutput(g, g->nsp, f->name, f);
260 * Make up a form with one tag, of type INDEX
261 * I have seen a page with <ISINDEX PROMPT="Enter a title here ">,
262 * which is nonstandard and not handled here.
264 form=emalloc(sizeof(Form));
267 s=pl_getattr(g->attr, "action");
268 form->action=strdup((s && s[0]) ? s : g->dst->url->fullname);
275 f->maxlength=0x3fffffff;
277 pl_htmloutput(g, g->nsp, f->value[0]?f->value:"blank field", f);
282 * Called by rdhtml on seeing a forms-related end tag
284 void endform(Hglob *g){
291 htmlerror(g->name, g->lineno, "</select> not in form, ignored\n");
293 pl_htmloutput(g, g->nsp, g->form->efields->name,g->form->efields);
299 char *nullgen(Panel *, int ){
302 char *selgen(Panel *p, int index){
307 for(a=f->options;index!=0 && a!=0;--index,a=a->next);
309 a->label[0]=a->selected?'*':' ';
312 char *seloption(Field *f){
314 for(a=f->options;a!=0;a=a->next)
319 void mkfieldpanel(Rtext *t){
321 Panel *win, *scrl, *menu, *pop, *button;
324 if((a = t->user) == nil)
326 if((f = a->field) == nil)
332 f->p=plentry(0, 0, f->size*chrwidth, f->value, h_submittype);
335 f->p=plcheckbutton(0, 0, "", h_checkinput);
337 plsetbutton(f->p, f->checked);
340 f->p=plradiobutton(0, 0, "", h_radioinput);
342 plsetbutton(f->p, f->checked);
345 f->p=plbutton(0, 0, f->value[0]?f->value:"submit", h_submitinput);
348 f->p=plbutton(0, 0, f->value[0]?f->value:"reset", h_resetinput);
351 f->pulldown=plgroup(0,0);
352 scrl=plscrollbar(f->pulldown, PACKW|FILLY);
353 win=pllist(f->pulldown, PACKN, nullgen, f->size, h_select);
355 plinitlist(win, PACKN, selgen, f->size, h_select);
356 plscroll(win, 0, scrl);
357 plpack(f->pulldown, Rect(0,0,1024,1024));
358 f->p=plpulldown(0, FIXEDX, seloption(f), f->pulldown, PACKS);
359 f->p->fixedsize.x=f->pulldown->r.max.x-f->pulldown->r.min.x;
364 pllabel(f->p, PACKN|FILLX, f->name);
365 scrl=plscrollbar(f->p, PACKW|FILLY);
366 pop=plpopup(f->p, PACKN|FILLX, 0, menu, 0);
367 f->textwin=pledit(pop, EXPAND, Pt(f->cols*chrwidth, f->rows*font->height),
370 button=plbutton(menu, PACKN|FILLX, "cut", h_cut);
371 button->userp=f->textwin;
372 button=plbutton(menu, PACKN|FILLX, "paste", h_paste);
373 button->userp=f->textwin;
374 button=plbutton(menu, PACKN|FILLX, "snarf", h_snarf);
375 button->userp=f->textwin;
376 plscroll(f->textwin, 0, scrl);
379 f->p=plentry(0, 0, f->size*chrwidth, f->value, h_submitindex);
390 void h_checkinput(Panel *p, int, int v){
391 ((Field *)p->userp)->state=v;
393 void h_radioinput(Panel *p, int, int v){
398 for(f=me->form->fields;f;f=f->next)
399 if(f->type==RADIO && f!=me && strcmp(f->name, me->name)==0){
400 plsetbutton(f->p, 0);
402 pldraw(f->p, screen);
406 void h_select(Panel *p, int, int index){
411 if(!f->multiple) for(a=f->options;a;a=a->next) a->selected=0;
412 for(a=f->options;index!=0 && a!=0;--index,a=a->next);
414 a->selected=!a->selected;
415 plinitpulldown(f->p, FIXEDX, seloption(f), f->pulldown, PACKS);
416 pldraw(f->p, screen);
418 void h_resetinput(Panel *p, int){
421 for(f=((Field *)p->userp)->form->fields;f;f=f->next) switch(f->type){
424 plinitentry(f->p, 0, f->size*chrwidth, f->value, 0);
429 plsetbutton(f->p, f->checked);
432 for(o=f->options;o;o=o->next)
436 pldraw(text, screen);
438 void h_edit(Panel *p){
443 void h_snarf(Panel *p, int){
447 plegetsel(p, &s0, &s1);
450 if(snarfbuf) free(snarfbuf);
452 snarfbuf=malloc(nsnarfbuf*sizeof(Rune));
454 fprint(2, "No mem\n");
457 memmove(snarfbuf, text+s0, nsnarfbuf*sizeof(Rune));
459 void h_cut(Panel *p, int b){
461 plepaste(p->userp, 0, 0);
463 void h_paste(Panel *p, int){
464 plepaste(p->userp, snarfbuf, nsnarfbuf);
470 if(strchr("/$-_@.!*'(), ", *s)
471 || 'a'<=*s && *s<='z'
472 || 'A'<=*s && *s<='Z'
473 || '0'<=*s && *s<='9')
481 return 0<=v && v<=9?'0'+v:'A'+v-10;
483 char *ucpy(char *buf, char *s){
485 if(strchr("/$-_@.!*'(),", *s)
486 || 'a'<=*s && *s<='z'
487 || 'A'<=*s && *s<='Z'
488 || '0'<=*s && *s<='9')
494 *buf++=hexdigit((*s>>4)&15);
495 *buf++=hexdigit(*s&15);
501 char *runetou(char *buf, Rune r){
511 * If there's exactly one button with type=text, then
512 * a CR in the button is supposed to submit the form.
514 void h_submittype(Panel *p, char *){
518 for(f=((Field *)p->userp)->form->fields;f;f=f->next) if(f->type==TYPEIN) ntype++;
519 if(ntype==1) h_submitinput(p, 0);
521 void h_submitindex(Panel *p, char *){
524 void h_submitinput(Panel *p, int){
527 char *buf, *bufp, sep;
531 form=((Field *)p->userp)->form;
532 if(form->method==GET) size=ulen(form->action)+1;
534 for(f=form->fields;f;f=f->next) switch(f->type){
537 size+=ulen(f->name)+1+ulen(plentryval(f->p))+1;
540 size+=ulen(plentryval(f->p))+1;
546 size+=ulen(f->name)+1+ulen(f->value)+1;
549 for(o=f->options;o;o=o->next)
551 size+=ulen(f->name)+1+ulen(o->value)+1;
554 size+=ulen(f->name)+1+plelen(f->textwin)*3+1;
558 if(form->method==GET){
559 strcpy(buf, form->action);
566 bufp=buf+strlen(buf);
567 if(form->method==GET && bufp!=buf && bufp[-1]=='?') *--bufp='\0'; /* spurious ? */
568 for(f=form->fields;f;f=f->next) switch(f->type){
573 bufp=ucpy(bufp, f->name);
575 bufp=ucpy(bufp, plentryval(f->p));
580 bufp=ucpy(bufp, plentryval(f->p));
588 bufp=ucpy(bufp, f->name);
590 bufp=ucpy(bufp, f->value);
593 for(o=f->options;o;o=o->next)
597 bufp=ucpy(bufp, f->name);
599 bufp=ucpy(bufp, o->value);
605 bufp=ucpy(bufp, f->name);
607 rp=pleget(f->textwin);
608 for(nrune=plelen(f->textwin);nrune!=0;--nrune)
609 bufp=runetou(bufp, *rp++);
613 if(form->method==GET){
614 if(debug)fprint(2, "GET %s\n", buf);
615 geturl(buf, GET, 0, 0, 0);
618 if(debug)fprint(2, "POST %s: %s\n", form->action, buf);
619 geturl(form->action, POST, buf, 0, 0);
624 void freeform(void *p)
633 while(f = form->fields){
634 form->fields = f->next;
642 while(o = f->options){
643 f->options = o->next;
644 if(o->value != o->label+1)