sty-genhead.c (24566B)
1 /* Copyright (C) 2017-2026 |Méso|Star> (contact@meso-star.com) 2 * 3 * This file is part Star-Typesetting. 4 * 5 * Star-Typesetting is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * Star-Typesetting is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with Star-Typesetting. If not, see <http://www.gnu.org/licenses/>. */ 17 18 #define _XOPEN_SOURCE 500 /* realpath */ 19 #define _POSIX_C_SOURCE 200809L /* stndup */ 20 21 #include <assert.h> 22 #include <errno.h> 23 #include <libgen.h> /* basename & dirname */ 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <unistd.h> 28 29 #include <sys/stat.h> 30 31 #define MENU_FILENAME "menu.tsv" /* File that lists the menu entries */ 32 #define MENU_MAX_ENTRIES 16 /* Maximum number of menu entries */ 33 34 #define INDEX_FILENAME "index.tsv" /* List the indexed content of a section */ 35 #define INDEX_MAX_ENTRIES 32 /* Maximum number of indexed items per section */ 36 37 #define DEFAULT_LANG "en" /* Default language */ 38 39 static const char LANG[] = "@LANG@"; 40 const size_t SZLANG = sizeof(LANG) - 1/* '\0' */; 41 42 static const char* g_cmd = NULL; /* Command name. Setuped on start-up */ 43 44 struct menu { 45 struct mentry { 46 char* label; 47 char* section; 48 char* mem__; /* Allocated memory */ 49 } items[MENU_MAX_ENTRIES]; 50 51 int nitems; 52 }; 53 static const struct menu MENU_NULL = {0}; 54 55 struct index { 56 struct ientry { 57 char* label; 58 char* uri; 59 char* lang; 60 char* template; /* Original URI, i.e. not expanded */ 61 char* lang_list; /* List of available languages */ 62 char* mem__; /* Allocated memory */ 63 } items[INDEX_MAX_ENTRIES]; 64 65 int nitems; 66 }; 67 static const struct index INDEX_NULL = {0}; 68 69 struct str { 70 char* buf; 71 size_t sz; /* Capacity */ 72 }; 73 static const struct str STR_NULL = {0}; 74 75 /******************************************************************************* 76 * Helper functions 77 ******************************************************************************/ 78 static void 79 str_release(struct str* s) 80 { 81 assert(s); 82 if(s->buf) free(s->buf); 83 *s = STR_NULL; 84 } 85 86 /* Read from fp the next non empty line. Returns null if an error occurs */ 87 static char* 88 rline(FILE* fp, struct str* s, int* lines_count/*#read lines*/) 89 { 90 size_t nlines = 0; 91 92 do { 93 size_t n = 0; /* #read chars for the current line */ 94 95 do { 96 char *ptr = NULL; 97 98 /* Allocate memory space */ 99 if(!s->buf) s->buf = realloc(s->buf, s->sz=128); 100 else if(n > 0) s->buf = realloc(s->buf, s->sz+=32); 101 if(!s->buf) { perror("realloc"); goto error; } 102 103 /* Read line data */ 104 ptr = fgets(s->buf+n, s->sz - n, fp); 105 if(!ptr) { 106 if(ferror(fp)) { perror("fgets"); goto error; } 107 s->buf[n] = '\0'; /* No read char */ 108 } 109 110 n = strlen(s->buf); /* Update the number of read chars */ 111 112 } while(!strrchr(s->buf, '\n') && !feof(fp)); /* Are there any chars left? */ 113 ++nlines; 114 115 s->buf[strcspn(s->buf, "\n\r#\0")] = '\0'; /* Remove new line and comments */ 116 117 } while(strlen(s->buf)==strspn(s->buf, " \t") && !feof(fp)); /* Empty line */ 118 119 exit: 120 *lines_count = nlines; 121 return s->buf; 122 error: 123 str_release(s); 124 goto exit; 125 } 126 127 static void 128 menu_release(struct menu* menu) 129 { 130 assert(menu); 131 for(int i=0; i < menu->nitems; ++i) { 132 if(menu->items[i].mem__) free(menu->items[i].mem__); 133 } 134 *menu = MENU_NULL; 135 } 136 137 static int 138 menu_load(struct menu* menu, const char* workdir) 139 { 140 struct str s = STR_NULL; 141 char* line = NULL; 142 FILE* fp = NULL; 143 int iline = 0; /* Line index */ 144 int n = 0; 145 int err = 0; 146 147 assert(menu && workdir); 148 149 *menu = MENU_NULL; 150 151 /* Open the menu file */ 152 if(!(fp=fopen(MENU_FILENAME, "r"))) { 153 perror(__func__); 154 fprintf(stderr, 155 "%s: expecting the \"%s\" file in the \"%s\" working directory\n", 156 g_cmd, MENU_FILENAME, workdir); 157 goto error; 158 } 159 160 for(line=rline(fp, &s, &n); line && line[0]!='\0'; line=rline(fp, &s, &n)) { 161 struct mentry* item = menu->items + menu->nitems; 162 char* ctx = NULL; 163 164 iline += n; 165 166 if(menu->nitems >= MENU_MAX_ENTRIES) { 167 /* There is too many menu entries to parse */ 168 errno = ENOMEM; 169 perror(__func__); 170 goto error; 171 } 172 173 ++menu->nitems; 174 175 /* Duplicate the line. It will be divided into sub-strings that the 176 * items's member variables will point to. */ 177 if(!(item->mem__ = strdup(line))) { perror("strdup"); goto error; } 178 179 /* Setup item member variables */ 180 if(!(item->label = strtok_r(item->mem__, "\t", &ctx)) 181 || !(item->section = strtok_r(NULL, "\t", &ctx))) { 182 fprintf(stderr, "%s:%s:%d: invalid menu entry -- %s\n", 183 g_cmd, MENU_FILENAME, iline, line); 184 goto error; 185 } 186 } 187 188 if(!line) goto error; /* A null lines means there is an error */ 189 190 exit: 191 if(fp) fclose(fp); 192 str_release(&s); 193 return err; 194 error: 195 menu_release(menu); 196 err = 1; 197 goto exit; 198 } 199 200 /* Resolve a pathname. Unlike realpath, handles paths to non-existent files */ 201 static char* 202 get_realpath(const char* path) 203 { 204 char* out = NULL; /* realpath */ 205 char* buf = NULL; /* working copy of input path */ 206 char* tmp = NULL; /* temporary path */ 207 char *p0, *p1; 208 209 if((out = realpath(path, NULL))) goto exit; 210 if(errno != ENOENT) goto error; 211 212 /* The path does not name an existing file */ 213 214 /* Ensure that path is absolute or start by "./" or "../" */ 215 if(path[0] == '/' || !strncmp(path, "./", 2) || !strncmp(path, "../", 3)) { 216 if(!(buf = strdup(path))) goto error; 217 } else { 218 if(!(buf = malloc(2/*"./"*/ + strlen(path) + 1/*'\0'*/))) goto error; 219 strcpy(buf, "./"); 220 strcat(buf, path); 221 } 222 223 /* Remove path components until a path is found that exists */ 224 for(p0=strrchr(buf, '/'); p0; p1=p0, *p1='\0', p0=strrchr(buf, '/'), *p1='/') { 225 226 *p0 = '\0'; 227 tmp = realpath(buf, NULL); 228 *p0 = '/'; 229 230 if(!tmp) continue; /* Keep removing path component */ 231 232 /* Concatante the existing path with the remaining compopnents */ 233 if(!(out = malloc(strlen(tmp) + strlen(p0) + 1/*'\0'*/))) goto error; 234 strcpy(out, tmp); 235 strcat(out, p0); 236 break; 237 } 238 239 exit: 240 if(buf) free(buf); 241 if(tmp) free(tmp); 242 return out; 243 error: 244 perror(__func__); 245 if(out) { free(out); out = NULL; } 246 goto exit; 247 } 248 249 /* Return the relative path from path to workdir. 250 * Path must lie in workdir. 251 * Return null if an error occurs */ 252 static char* 253 get_relpath(const char* workdir, const char* path) 254 { 255 const size_t sz = strlen(workdir); 256 const char* ptr = NULL; 257 char* buf = NULL; 258 int n = 0; /* Number of directory to travels */ 259 int i; 260 261 if(strncmp(workdir, path, sz) || path[sz] != '/') { 262 fprintf(stderr, "%s:%s: %s is not a subpath of %s\n", 263 g_cmd, __func__, path, workdir); 264 goto error; 265 } 266 267 /* Count the number of directories between the workdir and the path */ 268 for(n = 0, ptr=path + sz + 1; (ptr=strchr(ptr, '/')); ++n) { 269 /* Successive '/' should be counted as a single '/' */ 270 while(++ptr, *ptr=='/'); 271 } 272 273 if(!(buf = malloc(n*3/*"../"*/+1/*'\0'*/))) { perror("malloc"); goto error; } 274 for(i=0, buf[0]='\0'; i<n; strcat(buf, "../"), ++i); 275 276 exit: 277 return buf; 278 error: 279 if(buf) { free(buf); buf = NULL; } 280 goto exit; 281 } 282 283 /* Returns a pointer to a new index file in which the @LANG@ macro is resolved. 284 * Each entry in the original index file containing an @LANG@ macro is 285 * duplicated as many times as there are languages listed for that entry. In 286 * each of them, the @LANG@ macro is replaced by a value from the language in 287 * the order in which the languages are defined in the original index file. Then 288 * follow the language used to expand the @LANG@ macro and finally the 289 * original uri and the list of languages. 290 * 291 * For example, the following original string : 292 * 293 * Index\tindex-@LANG@.html\tfr:en\n 294 * 295 * is resolved in 296 * 297 * Index\tindex-fr.html\tfr\tindex-@LANG@.html\tfr:en\n 298 * Index\tindex-en.html\ten\tindex-@LANG@.html\tfr:en\n 299 * 300 * Return null if an error occurs */ 301 static FILE* 302 resolve_index(const char* path) 303 { 304 char* lang_list = NULL; 305 306 FILE* fp = NULL; 307 FILE* fp2 = NULL; 308 309 struct str s = STR_NULL; 310 char* line = NULL; 311 int iline = 0; /* Line index */ 312 int n = 0; 313 314 assert(path); 315 316 /* Open the original index */ 317 if(!(fp = fopen(path, "r"))) { 318 fprintf(stderr,"%s: unable to find the file %s\n", g_cmd, path); 319 goto error; 320 } 321 322 /* Open the resolved index */ 323 if(!(fp2 = tmpfile())) { perror("tmpfile"); goto error; } 324 325 for(line=rline(fp, &s, &n); line && line[0]!='\0'; line=rline(fp, &s, &n)) { 326 char *label, *uri, *langs, *ctx; 327 328 iline += n; 329 330 label = strtok_r(line, "\t", &ctx); 331 uri = strtok_r(NULL, "\t", &ctx); 332 langs = strtok_r(NULL, "\t", &ctx); 333 334 if(!label || !uri) { 335 fprintf(stderr, "%s:%s:%d: invalid index entry\n", g_cmd, path, iline); 336 goto error; 337 } 338 339 if(!langs) { 340 /* There is no several languages, just write the original entry */ 341 fprintf(fp2, "%s\t%s\n", label, uri); 342 343 } else { 344 char* l = NULL; 345 346 /* Keep a copy of the string listing the available languages. This will be 347 * the last field of the expanded entries */ 348 if(lang_list) free(lang_list); 349 if(!(lang_list=strdup(langs))) { perror("strdup"); goto error; } 350 351 /* Add as many index entries as there are languages available */ 352 for(l=strtok_r(langs, ":", &ctx); l; l=strtok_r(NULL, ":", &ctx)) { 353 char *ptr0, *ptr1; 354 355 fprintf(fp2, "%s\t", label); /* Firstly, print the entry label */ 356 357 /* Then print the expanded URI, i.e. the URI in which each instance of 358 * the @LANG@ macro is replaced by the current language */ 359 for(ptr0=uri; (ptr1 = strstr(ptr0, LANG)); ptr0=ptr1+SZLANG) { 360 *ptr1 = '\0'; 361 fprintf(fp2, "%s%s", ptr0, l); 362 *ptr1 = LANG[0]; 363 } 364 fprintf(fp2, "%s\t", ptr0); /* Rest of the URI */ 365 366 /* Print the URI language as the 3rd field */ 367 fprintf(fp2, "%s\t", l); 368 369 /* And finally, print the original URI (4th field) and the list of 370 * available languages (5th field). */ 371 fprintf(fp2, "%s\t%s\n", uri, lang_list); 372 } 373 } 374 } 375 376 rewind(fp2); 377 378 exit: 379 str_release(&s); 380 if(fp) fclose(fp); 381 if(lang_list) free(lang_list); 382 return fp2; 383 error: 384 if(fp2) { fclose(fp2); fp2 = NULL; } 385 goto exit; 386 } 387 388 static void 389 index_release(struct index* index) 390 { 391 assert(index); 392 for(int i=0; i < index->nitems; ++i) { 393 if(index->items[i].mem__) free(index->items[i].mem__); 394 } 395 *index = INDEX_NULL; 396 } 397 398 static int 399 index_load(struct index* index, const char* section) 400 { 401 struct str s = STR_NULL; 402 FILE* fp = NULL; 403 char* path = NULL; 404 char* line = NULL; 405 size_t sz = 0; 406 int i = 0; 407 int n = 0; 408 int err = 0; 409 410 assert(index && section); 411 412 /* Allocate the string to store the absolute path to the index file */ 413 sz = 2 /* "./" */ 414 + strlen(section) + 1/* '/' */ 415 + strlen(INDEX_FILENAME) + 1/* '\0' */; 416 if(!(path = malloc(sz))) { perror("malloc"); goto error; } 417 418 /* Define the absolute path to the index file */ 419 i = snprintf(path, sz, "./%s/%s", section, INDEX_FILENAME); 420 if(i >= (int)sz) abort(); /* Unexpected error */ 421 422 /* Resolve the index file, i.e. expand the @LANG@ macro */ 423 if(!(fp = resolve_index(path))) goto error; 424 425 for(line=rline(fp, &s, &n); line && line[0]!='\0'; line=rline(fp, &s, &n)) { 426 struct ientry* item = index->items + index->nitems; 427 char* ctx = NULL; 428 429 if(index->nitems >= INDEX_MAX_ENTRIES) { 430 /* There is too many menu entries to parse */ 431 errno = ENOMEM; 432 perror(__func__); 433 goto error; 434 } 435 436 ++index->nitems; 437 438 /* Duplicate the line. It will be divided into sub-strings that the 439 * items's member variables will point to. */ 440 if(!(item->mem__ = strdup(line))) { perror("strdup"); goto error; } 441 442 item->label = strtok_r(item->mem__, "\t", &ctx); 443 item->uri = strtok_r(NULL, "\t", &ctx); 444 item->lang = strtok_r(NULL, "\t", &ctx); 445 item->template = strtok_r(NULL, "\t", &ctx); 446 item->lang_list = strtok_r(NULL, "\t", &ctx); 447 448 if(!item->lang) item->lang = DEFAULT_LANG; 449 if(!item->template) item->template = item->uri; 450 if(!item->lang_list) item->lang_list = DEFAULT_LANG; 451 452 /* Already check when resolving index */ 453 assert(item->label && item->uri); 454 } 455 456 if(!line) goto error; /* A null lines means there is an error */ 457 if(index->nitems == 0) goto error; /* An empty index is an error */ 458 459 exit: 460 if(path) free(path); 461 if(fp) fclose(fp); 462 str_release(&s); 463 return err; 464 error: 465 index_release(index); 466 err = 1; 467 goto exit; 468 } 469 470 /* Returns null if no entry is found */ 471 const struct mentry* 472 menu_find(const struct menu* menu, const char* section) 473 { 474 int i = 0; 475 assert(menu && section); 476 477 for(i = 0; i < menu->nitems; ++i) { 478 if(!strcmp(menu->items[i].section, section)) break; 479 } 480 481 return i < menu->nitems ? menu->items + i : NULL; 482 } 483 484 /* Returns null if no entry is found */ 485 const struct ientry* 486 index_find 487 (const struct index* index, 488 const char* file) /* relative to the section */ 489 { 490 int i = 0; 491 assert(index && file); 492 493 for(i = 0; i < index->nitems; ++i) { 494 if(!strcmp(index->items[i].uri, file)) break; 495 } 496 497 return i < index->nitems ? index->items + i : NULL; 498 } 499 500 /* Return null if an error occurs */ 501 static char* 502 get_section(const char* workdir, const char* path) 503 { 504 const size_t sz = strlen(workdir); 505 char *buf; 506 const char *b, *e; 507 508 assert(workdir); 509 510 if(strncmp(path, workdir, sz) != 0 || path[sz] != '/') { 511 fprintf(stderr, "%s:%s: %s is not a subpath of %s\n", 512 g_cmd, __func__, path, workdir); 513 return NULL; 514 } 515 516 buf = (b = path+sz, *b != '/') || !(e = strchr(++b, '/')) 517 ? strdup(b) : strndup(b, e-b); 518 519 if(!buf) perror(__func__); 520 return buf; 521 } 522 523 /* Return path relative to workdir/section. path must be a subpath into 524 * workdir/section. Return null if an error occurs */ 525 static char* 526 get_section_path(const char* workdir, const char* section, const char* path) 527 { 528 const size_t sz0 = strlen(workdir); 529 const size_t sz1 = strlen(section); 530 char* buf; 531 532 if(strncmp(path, workdir, sz0) != 0 533 || strncmp(path+sz0+1, section, sz1) != 0 534 || path[sz0] != '/' 535 || path[sz0+1+sz1] != '/') { 536 fprintf(stderr, "%s:%s: %s is not a subpath in %s/%s\n", 537 g_cmd, __func__, path, workdir, section); 538 return NULL; 539 } 540 541 buf = strdup(path + sz0 + 1/*'/ */ + sz1 + 1/*'/'*/); 542 if(!buf) perror("strdup"); 543 544 return buf; 545 } 546 547 /* Return null if an error occurs */ 548 static char* 549 get_workdir(void) 550 { 551 char *buf, *ptr; 552 size_t sz = 256; /* Initial buffer size */ 553 554 for(buf = ptr = NULL; !ptr; sz*=2) { 555 556 if(!(buf = realloc(buf, sz))) { 557 perror("realloc"); 558 goto error; 559 } 560 561 if(!(ptr = getcwd(buf, sz)) && errno != ERANGE) { 562 perror("getcwd"); /* Unforeseen error */ 563 goto error; 564 } 565 } 566 567 exit: 568 return buf; 569 error: 570 if(buf) { free(buf); buf = NULL; } 571 goto exit; 572 } 573 574 static void 575 print_head(const char* root, const char* label, const char* lang) 576 { 577 assert(root && label && label); 578 579 printf("<!DOCTYPE html>\n"); 580 printf("<html lang=\"%s\">\n", lang); 581 printf("<head>\n"); 582 printf(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n"); 583 printf(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"); 584 printf(" <title>%s</title>\n", label); 585 printf(" <link rel=\"stylesheet\" title=\"default\" href=\"%ssty.css\">\n", root); 586 printf("</head>\n"); 587 printf("<body>\n"); 588 } 589 590 static void 591 print_menu1 592 (const char* root, 593 const struct menu* menu, 594 const struct mentry* selected_entry) 595 { 596 struct index index = INDEX_NULL; 597 assert(root && menu && selected_entry); 598 599 printf("<div id=\"menu\">\n"); 600 601 for(int i=0; i < menu->nitems; ++i) { 602 struct stat buf; 603 const struct mentry* entry = menu->items + i; 604 605 606 if(i) printf("  | \n"); 607 608 if(selected_entry == entry) { 609 printf(" %s\n", entry->label); 610 611 } else if(stat(entry->section, &buf) != -1 612 && S_ISDIR(buf.st_mode) 613 && !index_load(&index, entry->section)) { 614 printf(" <a href=\"%s%s/%s\">%s</a>\n", 615 root, entry->section, index.items[0].uri, entry->label); 616 index_release(&index); 617 618 } else if(!strncmp(entry->section, "http://", 7) 619 || !strncmp(entry->section, "https://",8)) { 620 /* The target is an URL */ 621 printf(" <a href=\"%s\">%s</a>\n", entry->section, entry->label); 622 623 } else { 624 /* The target is neither a local directory nor a URL. Let's assume it is 625 * a remote directory, still relative to the current root once the local 626 * content is installed, but managed elsewhere. */ 627 printf(" <a href=\"%s%s\">%s</a>\n", root, entry->section, entry->label); 628 } 629 } 630 631 printf("</div>\n<hr>\n"); 632 } 633 634 /* Return null if an error occurs */ 635 static char* 636 expand_lang(char* template, const char* value) 637 { 638 char* buf = NULL; 639 char* p0 = NULL; 640 char* p1 = NULL; 641 size_t valen = 0; 642 size_t sz = 0; 643 644 assert(template && value); 645 646 valen = strlen(value); 647 648 /* Calculate the size of the expanded string */ 649 for(p0=template; (p1 = strstr(p0, LANG)); p0=p1+SZLANG) { 650 *p1 = '\0'; 651 sz += strlen(p0) + valen; 652 *p1 = LANG[0]; 653 } 654 sz += strlen(p0)/* Rest of the template string */ + 1/* '\0' */; 655 656 /* Allocate the expanded string */ 657 if(!(buf=malloc(sz))) goto error; 658 659 /* Expand the string */ 660 buf[0] = '\0'; 661 for(p0=template; (p1 = strstr(p0, LANG)); p0=p1+SZLANG) { 662 *p1 = '\0'; 663 strcat(buf, p0); 664 strcat(buf, value); 665 *p1 = LANG[0]; 666 } 667 strcat(buf, p0); /* Rest of template string */ 668 669 exit: 670 return buf; 671 error: 672 perror(__func__); 673 if(buf) { free(buf); buf = NULL; } 674 goto exit; 675 } 676 677 static int 678 print_translations 679 (const char* root, 680 const char* section, 681 const struct ientry* entry) 682 { 683 char* l = NULL; 684 char* lang_list = NULL; 685 char* ctx = NULL; 686 int i = 0; 687 int err = 0; 688 assert(entry); 689 690 /* The URI and template are identical, meaning that no translation is 691 * available for this HTML content since its template has no @LANG@ macro */ 692 if(!strcmp(entry->uri, entry->template)) goto exit; 693 694 /* Duplicate the lang list */ 695 if(!(lang_list=strdup(entry->lang_list))) { perror("malloc"); goto error; } 696 697 /* Retrieve the 2 first langs in the list */ 698 l = strtok_r(lang_list, ":", &ctx); 699 l = strtok_r(NULL, ":", &ctx); 700 701 if(!l) goto exit; /* There is only one lang, i.e. there is no translation */ 702 703 704 /* Duplicate the lang list again since it was updated by tokenization */ 705 free(lang_list); 706 if(!(lang_list=strdup(entry->lang_list))) { perror("malloc"); goto error; } 707 708 printf(" <span style=\"float: right;\">\n"); 709 710 for(i=0, l=strtok_r(lang_list, ":", &ctx); l; l=strtok_r(NULL, ":", &ctx)) { 711 712 if(i) printf("  / \n"); 713 i += (i==0); 714 715 if(!strcmp(l, entry->lang)) { 716 /* This is the current translation */ 717 printf(" <span class=\"cur\">%s</span>\n", l); 718 719 } else { 720 char* translation = NULL; 721 722 if(!(translation = expand_lang(entry->template, l))) goto error; 723 printf(" <a href=\"%s%s/%s\">%s</a>\n", root, section, translation, l); 724 free(translation); 725 } 726 } 727 728 printf(" </span>\n"); 729 730 exit: 731 if(lang_list) free(lang_list); 732 return err; 733 error: 734 err = 1; 735 goto exit; 736 } 737 738 static int 739 print_menu2 740 (const char* root, 741 const char* section, 742 const struct index* index, 743 const struct ientry* selected_entry) /* Can be NULL */ 744 { 745 char* uri = NULL; 746 int err = 0; 747 748 assert(root && section && index); 749 750 printf("<div id=\"sub-menu\">\n"); 751 752 for(int i = 0, n = index->nitems; i < n; ++i) { 753 const struct ientry* entry = index->items + i; 754 const char* lang = selected_entry ? selected_entry->lang : entry->lang; 755 756 if(i) printf("  . \n"); 757 758 /* * Resolve the URI language with the selected entry if it exists, or the 759 * current entry if not */ 760 if(!(uri=expand_lang(entry->template, lang))) goto error; 761 762 /* This is the indexed content */ 763 if(selected_entry && !strcmp(uri, selected_entry->uri)) { 764 printf(" <span class=\"cur\">%s</span>\n", selected_entry->label); 765 print_translations(root, section, selected_entry); 766 767 /* The entry links to an http[s] URL */ 768 } else if(!strncmp(entry->uri, "http://", 7) 769 || !strncmp(entry->uri, "https://",8)) { 770 printf(" <a href=\"%s\">%s</a>\n", entry->uri, entry->label); 771 772 /* The entry links to a local web page for the section */ 773 } else { 774 printf(" <a href=\"%s%s/%s\">%s</a>\n", 775 root, section, entry->uri, entry->label); 776 } 777 778 /* Remove elements with the same template for the current label to avoid 779 * duplicates when the indexed content offers multiple translations. Note 780 * that the following loop ensures that the index increment by the main 781 * loop remains valid, i.e., does not skip an entry. */ 782 while(i+1 < n 783 && !strcmp(entry->template, index->items[i+1].template) 784 && !strcmp(entry->label, index->items[i+1].label)) { 785 i += 1; 786 } 787 788 free(uri); 789 uri = NULL; 790 } 791 792 printf("</div>\n"); 793 794 exit: 795 if(uri) free(uri); 796 return err; 797 error: 798 err = 1; 799 goto exit; 800 } 801 802 /******************************************************************************* 803 * The command 804 ******************************************************************************/ 805 int 806 main(int argc, char** argv) 807 { 808 struct menu menu = MENU_NULL; 809 const struct mentry* mentry = NULL; 810 811 struct index index = INDEX_NULL; 812 const struct ientry* ientry = NULL; 813 814 char* path = NULL; /* absolute path */ 815 char* dname = NULL; /* dirname of the absolute path */ 816 char* bname = NULL; /* basename of the absolute path */ 817 char* workdir = NULL; /* path from where the command is run */ 818 char* root = NULL; /* relative path from path to workdir */ 819 char* section = NULL; /* section name */ 820 char* file = NULL; /* path from the section to the file */ 821 822 int res = EXIT_SUCCESS; 823 824 if(argc < 2) { 825 fprintf(stderr, "usage: %s path\n", argv[0]); 826 goto error; 827 } 828 829 g_cmd = argv[0]; 830 831 /* Retrieve the absolute path to the input file */ 832 if(!(path = get_realpath(argv[1]))) goto error; 833 834 /* Extract the directory and file from the input path. Start with the file, as 835 * the dirname function could add a '\0' character to the path to terminate 836 * the directory name instead of copying it to local storage. */ 837 bname = strdup(basename(path)); 838 dname = strdup(dirname(path)); 839 840 /* Still retrieve the absolute path to the input file since it was overwritten 841 * by the basename/dirname calls */ 842 free(path); 843 if(!(path = get_realpath(argv[1]))) goto error; 844 845 /* Get the absolute path of the working directory */ 846 if(!(workdir = get_workdir())) goto error; 847 848 /* Get the section to which the file belongs, i.e. the first subdirectory of 849 * its path relative to the working directory */ 850 if(!(section = get_section(workdir, dname))) goto error; 851 852 /* Get the path to the file relatively to the section */ 853 if(!(file = get_section_path(workdir, section, path))) goto error; 854 855 /* Get the relative path from file to working directory. It is used to 856 * reference the CSS file relatively to the HTML content */ 857 if(!(root = get_relpath(workdir, path))) goto error; 858 859 /* Load the menu file of the working directory */ 860 if(menu_load(&menu, workdir)) goto error; 861 862 /* Load the index file of the section */ 863 if(index_load(&index, section)) goto error; 864 865 /* Find the menu entry corresponding to the section of the input file */ 866 if(!(mentry = menu_find(&menu, section))) goto error; 867 868 /* Find the index entry corresponding to the input file. It may be NULL if the 869 * content is not indexed by the section. */ 870 ientry = index_find(&index, file); 871 872 /* Print the HTML header for the input content */ 873 print_head(root, mentry->label, ientry ? ientry->lang : DEFAULT_LANG); 874 print_menu1(root, &menu, mentry); 875 print_menu2(root, section, &index, ientry); 876 877 printf("<div id=\"content\">\n"); 878 879 exit: 880 menu_release(&menu); 881 index_release(&index); 882 883 if(path) free(path); 884 if(dname) free(dname); 885 if(bname) free(bname); 886 if(workdir) free(workdir); 887 if(root) free(root); 888 if(section) free(section); 889 if(file) free(file); 890 return res; 891 error: 892 res = EXIT_FAILURE; 893 goto exit; 894 }