sedrename.c

00001 /* Sed rename plug-in for PCB
00002 
00003    Copyright (C) 2008 Peter Clifton <pcjc2@cam.ac.uk>
00004 
00005    Licensed under the terms of the GNU General Public License, version
00006    2 or later.
00007 
00008    Compile like this:
00009 
00010    gcc -Wall -I$HOME/pcbsrc/pcb.clean/src -I$HOME/pcbsrc/pcb.clean -O2 -shared sedrename.c -o sedrename.so
00011 
00012    The resulting sedrename.so goes in $HOME/.pcb/plugins/sedrename.so.
00013 
00014 */
00015 
00016 #include <stdio.h>
00017 
00018 #include "global.h"
00019 #include "data.h"
00020 #include "hid.h"
00021 #include "misc.h"
00022 #include "create.h"
00023 #include "error.h"
00024 #include "change.h"
00025 #include "rtree.h"
00026 #include "undo.h"
00027 
00028 static int
00029 sedrename (int argc, char **argv, int x, int y)
00030 {
00031   FILE *fp;
00032   int result = STATUS_OK;
00033   char *sed_prog = "sed ";
00034   char *sed_postfix = " > sedrename.tmp";
00035   char *sed_arguments;
00036   char *sed_cmd;
00037 
00038   static char *last_argument = NULL;
00039 
00040   if (last_argument == NULL)
00041     last_argument = strdup ("s///");
00042 
00043 #if 0
00044   if (argc != 1) {
00045     Message("Usage: sedrename sed_expression");
00046     return 1;
00047   }
00048 
00049   sed_arguments = argv[0];
00050 #else
00051   if (argc != 0) {
00052     Message("Usage: sedrename");
00053     return 1;
00054   }
00055 
00056   sed_arguments = gui->prompt_for ("sedrename", last_argument);
00057   if (sed_arguments == NULL) {
00058     Message("Got null for sed arguments");
00059     return 1;
00060   }
00061   free (last_argument);
00062   last_argument = sed_arguments;
00063 #endif
00064 
00065   sed_cmd = (char *) malloc (strlen (sed_prog) + strlen (sed_arguments) + strlen (sed_postfix) + 1);
00066   memcpy (sed_cmd, sed_prog, strlen (sed_prog) + 1);
00067   strcpy (sed_cmd + strlen (sed_prog), sed_arguments);
00068   strcpy (sed_cmd + strlen (sed_prog) + strlen (sed_arguments), sed_postfix);
00069 
00070   printf ("Calling sed: '%s'\n", sed_cmd);
00071 
00072   if ((fp = popen (sed_cmd, "w")) == NULL) {
00073     PopenErrorMessage (sed_cmd);
00074     free (sed_cmd);
00075     return STATUS_ERROR;
00076   }
00077 
00078   free (sed_cmd);
00079 
00080   SET_FLAG (NAMEONPCBFLAG, PCB);
00081 
00082   ELEMENT_LOOP (PCB->Data);
00083   {
00084 
00085     if (!TEST_FLAG (SELECTEDFLAG, element))
00086       continue;
00087 
00088     /* BADNESS.. IF SED DOESN'T MATCH OUTPUT LINE FOR LINE, WE STALL */
00089     fprintf (fp, "%s\n", element->Name[1].TextString);
00090   }
00091   END_LOOP;
00092 
00093   if (pclose (fp))
00094    return STATUS_ERROR;
00095 
00096   if ((fp = fopen ("sedrename.tmp", "rb")) == NULL) {
00097     Message("Cannot open sedrename.tmp for reading");
00098     return STATUS_ERROR;
00099   }
00100 
00101   ELEMENT_LOOP (PCB->Data);
00102   {
00103     char *new_ref;
00104 
00105     if (!TEST_FLAG (SELECTEDFLAG, element))
00106       continue;
00107 
00108     if (fscanf (fp, "%as", &new_ref) != EOF) {
00109       printf ("Send '%s', got '%s'\n", element->Name[1].TextString, new_ref);
00110 
00111       AddObjectToChangeNameUndoList (ELEMENT_TYPE, NULL, NULL,
00112                                      element,
00113                                      NAMEONPCB_NAME (element));
00114 
00115       ChangeObjectName (ELEMENT_TYPE, element, NULL, NULL, new_ref);
00116     }
00117   }
00118   END_LOOP;
00119 
00120   gui->invalidate_all ();
00121 
00122   IncrementUndoSerialNumber ();
00123 
00124   return (fclose (fp) ? STATUS_ERROR : result);
00125 }
00126 
00127 static HID_Action sedrename_action_list[] = {
00128   {"sedrename", NULL, sedrename,
00129    NULL, NULL}
00130 };
00131 
00132 REGISTER_ACTIONS (sedrename_action_list)
00133 
00134 void
00135 pcb_plugin_init()
00136 {
00137   register_sedrename_action_list();
00138 }
00139 

Generated on Tue Aug 17 15:28:04 2010 for pcb-plugins by  doxygen 1.4.6-NO