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;
124 htmlerror(g->name, g->lineno, "<%s> not in form, ignored\n",
129 s=pl_getattr(g->attr, "name");
134 s=pl_getattr(g->attr, "value");
139 f->checked=pl_hasattr(g->attr, "checked");
140 s=pl_getattr(g->attr, "size");
145 s=pl_getattr(g->attr, "maxlength");
147 f->maxlength=0x3fffffff;
149 f->maxlength=atoi(s);
150 s=pl_getattr(g->attr, "type");
151 /* bug -- password treated as text */
152 if(s==0 || cistrcmp(s, "text")==0 ||
153 cistrcmp(s, "password")==0 || cistrcmp(s, "int")==0 ||
154 cistrcmp(s, "email")==0){
155 s=pl_getattr(g->attr, "name");
156 if(s!=0 && strcmp(s, "isindex")==0)
161 * If there's exactly one attribute, use its value as the name,
162 * regardless of the attribute name. This makes
163 * http://linus.att.com/ias/puborder.html work.
166 if(g->attr[0].name && g->attr[1].name==0)
167 f->name=strdup(g->attr[0].value);
169 f->name=strdup("no-name");
172 else if(cistrcmp(s, "checkbox")==0)
174 else if(cistrcmp(s, "radio")==0)
176 else if(cistrcmp(s, "submit")==0)
178 else if(cistrcmp(s, "image")==0){
179 /* presotto's egregious hack to make image submits do something */
185 } else if(cistrcmp(s, "reset")==0)
187 else if(cistrcmp(s, "hidden")==0)
191 if((f->type==CHECK || f->type==RADIO) && !pl_hasattr(g->attr, "value")){
193 f->value=strdup("on");
196 pl_htmloutput(g, g->nsp, f->value[0]?f->value:"blank field", f);
199 if(g->form==0) goto BadTag;
201 s=pl_getattr(g->attr, "name");
203 f->name=strdup("select");
204 htmlerror(g->name, g->lineno, "select has no name=\n");
208 s=pl_getattr(g->attr, "size");
212 if(f->size<=0) f->size=1;
214 f->multiple=pl_hasattr(g->attr, "multiple");
222 if(g->form==0) goto BadTag;
224 o=emallocz(sizeof(Option), 1);
225 for(op=&f->options;*op;op=&(*op)->next);
230 g->etext=o->label+NLABEL;
231 memset(o->label, 0, NLABEL+1);
233 o->def=pl_hasattr(g->attr, "selected");
235 s=pl_getattr(g->attr, "value");
242 if(g->form==0) goto BadTag;
244 s=pl_getattr(g->attr, "name");
246 f->name=strdup("enter text");
247 htmlerror(g->name, g->lineno, "select has no name=\n");
251 s=pl_getattr(g->attr, "rows");
253 s=pl_getattr(g->attr, "cols");
254 f->cols=s?atoi(s):30;
256 /* suck up initial text */
257 pl_htmloutput(g, g->nsp, f->name, f);
261 * Make up a form with one tag, of type INDEX
262 * I have seen a page with <ISINDEX PROMPT="Enter a title here ">,
263 * which is nonstandard and not handled here.
265 form=emalloc(sizeof(Form));
268 s=pl_getattr(g->attr, "action");
269 form->action=strdup((s && s[0]) ? s : g->dst->url->fullname);
276 f->maxlength=0x3fffffff;
278 pl_htmloutput(g, g->nsp, f->value[0]?f->value:"blank field", f);
283 * Called by rdhtml on seeing a forms-related end tag
285 void endform(Hglob *g){
292 htmlerror(g->name, g->lineno, "</select> not in form, ignored\n");
294 pl_htmloutput(g, g->nsp, g->form->efields->name,g->form->efields);
300 char *nullgen(Panel *, int ){
303 char *selgen(Panel *p, int index){
308 for(a=f->options;index!=0 && a!=0;--index,a=a->next);
310 a->label[0]=a->selected?'*':' ';
313 char *seloption(Field *f){
315 for(a=f->options;a!=0;a=a->next)
320 void mkfieldpanel(Rtext *t){
322 Panel *win, *scrl, *menu, *pop, *button;
325 if((a = t->user) == nil)
327 if((f = a->field) == nil)
333 f->p=plentry(0, 0, f->size*chrwidth, f->value, h_submittype);
336 f->p=plcheckbutton(0, 0, "", h_checkinput);
338 plsetbutton(f->p, f->checked);
341 f->p=plradiobutton(0, 0, "", h_radioinput);
343 plsetbutton(f->p, f->checked);
346 f->p=plbutton(0, 0, f->value[0]?f->value:"submit", h_submitinput);
349 f->p=plbutton(0, 0, f->value[0]?f->value:"reset", h_resetinput);
352 f->pulldown=plgroup(0,0);
353 scrl=plscrollbar(f->pulldown, PACKW|FILLY);
354 win=pllist(f->pulldown, PACKN, nullgen, f->size, h_select);
356 plinitlist(win, PACKN, selgen, f->size, h_select);
357 plscroll(win, 0, scrl);
358 plpack(f->pulldown, Rect(0,0,1024,1024));
359 f->p=plpulldown(0, FIXEDX, seloption(f), f->pulldown, PACKS);
360 f->p->fixedsize.x=f->pulldown->r.max.x-f->pulldown->r.min.x;
365 pllabel(f->p, PACKN|FILLX, f->name);
366 scrl=plscrollbar(f->p, PACKW|FILLY);
367 pop=plpopup(f->p, PACKN|FILLX, 0, menu, 0);
368 f->textwin=pledit(pop, EXPAND, Pt(f->cols*chrwidth, f->rows*font->height),
371 button=plbutton(menu, PACKN|FILLX, "cut", h_cut);
372 button->userp=f->textwin;
373 button=plbutton(menu, PACKN|FILLX, "paste", h_paste);
374 button->userp=f->textwin;
375 button=plbutton(menu, PACKN|FILLX, "snarf", h_snarf);
376 button->userp=f->textwin;
377 plscroll(f->textwin, 0, scrl);
380 f->p=plentry(0, 0, f->size*chrwidth, f->value, h_submitindex);
391 void h_checkinput(Panel *p, int, int v){
392 ((Field *)p->userp)->state=v;
394 void h_radioinput(Panel *p, int, int v){
399 for(f=me->form->fields;f;f=f->next)
400 if(f->type==RADIO && f!=me && strcmp(f->name, me->name)==0){
401 plsetbutton(f->p, 0);
403 pldraw(f->p, screen);
407 void h_select(Panel *p, int, int index){
412 if(!f->multiple) for(a=f->options;a;a=a->next) a->selected=0;
413 for(a=f->options;index!=0 && a!=0;--index,a=a->next);
415 a->selected=!a->selected;
416 plinitpulldown(f->p, FIXEDX, seloption(f), f->pulldown, PACKS);
417 pldraw(f->p, screen);
419 void h_resetinput(Panel *p, int){
422 for(f=((Field *)p->userp)->form->fields;f;f=f->next) switch(f->type){
425 plinitentry(f->p, 0, f->size*chrwidth, f->value, 0);
430 plsetbutton(f->p, f->checked);
433 for(o=f->options;o;o=o->next)
437 pldraw(text, screen);
439 void h_edit(Panel *p){
444 void h_snarf(Panel *p, int){
448 plegetsel(p, &s0, &s1);
451 if(snarfbuf) free(snarfbuf);
453 snarfbuf=malloc(nsnarfbuf*sizeof(Rune));
455 fprint(2, "No mem\n");
458 memmove(snarfbuf, text+s0, nsnarfbuf*sizeof(Rune));
460 void h_cut(Panel *p, int b){
462 plepaste(p->userp, 0, 0);
464 void h_paste(Panel *p, int){
465 plepaste(p->userp, snarfbuf, nsnarfbuf);
471 if(strchr("/$-_@.!*'(), ", *s)
472 || 'a'<=*s && *s<='z'
473 || 'A'<=*s && *s<='Z'
474 || '0'<=*s && *s<='9')
482 return 0<=v && v<=9?'0'+v:'A'+v-10;
484 char *ucpy(char *buf, char *s){
486 if(strchr("/$-_@.!*'(),", *s)
487 || 'a'<=*s && *s<='z'
488 || 'A'<=*s && *s<='Z'
489 || '0'<=*s && *s<='9')
495 *buf++=hexdigit((*s>>4)&15);
496 *buf++=hexdigit(*s&15);
502 char *runetou(char *buf, Rune r){
512 * If there's exactly one button with type=text, then
513 * a CR in the button is supposed to submit the form.
515 void h_submittype(Panel *p, char *){
519 for(f=((Field *)p->userp)->form->fields;f;f=f->next) if(f->type==TYPEIN) ntype++;
520 if(ntype==1) h_submitinput(p, 0);
522 void h_submitindex(Panel *p, char *){
525 void h_submitinput(Panel *p, int){
528 char *buf, *bufp, sep;
532 form=((Field *)p->userp)->form;
533 if(form->method==GET) size=ulen(form->action)+1;
535 for(f=form->fields;f;f=f->next) switch(f->type){
538 size+=ulen(f->name)+1+ulen(plentryval(f->p))+1;
541 size+=ulen(plentryval(f->p))+1;
547 size+=ulen(f->name)+1+ulen(f->value)+1;
550 for(o=f->options;o;o=o->next)
552 size+=ulen(f->name)+1+ulen(o->value)+1;
555 size+=ulen(f->name)+1+plelen(f->textwin)*3+1;
559 if(form->method==GET){
560 strcpy(buf, form->action);
567 bufp=buf+strlen(buf);
568 if(form->method==GET && bufp!=buf && bufp[-1]=='?') *--bufp='\0'; /* spurious ? */
569 for(f=form->fields;f;f=f->next) switch(f->type){
574 bufp=ucpy(bufp, f->name);
576 bufp=ucpy(bufp, plentryval(f->p));
581 bufp=ucpy(bufp, plentryval(f->p));
589 bufp=ucpy(bufp, f->name);
591 bufp=ucpy(bufp, f->value);
594 for(o=f->options;o;o=o->next)
598 bufp=ucpy(bufp, f->name);
600 bufp=ucpy(bufp, o->value);
606 bufp=ucpy(bufp, f->name);
608 rp=pleget(f->textwin);
609 for(nrune=plelen(f->textwin);nrune!=0;--nrune)
610 bufp=runetou(bufp, *rp++);
614 if(form->method==GET){
615 if(debug)fprint(2, "GET %s\n", buf);
616 geturl(buf, GET, 0, 0, 0);
619 if(debug)fprint(2, "POST %s: %s\n", form->action, buf);
620 geturl(form->action, POST, buf, 0, 0);
625 void freeform(void *p)
634 while(f = form->fields){
635 form->fields = f->next;
643 while(o = f->options){
644 f->options = o->next;
645 if(o->value != o->label+1)