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 g->form->action=emalloc(sizeof(Url));
102 s=pl_getattr(g->attr, "action");
104 *g->form->action=*g->dst->url;
106 crackurl(g->form->action, s, g->dst->base);
107 s=pl_getattr(g->attr, "method");
110 else if(cistrcmp(s, "post")==0)
111 g->form->method=POST;
113 if(cistrcmp(s, "get")!=0)
114 htmlerror(g->name, g->lineno,
115 "unknown form method %s\n", s);
120 g->form->next = g->dst->form;
121 g->dst->form = g->form;
127 htmlerror(g->name, g->lineno, "<%s> not in form, ignored\n",
132 s=pl_getattr(g->attr, "name");
137 s=pl_getattr(g->attr, "value");
142 f->checked=pl_hasattr(g->attr, "checked");
143 s=pl_getattr(g->attr, "size");
148 s=pl_getattr(g->attr, "maxlength");
150 f->maxlength=0x3fffffff;
152 f->maxlength=atoi(s);
153 s=pl_getattr(g->attr, "type");
154 /* bug -- password treated as text */
155 if(s==0 || cistrcmp(s, "text")==0 || cistrcmp(s, "password")==0 || cistrcmp(s, "int")==0){
156 s=pl_getattr(g->attr, "name");
157 if(s!=0 && strcmp(s, "isindex")==0)
162 * If there's exactly one attribute, use its value as the name,
163 * regardless of the attribute name. This makes
164 * http://linus.att.com/ias/puborder.html work.
167 if(g->attr[0].name && g->attr[1].name==0)
168 f->name=strdup(g->attr[0].value);
170 f->name=strdup("no-name");
173 else if(cistrcmp(s, "checkbox")==0)
175 else if(cistrcmp(s, "radio")==0)
177 else if(cistrcmp(s, "submit")==0)
179 else if(cistrcmp(s, "image")==0){
180 /* presotto's egregious hack to make image submits do something */
186 } else if(cistrcmp(s, "reset")==0)
188 else if(cistrcmp(s, "hidden")==0)
191 htmlerror(g->name, g->lineno, "bad field type %s, ignored", s);
194 if((f->type==CHECK || f->type==RADIO) && !pl_hasattr(g->attr, "value")){
196 f->value=strdup("on");
199 pl_htmloutput(g, g->nsp, f->value[0]?f->value:"blank field", f);
202 if(g->form==0) goto BadTag;
204 s=pl_getattr(g->attr, "name");
206 f->name=strdup("select");
207 htmlerror(g->name, g->lineno, "select has no name=\n");
211 s=pl_getattr(g->attr, "size");
215 if(f->size<=0) f->size=1;
217 f->multiple=pl_hasattr(g->attr, "multiple");
225 if(g->form==0) goto BadTag;
227 o=emallocz(sizeof(Option), 1);
228 for(op=&f->options;*op;op=&(*op)->next);
233 g->etext=o->label+NLABEL;
234 memset(o->label, 0, NLABEL+1);
236 o->def=pl_hasattr(g->attr, "selected");
238 s=pl_getattr(g->attr, "value");
245 if(g->form==0) goto BadTag;
247 s=pl_getattr(g->attr, "name");
249 f->name=strdup("enter text");
250 htmlerror(g->name, g->lineno, "select has no name=\n");
254 s=pl_getattr(g->attr, "rows");
256 s=pl_getattr(g->attr, "cols");
257 f->cols=s?atoi(s):30;
259 /* suck up initial text */
260 pl_htmloutput(g, g->nsp, f->name, f);
264 * Make up a form with one tag, of type INDEX
265 * I have seen a page with <ISINDEX PROMPT="Enter a title here ">,
266 * which is nonstandard and not handled here.
268 form=emalloc(sizeof(Form));
271 form->action=emalloc(sizeof(Url));
272 s=pl_getattr(g->attr, "action");
274 *form->action=*g->dst->url;
276 crackurl(form->action, s, g->dst->base);
283 f->maxlength=0x3fffffff;
285 pl_htmloutput(g, g->nsp, f->value[0]?f->value:"blank field", f);
290 * Called by rdhtml on seeing a forms-related end tag
292 void endform(Hglob *g){
299 htmlerror(g->name, g->lineno, "</select> not in form, ignored\n");
301 pl_htmloutput(g, g->nsp, g->form->efields->name,g->form->efields);
307 char *nullgen(Panel *, int ){
310 char *selgen(Panel *p, int index){
315 for(a=f->options;index!=0 && a!=0;--index,a=a->next);
317 a->label[0]=a->selected?'*':' ';
320 char *seloption(Field *f){
322 for(a=f->options;a!=0;a=a->next)
327 void mkfieldpanel(Rtext *t){
329 Panel *win, *scrl, *menu, *pop, *button;
332 if((a = t->user) == nil)
334 if((f = a->field) == nil)
340 f->p=plentry(0, 0, f->size*chrwidth, f->value, h_submittype);
343 f->p=plcheckbutton(0, 0, "", h_checkinput);
345 plsetbutton(f->p, f->checked);
348 f->p=plradiobutton(0, 0, "", h_radioinput);
350 plsetbutton(f->p, f->checked);
353 f->p=plbutton(0, 0, f->value[0]?f->value:"submit", h_submitinput);
356 f->p=plbutton(0, 0, f->value[0]?f->value:"reset", h_resetinput);
359 f->pulldown=plgroup(0,0);
360 scrl=plscrollbar(f->pulldown, PACKW|FILLY);
361 win=pllist(f->pulldown, PACKN, nullgen, f->size, h_select);
363 plinitlist(win, PACKN, selgen, f->size, h_select);
364 plscroll(win, 0, scrl);
365 plpack(f->pulldown, Rect(0,0,1024,1024));
366 f->p=plpulldown(0, FIXEDX, seloption(f), f->pulldown, PACKS);
367 f->p->fixedsize.x=f->pulldown->r.max.x-f->pulldown->r.min.x;
372 pllabel(f->p, PACKN|FILLX, f->name);
373 scrl=plscrollbar(f->p, PACKW|FILLY);
374 pop=plpopup(f->p, PACKN|FILLX, 0, menu, 0);
375 f->textwin=pledit(pop, EXPAND, Pt(f->cols*chrwidth, f->rows*font->height),
378 button=plbutton(menu, PACKN|FILLX, "cut", h_cut);
379 button->userp=f->textwin;
380 button=plbutton(menu, PACKN|FILLX, "paste", h_paste);
381 button->userp=f->textwin;
382 button=plbutton(menu, PACKN|FILLX, "snarf", h_snarf);
383 button->userp=f->textwin;
384 plscroll(f->textwin, 0, scrl);
387 f->p=plentry(0, 0, f->size*chrwidth, f->value, h_submitindex);
398 void h_checkinput(Panel *p, int, int v){
399 ((Field *)p->userp)->state=v;
401 void h_radioinput(Panel *p, int, int v){
406 for(f=me->form->fields;f;f=f->next)
407 if(f->type==RADIO && f!=me && strcmp(f->name, me->name)==0){
408 plsetbutton(f->p, 0);
410 pldraw(f->p, screen);
414 void h_select(Panel *p, int, int index){
419 if(!f->multiple) for(a=f->options;a;a=a->next) a->selected=0;
420 for(a=f->options;index!=0 && a!=0;--index,a=a->next);
422 a->selected=!a->selected;
423 plinitpulldown(f->p, FIXEDX, seloption(f), f->pulldown, PACKS);
424 pldraw(f->p, screen);
426 void h_resetinput(Panel *p, int){
429 for(f=((Field *)p->userp)->form->fields;f;f=f->next) switch(f->type){
432 plinitentry(f->p, 0, f->size*chrwidth, f->value, 0);
437 plsetbutton(f->p, f->checked);
440 for(o=f->options;o;o=o->next)
444 pldraw(text, screen);
446 void h_edit(Panel *p){
451 void h_snarf(Panel *p, int){
455 plegetsel(p, &s0, &s1);
458 if(snarfbuf) free(snarfbuf);
460 snarfbuf=malloc(nsnarfbuf*sizeof(Rune));
462 fprint(2, "No mem\n");
465 memmove(snarfbuf, text+s0, nsnarfbuf*sizeof(Rune));
467 void h_cut(Panel *p, int b){
469 plepaste(p->userp, 0, 0);
471 void h_paste(Panel *p, int){
472 plepaste(p->userp, snarfbuf, nsnarfbuf);
478 if(strchr("/$-_@.!*'(), ", *s)
479 || 'a'<=*s && *s<='z'
480 || 'A'<=*s && *s<='Z'
481 || '0'<=*s && *s<='9')
489 return 0<=v && v<=9?'0'+v:'A'+v-10;
491 char *ucpy(char *buf, char *s){
493 if(strchr("/$-_@.!*'(),", *s)
494 || 'a'<=*s && *s<='z'
495 || 'A'<=*s && *s<='Z'
496 || '0'<=*s && *s<='9')
502 *buf++=hexdigit((*s>>4)&15);
503 *buf++=hexdigit(*s&15);
509 char *runetou(char *buf, Rune r){
519 * If there's exactly one button with type=text, then
520 * a CR in the button is supposed to submit the form.
522 void h_submittype(Panel *p, char *){
526 for(f=((Field *)p->userp)->form->fields;f;f=f->next) if(f->type==TYPEIN) ntype++;
527 if(ntype==1) h_submitinput(p, 0);
529 void h_submitindex(Panel *p, char *){
532 void h_submitinput(Panel *p, int){
535 char *buf, *bufp, sep;
539 form=((Field *)p->userp)->form;
540 if(form->method==GET) size=ulen(form->action->fullname)+1;
542 for(f=form->fields;f;f=f->next) switch(f->type){
545 size+=ulen(f->name)+1+ulen(plentryval(f->p))+1;
548 size+=ulen(plentryval(f->p))+1;
554 size+=ulen(f->name)+1+ulen(f->value)+1;
557 for(o=f->options;o;o=o->next)
559 size+=ulen(f->name)+1+ulen(o->value)+1;
562 size+=ulen(f->name)+1+plelen(f->textwin)*3+1;
566 if(form->method==GET){
567 strcpy(buf, form->action->fullname);
574 bufp=buf+strlen(buf);
575 if(form->method==GET && bufp!=buf && bufp[-1]=='?') *--bufp='\0'; /* spurious ? */
576 for(f=form->fields;f;f=f->next) switch(f->type){
581 bufp=ucpy(bufp, f->name);
583 bufp=ucpy(bufp, plentryval(f->p));
588 bufp=ucpy(bufp, plentryval(f->p));
596 bufp=ucpy(bufp, f->name);
598 bufp=ucpy(bufp, f->value);
601 for(o=f->options;o;o=o->next)
605 bufp=ucpy(bufp, f->name);
607 bufp=ucpy(bufp, o->value);
613 bufp=ucpy(bufp, f->name);
615 rp=pleget(f->textwin);
616 for(nrune=plelen(f->textwin);nrune!=0;--nrune)
617 bufp=runetou(bufp, *rp++);
621 if(form->method==GET){
622 fprint(2, "GET %s\n", buf);
623 geturl(buf, GET, 0, 0, 0);
626 fprint(2, "POST %s: %s\n", form->action->fullname, buf);
627 geturl(form->action->fullname, POST, buf, 0, 0);
632 void freeform(void *p)
641 while(f = form->fields){
642 form->fields = f->next;
650 while(o = f->options){
651 f->options = o->next;
652 if(o->value != o->label+1)