Added close gadget and increased hight of default con window.
[AROS.git] / tools / genmodule / config.c
blob883038d3556462e81b5c73c1436ea06b9e4f2dc8
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 Code to parse the command line options and the module config file for
6 the genmodule program
7 */
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #define __USE_XOPEN
13 #include <time.h>
14 #include <unistd.h>
15 #include <limits.h>
17 #include "functionhead.h"
18 #include "config.h"
20 const static char bannertemplate[] =
21 "/*\n"
22 " *** Automatically generated from '%s'. Edits will be lost. ***\n"
23 " Copyright © 1995-%4u, The AROS Development Team. All rights reserved.\n"
24 "*/\n";
26 char*
27 getBanner(struct config* config)
29 static unsigned currentyear = 0;
31 int bannerlength = strlen(config->conffile) + strlen(bannertemplate);
32 // No additional bytes needed because
33 // + 1 (NUL) + 4 (4 digit year) - strlen("%s") - strlen("%4u) = 0
35 char * banner = malloc(bannerlength);
37 if (currentyear == 0)
39 time_t rawtime;
40 time(&rawtime);
41 struct tm *utctm = gmtime(&rawtime);
42 currentyear = utctm->tm_year + 1900;
45 snprintf (banner, bannerlength, bannertemplate, config->conffile, currentyear);
47 return(banner);
50 void
51 freeBanner(char *banner)
53 free((void *)banner);
56 const static char usage[] =
57 "\n"
58 "Usage: genmodule [-c conffile] [-s suffix] [-d gendir] [-v versionextra]\n"
59 " {writefiles|writemakefile|writeincludes|writelibdefs|writefunclist|writefd|writeskel} modname modtype\n"
62 static void readconfig(struct config *);
63 static struct classinfo *newclass(struct config *);
64 static struct handlerinfo *newhandler(struct config *);
65 static struct interfaceinfo *newinterface(struct config *);
67 /* the method prefices for the supported classes */
68 static const char *muimprefix[] =
70 "__OM_",
71 "__MUIM_",
72 NULL
74 static const char *gadgetmprefix[] =
76 "__OM_",
77 "__GM_",
78 "__AROSM_",
79 NULL
81 static const char *dtmprefix[] =
83 "__OM_",
84 "__GM_",
85 "__DTM_",
86 "__PDTM_",
87 NULL
90 /* Create a config struct. Initialize with the values from the programs command
91 * line arguments and the contents of the modules .conf file
93 struct config *initconfig(int argc, char **argv)
95 struct config *cfg;
96 char *s, **argvit = argv + 1;
97 int hassuffix = 0, c;
99 cfg = malloc(sizeof(struct config));
100 if (cfg == NULL)
102 fprintf(stderr, "Out of memory\n");
103 exit(20);
106 memset(cfg, 0, sizeof(struct config));
108 while ((c = getopt(argc, argv, ":c:s:d:v:")) != -1)
110 if (c == ':')
112 fprintf(stderr, "Option -%c needs an argument\n",optopt);
113 exit(20);
116 switch (c)
118 case 'c':
119 cfg->conffile = optarg;
120 break;
122 case 's':
123 cfg->suffix = optarg;
124 hassuffix = 1;
125 break;
127 case 'd':
128 /* Remove / at end if present */
129 if ((optarg)[strlen(*argvit)-1]=='/') (optarg)[strlen(optarg)-1]='\0';
130 cfg->gendir = optarg;
131 break;
133 case 'v':
134 cfg->versionextra = optarg;
135 break;
137 default:
138 fprintf(stderr, "Internal error: Unhandled option\n");
139 exit(20);
143 if (optind + 3 != argc)
145 fprintf(stderr, "Wrong number of arguments.\n%s", usage);
146 exit(20);
149 if (strcmp(argv[optind], "writefiles") == 0)
151 cfg->command = FILES;
153 else if (strcmp(argv[optind], "writemakefile") == 0)
155 cfg->command = MAKEFILE;
157 else if (strcmp(argv[optind], "writeincludes") == 0)
159 cfg->command = INCLUDES;
161 else if (strcmp(argv[optind], "writelibdefs") == 0)
163 cfg->command = LIBDEFS;
165 else if (strcmp(argv[optind], "writefunclist") == 0)
167 cfg->command = WRITEFUNCLIST;
169 else if (strcmp(argv[optind], "writefd") == 0)
171 cfg->command = WRITEFD;
173 else if (strcmp(argv[optind], "writeskel") == 0)
175 cfg->command = WRITESKEL;
177 else
179 fprintf(stderr, "Unrecognized argument \"%s\"\n%s", argv[optind], usage);
180 exit(20);
183 cfg->modulename = argv[optind+1];
184 cfg->modulenameupper = strdup(cfg->modulename);
185 for (s=cfg->modulenameupper; *s!='\0'; *s = toupper(*s), s++) {
186 if (!isalnum(*s)) *s = '_';
189 if (strcmp(argv[optind+2],"library")==0)
191 cfg->modtype = LIBRARY;
192 cfg->moddir = "Libs";
194 else if (strcmp(argv[optind+2],"mcc")==0)
196 cfg->modtype = MCC;
197 cfg->moddir = "Classes/Zune";
199 else if (strcmp(argv[optind+2],"mui")==0)
201 cfg->modtype = MUI;
202 cfg->moddir = "Classes/Zune";
204 else if (strcmp(argv[optind+2],"mcp")==0)
206 cfg->modtype = MCP;
207 cfg->moddir = "Classes/Zune";
209 else if (strcmp(argv[optind+2], "device")==0)
211 cfg->modtype = DEVICE;
212 cfg->moddir = "Devs";
214 else if (strcmp(argv[optind+2], "resource")==0)
216 cfg->modtype = RESOURCE;
217 cfg->moddir = "Devs";
219 else if (strcmp(argv[optind+2], "gadget")==0)
221 cfg->modtype = GADGET;
222 cfg->moddir = "Classes/Gadgets";
224 else if (strcmp(argv[optind+2], "datatype")==0)
226 cfg->modtype = DATATYPE;
227 cfg->moddir = "Classes/DataTypes";
229 else if (strcmp(argv[optind+2], "usbclass")==0)
231 cfg->modtype = USBCLASS;
232 cfg->moddir = "Classes/USB";
233 if(!hassuffix)
235 cfg->suffix = "class";
236 hassuffix = 1;
239 else if (strcmp(argv[optind+2], "hidd")==0)
241 cfg->modtype = HIDD;
242 cfg->moddir = "Devs/Drivers";
244 else if (strcmp(argv[optind+2], "handler")==0)
246 cfg->modtype = HANDLER;
247 cfg->moddir = "$(AROS_DIR_FS)";
249 else if (strcmp(argv[optind+2], "hook")==0)
251 cfg->modtype = HANDLER;
252 cfg->moddir = "Devs";
254 else
256 fprintf(stderr, "Unknown modtype \"%s\" specified for second argument\n", argv[optind+2]);
257 exit(20);
259 cfg->modtypestr = argv[optind+2];
261 if (!hassuffix)
262 cfg->suffix = argv[optind+2];
264 /* Fill fields with default value if not specified on the command line */
266 char tmpbuf[256];
268 if (cfg->conffile == NULL)
270 snprintf(tmpbuf, sizeof(tmpbuf), "%s.conf", cfg->modulename);
271 cfg->conffile = strdup(tmpbuf);
274 if (cfg->gendir == NULL)
275 cfg->gendir = ".";
278 readconfig(cfg);
280 /* For a device add the functions given in beginiofunc and abortiofunc to the functionlist
281 * if they are provided
283 if (cfg->beginiofunc != NULL)
285 struct functionhead *funchead;
287 cfg->intcfg |= CFG_NOREADFUNCS;
289 /* Add beginio_func to the list of functions */
290 funchead = newfunctionhead(cfg->beginiofunc, REGISTERMACRO);
291 funchead->type = strdup("void");
292 funchead->lvo = 5;
293 funcaddarg(funchead, "struct IORequest *ioreq", "A1");
295 funchead->next = cfg->funclist;
296 cfg->funclist = funchead;
298 /* Add abortio_func to the list of functions */
299 funchead = newfunctionhead(cfg->abortiofunc, REGISTERMACRO);
300 funchead->type = strdup("LONG");
301 funchead->lvo = 6;
302 funcaddarg(funchead, "struct IORequest *ioreq", "A1");
304 funchead->next = cfg->funclist->next;
305 cfg->funclist->next = funchead;
307 else if (cfg->modtype == DEVICE && cfg->intcfg & CFG_NOREADFUNCS)
309 fprintf
311 stderr,
312 "beginio_func and abortio_func missing for a device with a non empty function list\n"
314 exit(20);
317 /* See if we have any stackcall options */
318 if (cfg->funclist) {
319 struct functionhead *funchead;
321 for (funchead = cfg->funclist; funchead; funchead = funchead->next) {
322 if (funchead->libcall == STACK) {
323 cfg->options |= OPTION_STACKCALL;
324 break;
329 /* Provide default version for functions that didnt have it */
330 if (cfg->funclist) {
331 struct functionhead *funchead;
332 int defversion = cfg->majorversion; /* Assume library version is default */
334 for (funchead = cfg->funclist; funchead; funchead = funchead->next) {
335 if (funchead->version > -1) {
336 /* There was at least one .version tag. Assume 0 is default */
337 defversion = 0;
338 break;
342 for (funchead = cfg->funclist; funchead; funchead = funchead->next) {
343 if (funchead->version == -1) {
344 funchead->version = defversion;
349 /* Verify that a handler has a handler */
350 if (cfg->modtype == HANDLER) {
351 if (cfg->handlerfunc == NULL) {
352 fprintf(stderr, "handler modules require a 'handler_func' ##config option\n");
353 exit(20);
355 cfg->options |= OPTION_NOAUTOLIB | OPTION_NOEXPUNGE | OPTION_NOOPENCLOSE;
358 return cfg;
361 /* Functions to read configuration from the configuration file */
363 #include "fileread.h"
365 static char *readsections(struct config *, struct classinfo *cl, struct interfaceinfo *in, int inclass);
366 static void readsectionconfig(struct config *, struct classinfo *cl, struct interfaceinfo *in, int inclass);
367 static void readsectioncdef(struct config *);
368 static void readsectioncdefprivate(struct config *);
369 static void readsectionstartup(struct config *);
370 static void readsectionfunctionlist(const char *type, struct functionhead **funclistptr, unsigned int firstlvo, int isattribute, enum libcall def_libcall);
371 static void readsectionclass_methodlist(struct classinfo *);
372 static void readsectionclass(struct config *);
373 static void readsectionhandler(struct config *);
374 static void readsectioninterface(struct config *);
376 static void readconfig(struct config *cfg)
378 struct classinfo *mainclass = NULL;
380 /* Create a classinfo structure if this module is a class */
381 switch (cfg->modtype)
383 case LIBRARY:
384 case DEVICE:
385 case RESOURCE:
386 case USBCLASS:
387 case HANDLER:
388 break;
390 case MCC:
391 case MUI:
392 case MCP:
393 case GADGET:
394 case DATATYPE:
395 case HIDD:
396 mainclass = newclass(cfg);
397 mainclass->classtype = cfg->modtype;
398 break;
400 default:
401 fprintf(stderr, "Internal error: unsupported modtype for classinfo creation\n");
402 exit(20);
405 switch (cfg->modtype)
407 case LIBRARY:
408 case USBCLASS:
409 cfg->firstlvo = 5;
410 break;
411 case DEVICE:
412 cfg->firstlvo = 7;
413 break;
414 case MCC:
415 case MUI:
416 case MCP:
417 cfg->firstlvo = 6;
418 mainclass->boopsimprefix = muimprefix;
419 break;
420 case HANDLER:
421 case RESOURCE:
422 cfg->firstlvo = 1;
423 break;
424 case GADGET:
425 cfg->firstlvo = 5;
426 mainclass->boopsimprefix = gadgetmprefix;
427 break;
428 case DATATYPE:
429 cfg->firstlvo = 6;
430 mainclass->boopsimprefix = dtmprefix;
431 break;
432 case HIDD:
433 cfg->firstlvo = 5;
434 /* FIXME: need boopsimprefix ? */
435 break;
436 default:
437 fprintf(stderr, "Internal error: unsupported modtype for firstlvo\n");
438 exit(20);
441 if (!fileopen(cfg->conffile))
443 fprintf(stderr, "In readconfig: Could not open %s\n", cfg->conffile);
444 exit(20);
447 /* Read all sections and see that we are at the end of the file */
448 if (readsections(cfg, mainclass, NULL, 0) != NULL)
449 exitfileerror(20, "Syntax error");
451 fileclose();
454 /* readsections will scan through all the sections in the config file.
455 * arguments:
456 * struct config *cfg: The module config data which may be updated by
457 * the information in the sections
458 * struct classinfo *cl: The classdata to be filled with data from the sections.
459 * This may be NULL if this is the main part of the configuration file and the
460 * type of the module is not a class
461 * int inclass: Boolean to indicate if we are in a class part. If not we are in the main
462 * part of the config file.
464 static char *readsections(struct config *cfg, struct classinfo *cl, struct interfaceinfo *in, int inclass)
466 char *line, *s, *s2;
467 int hasconfig = 0;
469 while ((line=readline())!=NULL)
471 if (strncmp(line, "##", 2)==0)
473 static char *parts[] =
475 "config", "cdefprivate", "cdef", "startup", "functionlist", "methodlist", "class", "handler", "interface", "attributelist", "cfunctionlist"
477 const unsigned int nums = sizeof(parts)/sizeof(char *);
478 unsigned int partnum;
479 int i, atend = 0;
481 s = line+2;
482 while (isspace(*s)) s++;
484 if (strncmp(s, "begin", 5)!=0)
485 return line;
487 s += 5;
488 if (!isspace(*s))
489 exitfileerror(20, "space after begin expected\n");
490 while (isspace(*s)) s++;
492 for (i = 0, partnum = 0; partnum==0 && i<nums; i++)
494 if (strncmp(s, parts[i], strlen(parts[i]))==0)
496 partnum = i+1;
497 s += strlen(parts[i]);
498 while (isspace(*s)) s++;
499 if (*s!='\0')
500 exitfileerror(20, "unexpected character on position %d\n", s-line);
503 if (partnum==0)
504 exitfileerror(20, "unknown start of section\n");
505 switch (partnum)
507 case 1: /* config */
508 readsectionconfig(cfg, cl, in, inclass);
509 hasconfig = 1;
510 break;
512 case 2: /* cdefprivate */
513 if (inclass)
514 exitfileerror(20, "cdefprivate section not allowed in class section\n");
515 readsectioncdefprivate(cfg);
516 break;
518 case 3: /* cdef */
519 if (inclass)
520 exitfileerror(20, "cdef section not allowed in class section\n");
521 readsectioncdef(cfg);
522 break;
524 case 4: /* startup */
525 if (inclass)
526 exitfileerror(20, "startup section not allowed in class section\n");
527 readsectionstartup(cfg);
528 break;
530 case 5: /* functionlist */
531 if (inclass)
532 exitfileerror(20, "functionlist section not allow in class section\n");
533 if (cfg->basename==NULL)
534 exitfileerror(20, "section functionlist has to come after section config\n");
536 readsectionfunctionlist("functionlist", &cfg->funclist, cfg->firstlvo, 0, REGISTERMACRO);
537 cfg->intcfg |= CFG_NOREADFUNCS;
538 break;
540 case 6: /* methodlist */
541 if (cl == NULL && in == NULL)
542 exitfileerror(20, "methodlist section when not in a class or interface\n");
543 if (cl)
544 readsectionclass_methodlist(cl);
545 else
546 readsectionfunctionlist("methodlist", &in->methodlist, 0, 0, REGISTERMACRO);
547 cfg->intcfg |= CFG_NOREADFUNCS;
548 break;
550 case 7: /* class */
551 if (inclass)
552 exitfileerror(20, "class section may not be in nested\n");
553 readsectionclass(cfg);
554 break;
555 case 8: /* handler */
556 readsectionhandler(cfg);
557 break;
558 case 9: /* interface */
559 if (inclass)
560 exitfileerror(20, "interface section may not be nested\n");
561 readsectioninterface(cfg);
562 break;
563 case 10: /* attributelist */
564 if (!in)
565 exitfileerror(20, "attributelist only valid in interface sections\n");
566 readsectionfunctionlist("attributelist", &in->attributelist, 0, 1, INVALID);
567 break;
568 case 11: /* cfunctionlist */
569 if (inclass)
570 exitfileerror(20, "cfunctionlist section not allow in class section\n");
571 if (cfg->basename==NULL)
572 exitfileerror(20, "section cfunctionlist has to come after section config\n");
574 readsectionfunctionlist("cfunctionlist", &cfg->funclist, cfg->firstlvo, 0, REGISTER);
575 cfg->intcfg |= CFG_NOREADFUNCS;
576 break;
579 else if (strlen(line)!=0)
580 filewarning("warning line outside section ignored\n");
583 if(!inclass)
585 if (!hasconfig)
586 exitfileerror(20, "No config section in conffile\n");
588 /* If no indication was given for generating includes or not
589 decide on module type and if there are functions
591 if(!((cfg->options & OPTION_INCLUDES) || (cfg->options & OPTION_NOINCLUDES)))
593 switch (cfg->modtype)
595 case LIBRARY:
596 case RESOURCE:
597 cfg->options |= OPTION_INCLUDES;
598 break;
600 case HANDLER:
601 case MCC:
602 case MUI:
603 case MCP:
604 case USBCLASS:
605 cfg->options |= OPTION_NOINCLUDES;
606 break;
608 case DEVICE:
609 cfg->options |= (
610 (cfg->funclist != NULL)
611 || (cfg->cdeflines != NULL)
612 || strcmp(cfg->libbasetypeptrextern, "struct Device *") != 0
613 ) ? OPTION_INCLUDES : OPTION_NOINCLUDES;
614 break;
616 case GADGET:
617 case DATATYPE:
618 case HIDD:
619 cfg->options |= (
620 (cfg->funclist != NULL)
621 ) ? OPTION_INCLUDES : OPTION_NOINCLUDES;
622 break;
624 default:
625 fprintf(stderr, "Internal error writemakefile: unhandled modtype for includes\n");
626 exit(20);
627 break;
631 /* If no indication was given for not generating stubs only generate them if
632 * the module has functions
634 if(!((cfg->options & OPTION_STUBS) || (cfg->options & OPTION_NOSTUBS)))
636 switch (cfg->modtype)
638 case LIBRARY:
639 cfg->options |= (cfg->funclist != NULL) ? OPTION_STUBS : OPTION_NOSTUBS;
640 break;
642 case USBCLASS:
643 case RESOURCE:
644 case GADGET:
645 case DEVICE:
646 case DATATYPE:
647 case MCC:
648 case MUI:
649 case MCP:
650 case HIDD:
651 case HANDLER:
652 cfg->options |= OPTION_NOSTUBS;
653 break;
655 default:
656 fprintf(stderr, "Internal error writemakefile: unhandled modtype for stubs\n");
657 exit(20);
658 break;
662 /* If no indication was given for generating autoinit code or not
663 decide on module type
665 if(!((cfg->options & OPTION_AUTOINIT) || (cfg->options & OPTION_NOAUTOINIT)))
667 switch (cfg->modtype)
669 case LIBRARY:
670 cfg->options |= OPTION_AUTOINIT;
671 break;
673 case USBCLASS:
674 case RESOURCE:
675 case GADGET:
676 case DEVICE:
677 case DATATYPE:
678 case MCC:
679 case MUI:
680 case MCP:
681 case HIDD:
682 case HANDLER:
683 cfg->options |= OPTION_NOAUTOINIT;
684 break;
686 default:
687 fprintf(stderr, "Internal error writemakefile: unhandled modtype for autoinit\n");
688 exit(20);
689 break;
693 if ((cfg->modtype == RESOURCE) || (cfg->modtype == HANDLER))
694 /* Enforce noopenclose for resources and handlers */
695 cfg->options |= OPTION_NOOPENCLOSE;
696 else if (!(cfg->options & OPTION_SELFINIT))
697 /* Enforce using RTF_AUTOINIT for everything except resources */
698 cfg->options |= OPTION_RESAUTOINIT;
701 return NULL;
704 static void readsectionconfig(struct config *cfg, struct classinfo *cl, struct interfaceinfo *in, int inclass)
706 int atend = 0, i;
707 char *line, *s, *s2, *libbasetypeextern = NULL;
708 struct tm date;
710 while (!atend)
712 line = readline();
713 if (line==NULL)
714 exitfileerror(20, "unexpected end of file in section config\n");
716 if (strncmp(line, "##", 2)!=0)
718 const char *names[] =
720 "basename", "libbase", "libbasetype", "libbasetypeextern",
721 "version", "date", "copyright", "libcall", "forcebase", "superclass",
722 "superclass_field", "residentpri", "options", "sysbase_field",
723 "seglist_field", "rootbase_field", "classptr_field", "classptr_var",
724 "classid", "classdatatype", "beginio_func", "abortio_func", "dispatcher",
725 "initpri", "type", "addromtag", "oopbase_field",
726 "rellib", "interfaceid", "interfacename",
727 "methodstub", "methodbase", "attributebase", "handler_func"
729 const unsigned int namenums = sizeof(names)/sizeof(char *);
730 unsigned int namenum;
732 for (i = 0, namenum = 0; namenum==0 && i<namenums; i++)
736 strncmp(line, names[i], strlen(names[i]))==0
737 && isspace(*(line+strlen(names[i])))
739 namenum = i+1;
741 if (namenum==0)
742 exitfileerror(20, "unrecognized configuration option\n");
744 s = line + strlen(names[namenum-1]);
745 if (!isspace(*s))
746 exitfileerror(20, "space character expected after \"%s\"\n", names[namenum-1]);
748 while (isspace(*s)) s++;
749 if (*s=='\0')
750 exitfileerror(20, "unexpected end of line\n");
752 s2 = s + strlen(s);
753 while (isspace(*(s2-1))) s2--;
754 *s2 = '\0';
756 switch (namenum)
758 case 1: /* basename */
759 if (!inclass)
760 cfg->basename = strdup(s);
761 if (cl != NULL)
762 cl->basename = strdup(s);
763 if (in != NULL)
764 exitfileerror(20, "basename not valid config option when in a interface section\n");
765 break;
767 case 2: /* libbase */
768 if (inclass)
769 exitfileerror(20, "libbase not valid config option when in a class section\n");
770 cfg->libbase = strdup(s);
771 break;
773 case 3: /* libbasetype */
774 if (inclass)
775 exitfileerror(20, "libbasetype not valid config option when in a class section\n");
776 cfg->libbasetype = strdup(s);
777 break;
779 case 4: /* libbasetypeextern */
780 if (inclass)
781 exitfileerror(20, "libbasetype not valid config option when in a class section\n");
782 libbasetypeextern = strdup(s);
783 break;
785 case 5: /* version */
786 if (inclass)
787 exitfileerror(20, "version not valid config option when in a class section\n");
788 if (sscanf(s, "%u.%u", &cfg->majorversion, &cfg->minorversion)!=2)
789 exitfileerror(20, "wrong version string \"%s\"\n", s);
790 break;
792 case 6: /* date */
793 if (inclass)
794 exitfileerror(20, "date not valid config option when in a class section\n");
795 #ifndef _WIN32
796 if (strptime(s, "%e.%m.%Y", &date) == NULL)
798 exitfileerror(20, "date string has to have d.m.yyyy format\n");
800 #endif
801 cfg->datestring = strdup(s);
802 break;
804 case 7: /* copyright */
805 if (inclass)
806 exitfileerror(20, "copyright not valid config option when in a class section\n");
807 cfg->copyright = strdup(s);
808 break;
810 case 8: /* libcall */
811 fprintf(stderr, "libcall specification is deprecated and ignored\n");
812 break;
814 case 9: /* forcebase */
815 if (inclass)
816 exitfileerror(20, "forcebase not valid config option when in a class section\n");
817 slist_append(&cfg->forcelist, s);
818 break;
820 case 10: /* superclass */
821 if (cl == NULL)
822 exitfileerror(20, "superclass specified when not a BOOPSI class\n");
823 cl->superclass = strdup(s);
824 break;
826 case 11: /* superclass_field */
827 if (cl == NULL)
828 exitfileerror(20, "superclass_field specified when not a BOOPSI class\n");
829 cl->superclass_field = strdup(s);
830 break;
832 case 12: /* residentpri */
833 if (!inclass)
835 int count;
836 char dummy;
838 count = sscanf(s, "%d%c", &cfg->residentpri, &dummy);
839 if (count != 1 ||
840 cfg->residentpri < -128 || cfg->residentpri > 127
843 exitfileerror(20, "residentpri number format error\n");
846 else
847 exitfileerror(20, "residentpri not valid config option when in a class section\n");
848 break;
850 case 13: /* options */
851 if (!inclass)
853 static const char *optionnames[] =
855 "noautolib", "noexpunge", "noresident", "peropenerbase",
856 "pertaskbase", "includes", "noincludes", "nostubs",
857 "autoinit", "noautoinit", "resautoinit", "noopenclose",
858 "selfinit"
860 const unsigned int optionnums = sizeof(optionnames)/sizeof(char *);
861 int optionnum;
865 for (i = 0, optionnum = 0; optionnum==0 && i<optionnums; i++)
867 if (strncmp(s, optionnames[i], strlen(optionnames[i]))==0)
869 optionnum = i + 1;
870 s += strlen(optionnames[i]);
871 while (isspace(*s)) s++;
872 if (*s == ',')
873 s++;
874 else if (*s != '\0')
875 exitfileerror(20, "Unrecognized option\n");
878 if (optionnum == 0)
879 exitfileerror(20, "Unrecognized option\n");
880 switch (optionnum)
882 case 1: /* noautolib */
883 cfg->options |= OPTION_NOAUTOLIB;
884 break;
885 case 2: /* noexpunge */
886 cfg->options |= OPTION_NOEXPUNGE;
887 break;
888 case 3: /* noresident */
889 cfg->options |= OPTION_NORESIDENT;
890 cfg->firstlvo = 1;
891 break;
892 case 5: /* pertaskbase */
893 cfg->options |= OPTION_PERTASKBASE;
894 /* Fall through */
895 case 4: /* peropenerbase */
896 if (cfg->options & OPTION_DUPBASE)
897 exitfileerror(20, "Only one option peropenerbase or pertaskbase allowed\n");
898 cfg->options |= OPTION_DUPBASE;
899 break;
900 case 6: /* includes */
901 if (cfg->options & OPTION_NOINCLUDES)
902 exitfileerror(20, "option includes and noincludes are incompatible\n");
903 cfg->options |= OPTION_INCLUDES;
904 break;
905 case 7: /* noincludes */
906 if (cfg->options & OPTION_INCLUDES)
907 exitfileerror(20, "option includes and noincludes are incompatible\n");
908 cfg->options |= OPTION_NOINCLUDES;
909 break;
910 case 8: /* nostubs */
911 cfg->options |= OPTION_NOSTUBS;
912 break;
913 case 9: /* autoinit */
914 if (cfg->options & OPTION_NOAUTOINIT)
915 exitfileerror(20, "option autoinit and noautoinit are incompatible\n");
916 cfg->options |= OPTION_AUTOINIT;
917 break;
918 case 10: /* noautoinit */
919 if (cfg->options & OPTION_AUTOINIT)
920 exitfileerror(20, "option autoinit and noautoinit are incompatible\n");
921 cfg->options |= OPTION_NOAUTOINIT;
922 break;
923 case 11: /* resautoinit */
924 if (cfg->options & OPTION_SELFINIT)
925 exitfileerror(20, "option resautoinit and selfinit are incompatible\n");
926 cfg->options |= OPTION_RESAUTOINIT;
927 break;
928 case 12:
929 cfg->options |= OPTION_NOOPENCLOSE;
930 break;
931 case 13: /* noresautoinit */
932 if (cfg->options & OPTION_RESAUTOINIT)
933 exitfileerror(20, "option resautoinit and selfinit are incompatible\n");
934 cfg->options |= OPTION_SELFINIT;
935 break;
937 while (isspace(*s)) s++;
938 } while(*s !='\0');
940 else
942 static const char *optionnames[] =
944 "private"
946 const unsigned int optionnums = sizeof(optionnames)/sizeof(char *);
947 int optionnum;
951 for (i = 0, optionnum = 0; optionnum==0 && i<optionnums; i++)
953 if (strncmp(s, optionnames[i], strlen(optionnames[i]))==0)
955 optionnum = i + 1;
956 s += strlen(optionnames[i]);
957 while (isspace(*s)) s++;
958 if (*s == ',')
959 s++;
960 else if (*s != '\0')
961 exitfileerror(20, "Unrecognized option\n");
964 if (optionnum == 0)
965 exitfileerror(20, "Unrecognized option\n");
966 switch (optionnum)
968 case 1: /* private */
969 cl->options |= COPTION_PRIVATE;
970 break;
972 while (isspace(*s)) s++;
973 } while(*s !='\0');
975 break;
977 case 14: /* sysbase_field */
978 if (inclass)
979 exitfileerror(20, "sysbase_field not valid config option when in a class section\n");
980 cfg->sysbase_field = strdup(s);
981 break;
983 case 15: /* seglist_field */
984 if (inclass)
985 exitfileerror(20, "seglist_field not valid config option when in a class section\n");
986 cfg->seglist_field = strdup(s);
987 break;
989 case 16: /* rootbase_field */
990 if (inclass)
991 exitfileerror(20, "rootbase_field not valid config option when in a class section\n");
992 cfg->rootbase_field = strdup(s);
993 break;
995 case 17: /* classptr_field */
996 if (cl == NULL)
998 exitfileerror
1001 "classptr_field specified when not a BOOPSI class\n"
1004 cl->classptr_field = strdup(s);
1005 break;
1007 case 18: /* classptr_var */
1008 if (cl == NULL)
1010 exitfileerror
1013 "classptr_var specified when not a BOOPSI class\n"
1016 cl->classptr_var = strdup(s);
1017 break;
1019 case 19: /* classid */
1020 if (cl == NULL)
1021 exitfileerror(20, "classid specified when not a BOOPSI class\n");
1022 if (cl->classid != NULL)
1023 exitfileerror(20, "classid specified twice\n");
1024 cl->classid = strdup(s);
1025 if (strcmp(cl->classid, "NULL") == 0)
1026 cl->options |= COPTION_PRIVATE;
1027 break;
1029 case 20: /* classdatatype */
1030 if (cl == NULL)
1031 exitfileerror(20, "classdatatype specified when not a BOOPSI class\n");
1032 cl->classdatatype = strdup(s);
1033 break;
1035 case 21: /* beginio_func */
1036 if (inclass)
1037 exitfileerror(20, "beginio_func not valid config option when in a class section\n");
1038 if (cfg->modtype != DEVICE)
1039 exitfileerror(20, "beginio_func specified when not a device\n");
1040 cfg->beginiofunc = strdup(s);
1041 break;
1043 case 22: /* abortio_func */
1044 if (inclass)
1045 exitfileerror(20, "abortio_func not valid config option when in a class section\n");
1046 if (cfg->modtype != DEVICE)
1047 exitfileerror(20, "abortio_func specified when not a device\n");
1048 cfg->abortiofunc = strdup(s);
1049 break;
1051 case 23: /* dispatcher */
1052 if (cl == NULL)
1053 exitfileerror(20, "dispatcher specified when not a BOOPSI class\n");
1054 cl->dispatcher = strdup(s);
1055 /* function references are not needed when dispatcher is specified */
1056 cfg->intcfg |= CFG_NOREADFUNCS;
1057 break;
1059 case 24: /* initpri */
1060 if (cl != NULL)
1062 int count;
1063 char dummy;
1065 count = sscanf(s, "%d%c", &cl->initpri, &dummy);
1066 if (count != 1 ||
1067 cl->initpri < -128 || cl->initpri > 127
1070 exitfileerror(20, "initpri number format error\n");
1073 else
1074 exitfileerror(20, "initpri only valid config option for a BOOPSI class\n");
1075 break;
1077 case 25: /* type */
1078 if (!inclass)
1079 exitfileerror(20, "type only valid config option in a class section\n");
1080 if (strcmp(s,"mcc")==0)
1081 cl->classtype = MCC;
1082 else if (strcmp(s,"mui")==0)
1083 cl->classtype = MUI;
1084 else if (strcmp(s,"mcp")==0)
1085 cl->classtype = MCP;
1086 else if (strcmp(s, "image")==0)
1087 cl->classtype = IMAGE;
1088 else if (strcmp(s, "gadget")==0)
1089 cl->classtype = GADGET;
1090 else if (strcmp(s, "datatype")==0)
1091 cl->classtype = DATATYPE;
1092 else if (strcmp(s, "usbclass")==0)
1093 cl->classtype = USBCLASS;
1094 else if (strcmp(s, "class")==0)
1095 cl->classtype = CLASS;
1096 else if (strcmp(s, "hidd")==0)
1097 cl->classtype = HIDD;
1098 else
1100 fprintf(stderr, "Unknown type \"%s\" specified\n", s);
1101 exit(20);
1103 break;
1105 case 26: /* addromtag */
1106 cfg->addromtag = strdup(s);
1107 break;
1109 case 27: /* oopbase_field */
1110 cfg->oopbase_field = strdup(s);
1111 break;
1112 case 28: /* rellib */
1113 slist_append(&cfg->rellibs, s);
1114 break;
1115 case 29: /* interfaceid */
1116 if (!in)
1117 exitfileerror(20, "interfaceid only valid config option for an interface\n");
1118 in->interfaceid = strdup(s);
1119 break;
1120 case 30: /* interfacename */
1121 if (!in)
1122 exitfileerror(20, "interfacename only valid config option for an interface\n");
1123 in->interfacename = strdup(s);
1124 break;
1125 case 31: /* methodstub */
1126 if (!in)
1127 exitfileerror(20, "methodstub only valid config option for an interface\n");
1128 in->methodstub = strdup(s);
1129 break;
1130 case 32: /* methodbase */
1131 if (!in)
1132 exitfileerror(20, "methodbase only valid config option for an interface\n");
1133 in->methodbase = strdup(s);
1134 break;
1135 case 33: /* attributebase */
1136 if (!in)
1137 exitfileerror(20, "attributebase only valid config option for an interface\n");
1138 in->attributebase = strdup(s);
1139 break;
1140 case 34: /* handler_func */
1141 if (cfg->modtype != HANDLER)
1142 exitfileerror(20, "handler specified when not a handler\n");
1143 cfg->handlerfunc = strdup(s);
1144 break;
1147 else /* Line starts with ## */
1149 s = line+2;
1150 while (isspace(*s)) s++;
1151 if (strncmp(s, "end", 3)!=0)
1152 exitfileerror(20, "\"##end config\" expected\n");
1154 s += 3;
1155 if (!isspace(*s))
1156 exitfileerror(20, "\"##end config\" expected\n");
1158 while (isspace(*s)) s++;
1159 if (strncmp(s, "config", 6)!=0)
1160 exitfileerror(20, "\"##end config\" expected\n");
1162 s += 6;
1163 while (isspace(*s)) s++;
1164 if (*s!='\0')
1165 exitfileerror(20, "\"##end config\" expected\n");
1167 atend = 1;
1171 /* When not in a class section fill in default values for fields in cfg */
1172 if (!inclass)
1174 if (cfg->basename==NULL)
1176 cfg->basename = strdup(cfg->modulename);
1177 *cfg->basename = toupper(*cfg->basename);
1179 if (cfg->libbase==NULL)
1181 unsigned int len = strlen(cfg->basename)+5;
1182 cfg->libbase = malloc(len);
1183 snprintf(cfg->libbase, len, "%sBase", cfg->basename);
1185 if (cfg->libbasetype == NULL && libbasetypeextern != NULL)
1186 cfg->libbasetype = strdup(libbasetypeextern);
1187 if (cfg->sysbase_field != NULL && cfg->libbasetype == NULL)
1188 exitfileerror(20, "sysbase_field specified when no libbasetype is given\n");
1189 if (cfg->seglist_field != NULL && cfg->libbasetype == NULL)
1190 exitfileerror(20, "seglist_field specified when no libbasetype is given\n");
1191 if (cfg->oopbase_field != NULL && cfg->libbasetype == NULL)
1192 exitfileerror(20, "oopbase_field specified when no libbasetype is given\n");
1193 /* rootbase_field only allowed when duplicating base */
1194 if (cfg->rootbase_field != NULL && !(cfg->options & OPTION_DUPBASE))
1195 exitfileerror(20, "rootbasefield only valid for option peropenerbase or pertaskbase\n");
1197 /* Set default date to current date */
1198 if (cfg->datestring == NULL)
1200 char tmpbuf[256];
1201 time_t now = time(NULL);
1202 struct tm *ltime = localtime(&now);
1204 snprintf(tmpbuf, sizeof(tmpbuf), "%u.%u.%u",
1205 ltime->tm_mday, 1 + ltime->tm_mon, 1900 + ltime->tm_year);
1207 cfg->datestring = strdup(tmpbuf);
1210 if (cfg->copyright == NULL)
1211 cfg->copyright = "";
1213 if ( (cfg->beginiofunc != NULL && cfg->abortiofunc == NULL)
1214 || (cfg->beginiofunc == NULL && cfg->abortiofunc != NULL)
1216 exitfileerror(20, "please specify both beginio_func and abortio_func\n");
1218 if (libbasetypeextern==NULL)
1220 switch (cfg->modtype)
1222 case DEVICE:
1223 cfg->libbasetypeptrextern = "struct Device *";
1224 break;
1225 case HANDLER:
1226 case RESOURCE:
1227 cfg->libbasetypeptrextern = "APTR ";
1228 break;
1229 case LIBRARY:
1230 case MUI:
1231 case MCP:
1232 case MCC:
1233 case GADGET:
1234 case DATATYPE:
1235 case USBCLASS:
1236 case HIDD:
1237 cfg->libbasetypeptrextern = "struct Library *";
1238 break;
1239 default:
1240 fprintf(stderr, "Internal error: Unsupported modtype for libbasetypeptrextern\n");
1241 exit(20);
1244 else
1246 cfg->libbasetypeptrextern = malloc(strlen(libbasetypeextern)+3);
1247 strcpy(cfg->libbasetypeptrextern, libbasetypeextern);
1248 strcat(cfg->libbasetypeptrextern, " *");
1249 free(libbasetypeextern);
1253 /* When class was given too fill in some defaults when not specified */
1254 if (cl != NULL)
1256 if (cl->classtype == UNSPECIFIED)
1257 cl->classtype = CLASS;
1259 if (cl->basename == NULL)
1261 if (!inclass)
1262 cl->basename = cfg->basename;
1263 else
1264 exitfileerror(20, "basename has to be specified in the config section inside of a class section\n");
1267 /* MUI classes are always private */
1268 if (cl->classtype == MUI || cl->classtype == MCC || cl->classtype == MCP)
1269 cl->options |= COPTION_PRIVATE;
1271 if (cl->classid == NULL
1272 && (cl->classtype != MUI && cl->classtype != MCC && cl->classtype != MCP)
1275 if (cl->classtype == HIDD)
1277 cl->options &= !COPTION_PRIVATE;
1279 else if (cl->options & COPTION_PRIVATE)
1281 cl->classid = "NULL";
1283 else
1285 char s[256] = "";
1287 if (cl->classtype == GADGET || cl->classtype == IMAGE || cl->classtype == CLASS || cl->classtype == USBCLASS)
1289 sprintf(s, "\"%sclass\"", inclass ? cl->basename : cfg->modulename);
1291 else if (cl->classtype == DATATYPE)
1293 sprintf(s, "\"%s.datatype\"", inclass ? cl->basename : cfg->modulename);
1295 cl->classid = strdup(s);
1299 /* Only specify superclass or superclass_field */
1300 if (cl->superclass != NULL && cl->superclass_field != NULL)
1301 exitfileerror(20, "Only specify one of superclass or superclass_field in config section\n");
1303 /* Give default value to superclass if it is not specified */
1304 if (cl->superclass == NULL && cl->superclass == NULL)
1306 switch (cl->classtype)
1308 case MUI:
1309 case MCC:
1310 cl->superclass = "MUIC_Area";
1311 break;
1312 case MCP:
1313 cl->superclass = "MUIC_Mccprefs";
1314 break;
1315 case IMAGE:
1316 cl->superclass = "IMAGECLASS";
1317 break;
1318 case GADGET:
1319 cl->superclass = "GADGETCLASS";
1320 break;
1321 case DATATYPE:
1322 cl->superclass = "DATATYPESCLASS";
1323 break;
1324 case CLASS:
1325 cl->superclass = "ROOTCLASS";
1326 break;
1327 case HIDD:
1328 cl->superclass = "CLID_Root";
1329 break;
1330 default:
1331 exitfileerror(20, "Internal error: unhandled classtype in readsectionconfig\n");
1332 break;
1338 static void readsectioncdef(struct config *cfg)
1340 int atend = 0;
1341 char *line, *s;
1343 while (!atend)
1345 line = readline();
1346 if (line==NULL)
1347 exitfileerror(20, "unexptected end of file in section cdef\n");
1349 if (strncmp(line, "##", 2)!=0)
1351 slist_append(&cfg->cdeflines, line);
1353 else
1355 s = line+2;
1356 while (isspace(*s)) s++;
1357 if (strncmp(s, "end", 3)!=0)
1358 exitfileerror(20, "\"##end cdef\" expected\n");
1360 s += 3;
1361 while (isspace(*s)) s++;
1362 if (strncmp(s, "cdef", 4)!=0)
1363 exitfileerror(20, "\"##end cdef\" expected\n");
1365 s += 5;
1366 while (isspace(*s)) s++;
1367 if (*s!='\0')
1368 exitfileerror(20, "unexpected character at position %d\n");
1370 atend = 1;
1375 static void readsectioncdefprivate(struct config *cfg)
1377 int atend = 0;
1378 char *line, *s;
1380 while (!atend)
1382 line = readline();
1383 if (line==NULL)
1384 exitfileerror(20, "unexptected end of file in section cdef\n");
1386 if (strncmp(line, "##", 2)!=0)
1388 slist_append(&cfg->cdefprivatelines, line);
1390 else
1392 s = line+2;
1393 while (isspace(*s)) s++;
1394 if (strncmp(s, "end", 3)!=0)
1395 exitfileerror(20, "\"##end cdefprivate\" expected\n");
1397 s += 3;
1398 while (isspace(*s)) s++;
1399 if (strncmp(s, "cdefprivate", 11)!=0)
1400 exitfileerror(20, "\"##end cdefprivate\" expected\n");
1402 s += 11;
1403 while (isspace(*s)) s++;
1404 if (*s!='\0')
1405 exitfileerror(20, "unexpected character at position %d\n");
1407 atend = 1;
1412 static void readsectionstartup(struct config *cfg)
1414 int atend = 0;
1415 char *line, *s;
1417 while (!atend)
1419 line = readline();
1420 if (line==NULL)
1421 exitfileerror(20, "unexptected end of file in section startup\n");
1423 if (strncmp(line, "##", 2)!=0)
1425 slist_append(&cfg->startuplines, line);
1427 else
1429 s = line+2;
1430 while (isspace(*s)) s++;
1431 if (strncmp(s, "end", 3)!=0)
1432 exitfileerror(20, "\"##end startup\" expected\n");
1434 s += 3;
1435 while (isspace(*s)) s++;
1436 if (strncmp(s, "startup", 7)!=0)
1437 exitfileerror(20, "\"##end startup\" expected\n");
1439 s += 7;
1440 while (isspace(*s)) s++;
1441 if (*s!='\0')
1442 exitfileerror(20, "unexpected character at position %d\n");
1444 atend = 1;
1449 static void readsectionfunctionlist(const char *type, struct functionhead **funclistptr, unsigned int firstlvo, int isattribute, enum libcall def_libcall)
1451 int atend = 0, i;
1452 char *line, *s, *s2;
1453 unsigned int lvo = firstlvo;
1454 int minversion = -1;
1456 while (!atend)
1458 line = readline();
1459 if (line==NULL)
1460 exitfileerror(20, "unexpected EOF in functionlist section\n");
1461 if (strlen(line)==0)
1463 if (*funclistptr != NULL)
1464 funclistptr = &((*funclistptr)->next);
1465 lvo++;
1467 else if (isspace(*line))
1469 s = line;
1470 while (isspace(*s)) s++;
1471 if (*s=='\0')
1473 if (*funclistptr != NULL)
1474 funclistptr = &((*funclistptr)->next);
1475 lvo++;
1477 else
1478 exitfileerror(20, "no space allowed before functionname\n");
1480 else if (strncmp(line, "##", 2)==0)
1482 s = line+2;
1483 while (isspace(*s)) s++;
1484 if (strncmp(s, "end", 3)!=0)
1485 exitfileerror(20, "\"##end %s\" expected\n", type);
1487 s += 3;
1488 while (isspace(*s)) s++;
1489 if (strncmp(s, type, strlen(type))!=0)
1490 exitfileerror(20, "\"##end %s\" expected\n", type);
1492 s += strlen(type);
1493 while (isspace(*s)) s++;
1494 if (*s!='\0')
1495 exitfileerror(20, "unexpected character on position %d\n", s-line);
1497 atend = 1;
1499 else if (*line=='.')
1501 s = line+1;
1502 if (strncmp(s, "skip", 4)==0)
1504 int n;
1506 s += 4;
1507 if (!isspace(*s))
1508 exitfileerror(20, "syntax is '.skip n'\n");
1510 n=strtol(s, &s2, 10);
1511 if (s2==NULL)
1512 exitfileerror(20, "positive number expected\n");
1514 while (isspace(*s2)) s2++;
1515 if ((*s2 != '\0') && (*s2 != '#'))
1516 exitfileerror(20, "syntax is '.skip n'\n");
1517 if (*funclistptr != NULL)
1518 funclistptr = &((*funclistptr)->next);
1519 lvo += n;
1521 else if (strncmp(s, "alias", 5)==0)
1523 s += 5;
1525 if (!isspace(*s))
1526 exitfileerror(20, "syntax is '.alias name'\n");
1528 while (isspace(*s)) s++;
1529 if (*s == '\0' || !(isalpha(*s) || *s == '_'))
1530 exitfileerror(20, "syntax is '.alias name'\n");
1532 s2 = s;
1533 s++;
1534 while (isalnum(*s) || *s == '_') s++;
1536 if (isspace(*s))
1538 *s = '\0';
1539 do {
1540 s++;
1541 } while (isspace(*s));
1544 if (*s != '\0')
1545 exitfileerror(20, "syntax is '.alias name'\n");
1547 if (*funclistptr == NULL)
1548 exitfileerror(20, ".alias has to come after a function declaration\n");
1550 slist_append(&(*funclistptr)->aliases, s2);
1552 else if (strncmp(s, "function", 8) == 0)
1554 s += 8;
1556 if (!isspace(*s))
1557 exitfileerror(20, "Syntax error\n");
1559 while (isspace(*s)) s++;
1560 if (*s == '\0' || !(isalpha(*s) || *s == '_'))
1561 exitfileerror(20, "syntax is '.function name'\n");
1563 s2 = s;
1564 s++;
1565 while (isalnum(*s) || *s == '_') s++;
1567 if (isspace(*s))
1569 *s = '\0';
1570 do {
1571 s++;
1572 } while (isspace(*s));
1575 if (*s != '\0')
1576 exitfileerror(20, "syntax is '.function name'\n");
1578 if (*funclistptr == NULL)
1579 exitfileerror(20, ".function has to come after a function declaration\n");
1581 funcsetinternalname(*funclistptr, s2);
1583 else if (strncmp(s, "cfunction", 9)==0)
1585 if (*funclistptr == NULL)
1586 exitfileerror(20, ".cfunction has to come after a function declaration\n");
1588 (*funclistptr)->libcall = REGISTER;
1590 else if (strncmp(s, "private", 7)==0)
1592 if (*funclistptr == NULL)
1593 exitfileerror(20, ".private has to come after a function declaration\n");
1595 (*funclistptr)->priv = 1;
1597 else if (strncmp(s, "novararg", 8)==0)
1599 if (*funclistptr == NULL)
1600 exitfileerror(20, ".novararg has to come after a function declaration\n");
1602 (*funclistptr)->novararg = 1;
1604 else if (strncmp(s, "version", 7) == 0)
1606 /* Mark version number for the following
1607 * functions, so that the automatic OpenLibrary()
1608 * will know what version to use.
1610 char *tmp;
1611 int ver;
1613 s += 7;
1615 while (isspace(*s)) s++;
1616 ver = (int)strtol(s, &tmp, 0);
1618 if (s == tmp)
1619 exitfileerror(20, ".version expects an integer\n");
1621 s = tmp;
1622 while (isspace(*s)) s++;
1624 if (*s && *s != '#')
1625 exitfileerror(20, ".version has junk after the version number\n");
1627 minversion = ver;
1629 else if (strncmp(s, "unusedlibbase", 13) == 0)
1631 if (*funclistptr == NULL)
1632 exitfileerror(20, ".unusedlibbase has to come after a function declaration\n");
1633 (*funclistptr)->unusedlibbase = 1;
1635 else
1636 exitfileerror(20, "Syntax error");
1638 else if (*line!='#') /* Ignore line that is a comment, e.g. that starts with a # */
1640 /* The line is a function or attribute prototype.
1641 * A function can have one of two syntaxes:
1642 * type funcname(argproto1, argproto2, ...)
1643 * type funcname(argproto1, argproto2, ...) (reg1, reg2, ...)
1644 * The former is for C type function argument passing, the latter for
1645 * register argument passing.
1646 * An attribute has the following syntax:
1647 * type attribute
1649 char c, *args[64], *regs[64], *funcname, *cp;
1650 int len, argcount = 0, regcount = 0, brcount = 0;
1652 cp = strchr(line,'#');
1653 if (cp)
1654 *(cp++) = 0;
1656 /* Parse 'type functionname' at the beginning of the line */
1657 if (isattribute) {
1658 s = line + strlen(line);
1659 } else {
1660 s = strchr(line, '(');
1661 if (s == NULL)
1662 exitfileerror(20, "( expected at position %d\n", strlen(line) + 1);
1665 s2 = s;
1666 while (isspace(*(s2-1)))
1667 s2--;
1668 *s2 = '\0';
1670 while (s2 > line && !isspace(*(s2-1)) && !(*(s2-1) == '*'))
1671 s2--;
1673 if (s2 == line)
1674 exitfileerror(20, "No type specifier before %s name\n", isattribute ? "attribute" : "function");
1676 if (*funclistptr != NULL)
1677 funclistptr = &((*funclistptr)->next);
1678 *funclistptr = newfunctionhead(s2, STACK);
1680 if (cp)
1681 (*funclistptr)->comment = strdup(cp);
1682 else
1683 (*funclistptr)->comment = NULL;
1685 while (isspace(*(s2-1)))
1686 s2--;
1687 *s2 = '\0';
1688 (*funclistptr)->type = strdup(line);
1689 (*funclistptr)->lvo = lvo;
1690 (*funclistptr)->version = minversion;
1691 lvo++;
1693 if (isattribute)
1694 continue;
1696 /* Parse function prototype */
1697 s++;
1698 while (isspace(*s))
1699 s++;
1700 c = *s;
1702 while (c != ')')
1704 while (isspace(*s))
1705 s++;
1707 args[argcount] = s;
1708 argcount++;
1710 while
1712 *s != '\0'
1713 && !(brcount == 0 && (*s == ',' || *s == ')'))
1716 if (*s == '(')
1717 brcount++;
1718 if (*s == ')')
1720 if (brcount > 0)
1721 brcount--;
1722 else
1723 exitfileerror(20, "Unexected ')' at position %d\n", s-line+1);
1725 s++;
1728 c = *s;
1729 if (c == '\0')
1730 exitfileerror(20, "'(' without ')'");
1732 s2 = s;
1733 while (isspace(*(s2-1)))
1734 s2--;
1735 *s2 = '\0';
1737 if (!(s2 > args[argcount - 1]))
1738 exitfileerror(20, "Syntax error in function prototype\n");
1740 s++;
1743 s++;
1744 while (*s != '\0' && isspace(*s))
1745 s++;
1747 if (*s == '(')
1749 /* Parse registers specifications if available otherwise this prototype for C type argument passing */
1751 /* There may be no register specified with () so be sure then c is == ')' */
1752 s++;
1753 while(isspace(*s))
1754 s++;
1756 c = *s;
1758 while (c != ')')
1760 while (isspace(*s))
1761 s++;
1763 regs[regcount] = s;
1764 regcount++;
1766 if (memchr("AD",s[0],2)!=NULL && memchr("01234567",s[1],8)!=NULL)
1768 s += 2;
1769 c = *s;
1770 if (c == '/')
1772 s++;
1773 if (s[0] == s[-3] && s[1] == s[-2] + 1)
1775 s += 2;
1776 c = *s;
1778 else
1779 exitfileerror(20,
1780 "wrong register specification \"%s\" for argument %u\n",
1781 regs[regcount-1], regcount
1784 if (regcount > 4)
1785 exitfileerror(20, "maximum four arguments passed in two registers allowed (%d, %s) \n", regcount, regs[regcount-1]);
1787 *s = '\0';
1789 else
1790 exitfileerror(20,
1791 "wrong register \"%s\" for argument %u\n",
1792 regs[regcount-1], regcount
1795 while (isspace(c))
1797 s++;
1798 c = *s;
1800 if (c == '\0')
1801 exitfileerror(20, "'(' without ')'\n");
1802 if (c != ',' && c != ')')
1803 exitfileerror(20, "',' or ')' expected at position %d\n", s-line+1);
1805 s++;
1808 s++;
1809 while (isspace(*s)) s++;
1810 if (*s!='\0')
1811 exitfileerror(20, "wrong char '%c' at position %d\n", *s, (int)(s-line) + 1);
1813 if (argcount != regcount)
1814 exitfileerror(20, "Number of arguments (%d) and registers (%d) mismatch\n",
1815 argcount, regcount
1818 (*funclistptr)->libcall = def_libcall;
1819 for (i = 0; i < argcount; i++)
1820 funcaddarg(*funclistptr, args[i], regs[i]);
1822 else if (*s == '\0')
1823 { /* No registers specified */
1824 for (i = 0; i < argcount; i++)
1825 funcaddarg(*funclistptr, args[i], NULL);
1827 else
1828 exitfileerror(20, "wrong char '%c' at position %d\n", *s, (int)(s-line) + 1);
1833 static void readsectionclass_methodlist(struct classinfo *cl)
1835 int atend = 0, i;
1836 char *line, *s, *s2;
1837 struct functionhead **methlistptr = &cl->methlist;
1838 struct stringlist *interface = NULL;
1840 if (cl->basename==NULL)
1841 exitfileerror(20, "section methodlist has to come after section config\n");
1843 while (!atend)
1845 line = readline();
1846 if (line==NULL)
1847 exitfileerror(20, "unexptected EOF in methodlist section\n");
1849 /* Ignore empty lines or lines that qre a comment, e.g. that starts with a # */
1850 if (strlen(line)==0 || (line[0] == '#' && line[1] != '#'))
1851 continue;
1853 if (isspace(*line))
1854 exitfileerror(20, "No space allowed at start of the line\n");
1856 if (strncmp(line, "##", 2)==0) /* Is this the end ? */
1858 s = line+2;
1859 while (isspace(*s)) s++;
1860 if (strncmp(s, "end", 3)!=0)
1861 exitfileerror(20, "\"##end methodlist\" expected\n");
1863 s += 3;
1864 while (isspace(*s)) s++;
1865 if (strncmp(s, "methodlist", 10)!=0)
1866 exitfileerror(20, "\"##end methodlist\" expected\n");
1868 s += 10;
1869 while (isspace(*s)) s++;
1870 if (*s!='\0')
1871 exitfileerror(20, "unexpected character on position %d\n", s-line);
1873 atend = 1;
1875 continue;
1878 if (*line=='.')
1880 s = line+1;
1881 if (strncmp(s, "alias", 5)==0)
1883 s += 5;
1885 if (!isspace(*s))
1886 exitfileerror(20, "syntax is '.alias name'\n");
1888 while (isspace(*s)) s++;
1889 if (*s == '\0' || !(isalpha(*s) || *s == '_'))
1890 exitfileerror(20, "syntax is '.alias name'\n");
1892 s2 = s;
1893 s++;
1894 while (isalnum(*s) || *s == '_') s++;
1896 if (isspace(*s))
1898 *s = '\0';
1899 do {
1900 s++;
1901 } while (isspace(*s));
1904 if (*s != '\0')
1905 exitfileerror(20, "syntax is '.alias name'\n");
1907 if (*methlistptr == NULL)
1908 exitfileerror(20, ".alias has to come after a function declaration\n");
1910 slist_append(&(*methlistptr)->aliases, s2);
1912 else if (strncmp(s, "function", 8) == 0)
1914 s += 8;
1916 if (!isspace(*s))
1917 exitfileerror(20, "Syntax error\n");
1919 while (isspace(*s)) s++;
1920 if (*s == '\0' || !(isalpha(*s) || *s == '_'))
1921 exitfileerror(20, "syntax is '.function name'\n");
1923 s2 = s;
1924 s++;
1925 while (isalnum(*s) || *s == '_') s++;
1927 if (isspace(*s))
1929 *s = '\0';
1930 do {
1931 s++;
1932 } while (isspace(*s));
1935 if (*s != '\0')
1936 exitfileerror(20, "syntax is '.function name'\n");
1938 if (*methlistptr == NULL)
1939 exitfileerror(20, ".function has to come after a function declaration\n");
1941 funcsetinternalname(*methlistptr, s2);
1943 else if (strncmp(s, "interface", 9) == 0)
1945 if (cl->classtype != HIDD)
1946 exitfileerror(20, "interface only valid for a HIDD\n");
1948 s += 9;
1950 if (!isspace(*s))
1951 exitfileerror(20, "Syntax error\n");
1953 while (isspace(*s)) s++;
1954 if (*s == '\0' || !isalpha(*s))
1955 exitfileerror(20, "syntax is '.interface name'\n");
1957 s2 = s;
1958 s++;
1959 while (isalnum(*s) || *s == '_') s++;
1961 if (isspace(*s))
1963 *s = '\0';
1964 do {
1965 s++;
1966 } while (isspace(*s));
1969 if (*s != '\0')
1970 exitfileerror(20, "syntax is '.interface name'\n");
1972 interface = slist_append(&cl->interfaces, s2);
1974 else
1975 exitfileerror(20, "Syntax error");
1977 else if (isalpha(*line))
1979 char stmp[256];
1981 for (s = line + 1; isalnum(*s) || *s == '_'; s++)
1984 if (cl->classtype == HIDD && interface == NULL)
1985 exitfileerror(20, "For a HIDD the first method has to come after an .interface line\n");
1987 if (*s != '\0')
1988 exitfileerror(20, "Only letters, digits and an underscore allowed in a methodname\n");
1990 if (*methlistptr != NULL)
1991 methlistptr = &((*methlistptr)->next);
1992 if (cl->classtype != HIDD)
1994 if (snprintf(stmp, 256, "%s__%s", cl->basename, line) >= 256)
1995 exitfileerror(20, "Method name too large\n");
1997 *methlistptr = newfunctionhead(stmp, STACK);
1998 (*methlistptr)->type = "IPTR";
1999 funcaddarg(*methlistptr, "Class *cl", NULL);
2000 funcaddarg(*methlistptr, "Object *o", NULL);
2001 funcaddarg(*methlistptr, "Msg msg", NULL);
2003 else
2005 if (snprintf(stmp, 256, "%s__%s__%s", cl->basename, interface->s, line) >= 256)
2006 exitfileerror(20, "Method name too large\n");
2008 *methlistptr = newfunctionhead(stmp, STACK);
2009 (*methlistptr)->type = "IPTR";
2010 funcaddarg(*methlistptr, "OOP_Class *cl", NULL);
2011 funcaddarg(*methlistptr, "OOP_Object *o", NULL);
2012 funcaddarg(*methlistptr, "OOP_Msg msg", NULL);
2013 (*methlistptr)->interface = interface;
2014 if (snprintf(stmp, 256, "mo%s_%s", interface->s, line) >= 256)
2015 exitfileerror(20, "Method name too large\n");
2016 (*methlistptr)->method = strdup(stmp);
2018 slist_append(&(*methlistptr)->aliases, line);
2020 else
2021 exitfileerror(20, "Methodname has to begin with a letter\n");
2025 static void
2026 readsectioninterface(struct config *cfg)
2028 char *s;
2029 struct interfaceinfo *in;
2031 in = newinterface(cfg);
2032 s = readsections(cfg, NULL, in, 1);
2033 if (s == NULL)
2034 exitfileerror(20, "Unexpected end of file\n");
2036 if (strncmp(s, "##", 2) != 0)
2037 exitfileerror(20, "'##end interface' expected\n");
2038 s += 2;
2040 while (isspace(*s)) s++;
2042 if (strncmp(s, "end", 3) != 0)
2043 exitfileerror(20, "'##end interface' expected\n");
2044 s += 3;
2046 if (!isspace(*s))
2047 exitfileerror(20, "'##end interface' expected\n");
2048 while (isspace(*s)) s++;
2050 if (strncmp(s, "interface", 9) != 0)
2051 exitfileerror(20, "'##end interface' expected\n");
2052 s += 9;
2054 while (isspace(*s)) s++;
2055 if (*s != '\0')
2056 exitfileerror(20, "'##end interface' expected\n");
2058 if (!in->interfaceid)
2059 exitfileerror(20, "interface has no 'interfaceid' defined!\n");
2061 if (!in->interfacename)
2062 exitfileerror(20, "interface has no 'interfacename' defined!\n");
2064 if (!in->methodstub)
2065 in->methodstub = strdup(in->interfacename);
2067 if (!in->methodbase) {
2068 int len = strlen(in->interfacename);
2069 in->methodbase = malloc(len + 4 + 1);
2070 strcpy(in->methodbase, in->interfacename);
2071 strcat(in->methodbase, "Base");
2074 if (!in->attributebase) {
2075 int len = strlen(in->interfacename);
2076 in->attributebase = malloc(len + 4 + 4 + 1);
2077 strcpy(in->attributebase, in->interfacename);
2078 strcat(in->attributebase, "AttrBase");
2082 static void
2083 readsectionclass(struct config *cfg)
2085 char *s;
2086 struct classinfo *cl;
2088 cl = newclass(cfg);
2089 s = readsections(cfg, cl, NULL, 1);
2090 if (s == NULL)
2091 exitfileerror(20, "Unexpected end of file\n");
2093 if (strncmp(s, "##", 2) != 0)
2094 exitfileerror(20, "'##end class' expected\n");
2095 s += 2;
2097 while (isspace(*s)) s++;
2099 if (strncmp(s, "end", 3) != 0)
2100 exitfileerror(20, "'##end class' expected\n");
2101 s += 3;
2103 if (!isspace(*s))
2104 exitfileerror(20, "'##end class' expected\n");
2105 while (isspace(*s)) s++;
2107 if (strncmp(s, "class", 5) != 0)
2108 exitfileerror(20, "'##end class' expected\n");
2109 s += 5;
2111 while (isspace(*s)) s++;
2112 if (*s != '\0')
2113 exitfileerror(20, "'##end class' expected\n");
2116 static struct classinfo *newclass(struct config *cfg)
2118 struct classinfo *cl, *classlistit;
2120 cl = malloc(sizeof(struct classinfo));
2121 if (cl == NULL)
2123 fprintf(stderr, "Out of memory\n");
2124 exit(20);
2126 memset(cl, 0, sizeof(struct classinfo));
2128 /* By default the classes are initialized with a priority of 1 so they
2129 * are initialized before any user added initialization with priority 1
2131 cl->initpri = 1;
2133 if (cfg->classlist == NULL)
2134 cfg->classlist = cl;
2135 else
2139 classlistit = cfg->classlist;
2140 classlistit->next != NULL;
2141 classlistit = classlistit->next
2144 classlistit->next = cl;
2147 return cl;
2150 static struct handlerinfo *newhandler(struct config *cfg)
2152 struct handlerinfo *hl;
2154 hl = calloc(1,sizeof(*hl));
2155 hl->next = cfg->handlerlist;
2156 cfg->handlerlist = hl;
2157 return hl;
2160 static struct interfaceinfo *newinterface(struct config *cfg)
2162 struct interfaceinfo *in, *interfacelistit;
2164 in = malloc(sizeof(struct interfaceinfo));
2165 if (in == NULL)
2167 fprintf(stderr, "Out of memory\n");
2168 exit(20);
2170 memset(in, 0, sizeof(struct interfaceinfo));
2172 if (cfg->interfacelist == NULL)
2173 cfg->interfacelist = in;
2174 else
2178 interfacelistit = cfg->interfacelist;
2179 interfacelistit->next != NULL;
2180 interfacelistit = interfacelistit->next
2183 interfacelistit->next = in;
2186 return in;
2190 static int getdirective(char *s, const char *directive, int range_min, int range_max, int *val)
2192 char *tmp;
2193 int newval;
2195 if (strncmp(s, directive, strlen(directive)) != 0)
2196 return 0;
2198 s += strlen(directive);
2199 if (*s && !isspace(*s))
2200 exitfileerror(20, "Unrecognized directive \".%s\"\n", directive);
2202 while (isspace(*s)) s++;
2203 if (!*s)
2204 exitfileerror(20, "No .%s value specified\n", directive);
2206 newval = strtol(s, &tmp, 0);
2207 if (s == tmp || !(newval >= range_min && newval <= range_max)) {
2208 tmp = s;
2209 while (*tmp && !isspace(*tmp)) tmp++;
2210 exitfileerror(20, "Invalid .%s value of %.*s\n", directive, tmp - s, s);
2213 *val = newval;
2214 return 1;
2217 static void
2218 readsectionhandler(struct config *cfg)
2220 char *line = NULL, *s;
2221 struct handlerinfo *hl;
2222 unsigned char autolevel = 0;
2223 unsigned int stacksize = 0;
2224 int startup = 0;
2225 char priority = 10;
2226 int bootpri = -128;
2227 int has_filesystem = 0;
2229 for (;;)
2231 char *function;
2232 int function_len;
2233 char *tmp;
2235 s = line = readline();
2237 if (s==NULL)
2238 exitfileerror(20, "unexpected end of file in section hanlder\n");
2240 if (strncmp(s, "##", 2)==0)
2241 break;
2243 /* Ignore comments */
2244 if (strncmp(s, "#", 1)==0)
2245 continue;
2247 /* Skip ahead to function name */
2248 while (*s && isspace(*s)) s++;
2250 /* Permit blank lines */
2251 if (!*s)
2252 continue;
2254 if (*s == '.') {
2255 int val;
2256 s++;
2258 if (getdirective(s, "autodetect", 0, 127, &val)) {
2259 autolevel = val;
2260 } else if (getdirective(s, "stacksize", 0, INT_MAX, &val)) {
2261 stacksize = val;
2262 } else if (getdirective(s, "priority", -128, 127, &val)) {
2263 priority = val;
2264 } else if (getdirective(s, "bootpri", -128, 127, &val)) {
2265 bootpri = val;
2266 } else if (getdirective(s, "startup", INT_MIN, INT_MAX, &val)) {
2267 startup = val;
2268 } else {
2269 exitfileerror(20, "Unrecognized directive \"%s\"\n", line);
2271 continue;
2274 do {
2275 unsigned int id = 0;
2277 if (strncasecmp(s,"resident=",9)==0) {
2278 char *res;
2280 s = strchr(s, '=') + 1;
2281 res = s;
2282 while (*s && !isspace(*s)) s++;
2283 if (res == s)
2284 exitfileerror(20, "Empty resident= is not permitted\n");
2286 if (*s)
2287 *(s++) = 0;
2289 hl = newhandler(cfg);
2290 hl->type = HANDLER_RESIDENT;
2291 hl->id = 0;
2292 hl->name = strdup(res);
2293 hl->autodetect = autolevel--;
2294 hl->stacksize = stacksize;
2295 hl->priority = priority;
2296 hl->startup = startup;
2297 } else if (strncasecmp(s,"dosnode=",8)==0) {
2298 char *dev;
2300 s = strchr(s, '=') + 1;
2301 dev = s;
2302 while (*s && !isspace(*s)) s++;
2303 if (dev == s)
2304 exitfileerror(20, "Empty dosnode= is not permitted\n");
2306 if (*s)
2307 *(s++) = 0;
2309 hl = newhandler(cfg);
2310 hl->type = HANDLER_DOSNODE;
2311 hl->id = 0;
2312 hl->name = strdup(dev);
2313 hl->autodetect = autolevel ? autolevel-- : 0;
2314 hl->stacksize = stacksize;
2315 hl->priority = priority;
2316 hl->startup = startup;
2317 hl->bootpri = bootpri;
2318 } else if (strncasecmp(s,"dostype=",8) == 0) {
2319 s = strchr(s, '=') + 1;
2321 id = (unsigned int)strtoul(s, &tmp, 0);
2323 if (s == tmp) {
2324 while (*tmp && !isspace(*tmp))
2325 tmp++;
2326 exitfileerror(20, "\"%.*s\" is not a numerical DOS ID\n", (tmp -s), s);
2328 s = tmp;
2330 if (id == 0 || id == ~0) {
2331 exitfileerror(20, "DOS ID 0x%08x is not permitted\n", id);
2334 hl = newhandler(cfg);
2335 hl->type = HANDLER_DOSTYPE;
2336 hl->id = id;
2337 hl->name = NULL;
2338 hl->autodetect = autolevel ? autolevel-- : 0;
2339 hl->stacksize = stacksize;
2340 hl->priority = priority;
2341 hl->startup = startup;
2342 } else {
2343 for (tmp = s; !isspace(*tmp); tmp++);
2344 exitfileerror(20, "Unknown option \"%.*s\"\n", tmp - s, s);
2347 /* Advance to next ID */
2348 while (*s && isspace(*s)) s++;
2350 } while (*s);
2353 if (s == NULL)
2354 exitfileerror(20, "Unexpected end of file\n");
2356 if (strncmp(s, "##", 2) != 0)
2357 exitfileerror(20, "'##end handler' expected\n");
2358 s += 2;
2360 while (isspace(*s)) s++;
2362 if (strncmp(s, "end", 3) != 0)
2363 exitfileerror(20, "'##end handler' expected\n");
2364 s += 3;
2366 while (isspace(*s)) s++;
2368 if (strncmp(s, "handler", 7) != 0)
2369 exitfileerror(20, "'##end handler' expected\n");
2370 s += 7;
2372 while (isspace(*s)) s++;
2373 if (*s != '\0')
2374 exitfileerror(20, "'##end handler' expected\n");