/* --------------------------------------------------------------------------- * writes to pipe using the command defined by Settings.SaveCommand * %f are replaced by the passed filename */ static int WritePipe (char *Filename, bool thePcb) { FILE *fp; int result; char *p; static DynamicStringType command; int used_popen = 0; if (EMPTY_STRING_P (Settings.SaveCommand)) { fp = fopen (Filename, "w"); if (fp == 0) { Message ("Unable to write to file %s\n", Filename); return STATUS_ERROR; } } else { used_popen = 1; /* setup commandline */ DSClearString (&command); for (p = Settings.SaveCommand; *p; p++) { /* copy character if not special or add string to command */ if (!(*p == '%' && *(p + 1) == 'f')) DSAddCharacter (&command, *p); else { DSAddString (&command, Filename); /* skip the character */ p++; } } DSAddCharacter (&command, '\0'); printf ("write to pipe \"%s\"\n", command.Data); if ((fp = popen (command.Data, "w")) == NULL) { PopenErrorMessage (command.Data); return (STATUS_ERROR); } } if (thePcb) result = WritePCB (fp); else result = WriteBuffer (fp); if (used_popen) return (pclose (fp) ? STATUS_ERROR : result); return (fclose (fp) ? STATUS_ERROR : result); }
int ReadNetlist (char *filename) { static char *command = NULL; char inputline[MAX_NETLIST_LINE_LENGTH + 1]; char temp[MAX_NETLIST_LINE_LENGTH + 1]; FILE *fp; LibraryMenuType *menu = NULL; LibraryEntryType *entry; int i, j, lines, kind; bool continued; bool used_popen = false; int retval = 0; if (!filename) return 1; /* nothing to do */ Message (_("Importing PCB netlist %s\n"), filename); if (EMPTY_STRING_P (Settings.RatCommand)) { fp = fopen (filename, "r"); if (!fp) { Message("Cannot open %s for reading", filename); return 1; } } else { used_popen = true; free (command); command = EvaluateFilename (Settings.RatCommand, Settings.RatPath, filename, NULL); /* open pipe to stdout of command */ if (*command == '\0' || (fp = popen (command, "r")) == NULL) { PopenErrorMessage (command); return 1; } } lines = 0; /* kind = 0 is net name * kind = 1 is route style name * kind = 2 is connection */ kind = 0; while (fgets (inputline, MAX_NETLIST_LINE_LENGTH, fp)) { size_t len = strlen (inputline); /* check for maximum length line */ if (len) { if (inputline[--len] != '\n') Message (_("Line length (%i) exceeded in netlist file.\n" "additional characters will be ignored.\n"), MAX_NETLIST_LINE_LENGTH); else inputline[len] = '\0'; } continued = (inputline[len - 1] == '\\') ? true : false; if (continued) inputline[len - 1] = '\0'; lines++; i = 0; while (inputline[i] != '\0') { j = 0; /* skip leading blanks */ while (inputline[i] != '\0' && BLANK (inputline[i])) i++; if (kind == 0) { /* add two spaces for included/unincluded */ temp[j++] = ' '; temp[j++] = ' '; } while (!BLANK (inputline[i])) temp[j++] = inputline[i++]; temp[j] = '\0'; while (inputline[i] != '\0' && BLANK (inputline[i])) i++; if (kind == 0) { menu = GetLibraryMenuMemory (&PCB->NetlistLib); menu->Name = strdup (temp); menu->flag = 1; kind++; } else { if (kind == 1 && strchr (temp, '-') == NULL) { kind++; menu->Style = strdup (temp); } else { entry = GetLibraryEntryMemory (menu); entry->ListEntry = strdup (temp); } } } if (!continued) kind = 0; } if (!lines) { Message (_("Empty netlist file!\n")); retval = 1; } if (used_popen) pclose (fp); else fclose (fp); sort_netlist (); return retval; }