123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635 |
- /*
- * Copyright 2021
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * These are the four essential freedoms with GNU GPL software:
- * 1: freedom to run the program, for any purpose
- * 2: freedom to study how the program works, and change it to make it do what you wish
- * 3: freedom to redistribute copies to help your Free Software friends
- * 4: freedom to distribute copies of your modified versions to your Free Software friends
- * , ,
- * / \
- * ((__-^^-,-^^-__))
- * `-_---' `---_-'
- * `--|o` 'o|--'
- * \ ` /
- * ): :(
- * :o_o:
- * "-"
- *
- * SPDX-License-Identifier: GPL-3.0+
- * License-Filename: LICENSE
- */
- #include "config.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #include <errno.h>
- #include <math.h>
- #include "splay-tree.h"
- #include "main.h"
- #include "dp.h"
- #include "dpmisc.h"
- #include "dpus.h"
- #include "dot.tab.h"
- #include "dpcolor.h"
- #include "dphl.h"
- #include "dpmem.h"
- /* parse edge arrow style todo:
- * name = aname[anema[anema[aname]]]
- * aname = [mod] shape
- * mod = ['o'] side
- * side = 'l' | 'r'
- * shape = box
- * | crow
- * | curve
- * | icurve
- * | diamond
- * | dot
- * | inv
- * | none
- * | normal
- * | tee
- * | vee
- */
- struct dparrow *dp_getarrow(char *s)
- {
- struct dparrow *ret = NULL;
- int t = 0;
- ret = (struct dparrow *)dp_calloc(1, sizeof(struct dparrow));
- if (s == NULL) {
- ret->pe = 1;
- return (ret);
- }
- if (strlen(s) == 0) {
- ret->pe = 1;
- ret->es = 1;
- return (ret);
- }
- /* the raw name */
- ret->name = dp_uniqstr(s);
- /* parse the basic shapes */
- if (strcasecmp(s, "box") == 0) {
- t = DP_EA_BOX;
- } else if (strcasecmp(s, "crow") == 0) {
- t = DP_EA_CROW;
- } else if (strcasecmp(s, "curve") == 0) {
- t = DP_EA_CURVE;
- } else if (strcasecmp(s, "icurve") == 0) {
- t = DP_EA_ICURVE;
- } else if (strcasecmp(s, "diamond") == 0) {
- t = DP_EA_DIAMOND;
- } else if (strcasecmp(s, "dot") == 0) {
- t = DP_EA_DOT;
- } else if (strcasecmp(s, "inv") == 0) {
- t = DP_EA_INV;
- } else if (strcasecmp(s, "none") == 0) {
- t = DP_EA_NONE;
- } else if (strcasecmp(s, "normal") == 0) {
- t = DP_EA_NORMAL;
- } else if (strcasecmp(s, "tee") == 0) {
- t = DP_EA_TEE;
- } else if (strcasecmp(s, "vee") == 0) {
- t = DP_EA_VEE;
- } else {
- t = DP_EA_UNKNOWN;
- }
- /* todo parse the full string with all modifiers */
- ret->type = t;
- return (ret);
- }
- /* edge arrow direction */
- struct dpdir *dp_getdir(char *s)
- {
- struct dpdir *ret = NULL;
- ret = (struct dpdir *)dp_calloc(1, sizeof(struct dpdir));
- if (s == NULL) {
- ret->pe = 1;
- return (ret);
- }
- if (strlen(s) == 0) {
- ret->pe = 1;
- ret->es = 1;
- return (ret);
- }
- if (strcasecmp(s, "none") == 0) {
- ret->type = DP_DIR_NONE;
- } else if (strcasecmp(s, "both") == 0) {
- ret->type = DP_DIR_BOTH;
- } else if (strcasecmp(s, "back") == 0) {
- ret->type = DP_DIR_BACK;
- } else if (strcasecmp(s, "forward") == 0) {
- ret->type = DP_DIR_FORWARD;
- } else {
- /* unknown is type=0 */
- ret->pe = 1;
- }
- return (ret);
- }
- /* return 0 if str is a number string */
- int dp_chknum(char *str)
- {
- int c = 0;
- char *p = NULL;
- if (str == NULL) {
- return (0);
- }
- if (strlen(str) == 0) {
- return (0);
- }
- c = 0;
- p = str;
- while (*p) {
- switch (*p) {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- case ' ':
- case ',':
- case '.':
- case '-': /* for negative numbers */
- break;
- default:
- c++;
- break;
- }
- if (c) {
- break;
- }
- p++;
- }
- return (c);
- }
- /* return 1 if is a compass point */
- int dp_iscompass(char *compass)
- {
- int yes = 0;
- if (compass == NULL) {
- return (0);
- }
- /* check for valid compass point */
- yes = 1;
- if (strcmp(compass, "n") == 0) {
- } else if (strcmp(compass, "ne") == 0) {
- } else if (strcmp(compass, "e") == 0) {
- } else if (strcmp(compass, "se") == 0) {
- } else if (strcmp(compass, "s") == 0) {
- } else if (strcmp(compass, "sw") == 0) {
- } else if (strcmp(compass, "w") == 0) {
- } else if (strcmp(compass, "nw") == 0) {
- } else if (strcmp(compass, "c") == 0) {
- /* c is center of node */
- } else if (strcmp(compass, "_") == 0) {
- } else {
- /* not valid */
- yes = 0;
- }
- return (yes);
- }
- /* is a known shape name, -1 at error */
- int dp_isdotshape(char *s)
- {
- int ret = -1;
- if (s == NULL) {
- return (-1);
- }
- if (strlen(s) == 0) {
- return (DPSHAPE_ELLIPSE);
- }
- if (strcasecmp("assembly", s) == 0) {
- ret = DPSHAPE_ASSEMBLY;
- } else if (strcasecmp("box", s) == 0) {
- ret = DPSHAPE_BOX;
- } else if (strcasecmp("box3d", s) == 0) {
- ret = DPSHAPE_BOX3D;
- } else if (strcasecmp("cds", s) == 0) {
- ret = DPSHAPE_CDS;
- } else if (strcasecmp("circle", s) == 0) {
- ret = DPSHAPE_CIRCLE;
- } else if (strcasecmp("component", s) == 0) {
- ret = DPSHAPE_COMPONENT;
- } else if (strcasecmp("cylinder", s) == 0) {
- ret = DPSHAPE_CYLINDER;
- } else if (strcasecmp("diamond", s) == 0) {
- ret = DPSHAPE_DIAMOND;
- } else if (strcasecmp("doublecircle", s) == 0) {
- ret = DPSHAPE_DOUBLECIRCLE;
- } else if (strcasecmp("doubleoctagon", s) == 0) {
- ret = DPSHAPE_DOUBLEOCTAGON;
- } else if (strcasecmp("egg", s) == 0) {
- ret = DPSHAPE_EGG;
- } else if (strcasecmp("ellipse", s) == 0) {
- ret = DPSHAPE_ELLIPSE;
- } else if (strcasecmp("fivepoverhang", s) == 0) {
- ret = DPSHAPE_FIVEPOVERHANG;
- } else if (strcasecmp("folder", s) == 0) {
- ret = DPSHAPE_FOLDER;
- } else if (strcasecmp("hexagon", s) == 0) {
- ret = DPSHAPE_HEXAGON;
- } else if (strcasecmp("house", s) == 0) {
- ret = DPSHAPE_HOUSE;
- } else if (strcasecmp("insulator", s) == 0) {
- ret = DPSHAPE_INSULATOR;
- } else if (strcasecmp("invhouse", s) == 0) {
- ret = DPSHAPE_INVHOUSE;
- } else if (strcasecmp("invtrapezium", s) == 0) {
- ret = DPSHAPE_INVTRAPEZIUM;
- } else if (strcasecmp("invtriangle", s) == 0) {
- ret = DPSHAPE_INVTRIANGLE;
- } else if (strcasecmp("larrow", s) == 0) {
- ret = DPSHAPE_LARROW;
- } else if (strcasecmp("lpromoter", s) == 0) {
- ret = DPSHAPE_LPROMOTOR;
- } else if (strcasecmp("Mcircle", s) == 0) {
- ret = DPSHAPE_MCIRCLE;
- } else if (strcasecmp("Mdiamond", s) == 0) {
- ret = DPSHAPE_MDIAMOND;
- } else if (strcasecmp("Msquare", s) == 0) {
- ret = DPSHAPE_MSQUARE;
- } else if (strcasecmp("Mrecord", s) == 0) {
- /* special */
- ret = DPSHAPE_MRECORD;
- } else if (strcasecmp("none", s) == 0) {
- ret = DPSHAPE_NONE;
- } else if (strcasecmp("note", s) == 0) {
- ret = DPSHAPE_NOTE;
- } else if (strcasecmp("noverhang", s) == 0) {
- ret = DPSHAPE_NOVERHANG;
- } else if (strcasecmp("octagon", s) == 0) {
- ret = DPSHAPE_OCTAGON;
- } else if (strcasecmp("oval", s) == 0) {
- ret = DPSHAPE_OVAL;
- } else if (strcasecmp("parallelogram", s) == 0) {
- ret = DPSHAPE_PARALLELOGRAM;
- } else if (strcasecmp("pentagon", s) == 0) {
- ret = DPSHAPE_PENTAGON;
- } else if (strcasecmp("plain", s) == 0) {
- ret = DPSHAPE_PLAIN;
- } else if (strcasecmp("plaintext", s) == 0) {
- ret = DPSHAPE_PLAINTEXT;
- } else if (strcasecmp("point", s) == 0) {
- ret = DPSHAPE_POINT;
- } else if (strcasecmp("polygon", s) == 0) {
- ret = DPSHAPE_POLYGON;
- } else if (strcasecmp("primersite", s) == 0) {
- ret = DPSHAPE_PRIMERSITE;
- } else if (strcasecmp("promoter", s) == 0) {
- ret = DPSHAPE_PROMOTER;
- } else if (strcasecmp("proteasesite", s) == 0) {
- ret = DPSHAPE_PROTEASESITE;
- } else if (strcasecmp("proteinstab", s) == 0) {
- ret = DPSHAPE_PROTEINSTAB;
- } else if (strcasecmp("rarrow", s) == 0) {
- ret = DPSHAPE_RARROW;
- } else if (strcasecmp("record", s) == 0) {
- /* special */
- ret = DPSHAPE_RECORD;
- } else if (strcasecmp("rect", s) == 0) {
- ret = DPSHAPE_RECT;
- } else if (strcasecmp("rectangle", s) == 0) {
- ret = DPSHAPE_RECTANGLE;
- } else if (strcasecmp("restrictionsite", s) == 0) {
- ret = DPSHAPE_RESTRICTIONSITE;
- } else if (strcasecmp("ribosite", s) == 0) {
- ret = DPSHAPE_RIBOSITE;
- } else if (strcasecmp("rnastab", s) == 0) {
- ret = DPSHAPE_RNASTAB;
- } else if (strcasecmp("rpromoter", s) == 0) {
- ret = DPSHAPE_RPROMOTER;
- } else if (strcasecmp("septagon", s) == 0) {
- ret = DPSHAPE_SEPTAGON;
- } else if (strcasecmp("signature", s) == 0) {
- ret = DPSHAPE_SIGNATURE;
- } else if (strcasecmp("square", s) == 0) {
- ret = DPSHAPE_SQUARE;
- } else if (strcasecmp("star", s) == 0) {
- ret = DPSHAPE_STAR;
- } else if (strcasecmp("tab", s) == 0) {
- ret = DPSHAPE_TAB;
- } else if (strcasecmp("terminator", s) == 0) {
- ret = DPSHAPE_TERMINATOR;
- } else if (strcasecmp("threepoverhang", s) == 0) {
- ret = DPSHAPE_THREEPOVERHANG;
- } else if (strcasecmp("trapezium", s) == 0) {
- ret = DPSHAPE_TRAPEZIUM;
- } else if (strcasecmp("triangle", s) == 0) {
- ret = DPSHAPE_TRIANGLE;
- } else if (strcasecmp("tripleoctagon", s) == 0) {
- ret = DPSHAPE_TRIPLEOCTAGON;
- } else if (strcasecmp("underline", s) == 0) {
- ret = DPSHAPE_UNDERLINE;
- } else if (strcasecmp("utr", s) == 0) {
- ret = DPSHAPE_UTR;
- } else {
- /* unknown node shape */
- ret = -1;
- }
- return (ret);
- }
- /* labelloc can be t,b,c, for graph only t,b */
- int dp_islabelloc(char *loc)
- {
- int ret = 0;
- if (loc == NULL) {
- return (DP_LLC);
- }
- /* use default 'c' at "" */
- if (strlen(loc) == 0) {
- return (DP_LLC);
- }
- if (strcasecmp("t", loc) == 0) {
- ret = DP_LLT;
- } else if (strcasecmp("b", loc) == 0) {
- ret = DP_LLB;
- } else if (strcasecmp("c", loc) == 0) {
- ret = DP_LLC;
- } else {
- /* wrong value */
- ret = 0;
- }
- return (ret);
- }
- /* tmp label char */
- static char *clb = NULL;
- static size_t clblen = 0;
- /* parse */
- static struct dppart *dp_2chkrec(struct dpnode *node, int dir)
- {
- struct dppart *nrec = NULL;
- struct dppart *fp = NULL;
- char *s = NULL;
- int c = 0;
- int nf = 0;
- char *rs1 = NULL;
- char *prs = NULL;
- char *prt = NULL;
- char *pprt = NULL;
- int pos1 = 0;
- int maxpos = 0;
- int ndir = 0;
- char *lastport = NULL;
- char *lastfield = NULL;
- int hassub = 0;
- nrec = (struct dppart *)dp_calloc(1, sizeof(struct dppart));
- /* get number of fields now */
- s = clb;
- c = 0;
- nf = 0;
- while (*s) {
- if ((*s) == '\\') {
- s++;
- if ((*s) == 0) {
- break;
- }
- } else {
- if ((*s) == '{') {
- /* increase nesting count */
- c++;
- } else if ((*s) == '}') {
- c--;
- } else if ((*s) == '|') {
- if (c == 0) {
- nf++;
- }
- } else {
- /* nop */
- }
- }
- if (c < 0) {
- break;
- }
- s++;
- }
- /* todo valgrind says memleak and errors in this routine */
- /* tmp fix (nrec->ndpparts + 1) */
- nrec->dir = dir;
- nrec->ndpparts = (nf + 1);
- /* */
- nrec->parts = (struct dppart **)dp_calloc(1, ((nrec->ndpparts + 0) * sizeof(struct dppart *)));
- maxpos = (nf + 1);
- if (yydebug || 0) {
- printf("dot %s(): %d parts with direction %d\n", __func__, nrec->ndpparts, nrec->dir);
- fflush(stdout);
- }
- /* tmp buffer for string and port */
- rs1 = (char *)dp_calloc(1, clblen + 1);
- lastfield = NULL;
- prt = (char *)dp_calloc(1, clblen + 1);
- pprt = prt;
- lastport = NULL;
- pos1 = 0;
- while (*clb) {
- /* get port as in "<portname>" creating string in lastport */
- if ((*clb) == '<') {
- lastport = NULL;
- clb++;
- while (*clb) {
- if (*clb == '>') {
- break;
- }
- if (*clb == '\\') {
- (*pprt) = (*clb);
- pprt++;
- clb++;
- (*pprt) = (*clb);
- pprt++;
- clb++;
- } else {
- (*pprt) = (*clb);
- pprt++;
- clb++;
- }
- }
- lastport = dp_uniqstr(prt);
- memset(prt, 0, clblen + 1);
- pprt = prt;
- if (*clb) {
- clb++;
- }
- } else if ((*clb) == '{') {
- clb++;
- /* toggle direction */
- if (dir == 0) {
- /* vertical */
- ndir = 1;
- } else {
- /* horizontal */
- ndir = 0;
- }
- hassub = 1;
- fp = dp_2chkrec(node, ndir);
- nrec->parts[pos1] = fp;
- pos1++;
- } else if ((*clb) == '}' || (*clb) == '|') {
- fp = (struct dppart *)dp_calloc(1, sizeof(struct dppart));
- /* check for "{aa|bb|}" */
- if (*clb == '}') {
- if (*(clb - 1) == '|') {
- lastfield = dp_uniqstr((char *)" ");
- }
- }
- /* for "|aa", "|", "aa||bb" */
- if (*clb == '|') {
- if (lastfield == NULL) {
- lastfield = dp_uniqstr((char *)" ");
- }
- }
- fp->lp = lastfield;
- fp->id = lastport;
- fp->dir = dir;
- if (lastfield || lastport) {
- fp->hd = 1;
- }
- if (fp->hd || fp->id || fp->lp) {
- if (hassub == 0) {
- if (pos1 >= maxpos) {
- printf("dot %s(): pos1=%d maxpos=%d fixme (1)\n", __func__, pos1, maxpos);
- }
- nrec->parts[pos1] = fp;
- pos1++;
- } else {
- /* nothing if at "...}|..." */
- fp = (struct dppart *)dp_free((void *)fp);
- if (fp) {
- }
- }
- hassub = 0;
- } else {
- fp = (struct dppart *)dp_free((void *)fp);
- if (fp) {
- }
- }
- lastfield = NULL;
- lastport = NULL;
- if ((*clb) == '}') {
- clb++;
- if (rs1) {
- rs1 = (char *)dp_free((void *)rs1);
- if (rs1) {
- }
- }
- if (prt) {
- prt = (char *)dp_free((void *)prt);
- if (prt) {
- }
- }
- return (nrec);
- }
- if (*clb) {
- clb++;
- }
- } else {
- prs = rs1;
- /* copy field part */
- while (*clb) {
- if (*clb == '{' || *clb == '}' || *clb == '|' || *clb == '<' || *clb == '>') {
- /* stop on ctrl char */
- break;
- }
- /* copy to text part */
- if ((*clb) == '\\') {
- (*prs) = (*clb);
- prs++;
- clb++;
- (*prs) = (*clb);
- prs++;
- clb++;
- } else {
- (*prs) = (*clb);
- prs++;
- clb++;
- }
- }
- lastfield = dp_uniqstr(rs1);
- memset(rs1, 0, (clblen + 1));
- }
- }
- /* */
- fp = (struct dppart *)dp_calloc(1, sizeof(struct dppart));
- /* example "aa|bb|" or "aa|bb|<pos>" */
- if ((*(clb - 1) == '|') || (*(clb - 1) == '>')) {
- lastfield = dp_uniqstr((char *)" ");
- fp->hd = 1;
- }
- fp->lp = lastfield;
- if (lastfield || lastport) {
- fp->hd = 1;
- }
- fp->id = lastport;
- fp->dir = dir;
- if (fp->hd || fp->id || fp->lp) {
- if (pos1 >= maxpos) {
- /* this happens normally */
- if (0) {
- printf("dot %s(): pos1=%d maxpos=%d fixme (2) id=\"%s\" lp=\"%s\"\n", __func__, pos1, maxpos, lastfield, lastport);
- }
- fp = (struct dppart *)dp_free((void *)fp);
- if (fp) {
- }
- } else {
- nrec->parts[pos1] = fp;
- }
- } else {
- fp = (struct dppart *)dp_free((void *)fp);
- if (fp) {
- }
- }
- if (rs1) {
- rs1 = (char *)dp_free((void *)rs1);
- if (rs1) {
- }
- }
- if (prt) {
- prt = (char *)dp_free((void *)prt);
- if (prt) {
- }
- }
- return (nrec);
- }
- static void dp_1chkrecpr(struct dppart *info, int ind)
- {
- int i = 0;
- if (info == NULL) {
- printf("dot %s(): nil info\n", __func__);
- return;
- }
- for (i = 0; i < ind; i++) {
- printf(" ");
- }
- printf("%p---begin\n", (void *)info);
- for (i = 0; i < ind; i++) {
- printf("\t");
- }
- printf("`%s' `%s' %d parts dir=%d hd=%d\n", info->id, info->lp, info->ndpparts, info->dir, info->hd);
- for (i = 0; i < info->ndpparts; i++) {
- ind++;
- if (info->parts[i]) {
- dp_1chkrecpr(info->parts[i], (ind + 1));
- }
- ind--;
- }
- for (i = 0; i < ind; i++) {
- printf(" ");
- }
- printf("%p---end\n", (void *)info);
- return;
- }
- /* check 1 node record label */
- static int dp_1chkrec(struct dpnode *node)
- {
- int bo = 0;
- int bc = 0;
- char *p = NULL;
- int status = 0;
- struct dppart *inf = NULL;
- /* check for matching {} in string */
- p = node->label;
- status = 0;
- if (p == NULL) {
- return (0);
- }
- /* if "" */
- if (strlen(p) == 0) {
- /* oke */
- return (0);
- }
- bo = 0;
- bc = 0;
- while ((*p)) {
- if ((*p) == '\\') {
- p++;
- if ((*p) == 0) {
- break;
- }
- } else if (((*p) == '{') || ((*p) == '<')) {
- bo++;
- } else if (((*p) == '}') || ((*p) == '>')) {
- bc++;
- } else { /* nop */
- }
- p++;
- }
- /* check for matching {} and issue error */
- if (bo != bc) {
- /* todo change in parse error */
- memset(dp_errmsg, 0, (256 - 1));
- snprintf(dp_errmsg, (256 - 1),
- "dot %s(): mismatch in record label string in node `%s' at line %d\n", __func__, node->name, node->yylineno);
- return (1);
- }
- /* string is oke, parse rest of format string */
- clb = node->label;
- clblen = strlen(clb);
- inf = dp_2chkrec(node, /* direction hor. */ 0);
- if (yydebug || 0) {
- dp_1chkrecpr(inf, 0);
- }
- fflush(stdout);
- /* tmp label char */
- clb = NULL;
- clblen = 0;
- node->labelinfo = inf;
- return (status);
- }
- /* check nodes with record labels */
- static int dp_chkrec(void)
- {
- int status = 0;
- struct dpnlink *nl = NULL;
- nl = dp_anodes;
- while (nl) {
- status = 0;
- if (nl->n->shape == DPSHAPE_RECORD || nl->n->shape == DPSHAPE_MRECORD) {
- status = dp_1chkrec(nl->n);
- }
- if (status) {
- break;
- }
- nl = nl->next;
- }
- return (status);
- }
- /* check one node html label starting with '<' */
- static int dp1_chkhmln(struct dpnode *node)
- {
- int status = 0;
- status = htmlparse(node);
- if (status) { /*parse error */
- }
- return (status);
- }
- /* check nodes with html labels */
- static int dp_chkhtmln(void)
- {
- int status = 0;
- struct dpnlink *nl = NULL;
- nl = dp_anodes;
- while (nl) {
- if (nl->n->name == NULL) {
- /* shouldnothappen */
- printf("%s(): nil node name for node with label \"%s\"\n", __func__, nl->n->label);
- }
- if (nl->n->label) {
- if (strlen(nl->n->label) > 0) {
- /* non-record shape node with optional <> html label */
- if (nl->n->htmllabel) {
- status = dp1_chkhmln(nl->n);
- }
- if (yydebug || 0) {
- printf("%s(): node \"%s\" html-label=%d ", __func__, nl->n->name, nl->n->htmllabel);
- if (nl->n->htmllabel) {
- if (nl->n->hlinfo) {
- printf("hlinfo=%p mode=%d il=%p tl=%p\n", (void *)nl->n->hlinfo,
- nl->n->hlinfo->mode, (void *)nl->n->hlinfo->il, (void *)nl->n->hlinfo->tl);
- } else {
- printf("hlinfo=nil\n");
- }
- } else {
- printf("\n");
- }
- }
- if (status) {
- break;
- }
- }
- }
- nl = nl->next;
- }
- return (status);
- }
- /* check data, return 0 if oke. */
- int dp_datachk(void)
- {
- int status1 = 0;
- int status2 = 0;
- /* check nodes with record labels */
- status1 = dp_chkrec();
- if (yydebug || 0) {
- printf("%s(): status=%d dp_chkrec();\n", __func__, status1);
- }
- /* check nodes with html labels */
- status2 = dp_chkhtmln();
- if (yydebug || 0) {
- printf("%s(): status=%d dp_chkhtmln();\n", __func__, status2);
- }
- /* check edges with html labels */
- /* todo */
- if (yydebug || 0) {
- printf("%s(): status=%d\n", __func__, status1 + status2);
- }
- return (status1 + status2);
- }
- /* parse number, use default at "" string */
- struct dpnum *dp_getnum(char *s)
- {
- struct dpnum *ret = NULL;
- double n = 0.0;
- char *ep = NULL;
- ret = (struct dpnum *)dp_calloc(1, sizeof(struct dpnum));
- if (s == NULL) {
- /* parse error */
- ret->pe = 1;
- return (ret);
- }
- if (strlen(s) == 0) {
- ret->es = 1;
- /* parse error */
- ret->pe = 1;
- return (ret);
- }
- errno = 0;
- n = strtod(s, &ep);
- if (ep) {
- if (strlen(ep)) {
- /* parse error */
- ret->pe = 1;
- return (ret);
- }
- }
- if (errno) {
- /* parse error */
- ret->pe = 1;
- return (ret);
- }
- /* check for unusual "-0", different languages handle -0 different,
- * go language does not implement -0 correctly
- * if (n==0){}
- */
- if (fabs(n) < 0.01) {
- if (strchr(s, '-')) {
- printf("%s(): number `%s' is negative zero\n", __func__, s);
- }
- }
- ret->number = n;
- ret->pe = 0;
- ret->es = 0;
- return (ret);
- }
- /* parse int number */
- struct dpinum *dp_getinum(char *s)
- {
- struct dpinum *ret = NULL;
- long n = 0;
- char *ep = NULL;
- ret = (struct dpinum *)dp_calloc(1, sizeof(struct dpinum));
- if (s == NULL) {
- /* parse error */
- ret->pe = 1;
- return (ret);
- }
- if (strlen(s) == 0) {
- ret->es = 1;
- /* parse error */
- ret->pe = 1;
- return (ret);
- }
- errno = 0;
- /* base 10 */
- n = strtol(s, &ep, 10);
- if (ep) {
- if (strlen(ep)) {
- /* parse error */
- ret->pe = 1;
- return (ret);
- }
- }
- if (errno) {
- /* parse error */
- ret->pe = 1;
- return (ret);
- }
- /* check for unusual "-0" */
- if (n == 0) {
- if (strchr(s, '-')) {
- printf("%s(): number `%s' is negative zero\n", __func__, s);
- }
- }
- ret->number = (int)n;
- ret->pe = 0;
- ret->es = 0;
- return (ret);
- }
- /* parse boolean
- * dot accepts also " No " and " Yes " with the spaces,
- * this does now accept strings as " true" or " 1 "
- */
- struct dpbool *dp_getbool(char *s)
- {
- struct dpbool *ret = NULL;
- char *s2 = NULL;
- char *p = NULL;
- char *q = NULL;
- ret = (struct dpbool *)dp_calloc(1, sizeof(struct dpbool));
- if (s == NULL) {
- /* parse error */
- ret->pe = 1;
- return (ret);
- }
- if (strlen(s) == 0) {
- /* parse error */
- ret->es = 1;
- ret->pe = 1;
- return (ret);
- }
- s2 = (char *)dp_calloc(1, (strlen(s) + 1));
- /* skip leading spaces */
- p = s;
- while (*p) {
- if (*p != ' ') {
- break;
- }
- p++;
- }
- /* copy chars until trailing spaces */
- q = s2;
- while (*p) {
- if (*p == ' ') {
- break;
- }
- *q = *p;
- p++;
- q++;
- }
- if (strcasecmp(s2, "true") == 0) {
- ret->number = 1;
- } else if (strcasecmp(s2, "false") == 0) {
- ret->number = 0;
- } else if (strcasecmp(s2, "1") == 0) {
- ret->number = 1;
- } else if (strcasecmp(s2, "0") == 0) {
- ret->number = 0;
- } else if (strcasecmp(s2, "on") == 0) {
- ret->number = 1;
- } else if (strcasecmp(s2, "off") == 0) {
- ret->number = 0;
- } else if (strcasecmp(s2, "yes") == 0) {
- ret->number = 1;
- } else if (strcasecmp(s2, "no") == 0) {
- ret->number = 0;
- } else {
- /* parse error */
- ret->pe = 1;
- }
- s2 = (char *)dp_free((void *)s2);
- if (s2) {
- }
- return (ret);
- }
- /* parse color */
- struct dpcolor *dp_getcolor(int cs, int csnum, char *s)
- {
- struct dpcolor *ret = NULL;
- int tmpi = 0;
- ret = (struct dpcolor *)dp_calloc(1, sizeof(struct dpcolor));
- if (s == NULL) {
- ret->pe = 1;
- return (ret);
- }
- if (strlen(s) == 0) {
- ret->pe = 1;
- ret->es = 1;
- return (ret);
- }
- if (cs) {
- tmpi = dp_findcolor(csnum, s);
- } else {
- tmpi = dp_findcolor(0, s);
- }
- if (tmpi == -1) {
- ret->pe = 1;
- return (ret);
- }
- if (tmpi == -2) {
- ret->islist = 1;
- ret->pe = 1;
- return (ret);
- }
- if (tmpi == -3) {
- ret->pe = 1;
- ret->es = 1;
- return (ret);
- }
- ret->color = tmpi;
- return (ret);
- }
- /* parse style= */
- struct dpstyle *dp_getstyle(char *s)
- {
- struct dpstyle *ret = NULL;
- size_t lens = 0;
- char *buf = NULL;
- char *sep = NULL;
- char *token = NULL;
- int slwnext = 0;
- double n = 0.0;
- char *ep = NULL;
- ret = (struct dpstyle *)dp_calloc(1, sizeof(struct dpstyle));
- if (s == NULL) {
- ret->pe = 1;
- ret->es = 1;
- return (ret);
- }
- lens = strlen(s);
- if (lens == 0) {
- ret->pe = 1;
- ret->es = 1;
- return (ret);
- }
- /* copy string in tmp buffer */
- buf = (char *)dp_calloc(1, (lens + 1));
- strcpy(buf, s);
- /* possible seperators in string */
- sep = dp_uniqstr((char *)",() ");
- slwnext = 0;
- /* strsep() is a replacement for old strtok() */
- token = strtok(buf, sep);
- while (token) {
- if (0) {
- printf("dot %s(): token `%s'\n", __func__, token);
- }
- if (slwnext) {
- /* expect number for setlinewidth */
- errno = 0;
- n = strtod(token, &ep);
- if (ep) {
- if (strlen(ep)) {
- /* parse error on number */
- ret->pe = 1;
- ret->pe_slw = 1;
- break;
- }
- }
- if (errno) {
- /* parse error on number */
- ret->pe = 1;
- ret->pe_slw = 1;
- break;
- }
- if (n < 0) {
- /* parse error on number */
- ret->pe = 1;
- ret->pe_slw = 1;
- break;
- }
- ret->slwset = 1;
- ret->slw = n;
- slwnext = 0;
- } else {
- if (strcasecmp(token, "dashed") == 0) {
- ret->dashed = 1;
- } else if (strcasecmp(token, "dotted") == 0) {
- ret->dotted = 1;
- } else if (strcasecmp(token, "solid") == 0) {
- ret->solid = 1;
- } else if (strcasecmp(token, "invis") == 0) {
- ret->invis = 1;
- } else if (strcasecmp(token, "invisible") == 0) {
- ret->invis = 1;
- } else if (strcasecmp(token, "bold") == 0) {
- ret->bold = 1;
- } else if (strcasecmp(token, "tapered") == 0) {
- ret->tapered = 1;
- } else if (strcasecmp(token, "filled") == 0) {
- ret->filled = 1;
- } else if (strcasecmp(token, "striped") == 0) {
- ret->striped = 1;
- } else if (strcasecmp(token, "wedged") == 0) {
- ret->wedged = 1;
- } else if (strcasecmp(token, "diagonals") == 0) {
- ret->diagonals = 1;
- } else if (strcasecmp(token, "rounded") == 0) {
- ret->rounded = 1;
- } else if (strcasecmp(token, "radial") == 0) {
- ret->radial = 1;
- } else if (strncmp(token, "setlinewidth", 12) == 0) {
- printf("dot %s(): setlinewidth is depreciated, use penwidth instead\n", __func__);
- /* next token must be the number */
- slwnext = 1;
- } else {
- /* something else */
- ret->unknown = dp_uniqstr(token);
- ret->pe = 1;
- ret->pe_unk = 1;
- break;
- }
- }
- token = strtok(NULL, sep);
- }
- /* check for missing number for setlinewidth as last item */
- if (slwnext) {
- ret->pe_exp = 1;
- ret->pe = 1;
- }
- buf = (char *)dp_free((void *)buf);
- if (buf) {
- }
- return (ret);
- }
- /* point (x,y) with optional '!' flag */
- /* neato: if dim=3 is set point can also be %f,%f,%f for (x,y,z) */
- struct dppoint *dp_getpoint(char *s)
- {
- struct dppoint *ret = NULL;
- char *str = NULL;
- int f = 0;
- int n = 0;
- float x = 0.0;
- float y = 0.0;
- ret = (struct dppoint *)dp_calloc(1, sizeof(struct dppoint));
- if (s == NULL) {
- ret->pe = 1;
- return (ret);
- }
- if (strlen(s) == 0) {
- ret->pe = 1;
- return (ret);
- }
- str = (char *)dp_calloc(1, (strlen(s) + 1));
- strcpy(str, s);
- /* if last char is '!' set the flag and erase the '!' from the string */
- if (str[strlen(s) - 1] == '!') {
- f = 1;
- str[strlen(s) - 1] = 0;
- } else {
- f = 0;
- }
- /* in neato data and dim=3 it can be x,y,z */
- errno = 0;
- if (strchr(str, ',')) {
- n = sscanf(str, "%f,%f", &x, &y);
- } else {
- n = sscanf(str, "%f %f", &x, &y);
- }
- if (n != 2 || errno != 0) {
- ret->pe = 1;
- } else {
- ret->flag = f;
- ret->x = x;
- ret->y = y;
- }
- str = (char *)dp_free((void *)str);
- if (str) {
- }
- return (ret);
- }
- /* get rect %f,%f,%f,%f */
- struct dprect *dp_getrect(char *s)
- {
- struct dprect *ret = NULL;
- float x0 = 0;
- float y0 = 0;
- float x1 = 0;
- float y1 = 0;
- int n = 0;
- ret = (struct dprect *)dp_calloc(1, sizeof(struct dprect));
- if (strlen(s) == 0) {
- ret->pe = 1;
- ret->es = 1;
- return (ret);
- }
- errno = 0;
- /* try ',' or ' ' as sep. char */
- if (strchr(s, ',')) {
- n = sscanf(s, "%f,%f,%f,%f", &x0, &y0, &x1, &y1);
- } else {
- /* assume sep. char is a ' ' space */
- n = sscanf(s, "%f %f %f %f", &x0, &y0, &x1, &y1);
- }
- if (n != 4 || errno != 0) {
- /* some parse error */
- ret->pe = 1;
- } else {
- /* oke */
- ret->x0 = x0;
- ret->y0 = y0;
- ret->x1 = x1;
- ret->y1 = y1;
- }
- return (ret);
- }
- /* clusterrank value */
- int dp_getclrank(char *s)
- {
- int ret = 0;
- if (s == NULL) {
- return (0);
- }
- if (strlen(s) == 0) {
- return (0);
- }
- if (strcasecmp(s, "local") == 0) {
- ret = 0;
- } else if (strcasecmp(s, "global") == 0) {
- ret = 1;
- } else if (strcasecmp(s, "none") == 0) {
- ret = 2;
- } else {
- ret = -1;
- }
- return (ret);
- }
- /* margin %f,%f or %f */
- struct dpmargin *dp_getmargin(char *s)
- {
- struct dpmargin *ret = NULL;
- float x = 0;
- float y = 0;
- int n = 0;
- ret = (struct dpmargin *)dp_calloc(1, sizeof(struct dpmargin));
- if (strlen(s) == 0) {
- ret->pe = 1;
- ret->es = 1;
- return (ret);
- }
- errno = 0;
- /* try ',' or ' ' as sep. char */
- if (strchr(s, ',')) {
- n = sscanf(s, "%f,%f", &x, &y);
- } else {
- n = sscanf(s, "%f %f", &x, &y);
- }
- if (errno) {
- ret->pe = 1;
- } else {
- if (n == 1) {
- ret->x = x;
- ret->y = x;
- } else if (n == 2) {
- ret->x = x;
- ret->y = y;
- } else {
- ret->pe = 1;
- }
- }
- return (ret);
- }
- /* outputorder */
- struct dpoo *dp_getoo(char *s)
- {
- struct dpoo *ret = NULL;
- ret = (struct dpoo *)dp_calloc(1, sizeof(struct dpoo));
- if (s == NULL) {
- ret->pe = 1;
- return (ret);
- }
- if (strlen(s) == 0) {
- ret->pe = 1;
- ret->es = 1;
- return (ret);
- }
- if (strcasecmp(s, "breadthfirst") == 0) {
- ret->mode = DP_OO_BF;
- } else if (strcasecmp(s, "nodesfirst") == 0) {
- ret->mode = DP_OO_NF;
- } else if (strcasecmp(s, "edgesfirst") == 0) {
- ret->mode = DP_OO_EF;
- } else {
- ret->pe = 1;
- }
- return (ret);
- }
- /* rank */
- struct dprank *dp_getrank(char *s)
- {
- struct dprank *ret = NULL;
- ret = (struct dprank *)dp_calloc(1, sizeof(struct dprank));
- if (s == NULL) {
- ret->pe = 1;
- return (ret);
- }
- if (strlen(s) == 0) {
- ret->pe = 1;
- ret->es = 1;
- return (ret);
- }
- /* added: rank=none */
- if (strcasecmp(s, "same") == 0) {
- ret->mode = DP_RANK_SAME;
- } else if (strcasecmp(s, "none") == 0) {
- ret->mode = DP_RANK_NONE;
- } else if (strcasecmp(s, "min") == 0) {
- ret->mode = DP_RANK_MIN;
- } else if (strcasecmp(s, "source") == 0) {
- ret->mode = DP_RANK_SRC;
- } else if (strcasecmp(s, "max") == 0) {
- ret->mode = DP_RANK_MAX;
- } else if (strcasecmp(s, "sink") == 0) {
- ret->mode = DP_RANK_SINK;
- } else {
- ret->pe = 1;
- }
- return (ret);
- }
- /* rankdir */
- struct dprankdir *dp_getrankdir(char *s)
- {
- struct dprankdir *ret = NULL;
- ret = (struct dprankdir *)dp_calloc(1, sizeof(struct dprankdir));
- if (s == NULL) {
- ret->pe = 1;
- return (ret);
- }
- if (strlen(s) == 0) {
- ret->pe = 1;
- ret->es = 1;
- return (ret);
- }
- if (strcasecmp(s, "tb") == 0) {
- ret->mode = DP_RANKDIR_TB;
- } else if (strcasecmp(s, "lr") == 0) {
- ret->mode = DP_RANKDIR_LR;
- } else if (strcasecmp(s, "bt") == 0) {
- ret->mode = DP_RANKDIR_BT;
- } else if (strcasecmp(s, "rl") == 0) {
- ret->mode = DP_RANKDIR_RL;
- } else {
- ret->pe = 1;
- }
- return (ret);
- }
- /* ranksep */
- struct dpranksep *dp_getranksep(char *s)
- {
- struct dpranksep *ret = NULL;
- float f = 0;
- int n = 0;
- ret = (struct dpranksep *)dp_calloc(1, sizeof(struct dpranksep));
- if (s == NULL) {
- ret->pe = 1;
- return (ret);
- }
- if (strlen(s) == 0) {
- ret->pe = 1;
- ret->es = 1;
- return (ret);
- }
- errno = 0;
- n = sscanf(s, "%f", &f);
- if (errno) {
- ret->pe = 1;
- return (ret);
- }
- if (n == 1) {
- ret->number = (double)f;
- return (ret);
- }
- errno = 0;
- n = sscanf(s, "%f equally", &f);
- if (errno) {
- ret->pe = 1;
- return (ret);
- }
- if (n == 1) {
- ret->number = (double)f;
- ret->eq = 1;
- return (ret);
- }
- ret->pe = 1;
- return (ret);
- }
- /* ratio */
- struct dpratio *dp_getratio(char *s)
- {
- struct dpratio *ret = NULL;
- float f = 0;
- int n = 0;
- ret = (struct dpratio *)dp_calloc(1, sizeof(struct dpratio));
- if (s == NULL) {
- ret->pe = 1;
- return (ret);
- }
- if (strlen(s) == 0) {
- ret->es = 1;
- ret->pe = 1;
- return (ret);
- }
- if (strcasecmp(s, "fill") == 0) {
- ret->mode = DP_RATIO_FILL;
- } else if (strcasecmp(s, "compress") == 0) {
- ret->mode = DP_RATIO_COM;
- } else if (strcasecmp(s, "expand") == 0) {
- ret->mode = DP_RATIO_EXP;
- } else if (strcasecmp(s, "auto") == 0) {
- ret->mode = DP_RATIO_AUTO;
- } else {
- ret->mode = 0;
- }
- if (ret->mode) {
- return (ret);
- }
- errno = 0;
- n = sscanf(s, "%f", &f);
- if (errno) {
- ret->pe = 1;
- return (ret);
- }
- if (n == 1) {
- ret->number = f;
- } else {
- ret->pe = 1;
- }
- return (ret);
- }
- /* splines */
- struct dpsplines *dp_getsplines(char *s)
- {
- struct dpsplines *ret = NULL;
- ret = (struct dpsplines *)dp_calloc(1, sizeof(struct dpsplines));
- if (s == NULL) {
- ret->pe = 1;
- return (ret);
- }
- if (strlen(s) == 0) {
- ret->mode = DP_SPLINE_NONE;
- return (ret);
- }
- if (strcasecmp(s, "none") == 0) {
- ret->mode = DP_SPLINE_NONE;
- } else if (strcasecmp(s, "line") == 0) {
- ret->mode = DP_SPLINE_LINE;
- } else if (strcasecmp(s, "false") == 0) {
- ret->mode = DP_SPLINE_LINE;
- } else if (strcasecmp(s, "polyline") == 0) {
- ret->mode = DP_SPLINE_POLY;
- } else if (strcasecmp(s, "curved") == 0) {
- ret->mode = DP_SPLINE_CURVED;
- } else if (strcasecmp(s, "ortho") == 0) {
- ret->mode = DP_SPLINE_ORTHO;
- } else if (strcasecmp(s, "spline") == 0) {
- ret->mode = DP_SPLINE_SPLINE;
- } else if (strcasecmp(s, "true") == 0) {
- ret->mode = DP_SPLINE_SPLINE;
- } else {
- ret->pe = 1;
- }
- return (ret);
- }
- /* end */
|