/* Extract the file name, search for it in known directories * and create a sub-parser to handle it. * Returns False if the file could not be found */ static Bool menu_parser_include_file(WMenuParser parser) { char buffer[MAXLINE]; char *req_filename, *fullfilename, *p; char eot; FILE *fh; if (!menu_parser_skip_spaces_and_comments(parser)) { WMenuParserError(parser, _("no file name found for #include") ); return False; } switch (*parser->rd++) { case '<': eot = '>'; break; case '"': eot = '"'; break; default: WMenuParserError(parser, _("file name must be enclosed in brackets or double-quotes for #define") ); return False; } req_filename = parser->rd; while (*parser->rd) { if (*parser->rd == eot) { *parser->rd++ = '\0'; goto found_end_define_fname; } else { parser->rd++; } } WMenuParserError(parser, _("missing closing '%c' in filename specification"), eot); return False; found_end_define_fname: /* If we're inside a #if sequence, we abort now, but not sooner in * order to keep the syntax check */ if (parser->cond.stack[0].skip) return False; { /* Check we are not nesting too many includes */ WMenuParser p; int count; count = 0; for (p = parser; p->parent_file; p = p->parent_file) count++; if (count > MAX_NESTED_INCLUDES) { WMenuParserError(parser, _("too many nested includes") ); return False; } } /* Absolute paths */ fullfilename = req_filename; if (req_filename[0] != '/') { /* Search first in the same directory as the current file */ p = strrchr(parser->file_name, '/'); if (p != NULL) { int len; len = p - parser->file_name + 1; if (len > sizeof(buffer) - 1) len = sizeof(buffer) - 1; strncpy(buffer, parser->file_name, len); strncpy(buffer+len, req_filename, sizeof(buffer) - len - 1); buffer[sizeof(buffer) - 1] = '\0'; fullfilename = buffer; } } fh = fopen(fullfilename, "rb"); /* Not found? Search in wmaker's known places */ if (fh == NULL) { if (req_filename[0] != '/') { const char *src; int idx; fullfilename = buffer; src = parser->include_default_paths; while (*src != '\0') { idx = 0; if (*src == '~') { char *home = wgethomedir(); while (*home != '\0') { if (idx < sizeof(buffer) - 2) buffer[idx++] = *home; home++; } src++; } while ((*src != '\0') && (*src != ':')) { if (idx < sizeof(buffer) - 2) buffer[idx++] = *src; src++; } buffer[idx++] = '/'; for (p = req_filename; *p != '\0'; p++) if (idx < sizeof(buffer) - 1) buffer[idx++] = *p; buffer[idx] = '\0'; fh = fopen(fullfilename, "rb"); if (fh != NULL) goto found_valid_file; if (*src == ':') src++; } } WMenuParserError(parser, _("could not find file \"%s\" for include"), req_filename); return False; } /* Found the file, make it our new source */ found_valid_file: parser->include_file = menu_parser_create_new(wstrdup(req_filename), fh, parser->include_default_paths); parser->include_file->parent_file = parser; return True; }
char* wexpandpath(char *path) { char *origpath = path; char buffer2[PATH_MAX+2]; char buffer[PATH_MAX+2]; int i; memset(buffer, 0, PATH_MAX+2); if (*path=='~') { char *home; path++; if (*path=='/' || *path==0) { home = wgethomedir(); if (strlen(home) > PATH_MAX) goto error; strcat(buffer, home); } else { int j; j = 0; while (*path!=0 && *path!='/') { if (j > PATH_MAX) goto error; buffer2[j++] = *path; buffer2[j] = 0; path++; } home = getuserhomedir(buffer2); if (!home || strlen(home) > PATH_MAX) goto error; strcat(buffer, home); } } i = strlen(buffer); while (*path!=0 && i <= PATH_MAX) { char *tmp; if (*path=='$') { int j = 0; path++; /* expand $(HOME) or $HOME style environment variables */ if (*path=='(') { path++; while (*path!=0 && *path!=')') { if (j > PATH_MAX) goto error; buffer2[j++] = *(path++); buffer2[j] = 0; } if (*path==')') { path++; tmp = getenv(buffer2); } else { tmp = NULL; } if (!tmp) { if ((i += strlen(buffer2)+2) > PATH_MAX) goto error; buffer[i] = 0; strcat(buffer, "$("); strcat(buffer, buffer2); if (*(path-1)==')') { if (++i > PATH_MAX) goto error; strcat(buffer, ")"); } } else { if ((i += strlen(tmp)) > PATH_MAX) goto error; strcat(buffer, tmp); } } else { while (*path!=0 && *path!='/') { if (j > PATH_MAX) goto error; buffer2[j++] = *(path++); buffer2[j] = 0; } tmp = getenv(buffer2); if (!tmp) { if ((i += strlen(buffer2)+1) > PATH_MAX) goto error; strcat(buffer, "$"); strcat(buffer, buffer2); } else { if ((i += strlen(tmp)) > PATH_MAX) goto error; strcat(buffer, tmp); } } } else { buffer[i++] = *path; path++; } } if (*path!=0) goto error; return wstrdup(buffer); error: errno = ENAMETOOLONG; wsyserror(_("could not expand %s"), origpath); /* FIXME: too many functions handle a return value of NULL incorrectly */ exit(1); }
char * MakeCPPArgs(char *path) { int i; char buffer[MAXLINE], *buf, *line; Visual *visual; char *tmp; line = wmalloc(MAXLINE); *line = 0; i=1; if ((buf=getenv("HOSTNAME"))!=NULL) { if (buf[0]=='(') { wwarning(_("your machine is misconfigured. HOSTNAME is set to %s"), buf); } else putdef(line, " -DHOST=", buf); } else if ((buf=getenv("HOST"))!=NULL) { if (buf[0]=='(') { wwarning(_("your machine is misconfigured. HOST is set to %s"), buf); } else putdef(line, " -DHOST=", buf); } buf = username(); if (buf) putdef(line, " -DUSER="******" -DUID=", getuid()); buf = XDisplayName(DisplayString(dpy)); putdef(line, " -DDISPLAY=", buf); putdef(line, " -DWM_VERSION=", VERSION); visual = DefaultVisual(dpy, DefaultScreen(dpy)); putidef(line, " -DVISUAL=", visual->class); putidef(line, " -DDEPTH=", DefaultDepth(dpy, DefaultScreen(dpy))); putidef(line, " -DSCR_WIDTH=", WidthOfScreen(DefaultScreenOfDisplay(dpy))); putidef(line, " -DSCR_HEIGHT=", HeightOfScreen(DefaultScreenOfDisplay(dpy))); /* put the dir where the menu is being read from to the * search path */ if (path) { tmp = wstrdup(path); buf = strchr(tmp+1, ' '); if (buf) { *buf = 0; } buf = strrchr(tmp, '/'); if (buf) { *buf = 0; /* trunc filename */ putdef(line, " -I", tmp); } wfree(tmp); } /* this should be done just once, but it works this way */ strcpy(buffer, DEF_CONFIG_PATHS); buf = strtok(buffer, ":"); do { char fullpath[MAXLINE]; if (buf[0]!='~') { strcpy(fullpath, buf); } else { char * wgethomedir(); /* home is statically allocated. Don't free it! */ char *home = wgethomedir(); strcpy(fullpath, home); strcat(fullpath, &(buf[1])); } putdef(line, " -I", fullpath); } while ((buf = strtok(NULL, ":"))!=NULL); #undef arg #ifdef DEBUG puts("CPP ARGS"); puts(line); #endif return line; }
char *wexpandpath(const char *path) { const char *origpath = path; char buffer2[PATH_MAX + 2]; char buffer[PATH_MAX + 2]; int i; memset(buffer, 0, PATH_MAX + 2); if (*path == '~') { const char *home; path++; if (*path == '/' || *path == 0) { home = wgethomedir(); if (strlen(home) > PATH_MAX || wstrlcpy(buffer, home, sizeof(buffer)) >= sizeof(buffer)) goto error; } else { int j; j = 0; while (*path != 0 && *path != '/') { if (j > PATH_MAX) goto error; buffer2[j++] = *path; buffer2[j] = 0; path++; } home = getuserhomedir(buffer2); if (!home || wstrlcat(buffer, home, sizeof(buffer)) >= sizeof(buffer)) goto error; } } i = strlen(buffer); while (*path != 0 && i <= PATH_MAX) { char *tmp; if (*path == '$') { int j; path++; /* expand $(HOME) or $HOME style environment variables */ if (*path == '(') { path++; j = 0; while (*path != 0 && *path != ')') { if (j > PATH_MAX) goto error; buffer2[j++] = *(path++); } buffer2[j] = 0; if (*path == ')') { path++; tmp = getenv(buffer2); } else { tmp = NULL; } if (!tmp) { if ((i += strlen(buffer2) + 2) > PATH_MAX) goto error; buffer[i] = 0; if (wstrlcat(buffer, "$(", sizeof(buffer)) >= sizeof(buffer) || wstrlcat(buffer, buffer2, sizeof(buffer)) >= sizeof(buffer)) goto error; if (*(path-1)==')') { if (++i > PATH_MAX || wstrlcat(buffer, ")", sizeof(buffer)) >= sizeof(buffer)) goto error; } } else { if ((i += strlen(tmp)) > PATH_MAX || wstrlcat(buffer, tmp, sizeof(buffer)) >= sizeof(buffer)) goto error; } } else { j = 0; while (*path != 0 && *path != '/') { if (j > PATH_MAX) goto error; buffer2[j++] = *(path++); } buffer2[j] = 0; tmp = getenv(buffer2); if (!tmp) { if ((i += strlen(buffer2) + 1) > PATH_MAX || wstrlcat(buffer, "$", sizeof(buffer)) >= sizeof(buffer) || wstrlcat(buffer, buffer2, sizeof(buffer)) >= sizeof(buffer)) goto error; } else { if ((i += strlen(tmp)) > PATH_MAX || wstrlcat(buffer, tmp, sizeof(buffer)) >= sizeof(buffer)) goto error; } } } else { buffer[i++] = *path; path++; } } if (*path!=0) goto error; return wstrdup(buffer); error: errno = ENAMETOOLONG; werror(_("could not expand %s"), origpath); return NULL; }